revert the 21393, which was revert of 21389, which was revert of 20291.
[lhc/web/wiklou.git] / includes / DjVuImage.php
1 <?php
2
3 /**
4 *
5 * Copyright (C) 2006 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program 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 General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 */
24
25 /**
26 * Support for detecting/validating DjVu image files and getting
27 * some basic file metadata (resolution etc)
28 *
29 * File format docs are available in source package for DjVuLibre:
30 * http://djvulibre.djvuzone.org/
31 */
32 class DjVuImage {
33 function __construct( $filename ) {
34 $this->mFilename = $filename;
35 }
36
37 /**
38 * Check if the given file is indeed a valid DjVu image file
39 * @return bool
40 */
41 public function isValid() {
42 $info = $this->getInfo();
43 return $info !== false;
44 }
45
46
47 /**
48 * Return data in the style of getimagesize()
49 * @return array or false on failure
50 */
51 public function getImageSize() {
52 $data = $this->getInfo();
53
54 if( $data !== false ) {
55 $width = $data['width'];
56 $height = $data['height'];
57
58 return array( $width, $height, 'DjVu',
59 "width=\"$width\" height=\"$height\"" );
60 }
61 return false;
62 }
63
64 // ---------
65
66 /**
67 * For debugging; dump the IFF chunk structure
68 */
69 function dump() {
70 $file = fopen( $this->mFilename, 'rb' );
71 $header = fread( $file, 12 );
72 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
73 extract( unpack( 'a4magic/a4chunk/NchunkLength', $header ) );
74 echo "$chunk $chunkLength\n";
75 $this->dumpForm( $file, $chunkLength, 1 );
76 fclose( $file );
77 }
78
79 private function dumpForm( $file, $length, $indent ) {
80 $start = ftell( $file );
81 $secondary = fread( $file, 4 );
82 echo str_repeat( ' ', $indent * 4 ) . "($secondary)\n";
83 while( ftell( $file ) - $start < $length ) {
84 $chunkHeader = fread( $file, 8 );
85 if( $chunkHeader == '' ) {
86 break;
87 }
88 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
89 extract( unpack( 'a4chunk/NchunkLength', $chunkHeader ) );
90 echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
91
92 if( $chunk == 'FORM' ) {
93 $this->dumpForm( $file, $chunkLength, $indent + 1 );
94 } else {
95 fseek( $file, $chunkLength, SEEK_CUR );
96 if( $chunkLength & 1 == 1 ) {
97 // Padding byte between chunks
98 fseek( $file, 1, SEEK_CUR );
99 }
100 }
101 }
102 }
103
104 function getInfo() {
105 $file = fopen( $this->mFilename, 'rb' );
106 if( $file === false ) {
107 wfDebug( __METHOD__ . ": missing or failed file read\n" );
108 return false;
109 }
110
111 $header = fread( $file, 16 );
112 $info = false;
113
114 if( strlen( $header ) < 16 ) {
115 wfDebug( __METHOD__ . ": too short file header\n" );
116 } else {
117 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
118 extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
119
120 if( $magic != 'AT&T' ) {
121 wfDebug( __METHOD__ . ": not a DjVu file\n" );
122 } elseif( $subtype == 'DJVU' ) {
123 // Single-page document
124 $info = $this->getPageInfo( $file, $formLength );
125 } elseif( $subtype == 'DJVM' ) {
126 // Multi-page document
127 $info = $this->getMultiPageInfo( $file, $formLength );
128 } else {
129 wfDebug( __METHOD__ . ": unrecognized DJVU file type '$formType'\n" );
130 }
131 }
132 fclose( $file );
133 return $info;
134 }
135
136 private function readChunk( $file ) {
137 $header = fread( $file, 8 );
138 if( strlen( $header ) < 8 ) {
139 return array( false, 0 );
140 } else {
141 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
142 extract( unpack( 'a4chunk/Nlength', $header ) );
143 return array( $chunk, $length );
144 }
145 }
146
147 private function skipChunk( $file, $chunkLength ) {
148 fseek( $file, $chunkLength, SEEK_CUR );
149
150 if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
151 // padding byte
152 fseek( $file, 1, SEEK_CUR );
153 }
154 }
155
156 private function getMultiPageInfo( $file, $formLength ) {
157 // For now, we'll just look for the first page in the file
158 // and report its information, hoping others are the same size.
159 $start = ftell( $file );
160 do {
161 list( $chunk, $length ) = $this->readChunk( $file );
162 if( !$chunk ) {
163 break;
164 }
165
166 if( $chunk == 'FORM' ) {
167 $subtype = fread( $file, 4 );
168 if( $subtype == 'DJVU' ) {
169 wfDebug( __METHOD__ . ": found first subpage\n" );
170 return $this->getPageInfo( $file, $length );
171 }
172 $this->skipChunk( $file, $length - 4 );
173 } else {
174 wfDebug( __METHOD__ . ": skipping '$chunk' chunk\n" );
175 $this->skipChunk( $file, $length );
176 }
177 } while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
178
179 wfDebug( __METHOD__ . ": multi-page DJVU file contained no pages\n" );
180 return false;
181 }
182
183 private function getPageInfo( $file, $formLength ) {
184 list( $chunk, $length ) = $this->readChunk( $file );
185 if( $chunk != 'INFO' ) {
186 wfDebug( __METHOD__ . ": expected INFO chunk, got '$chunk'\n" );
187 return false;
188 }
189
190 if( $length < 9 ) {
191 wfDebug( __METHOD__ . ": INFO should be 9 or 10 bytes, found $length\n" );
192 return false;
193 }
194 $data = fread( $file, $length );
195 if( strlen( $data ) < $length ) {
196 wfDebug( __METHOD__ . ": INFO chunk cut off\n" );
197 return false;
198 }
199
200 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
201 extract( unpack(
202 'nwidth/' .
203 'nheight/' .
204 'Cminor/' .
205 'Cmajor/' .
206 'vresolution/' .
207 'Cgamma', $data ) );
208 # Newer files have rotation info in byte 10, but we don't use it yet.
209
210 return array(
211 'width' => $width,
212 'height' => $height,
213 'version' => "$major.$minor",
214 'resolution' => $resolution,
215 'gamma' => $gamma / 10.0 );
216 }
217
218 /**
219 * Return an XML string describing the DjVu image
220 * @return string
221 */
222 function retrieveMetaData() {
223 global $wgDjvuToXML, $wgDjvuDump;
224 if ( isset( $wgDjvuDump ) ) {
225 # djvudump is faster as of version 3.5
226 # http://sourceforge.net/tracker/index.php?func=detail&aid=1704049&group_id=32953&atid=406583
227 wfProfileIn( 'djvudump' );
228 $cmd = wfEscapeShellArg( $wgDjvuDump ) . ' ' . wfEscapeShellArg( $this->mFilename );
229 $dump = wfShellExec( $cmd );
230 $xml = $this->convertDumpToXML( $dump );
231 wfProfileOut( 'djvudump' );
232 } elseif ( isset( $wgDjvuToXML ) ) {
233 wfProfileIn( 'djvutoxml' );
234 $cmd = wfEscapeShellArg( $wgDjvuToXML ) . ' --without-anno --without-text ' .
235 wfEscapeShellArg( $this->mFilename );
236 $xml = wfShellExec( $cmd );
237 wfProfileOut( 'djvutoxml' );
238 } else {
239 $xml = null;
240 }
241 return $xml;
242 }
243
244 /**
245 * Hack to temporarily work around djvutoxml bug
246 */
247 function convertDumpToXML( $dump ) {
248 if ( strval( $dump ) == '' ) {
249 return false;
250 }
251
252 $xml = <<<EOT
253 <?xml version="1.0" ?>
254 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
255 <DjVuXML>
256 <HEAD></HEAD>
257 <BODY>
258 EOT;
259
260 $dump = str_replace( "\r", '', $dump );
261 $line = strtok( $dump, "\n" );
262 $m = false;
263 $good = false;
264 if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
265 # Single-page
266 if ( $this->parseFormDjvu( $line, $xml ) ) {
267 $good = true;
268 } else {
269 return false;
270 }
271 } elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
272 # Multi-page
273 $parentLevel = strlen( $m[1] );
274 # Find DIRM
275 $line = strtok( "\n" );
276 while ( $line !== false ) {
277 $childLevel = strspn( $line, ' ' );
278 if ( $childLevel <= $parentLevel ) {
279 # End of chunk
280 break;
281 }
282
283 if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
284 wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
285 return false;
286 }
287 if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
288 # Found page
289 if ( $this->parseFormDjvu( $line, $xml ) ) {
290 $good = true;
291 } else {
292 return false;
293 }
294 }
295 $line = strtok( "\n" );
296 }
297 }
298 if ( !$good ) {
299 return false;
300 }
301
302 $xml .= "</BODY>\n</DjVuXML>\n";
303 return $xml;
304 }
305
306 function parseFormDjvu( $line, &$xml ) {
307 $parentLevel = strspn( $line, ' ' );
308 $line = strtok( "\n" );
309
310 # Find INFO
311 while ( $line !== false ) {
312 $childLevel = strspn( $line, ' ' );
313 if ( $childLevel <= $parentLevel ) {
314 # End of chunk
315 break;
316 }
317
318 if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
319 $xml .= Xml::tags( 'OBJECT',
320 array(
321 #'data' => '',
322 #'type' => 'image/x.djvu',
323 'height' => $m[2],
324 'width' => $m[1],
325 #'usemap' => '',
326 ),
327 "\n" .
328 Xml::element( 'PARAM', array( 'name' => 'DPI', 'value' => $m[3] ) ) . "\n" .
329 Xml::element( 'PARAM', array( 'name' => 'GAMMA', 'value' => $m[4] ) ) . "\n"
330 ) . "\n";
331 return true;
332 }
333 $line = strtok( "\n" );
334 }
335 # Not found
336 return false;
337 }
338 }
339
340
341 ?>