[Bins] [patch] make each generated jpeg progressive

Bill Clarke llib at computer.org
Mon Sep 29 13:25:52 BST 2003


attached is a patch to bins 1.1.23 to make each generated jpeg
progressive (instead of baseline) using jpegtran.
usually progressive jpegs save space, but by default in this patch it
only makes them progressive if it does save space.

the jpegProgressify config option is either "never" (no change to
previous behaviour), "always" or "smaller" where i've set the default to
"smaller".  in my limited testing, only the larger images (often the
original) save space with making it progressive.

in the past libraries that could read progressive jpeg were limited, but
i don't believe that to be the case now (i.e., you'd be very unlucky if
something couldn't read a progressive jpeg).

cheers,
/lib
-- 
/lib BillClarke PostdoctoralFellow CompSci ANU cs.anu.edu.au/CC-NUMA
http://llib.cjb.net llib at computer.org  tel:+61-2-6125x5687 fax:x0010
PGPid:B381EE7DB7D3E58F17248C672E2DA124ADADF444 GNU unix LaTeX XPilot
Buffy DrWho Goodies StarTrek XFiles Origami SML SMP MPI mozilla tcsh
Asimov Bear Clarke Donaldson Volleyball Ultimate Cricket emacs C++ X
Jordan Kay Lackey Martin Stasheff DeepPurple H&C KLF Queen PinkFloyd
-------------- next part --------------
diff -ur bins-1.1.23/bins bins-1.1.23.lib/bins
--- bins-1.1.23/bins	Tue Sep  9 21:54:55 2003
+++ bins-1.1.23.lib/bins	Mon Sep 29 12:14:05 2003
@@ -152,6 +152,12 @@
    jpegQuality => 75,         # Quality of scaled jpegs (lower number = more
                               # compression, lower quality) in 1-100 range.
 
+   jpegProgressify => "smaller", # values: never, always, smaller.  whether
+				 # to make jpegs progressive using jpegtran
+				 # (if available).  smaller means only if
+				 # the progressive file is smaller than the
+				 # original
+
    titleOnThumbnail => 1,     # Should the title be displayed on top on the
                               # thumbnail in the thumbnails page ?
 
@@ -2572,6 +2578,7 @@
       # Perform a rotation of the picture if needed
       if (rotateImage($dest, $imageData->{'Orientation'}, "", $configHash)
 	  == 1) {
+	progressifyImage($dest, "", $configHash);
 	# swap width & height
 	my $numsizes = $#{$configHash->{scaledWidths}};
 	for(my $j=0; $j<=$numsizes; $j++) {
@@ -3022,9 +3029,11 @@
       # Perform a rotation of the picture if needed
       if (rotateImage("$albumdir$newName", $imageRef->{'Orientation'}, "",
 		      $configHash) == 1){
+	progressifyImage("$albumdir$newName", "", $configHash);
 	return ($newHeight, $newWidth);
       }
     }
+    progressifyImage("$albumdir$newName", "", $configHash);
     return ($newWidth, $newHeight);
   }
   ($newWidth, $newHeight) = imgsize($albumdir.$newName);
@@ -3879,6 +3888,7 @@
       # orientation tag if the image was rotated
       if(rotateImage($pictureFile, $exifHash->{Orientation},
 		     $exifHash->{file_ext}, $configHash)){
+	progressifyImage("$pictureFile", $exifHash->{file_ext}, $configHash);
 	push @{$priorityList}, "Orientation";
 	$exifHash->{Orientation} = "top_left";
       }
@@ -4371,6 +4381,106 @@
   }
   return 0;
 }
