1-bit detection routines for jp2, tiff, jpeg and png
authorMichael Ang <mang@archive.org>
Fri, 5 Mar 2010 06:12:37 +0000 (06:12 +0000)
committerMichael Ang <mang@archive.org>
Fri, 5 Mar 2010 06:12:37 +0000 (06:12 +0000)
BookReaderIA/datanode/BookReaderImages.php
BookReaderIA/test/unit/Images.js

index 9b6dfe1..4fcda01 100644 (file)
@@ -118,24 +118,46 @@ function getImageInfo($zipPath, $file)
     $fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
     $type = imageExtensionToType($fileExt);
      
-    // $$$ will exiftool work for *all* of our images?
-    // BitsPerComponent present in jp2. Not present in jpeg.
+    // We look for all the possible tags of interest then act on the
+    // ones presumed present based on the file type
+    $tagsToGet = ' -ImageWidth -ImageHeight -FileType'        // all formats
+                 . ' -BitsPerComponent -ColorSpace'          // jp2
+                 . ' -BitDepth'                              // png
+                 . ' -BitsPerSample';                        // tiff
+                        
     $cmd = getUnarchiveCommand($zipPath, $file)
-        . ' | '. $exiftool . ' -s -s -s -ImageWidth -ImageHeight -BitsPerComponent -Colorspace -';
+        . ' | '. $exiftool . ' -S -fast' . $tagsToGet . ' -';
     exec($cmd, $output);
     
-    $width = intval($output[0]);
-    $height = intval($output[1]);
-    preg_match('/^(\d+)/', $output[2], $groups);
-    $bits = intval($groups[1]);
-    $colorspace = intval($output[3]);
-    
-    // Format-specific overrides
-    if ('jpeg' == $type) {
-        // Note: JPEG may be single channel grayscale. jpegtopnm will create PGM in this case.
-        $bits = 8;
+    $tags = Array();
+    foreach ($output as $line) {
+        $keyValue = explode(": ", $line);
+        $tags[$keyValue[0]] = $keyValue[1];
     }
     
+    $width = intval($tags["ImageWidth"]);
+    $height = intval($tags["ImageHeight"]);
+    $type = strtolower($tags["FileType"]);
+    
+    switch ($tags["FileType"]) {
+        case "JP2":
+            $bits = intval($tags["BitsPerComponent"]);
+            break;
+        case "TIFF":
+            $bits = intval($tags["BitsPerSample"]);
+            break;
+        case "JPEG":
+            $bits = 8;
+            break;
+        case "PNG":
+            $bits = intval($tags["BitDepth"]);
+            break;
+        default:
+            BRfatal("Unsupported image type");
+            break;
+    }
+   
+   
     $retval = Array('width' => $width, 'height' => $height,
         'bits' => $bits, 'type' => $type);
     
@@ -260,6 +282,9 @@ if ('jp2' == $fileExt) {
         
 } else if ('jpg' == $fileExt) {
     $decompressCmd = ' | jpegtopnm ' . reduceCommand($scale);
+
+} else if ('png' == $fileExt) {
+    $decompressCmd = ' | pngtopnm ' . reduceCommand($scale);
     
 } else {
     BRfatal('Unknown source file extension: ' . $fileExt);
index 1f48ef0..bf87e3c 100644 (file)
@@ -106,6 +106,46 @@ asyncTest("Load windwavesatseabr00bige image 5", function() {
 });
 
 
+/// nybc200109 - 1-bit tiff zip
+asyncTest("JSLocate for nybc200109 - 1-bit tiff.zip book", function() {
+    expect(1);
+    $.getScript( jsLocateURL('nybc200109'), function() {
+        equals(br.numLeafs,
+               694,
+               'Number of pages');
+        start();
+    });
+});
+
+asyncTest("Image info for 1-bit tiff", function() {
+    expect(3);
+    var expected = {"width":5081,"height":6592,"bits":1,"type":"tiff"};
+    var imageInfoURL = br.getPageURI(0) + '&ext=json&callback=?';
+    
+    $.getJSON(imageInfoURL, function(data) {
+        equals(data != null, true, 'data is not null');
+        if (data != null) {
+            equals(data.width, expected.width, 'Image width');
+            same(data, expected, 'Image info object');
+        }
+        start();
+    });
+});
+
+asyncTest("Load 1-bit tiff image from zip", function() {
+    expect(2);
+    var pageURI = br.getPageURI(6, 16);
+    var img = new Image();
+    $(img).bind( 'load error', function(eventObj) {
+        equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
+        equals(this.width, 318, 'Image width');
+        start();
+    })
+    .attr('src', pageURI);
+});
+
+
+
 /// asamoandictiona00pragoog - tiff zip
 asyncTest("JSLocate for asamoandictiona00pragoog - tiff.zip book", function() {
     expect(1);
@@ -117,6 +157,21 @@ asyncTest("JSLocate for asamoandictiona00pragoog - tiff.zip book", function() {
     });
 });
 
+asyncTest("Image info for 8-bit tiff", function() {
+    expect(3);
+    var expected = {"width":1275,"height":1650,"bits":8,"type":"tiff"};
+    var imageInfoURL = br.getPageURI(0) + '&ext=json&callback=?';
+    
+    $.getJSON(imageInfoURL, function(data) {
+        equals(data != null, true, 'data is not null');
+        if (data != null) {
+            equals(data.width, expected.width, 'Image width');
+            same(data, expected, 'Image info object');
+        }
+        start();
+    });
+});
+
 asyncTest("Load tiff image from zip", function() {
     expect(2);
     var pageURI = br.getPageURI(23, 8);