*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Pdf / Resource / Image / Png.php
blob7aa87c0e8cad79fc2820f7480984b9526cfd1eec
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Pdf
17 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id: Png.php 16541 2009-07-07 06:59:03Z bkarwin $
22 /** Zend_Pdf_Resource_Image */
23 require_once 'Zend/Pdf/Resource/Image.php';
25 /** Zend_Pdf_Element_Numeric */
26 require_once 'Zend/Pdf/Element/Numeric.php';
28 /** Zend_Pdf_Element_Name */
29 require_once 'Zend/Pdf/Element/Name.php';
31 /** Zend_Pdf_ElementFactory */
32 require_once 'Zend/Pdf/ElementFactory.php';
34 /**
35 * PNG image
37 * @package Zend_Pdf
38 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
39 * @license http://framework.zend.com/license/new-bsd New BSD License
41 class Zend_Pdf_Resource_Image_Png extends Zend_Pdf_Resource_Image
43 const PNG_COMPRESSION_DEFAULT_STRATEGY = 0;
44 const PNG_COMPRESSION_FILTERED = 1;
45 const PNG_COMPRESSION_HUFFMAN_ONLY = 2;
46 const PNG_COMPRESSION_RLE = 3;
48 const PNG_FILTER_NONE = 0;
49 const PNG_FILTER_SUB = 1;
50 const PNG_FILTER_UP = 2;
51 const PNG_FILTER_AVERAGE = 3;
52 const PNG_FILTER_PAETH = 4;
54 const PNG_INTERLACING_DISABLED = 0;
55 const PNG_INTERLACING_ENABLED = 1;
57 const PNG_CHANNEL_GRAY = 0;
58 const PNG_CHANNEL_RGB = 2;
59 const PNG_CHANNEL_INDEXED = 3;
60 const PNG_CHANNEL_GRAY_ALPHA = 4;
61 const PNG_CHANNEL_RGB_ALPHA = 6;
63 protected $_width;
64 protected $_height;
65 protected $_imageProperties;
67 /**
68 * Object constructor
70 * @param string $imageFileName
71 * @throws Zend_Pdf_Exception
72 * @todo Add compression conversions to support compression strategys other than PNG_COMPRESSION_DEFAULT_STRATEGY.
73 * @todo Add pre-compression filtering.
74 * @todo Add interlaced image handling.
75 * @todo Add support for 16-bit images. Requires PDF version bump to 1.5 at least.
76 * @todo Add processing for all PNG chunks defined in the spec. gAMA etc.
77 * @todo Fix tRNS chunk support for Indexed Images to a SMask.
79 public function __construct($imageFileName)
81 if (($imageFile = @fopen($imageFileName, 'rb')) === false ) {
82 require_once 'Zend/Pdf/Exception.php';
83 throw new Zend_Pdf_Exception( "Can not open '$imageFileName' file for reading." );
86 parent::__construct();
88 //Check if the file is a PNG
89 fseek($imageFile, 1, SEEK_CUR); //First signature byte (%)
90 if ('PNG' != fread($imageFile, 3)) {
91 require_once 'Zend/Pdf/Exception.php';
92 throw new Zend_Pdf_Exception('Image is not a PNG');
94 fseek($imageFile, 12, SEEK_CUR); //Signature bytes (Includes the IHDR chunk) IHDR processed linerarly because it doesnt contain a variable chunk length
95 $wtmp = unpack('Ni',fread($imageFile, 4)); //Unpack a 4-Byte Long
96 $width = $wtmp['i'];
97 $htmp = unpack('Ni',fread($imageFile, 4));
98 $height = $htmp['i'];
99 $bits = ord(fread($imageFile, 1)); //Higher than 8 bit depths are only supported in later versions of PDF.
100 $color = ord(fread($imageFile, 1));
102 $compression = ord(fread($imageFile, 1));
103 $prefilter = ord(fread($imageFile,1));
105 if (($interlacing = ord(fread($imageFile,1))) != Zend_Pdf_Resource_Image_Png::PNG_INTERLACING_DISABLED) {
106 require_once 'Zend/Pdf/Exception.php';
107 throw new Zend_Pdf_Exception( "Only non-interlaced images are currently supported." );
110 $this->_width = $width;
111 $this->_height = $height;
112 $this->_imageProperties = array();
113 $this->_imageProperties['bitDepth'] = $bits;
114 $this->_imageProperties['pngColorType'] = $color;
115 $this->_imageProperties['pngFilterType'] = $prefilter;
116 $this->_imageProperties['pngCompressionType'] = $compression;
117 $this->_imageProperties['pngInterlacingType'] = $interlacing;
119 fseek($imageFile, 4, SEEK_CUR); //4 Byte Ending Sequence
120 $imageData = '';
123 * The following loop processes PNG chunks. 4 Byte Longs are packed first give the chunk length
124 * followed by the chunk signature, a four byte code. IDAT and IEND are manditory in any PNG.
126 while(($chunkLengthBytes = fread($imageFile, 4)) !== false) {
127 $chunkLengthtmp = unpack('Ni', $chunkLengthBytes);
128 $chunkLength = $chunkLengthtmp['i'];
129 $chunkType = fread($imageFile, 4);
130 switch($chunkType) {
131 case 'IDAT': //Image Data
133 * Reads the actual image data from the PNG file. Since we know at this point that the compression
134 * strategy is the default strategy, we also know that this data is Zip compressed. We will either copy
135 * the data directly to the PDF and provide the correct FlateDecode predictor, or decompress the data
136 * decode the filters and output the data as a raw pixel map.
138 $imageData .= fread($imageFile, $chunkLength);
139 fseek($imageFile, 4, SEEK_CUR);
140 break;
142 case 'PLTE': //Palette
143 $paletteData = fread($imageFile, $chunkLength);
144 fseek($imageFile, 4, SEEK_CUR);
145 break;
147 case 'tRNS': //Basic (non-alpha channel) transparency.
148 $trnsData = fread($imageFile, $chunkLength);
149 switch ($color) {
150 case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_GRAY:
151 $baseColor = ord(substr($trnsData, 1, 1));
152 $transparencyData = array(new Zend_Pdf_Element_Numeric($baseColor), new Zend_Pdf_Element_Numeric($baseColor));
153 break;
155 case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB:
156 $red = ord(substr($trnsData,1,1));
157 $green = ord(substr($trnsData,3,1));
158 $blue = ord(substr($trnsData,5,1));
159 $transparencyData = array(new Zend_Pdf_Element_Numeric($red), new Zend_Pdf_Element_Numeric($red), new Zend_Pdf_Element_Numeric($green), new Zend_Pdf_Element_Numeric($green), new Zend_Pdf_Element_Numeric($blue), new Zend_Pdf_Element_Numeric($blue));
160 break;
162 case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_INDEXED:
163 //Find the first transparent color in the index, we will mask that. (This is a bit of a hack. This should be a SMask and mask all entries values).
164 if(($trnsIdx = strpos($trnsData, chr(0))) !== false) {
165 $transparencyData = array(new Zend_Pdf_Element_Numeric($trnsIdx), new Zend_Pdf_Element_Numeric($trnsIdx));
167 break;
169 case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_GRAY_ALPHA:
170 // Fall through to the next case
172 case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB_ALPHA:
173 require_once 'Zend/Pdf/Exception.php';
174 throw new Zend_Pdf_Exception( "tRNS chunk illegal for Alpha Channel Images" );
175 break;
177 fseek($imageFile, 4, SEEK_CUR); //4 Byte Ending Sequence
178 break;
180 case 'IEND';
181 break 2; //End the loop too
183 default:
184 fseek($imageFile, $chunkLength + 4, SEEK_CUR); //Skip the section
185 break;
188 fclose($imageFile);
190 $compressed = true;
191 $imageDataTmp = '';
192 $smaskData = '';
193 switch ($color) {
194 case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB:
195 $colorSpace = new Zend_Pdf_Element_Name('DeviceRGB');
196 break;
198 case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_GRAY:
199 $colorSpace = new Zend_Pdf_Element_Name('DeviceGray');
200 break;
202 case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_INDEXED:
203 if(empty($paletteData)) {
204 require_once 'Zend/Pdf/Exception.php';
205 throw new Zend_Pdf_Exception( "PNG Corruption: No palette data read for indexed type PNG." );
207 $colorSpace = new Zend_Pdf_Element_Array();
208 $colorSpace->items[] = new Zend_Pdf_Element_Name('Indexed');
209 $colorSpace->items[] = new Zend_Pdf_Element_Name('DeviceRGB');
210 $colorSpace->items[] = new Zend_Pdf_Element_Numeric((strlen($paletteData)/3-1));
211 $paletteObject = $this->_objectFactory->newObject(new Zend_Pdf_Element_String_Binary($paletteData));
212 $colorSpace->items[] = $paletteObject;
213 break;
215 case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_GRAY_ALPHA:
217 * To decode PNG's with alpha data we must create two images from one. One image will contain the Gray data
218 * the other will contain the Gray transparency overlay data. The former will become the object data and the latter
219 * will become the Shadow Mask (SMask).
221 if($bits > 8) {
222 require_once 'Zend/Pdf/Exception.php';
223 throw new Zend_Pdf_Exception("Alpha PNGs with bit depth > 8 are not yet supported");
226 $colorSpace = new Zend_Pdf_Element_Name('DeviceGray');
228 $decodingObjFactory = Zend_Pdf_ElementFactory::createFactory(1);
229 $decodingStream = $decodingObjFactory->newStreamObject($imageData);
230 $decodingStream->dictionary->Filter = new Zend_Pdf_Element_Name('FlateDecode');
231 $decodingStream->dictionary->DecodeParms = new Zend_Pdf_Element_Dictionary();
232 $decodingStream->dictionary->DecodeParms->Predictor = new Zend_Pdf_Element_Numeric(15);
233 $decodingStream->dictionary->DecodeParms->Columns = new Zend_Pdf_Element_Numeric($width);
234 $decodingStream->dictionary->DecodeParms->Colors = new Zend_Pdf_Element_Numeric(2); //GreyAlpha
235 $decodingStream->dictionary->DecodeParms->BitsPerComponent = new Zend_Pdf_Element_Numeric($bits);
236 $decodingStream->skipFilters();
238 $pngDataRawDecoded = $decodingStream->value;
240 //Iterate every pixel and copy out gray data and alpha channel (this will be slow)
241 for($pixel = 0, $pixelcount = ($width * $height); $pixel < $pixelcount; $pixel++) {
242 $imageDataTmp .= $pngDataRawDecoded[($pixel*2)];
243 $smaskData .= $pngDataRawDecoded[($pixel*2)+1];
245 $compressed = false;
246 $imageData = $imageDataTmp; //Overwrite image data with the gray channel without alpha
247 break;
249 case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB_ALPHA:
251 * To decode PNG's with alpha data we must create two images from one. One image will contain the RGB data
252 * the other will contain the Gray transparency overlay data. The former will become the object data and the latter
253 * will become the Shadow Mask (SMask).
255 if($bits > 8) {
256 require_once 'Zend/Pdf/Exception.php';
257 throw new Zend_Pdf_Exception("Alpha PNGs with bit depth > 8 are not yet supported");
260 $colorSpace = new Zend_Pdf_Element_Name('DeviceRGB');
262 $decodingObjFactory = Zend_Pdf_ElementFactory::createFactory(1);
263 $decodingStream = $decodingObjFactory->newStreamObject($imageData);
264 $decodingStream->dictionary->Filter = new Zend_Pdf_Element_Name('FlateDecode');
265 $decodingStream->dictionary->DecodeParms = new Zend_Pdf_Element_Dictionary();
266 $decodingStream->dictionary->DecodeParms->Predictor = new Zend_Pdf_Element_Numeric(15);
267 $decodingStream->dictionary->DecodeParms->Columns = new Zend_Pdf_Element_Numeric($width);
268 $decodingStream->dictionary->DecodeParms->Colors = new Zend_Pdf_Element_Numeric(4); //RGBA
269 $decodingStream->dictionary->DecodeParms->BitsPerComponent = new Zend_Pdf_Element_Numeric($bits);
270 $decodingStream->skipFilters();
272 $pngDataRawDecoded = $decodingStream->value;
274 //Iterate every pixel and copy out rgb data and alpha channel (this will be slow)
275 for($pixel = 0, $pixelcount = ($width * $height); $pixel < $pixelcount; $pixel++) {
276 $imageDataTmp .= $pngDataRawDecoded[($pixel*4)+0] . $pngDataRawDecoded[($pixel*4)+1] . $pngDataRawDecoded[($pixel*4)+2];
277 $smaskData .= $pngDataRawDecoded[($pixel*4)+3];
280 $compressed = false;
281 $imageData = $imageDataTmp; //Overwrite image data with the RGB channel without alpha
282 break;
284 default:
285 require_once 'Zend/Pdf/Exception.php';
286 throw new Zend_Pdf_Exception( "PNG Corruption: Invalid color space." );
289 if(empty($imageData)) {
290 require_once 'Zend/Pdf/Exception.php';
291 throw new Zend_Pdf_Exception( "Corrupt PNG Image. Mandatory IDAT chunk not found." );
294 $imageDictionary = $this->_resource->dictionary;
295 if(!empty($smaskData)) {
297 * Includes the Alpha transparency data as a Gray Image, then assigns the image as the Shadow Mask for the main image data.
299 $smaskStream = $this->_objectFactory->newStreamObject($smaskData);
300 $smaskStream->dictionary->Type = new Zend_Pdf_Element_Name('XObject');
301 $smaskStream->dictionary->Subtype = new Zend_Pdf_Element_Name('Image');
302 $smaskStream->dictionary->Width = new Zend_Pdf_Element_Numeric($width);
303 $smaskStream->dictionary->Height = new Zend_Pdf_Element_Numeric($height);
304 $smaskStream->dictionary->ColorSpace = new Zend_Pdf_Element_Name('DeviceGray');
305 $smaskStream->dictionary->BitsPerComponent = new Zend_Pdf_Element_Numeric($bits);
306 $imageDictionary->SMask = $smaskStream;
308 // Encode stream with FlateDecode filter
309 $smaskStreamDecodeParms = array();
310 $smaskStreamDecodeParms['Predictor'] = new Zend_Pdf_Element_Numeric(15);
311 $smaskStreamDecodeParms['Columns'] = new Zend_Pdf_Element_Numeric($width);
312 $smaskStreamDecodeParms['Colors'] = new Zend_Pdf_Element_Numeric(1);
313 $smaskStreamDecodeParms['BitsPerComponent'] = new Zend_Pdf_Element_Numeric(8);
314 $smaskStream->dictionary->DecodeParms = new Zend_Pdf_Element_Dictionary($smaskStreamDecodeParms);
315 $smaskStream->dictionary->Filter = new Zend_Pdf_Element_Name('FlateDecode');
318 if(!empty($transparencyData)) {
319 //This is experimental and not properly tested.
320 $imageDictionary->Mask = new Zend_Pdf_Element_Array($transparencyData);
323 $imageDictionary->Width = new Zend_Pdf_Element_Numeric($width);
324 $imageDictionary->Height = new Zend_Pdf_Element_Numeric($height);
325 $imageDictionary->ColorSpace = $colorSpace;
326 $imageDictionary->BitsPerComponent = new Zend_Pdf_Element_Numeric($bits);
327 $imageDictionary->Filter = new Zend_Pdf_Element_Name('FlateDecode');
329 $decodeParms = array();
330 $decodeParms['Predictor'] = new Zend_Pdf_Element_Numeric(15); // Optimal prediction
331 $decodeParms['Columns'] = new Zend_Pdf_Element_Numeric($width);
332 $decodeParms['Colors'] = new Zend_Pdf_Element_Numeric((($color==Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB || $color==Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB_ALPHA)?(3):(1)));
333 $decodeParms['BitsPerComponent'] = new Zend_Pdf_Element_Numeric($bits);
334 $imageDictionary->DecodeParms = new Zend_Pdf_Element_Dictionary($decodeParms);
336 //Include only the image IDAT section data.
337 $this->_resource->value = $imageData;
339 //Skip double compression
340 if ($compressed) {
341 $this->_resource->skipFilters();
346 * Image width
348 public function getPixelWidth() {
349 return $this->_width;
353 * Image height
355 public function getPixelHeight() {
356 return $this->_height;
360 * Image properties
362 public function getProperties() {
363 return $this->_imageProperties;