Fix regression in even sizing of diff columns; forgot to restore a couple bits I...
[mediawiki.git] / includes / DjVuImage.php
blob1e423565cd67a41d63652ff032b2b3810c85d88b
1 <?php
3 /**
5 * Copyright (C) 2006 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
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.
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.
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
25 /**
26 * Support for detecting/validating DjVu image files and getting
27 * some basic file metadata (resolution etc)
29 * File format docs are available in source package for DjVuLibre:
30 * http://djvulibre.djvuzone.org/
32 * @addtogroup Media
34 class DjVuImage {
35 function __construct( $filename ) {
36 $this->mFilename = $filename;
39 /**
40 * Check if the given file is indeed a valid DjVu image file
41 * @return bool
43 public function isValid() {
44 $info = $this->getInfo();
45 return $info !== false;
49 /**
50 * Return data in the style of getimagesize()
51 * @return array or false on failure
53 public function getImageSize() {
54 $data = $this->getInfo();
56 if( $data !== false ) {
57 $width = $data['width'];
58 $height = $data['height'];
60 return array( $width, $height, 'DjVu',
61 "width=\"$width\" height=\"$height\"" );
63 return false;
66 // ---------
68 /**
69 * For debugging; dump the IFF chunk structure
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 );
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;
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";
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 );
106 function getInfo() {
107 $file = fopen( $this->mFilename, 'rb' );
108 if( $file === false ) {
109 wfDebug( __METHOD__ . ": missing or failed file read\n" );
110 return false;
113 $header = fread( $file, 16 );
114 $info = false;
116 if( strlen( $header ) < 16 ) {
117 wfDebug( __METHOD__ . ": too short file header\n" );
118 } else {
119 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
120 extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
122 if( $magic != 'AT&T' ) {
123 wfDebug( __METHOD__ . ": not a DjVu file\n" );
124 } elseif( $subtype == 'DJVU' ) {
125 // Single-page document
126 $info = $this->getPageInfo( $file, $formLength );
127 } elseif( $subtype == 'DJVM' ) {
128 // Multi-page document
129 $info = $this->getMultiPageInfo( $file, $formLength );
130 } else {
131 wfDebug( __METHOD__ . ": unrecognized DJVU file type '$formType'\n" );
134 fclose( $file );
135 return $info;
138 private function readChunk( $file ) {
139 $header = fread( $file, 8 );
140 if( strlen( $header ) < 8 ) {
141 return array( false, 0 );
142 } else {
143 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
144 extract( unpack( 'a4chunk/Nlength', $header ) );
145 return array( $chunk, $length );
149 private function skipChunk( $file, $chunkLength ) {
150 fseek( $file, $chunkLength, SEEK_CUR );
152 if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
153 // padding byte
154 fseek( $file, 1, SEEK_CUR );
158 private function getMultiPageInfo( $file, $formLength ) {
159 // For now, we'll just look for the first page in the file
160 // and report its information, hoping others are the same size.
161 $start = ftell( $file );
162 do {
163 list( $chunk, $length ) = $this->readChunk( $file );
164 if( !$chunk ) {
165 break;
168 if( $chunk == 'FORM' ) {
169 $subtype = fread( $file, 4 );
170 if( $subtype == 'DJVU' ) {
171 wfDebug( __METHOD__ . ": found first subpage\n" );
172 return $this->getPageInfo( $file, $length );
174 $this->skipChunk( $file, $length - 4 );
175 } else {
176 wfDebug( __METHOD__ . ": skipping '$chunk' chunk\n" );
177 $this->skipChunk( $file, $length );
179 } while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
181 wfDebug( __METHOD__ . ": multi-page DJVU file contained no pages\n" );
182 return false;
185 private function getPageInfo( $file, $formLength ) {
186 list( $chunk, $length ) = $this->readChunk( $file );
187 if( $chunk != 'INFO' ) {
188 wfDebug( __METHOD__ . ": expected INFO chunk, got '$chunk'\n" );
189 return false;
192 if( $length < 9 ) {
193 wfDebug( __METHOD__ . ": INFO should be 9 or 10 bytes, found $length\n" );
194 return false;
196 $data = fread( $file, $length );
197 if( strlen( $data ) < $length ) {
198 wfDebug( __METHOD__ . ": INFO chunk cut off\n" );
199 return false;
202 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
203 extract( unpack(
204 'nwidth/' .
205 'nheight/' .
206 'Cminor/' .
207 'Cmajor/' .
208 'vresolution/' .
209 'Cgamma', $data ) );
210 # Newer files have rotation info in byte 10, but we don't use it yet.
212 return array(
213 'width' => $width,
214 'height' => $height,
215 'version' => "$major.$minor",
216 'resolution' => $resolution,
217 'gamma' => $gamma / 10.0 );
221 * Return an XML string describing the DjVu image
222 * @return string
224 function retrieveMetaData() {
225 global $wgDjvuToXML, $wgDjvuDump;
226 if ( isset( $wgDjvuDump ) ) {
227 # djvudump is faster as of version 3.5
228 # http://sourceforge.net/tracker/index.php?func=detail&aid=1704049&group_id=32953&atid=406583
229 wfProfileIn( 'djvudump' );
230 $cmd = wfEscapeShellArg( $wgDjvuDump ) . ' ' . wfEscapeShellArg( $this->mFilename );
231 $dump = wfShellExec( $cmd );
232 $xml = $this->convertDumpToXML( $dump );
233 wfProfileOut( 'djvudump' );
234 } elseif ( isset( $wgDjvuToXML ) ) {
235 wfProfileIn( 'djvutoxml' );
236 $cmd = wfEscapeShellArg( $wgDjvuToXML ) . ' --without-anno --without-text ' .
237 wfEscapeShellArg( $this->mFilename );
238 $xml = wfShellExec( $cmd );
239 wfProfileOut( 'djvutoxml' );
240 } else {
241 $xml = null;
243 return $xml;
247 * Hack to temporarily work around djvutoxml bug
249 function convertDumpToXML( $dump ) {
250 if ( strval( $dump ) == '' ) {
251 return false;
254 $xml = <<<EOT
255 <?xml version="1.0" ?>
256 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
257 <DjVuXML>
258 <HEAD></HEAD>
259 <BODY>
260 EOT;
262 $dump = str_replace( "\r", '', $dump );
263 $line = strtok( $dump, "\n" );
264 $m = false;
265 $good = false;
266 if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
267 # Single-page
268 if ( $this->parseFormDjvu( $line, $xml ) ) {
269 $good = true;
270 } else {
271 return false;
273 } elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
274 # Multi-page
275 $parentLevel = strlen( $m[1] );
276 # Find DIRM
277 $line = strtok( "\n" );
278 while ( $line !== false ) {
279 $childLevel = strspn( $line, ' ' );
280 if ( $childLevel <= $parentLevel ) {
281 # End of chunk
282 break;
285 if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
286 wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
287 return false;
289 if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
290 # Found page
291 if ( $this->parseFormDjvu( $line, $xml ) ) {
292 $good = true;
293 } else {
294 return false;
297 $line = strtok( "\n" );
300 if ( !$good ) {
301 return false;
304 $xml .= "</BODY>\n</DjVuXML>\n";
305 return $xml;
308 function parseFormDjvu( $line, &$xml ) {
309 $parentLevel = strspn( $line, ' ' );
310 $line = strtok( "\n" );
312 # Find INFO
313 while ( $line !== false ) {
314 $childLevel = strspn( $line, ' ' );
315 if ( $childLevel <= $parentLevel ) {
316 # End of chunk
317 break;
320 if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
321 $xml .= Xml::tags( 'OBJECT',
322 array(
323 #'data' => '',
324 #'type' => 'image/x.djvu',
325 'height' => $m[2],
326 'width' => $m[1],
327 #'usemap' => '',
329 "\n" .
330 Xml::element( 'PARAM', array( 'name' => 'DPI', 'value' => $m[3] ) ) . "\n" .
331 Xml::element( 'PARAM', array( 'name' => 'GAMMA', 'value' => $m[4] ) ) . "\n"
332 ) . "\n";
333 return true;
335 $line = strtok( "\n" );
337 # Not found
338 return false;