PageRenderTime 48ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/getid3/module.graphic.bmp.php

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