1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Pdf
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Tiff.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/** Internally used classes */
require_once 'Zend/Pdf/Element/Array.php';
require_once 'Zend/Pdf/Element/Name.php';
require_once 'Zend/Pdf/Element/Numeric.php';
/** Zend_Pdf_Resource_Image */
require_once 'Zend/Pdf/Resource/Image.php';
/**
* TIFF image
*
* @package Zend_Pdf
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Pdf_Resource_Image_Tiff extends Zend_Pdf_Resource_Image
{
const TIFF_FIELD_TYPE_BYTE=1;
const TIFF_FIELD_TYPE_ASCII=2;
const TIFF_FIELD_TYPE_SHORT=3;
const TIFF_FIELD_TYPE_LONG=4;
const TIFF_FIELD_TYPE_RATIONAL=5;
const TIFF_TAG_IMAGE_WIDTH=256;
const TIFF_TAG_IMAGE_LENGTH=257; //Height
const TIFF_TAG_BITS_PER_SAMPLE=258;
const TIFF_TAG_COMPRESSION=259;
const TIFF_TAG_PHOTOMETRIC_INTERPRETATION=262;
const TIFF_TAG_STRIP_OFFSETS=273;
const TIFF_TAG_SAMPLES_PER_PIXEL=277;
const TIFF_TAG_STRIP_BYTE_COUNTS=279;
const TIFF_COMPRESSION_UNCOMPRESSED = 1;
const TIFF_COMPRESSION_CCITT1D = 2;
const TIFF_COMPRESSION_GROUP_3_FAX = 3;
const TIFF_COMPRESSION_GROUP_4_FAX = 4;
const TIFF_COMPRESSION_LZW = 5;
const TIFF_COMPRESSION_JPEG = 6;
const TIFF_COMPRESSION_FLATE = 8;
const TIFF_COMPRESSION_FLATE_OBSOLETE_CODE = 32946;
const TIFF_COMPRESSION_PACKBITS = 32773;
const TIFF_PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO=0;
const TIFF_PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO=1;
const TIFF_PHOTOMETRIC_INTERPRETATION_RGB=2;
const TIFF_PHOTOMETRIC_INTERPRETATION_RGB_INDEXED=3;
const TIFF_PHOTOMETRIC_INTERPRETATION_CMYK=5;
const TIFF_PHOTOMETRIC_INTERPRETATION_YCBCR=6;
const TIFF_PHOTOMETRIC_INTERPRETATION_CIELAB=8;
protected $_width;
protected $_height;
protected $_imageProperties;
protected $_endianType;
protected $_fileSize;
protected $_bitsPerSample;
protected $_compression;
protected $_filter;
protected $_colorCode;
protected $_whiteIsZero;
protected $_blackIsZero;
protected $_colorSpace;
protected $_imageDataOffset;
protected $_imageDataLength;
const TIFF_ENDIAN_BIG=0;
const TIFF_ENDIAN_LITTLE=1;
const UNPACK_TYPE_BYTE=0;
const UNPACK_TYPE_SHORT=1;
const UNPACK_TYPE_LONG=2;
const UNPACK_TYPE_RATIONAL=3;
/**
* Byte unpacking function
*
* Makes it possible to unpack bytes in one statement for enhanced logic readability.
*
* @param int $type
* @param string $bytes
* @throws Zend_Pdf_Exception
*/
protected function unpackBytes($type, $bytes) {
if(!isset($this->_endianType)) {
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception("The unpackBytes function can only be used after the endianness of the file is known");
}
switch($type) {
case Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_BYTE:
$format = 'C';
$unpacked = unpack($format, $bytes);
return $unpacked[1];
break;
case Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT:
$format = ($this->_endianType == Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_LITTLE)?'v':'n';
$unpacked = unpack($format, $bytes);
return $unpacked[1];
break;
case Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_LONG:
$format = ($this->_endianType == Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_LITTLE)?'V':'N';
$unpacked = unpack($format, $bytes);
return $unpacked[1];
break;
case Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_RATIONAL:
$format = ($this->_endianType == Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_LITTLE)?'V2':'N2';
$unpacked = unpack($format, $bytes);
return ($unpacked[1]/$unpacked[2]);
break;
}
}
/**
* Object constructor
*
* @param string $imageFileName
* @throws Zend_Pdf_Exception
*/
public function __construct($imageFileName)
{
if (($imageFile = @fopen($imageFileName, 'rb')) === false ) {
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception( "Can not open '$imageFileName' file for reading." );
}
$byteOrderIndicator = fread($imageFile, 2);
if($byteOrderIndicator == 'II') {
$this->_endianType = Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_LITTLE;
} else if($byteOrderIndicator == 'MM') {
$this->_endianType = Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_BIG;
} else {
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception( "Not a tiff file or Tiff corrupt. No byte order indication found" );
}
$version = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT, fread($imageFile, 2));
if($version != 42) {
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception( "Not a tiff file or Tiff corrupt. Incorrect version number." );
}
$ifdOffset = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_LONG, fread($imageFile, 4));
$fileStats = fstat($imageFile);
$this->_fileSize = $fileStats['size'];
/*
* Tiff files are stored as a series of Image File Directories (IFD) each direcctory
* has a specific number of entries each 12 bytes in length. At the end of the directories
* is four bytes pointing to the offset of the next IFD.
*/
while($ifdOffset > 0) {
if(fseek($imageFile, $ifdOffset, SEEK_SET) == -1 || $ifdOffset+2 >= $this->_fileSize) {
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception("Could not seek to the image file directory as indexed by the file. Likely cause is TIFF corruption. Offset: ". $ifdOffset);
}
$numDirEntries = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT, fread($imageFile, 2));
/*
* Since we now know how many entries are in this (IFD) we can extract the data.
* The format of a TIFF directory entry is:
*
* 2 bytes (short) tag code; See TIFF_TAG constants at the top for supported values. (There are many more in the spec)
* 2 bytes (short) field type
* 4 bytes (long) number of values, or value count.
* 4 bytes (mixed) data if the data will fit into 4 bytes or an offset if the data is too large.
*/
for($dirEntryIdx = 1; $dirEntryIdx <= $numDirEntries; $dirEntryIdx++) {
$tag = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT, fread($imageFile, 2));
$fieldType = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT, fread($imageFile, 2));
$valueCount = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_LONG, fread($imageFile, 4));
switch($fieldType) {
case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_BYTE:
$fieldLength = $valueCount;
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_ASCII:
$fieldLength = $valueCount;
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_SHORT:
$fieldLength = $valueCount * 2;
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_LONG:
$fieldLength = $valueCount * 4;
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_RATIONAL:
$fieldLength = $valueCount * 8;
break;
default:
$fieldLength = $valueCount;
}
$offsetBytes = fread($imageFile, 4);
if($fieldLength <= 4) {
switch($fieldType) {
case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_BYTE:
$value = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_BYTE, $offsetBytes);
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_ASCII:
//Fall through to next case
case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_LONG:
$value = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_LONG, $offsetBytes);
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_SHORT:
//Fall through to next case
default:
$value = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT, $offsetBytes);
}
} else {
$refOffset = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_LONG, $offsetBytes);
}
/*
* Linear tag processing is probably not the best way to do this. I've processed the tags according to the
* Tiff 6 specification and make some assumptions about when tags will be < 4 bytes and fit into $value and when
* they will be > 4 bytes and require seek/extraction of the offset. Same goes for extracting arrays of data, like
* the data offsets and length. This should be fixed in the future.
*/
switch($tag) {
case Zend_Pdf_Resource_Image_Tiff::TIFF_TAG_IMAGE_WIDTH:
$this->_width = $value;
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_TAG_IMAGE_LENGTH:
$this->_height = $value;
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_TAG_BITS_PER_SAMPLE:
if($valueCount>1) {
$fp = ftell($imageFile);
fseek($imageFile, $refOffset, SEEK_SET);
$this->_bitsPerSample = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT, fread($imageFile, 2));
fseek($imageFile, $fp, SEEK_SET);
} else {
$this->_bitsPerSample = $value;
}
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_TAG_COMPRESSION:
$this->_compression = $value;
switch($value) {
case Zend_Pdf_Resource_Image_Tiff::TIFF_COMPRESSION_UNCOMPRESSED:
$this->_filter = 'None';
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_COMPRESSION_CCITT1D:
//Fall through to next case
case Zend_Pdf_Resource_Image_Tiff::TIFF_COMPRESSION_GROUP_3_FAX:
//Fall through to next case
case Zend_Pdf_Resource_Image_Tiff::TIFF_COMPRESSION_GROUP_4_FAX:
$this->_filter = 'CCITTFaxDecode';
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception("CCITTFaxDecode Compression Mode Not Currently Supported");
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_COMPRESSION_LZW:
$this->_filter = 'LZWDecode';
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception("LZWDecode Compression Mode Not Currently Supported");
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_COMPRESSION_JPEG:
$this->_filter = 'DCTDecode'; //Should work, doesnt...
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception("JPEG Compression Mode Not Currently Supported");
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_COMPRESSION_FLATE:
//fall through to next case
case Zend_Pdf_Resource_Image_Tiff::TIFF_COMPRESSION_FLATE_OBSOLETE_CODE:
$this->_filter = 'FlateDecode';
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception("ZIP/Flate Compression Mode Not Currently Supported");
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_COMPRESSION_PACKBITS:
$this->_filter = 'RunLengthDecode';
break;
}
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_TAG_PHOTOMETRIC_INTERPRETATION:
$this->_colorCode = $value;
$this->_whiteIsZero = false;
$this->_blackIsZero = false;
switch($value) {
case Zend_Pdf_Resource_Image_Tiff::TIFF_PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO:
$this->_whiteIsZero = true;
$this->_colorSpace = 'DeviceGray';
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO:
$this->_blackIsZero = true;
$this->_colorSpace = 'DeviceGray';
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_PHOTOMETRIC_INTERPRETATION_YCBCR:
//fall through to next case
case Zend_Pdf_Resource_Image_Tiff::TIFF_PHOTOMETRIC_INTERPRETATION_RGB:
$this->_colorSpace = 'DeviceRGB';
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_PHOTOMETRIC_INTERPRETATION_RGB_INDEXED:
$this->_colorSpace = 'Indexed';
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_PHOTOMETRIC_INTERPRETATION_CMYK:
$this->_colorSpace = 'DeviceCMYK';
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_PHOTOMETRIC_INTERPRETATION_CIELAB:
$this->_colorSpace = 'Lab';
break;
default:
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception('TIFF: Unknown or Unsupported Color Type: '. $value);
}
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_TAG_STRIP_OFFSETS:
if($valueCount>1) {
$format = ($this->_endianType == Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_LITTLE)?'V*':'N*';
$fp = ftell($imageFile);
fseek($imageFile, $refOffset, SEEK_SET);
$stripOffsetsBytes = fread($imageFile, $fieldLength);
$this->_imageDataOffset = unpack($format, $stripOffsetsBytes);
fseek($imageFile, $fp, SEEK_SET);
} else {
$this->_imageDataOffset = $value;
}
break;
case Zend_Pdf_Resource_Image_Tiff::TIFF_TAG_STRIP_BYTE_COUNTS:
if($valueCount>1) {
$format = ($this->_endianType == Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_LITTLE)?'V*':'N*';
$fp = ftell($imageFile);
fseek($imageFile, $refOffset, SEEK_SET);
$stripByteCountsBytes = fread($imageFile, $fieldLength);
$this->_imageDataLength = unpack($format, $stripByteCountsBytes);
fseek($imageFile, $fp, SEEK_SET);
} else {
$this->_imageDataLength = $value;
}
break;
default:
//For debugging. It should be harmless to ignore unknown tags, though there is some good info in them.
//echo "Unknown tag detected: ". $tag . " value: ". $value;
}
}
$ifdOffset = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_LONG, fread($imageFile, 4));
}
if(!isset($this->_imageDataOffset) || !isset($this->_imageDataLength)) {
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception("TIFF: The image processed did not contain image data as expected.");
}
$imageDataBytes = '';
if(is_array($this->_imageDataOffset)) {
if(!is_array($this->_imageDataLength)) {
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception("TIFF: The image contained multiple data offsets but not multiple data lengths. Tiff may be corrupt.");
}
foreach($this->_imageDataOffset as $idx => $offset) {
fseek($imageFile, $this->_imageDataOffset[$idx], SEEK_SET);
$imageDataBytes .= fread($imageFile, $this->_imageDataLength[$idx]);
}
} else {
fseek($imageFile, $this->_imageDataOffset, SEEK_SET);
$imageDataBytes = fread($imageFile, $this->_imageDataLength);
}
if($imageDataBytes === '') {
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception("TIFF: No data. Image Corruption");
}
fclose($imageFile);
parent::__construct();
$imageDictionary = $this->_resource->dictionary;
if(!isset($this->_width) || !isset($this->_width)) {
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception("Problem reading tiff file. Tiff is probably corrupt.");
}
$this->_imageProperties = array();
$this->_imageProperties['bitDepth'] = $this->_bitsPerSample;
$this->_imageProperties['fileSize'] = $this->_fileSize;
$this->_imageProperties['TIFFendianType'] = $this->_endianType;
$this->_imageProperties['TIFFcompressionType'] = $this->_compression;
$this->_imageProperties['TIFFwhiteIsZero'] = $this->_whiteIsZero;
$this->_imageProperties['TIFFblackIsZero'] = $this->_blackIsZero;
$this->_imageProperties['TIFFcolorCode'] = $this->_colorCode;
$this->_imageProperties['TIFFimageDataOffset'] = $this->_imageDataOffset;
$this->_imageProperties['TIFFimageDataLength'] = $this->_imageDataLength;
$this->_imageProperties['PDFfilter'] = $this->_filter;
$this->_imageProperties['PDFcolorSpace'] = $this->_colorSpace;
$imageDictionary->Width = new Zend_Pdf_Element_Numeric($this->_width);
if($this->_whiteIsZero === true) {
$imageDictionary->Decode = new Zend_Pdf_Element_Array(array(new Zend_Pdf_Element_Numeric(1), new Zend_Pdf_Element_Numeric(0)));
}
$imageDictionary->Height = new Zend_Pdf_Element_Numeric($this->_height);
$imageDictionary->ColorSpace = new Zend_Pdf_Element_Name($this->_colorSpace);
$imageDictionary->BitsPerComponent = new Zend_Pdf_Element_Numeric($this->_bitsPerSample);
if(isset($this->_filter) && $this->_filter != 'None') {
$imageDictionary->Filter = new Zend_Pdf_Element_Name($this->_filter);
}
$this->_resource->value = $imageDataBytes;
$this->_resource->skipFilters();
}
/**
* Image width (defined in Zend_Pdf_Resource_Image_Interface)
*/
public function getPixelWidth() {
return $this->_width;
}
/**
* Image height (defined in Zend_Pdf_Resource_Image_Interface)
*/
public function getPixelHeight() {
return $this->_height;
}
/**
* Image properties (defined in Zend_Pdf_Resource_Image_Interface)
*/
public function getProperties() {
return $this->_imageProperties;
}
}
|