module.graphic.bmp.php 31 KB

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