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