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
32 function __construct( $filename ) {
33 $this->mFilename
= $filename;
37 * Check if the given file is indeed a valid DjVu image file
40 public function isValid() {
41 $info = $this->getInfo();
42 return $info !== false;
47 * Return data in the style of getimagesize()
48 * @return array or false on failure
50 public function getImageSize() {
51 $data = $this->getInfo();
53 if( $data !== false ) {
54 $width = $data['width'];
55 $height = $data['height'];
57 return array( $width, $height, 'DjVu',
58 "width=\"$width\" height=\"$height\"" );
66 * For debugging; dump the IFF chunk structure
69 $file = fopen( $this->mFilename
, 'rb' );
70 $header = fread( $file, 12 );
71 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
72 extract( unpack( 'a4magic/a4chunk/NchunkLength', $header ) );
73 echo "$chunk $chunkLength\n";
74 $this->dumpForm( $file, $chunkLength, 1 );
78 private function dumpForm( $file, $length, $indent ) {
79 $start = ftell( $file );
80 $secondary = fread( $file, 4 );
81 echo str_repeat( ' ', $indent * 4 ) . "($secondary)\n";
82 while( ftell( $file ) - $start < $length ) {
83 $chunkHeader = fread( $file, 8 );
84 if( $chunkHeader == '' ) {
87 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
88 extract( unpack( 'a4chunk/NchunkLength', $chunkHeader ) );
89 echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
91 if( $chunk == 'FORM' ) {
92 $this->dumpForm( $file, $chunkLength, $indent +
1 );
94 fseek( $file, $chunkLength, SEEK_CUR
);
95 if( $chunkLength & 1 == 1 ) {
96 // Padding byte between chunks
97 fseek( $file, 1, SEEK_CUR
);
104 $file = fopen( $this->mFilename
, 'rb' );
105 if( $file === false ) {
106 wfDebug( __METHOD__
. ": missing or failed file read\n" );
110 $header = fread( $file, 16 );
113 if( strlen( $header ) < 16 ) {
114 wfDebug( __METHOD__
. ": too short file header\n" );
116 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
117 extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
119 if( $magic != 'AT&T' ) {
120 wfDebug( __METHOD__
. ": not a DjVu file\n" );
121 } elseif( $subtype == 'DJVU' ) {
122 // Single-page document
123 $info = $this->getPageInfo( $file, $formLength );
124 } elseif( $subtype == 'DJVM' ) {
125 // Multi-page document
126 $info = $this->getMultiPageInfo( $file, $formLength );
128 wfDebug( __METHOD__
. ": unrecognized DJVU file type '$formType'\n" );
135 private function readChunk( $file ) {
136 $header = fread( $file, 8 );
137 if( strlen( $header ) < 8 ) {
138 return array( false, 0 );
140 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
141 extract( unpack( 'a4chunk/Nlength', $header ) );
142 return array( $chunk, $length );
146 private function skipChunk( $file, $chunkLength ) {
147 fseek( $file, $chunkLength, SEEK_CUR
);
149 if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
151 fseek( $file, 1, SEEK_CUR
);
155 private function getMultiPageInfo( $file, $formLength ) {
156 // For now, we'll just look for the first page in the file
157 // and report its information, hoping others are the same size.
158 $start = ftell( $file );
160 list( $chunk, $length ) = $this->readChunk( $file );
165 if( $chunk == 'FORM' ) {
166 $subtype = fread( $file, 4 );
167 if( $subtype == 'DJVU' ) {
168 wfDebug( __METHOD__
. ": found first subpage\n" );
169 return $this->getPageInfo( $file, $length );
171 $this->skipChunk( $file, $length - 4 );
173 wfDebug( __METHOD__
. ": skipping '$chunk' chunk\n" );
174 $this->skipChunk( $file, $length );
176 } while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
178 wfDebug( __METHOD__
. ": multi-page DJVU file contained no pages\n" );
182 private function getPageInfo( $file, $formLength ) {
183 list( $chunk, $length ) = $this->readChunk( $file );
184 if( $chunk != 'INFO' ) {
185 wfDebug( __METHOD__
. ": expected INFO chunk, got '$chunk'\n" );
190 wfDebug( __METHOD__
. ": INFO should be 9 or 10 bytes, found $length\n" );
193 $data = fread( $file, $length );
194 if( strlen( $data ) < $length ) {
195 wfDebug( __METHOD__
. ": INFO chunk cut off\n" );
199 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
207 # Newer files have rotation info in byte 10, but we don't use it yet.
212 'version' => "$major.$minor",
213 'resolution' => $resolution,
214 'gamma' => $gamma / 10.0 );
218 * Return an XML string describing the DjVu image
221 function retrieveMetaData() {
223 if ( isset( $wgDjvuToXML ) ) {
224 $cmd = $wgDjvuToXML . ' --without-anno --without-text ' . escapeshellarg( $this->mFilename
);
225 $xml = wfShellExec( $cmd );