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
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/
33 function __construct( $filename ) {
34 $this->mFilename
= $filename;
38 * Check if the given file is indeed a valid DjVu image file
41 public function isValid() {
42 $info = $this->getInfo();
43 return $info !== false;
48 * Return data in the style of getimagesize()
49 * @return array or false on failure
51 public function getImageSize() {
52 $data = $this->getInfo();
54 if( $data !== false ) {
55 $width = $data['width'];
56 $height = $data['height'];
58 return array( $width, $height, 'DjVu',
59 "width=\"$width\" height=\"$height\"" );
67 * For debugging; dump the IFF chunk structure
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 );
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 == '' ) {
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";
92 if( $chunk == 'FORM' ) {
93 $this->dumpForm( $file, $chunkLength, $indent +
1 );
95 fseek( $file, $chunkLength, SEEK_CUR
);
96 if( $chunkLength & 1 == 1 ) {
97 // Padding byte between chunks
98 fseek( $file, 1, SEEK_CUR
);
105 $file = fopen( $this->mFilename
, 'rb' );
106 if( $file === false ) {
107 wfDebug( __METHOD__
. ": missing or failed file read\n" );
111 $header = fread( $file, 16 );
114 if( strlen( $header ) < 16 ) {
115 wfDebug( __METHOD__
. ": too short file header\n" );
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 ) );
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 );
129 wfDebug( __METHOD__
. ": unrecognized DJVU file type '$formType'\n" );
136 private function readChunk( $file ) {
137 $header = fread( $file, 8 );
138 if( strlen( $header ) < 8 ) {
139 return array( false, 0 );
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 );
147 private function skipChunk( $file, $chunkLength ) {
148 fseek( $file, $chunkLength, SEEK_CUR
);
150 if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
152 fseek( $file, 1, SEEK_CUR
);
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 );
161 list( $chunk, $length ) = $this->readChunk( $file );
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 );
172 $this->skipChunk( $file, $length - 4 );
174 wfDebug( __METHOD__
. ": skipping '$chunk' chunk\n" );
175 $this->skipChunk( $file, $length );
177 } while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
179 wfDebug( __METHOD__
. ": multi-page DJVU file contained no pages\n" );
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" );
191 wfDebug( __METHOD__
. ": INFO should be 9 or 10 bytes, found $length\n" );
194 $data = fread( $file, $length );
195 if( strlen( $data ) < $length ) {
196 wfDebug( __METHOD__
. ": INFO chunk cut off\n" );
200 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
208 # Newer files have rotation info in byte 10, but we don't use it yet.
213 'version' => "$major.$minor",
214 'resolution' => $resolution,
215 'gamma' => $gamma / 10.0 );
219 * Return an XML string describing the DjVu image
222 function retrieveMetaData() {
224 if ( isset( $wgDjvuToXML ) ) {
225 $cmd = wfEscapeShellArg( $wgDjvuToXML ) . ' --without-anno --without-text ' .
226 wfEscapeShellArg( $this->mFilename
);
227 $xml = wfShellExec( $cmd );