1<?php
2
3/////////////////////////////////////////////////////////////////
4/// getID3() by James Heinrich <info@getid3.org>               //
5//  available at https://github.com/JamesHeinrich/getID3       //
6//            or https://www.getid3.org                        //
7//            or http://getid3.sourceforge.net                 //
8//  see readme.txt for more details                            //
9/////////////////////////////////////////////////////////////////
10//                                                             //
11// module.graphic.png.php                                      //
12// module for analyzing PNG Image files                        //
13// dependencies: NONE                                          //
14//                                                            ///
15/////////////////////////////////////////////////////////////////
16
17if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
18	exit;
19}
20
21class getid3_png extends getid3_handler
22{
23	/**
24	 * If data chunk is larger than this do not read it completely (getID3 only needs the first
25	 * few dozen bytes for parsing).
26	 *
27	 * @var int
28	 */
29	public $max_data_bytes = 10000000;
30
31	/**
32	 * @return bool
33	 */
34	public function Analyze() {
35
36		$info = &$this->getid3->info;
37
38		// shortcut
39		$info['png'] = array();
40		$thisfile_png = &$info['png'];
41
42		$info['fileformat']          = 'png';
43		$info['video']['dataformat'] = 'png';
44		$info['video']['lossless']   = false;
45
46		$this->fseek($info['avdataoffset']);
47		$PNGfiledata = $this->fread($this->getid3->fread_buffer_size());
48		$offset = 0;
49
50		$PNGidentifier = substr($PNGfiledata, $offset, 8); // $89 $50 $4E $47 $0D $0A $1A $0A
51		$offset += 8;
52
53		if ($PNGidentifier != "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") {
54			$this->error('First 8 bytes of file ('.getid3_lib::PrintHexBytes($PNGidentifier).') did not match expected PNG identifier');
55			unset($info['fileformat']);
56			return false;
57		}
58
59		while ((($this->ftell() - (strlen($PNGfiledata) - $offset)) < $info['filesize'])) {
60			$chunk['data_length'] = getid3_lib::BigEndian2Int(substr($PNGfiledata, $offset, 4));
61			if ($chunk['data_length'] === false) {
62				$this->error('Failed to read data_length at offset '.$offset);
63				return false;
64			}
65			$offset += 4;
66			$truncated_data = false;
67			while (((strlen($PNGfiledata) - $offset) < ($chunk['data_length'] + 4)) && ($this->ftell() < $info['filesize'])) {
68				if (strlen($PNGfiledata) < $this->max_data_bytes) {
69					$PNGfiledata .= $this->fread($this->getid3->fread_buffer_size());
70				} else {
71					$this->warning('At offset '.$offset.' chunk "'.substr($PNGfiledata, $offset, 4).'" exceeded max_data_bytes value of '.$this->max_data_bytes.', data chunk will be truncated at '.(strlen($PNGfiledata) - 8).' bytes');
72					break;
73				}
74			}
75			$chunk['type_text']   =                           substr($PNGfiledata, $offset, 4);
76			$offset += 4;
77			$chunk['type_raw']    = getid3_lib::BigEndian2Int($chunk['type_text']);
78			$chunk['data']        =                           substr($PNGfiledata, $offset, $chunk['data_length']);
79			$offset += $chunk['data_length'];
80			$chunk['crc']         = getid3_lib::BigEndian2Int(substr($PNGfiledata, $offset, 4));
81			$offset += 4;
82
83			$chunk['flags']['ancilliary']   = (bool) ($chunk['type_raw'] & 0x20000000);
84			$chunk['flags']['private']      = (bool) ($chunk['type_raw'] & 0x00200000);
85			$chunk['flags']['reserved']     = (bool) ($chunk['type_raw'] & 0x00002000);
86			$chunk['flags']['safe_to_copy'] = (bool) ($chunk['type_raw'] & 0x00000020);
87
88			// shortcut
89			$thisfile_png[$chunk['type_text']] = array();
90			$thisfile_png_chunk_type_text = &$thisfile_png[$chunk['type_text']];
91
92			switch ($chunk['type_text']) {
93
94				case 'IHDR': // Image Header
95					$thisfile_png_chunk_type_text['header'] = $chunk;
96					$thisfile_png_chunk_type_text['width']                     = getid3_lib::BigEndian2Int(substr($chunk['data'],  0, 4));
97					$thisfile_png_chunk_type_text['height']                    = getid3_lib::BigEndian2Int(substr($chunk['data'],  4, 4));
98					$thisfile_png_chunk_type_text['raw']['bit_depth']          = getid3_lib::BigEndian2Int(substr($chunk['data'],  8, 1));
99					$thisfile_png_chunk_type_text['raw']['color_type']         = getid3_lib::BigEndian2Int(substr($chunk['data'],  9, 1));
100					$thisfile_png_chunk_type_text['raw']['compression_method'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 10, 1));
101					$thisfile_png_chunk_type_text['raw']['filter_method']      = getid3_lib::BigEndian2Int(substr($chunk['data'], 11, 1));
102					$thisfile_png_chunk_type_text['raw']['interlace_method']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 12, 1));
103
104					$thisfile_png_chunk_type_text['compression_method_text']   = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['raw']['compression_method']);
105					$thisfile_png_chunk_type_text['color_type']['palette']     = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x01);
106					$thisfile_png_chunk_type_text['color_type']['true_color']  = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x02);
107					$thisfile_png_chunk_type_text['color_type']['alpha']       = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x04);
108
109					$info['video']['resolution_x']    = $thisfile_png_chunk_type_text['width'];
110					$info['video']['resolution_y']    = $thisfile_png_chunk_type_text['height'];
111
112					$info['video']['bits_per_sample'] = $this->IHDRcalculateBitsPerSample($thisfile_png_chunk_type_text['raw']['color_type'], $thisfile_png_chunk_type_text['raw']['bit_depth']);
113					break;
114
115
116				case 'PLTE': // Palette
117					$thisfile_png_chunk_type_text['header'] = $chunk;
118					$paletteoffset = 0;
119					for ($i = 0; $i <= 255; $i++) {
120						//$thisfile_png_chunk_type_text['red'][$i]   = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
121						//$thisfile_png_chunk_type_text['green'][$i] = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
122						//$thisfile_png_chunk_type_text['blue'][$i]  = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
123						$red   = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
124						$green = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
125						$blue  = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
126						$thisfile_png_chunk_type_text[$i] = (($red << 16) | ($green << 8) | ($blue));
127					}
128					break;
129
130
131				case 'tRNS': // Transparency
132					$thisfile_png_chunk_type_text['header'] = $chunk;
133					switch ($thisfile_png['IHDR']['raw']['color_type']) {
134						case 0:
135							$thisfile_png_chunk_type_text['transparent_color_gray']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
136							break;
137
138						case 2:
139							$thisfile_png_chunk_type_text['transparent_color_red']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
140							$thisfile_png_chunk_type_text['transparent_color_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 2));
141							$thisfile_png_chunk_type_text['transparent_color_blue']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 2));
142							break;
143
144						case 3:
145							for ($i = 0; $i < strlen($chunk['data']); $i++) {
146								$thisfile_png_chunk_type_text['palette_opacity'][$i] = getid3_lib::BigEndian2Int(substr($chunk['data'], $i, 1));
147							}
148							break;
149
150						case 4:
151						case 6:
152							$this->error('Invalid color_type in tRNS chunk: '.$thisfile_png['IHDR']['raw']['color_type']);
153							break;
154
155						default:
156							$this->warning('Unhandled color_type in tRNS chunk: '.$thisfile_png['IHDR']['raw']['color_type']);
157							break;
158					}
159					break;
160
161
162				case 'gAMA': // Image Gamma
163					$thisfile_png_chunk_type_text['header'] = $chunk;
164					$thisfile_png_chunk_type_text['gamma']  = getid3_lib::BigEndian2Int($chunk['data']) / 100000;
165					break;
166
167
168				case 'cHRM': // Primary Chromaticities
169					$thisfile_png_chunk_type_text['header']  = $chunk;
170					$thisfile_png_chunk_type_text['white_x'] = getid3_lib::BigEndian2Int(substr($chunk['data'],  0, 4)) / 100000;
171					$thisfile_png_chunk_type_text['white_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'],  4, 4)) / 100000;
172					$thisfile_png_chunk_type_text['red_y']   = getid3_lib::BigEndian2Int(substr($chunk['data'],  8, 4)) / 100000;
173					$thisfile_png_chunk_type_text['red_y']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 12, 4)) / 100000;
174					$thisfile_png_chunk_type_text['green_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 16, 4)) / 100000;
175					$thisfile_png_chunk_type_text['green_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 20, 4)) / 100000;
176					$thisfile_png_chunk_type_text['blue_y']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 24, 4)) / 100000;
177					$thisfile_png_chunk_type_text['blue_y']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 28, 4)) / 100000;
178					break;
179
180
181				case 'sRGB': // Standard RGB Color Space
182					$thisfile_png_chunk_type_text['header']                 = $chunk;
183					$thisfile_png_chunk_type_text['reindering_intent']      = getid3_lib::BigEndian2Int($chunk['data']);
184					$thisfile_png_chunk_type_text['reindering_intent_text'] = $this->PNGsRGBintentLookup($thisfile_png_chunk_type_text['reindering_intent']);
185					break;
186
187
188				case 'iCCP': // Embedded ICC Profile
189					$thisfile_png_chunk_type_text['header']                  = $chunk;
190					list($profilename, $compressiondata)                     = explode("\x00", $chunk['data'], 2);
191					$thisfile_png_chunk_type_text['profile_name']            = $profilename;
192					$thisfile_png_chunk_type_text['compression_method']      = getid3_lib::BigEndian2Int(substr($compressiondata, 0, 1));
193					$thisfile_png_chunk_type_text['compression_profile']     = substr($compressiondata, 1);
194
195					$thisfile_png_chunk_type_text['compression_method_text'] = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['compression_method']);
196					break;
197
198
199				case 'tEXt': // Textual Data
200					$thisfile_png_chunk_type_text['header']  = $chunk;
201					list($keyword, $text) = explode("\x00", $chunk['data'], 2);
202					$thisfile_png_chunk_type_text['keyword'] = $keyword;
203					$thisfile_png_chunk_type_text['text']    = $text;
204
205					$thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
206					break;
207
208
209				case 'zTXt': // Compressed Textual Data
210					$thisfile_png_chunk_type_text['header']                  = $chunk;
211					list($keyword, $otherdata)                               = explode("\x00", $chunk['data'], 2);
212					$thisfile_png_chunk_type_text['keyword']                 = $keyword;
213					$thisfile_png_chunk_type_text['compression_method']      = getid3_lib::BigEndian2Int(substr($otherdata, 0, 1));
214					$thisfile_png_chunk_type_text['compressed_text']         = substr($otherdata, 1);
215					$thisfile_png_chunk_type_text['compression_method_text'] = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['compression_method']);
216					switch ($thisfile_png_chunk_type_text['compression_method']) {
217						case 0:
218							$thisfile_png_chunk_type_text['text']            = gzuncompress($thisfile_png_chunk_type_text['compressed_text']);
219							break;
220
221						default:
222							// unknown compression method
223							break;
224					}
225
226					if (isset($thisfile_png_chunk_type_text['text'])) {
227						$thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
228					}
229					break;
230
231
232				case 'iTXt': // International Textual Data
233					$thisfile_png_chunk_type_text['header']                  = $chunk;
234					list($keyword, $otherdata)                               = explode("\x00", $chunk['data'], 2);
235					$thisfile_png_chunk_type_text['keyword']                 = $keyword;
236					$thisfile_png_chunk_type_text['compression']             = (bool) getid3_lib::BigEndian2Int(substr($otherdata, 0, 1));
237					$thisfile_png_chunk_type_text['compression_method']      = getid3_lib::BigEndian2Int(substr($otherdata, 1, 1));
238					$thisfile_png_chunk_type_text['compression_method_text'] = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['compression_method']);
239					list($languagetag, $translatedkeyword, $text)                        = explode("\x00", substr($otherdata, 2), 3);
240					$thisfile_png_chunk_type_text['language_tag']            = $languagetag;
241					$thisfile_png_chunk_type_text['translated_keyword']      = $translatedkeyword;
242
243					if ($thisfile_png_chunk_type_text['compression']) {
244
245						switch ($thisfile_png_chunk_type_text['compression_method']) {
246							case 0:
247								$thisfile_png_chunk_type_text['text']        = gzuncompress($text);
248								break;
249
250							default:
251								// unknown compression method
252								break;
253						}
254
255					} else {
256
257						$thisfile_png_chunk_type_text['text']                = $text;
258
259					}
260
261					if (isset($thisfile_png_chunk_type_text['text'])) {
262						$thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
263					}
264					break;
265
266
267				case 'bKGD': // Background Color
268					$thisfile_png_chunk_type_text['header']                   = $chunk;
269					switch ($thisfile_png['IHDR']['raw']['color_type']) {
270						case 0:
271						case 4:
272							$thisfile_png_chunk_type_text['background_gray']  = getid3_lib::BigEndian2Int($chunk['data']);
273							break;
274
275						case 2:
276						case 6:
277							$thisfile_png_chunk_type_text['background_red']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 0 * $thisfile_png['IHDR']['raw']['bit_depth'], $thisfile_png['IHDR']['raw']['bit_depth']));
278							$thisfile_png_chunk_type_text['background_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1 * $thisfile_png['IHDR']['raw']['bit_depth'], $thisfile_png['IHDR']['raw']['bit_depth']));
279							$thisfile_png_chunk_type_text['background_blue']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 2 * $thisfile_png['IHDR']['raw']['bit_depth'], $thisfile_png['IHDR']['raw']['bit_depth']));
280							break;
281
282						case 3:
283							$thisfile_png_chunk_type_text['background_index'] = getid3_lib::BigEndian2Int($chunk['data']);
284							break;
285
286						default:
287							break;
288					}
289					break;
290
291
292				case 'pHYs': // Physical Pixel Dimensions
293					$thisfile_png_chunk_type_text['header']                 = $chunk;
294					$thisfile_png_chunk_type_text['pixels_per_unit_x']      = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4));
295					$thisfile_png_chunk_type_text['pixels_per_unit_y']      = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4));
296					$thisfile_png_chunk_type_text['unit_specifier']         = getid3_lib::BigEndian2Int(substr($chunk['data'], 8, 1));
297					$thisfile_png_chunk_type_text['unit']                   = $this->PNGpHYsUnitLookup($thisfile_png_chunk_type_text['unit_specifier']);
298					break;
299
300
301				case 'sBIT': // Significant Bits
302					$thisfile_png_chunk_type_text['header'] = $chunk;
303					switch ($thisfile_png['IHDR']['raw']['color_type']) {
304						case 0:
305							$thisfile_png_chunk_type_text['significant_bits_gray']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
306							break;
307
308						case 2:
309						case 3:
310							$thisfile_png_chunk_type_text['significant_bits_red']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
311							$thisfile_png_chunk_type_text['significant_bits_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
312							$thisfile_png_chunk_type_text['significant_bits_blue']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 1));
313							break;
314
315						case 4:
316							$thisfile_png_chunk_type_text['significant_bits_gray']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
317							$thisfile_png_chunk_type_text['significant_bits_alpha'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
318							break;
319
320						case 6:
321							$thisfile_png_chunk_type_text['significant_bits_red']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
322							$thisfile_png_chunk_type_text['significant_bits_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
323							$thisfile_png_chunk_type_text['significant_bits_blue']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 1));
324							$thisfile_png_chunk_type_text['significant_bits_alpha'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 3, 1));
325							break;
326
327						default:
328							break;
329					}
330					break;
331
332
333				case 'sPLT': // Suggested Palette
334					$thisfile_png_chunk_type_text['header']                           = $chunk;
335					list($palettename, $otherdata)                                    = explode("\x00", $chunk['data'], 2);
336					$thisfile_png_chunk_type_text['palette_name']                     = $palettename;
337					$sPLToffset = 0;
338					$thisfile_png_chunk_type_text['sample_depth_bits']                = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, 1));
339					$sPLToffset += 1;
340					$thisfile_png_chunk_type_text['sample_depth_bytes']               = $thisfile_png_chunk_type_text['sample_depth_bits'] / 8;
341					$paletteCounter = 0;
342					while ($sPLToffset < strlen($otherdata)) {
343						$thisfile_png_chunk_type_text['red'][$paletteCounter]       = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
344						$sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
345						$thisfile_png_chunk_type_text['green'][$paletteCounter]     = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
346						$sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
347						$thisfile_png_chunk_type_text['blue'][$paletteCounter]      = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
348						$sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
349						$thisfile_png_chunk_type_text['alpha'][$paletteCounter]     = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
350						$sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
351						$thisfile_png_chunk_type_text['frequency'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, 2));
352						$sPLToffset += 2;
353						$paletteCounter++;
354					}
355					break;
356
357
358				case 'hIST': // Palette Histogram
359					$thisfile_png_chunk_type_text['header'] = $chunk;
360					$hISTcounter = 0;
361					while ($hISTcounter < strlen($chunk['data'])) {
362						$thisfile_png_chunk_type_text[$hISTcounter] = getid3_lib::BigEndian2Int(substr($chunk['data'], $hISTcounter / 2, 2));
363						$hISTcounter += 2;
364					}
365					break;
366
367
368				case 'tIME': // Image Last-Modification Time
369					$thisfile_png_chunk_type_text['header'] = $chunk;
370					$thisfile_png_chunk_type_text['year']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
371					$thisfile_png_chunk_type_text['month']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 1));
372					$thisfile_png_chunk_type_text['day']    = getid3_lib::BigEndian2Int(substr($chunk['data'], 3, 1));
373					$thisfile_png_chunk_type_text['hour']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 1));
374					$thisfile_png_chunk_type_text['minute'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 5, 1));
375					$thisfile_png_chunk_type_text['second'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 6, 1));
376					$thisfile_png_chunk_type_text['unix']   = gmmktime($thisfile_png_chunk_type_text['hour'], $thisfile_png_chunk_type_text['minute'], $thisfile_png_chunk_type_text['second'], $thisfile_png_chunk_type_text['month'], $thisfile_png_chunk_type_text['day'], $thisfile_png_chunk_type_text['year']);
377					break;
378
379
380				case 'oFFs': // Image Offset
381					$thisfile_png_chunk_type_text['header']         = $chunk;
382					$thisfile_png_chunk_type_text['position_x']     = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4), false, true);
383					$thisfile_png_chunk_type_text['position_y']     = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4), false, true);
384					$thisfile_png_chunk_type_text['unit_specifier'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 8, 1));
385					$thisfile_png_chunk_type_text['unit']           = $this->PNGoFFsUnitLookup($thisfile_png_chunk_type_text['unit_specifier']);
386					break;
387
388
389				case 'pCAL': // Calibration Of Pixel Values
390					$thisfile_png_chunk_type_text['header']             = $chunk;
391					list($calibrationname, $otherdata)                              = explode("\x00", $chunk['data'], 2);
392					$thisfile_png_chunk_type_text['calibration_name']   = $calibrationname;
393					$pCALoffset = 0;
394					$thisfile_png_chunk_type_text['original_zero']      = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 4), false, true);
395					$pCALoffset += 4;
396					$thisfile_png_chunk_type_text['original_max']       = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 4), false, true);
397					$pCALoffset += 4;
398					$thisfile_png_chunk_type_text['equation_type']      = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 1));
399					$pCALoffset += 1;
400					$thisfile_png_chunk_type_text['equation_type_text'] = $this->PNGpCALequationTypeLookup($thisfile_png_chunk_type_text['equation_type']);
401					$thisfile_png_chunk_type_text['parameter_count']    = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 1));
402					$pCALoffset += 1;
403					$thisfile_png_chunk_type_text['parameters']         = explode("\x00", substr($chunk['data'], $pCALoffset));
404					break;
405
406
407				case 'sCAL': // Physical Scale Of Image Subject
408					$thisfile_png_chunk_type_text['header']         = $chunk;
409					$thisfile_png_chunk_type_text['unit_specifier'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
410					$thisfile_png_chunk_type_text['unit']           = $this->PNGsCALUnitLookup($thisfile_png_chunk_type_text['unit_specifier']);
411					list($pixelwidth, $pixelheight)                             = explode("\x00", substr($chunk['data'], 1));
412					$thisfile_png_chunk_type_text['pixel_width']    = $pixelwidth;
413					$thisfile_png_chunk_type_text['pixel_height']   = $pixelheight;
414					break;
415
416
417				case 'gIFg': // GIF Graphic Control Extension
418					$gIFgCounter = 0;
419					if (isset($thisfile_png_chunk_type_text) && is_array($thisfile_png_chunk_type_text)) {
420						$gIFgCounter = count($thisfile_png_chunk_type_text);
421					}
422					$thisfile_png_chunk_type_text[$gIFgCounter]['header']          = $chunk;
423					$thisfile_png_chunk_type_text[$gIFgCounter]['disposal_method'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
424					$thisfile_png_chunk_type_text[$gIFgCounter]['user_input_flag'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
425					$thisfile_png_chunk_type_text[$gIFgCounter]['delay_time']      = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 2));
426					break;
427
428
429				case 'gIFx': // GIF Application Extension
430					$gIFxCounter = 0;
431					if (isset($thisfile_png_chunk_type_text) && is_array($thisfile_png_chunk_type_text)) {
432						$gIFxCounter = count($thisfile_png_chunk_type_text);
433					}
434					$thisfile_png_chunk_type_text[$gIFxCounter]['header']                 = $chunk;
435					$thisfile_png_chunk_type_text[$gIFxCounter]['application_identifier'] = substr($chunk['data'],  0, 8);
436					$thisfile_png_chunk_type_text[$gIFxCounter]['authentication_code']    = substr($chunk['data'],  8, 3);
437					$thisfile_png_chunk_type_text[$gIFxCounter]['application_data']       = substr($chunk['data'], 11);
438					break;
439
440
441				case 'IDAT': // Image Data
442					$idatinformationfieldindex = 0;
443					if (isset($thisfile_png['IDAT']) && is_array($thisfile_png['IDAT'])) {
444						$idatinformationfieldindex = count($thisfile_png['IDAT']);
445					}
446					unset($chunk['data']);
447					$thisfile_png_chunk_type_text[$idatinformationfieldindex]['header'] = $chunk;
448					break;
449
450				case 'IEND': // Image Trailer
451					$thisfile_png_chunk_type_text['header'] = $chunk;
452					break;
453
454				case 'acTL': // Animation Control chunk
455					// https://wiki.mozilla.org/APNG_Specification#.60acTL.60:_The_Animation_Control_Chunk
456					$thisfile_png['animation']['num_frames'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4)); // Number of frames
457					$thisfile_png['animation']['num_plays']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4)); // Number of times to loop this APNG.  0 indicates infinite looping.
458
459					unset($chunk['data']);
460					$thisfile_png_chunk_type_text['header'] = $chunk;
461					break;
462
463				case 'fcTL': // Frame Control chunk
464					// https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk
465					$fcTL = array();
466					$fcTL['sequence_number'] = getid3_lib::BigEndian2Int(substr($chunk['data'],  0, 4)); // Sequence number of the animation chunk, starting from 0
467					$fcTL['width']           = getid3_lib::BigEndian2Int(substr($chunk['data'],  4, 4)); // Width of the following frame
468					$fcTL['height']          = getid3_lib::BigEndian2Int(substr($chunk['data'],  8, 4)); // Height of the following frame
469					$fcTL['x_offset']        = getid3_lib::BigEndian2Int(substr($chunk['data'], 12, 4)); // X position at which to render the following frame
470					$fcTL['y_offset']        = getid3_lib::BigEndian2Int(substr($chunk['data'], 16, 4)); // Y position at which to render the following frame
471					$fcTL['delay_num']       = getid3_lib::BigEndian2Int(substr($chunk['data'], 20, 2)); // Frame delay fraction numerator
472					$fcTL['delay_den']       = getid3_lib::BigEndian2Int(substr($chunk['data'], 22, 2)); // Frame delay fraction numerator
473					$fcTL['dispose_op']      = getid3_lib::BigEndian2Int(substr($chunk['data'], 23, 1)); // Type of frame area disposal to be done after rendering this frame
474					$fcTL['blend_op']        = getid3_lib::BigEndian2Int(substr($chunk['data'], 23, 1)); // Type of frame area rendering for this frame
475					if ($fcTL['delay_den']) {
476						$fcTL['delay'] = $fcTL['delay_num'] / $fcTL['delay_den'];
477					}
478					$thisfile_png['animation']['fcTL'][$fcTL['sequence_number']] = $fcTL;
479
480					unset($chunk['data']);
481					$thisfile_png_chunk_type_text['header'] = $chunk;
482					break;
483
484				case 'fdAT': // Frame Data chunk
485					// https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk
486					// "The `fdAT` chunk has the same purpose as an `IDAT` chunk. It has the same structure as an `IDAT` chunk, except preceded by a sequence number."
487					unset($chunk['data']);
488					$thisfile_png_chunk_type_text['header'] = $chunk;
489					break;
490
491				default:
492					//unset($chunk['data']);
493					$thisfile_png_chunk_type_text['header'] = $chunk;
494					$this->warning('Unhandled chunk type: '.$chunk['type_text']);
495					break;
496			}
497		}
498		if (!empty($thisfile_png['animation']['num_frames']) && !empty($thisfile_png['animation']['fcTL'])) {
499			$info['video']['dataformat'] = 'apng';
500			$info['playtime_seconds'] = 0;
501			foreach ($thisfile_png['animation']['fcTL'] as $seqno => $fcTL) {
502				$info['playtime_seconds'] += $fcTL['delay'];
503			}
504		}
505		return true;
506	}
507
508	/**
509	 * @param int $sRGB
510	 *
511	 * @return string
512	 */
513	public function PNGsRGBintentLookup($sRGB) {
514		static $PNGsRGBintentLookup = array(
515			0 => 'Perceptual',
516			1 => 'Relative colorimetric',
517			2 => 'Saturation',
518			3 => 'Absolute colorimetric'
519		);
520		return (isset($PNGsRGBintentLookup[$sRGB]) ? $PNGsRGBintentLookup[$sRGB] : 'invalid');
521	}
522
523	/**
524	 * @param int $compressionmethod
525	 *
526	 * @return string
527	 */
528	public function PNGcompressionMethodLookup($compressionmethod) {
529		static $PNGcompressionMethodLookup = array(
530			0 => 'deflate/inflate'
531		);
532		return (isset($PNGcompressionMethodLookup[$compressionmethod]) ? $PNGcompressionMethodLookup[$compressionmethod] : 'invalid');
533	}
534
535	/**
536	 * @param int $unitid
537	 *
538	 * @return string
539	 */
540	public function PNGpHYsUnitLookup($unitid) {
541		static $PNGpHYsUnitLookup = array(
542			0 => 'unknown',
543			1 => 'meter'
544		);
545		return (isset($PNGpHYsUnitLookup[$unitid]) ? $PNGpHYsUnitLookup[$unitid] : 'invalid');
546	}
547
548	/**
549	 * @param int $unitid
550	 *
551	 * @return string
552	 */
553	public function PNGoFFsUnitLookup($unitid) {
554		static $PNGoFFsUnitLookup = array(
555			0 => 'pixel',
556			1 => 'micrometer'
557		);
558		return (isset($PNGoFFsUnitLookup[$unitid]) ? $PNGoFFsUnitLookup[$unitid] : 'invalid');
559	}
560
561	/**
562	 * @param int $equationtype
563	 *
564	 * @return string
565	 */
566	public function PNGpCALequationTypeLookup($equationtype) {
567		static $PNGpCALequationTypeLookup = array(
568			0 => 'Linear mapping',
569			1 => 'Base-e exponential mapping',
570			2 => 'Arbitrary-base exponential mapping',
571			3 => 'Hyperbolic mapping'
572		);
573		return (isset($PNGpCALequationTypeLookup[$equationtype]) ? $PNGpCALequationTypeLookup[$equationtype] : 'invalid');
574	}
575
576	/**
577	 * @param int $unitid
578	 *
579	 * @return string
580	 */
581	public function PNGsCALUnitLookup($unitid) {
582		static $PNGsCALUnitLookup = array(
583			0 => 'meter',
584			1 => 'radian'
585		);
586		return (isset($PNGsCALUnitLookup[$unitid]) ? $PNGsCALUnitLookup[$unitid] : 'invalid');
587	}
588
589	/**
590	 * @param int $color_type
591	 * @param int $bit_depth
592	 *
593	 * @return int|false
594	 */
595	public function IHDRcalculateBitsPerSample($color_type, $bit_depth) {
596		switch ($color_type) {
597			case 0: // Each pixel is a grayscale sample.
598				return $bit_depth;
599
600			case 2: // Each pixel is an R,G,B triple
601				return 3 * $bit_depth;
602
603			case 3: // Each pixel is a palette index; a PLTE chunk must appear.
604				return $bit_depth;
605
606			case 4: // Each pixel is a grayscale sample, followed by an alpha sample.
607				return 2 * $bit_depth;
608
609			case 6: // Each pixel is an R,G,B triple, followed by an alpha sample.
610				return 4 * $bit_depth;
611		}
612		return false;
613	}
614
615}
616