module.graphic.bmp.php 30 KB

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