3 * Support for detecting/validating DjVu image files and getting
4 * some basic file metadata (resolution etc)
6 * File format docs are available in source package for DjVuLibre:
7 * http://djvulibre.djvuzone.org/
10 * Copyright (C) 2006 Brion Vibber <brion@pobox.com>
11 * http://www.mediawiki.org/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 * http://www.gnu.org/copyleft/gpl.html
31 function __construct( $filename ) {
32 $this->mFilename
= $filename;
36 * Check if the given file is indeed a valid DjVu image file
39 public function isValid() {
40 $info = $this->getInfo();
41 return $info !== false;
46 * Return data in the style of getimagesize()
47 * @return array or false on failure
49 public function getImageSize() {
50 $data = $this->getInfo();
52 if( $data !== false ) {
53 $width = $data['width'];
54 $height = $data['height'];
56 return array( $width, $height, 'DjVu',
57 "width=\"$width\" height=\"$height\"" );
65 * For debugging; dump the IFF chunk structure
68 $file = fopen( $this->mFilename
, 'rb' );
69 $header = fread( $file, 12 );
70 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
71 extract( unpack( 'a4magic/a4chunk/NchunkLength', $header ) );
72 echo "$chunk $chunkLength\n";
73 $this->dumpForm( $file, $chunkLength, 1 );
77 private function dumpForm( $file, $length, $indent ) {
78 $start = ftell( $file );
79 $secondary = fread( $file, 4 );
80 echo str_repeat( ' ', $indent * 4 ) . "($secondary)\n";
81 while( ftell( $file ) - $start < $length ) {
82 $chunkHeader = fread( $file, 8 );
83 if( $chunkHeader == '' ) {
86 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
87 extract( unpack( 'a4chunk/NchunkLength', $chunkHeader ) );
88 echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
90 if( $chunk == 'FORM' ) {
91 $this->dumpForm( $file, $chunkLength, $indent +
1 );
93 fseek( $file, $chunkLength, SEEK_CUR
);
94 if( $chunkLength & 1 == 1 ) {
95 // Padding byte between chunks
96 fseek( $file, 1, SEEK_CUR
);
103 $file = fopen( $this->mFilename
, 'rb' );
104 if( $file === false ) {
105 wfDebug( __METHOD__
. ": missing or failed file read\n" );
109 $header = fread( $file, 16 );
112 if( strlen( $header ) < 16 ) {
113 wfDebug( __METHOD__
. ": too short file header\n" );
115 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
116 extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
118 if( $magic != 'AT&T' ) {
119 wfDebug( __METHOD__
. ": not a DjVu file\n" );
120 } elseif( $subtype == 'DJVU' ) {
121 // Single-page document
122 $info = $this->getPageInfo( $file, $formLength );
123 } elseif( $subtype == 'DJVM' ) {
124 // Multi-page document
125 $info = $this->getMultiPageInfo( $file, $formLength );
127 wfDebug( __METHOD__
. ": unrecognized DJVU file type '$formType'\n" );
134 private function readChunk( $file ) {
135 $header = fread( $file, 8 );
136 if( strlen( $header ) < 8 ) {
137 return array( false, 0 );
139 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
140 extract( unpack( 'a4chunk/Nlength', $header ) );
141 return array( $chunk, $length );
145 private function skipChunk( $file, $chunkLength ) {
146 fseek( $file, $chunkLength, SEEK_CUR
);
148 if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
150 fseek( $file, 1, SEEK_CUR
);
154 private function getMultiPageInfo( $file, $formLength ) {
155 // For now, we'll just look for the first page in the file
156 // and report its information, hoping others are the same size.
157 $start = ftell( $file );
159 list( $chunk, $length ) = $this->readChunk( $file );
164 if( $chunk == 'FORM' ) {
165 $subtype = fread( $file, 4 );
166 if( $subtype == 'DJVU' ) {
167 wfDebug( __METHOD__
. ": found first subpage\n" );
168 return $this->getPageInfo( $file, $length );
170 $this->skipChunk( $file, $length - 4 );
172 wfDebug( __METHOD__
. ": skipping '$chunk' chunk\n" );
173 $this->skipChunk( $file, $length );
175 } while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
177 wfDebug( __METHOD__
. ": multi-page DJVU file contained no pages\n" );
181 private function getPageInfo( $file, $formLength ) {
182 list( $chunk, $length ) = $this->readChunk( $file );
183 if( $chunk != 'INFO' ) {
184 wfDebug( __METHOD__
. ": expected INFO chunk, got '$chunk'\n" );
189 wfDebug( __METHOD__
. ": INFO should be 9 or 10 bytes, found $length\n" );
192 $data = fread( $file, $length );
193 if( strlen( $data ) < $length ) {
194 wfDebug( __METHOD__
. ": INFO chunk cut off\n" );
198 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
206 # Newer files have rotation info in byte 10, but we don't use it yet.
211 'version' => "$major.$minor",
212 'resolution' => $resolution,
213 'gamma' => $gamma / 10.0 );
217 * Return an XML string describing the DjVu image
220 function retrieveMetaData() {
222 if ( isset( $wgDjvuToXML ) ) {
223 $cmd = $wgDjvuToXML . ' --without-anno --without-text ' .
224 wfEscapeShellArg( $this->mFilename
);
225 $xml = wfShellExec( $cmd );