4 Copyright(c) 2008-2010 Internet Archive. Software license AGPL version 3.
6 This file is part of BookReader. The full source code can be found at GitHub:
7 http://github.com/openlibrary/bookreader
9 The canonical short name of an image type is the same as in the MIME type.
10 For example both .jpeg and .jpg are considered to have type "jpeg" since
11 the MIME type is "image/jpeg".
13 BookReader is free software: you can redistribute it and/or modify
14 it under the terms of the GNU Affero General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
18 BookReader is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Affero General Public License for more details.
23 You should have received a copy of the GNU Affero General Public License
24 along with BookReader. If not, see <http://www.gnu.org/licenses/>.
27 $MIMES = array('gif' => 'image/gif',
29 'jpg' => 'image/jpeg',
30 'jpeg' => 'image/jpeg',
32 'tif' => 'image/tiff',
33 'tiff' => 'image/tiff');
35 $EXTENSIONS = array('gif' => 'gif',
43 // Paths to command-line tools
44 $exiftool = '/petabox/sw/books/exiftool/exiftool';
45 $kduExpand = '/petabox/sw/bin/kdu_expand';
47 // Process some of the request parameters
48 $zipPath = $_REQUEST['zip'];
49 $file = $_REQUEST['file'];
50 if (isset($_REQUEST['ext'])) {
51 $ext = $_REQUEST['ext'];
56 if (isset($_REQUEST['callback'])) {
57 // validate callback is valid JS identifier (only)
58 $callback = $_REQUEST['callback'];
59 $identifierPatt = '/^[[:alpha:]$_]([[:alnum:]$_])*$/';
60 if (! preg_match($identifierPatt, $callback)) {
61 BRfatal('Invalid callback');
70 * Get info about requested image (input)
71 * Get info about requested output format
72 * Determine processing parameters
75 * Clean up temporary files
78 function getUnarchiveCommand($archivePath, $file)
80 $lowerPath = strtolower($archivePath);
81 if (preg_match('/\.([^\.]+)$/', $lowerPath, $matches)) {
82 $suffix = $matches[1];
84 if ($suffix == 'zip') {
86 . escapeshellarg($archivePath)
87 . ' ' . escapeshellarg($file);
88 } else if ($suffix == 'tar') {
90 . escapeshellarg($archivePath)
91 . ' ' . escapeshellarg($file);
93 BRfatal('Incompatible archive format');
97 BRfatal('Bad image stack path');
100 BRfatal('Bad image stack path or archive format');
105 * Returns the image type associated with the file extension.
107 function imageExtensionToType($extension)
111 if (array_key_exists($extension, $EXTENSIONS)) {
112 return $EXTENSIONS[$extension];
114 BRfatal('Unknown image extension');
119 * Get the image information. The returned associative array fields will
120 * vary depending on the image type. The basic keys are width, height, type
123 function getImageInfo($zipPath, $file)
125 return getImageInfoFromExif($zipPath, $file); // this is fast
128 $fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
129 $type = imageExtensionToType($fileExt);
133 return getImageInfoFromJp2($zipPath, $file);
136 return getImageInfoFromExif($zipPath, $file);
141 // Get the records of of JP2 as returned by kdu_expand
142 function getJp2Records($zipPath, $file)
146 $cmd = getUnarchiveCommand($zipPath, $file)
148 . ' -no_seek -quiet -i /dev/stdin -record /dev/stdout';
152 foreach ($output as $line) {
153 $elems = explode("=", $line, 2);
154 if (1 == count($elems)) {
155 // delimiter not found
158 $records[$elems[0]] = $elems[1];
165 * Get the image width, height and depth using the EXIF information.
167 function getImageInfoFromExif($zipPath, $file)
171 // We look for all the possible tags of interest then act on the
172 // ones presumed present based on the file type
173 $tagsToGet = ' -ImageWidth -ImageHeight -FileType' // all formats
174 . ' -BitsPerComponent -ColorSpace' // jp2
175 . ' -BitDepth' // png
176 . ' -BitsPerSample'; // tiff
178 $cmd = getUnarchiveCommand($zipPath, $file)
179 . ' | '. $exiftool . ' -S -fast' . $tagsToGet . ' -';
183 foreach ($output as $line) {
184 $keyValue = explode(": ", $line);
185 $tags[$keyValue[0]] = $keyValue[1];
188 $width = intval($tags["ImageWidth"]);
189 $height = intval($tags["ImageHeight"]);
190 $type = strtolower($tags["FileType"]);
194 $bits = intval($tags["BitsPerComponent"]);
197 $bits = intval($tags["BitsPerSample"]);
203 $bits = intval($tags["BitDepth"]);
206 BRfatal("Unsupported image type");
211 $retval = Array('width' => $width, 'height' => $height,
212 'bits' => $bits, 'type' => $type);
218 * Output JSON given the imageInfo associative array
220 function outputJSON($imageInfo, $callback)
222 header('Content-type: text/plain');
223 $jsonOutput = json_encode($imageInfo);
225 $jsonOutput = $callback . '(' . $jsonOutput . ');';
230 // Get the image size and depth
231 $imageInfo = getImageInfo($zipPath, $file);
233 // Output json if requested
234 if ('json' == $ext) {
235 // $$$ we should determine the output size first based on requested scale
236 outputJSON($imageInfo, $callback);
240 // Unfortunately kakadu requires us to know a priori if the
241 // output file should be .ppm or .pgm. By decompressing to
242 // .bmp kakadu will write a file we can consistently turn into
243 // .pnm. Really kakadu should support .pnm as the file output
244 // extension and automatically write ppm or pgm format as
246 $decompressToBmp = true;
247 if ($decompressToBmp) {
248 $stdoutLink = '/tmp/stdout.bmp';
250 $stdoutLink = '/tmp/stdout.ppm';
253 $fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
255 // Rotate is currently only supported for jp2 since it does not add server load
256 $allowedRotations = array("0", "90", "180", "270");
257 $rotate = $_REQUEST['rotate'];
258 if ( !in_array($rotate, $allowedRotations) ) {
262 // Image conversion options
264 $jpegOptions = '-quality 75';
266 // The pbmreduce reduction factor produces an image with dimension 1/n
267 // The kakadu reduction factor produceds an image with dimension 1/(2^n)
268 // $$$ handle continuous values for scale
269 if (isset($_REQUEST['height'])) {
270 $ratio = floatval($_REQUEST['origHeight']) / floatval($_REQUEST['height']);
274 } else if ($ratio <= 4) {
278 //$powReduce = 3; //too blurry!
284 // $$$ could be cleaner
285 $scale = intval($_REQUEST['scale']);
289 } else if (2 > $scale) {
292 } else if (4 > $scale) {
295 } else if (8 > $scale) {
298 } else if (16 > $scale) {
301 } else if (32 > $scale) {
304 } else if (64 > $scale) {
308 // $$$ Leaving this in as default though I'm not sure why it is...
314 // Override depending on source image format
315 // $$$ consider doing a 302 here instead, to make better use of the browser cache
316 // Limit scaling for 1-bit images. See https://bugs.edge.launchpad.net/bookreader/+bug/486011
317 if (1 == $imageInfo['bits']) {
322 // Hard limit so there are some black pixels to use!
330 if (!file_exists($stdoutLink))
332 system('ln -s /dev/stdout ' . $stdoutLink);
336 putenv('LD_LIBRARY_PATH=/petabox/sw/lib/kakadu');
338 $unzipCmd = getUnarchiveCommand($zipPath, $file);
340 switch ($imageInfo['type']) {
343 " | " . $kduExpand . " -no_seek -quiet -reduce $powReduce -rotate $rotate -i /dev/stdin -o " . $stdoutLink;
344 if ($decompressToBmp) {
345 $decompressCmd .= ' | bmptopnm ';
350 // We need to create a temporary file for tifftopnm since it cannot
351 // work on a pipe (the file must be seekable).
352 // We use the BookReaderTiff prefix to give a hint in case things don't
354 $tempFile = tempnam("/tmp", "BookReaderTiff");
356 // $$$ look at bit depth when reducing
358 ' > ' . $tempFile . ' ; tifftopnm ' . $tempFile . ' 2>/dev/null' . reduceCommand($scale);
362 $decompressCmd = ' | jpegtopnm ' . reduceCommand($scale);
366 $decompressCmd = ' | pngtopnm ' . reduceCommand($scale);
370 BRfatal('Unknown source file extension: ' . $fileExt);
374 // Non-integer scaling is currently disabled on the cluster
375 // if (isset($_REQUEST['height'])) {
376 // $cmd .= " | pnmscale -height {$_REQUEST['height']} ";
381 $compressCmd = ' | pnmtopng ' . $pngOptions;
387 $compressCmd = ' | pnmtojpeg ' . $jpegOptions;
388 $ext = 'jpeg'; // for matching below
393 if (($ext == $fileExt) && ($scale == 1) && ($rotate === "0")) {
394 // Just pass through original data if same format and size
397 $cmd = $unzipCmd . $decompressCmd . $compressCmd;
403 // $$$ investigate how to flush cache when this file is changed
404 header('Content-type: ' . $MIMES[$ext]);
405 header('Cache-Control: max-age=15552000');
406 passthru ($cmd); # cmd returns image data
408 if (isset($tempFile)) {
412 function BRFatal($string) {
413 echo "alert('$string');\n";
417 // Returns true if using a power node
418 function onPowerNode() {
419 exec("lspci | fgrep -c Realtek", $output, $return);
420 if ("0" != $output[0]) {
423 exec("egrep -q AMD /proc/cpuinfo", $output, $return);
431 function reduceCommand($scale) {
434 return ' | pnmscale -reduce ' . $scale;
436 return ' | pnmscale -nomix -reduce ' . $scale;