7637b32feaac82b87e53598e7833b9a7762923c2
[lhc/web/wiklou.git] / includes / DjVuImage.php
1 <?php
2 /**
3 * Support for detecting/validating DjVu image files and getting
4 * some basic file metadata (resolution etc)
5 *
6 * File format docs are available in source package for DjVuLibre:
7 * http://djvulibre.djvuzone.org/
8 *
9 *
10 * Copyright (C) 2006 Brion Vibber <brion@pobox.com>
11 * http://www.mediawiki.org/
12 *
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.
17 *
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.
22 *
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
27 *
28 * @package MediaWiki
29 */
30
31 class DjVuImage {
32 function __construct( $filename ) {
33 $this->mFilename = $filename;
34 }
35
36 /**
37 * Check if the given file is indeed a valid DjVu image file
38 * @return bool
39 */
40 public function isValid() {
41 $info = $this->getInfo();
42 return $info !== false;
43 }
44
45
46 /**
47 * Return data in the style of getimagesize()
48 * @return array or false on failure
49 */
50 public function getImageSize() {
51 $data = $this->getInfo();
52
53 if( $data !== false ) {
54 $width = $data['width'];
55 $height = $data['height'];
56
57 return array( $width, $height, 'DjVu',
58 "width=\"$width\" height=\"$height\"" );
59 }
60 return false;
61 }
62
63 // ---------
64
65 /**
66 * For debugging; dump the IFF chunk structure
67 */
68 function dump() {
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 );
75 fclose( $file );
76 }
77
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 == '' ) {
85 break;
86 }
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";
90
91 if( $chunk == 'FORM' ) {
92 $this->dumpForm( $file, $chunkLength, $indent + 1 );
93 } else {
94 fseek( $file, $chunkLength, SEEK_CUR );
95 if( $chunkLength & 1 == 1 ) {
96 // Padding byte between chunks
97 fseek( $file, 1, SEEK_CUR );
98 }
99 }
100 }
101 }
102
103 function getInfo() {
104 $file = fopen( $this->mFilename, 'rb' );
105 if( $file === false ) {
106 wfDebug( __METHOD__ . ": missing or failed file read\n" );
107 return false;
108 }
109
110 $header = fread( $file, 16 );
111 $info = false;
112
113 if( strlen( $header ) < 16 ) {
114 wfDebug( __METHOD__ . ": too short file header\n" );
115 } else {
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 ) );
118
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 );
127 } else {
128 wfDebug( __METHOD__ . ": unrecognized DJVU file type '$formType'\n" );
129 }
130 }
131 fclose( $file );
132 return $info;
133 }
134
135 private function readChunk( $file ) {
136 $header = fread( $file, 8 );
137 if( strlen( $header ) < 8 ) {
138 return array( false, 0 );
139 } else {
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 );
143 }
144 }
145
146 private function skipChunk( $file, $chunkLength ) {
147 fseek( $file, $chunkLength, SEEK_CUR );
148
149 if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
150 // padding byte
151 fseek( $file, 1, SEEK_CUR );
152 }
153 }
154
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 );
159 do {
160 list( $chunk, $length ) = $this->readChunk( $file );
161 if( !$chunk ) {
162 break;
163 }
164
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 );
170 }
171 $this->skipChunk( $file, $length - 4 );
172 } else {
173 wfDebug( __METHOD__ . ": skipping '$chunk' chunk\n" );
174 $this->skipChunk( $file, $length );
175 }
176 } while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
177
178 wfDebug( __METHOD__ . ": multi-page DJVU file contained no pages\n" );
179 return false;
180 }
181
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" );
186 return false;
187 }
188
189 if( $length < 9 ) {
190 wfDebug( __METHOD__ . ": INFO should be 9 or 10 bytes, found $length\n" );
191 return false;
192 }
193 $data = fread( $file, $length );
194 if( strlen( $data ) < $length ) {
195 wfDebug( __METHOD__ . ": INFO chunk cut off\n" );
196 return false;
197 }
198
199 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
200 extract( unpack(
201 'nwidth/' .
202 'nheight/' .
203 'Cminor/' .
204 'Cmajor/' .
205 'vresolution/' .
206 'Cgamma', $data ) );
207 # Newer files have rotation info in byte 10, but we don't use it yet.
208
209 return array(
210 'width' => $width,
211 'height' => $height,
212 'version' => "$major.$minor",
213 'resolution' => $resolution,
214 'gamma' => $gamma / 10.0 );
215 }
216
217 /**
218 * Return an XML string describing the DjVu image
219 * @return string
220 */
221 function retrieveMetaData() {
222 global $wgDjvuToXML;
223 if ( isset( $wgDjvuToXML ) ) {
224 $cmd = $wgDjvuToXML . ' --without-anno --without-text ' .
225 wfEscapeShellArg( $this->mFilename );
226 $xml = wfShellExec( $cmd );
227 } else {
228 $xml = null;
229 }
230 return $xml;
231 }
232
233 }
234
235
236 ?>