PageRenderTime 88ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 1ms

/common/libraries/plugin/getid3/module.graphic.bmp.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 763 lines | 556 code | 79 blank | 128 comment | 93 complexity | 0cc0c746ffd03e4c71c899eaee76eda7 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0
  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.bmp.php //
  11. // module for analyzing BMP Image files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_bmp
  16. {
  17. function __construct(&$fd, &$ThisFileInfo, $ExtractPalette = false, $ExtractData = false)
  18. {
  19. // shortcuts
  20. $ThisFileInfo['bmp']['header']['raw'] = array();
  21. $thisfile_bmp = &$ThisFileInfo['bmp'];
  22. $thisfile_bmp_header = &$thisfile_bmp['header'];
  23. $thisfile_bmp_header_raw = &$thisfile_bmp_header['raw'];
  24. // BITMAPFILEHEADER [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp
  25. // all versions
  26. // WORD bfType;
  27. // DWORD bfSize;
  28. // WORD bfReserved1;
  29. // WORD bfReserved2;
  30. // DWORD bfOffBits;
  31. fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
  32. $offset = 0;
  33. $BMPheader = fread($fd, 14 + 40);
  34. $thisfile_bmp_header_raw['identifier'] = substr($BMPheader, $offset, 2);
  35. $offset += 2;
  36. if ($thisfile_bmp_header_raw['identifier'] != 'BM')
  37. {
  38. $ThisFileInfo['error'][] = 'Expecting "BM" at offset ' . $ThisFileInfo['avdataoffset'] . ', found "' . $thisfile_bmp_header_raw['identifier'] . '"';
  39. unset($ThisFileInfo['fileformat']);
  40. unset($ThisFileInfo['bmp']);
  41. return false;
  42. }
  43. $thisfile_bmp_header_raw['filesize'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  44. $offset += 4;
  45. $thisfile_bmp_header_raw['reserved1'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  46. $offset += 2;
  47. $thisfile_bmp_header_raw['reserved2'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  48. $offset += 2;
  49. $thisfile_bmp_header_raw['data_offset'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  50. $offset += 4;
  51. $thisfile_bmp_header_raw['header_size'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  52. $offset += 4;
  53. // check if the hardcoded-to-1 "planes" is at offset 22 or 26
  54. $planes22 = getid3_lib :: LittleEndian2Int(substr($BMPheader, 22, 2));
  55. $planes26 = getid3_lib :: LittleEndian2Int(substr($BMPheader, 26, 2));
  56. if (($planes22 == 1) && ($planes26 != 1))
  57. {
  58. $thisfile_bmp['type_os'] = 'OS/2';
  59. $thisfile_bmp['type_version'] = 1;
  60. }
  61. elseif (($planes26 == 1) && ($planes22 != 1))
  62. {
  63. $thisfile_bmp['type_os'] = 'Windows';
  64. $thisfile_bmp['type_version'] = 1;
  65. }
  66. elseif ($thisfile_bmp_header_raw['header_size'] == 12)
  67. {
  68. $thisfile_bmp['type_os'] = 'OS/2';
  69. $thisfile_bmp['type_version'] = 1;
  70. }
  71. elseif ($thisfile_bmp_header_raw['header_size'] == 40)
  72. {
  73. $thisfile_bmp['type_os'] = 'Windows';
  74. $thisfile_bmp['type_version'] = 1;
  75. }
  76. elseif ($thisfile_bmp_header_raw['header_size'] == 84)
  77. {
  78. $thisfile_bmp['type_os'] = 'Windows';
  79. $thisfile_bmp['type_version'] = 4;
  80. }
  81. elseif ($thisfile_bmp_header_raw['header_size'] == 100)
  82. {
  83. $thisfile_bmp['type_os'] = 'Windows';
  84. $thisfile_bmp['type_version'] = 5;
  85. }
  86. else
  87. {
  88. $ThisFileInfo['error'][] = 'Unknown BMP subtype (or not a BMP file)';
  89. unset($ThisFileInfo['fileformat']);
  90. unset($ThisFileInfo['bmp']);
  91. return false;
  92. }
  93. $ThisFileInfo['fileformat'] = 'bmp';
  94. $ThisFileInfo['video']['dataformat'] = 'bmp';
  95. $ThisFileInfo['video']['lossless'] = true;
  96. $ThisFileInfo['video']['pixel_aspect_ratio'] = (float) 1;
  97. if ($thisfile_bmp['type_os'] == 'OS/2')
  98. {
  99. // OS/2-format BMP
  100. // http://netghost.narod.ru/gff/graphics/summary/os2bmp.htm
  101. // DWORD Size; /* Size of this structure in bytes */
  102. // DWORD Width; /* Bitmap width in pixels */
  103. // DWORD Height; /* Bitmap height in pixel */
  104. // WORD NumPlanes; /* Number of bit planes (color depth) */
  105. // WORD BitsPerPixel; /* Number of bits per pixel per plane */
  106. $thisfile_bmp_header_raw['width'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  107. $offset += 2;
  108. $thisfile_bmp_header_raw['height'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  109. $offset += 2;
  110. $thisfile_bmp_header_raw['planes'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  111. $offset += 2;
  112. $thisfile_bmp_header_raw['bits_per_pixel'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  113. $offset += 2;
  114. $ThisFileInfo['video']['resolution_x'] = $thisfile_bmp_header_raw['width'];
  115. $ThisFileInfo['video']['resolution_y'] = $thisfile_bmp_header_raw['height'];
  116. $ThisFileInfo['video']['codec'] = 'BI_RGB ' . $thisfile_bmp_header_raw['bits_per_pixel'] . '-bit';
  117. $ThisFileInfo['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel'];
  118. if ($thisfile_bmp['type_version'] >= 2)
  119. {
  120. // DWORD Compression; /* Bitmap compression scheme */
  121. // DWORD ImageDataSize; /* Size of bitmap data in bytes */
  122. // DWORD XResolution; /* X resolution of display device */
  123. // DWORD YResolution; /* Y resolution of display device */
  124. // DWORD ColorsUsed; /* Number of color table indices used */
  125. // DWORD ColorsImportant; /* Number of important color indices */
  126. // WORD Units; /* Type of units used to measure resolution */
  127. // WORD Reserved; /* Pad structure to 4-byte boundary */
  128. // WORD Recording; /* Recording algorithm */
  129. // WORD Rendering; /* Halftoning algorithm used */
  130. // DWORD Size1; /* Reserved for halftoning algorithm use */
  131. // DWORD Size2; /* Reserved for halftoning algorithm use */
  132. // DWORD ColorEncoding; /* Color model used in bitmap */
  133. // DWORD Identifier; /* Reserved for application use */
  134. $thisfile_bmp_header_raw['compression'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  135. $offset += 4;
  136. $thisfile_bmp_header_raw['bmp_data_size'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  137. $offset += 4;
  138. $thisfile_bmp_header_raw['resolution_h'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  139. $offset += 4;
  140. $thisfile_bmp_header_raw['resolution_v'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  141. $offset += 4;
  142. $thisfile_bmp_header_raw['colors_used'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  143. $offset += 4;
  144. $thisfile_bmp_header_raw['colors_important'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  145. $offset += 4;
  146. $thisfile_bmp_header_raw['resolution_units'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  147. $offset += 2;
  148. $thisfile_bmp_header_raw['reserved1'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  149. $offset += 2;
  150. $thisfile_bmp_header_raw['recording'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  151. $offset += 2;
  152. $thisfile_bmp_header_raw['rendering'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  153. $offset += 2;
  154. $thisfile_bmp_header_raw['size1'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  155. $offset += 4;
  156. $thisfile_bmp_header_raw['size2'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  157. $offset += 4;
  158. $thisfile_bmp_header_raw['color_encoding'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  159. $offset += 4;
  160. $thisfile_bmp_header_raw['identifier'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  161. $offset += 4;
  162. $thisfile_bmp_header['compression'] = $this->BMPcompressionOS2Lookup($thisfile_bmp_header_raw['compression']);
  163. $ThisFileInfo['video']['codec'] = $thisfile_bmp_header['compression'] . ' ' . $thisfile_bmp_header_raw['bits_per_pixel'] . '-bit';
  164. }
  165. }
  166. elseif ($thisfile_bmp['type_os'] == 'Windows')
  167. {
  168. // Windows-format BMP
  169. // BITMAPINFOHEADER - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp
  170. // all versions
  171. // DWORD biSize;
  172. // LONG biWidth;
  173. // LONG biHeight;
  174. // WORD biPlanes;
  175. // WORD biBitCount;
  176. // DWORD biCompression;
  177. // DWORD biSizeImage;
  178. // LONG biXPelsPerMeter;
  179. // LONG biYPelsPerMeter;
  180. // DWORD biClrUsed;
  181. // DWORD biClrImportant;
  182. // possibly integrate this section and module.audio-video.riff.php::ParseBITMAPINFOHEADER() ?
  183. $thisfile_bmp_header_raw['width'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4), true);
  184. $offset += 4;
  185. $thisfile_bmp_header_raw['height'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4), true);
  186. $offset += 4;
  187. $thisfile_bmp_header_raw['planes'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  188. $offset += 2;
  189. $thisfile_bmp_header_raw['bits_per_pixel'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 2));
  190. $offset += 2;
  191. $thisfile_bmp_header_raw['compression'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  192. $offset += 4;
  193. $thisfile_bmp_header_raw['bmp_data_size'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  194. $offset += 4;
  195. $thisfile_bmp_header_raw['resolution_h'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4), true);
  196. $offset += 4;
  197. $thisfile_bmp_header_raw['resolution_v'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4), true);
  198. $offset += 4;
  199. $thisfile_bmp_header_raw['colors_used'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  200. $offset += 4;
  201. $thisfile_bmp_header_raw['colors_important'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  202. $offset += 4;
  203. $thisfile_bmp_header['compression'] = $this->BMPcompressionWindowsLookup($thisfile_bmp_header_raw['compression']);
  204. $ThisFileInfo['video']['resolution_x'] = $thisfile_bmp_header_raw['width'];
  205. $ThisFileInfo['video']['resolution_y'] = $thisfile_bmp_header_raw['height'];
  206. $ThisFileInfo['video']['codec'] = $thisfile_bmp_header['compression'] . ' ' . $thisfile_bmp_header_raw['bits_per_pixel'] . '-bit';
  207. $ThisFileInfo['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel'];
  208. if (($thisfile_bmp['type_version'] >= 4) || ($thisfile_bmp_header_raw['compression'] == 3))
  209. {
  210. // should only be v4+, but BMPs with type_version==1 and BI_BITFIELDS compression have been seen
  211. $BMPheader .= fread($fd, 44);
  212. // BITMAPV4HEADER - [44 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_2k1e.asp
  213. // Win95+, WinNT4.0+
  214. // DWORD bV4RedMask;
  215. // DWORD bV4GreenMask;
  216. // DWORD bV4BlueMask;
  217. // DWORD bV4AlphaMask;
  218. // DWORD bV4CSType;
  219. // CIEXYZTRIPLE bV4Endpoints;
  220. // DWORD bV4GammaRed;
  221. // DWORD bV4GammaGreen;
  222. // DWORD bV4GammaBlue;
  223. $thisfile_bmp_header_raw['red_mask'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  224. $offset += 4;
  225. $thisfile_bmp_header_raw['green_mask'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  226. $offset += 4;
  227. $thisfile_bmp_header_raw['blue_mask'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  228. $offset += 4;
  229. $thisfile_bmp_header_raw['alpha_mask'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  230. $offset += 4;
  231. $thisfile_bmp_header_raw['cs_type'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  232. $offset += 4;
  233. $thisfile_bmp_header_raw['ciexyz_red'] = substr($BMPheader, $offset, 4);
  234. $offset += 4;
  235. $thisfile_bmp_header_raw['ciexyz_green'] = substr($BMPheader, $offset, 4);
  236. $offset += 4;
  237. $thisfile_bmp_header_raw['ciexyz_blue'] = substr($BMPheader, $offset, 4);
  238. $offset += 4;
  239. $thisfile_bmp_header_raw['gamma_red'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  240. $offset += 4;
  241. $thisfile_bmp_header_raw['gamma_green'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  242. $offset += 4;
  243. $thisfile_bmp_header_raw['gamma_blue'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  244. $offset += 4;
  245. $thisfile_bmp_header['ciexyz_red'] = getid3_lib :: FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_red']));
  246. $thisfile_bmp_header['ciexyz_green'] = getid3_lib :: FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_green']));
  247. $thisfile_bmp_header['ciexyz_blue'] = getid3_lib :: FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_blue']));
  248. }
  249. if ($thisfile_bmp['type_version'] >= 5)
  250. {
  251. $BMPheader .= fread($fd, 16);
  252. // BITMAPV5HEADER - [16 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_7c36.asp
  253. // Win98+, Win2000+
  254. // DWORD bV5Intent;
  255. // DWORD bV5ProfileData;
  256. // DWORD bV5ProfileSize;
  257. // DWORD bV5Reserved;
  258. $thisfile_bmp_header_raw['intent'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  259. $offset += 4;
  260. $thisfile_bmp_header_raw['profile_data_offset'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  261. $offset += 4;
  262. $thisfile_bmp_header_raw['profile_data_size'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  263. $offset += 4;
  264. $thisfile_bmp_header_raw['reserved3'] = getid3_lib :: LittleEndian2Int(substr($BMPheader, $offset, 4));
  265. $offset += 4;
  266. }
  267. }
  268. else
  269. {
  270. $ThisFileInfo['error'][] = 'Unknown BMP format in header.';
  271. return false;
  272. }
  273. if ($ExtractPalette || $ExtractData)
  274. {
  275. $PaletteEntries = 0;
  276. if ($thisfile_bmp_header_raw['bits_per_pixel'] < 16)
  277. {
  278. $PaletteEntries = pow(2, $thisfile_bmp_header_raw['bits_per_pixel']);
  279. }
  280. elseif (isset($thisfile_bmp_header_raw['colors_used']) && ($thisfile_bmp_header_raw['colors_used'] > 0) && ($thisfile_bmp_header_raw['colors_used'] <= 256))
  281. {
  282. $PaletteEntries = $thisfile_bmp_header_raw['colors_used'];
  283. }
  284. if ($PaletteEntries > 0)
  285. {
  286. $BMPpalette = fread($fd, 4 * $PaletteEntries);
  287. $paletteoffset = 0;
  288. for($i = 0; $i < $PaletteEntries; $i ++)
  289. {
  290. // RGBQUAD - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_5f8y.asp
  291. // BYTE rgbBlue;
  292. // BYTE rgbGreen;
  293. // BYTE rgbRed;
  294. // BYTE rgbReserved;
  295. $blue = getid3_lib :: LittleEndian2Int(substr($BMPpalette, $paletteoffset ++, 1));
  296. $green = getid3_lib :: LittleEndian2Int(substr($BMPpalette, $paletteoffset ++, 1));
  297. $red = getid3_lib :: LittleEndian2Int(substr($BMPpalette, $paletteoffset ++, 1));
  298. if (($thisfile_bmp['type_os'] == 'OS/2') && ($thisfile_bmp['type_version'] == 1))
  299. {
  300. // no padding byte
  301. }
  302. else
  303. {
  304. $paletteoffset ++; // padding byte
  305. }
  306. $thisfile_bmp['palette'][$i] = (($red << 16) | ($green << 8) | $blue);
  307. }
  308. }
  309. }
  310. if ($ExtractData)
  311. {
  312. fseek($fd, $thisfile_bmp_header_raw['data_offset'], SEEK_SET);
  313. $RowByteLength = ceil(($thisfile_bmp_header_raw['width'] * ($thisfile_bmp_header_raw['bits_per_pixel'] / 8)) / 4) * 4; // round up to nearest DWORD boundry
  314. $BMPpixelData = fread($fd, $thisfile_bmp_header_raw['height'] * $RowByteLength);
  315. $pixeldataoffset = 0;
  316. switch (@$thisfile_bmp_header_raw['compression'])
  317. {
  318. case 0 : // BI_RGB
  319. switch ($thisfile_bmp_header_raw['bits_per_pixel'])
  320. {
  321. case 1 :
  322. for($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row --)
  323. {
  324. for($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col)
  325. {
  326. $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset ++});
  327. for($i = 7; $i >= 0; $i --)
  328. {
  329. $paletteindex = ($paletteindexbyte & (0x01 << $i)) >> $i;
  330. $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
  331. $col ++;
  332. }
  333. }
  334. while (($pixeldataoffset % 4) != 0)
  335. {
  336. // lines are padded to nearest DWORD
  337. $pixeldataoffset ++;
  338. }
  339. }
  340. break;
  341. case 4 :
  342. for($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row --)
  343. {
  344. for($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col)
  345. {
  346. $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset ++});
  347. for($i = 1; $i >= 0; $i --)
  348. {
  349. $paletteindex = ($paletteindexbyte & (0x0F << (4 * $i))) >> (4 * $i);
  350. $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
  351. $col ++;
  352. }
  353. }
  354. while (($pixeldataoffset % 4) != 0)
  355. {
  356. // lines are padded to nearest DWORD
  357. $pixeldataoffset ++;
  358. }
  359. }
  360. break;
  361. case 8 :
  362. for($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row --)
  363. {
  364. for($col = 0; $col < $thisfile_bmp_header_raw['width']; $col ++)
  365. {
  366. $paletteindex = ord($BMPpixelData{$pixeldataoffset ++});
  367. $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
  368. }
  369. while (($pixeldataoffset % 4) != 0)
  370. {
  371. // lines are padded to nearest DWORD
  372. $pixeldataoffset ++;
  373. }
  374. }
  375. break;
  376. case 24 :
  377. for($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row --)
  378. {
  379. for($col = 0; $col < $thisfile_bmp_header_raw['width']; $col ++)
  380. {
  381. $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset + 2}) << 16) | (ord($BMPpixelData{$pixeldataoffset + 1}) << 8) | ord($BMPpixelData{$pixeldataoffset});
  382. $pixeldataoffset += 3;
  383. }
  384. while (($pixeldataoffset % 4) != 0)
  385. {
  386. // lines are padded to nearest DWORD
  387. $pixeldataoffset ++;
  388. }
  389. }
  390. break;
  391. case 32 :
  392. for($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row --)
  393. {
  394. for($col = 0; $col < $thisfile_bmp_header_raw['width']; $col ++)
  395. {
  396. $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset + 3}) << 24) | (ord($BMPpixelData{$pixeldataoffset + 2}) << 16) | (ord($BMPpixelData{$pixeldataoffset + 1}) << 8) | ord($BMPpixelData{$pixeldataoffset});
  397. $pixeldataoffset += 4;
  398. }
  399. while (($pixeldataoffset % 4) != 0)
  400. {
  401. // lines are padded to nearest DWORD
  402. $pixeldataoffset ++;
  403. }
  404. }
  405. break;
  406. case 16 :
  407. // ?
  408. break;
  409. default :
  410. $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value (' . $thisfile_bmp_header_raw['bits_per_pixel'] . ') - cannot read pixel data';
  411. break;
  412. }
  413. break;
  414. case 1 : // BI_RLE8 - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_6x0u.asp
  415. switch ($thisfile_bmp_header_raw['bits_per_pixel'])
  416. {
  417. case 8 :
  418. $pixelcounter = 0;
  419. while ($pixeldataoffset < strlen($BMPpixelData))
  420. {
  421. $firstbyte = getid3_lib :: LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset ++, 1));
  422. $secondbyte = getid3_lib :: LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset ++, 1));
  423. if ($firstbyte == 0)
  424. {
  425. // escaped/absolute mode - the first byte of the pair can be set to zero to
  426. // indicate an escape character that denotes the end of a line, the end of
  427. // a bitmap, or a delta, depending on the value of the second byte.
  428. switch ($secondbyte)
  429. {
  430. case 0 :
  431. // end of line
  432. // no need for special processing, just ignore
  433. break;
  434. case 1 :
  435. // end of bitmap
  436. $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case
  437. break;
  438. case 2 :
  439. // delta - The 2 bytes following the escape contain unsigned values
  440. // indicating the horizontal and vertical offsets of the next pixel
  441. // from the current position.
  442. $colincrement = getid3_lib :: LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset ++, 1));
  443. $rowincrement = getid3_lib :: LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset ++, 1));
  444. $col = ($pixelcounter % $thisfile_bmp_header_raw['width']) + $colincrement;
  445. $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement;
  446. $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) + $col;
  447. break;
  448. default :
  449. // In absolute mode, the first byte is zero and the second byte is a
  450. // value in the range 03H through FFH. The second byte represents the
  451. // number of bytes that follow, each of which contains the color index
  452. // of a single pixel. Each run must be aligned on a word boundary.
  453. for($i = 0; $i < $secondbyte; $i ++)
  454. {
  455. $paletteindex = getid3_lib :: LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset ++, 1));
  456. $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
  457. $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
  458. $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
  459. $pixelcounter ++;
  460. }
  461. while (($pixeldataoffset % 2) != 0)
  462. {
  463. // Each run must be aligned on a word boundary.
  464. $pixeldataoffset ++;
  465. }
  466. break;
  467. }
  468. }
  469. else
  470. {
  471. // encoded mode - the first byte specifies the number of consecutive pixels
  472. // to be drawn using the color index contained in the second byte.
  473. for($i = 0; $i < $firstbyte; $i ++)
  474. {
  475. $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
  476. $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
  477. $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$secondbyte];
  478. $pixelcounter ++;
  479. }
  480. }
  481. }
  482. break;
  483. default :
  484. $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value (' . $thisfile_bmp_header_raw['bits_per_pixel'] . ') - cannot read pixel data';
  485. break;
  486. }
  487. break;
  488. case 2 : // BI_RLE4 - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_6x0u.asp
  489. switch ($thisfile_bmp_header_raw['bits_per_pixel'])
  490. {
  491. case 4 :
  492. $pixelcounter = 0;
  493. while ($pixeldataoffset < strlen($BMPpixelData))
  494. {
  495. $firstbyte = getid3_lib :: LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset ++, 1));
  496. $secondbyte = getid3_lib :: LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset ++, 1));
  497. if ($firstbyte == 0)
  498. {
  499. // escaped/absolute mode - the first byte of the pair can be set to zero to
  500. // indicate an escape character that denotes the end of a line, the end of
  501. // a bitmap, or a delta, depending on the value of the second byte.
  502. switch ($secondbyte)
  503. {
  504. case 0 :
  505. // end of line
  506. // no need for special processing, just ignore
  507. break;
  508. case 1 :
  509. // end of bitmap
  510. $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case
  511. break;
  512. case 2 :
  513. // delta - The 2 bytes following the escape contain unsigned values
  514. // indicating the horizontal and vertical offsets of the next pixel
  515. // from the current position.
  516. $colincrement = getid3_lib :: LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset ++, 1));
  517. $rowincrement = getid3_lib :: LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset ++, 1));
  518. $col = ($pixelcounter % $thisfile_bmp_header_raw['width']) + $colincrement;
  519. $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement;
  520. $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) + $col;
  521. break;
  522. default :
  523. // In absolute mode, the first byte is zero. The second byte contains the number
  524. // of color indexes that follow. Subsequent bytes contain color indexes in their
  525. // high- and low-order 4 bits, one color index for each pixel. In absolute mode,
  526. // each run must be aligned on a word boundary.
  527. unset($paletteindexes);
  528. for($i = 0; $i < ceil($secondbyte / 2); $i ++)
  529. {
  530. $paletteindexbyte = getid3_lib :: LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset ++, 1));
  531. $paletteindexes[] = ($paletteindexbyte & 0xF0) >> 4;
  532. $paletteindexes[] = ($paletteindexbyte & 0x0F);
  533. }
  534. while (($pixeldataoffset % 2) != 0)
  535. {
  536. // Each run must be aligned on a word boundary.
  537. $pixeldataoffset ++;
  538. }
  539. foreach ($paletteindexes as $paletteindex)
  540. {
  541. $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
  542. $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
  543. $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
  544. $pixelcounter ++;
  545. }
  546. break;
  547. }
  548. }
  549. else
  550. {
  551. // encoded mode - the first byte of the pair contains the number of pixels to be
  552. // drawn using the color indexes in the second byte. The second byte contains two
  553. // color indexes, one in its high-order 4 bits and one in its low-order 4 bits.
  554. // The first of the pixels is drawn using the color specified by the high-order
  555. // 4 bits, the second is drawn using the color in the low-order 4 bits, the third
  556. // is drawn using the color in the high-order 4 bits, and so on, until all the
  557. // pixels specified by the first byte have been drawn.
  558. $paletteindexes[0] = ($secondbyte & 0xF0) >> 4;
  559. $paletteindexes[1] = ($secondbyte & 0x0F);
  560. for($i = 0; $i < $firstbyte; $i ++)
  561. {
  562. $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
  563. $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
  564. $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindexes[($i % 2)]];
  565. $pixelcounter ++;
  566. }
  567. }
  568. }
  569. break;
  570. default :
  571. $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value (' . $thisfile_bmp_header_raw['bits_per_pixel'] . ') - cannot read pixel data';
  572. break;
  573. }
  574. break;
  575. case 3 : // BI_BITFIELDS
  576. switch ($thisfile_bmp_header_raw['bits_per_pixel'])
  577. {
  578. case 16 :
  579. case 32 :
  580. $redshift = 0;
  581. $greenshift = 0;
  582. $blueshift = 0;
  583. while ((($thisfile_bmp_header_raw['red_mask'] >> $redshift) & 0x01) == 0)
  584. {
  585. $redshift ++;
  586. }
  587. while ((($thisfile_bmp_header_raw['green_mask'] >> $greenshift) & 0x01) == 0)
  588. {
  589. $greenshift ++;
  590. }
  591. while ((($thisfile_bmp_header_raw['blue_mask'] >> $blueshift) & 0x01) == 0)
  592. {
  593. $blueshift ++;
  594. }
  595. for($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row --)
  596. {
  597. for($col = 0; $col < $thisfile_bmp_header_raw['width']; $col ++)
  598. {
  599. $pixelvalue = getid3_lib :: LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset, $thisfile_bmp_header_raw['bits_per_pixel'] / 8));
  600. $pixeldataoffset += $thisfile_bmp_header_raw['bits_per_pixel'] / 8;
  601. $red = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['red_mask']) >> $redshift) / ($thisfile_bmp_header_raw['red_mask'] >> $redshift)) * 255));
  602. $green = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['green_mask']) >> $greenshift) / ($thisfile_bmp_header_raw['green_mask'] >> $greenshift)) * 255));
  603. $blue = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['blue_mask']) >> $blueshift) / ($thisfile_bmp_header_raw['blue_mask'] >> $blueshift)) * 255));
  604. $thisfile_bmp['data'][$row][$col] = (($red << 16) | ($green << 8) | ($blue));
  605. }
  606. while (($pixeldataoffset % 4) != 0)
  607. {
  608. // lines are padded to nearest DWORD
  609. $pixeldataoffset ++;
  610. }
  611. }
  612. break;
  613. default :
  614. $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value (' . $thisfile_bmp_header_raw['bits_per_pixel'] . ') - cannot read pixel data';
  615. break;
  616. }
  617. break;
  618. default : // unhandled compression type
  619. $ThisFileInfo['error'][] = 'Unknown/unhandled compression type value (' . $thisfile_bmp_header_raw['compression'] . ') - cannot decompress pixel data';
  620. break;
  621. }
  622. }
  623. return true;
  624. }
  625. function PlotBMP(&$BMPinfo)
  626. {
  627. $starttime = time();
  628. if (! isset($BMPinfo['bmp']['data']) || ! is_array($BMPinfo['bmp']['data']))
  629. {
  630. echo 'ERROR: no pixel data<BR>';
  631. return false;
  632. }
  633. set_time_limit(intval(round($BMPinfo['resolution_x'] * $BMPinfo['resolution_y'] / 10000)));
  634. if ($im = ImageCreateTrueColor($BMPinfo['resolution_x'], $BMPinfo['resolution_y']))
  635. {
  636. for($row = 0; $row < $BMPinfo['resolution_y']; $row ++)
  637. {
  638. for($col = 0; $col < $BMPinfo['resolution_x']; $col ++)
  639. {
  640. if (isset($BMPinfo['bmp']['data'][$row][$col]))
  641. {
  642. $red = ($BMPinfo['bmp']['data'][$row][$col] & 0x00FF0000) >> 16;
  643. $green = ($BMPinfo['bmp']['data'][$row][$col] & 0x0000FF00) >> 8;
  644. $blue = ($BMPinfo['bmp']['data'][$row][$col] & 0x000000FF);
  645. $pixelcolor = ImageColorAllocate($im, $red, $green, $blue);
  646. ImageSetPixel($im, $col, $row, $pixelcolor);
  647. }
  648. else
  649. {
  650. //echo 'ERROR: no data for pixel '.$row.' x '.$col.'<BR>';
  651. //return false;
  652. }
  653. }
  654. }
  655. if (headers_sent())
  656. {
  657. echo 'plotted ' . ($BMPinfo['resolution_x'] * $BMPinfo['resolution_y']) . ' pixels in ' . (time() - $starttime) . ' seconds<BR>';
  658. ImageDestroy($im);
  659. exit();
  660. }
  661. else
  662. {
  663. header('Content-type: image/png');
  664. ImagePNG($im);
  665. ImageDestroy($im);
  666. return true;
  667. }
  668. }
  669. return false;
  670. }
  671. function BMPcompressionWindowsLookup($compressionid)
  672. {
  673. static $BMPcompressionWindowsLookup = array(0 => 'BI_RGB', 1 => 'BI_RLE8', 2 => 'BI_RLE4', 3 => 'BI_BITFIELDS',
  674. 4 => 'BI_JPEG', 5 => 'BI_PNG');
  675. return (isset($BMPcompressionWindowsLookup[$compressionid]) ? $BMPcompressionWindowsLookup[$compressionid] : 'invalid');
  676. }
  677. function BMPcompressionOS2Lookup($compressionid)
  678. {
  679. static $BMPcompressionOS2Lookup = array(0 => 'BI_RGB', 1 => 'BI_RLE8', 2 => 'BI_RLE4', 3 => 'Huffman 1D',
  680. 4 => 'BI_RLE24');
  681. return (isset($BMPcompressionOS2Lookup[$compressionid]) ? $BMPcompressionOS2Lookup[$compressionid] : 'invalid');
  682. }
  683. }
  684. ?>