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