]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/DjVuImage.php
MediaWiki 1.16.4
[autoinstalls/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, $wgDjvuTxt;
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                 # Text layer
246                 if ( isset( $wgDjvuTxt ) ) { 
247                         wfProfileIn( 'djvutxt' );
248                         $cmd = wfEscapeShellArg( $wgDjvuTxt ) . ' --detail=page ' . wfEscapeShellArg( $this->mFilename ) ;
249                         wfDebug( __METHOD__.": $cmd\n" );
250                         $txt = wfShellExec( $cmd, $retval );
251                         wfProfileOut( 'djvutxt' );
252                         if( $retval == 0) {
253                                 # Get rid of invalid UTF-8, strip control characters
254                                 if( is_callable( 'iconv' ) ) {
255                                         wfSuppressWarnings();
256                                         $txt = iconv( "UTF-8","UTF-8//IGNORE", $txt );
257                                         wfRestoreWarnings();
258                                 } else {
259                                         $txt = UtfNormal::cleanUp( $txt );
260                                 }
261                                 $txt = preg_replace( "/[\013\035\037]/", "", $txt );
262                                 $txt = htmlspecialchars($txt);
263                                 $txt = preg_replace( "/\((page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*\&quot;([^<]*?)\&quot;\s*|)\)/s", "<PAGE value=\"$2\" />", $txt  );
264                                 $txt = "<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt . "</BODY>\n</DjVuTxt>\n";
265                                 $xml = preg_replace( "/<DjVuXML>/", "<mw-djvu><DjVuXML>", $xml );
266                                 $xml = $xml . $txt. '</mw-djvu>' ;
267                         }
268                 }
269                 return $xml;
270         }
271
272         /**
273          * Hack to temporarily work around djvutoxml bug
274          */
275         function convertDumpToXML( $dump ) {
276                 if ( strval( $dump ) == '' ) {
277                         return false;
278                 }
279
280                 $xml = <<<EOT
281 <?xml version="1.0" ?>
282 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
283 <DjVuXML>
284 <HEAD></HEAD>
285 <BODY>
286 EOT;
287
288                 $dump = str_replace( "\r", '', $dump );
289                 $line = strtok( $dump, "\n" );
290                 $m = false;
291                 $good = false;
292                 if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
293                         # Single-page
294                         if ( $this->parseFormDjvu( $line, $xml ) ) {
295                                 $good = true;
296                         } else {
297                                 return false;
298                         }
299                 } elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
300                         # Multi-page
301                         $parentLevel = strlen( $m[1] );
302                         # Find DIRM
303                         $line = strtok( "\n" );
304                         while ( $line !== false ) {
305                                 $childLevel = strspn( $line, ' ' );
306                                 if ( $childLevel <= $parentLevel ) {
307                                         # End of chunk
308                                         break;
309                                 }
310
311                                 if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
312                                         wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
313                                         return false;
314                                 }
315                                 if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
316                                         # Found page
317                                         if ( $this->parseFormDjvu( $line, $xml ) ) {
318                                                 $good = true;
319                                         } else {
320                                                 return false;
321                                         }
322                                 }
323                                 $line = strtok( "\n" );
324                         }
325                 }
326                 if ( !$good ) {
327                         return false;
328                 }
329
330                 $xml .= "</BODY>\n</DjVuXML>\n";
331                 return $xml;
332         }
333
334         function parseFormDjvu( $line, &$xml ) {
335                 $parentLevel = strspn( $line, ' ' );
336                 $line = strtok( "\n" );
337
338                 # Find INFO
339                 while ( $line !== false ) {
340                         $childLevel = strspn( $line, ' ' );
341                         if ( $childLevel <= $parentLevel ) {
342                                 # End of chunk
343                                 break;
344                         }
345
346                         if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
347                                 $xml .= Xml::tags( 'OBJECT',
348                                         array(
349                                                 #'data' => '',
350                                                 #'type' => 'image/x.djvu',
351                                                 'height' => $m[2],
352                                                 'width' => $m[1],
353                                                 #'usemap' => '',
354                                         ),
355                                         "\n" .
356                                         Xml::element( 'PARAM', array( 'name' => 'DPI', 'value' => $m[3] ) ) . "\n" .
357                                         Xml::element( 'PARAM', array( 'name' => 'GAMMA', 'value' => $m[4] ) ) . "\n"
358                                 ) . "\n";
359                                 return true;
360                         }
361                         $line = strtok( "\n" );
362                 }
363                 # Not found
364                 return false;
365         }
366 }