From a7c36d1ea0fa39904adf9576a60850989f96752a Mon Sep 17 00:00:00 2001 From: Michael Ang Date: Fri, 5 Mar 2010 06:12:37 +0000 Subject: [PATCH] 1-bit detection routines for jp2, tiff, jpeg and png --- BookReaderIA/datanode/BookReaderImages.php | 51 +++++++++++++++----- BookReaderIA/test/unit/Images.js | 55 ++++++++++++++++++++++ 2 files changed, 93 insertions(+), 13 deletions(-) diff --git a/BookReaderIA/datanode/BookReaderImages.php b/BookReaderIA/datanode/BookReaderImages.php index 9b6dfe1..4fcda01 100644 --- a/BookReaderIA/datanode/BookReaderImages.php +++ b/BookReaderIA/datanode/BookReaderImages.php @@ -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); diff --git a/BookReaderIA/test/unit/Images.js b/BookReaderIA/test/unit/Images.js index 1f48ef0..bf87e3c 100644 --- a/BookReaderIA/test/unit/Images.js +++ b/BookReaderIA/test/unit/Images.js @@ -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); -- 2.20.1