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