module.graphic.bmp.php 31 KB

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