#!/usr/bin/perl -w

# because imagemagick scales images better than bdsup2sub does

use POSIX;

if($#ARGV!=1) {
  print "Usage: perl convert_res.pl <newres> <dir>\n";
  exit;
}
my $newres=$ARGV[0];
my $sourcedir=$ARGV[1];
my $res_y=1080;
my $newres_y=0;

$newres=lc($newres);
if($newres eq "720" || $newres eq "1280x720") {
  $newres="720p";
}

if($newres eq "720p") {
  $newres_y=720;
}

if($newres ne "720p") {
  print "Error: Unsupported resolution: $newres\n";
  exit;
}

if($^O eq "MSWin32") {
  @filelist=glob("\"$sourcedir\\\*.xml\"");
} else {
  @filelist=glob("\"$sourcedir/\*.xml\"");
}
my $listsize=@filelist;
if($listsize==0) {
  print "Error: No XML files found in $sourcedir\n";
  exit;
}

sub round {
  $_[0] > 0 ? int($_[0] + .5) : -int(-$_[0] + .5)
}

FILELOOP: for $sourcefile (@filelist) {
  my $graphic=0;
  my $y=0;
  my $height;
  my $token1="";
  my $token2="";
  my $newimgwidth=0;
  my $newimgheight=0;
  my $imagetype="";
  my @arr;
  my $image_path="";
  if($^O eq "MSWin32") {
    @arr=split(/\\/,$sourcefile);
  } else {
    @arr=split(/\//,$sourcefile);
  }
  my $fname=$arr[-1];
  print "Processing ".$fname."...\n";
  open(OLDXML, "<".$sourcefile);
  if($^O eq "MSWin32") {
    open(NEWXML, '>' . $sourcedir . '\\_' . $fname);
  } else {
    open(NEWXML, '>' . $sourcedir . '/_' . $fname);
  }
  foreach my $line (<OLDXML>) {
    if (index($line, '<Graphic') != -1) {
      $graphic=1;
    }
    if($graphic==1) {
      if (index($line, ".png") != -1) {
        my $newy=$y;
        @arr=split(/>/,$line);
        $sub_image=$arr[1];
        @arr=split(/</,$sub_image);
        $sub_image=$arr[0];
          if($^O eq "MSWin32") {
            $image_path=$sourcedir."\\".$sub_image;
          } else {
            $image_path=$sourcedir."/".$sub_image;
          }
          if(length($imagetype)==0) {
            $imagetype="grayscalealpha";
            if($^O eq "MSWin32") {
              system("identify -format %[type] $image_path > ".$sourcedir."\\_dims.txt");
              open(DIMS, "<".$sourcedir."\\_dims.txt");
            } else {
              system("identify -format %[type] $image_path > ".$sourcedir."/_dims.txt");
              open(DIMS, "<".$sourcedir."/_dims.txt");
            }
            $dims=<DIMS>;
            close(DIMS);
            if(index(lc($dims),"grayscalealpha")==-1) {
              $imagetype="palettealpha";
            }
            if($^O eq "MSWin32") {
              unlink($sourcedir."\\_dims.txt");
            } else {
              unlink($sourcedir."\\_dims.txt");
            }
          }
          system("convert $image_path -resize ".100*$newres_y/$res_y."% -type $imagetype $image_path");
          if($^O eq "MSWin32") {
            system("identify -format %wx%h $image_path > ".$sourcedir."\\_dims.txt");
            open(DIMS, "<".$sourcedir."\\_dims.txt");
          } else {
            system("identify -format %wx%h $image_path > ".$sourcedir."/_dims.txt");
            open(DIMS, "<".$sourcedir."/_dims.txt");
          }
          $dims=<DIMS>;
          close(DIMS);
          if($^O eq "MSWin32") {
            unlink($sourcedir."\\_dims.txt");
          } else {
            unlink($sourcedir."\\_dims.txt");
          }
          @arr=split(/x/,$dims);
          $newimgwidth=$arr[0];
          $newimgheight=$arr[1];
        }
        if (index($line, "Width=") != -1) {
          @arr=split(/Width="/,$line);
          $imgwidth=$arr[1];
          @arr=split(/"/,$imgwidth);
          $imgwidth=$arr[0];
          $token1="Width=\"".$imgwidth;
          $token2="Width=\"".$newimgwidth;
          $line=~s/$token1/$token2/;
        }
        if (index($line, "Height=") != -1) {
          @arr=split(/Height="/,$line);
          $imgheight=$arr[1];
          @arr=split(/"/,$imgheight);
          $imgheight=$arr[0];
          $token1="Height=\"".$imgheight;
          $token2="Height=\"".$newimgheight;
          $line=~s/$token1/$token2/;
        }
        if (index($line, "X=") != -1) {
          @arr=split(/X="/,$line);
          $x=$arr[1];
          @arr=split(/"/,$x);
          $x=$arr[0];
          $token1="X=\"".$x;
          $token2="X=\"".round($x*$newres_y/$res_y);
          $line=~s/$token1/$token2/;
        }
        if (index($line, "Y=") != -1) {
          @arr=split(/Y="/,$line);
          $y=$arr[1];
          @arr=split(/"/,$y);
          $y=$arr[0];
          $token1="Y=\"".$y;
          $token2="Y=\"".round($y*$newres_y/$res_y);
          $line=~s/$token1/$token2/;
        }
    } else {
      if (index($line, "VideoFormat") != -1) {
        if (index($line, "1080p") != -1) {
          $res_y=1080;
        }
        if (index($line, "720p") != -1) {
          $res_y=720;
        }
        if (index($line, "480i") != -1) {
          $res_y=480;
        }
        if (index($line, "576i") != -1) {
          $res_y=576;
        }
        $token1="VideoFormat=\"".$res_y;
        if($res_y>700) {
          $token1=$token1."p";
        } else {
          $token1=$token1."i";
        }
        $token2="VideoFormat=\"".$newres;
        $line=~s/$token1/$token2/;
      }
    }
    print NEWXML $line;
  }
  close(OLDXML);
  close(NEWXML);
  if($^O eq "MSWin32") {
    rename($sourcedir . '\\_' . $fname,$sourcedir . '\\' . $fname);
  } else {
    rename($sourcedir . '/_' . $fname,$sourcedir . '/' . $fname);
  }
}

print "Process complete. Subtitles in $sourcedir have been converted to $newres.\n";
