Author: Alexander Zangerl <az@debian.org>
Subject: replace imagemagick (and tons of forks) with image::imlib and ::size

diff -urNad '--exclude=CVS' '--exclude=.svn' '--exclude=.git' '--exclude=.arch' '--exclude=.hg' '--exclude=_darcs' '--exclude=.bzr' igal2-2.1~/igal2 igal2-2.1/igal2
--- igal2-2.1~/igal2	2012-05-30 21:38:23.559841252 +1000
+++ igal2-2.1/igal2	2012-05-30 21:39:30.551221803 +1000
@@ -58,6 +58,8 @@
 use FileHandle;
 use Getopt::Long;
 use POSIX qw(ceil floor);
+use Image::Imlib2;
+use Image::Size qw(:all);
 # Check if EXIF Tool library is installed
 $ExifTool = eval "use Image::ExifTool; 1" ? 1 : 0;
 
@@ -316,16 +318,7 @@
 	print "Found $nfiles image files in directory $opt_d\n";
 }
 
-# find out if the imagemagick commands exist
-$HAVEIM = (&exist("identify") and &exist("convert")) unless ($HAVEIM);
-print "\nWARNING:  The ImageMagick package (imagemagick.org) doesn't seem to be\ninstalled.  Image dimensions will not be available in the generated HTML.\nOnly JPG images are supported... falling back onto cjpeg/djpeg/pnmscale.\n\n" unless ($HAVEIM);
 
-# find out if the libjpeg commands exist
-$HAVELJ = (&exist("cjpeg") and &exist("djpeg") &exist("pnmscale")) unless ($HAVELJ or ($njpg == 0) or $HAVEIM);
-print "\nWARNING:  at least one of the commands \"cjpeg\", \"djpeg\" and \"pnmscale\" is not\ninstalled.  You can find these at rpmfind.net (inside libjpeg and libgr-progs)\nor you can download the source code from:\n    http://www.ijg.org/ and\n    http://netpbm.sourceforge.net/\n\n" unless ($HAVELJ or ($njpg == 0) or $HAVEIM);
-
-# give up if no image processing commands can be found
-die "ERROR:  the necessary image processing tools aren't installed on your system.\nPlease obtain them as specified above.\n\n" unless ($HAVEIM or $HAVELJ);
 
 # initialize the arrays that will hold image file sizes
 @xdim=(); @ydim=(); @isiz=();
@@ -353,51 +346,45 @@
 	$fullfile = "$opt_d/$file";
 	die "Can't open $fullfile\n" unless (-r $fullfile);
 	$fullthumb = "$opt_d/$opt_dest$thumbprefix$file";
-	if ((! -e $fullthumb) or $opt_f) {
-		if ($HAVEIM) {
-			$command = "convert $opt_con -scale x$opt_y \"$fullfile\" \"$fullthumb\"";
-			# opt_xy handling: determine dimensions and scale the bigger dimension
-			# Bug fixing code new since version 1.4.7
-			if ($opt_xy) {
-			        $temp = `identify -ping -format \"\%wx\%h %b\" \"$fullfile\"`;
-				$temp =~ m/(\d+)x(\d+)\s([\d\.]+[mkb]*)/i;
-				$width = $1; $height = $2; $temp = $3;
-				if ($width > $height) {
-				    $dimension = "$opt_xy" . "x";
-				} else {
-				    $dimension = "x" . "$opt_xy";
-				}
-				$command = "convert $opt_con -scale $dimension \"$fullfile\" \"$fullthumb\"";
-			}
-		} else {
-			if ($imgext[$i] =~ m/(gif|png)/i) {
-				print "\nIgnoring $file:  get ImageMagick (imagemagick.org) to process GIF and PNG files.\n";
-				$command = 0;
-			} else {
-				$com1 = "djpeg \"$fullfile\"";
-				$com3 = "cjpeg $opt_con -outfile \"$fullthumb\"";
-				$scale = "pnmscale -ysize $opt_y";
-				$scale = "pnmscale -xysize $opt_xy $opt_xy" if ($opt_xy);
-				$command = $com1 . " | " . $scale . " | " . $com3;
-			}
-		}
-		system("$command") if ($command);
-		print "$thumbprefix$file " if ($command);
+	if ((! -e $fullthumb) or $opt_f) 
+	{
+	    my $img;
+	    eval { $img=Image::Imlib2->load($fullfile); };
+	    if ($@)
+	    {
+		print "problem converting $fullfile: $@\n";
+		next;
+	    }
+	    my $width=$img->width;
+	    my $height=$img->height;
+
+	    my @dims=(0,$opt_y);
+	    # opt_xy handling: determine dimensions and scale 
+	    # the bigger dimension
+	    # Bug fixing code new since version 1.4.7
+	    if ($opt_xy) 
+	    {
+		(@dims)=($width>$height?($opt_xy,0):(0,$opt_xy));
+	    }
+	    my $thumb=$img->create_scaled_image(@dims);
+	    $thumb->set_quality(95);
+	    # until bug #595376 is addressed...
+	    $thumb->image_set_format("jpeg");
+	    $thumb->save($fullthumb);
+	    print "$thumbprefix$file " if ($command);
 	}
 }
 print "... done!\n";
 
 # determine image file sizes