+
+
+BEGIN {
+  my $progressifyJPEG="none";
+  sub progressifyJPEGCommand{
+    my $filein = shift;
+    my $fileout = shift;
+    my $verbose = shift;
+
+    if ($progressifyJPEG eq "none") {
+      beVerbose("\n  Looking for a progressive JPEG utility (jpegtran)... ", 3);
+      if (commandAvailable("jpegtran")) {
+	$progressifyJPEG = 'jpegtran -copy all -progressive -outfile "%s" "%s"';
+	beVerboseN(" found jpegtran.", 3);
+      } else {
+	$progressifyJPEG = "";
+	beVerboseN(" not found, cannot make JPEGs progressive", 3);
+      }
+    }
+
+    if ($progressifyJPEG) {
+      $filein =~ s|\\|\\\\|g;
+      $filein =~ s|\$|\\\$|g;
+      $filein =~ s|"|\\"|g;
+      $filein =~ s|`|\\`|g;
+      $fileout =~ s|\\|\\\\|g;
+      $fileout =~ s|\$|\\\$|g;
+      $fileout =~ s|"|\\"|g;
+      $fileout =~ s|`|\\`|g;
+      return sprintf($progressifyJPEG, $fileout, $filein);
+    }
+    return "";
+  }
+}
+
+# fileSizeCmp(file1, file2): return file1.file-size - file2.file-size
+sub fileSizeCmp {
+  my $file1 = shift;
+  my $file2 = shift;
+  return ((stat($file1))[7]) - ((stat($file2))[7]);
+}
+
+# progressifyJPEGImage(imageName, configHash)
+# make a JPEG image progressive. Return 0 if no conversion was performed, and 1 if a
+# conversion was performed
+sub progressifyJPEGImage{
+  my $imageName = shift;
+  my $configHash = shift;
+  my $tempFile = "$imageName.tmp";
+
+  beVerbose("     Making $imageName progressive JPEG... ", 2);
+
+  my $command = progressifyJPEGCommand($imageName, $tempFile, $verbose);
+
+  if ($command) {
+    beVerbose("\n      Running '$command'... ", 3);
+    if(!system($command)){
+      if (($configHash->{jpegProgressify} eq "always")) {
+	rename($tempFile, $imageName)
+	  or die("\nfailed to rename $tempFile to $imageName: $?");
+	beVerboseN("OK", 2);
+	return 1;
+      }
+      my $diff = fileSizeCmp($tempFile, $imageName);
+      if ($diff < 0) {
+	rename($tempFile, $imageName)
+	  or die("\nfailed to rename $tempFile to $imageName: $?");
+	$diff = -$diff;
+	beVerbose("$diff bytes smaller; ", 3);
+	beVerboseN("OK", 2);
+	return 1;
+      } else {
+	beVerbose("$diff bytes larger; ", 3);
+	beVerboseN("NO", 2);
+	unlink($tempFile) or warn("\nfailed to unlink $tempFile: $?");
+      }
+    }
+  }else{
+    beVerboseN("impossible.\n  Cannot make progressive JPEG, no utility installed.",
+	       2);
+  }
+  return 0;
+}
+
+# progressifyImage(imageName, ext, configHash)
+# make a JPEG image progressive. Return 0 if no conversion was performed, and 1 if a
+# conversion was performed.  Skips non JPEG images or when jpegProgressify
+# is false (returns 0).
+sub progressifyImage{
+  my $imageName = shift;
+  my $ext = shift;
+  my $configHash = shift;
+
+  my $type = $ext ? $ext : "jpg";
+  if (($type eq "jpg") && ($configHash->{jpegProgressify} ne "never")) {
+    return progressifyJPEGImage($imageName, $configHash);
+  }
+  return 0;
+}
+
 
 
 sub readConfigFile{
diff -ur bins-1.1.23/binsrc bins-1.1.23.lib/binsrc
--- bins-1.1.23/binsrc	Tue Sep  9 21:54:54 2003
+++ bins-1.1.23.lib/binsrc	Mon Sep 29 12:10:34 2003
@@ -139,6 +139,13 @@
     75
   </parameter>
 
+  <!-- whether to convert generated jpegs to progressive using
+  jpegtran (if available).  can be never, always, or smaller (if the
+  progressified file is smaller than the baseline). --> 
+  <parameter name="jpegProgressify">
+    smaller
+  </parameter>
+
   <!-- Should the title be displayed on top on the thumbnail in the
   thumbnails page ? (1 = yes, 0 = no)-->
   <parameter name="titleOnThumbnail">
@@ -473,4 +480,4 @@
       #FFFFFF
     </color>
   </colors>
-</bins>
\ No newline at end of file
+</bins>


More information about the Bins mailing list