PageRenderTime 67ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/getid3/module.graphic.bmp.php

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