00001 <?php
00002
00034 class DjVuImage {
00035 function __construct( $filename ) {
00036 $this->mFilename = $filename;
00037 }
00038
00043 public function isValid() {
00044 $info = $this->getInfo();
00045 return $info !== false;
00046 }
00047
00048
00053 public function getImageSize() {
00054 $data = $this->getInfo();
00055
00056 if( $data !== false ) {
00057 $width = $data['width'];
00058 $height = $data['height'];
00059
00060 return array( $width, $height, 'DjVu',
00061 "width=\"$width\" height=\"$height\"" );
00062 }
00063 return false;
00064 }
00065
00066
00067
00071 function dump() {
00072 $file = fopen( $this->mFilename, 'rb' );
00073 $header = fread( $file, 12 );
00074
00075 extract( unpack( 'a4magic/a4chunk/NchunkLength', $header ) );
00076 echo "$chunk $chunkLength\n";
00077 $this->dumpForm( $file, $chunkLength, 1 );
00078 fclose( $file );
00079 }
00080
00081 private function dumpForm( $file, $length, $indent ) {
00082 $start = ftell( $file );
00083 $secondary = fread( $file, 4 );
00084 echo str_repeat( ' ', $indent * 4 ) . "($secondary)\n";
00085 while( ftell( $file ) - $start < $length ) {
00086 $chunkHeader = fread( $file, 8 );
00087 if( $chunkHeader == '' ) {
00088 break;
00089 }
00090
00091 extract( unpack( 'a4chunk/NchunkLength', $chunkHeader ) );
00092 echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
00093
00094 if( $chunk == 'FORM' ) {
00095 $this->dumpForm( $file, $chunkLength, $indent + 1 );
00096 } else {
00097 fseek( $file, $chunkLength, SEEK_CUR );
00098 if( $chunkLength & 1 == 1 ) {
00099
00100 fseek( $file, 1, SEEK_CUR );
00101 }
00102 }
00103 }
00104 }
00105
00106 function getInfo() {
00107 wfSuppressWarnings();
00108 $file = fopen( $this->mFilename, 'rb' );
00109 wfRestoreWarnings();
00110 if( $file === false ) {
00111 wfDebug( __METHOD__ . ": missing or failed file read\n" );
00112 return false;
00113 }
00114
00115 $header = fread( $file, 16 );
00116 $info = false;
00117
00118 if( strlen( $header ) < 16 ) {
00119 wfDebug( __METHOD__ . ": too short file header\n" );
00120 } else {
00121
00122 extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
00123
00124 if( $magic != 'AT&T' ) {
00125 wfDebug( __METHOD__ . ": not a DjVu file\n" );
00126 } elseif( $subtype == 'DJVU' ) {
00127
00128 $info = $this->getPageInfo( $file, $formLength );
00129 } elseif( $subtype == 'DJVM' ) {
00130
00131 $info = $this->getMultiPageInfo( $file, $formLength );
00132 } else {
00133 wfDebug( __METHOD__ . ": unrecognized DJVU file type '$formType'\n" );
00134 }
00135 }
00136 fclose( $file );
00137 return $info;
00138 }
00139
00140 private function readChunk( $file ) {
00141 $header = fread( $file, 8 );
00142 if( strlen( $header ) < 8 ) {
00143 return array( false, 0 );
00144 } else {
00145
00146 extract( unpack( 'a4chunk/Nlength', $header ) );
00147 return array( $chunk, $length );
00148 }
00149 }
00150
00151 private function skipChunk( $file, $chunkLength ) {
00152 fseek( $file, $chunkLength, SEEK_CUR );
00153
00154 if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
00155
00156 fseek( $file, 1, SEEK_CUR );
00157 }
00158 }
00159
00160 private function getMultiPageInfo( $file, $formLength ) {
00161
00162
00163 $start = ftell( $file );
00164 do {
00165 list( $chunk, $length ) = $this->readChunk( $file );
00166 if( !$chunk ) {
00167 break;
00168 }
00169
00170 if( $chunk == 'FORM' ) {
00171 $subtype = fread( $file, 4 );
00172 if( $subtype == 'DJVU' ) {
00173 wfDebug( __METHOD__ . ": found first subpage\n" );
00174 return $this->getPageInfo( $file, $length );
00175 }
00176 $this->skipChunk( $file, $length - 4 );
00177 } else {
00178 wfDebug( __METHOD__ . ": skipping '$chunk' chunk\n" );
00179 $this->skipChunk( $file, $length );
00180 }
00181 } while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
00182
00183 wfDebug( __METHOD__ . ": multi-page DJVU file contained no pages\n" );
00184 return false;
00185 }
00186
00187 private function getPageInfo( $file, $formLength ) {
00188 list( $chunk, $length ) = $this->readChunk( $file );
00189 if( $chunk != 'INFO' ) {
00190 wfDebug( __METHOD__ . ": expected INFO chunk, got '$chunk'\n" );
00191 return false;
00192 }
00193
00194 if( $length < 9 ) {
00195 wfDebug( __METHOD__ . ": INFO should be 9 or 10 bytes, found $length\n" );
00196 return false;
00197 }
00198 $data = fread( $file, $length );
00199 if( strlen( $data ) < $length ) {
00200 wfDebug( __METHOD__ . ": INFO chunk cut off\n" );
00201 return false;
00202 }
00203
00204
00205 extract( unpack(
00206 'nwidth/' .
00207 'nheight/' .
00208 'Cminor/' .
00209 'Cmajor/' .
00210 'vresolution/' .
00211 'Cgamma', $data ) );
00212 # Newer files have rotation info in byte 10, but we don't use it yet.
00213
00214 return array(
00215 'width' => $width,
00216 'height' => $height,
00217 'version' => "$major.$minor",
00218 'resolution' => $resolution,
00219 'gamma' => $gamma / 10.0 );
00220 }
00221
00226 function retrieveMetaData() {
00227 global $wgDjvuToXML, $wgDjvuDump, $wgDjvuTxt;
00228 if ( isset( $wgDjvuDump ) ) {
00229 # djvudump is faster as of version 3.5
00230 # http://sourceforge.net/tracker/index.php?func=detail&aid=1704049&group_id=32953&atid=406583
00231 wfProfileIn( 'djvudump' );
00232 $cmd = wfEscapeShellArg( $wgDjvuDump ) . ' ' . wfEscapeShellArg( $this->mFilename );
00233 $dump = wfShellExec( $cmd );
00234 $xml = $this->convertDumpToXML( $dump );
00235 wfProfileOut( 'djvudump' );
00236 } elseif ( isset( $wgDjvuToXML ) ) {
00237 wfProfileIn( 'djvutoxml' );
00238 $cmd = wfEscapeShellArg( $wgDjvuToXML ) . ' --without-anno --without-text ' .
00239 wfEscapeShellArg( $this->mFilename );
00240 $xml = wfShellExec( $cmd );
00241 wfProfileOut( 'djvutoxml' );
00242 } else {
00243 $xml = null;
00244 }
00245 # Text layer
00246 if ( isset( $wgDjvuTxt ) ) {
00247 wfProfileIn( 'djvutxt' );
00248 $cmd = wfEscapeShellArg( $wgDjvuTxt ) . ' --detail=page ' . wfEscapeShellArg( $this->mFilename ) ;
00249 wfDebug( __METHOD__.": $cmd\n" );
00250 $txt = wfShellExec( $cmd, $retval );
00251 wfProfileOut( 'djvutxt' );
00252 if( $retval == 0) {
00253 # Get rid of invalid UTF-8, strip control characters
00254 if( is_callable( 'iconv' ) ) {
00255 wfSuppressWarnings();
00256 $txt = iconv( "UTF-8","UTF-8//IGNORE", $txt );
00257 wfRestoreWarnings();
00258 } else {
00259 $txt = UtfNormal::cleanUp( $txt );
00260 }
00261 $txt = preg_replace( "/[\013\035\037]/", "", $txt );
00262 $txt = htmlspecialchars($txt);
00263 $txt = preg_replace( "/\((page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*\"([^<]*?)\"\s*|)\)/s", "<PAGE value=\"$2\" />", $txt );
00264 $txt = "<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt . "</BODY>\n</DjVuTxt>\n";
00265 $xml = preg_replace( "/<DjVuXML>/", "<mw-djvu><DjVuXML>", $xml );
00266 $xml = $xml . $txt. '</mw-djvu>' ;
00267 }
00268 }
00269 return $xml;
00270 }
00271
00275 function convertDumpToXML( $dump ) {
00276 if ( strval( $dump ) == '' ) {
00277 return false;
00278 }
00279
00280 $xml = <<<EOT
00281 <?xml version="1.0" ?>
00282 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
00283 <DjVuXML>
00284 <HEAD></HEAD>
00285 <BODY>
00286 EOT;
00287
00288 $dump = str_replace( "\r", '', $dump );
00289 $line = strtok( $dump, "\n" );
00290 $m = false;
00291 $good = false;
00292 if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
00293 # Single-page
00294 if ( $this->parseFormDjvu( $line, $xml ) ) {
00295 $good = true;
00296 } else {
00297 return false;
00298 }
00299 } elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
00300 # Multi-page
00301 $parentLevel = strlen( $m[1] );
00302 # Find DIRM
00303 $line = strtok( "\n" );
00304 while ( $line !== false ) {
00305 $childLevel = strspn( $line, ' ' );
00306 if ( $childLevel <= $parentLevel ) {
00307 # End of chunk
00308 break;
00309 }
00310
00311 if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
00312 wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
00313 return false;
00314 }
00315 if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
00316 # Found page
00317 if ( $this->parseFormDjvu( $line, $xml ) ) {
00318 $good = true;
00319 } else {
00320 return false;
00321 }
00322 }
00323 $line = strtok( "\n" );
00324 }
00325 }
00326 if ( !$good ) {
00327 return false;
00328 }
00329
00330 $xml .= "</BODY>\n</DjVuXML>\n";
00331 return $xml;
00332 }
00333
00334 function parseFormDjvu( $line, &$xml ) {
00335 $parentLevel = strspn( $line, ' ' );
00336 $line = strtok( "\n" );
00337
00338 # Find INFO
00339 while ( $line !== false ) {
00340 $childLevel = strspn( $line, ' ' );
00341 if ( $childLevel <= $parentLevel ) {
00342 # End of chunk
00343 break;
00344 }
00345
00346 if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
00347 $xml .= Xml::tags( 'OBJECT',
00348 array(
00349 #'data' => '',
00350 #'type' => 'image/x.djvu',
00351 'height' => $m[2],
00352 'width' => $m[1],
00353 #'usemap' => '',
00354 ),
00355 "\n" .
00356 Xml::element( 'PARAM', array( 'name' => 'DPI', 'value' => $m[3] ) ) . "\n" .
00357 Xml::element( 'PARAM', array( 'name' => 'GAMMA', 'value' => $m[4] ) ) . "\n"
00358 ) . "\n";
00359 return true;
00360 }
00361 $line = strtok( "\n" );
00362 }
00363 # Not found
00364 return false;
00365 }
00366 }