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

--- a/igal2
+++ b/igal2
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 
 ############################################################################
-# Please change this accordingly if you change the DESTDIR in the tarball 
+# Please change this accordingly if you change the DESTDIR in the tarball
 # distribution Makefile or the --prefix of the RPM installation
 $LIBDIR = "/usr/share/igal2";
 # for example if you make DESTDIR=/usr then $LIBDIR="/usr/lib/igal2"
@@ -58,6 +58,8 @@ $HAVELJ = 0;
 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;
 
@@ -139,14 +141,14 @@ Options:     -a           write image si
              --www        make all iGal files world-readable
              --xy <n>     scale thumbs to <n> pixels in their longer dimension
              ------------- N E W   F U N C T I O N S --------------------------
-	     --dest <dir> Destination directory for the helper files 
+	     --dest <dir> Destination directory for the helper files
 			  (thumbnails, slides, etc.) igal generates. This is
 			  relative to the operative directory -d and has to be
 			  one level below (.)
 	     --AddSubdir  If igal finds further directories beneath your image
-	     	          directory -d, it will generate HTML Links in the 
-			  index file (index.html) to this directories. 
-		 	  Default is (off). Directories given with --dest or 
+	     	          directory -d, it will generate HTML Links in the
+			  index file (index.html) to this directories.
+		 	  Default is (off). Directories given with --dest or
 			  starting with a "." will be ignored.
 			  Note: igal will not work recursively, it just adds
                           HTML links to the directories below.
@@ -159,14 +161,14 @@ Options:     -a           write image si
                           size images. Use this if your indexes will be in
                           a different location/server from the back end images
              --pagination <n> Maximum number of images on one page.
-                          If the given number of images is reached a new page 
-                          is started. Pagination number <n> should be a multiple 
+                          If the given number of images is reached a new page
+                          is started. Pagination number <n> should be a multiple
                           of parameter -w (default 5).
                           Default 0 - means no pagination at all.
 Note:         default values are given in parentheses (where applicable).
 Authors:      Wolfgang Trexler <wt-igal\@trexler.at>
               Eric Pop <epop\@stanford.edu> (original igal)
-	      contributions: 
+	      contributions:
               Stewart Addison <contactsxa\@gmail.com>, Riku Kalinen,
               Alexander Zangerl
 URL:          http://igal.trexler.at/
@@ -178,7 +180,7 @@ END_OF_USAGE
 # process command-line arguments (overriding defaults above)
 GetOptions('a','c','C','d=s','e','f','h','i=s','k','m=s','n','o=s',
            'p=i','r','s','t=i','u','w=i','x','y=i','ad','as',
-           'bigy=i', 'con=s','help','im','www','xy=i', 'dest=s', 
+           'bigy=i', 'con=s','help','im','www','xy=i', 'dest=s',
 	   'AddSubdir', 'pagination=s') or die "$usage";
 
 die $usage if ($opt_help or $opt_h);
@@ -198,7 +200,7 @@ die "Please enter a nonnegative paginati
 $opt_o = "$opt_o/" if ( $opt_o );
 
 die "Critical failure: Lib Image::ExifTool not install but EXIF Tags (option -e) requested\n" if (($ExifTool == 0) and ($opt_e));
- 
+
 # strip any unnecessary slashes from the end of the given $opt_d directory
 $opt_d =~ s/\/$//;
 
@@ -316,17 +318,6 @@ if ($opt_c or $opt_C) {
 	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:  commands identify/convert missing!\nThe ImageMagick (imagemagick.org) or GraphicsMagick package with compatibility 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 +344,45 @@ for ($i = 0; $i < $nfiles; $i++) {
 	$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 +390,34 @@ if ($HAVEIM && !($opt_bigy)) {
 
 # 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 ($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);
+		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));
+		}
+		else
+		{
+		    (my $width,$height)=imgsize($fullslide);
+		    push @xdim,$width;
+		    push @ydim,$height;
+		    push(@isiz,sprintf("%.1fk",(-s $fullslide)/1024));
 		}
 	}
 	print "... done!\n";
@@ -528,19 +506,13 @@ if ($opt_s) {
 		$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) {
 			$imagecaption = $captions[$i];
@@ -549,7 +521,7 @@ if ($opt_s) {
 		}
 		$workarea =~ s/<!--IMAGE-CAPTION-->/$imagecaption/g;
 
-		# add EXIF information if -e is specified 
+		# add EXIF information if -e is specified
 		if ($opt_e) {
 		    my $exif=Image::ExifTool::ImageInfo($imgfiles[$i]);
 		    my $exifinfo= "<A href=\"javascript:toggle('exifinfo');\">Show/hide EXIF data</A>\n";
@@ -605,7 +577,7 @@ if ($opt_s) {
 		print SW "$workarea";
 		close(SW);
 	}
-	
+
 	print "... done!\n";
 }
 
@@ -623,10 +595,15 @@ if ($opt_r) {
 	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") {
@@ -689,7 +666,7 @@ for ($page = 0; $page * $paginate < $nfi
 
 	if ($rem > 0) { $rows++; }	# is there a last incomplete row? if yes, we need one more circle for this one
 	for ($i = 1; $i <= $rows; $i++) {
-		# 
+		#
 		$columns = ($i == $rows && $rem > 0) ? $rem : $opt_w; # last row needs less columns
 
 		$output .= "  <TABLE border=0 cellspacing=0 cellpadding=$opt_p>\n";
@@ -708,18 +685,16 @@ for ($page = 0; $page * $paginate < $nfi
 			$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
@@ -759,7 +734,7 @@ for ($page = 0; $page * $paginate < $nfi
 			$nextpagename = "$nextpage$opt_i";
 			$lastpage = $numberpages -1;
 			$tmptext .= "<a href=\"$nextpagename\">&nbsp;&gt;&gt;</a>&nbsp;&nbsp;<a href=\"$lastpage$opt_i\">&gt;|</a>";
-		}		
+		}
 		# backward navigation
 		if ($page > 0) {
 			$prevpage = $page-1;
@@ -824,7 +799,7 @@ sub read_file_in_string {
 	local($filename) = @_;
 
 	open(FILE,$filename) or die "Can't open file $filename [$!]\n";
-	local $/; 
+	local $/;
 	$string = <FILE>;
 	close(FILE);
 
