module.graphic.png.php 25 KB

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