module.graphic.png.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.graphic.png.php //
  11. // module for analyzing PNG Image files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_png extends getid3_handler
  16. {
  17. function Analyze() {
  18. $info = &$this->getid3->info;
  19. // shortcut
  20. $info['png'] = array();
  21. $thisfile_png = &$info['png'];
  22. $info['fileformat'] = 'png';
  23. $info['video']['dataformat'] = 'png';
  24. $info['video']['lossless'] = false;
  25. fseek($this->getid3->fp, $info['avdataoffset'], SEEK_SET);
  26. $PNGfiledata = fread($this->getid3->fp, $this->getid3->fread_buffer_size());
  27. $offset = 0;
  28. $PNGidentifier = substr($PNGfiledata, $offset, 8); // $89 $50 $4E $47 $0D $0A $1A $0A
  29. $offset += 8;
  30. if ($PNGidentifier != "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") {
  31. $info['error'][] = 'First 8 bytes of file ('.getid3_lib::PrintHexBytes($PNGidentifier).') did not match expected PNG identifier';
  32. unset($info['fileformat']);
  33. return false;
  34. }
  35. while (((ftell($this->getid3->fp) - (strlen($PNGfiledata) - $offset)) < $info['filesize'])) {
  36. $chunk['data_length'] = getid3_lib::BigEndian2Int(substr($PNGfiledata, $offset, 4));
  37. $offset += 4;
  38. while (((strlen($PNGfiledata) - $offset) < ($chunk['data_length'] + 4)) && (ftell($this->getid3->fp) < $info['filesize'])) {
  39. $PNGfiledata .= fread($this->getid3->fp, $this->getid3->fread_buffer_size());
  40. }
  41. $chunk['type_text'] = substr($PNGfiledata, $offset, 4);
  42. $offset += 4;
  43. $chunk['type_raw'] = getid3_lib::BigEndian2Int($chunk['type_text']);
  44. $chunk['data'] = substr($PNGfiledata, $offset, $chunk['data_length']);
  45. $offset += $chunk['data_length'];
  46. $chunk['crc'] = getid3_lib::BigEndian2Int(substr($PNGfiledata, $offset, 4));
  47. $offset += 4;
  48. $chunk['flags']['ancilliary'] = (bool) ($chunk['type_raw'] & 0x20000000);
  49. $chunk['flags']['private'] = (bool) ($chunk['type_raw'] & 0x00200000);
  50. $chunk['flags']['reserved'] = (bool) ($chunk['type_raw'] & 0x00002000);
  51. $chunk['flags']['safe_to_copy'] = (bool) ($chunk['type_raw'] & 0x00000020);
  52. // shortcut
  53. $thisfile_png[$chunk['type_text']] = array();
  54. $thisfile_png_chunk_type_text = &$thisfile_png[$chunk['type_text']];
  55. switch ($chunk['type_text']) {
  56. case 'IHDR': // Image Header
  57. $thisfile_png_chunk_type_text['header'] = $chunk;
  58. $thisfile_png_chunk_type_text['width'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4));
  59. $thisfile_png_chunk_type_text['height'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4));
  60. $thisfile_png_chunk_type_text['raw']['bit_depth'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 8, 1));
  61. $thisfile_png_chunk_type_text['raw']['color_type'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 9, 1));
  62. $thisfile_png_chunk_type_text['raw']['compression_method'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 10, 1));
  63. $thisfile_png_chunk_type_text['raw']['filter_method'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 11, 1));
  64. $thisfile_png_chunk_type_text['raw']['interlace_method'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 12, 1));
  65. $thisfile_png_chunk_type_text['compression_method_text'] = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['raw']['compression_method']);
  66. $thisfile_png_chunk_type_text['color_type']['palette'] = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x01);
  67. $thisfile_png_chunk_type_text['color_type']['true_color'] = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x02);
  68. $thisfile_png_chunk_type_text['color_type']['alpha'] = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x04);
  69. $info['video']['resolution_x'] = $thisfile_png_chunk_type_text['width'];
  70. $info['video']['resolution_y'] = $thisfile_png_chunk_type_text['height'];
  71. $info['video']['bits_per_sample'] = $this->IHDRcalculateBitsPerSample($thisfile_png_chunk_type_text['raw']['color_type'], $thisfile_png_chunk_type_text['raw']['bit_depth']);
  72. break;
  73. case 'PLTE': // Palette
  74. $thisfile_png_chunk_type_text['header'] = $chunk;
  75. $paletteoffset = 0;
  76. for ($i = 0; $i <= 255; $i++) {
  77. //$thisfile_png_chunk_type_text['red'][$i] = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  78. //$thisfile_png_chunk_type_text['green'][$i] = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  79. //$thisfile_png_chunk_type_text['blue'][$i] = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  80. $red = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  81. $green = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  82. $blue = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  83. $thisfile_png_chunk_type_text[$i] = (($red << 16) | ($green << 8) | ($blue));
  84. }
  85. break;
  86. case 'tRNS': // Transparency
  87. $thisfile_png_chunk_type_text['header'] = $chunk;
  88. switch ($thisfile_png['IHDR']['raw']['color_type']) {
  89. case 0:
  90. $thisfile_png_chunk_type_text['transparent_color_gray'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
  91. break;
  92. case 2:
  93. $thisfile_png_chunk_type_text['transparent_color_red'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
  94. $thisfile_png_chunk_type_text['transparent_color_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 2));
  95. $thisfile_png_chunk_type_text['transparent_color_blue'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 2));
  96. break;
  97. case 3:
  98. for ($i = 0; $i < strlen($chunk['data']); $i++) {
  99. $thisfile_png_chunk_type_text['palette_opacity'][$i] = getid3_lib::BigEndian2Int(substr($chunk['data'], $i, 1));
  100. }
  101. break;
  102. case 4:
  103. case 6:
  104. $info['error'][] = 'Invalid color_type in tRNS chunk: '.$thisfile_png['IHDR']['raw']['color_type'];
  105. default:
  106. $info['warning'][] = 'Unhandled color_type in tRNS chunk: '.$thisfile_png['IHDR']['raw']['color_type'];
  107. break;
  108. }
  109. break;
  110. case 'gAMA': // Image Gamma
  111. $thisfile_png_chunk_type_text['header'] = $chunk;
  112. $thisfile_png_chunk_type_text['gamma'] = getid3_lib::BigEndian2Int($chunk['data']) / 100000;
  113. break;
  114. case 'cHRM': // Primary Chromaticities
  115. $thisfile_png_chunk_type_text['header'] = $chunk;
  116. $thisfile_png_chunk_type_text['white_x'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4)) / 100000;
  117. $thisfile_png_chunk_type_text['white_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4)) / 100000;
  118. $thisfile_png_chunk_type_text['red_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 8, 4)) / 100000;
  119. $thisfile_png_chunk_type_text['red_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 12, 4)) / 100000;
  120. $thisfile_png_chunk_type_text['green_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 16, 4)) / 100000;
  121. $thisfile_png_chunk_type_text['green_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 20, 4)) / 100000;
  122. $thisfile_png_chunk_type_text['blue_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 24, 4)) / 100000;
  123. $thisfile_png_chunk_type_text['blue_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 28, 4)) / 100000;
  124. break;
  125. case 'sRGB': // Standard RGB Color Space
  126. $thisfile_png_chunk_type_text['header'] = $chunk;
  127. $thisfile_png_chunk_type_text['reindering_intent'] = getid3_lib::BigEndian2Int($chunk['data']);
  128. $thisfile_png_chunk_type_text['reindering_intent_text'] = $this->PNGsRGBintentLookup($thisfile_png_chunk_type_text['reindering_intent']);
  129. break;
  130. case 'iCCP': // Embedded ICC Profile
  131. $thisfile_png_chunk_type_text['header'] = $chunk;
  132. list($profilename, $compressiondata) = explode("\x00", $chunk['data'], 2);
  133. $thisfile_png_chunk_type_text['profile_name'] = $profilename;
  134. $thisfile_png_chunk_type_text['compression_method'] = getid3_lib::BigEndian2Int(substr($compressiondata, 0, 1));
  135. $thisfile_png_chunk_type_text['compression_profile'] = substr($compressiondata, 1);
  136. $thisfile_png_chunk_type_text['compression_method_text'] = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['compression_method']);
  137. break;
  138. case 'tEXt': // Textual Data
  139. $thisfile_png_chunk_type_text['header'] = $chunk;
  140. list($keyword, $text) = explode("\x00", $chunk['data'], 2);
  141. $thisfile_png_chunk_type_text['keyword'] = $keyword;
  142. $thisfile_png_chunk_type_text['text'] = $text;
  143. $thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
  144. break;
  145. case 'zTXt': // Compressed Textual Data
  146. $thisfile_png_chunk_type_text['header'] = $chunk;
  147. list($keyword, $otherdata) = explode("\x00", $chunk['data'], 2);
  148. $thisfile_png_chunk_type_text['keyword'] = $keyword;
  149. $thisfile_png_chunk_type_text['compression_method'] = getid3_lib::BigEndian2Int(substr($otherdata, 0, 1));
  150. $thisfile_png_chunk_type_text['compressed_text'] = substr($otherdata, 1);
  151. $thisfile_png_chunk_type_text['compression_method_text'] = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['compression_method']);
  152. switch ($thisfile_png_chunk_type_text['compression_method']) {
  153. case 0:
  154. $thisfile_png_chunk_type_text['text'] = gzuncompress($thisfile_png_chunk_type_text['compressed_text']);
  155. break;
  156. default:
  157. // unknown compression method
  158. break;
  159. }
  160. if (isset($thisfile_png_chunk_type_text['text'])) {
  161. $thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
  162. }
  163. break;
  164. case 'iTXt': // International Textual Data
  165. $thisfile_png_chunk_type_text['header'] = $chunk;
  166. list($keyword, $otherdata) = explode("\x00", $chunk['data'], 2);
  167. $thisfile_png_chunk_type_text['keyword'] = $keyword;
  168. $thisfile_png_chunk_type_text['compression'] = (bool) getid3_lib::BigEndian2Int(substr($otherdata, 0, 1));
  169. $thisfile_png_chunk_type_text['compression_method'] = getid3_lib::BigEndian2Int(substr($otherdata, 1, 1));
  170. $thisfile_png_chunk_type_text['compression_method_text'] = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['compression_method']);
  171. list($languagetag, $translatedkeyword, $text) = explode("\x00", substr($otherdata, 2), 3);
  172. $thisfile_png_chunk_type_text['language_tag'] = $languagetag;
  173. $thisfile_png_chunk_type_text['translated_keyword'] = $translatedkeyword;
  174. if ($thisfile_png_chunk_type_text['compression']) {
  175. switch ($thisfile_png_chunk_type_text['compression_method']) {
  176. case 0:
  177. $thisfile_png_chunk_type_text['text'] = gzuncompress($text);
  178. break;
  179. default:
  180. // unknown compression method
  181. break;
  182. }
  183. } else {
  184. $thisfile_png_chunk_type_text['text'] = $text;
  185. }
  186. if (isset($thisfile_png_chunk_type_text['text'])) {
  187. $thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
  188. }
  189. break;
  190. case 'bKGD': // Background Color
  191. $thisfile_png_chunk_type_text['header'] = $chunk;
  192. switch ($thisfile_png['IHDR']['raw']['color_type']) {
  193. case 0:
  194. case 4:
  195. $thisfile_png_chunk_type_text['background_gray'] = getid3_lib::BigEndian2Int($chunk['data']);
  196. break;
  197. case 2:
  198. case 6:
  199. $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']));
  200. $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']));
  201. $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']));
  202. break;
  203. case 3:
  204. $thisfile_png_chunk_type_text['background_index'] = getid3_lib::BigEndian2Int($chunk['data']);
  205. break;
  206. default:
  207. break;
  208. }
  209. break;
  210. case 'pHYs': // Physical Pixel Dimensions
  211. $thisfile_png_chunk_type_text['header'] = $chunk;
  212. $thisfile_png_chunk_type_text['pixels_per_unit_x'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4));
  213. $thisfile_png_chunk_type_text['pixels_per_unit_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4));
  214. $thisfile_png_chunk_type_text['unit_specifier'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 8, 1));
  215. $thisfile_png_chunk_type_text['unit'] = $this->PNGpHYsUnitLookup($thisfile_png_chunk_type_text['unit_specifier']);
  216. break;
  217. case 'sBIT': // Significant Bits
  218. $thisfile_png_chunk_type_text['header'] = $chunk;
  219. switch ($thisfile_png['IHDR']['raw']['color_type']) {
  220. case 0:
  221. $thisfile_png_chunk_type_text['significant_bits_gray'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  222. break;
  223. case 2:
  224. case 3:
  225. $thisfile_png_chunk_type_text['significant_bits_red'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  226. $thisfile_png_chunk_type_text['significant_bits_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
  227. $thisfile_png_chunk_type_text['significant_bits_blue'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 1));
  228. break;
  229. case 4:
  230. $thisfile_png_chunk_type_text['significant_bits_gray'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  231. $thisfile_png_chunk_type_text['significant_bits_alpha'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
  232. break;
  233. case 6:
  234. $thisfile_png_chunk_type_text['significant_bits_red'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  235. $thisfile_png_chunk_type_text['significant_bits_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
  236. $thisfile_png_chunk_type_text['significant_bits_blue'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 1));
  237. $thisfile_png_chunk_type_text['significant_bits_alpha'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 3, 1));
  238. break;
  239. default:
  240. break;
  241. }
  242. break;
  243. case 'sPLT': // Suggested Palette
  244. $thisfile_png_chunk_type_text['header'] = $chunk;
  245. list($palettename, $otherdata) = explode("\x00", $chunk['data'], 2);
  246. $thisfile_png_chunk_type_text['palette_name'] = $palettename;
  247. $sPLToffset = 0;
  248. $thisfile_png_chunk_type_text['sample_depth_bits'] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, 1));
  249. $sPLToffset += 1;
  250. $thisfile_png_chunk_type_text['sample_depth_bytes'] = $thisfile_png_chunk_type_text['sample_depth_bits'] / 8;
  251. $paletteCounter = 0;
  252. while ($sPLToffset < strlen($otherdata)) {
  253. $thisfile_png_chunk_type_text['red'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
  254. $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
  255. $thisfile_png_chunk_type_text['green'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
  256. $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
  257. $thisfile_png_chunk_type_text['blue'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
  258. $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
  259. $thisfile_png_chunk_type_text['alpha'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
  260. $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
  261. $thisfile_png_chunk_type_text['frequency'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, 2));
  262. $sPLToffset += 2;
  263. $paletteCounter++;
  264. }
  265. break;
  266. case 'hIST': // Palette Histogram
  267. $thisfile_png_chunk_type_text['header'] = $chunk;
  268. $hISTcounter = 0;
  269. while ($hISTcounter < strlen($chunk['data'])) {
  270. $thisfile_png_chunk_type_text[$hISTcounter] = getid3_lib::BigEndian2Int(substr($chunk['data'], $hISTcounter / 2, 2));
  271. $hISTcounter += 2;
  272. }
  273. break;
  274. case 'tIME': // Image Last-Modification Time
  275. $thisfile_png_chunk_type_text['header'] = $chunk;
  276. $thisfile_png_chunk_type_text['year'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
  277. $thisfile_png_chunk_type_text['month'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 1));
  278. $thisfile_png_chunk_type_text['day'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 3, 1));
  279. $thisfile_png_chunk_type_text['hour'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 1));
  280. $thisfile_png_chunk_type_text['minute'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 5, 1));
  281. $thisfile_png_chunk_type_text['second'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 6, 1));
  282. $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']);
  283. break;
  284. case 'oFFs': // Image Offset
  285. $thisfile_png_chunk_type_text['header'] = $chunk;
  286. $thisfile_png_chunk_type_text['position_x'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4), false, true);
  287. $thisfile_png_chunk_type_text['position_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4), false, true);
  288. $thisfile_png_chunk_type_text['unit_specifier'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 8, 1));
  289. $thisfile_png_chunk_type_text['unit'] = $this->PNGoFFsUnitLookup($thisfile_png_chunk_type_text['unit_specifier']);
  290. break;
  291. case 'pCAL': // Calibration Of Pixel Values
  292. $thisfile_png_chunk_type_text['header'] = $chunk;
  293. list($calibrationname, $otherdata) = explode("\x00", $chunk['data'], 2);
  294. $thisfile_png_chunk_type_text['calibration_name'] = $calibrationname;
  295. $pCALoffset = 0;
  296. $thisfile_png_chunk_type_text['original_zero'] = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 4), false, true);
  297. $pCALoffset += 4;
  298. $thisfile_png_chunk_type_text['original_max'] = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 4), false, true);
  299. $pCALoffset += 4;
  300. $thisfile_png_chunk_type_text['equation_type'] = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 1));
  301. $pCALoffset += 1;
  302. $thisfile_png_chunk_type_text['equation_type_text'] = $this->PNGpCALequationTypeLookup($thisfile_png_chunk_type_text['equation_type']);
  303. $thisfile_png_chunk_type_text['parameter_count'] = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 1));
  304. $pCALoffset += 1;
  305. $thisfile_png_chunk_type_text['parameters'] = explode("\x00", substr($chunk['data'], $pCALoffset));
  306. break;
  307. case 'sCAL': // Physical Scale Of Image Subject
  308. $thisfile_png_chunk_type_text['header'] = $chunk;
  309. $thisfile_png_chunk_type_text['unit_specifier'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  310. $thisfile_png_chunk_type_text['unit'] = $this->PNGsCALUnitLookup($thisfile_png_chunk_type_text['unit_specifier']);
  311. list($pixelwidth, $pixelheight) = explode("\x00", substr($chunk['data'], 1));
  312. $thisfile_png_chunk_type_text['pixel_width'] = $pixelwidth;
  313. $thisfile_png_chunk_type_text['pixel_height'] = $pixelheight;
  314. break;
  315. case 'gIFg': // GIF Graphic Control Extension
  316. $gIFgCounter = 0;
  317. if (isset($thisfile_png_chunk_type_text) && is_array($thisfile_png_chunk_type_text)) {
  318. $gIFgCounter = count($thisfile_png_chunk_type_text);
  319. }
  320. $thisfile_png_chunk_type_text[$gIFgCounter]['header'] = $chunk;
  321. $thisfile_png_chunk_type_text[$gIFgCounter]['disposal_method'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  322. $thisfile_png_chunk_type_text[$gIFgCounter]['user_input_flag'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
  323. $thisfile_png_chunk_type_text[$gIFgCounter]['delay_time'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 2));
  324. break;
  325. case 'gIFx': // GIF Application Extension
  326. $gIFxCounter = 0;
  327. if (isset($thisfile_png_chunk_type_text) && is_array($thisfile_png_chunk_type_text)) {
  328. $gIFxCounter = count($thisfile_png_chunk_type_text);
  329. }
  330. $thisfile_png_chunk_type_text[$gIFxCounter]['header'] = $chunk;
  331. $thisfile_png_chunk_type_text[$gIFxCounter]['application_identifier'] = substr($chunk['data'], 0, 8);
  332. $thisfile_png_chunk_type_text[$gIFxCounter]['authentication_code'] = substr($chunk['data'], 8, 3);
  333. $thisfile_png_chunk_type_text[$gIFxCounter]['application_data'] = substr($chunk['data'], 11);
  334. break;
  335. case 'IDAT': // Image Data
  336. $idatinformationfieldindex = 0;
  337. if (isset($thisfile_png['IDAT']) && is_array($thisfile_png['IDAT'])) {
  338. $idatinformationfieldindex = count($thisfile_png['IDAT']);
  339. }
  340. unset($chunk['data']);
  341. $thisfile_png_chunk_type_text[$idatinformationfieldindex]['header'] = $chunk;
  342. break;
  343. case 'IEND': // Image Trailer
  344. $thisfile_png_chunk_type_text['header'] = $chunk;
  345. break;
  346. default:
  347. //unset($chunk['data']);
  348. $thisfile_png_chunk_type_text['header'] = $chunk;
  349. $info['warning'][] = 'Unhandled chunk type: '.$chunk['type_text'];
  350. break;
  351. }
  352. }
  353. return true;
  354. }
  355. function PNGsRGBintentLookup($sRGB) {
  356. static $PNGsRGBintentLookup = array(
  357. 0 => 'Perceptual',
  358. 1 => 'Relative colorimetric',
  359. 2 => 'Saturation',
  360. 3 => 'Absolute colorimetric'
  361. );
  362. return (isset($PNGsRGBintentLookup[$sRGB]) ? $PNGsRGBintentLookup[$sRGB] : 'invalid');
  363. }
  364. function PNGcompressionMethodLookup($compressionmethod) {
  365. static $PNGcompressionMethodLookup = array(
  366. 0 => 'deflate/inflate'
  367. );
  368. return (isset($PNGcompressionMethodLookup[$compressionmethod]) ? $PNGcompressionMethodLookup[$compressionmethod] : 'invalid');
  369. }
  370. function PNGpHYsUnitLookup($unitid) {
  371. static $PNGpHYsUnitLookup = array(
  372. 0 => 'unknown',
  373. 1 => 'meter'
  374. );
  375. return (isset($PNGpHYsUnitLookup[$unitid]) ? $PNGpHYsUnitLookup[$unitid] : 'invalid');
  376. }
  377. function PNGoFFsUnitLookup($unitid) {
  378. static $PNGoFFsUnitLookup = array(
  379. 0 => 'pixel',
  380. 1 => 'micrometer'
  381. );
  382. return (isset($PNGoFFsUnitLookup[$unitid]) ? $PNGoFFsUnitLookup[$unitid] : 'invalid');
  383. }
  384. function PNGpCALequationTypeLookup($equationtype) {
  385. static $PNGpCALequationTypeLookup = array(
  386. 0 => 'Linear mapping',
  387. 1 => 'Base-e exponential mapping',
  388. 2 => 'Arbitrary-base exponential mapping',
  389. 3 => 'Hyperbolic mapping'
  390. );
  391. return (isset($PNGpCALequationTypeLookup[$equationtype]) ? $PNGpCALequationTypeLookup[$equationtype] : 'invalid');
  392. }
  393. function PNGsCALUnitLookup($unitid) {
  394. static $PNGsCALUnitLookup = array(
  395. 0 => 'meter',
  396. 1 => 'radian'
  397. );
  398. return (isset($PNGsCALUnitLookup[$unitid]) ? $PNGsCALUnitLookup[$unitid] : 'invalid');
  399. }
  400. function IHDRcalculateBitsPerSample($color_type, $bit_depth) {
  401. switch ($color_type) {
  402. case 0: // Each pixel is a grayscale sample.
  403. return $bit_depth;
  404. break;
  405. case 2: // Each pixel is an R,G,B triple
  406. return 3 * $bit_depth;
  407. break;
  408. case 3: // Each pixel is a palette index; a PLTE chunk must appear.
  409. return $bit_depth;
  410. break;
  411. case 4: // Each pixel is a grayscale sample, followed by an alpha sample.
  412. return 2 * $bit_depth;
  413. break;
  414. case 6: // Each pixel is an R,G,B triple, followed by an alpha sample.
  415. return 4 * $bit_depth;
  416. break;
  417. }
  418. return false;
  419. }
  420. }
  421. ?>