-if ($HAVEIM && !($opt_bigy)) {
+if (!($opt_bigy)) {
 	print "Determining image sizes ";
 	foreach $file (@imgfiles) {
 		$fullfile = "$opt_d/$file";
-		$temp = `identify -ping -format \"\%wx\%h %b\" \"$fullfile\"`;
-		$temp =~ m/(\d+)x(\d+)\s([\d\.]+[mkb]*)/i;
-		push(@xdim,$1);  push(@ydim,$2);  $temp = $3;
-		# round nicely in kb if identify returned the size in bytes
-		$temp = sprintf ("%.1fk", $temp/1024) if ($temp =~ s/([\d\.]+)b*$/$1/i);
-		push(@isiz,$temp);
+		(my $width,$height)=imgsize($fullfile);
+		push @xdim,$width;
+		push @ydim,$height;
+		push(@isiz,sprintf("%.1fk",(-s $fullfile)/1024));
 		print ".";
 	}
 	print " done!\n";
@@ -405,41 +392,34 @@
 
 # scale down slides if the --bigy <n> option was given
 if ($opt_bigy and !($opt_s)) {
-	if ($HAVEIM) {
-		print "Scaling down big slides and determining new slide sizes:  ";
-		@xdim=(); @ydim=(); @isiz=();    # reset image size arrays
-	} else {
-		print "Scaling down big slides:  ";
-	}
+    print "Scaling down big slides and determining new slide sizes:  ";
+    @xdim=(); @ydim=(); @isiz=();    # reset image size arrays
 	for ($i = 0; $i < $nfiles; $i++) {
 		$file = $imgfiles[$i];
 		$fullfile = "$opt_d/$file";
 		die "Can't open $fullfile\n" unless (-r $fullfile);
 		$fullslide = "$opt_d/$opt_dest$slideprefix$file";
-		if ((! -e $fullslide) or $opt_f) {
-			if ($HAVEIM) {
-				$command = "convert $opt_con -scale x$opt_bigy \"$fullfile\" \"$fullslide\"";
-			} else {
-				if ($imgext[$i] =~ m/(gif|png)/i) {
-					print "\nIgnoring $file:  get ImageMagick (imagemagick.org) to process GIF and PNG files.\n";
-					$command = 0;
-				} else {
-					$com1 = "djpeg \"$fullfile\"";
-					$com3 = "cjpeg $opt_con -outfile \"$fullslide\"";
-					$scale = "pnmscale -ysize $opt_bigy";
-					$command = $com1 . " | " . $scale . " | " . $com3;
-				}
-			}
-			system("$command") if ($command);
-			print "$slideprefix$file " if ($command);
+		if ((! -e $fullslide) or $opt_f) 
+		{
+		    my $img=Image::Imlib2->load($fullfile);
+		    my $width=$img->width;
+		    my $height=$img->height;
+
+		    my $slide=$img->create_scaled_image(0,$opt_bigy);
+		    $slide->set_quality(95);
+		    $slide->save($fullslide);
+		    print "$slideprefix$file ";
+
+		    push(@xdim,$slide->width);  
+		    push(@ydim,$slide->height);
+		    push(@isiz,sprintf("%.1fk",(-s $fullslide)/1024));
 		}
-		if ($HAVEIM) {
-			$temp = `identify -ping -format \"\%wx\%h %b\" \"$fullslide\"`;
-			$temp =~ m/(\d+)x(\d+)\s([\d\.]+[mkb]*)/i;
-			push(@xdim,$1);  push(@ydim,$2);  $temp = $3;
-			# round nicely in kb if identify returned the size in bytes
-			$temp = sprintf ("%.1fk", $temp/1024) if ($temp =~ s/([\d\.]+)b*$/$1/i);
-			push(@isiz,$temp);
+		else
+		{
+		    (my $width,$height)=imgsize($fullslide);
+		    push @xdim,$width;
+		    push @ydim,$height;
+		    push(@isiz,sprintf("%.1fk",(-s $fullslide)/1024));
 		}
 	}
 	print "... done!\n";
@@ -528,18 +508,12 @@
 		$imgfile_i_encoded = URLencode($imgfiles[$i]);
 		if ($opt_bigy) {
 			$slide = URLencode($slideprefix . $imgfiles[$i]);
-			if ($HAVEIM) {
-				$workarea =~ s/(<.*)<!--THIS-IMAGE-->(.)(.*>)/<A href=\"$destback$imgfile_i_encoded\">$1$slide$2 width=$xdim[$i] height=$ydim[$i] alt=\"$title\" title=\"click to see full-size\"$3<\/a>/;
-			} else {
-				$workarea =~ s/(<.*)<!--THIS-IMAGE-->(.)(.*>)/<A href=\"$destback$imgfile_i_encoded\">$1$slide$2 alt=\"$title\" title=\"click to see full-size\"$3<\/a>/;
-			}
+			$workarea =~ s/(<.*)<!--THIS-IMAGE-->(.)(.*>)/<A href=\"$destback$imgfile_i_encoded\">$1$slide$2 width=$xdim[$i] height=$ydim[$i] alt=\"$title\" title=\"click to see full-size\"$3<\/a>/;
+			
 		} else {
-			if ($HAVEIM) {
+			
 				$workarea =~ s/<!--THIS-IMAGE-->(.)/$destback$imgfile_i_encoded$1 width=$xdim[$i] height=$ydim[$i] alt=\"$title\"/g;
-			} else {
-				$workarea =~ s/<!--THIS-IMAGE-->/$destback$imgfile_i_encoded/g;
-			}
-		}
+			    }
 		
 		# add in the image counter unless -x is specified
 		if ($opt_x) {
@@ -623,10 +597,15 @@
 	print "Found $opt_dest.$itile file ... using it.\n";
 }
 # rescale the tiled image if the -t switch was called but -r wasn't
-if (($HAVEIM) and (! $opt_r)) {
-	$tileh = `identify -ping -format \"\%wx\%h %b\" \"$opt_d\/$opt_dest.$itile\"`;
-	$tileh =~ s/\d+x(\d+)\s[\d\.]+./$1/;
-	system("mogrify -scale x$opt_t $opt_d/$opt_dest.$itile") if ($opt_t != $tileh);
+if (!$opt_r) {
+    my (undef,$tileh)=imgsize("$opt_d/$opt_dest.$itile");
+    if ($tileh != $opt_t)
+    {
+	my $img=Image::Imlib2->load("$opt_d/$opt_dest.$itile");
+	my $resized=$img->create_scaled_image(0,$opt_t);
+	$resized->set_quality(95);
+	$resized->save("$opt_d/$opt_dest.$itile");
+    }
 }
 
 if (! -e "$opt_d/$opt_dest.$indextemplate") {
@@ -708,18 +687,16 @@
 			$altname = $captions[$fileindex];
 			$thumb = $opt_dest . $thumbprefix . $imgfiles[$fileindex];
 			$thumbEncoded = $opt_dest . URLencode($thumbprefix . $imgfiles[$fileindex]);
-			if ($HAVEIM) {
-				$temp = `identify -ping -format \"\%wx\%h\" \"$opt_d/$thumb\"`;
-				$temp =~ m/(\d+)x(\d+)/;  $x=$1;  $y=$2;
-				$output .= "<IMG src=\"" . $thumbEncoded . "\" width=$x height=$y alt=\"$altname\" title=\"$altname\" border=0>";
-			} else {
-				$output .= "<IMG src=\"" . $thumbEncoded . "\" alt=\"$altname\" title=\"$altname\" border=0>";
-			}
+
+			my $size=html_imgsize("$opt_d/$thumb");
+			$output .= "<IMG src=\"" . $thumbEncoded 
+			    . "\" $size alt=\"$altname\" title=\"$altname\" border=0>";
+
 			$output .= "</A></TD>\n";
 		}
 		$output .= "        <TD class=\"thumb\">&nbsp;</TD>\n    </TR>\n";
 		$output .= "    <TR><TD class=\"tiled\" height=$opt_t colspan=" . ($columns+2) . ">&nbsp;</TD></TR>\n" unless ($opt_r);
-		if (($opt_a or $opt_ad or $opt_as) and $HAVEIM) {
+		if ($opt_a or $opt_ad or $opt_as) {
 			$output .= "    <TR>\n      <TD>&nbsp;</TD>\n";
 			for ($j = 1; $j <= $columns; $j++) {
 				$fileindex = ($page * $paginate) + (($i-1)*$opt_w+$j-1);	# calculate which file is in use
