Bump version number
[bookreader.git] / GnuBookIA / datanode / GnuBookImages.php
index a8c16ea..d1c8485 100644 (file)
@@ -1,8 +1,4 @@
 <?php
-/* This file is deprecated -- image stack processing has been 
- * combined into GnuBookImages.php.  This file will eventually
- * be removed.
- */
 
 /*
 Copyright(c)2008 Internet Archive. Software license AGPL version 3.
@@ -51,8 +47,16 @@ if (isset($_REQUEST['ext'])) {
 
 $fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
 
-// Png conversion options
+// Rotate is currently only supported for jp2 since it does not add server load
+$allowedRotations = array("0", "90", "180", "270");
+$rotate = $_REQUEST['rotate'];
+if ( !in_array($rotate, $allowedRotations) ) {
+    $rotate = "0";
+}
+
+// Image conversion options
 $pngOptions = '';
+$jpegOptions = '-quality 75';
 
 // The pbmreduce reduction factor produces an image with dimension 1/n
 // The kakadu reduction factor produceds an image with dimension 1/(2^n)
@@ -61,29 +65,35 @@ if (isset($_REQUEST['height'])) {
     $ratio = floatval($_REQUEST['origHeight']) / floatval($_REQUEST['height']);
     if ($ratio <= 2) {
         $scale = 2;
-        $reduce = 1;    
+        $powReduce = 1;    
     } else if ($ratio <= 4) {
         $scale = 4;
-        $reduce = 2;
+        $powReduce = 2;
     } else {
-        //$reduce = 3; //too blurry!
+        //$powReduce = 3; //too blurry!
         $scale = 2;
-        $reduce = 1;
+        $powReduce = 1;
     }
 
 } else {
     $scale = $_REQUEST['scale'];
     if (1 >= $scale) {
         $scale = 1;
-        $reduce = 0;
+        $powReduce = 0;
     } else if (2 == $scale) {
-        $reduce = 1;
+        $powReduce = 1;
     } else if (4 == $scale) {
-        $reduce = 2;
+        $powReduce = 2;
+    } else if (8 == $scale) {
+        $powReduce = 3;
+    } else if (16 == $scale) {
+        $powReduce = 4;
+    } else if (32 == $scale) {
+        $powReduce = 5;
     } else {
-        // $$$ why do we default to such a small scale?
+        // $$$ Leaving this in as default though I'm not sure why it is...
         $scale = 8;
-        $reduce = 3;
+        $powReduce = 3;
     }
 }
 
@@ -101,10 +111,11 @@ $unzipCmd  = 'unzip -p ' .
         
 if ('jp2' == $fileExt) {
     $decompressCmd = 
-        " | /petabox/sw/bin/kdu_expand -no_seek -quiet -reduce $reduce -i /dev/stdin -o " . $stdoutLink;
+        " | /petabox/sw/bin/kdu_expand -no_seek -quiet -reduce $powReduce -rotate $rotate -i /dev/stdin -o " . $stdoutLink;
     if ($decompressToBmp) {
         $decompressCmd .= ' | bmptopnm ';
     }
+    
 } else if ('tif' == $fileExt) {
     // We need to create a temporary file for tifftopnm since it cannot
     // work on a pipe (the file must be seekable).
@@ -113,7 +124,11 @@ if ('jp2' == $fileExt) {
     $tempFile = tempnam("/tmp", "GnuBookTiff");
     
     if (1 != $scale) {
-        $pbmReduce = ' | pbmreduce -threshold ' . $scale;
+        if (onPowerNode()) {
+            $pbmReduce = ' | pnmscale -reduce ' . $scale;
+        } else {
+            $pbmReduce = ' | pnmscale -nomix -reduce ' . $scale;
+        }
     } else {
         $pbmReduce = '';
     }
@@ -131,9 +146,9 @@ if ('jp2' == $fileExt) {
 // }
 
 if ('jpg' == $ext) {
-    $compressCmd = ' | pnmtojpeg -quality 90';
+    $compressCmd = ' | pnmtojpeg ' . $jpegOptions;
 } else if ('png' == $ext) {
-    $compressCmd = ' | pnmtopng $pngOptions';
+    $compressCmd = ' | pnmtopng ' . $pngOptions;
 }
 
 $cmd = $unzipCmd . $decompressCmd . $compressCmd;
@@ -154,5 +169,20 @@ function GBFatal($string) {
     die(-1);
 }
 
+// Returns true if using a power node
+function onPowerNode() {
+    exec("lspci | fgrep -c Realtek", $output, $return);
+    if ("0" != $output[0]) {
+        return true;
+    } else {
+        exec("egrep -q AMD /proc/cpuinfo", $output, $return);
+        if ($return == 0) {
+            return true;
+        }
+    }
+    return false;
+}
+
+
 ?>