4 Copyright(c)2008 Internet Archive. Software license AGPL version 3.
6 This file is part of BookReader.
8 BookReader is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 BookReader is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with BookReader. If not, see <http://www.gnu.org/licenses/>.
22 $MIMES = array('jpg' => 'image/jpeg',
23 'png' => 'image/png');
25 $exiftool = '/petabox/sw/books/exiftool/exiftool';
27 $zipPath = $_REQUEST['zip'];
28 $file = $_REQUEST['file'];
33 * Get info about requested image (input)
34 * Get info about requested output format
35 * Determine processing parameters
38 * Clean up temporary files
41 function getUnarchiveCommand($archivePath, $file)
43 $lowerPath = strtolower($archivePath);
44 if (preg_match('/\.([^\.]+)$/', $lowerPath, $matches)) {
45 $suffix = $matches[1];
47 if ($suffix == 'zip') {
49 . escapeshellarg($archivePath)
50 . ' ' . escapeshellarg($file);
51 } else if ($suffix == 'tar') {
53 . escapeshellarg($archivePath)
54 . ' ' . escapeshellarg($file);
59 BRfatal('Incompatible image stack path');
64 * Get the image width, height and depth from a jp2 file in zip.
66 function getImageSizeAndDepth($zipPath, $file)
70 # $$$ will exiftool work for *all* of our images?
71 $cmd = getUnarchiveCommand($zipPath, $file)
72 . ' | '. $exiftool . ' -s -s -s -ImageWidth -ImageHeight -BitsPerComponent -';
75 preg_match('/^(\d+)/', $output[2], $groups);
76 $bits = intval($groups[1]);
78 $retval = Array('width' => intval($output[0]), 'height' => intval($output[1]),
83 // Unfortunately kakadu requires us to know a priori if the
84 // output file should be .ppm or .pgm. By decompressing to
85 // .bmp kakadu will write a file we can consistently turn into
86 // .pnm. Really kakadu should support .pnm as the file output
87 // extension and automatically write ppm or pgm format as
89 $decompressToBmp = true;
90 if ($decompressToBmp) {
91 $stdoutLink = '/tmp/stdout.bmp';
93 $stdoutLink = '/tmp/stdout.ppm';
96 if (isset($_REQUEST['ext'])) {
97 $ext = $_REQUEST['ext'];
103 $fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
105 // Rotate is currently only supported for jp2 since it does not add server load
106 $allowedRotations = array("0", "90", "180", "270");
107 $rotate = $_REQUEST['rotate'];
108 if ( !in_array($rotate, $allowedRotations) ) {
112 // Image conversion options
114 $jpegOptions = '-quality 75';
116 // The pbmreduce reduction factor produces an image with dimension 1/n
117 // The kakadu reduction factor produceds an image with dimension 1/(2^n)
119 if (isset($_REQUEST['height'])) {
120 $ratio = floatval($_REQUEST['origHeight']) / floatval($_REQUEST['height']);
124 } else if ($ratio <= 4) {
128 //$powReduce = 3; //too blurry!
134 $scale = $_REQUEST['scale'];
138 } else if (2 == $scale) {
140 } else if (4 == $scale) {
142 } else if (8 == $scale) {
144 } else if (16 == $scale) {
146 } else if (32 == $scale) {
149 // $$$ Leaving this in as default though I'm not sure why it is...
155 if (!file_exists($stdoutLink))
157 system('ln -s /dev/stdout ' . $stdoutLink);
161 putenv('LD_LIBRARY_PATH=/petabox/sw/lib/kakadu');
163 $unzipCmd = getUnarchiveCommand($zipPath, $file);
165 if ('jp2' == $fileExt) {
167 " | /petabox/sw/bin/kdu_expand -no_seek -quiet -reduce $powReduce -rotate $rotate -i /dev/stdin -o " . $stdoutLink;
168 if ($decompressToBmp) {
169 $decompressCmd .= ' | bmptopnm ';
172 } else if ('tif' == $fileExt) {
173 // We need to create a temporary file for tifftopnm since it cannot
174 // work on a pipe (the file must be seekable).
175 // We use the BookReaderTiff prefix to give a hint in case things don't
177 $tempFile = tempnam("/tmp", "BookReaderTiff");
181 $pbmReduce = ' | pnmscale -reduce ' . $scale;
183 $pbmReduce = ' | pnmscale -nomix -reduce ' . $scale;
190 ' > ' . $tempFile . ' ; tifftopnm ' . $tempFile . ' 2>/dev/null' . $pbmReduce;
193 BRfatal('Unknown source file extension: ' . $fileExt);
196 // Non-integer scaling is currently disabled on the cluster
197 // if (isset($_REQUEST['height'])) {
198 // $cmd .= " | pnmscale -height {$_REQUEST['height']} ";
202 $compressCmd = ' | pnmtojpeg ' . $jpegOptions;
203 } else if ('png' == $ext) {
204 $compressCmd = ' | pnmtopng ' . $pngOptions;
207 $cmd = $unzipCmd . $decompressCmd . $compressCmd;
212 header('Content-type: ' . $MIMES[$ext]);
213 header('Cache-Control: max-age=15552000');
214 passthru ($cmd); # cmd returns image data
216 if (isset($tempFile)) {
220 function BRFatal($string) {
221 echo "alert('$string')\n";
225 // Returns true if using a power node
226 function onPowerNode() {
227 exec("lspci | fgrep -c Realtek", $output, $return);
228 if ("0" != $output[0]) {
231 exec("egrep -q AMD /proc/cpuinfo", $output, $return);