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