90cbb38f0b620467a175b45069865ed8a91b34ec
2 /////////////////////////////////////////////////////////////////
3 /// getID3() by James Heinrich <info@getid3.org> //
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
7 /////////////////////////////////////////////////////////////////
8 // See readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.graphic.bmp.php //
12 // module for analyzing BMP Image files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
18 class getid3_bmp
extends getid3_handler
20 public $ExtractPalette = false;
21 public $ExtractData = false;
23 public function Analyze() {
24 $info = &$this->getid3
->info
;
27 $info['bmp']['header']['raw'] = array();
28 $thisfile_bmp = &$info['bmp'];
29 $thisfile_bmp_header = &$thisfile_bmp['header'];
30 $thisfile_bmp_header_raw = &$thisfile_bmp_header['raw'];
32 // BITMAPFILEHEADER [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp
40 $this->fseek($info['avdataoffset']);
42 $BMPheader = $this->fread(14 +
40);
44 $thisfile_bmp_header_raw['identifier'] = substr($BMPheader, $offset, 2);
48 if ($thisfile_bmp_header_raw['identifier'] != $magic) {
49 $this->error('Expecting "'.getid3_lib
::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes($thisfile_bmp_header_raw['identifier']).'"');
50 unset($info['fileformat']);
55 $thisfile_bmp_header_raw['filesize'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
57 $thisfile_bmp_header_raw['reserved1'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
59 $thisfile_bmp_header_raw['reserved2'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
61 $thisfile_bmp_header_raw['data_offset'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
63 $thisfile_bmp_header_raw['header_size'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
67 // check if the hardcoded-to-1 "planes" is at offset 22 or 26
68 $planes22 = getid3_lib
::LittleEndian2Int(substr($BMPheader, 22, 2));
69 $planes26 = getid3_lib
::LittleEndian2Int(substr($BMPheader, 26, 2));
70 if (($planes22 == 1) && ($planes26 != 1)) {
71 $thisfile_bmp['type_os'] = 'OS/2';
72 $thisfile_bmp['type_version'] = 1;
73 } elseif (($planes26 == 1) && ($planes22 != 1)) {
74 $thisfile_bmp['type_os'] = 'Windows';
75 $thisfile_bmp['type_version'] = 1;
76 } elseif ($thisfile_bmp_header_raw['header_size'] == 12) {
77 $thisfile_bmp['type_os'] = 'OS/2';
78 $thisfile_bmp['type_version'] = 1;
79 } elseif ($thisfile_bmp_header_raw['header_size'] == 40) {
80 $thisfile_bmp['type_os'] = 'Windows';
81 $thisfile_bmp['type_version'] = 1;
82 } elseif ($thisfile_bmp_header_raw['header_size'] == 84) {
83 $thisfile_bmp['type_os'] = 'Windows';
84 $thisfile_bmp['type_version'] = 4;
85 } elseif ($thisfile_bmp_header_raw['header_size'] == 100) {
86 $thisfile_bmp['type_os'] = 'Windows';
87 $thisfile_bmp['type_version'] = 5;
89 $this->error('Unknown BMP subtype (or not a BMP file)');
90 unset($info['fileformat']);
95 $info['fileformat'] = 'bmp';
96 $info['video']['dataformat'] = 'bmp';
97 $info['video']['lossless'] = true;
98 $info['video']['pixel_aspect_ratio'] = (float) 1;
100 if ($thisfile_bmp['type_os'] == 'OS/2') {
103 // http://netghost.narod.ru/gff/graphics/summary/os2bmp.htm
105 // DWORD Size; /* Size of this structure in bytes */
106 // DWORD Width; /* Bitmap width in pixels */
107 // DWORD Height; /* Bitmap height in pixel */
108 // WORD NumPlanes; /* Number of bit planes (color depth) */
109 // WORD BitsPerPixel; /* Number of bits per pixel per plane */
111 $thisfile_bmp_header_raw['width'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
113 $thisfile_bmp_header_raw['height'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
115 $thisfile_bmp_header_raw['planes'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
117 $thisfile_bmp_header_raw['bits_per_pixel'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
120 $info['video']['resolution_x'] = $thisfile_bmp_header_raw['width'];
121 $info['video']['resolution_y'] = $thisfile_bmp_header_raw['height'];
122 $info['video']['codec'] = 'BI_RGB '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
123 $info['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel'];
125 if ($thisfile_bmp['type_version'] >= 2) {
126 // DWORD Compression; /* Bitmap compression scheme */
127 // DWORD ImageDataSize; /* Size of bitmap data in bytes */
128 // DWORD XResolution; /* X resolution of display device */
129 // DWORD YResolution; /* Y resolution of display device */
130 // DWORD ColorsUsed; /* Number of color table indices used */
131 // DWORD ColorsImportant; /* Number of important color indices */
132 // WORD Units; /* Type of units used to measure resolution */
133 // WORD Reserved; /* Pad structure to 4-byte boundary */
134 // WORD Recording; /* Recording algorithm */
135 // WORD Rendering; /* Halftoning algorithm used */
136 // DWORD Size1; /* Reserved for halftoning algorithm use */
137 // DWORD Size2; /* Reserved for halftoning algorithm use */
138 // DWORD ColorEncoding; /* Color model used in bitmap */
139 // DWORD Identifier; /* Reserved for application use */
141 $thisfile_bmp_header_raw['compression'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
143 $thisfile_bmp_header_raw['bmp_data_size'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
145 $thisfile_bmp_header_raw['resolution_h'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
147 $thisfile_bmp_header_raw['resolution_v'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
149 $thisfile_bmp_header_raw['colors_used'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
151 $thisfile_bmp_header_raw['colors_important'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
153 $thisfile_bmp_header_raw['resolution_units'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
155 $thisfile_bmp_header_raw['reserved1'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
157 $thisfile_bmp_header_raw['recording'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
159 $thisfile_bmp_header_raw['rendering'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
161 $thisfile_bmp_header_raw['size1'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
163 $thisfile_bmp_header_raw['size2'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
165 $thisfile_bmp_header_raw['color_encoding'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
167 $thisfile_bmp_header_raw['identifier'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
170 $thisfile_bmp_header['compression'] = $this->BMPcompressionOS2Lookup($thisfile_bmp_header_raw['compression']);
172 $info['video']['codec'] = $thisfile_bmp_header['compression'].' '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
175 } elseif ($thisfile_bmp['type_os'] == 'Windows') {
177 // Windows-format BMP
179 // BITMAPINFOHEADER - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp
186 // DWORD biCompression;
187 // DWORD biSizeImage;
188 // LONG biXPelsPerMeter;
189 // LONG biYPelsPerMeter;
191 // DWORD biClrImportant;
193 // possibly integrate this section and module.audio-video.riff.php::ParseBITMAPINFOHEADER() ?
195 $thisfile_bmp_header_raw['width'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
197 $thisfile_bmp_header_raw['height'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
199 $thisfile_bmp_header_raw['planes'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
201 $thisfile_bmp_header_raw['bits_per_pixel'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
203 $thisfile_bmp_header_raw['compression'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
205 $thisfile_bmp_header_raw['bmp_data_size'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
207 $thisfile_bmp_header_raw['resolution_h'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
209 $thisfile_bmp_header_raw['resolution_v'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
211 $thisfile_bmp_header_raw['colors_used'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
213 $thisfile_bmp_header_raw['colors_important'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
216 $thisfile_bmp_header['compression'] = $this->BMPcompressionWindowsLookup($thisfile_bmp_header_raw['compression']);
217 $info['video']['resolution_x'] = $thisfile_bmp_header_raw['width'];
218 $info['video']['resolution_y'] = $thisfile_bmp_header_raw['height'];
219 $info['video']['codec'] = $thisfile_bmp_header['compression'].' '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
220 $info['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel'];
222 if (($thisfile_bmp['type_version'] >= 4) ||
($thisfile_bmp_header_raw['compression'] == 3)) {
223 // should only be v4+, but BMPs with type_version==1 and BI_BITFIELDS compression have been seen
224 $BMPheader .= $this->fread(44);
226 // BITMAPV4HEADER - [44 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_2k1e.asp
229 // DWORD bV4GreenMask;
230 // DWORD bV4BlueMask;
231 // DWORD bV4AlphaMask;
233 // CIEXYZTRIPLE bV4Endpoints;
234 // DWORD bV4GammaRed;
235 // DWORD bV4GammaGreen;
236 // DWORD bV4GammaBlue;
237 $thisfile_bmp_header_raw['red_mask'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
239 $thisfile_bmp_header_raw['green_mask'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
241 $thisfile_bmp_header_raw['blue_mask'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
243 $thisfile_bmp_header_raw['alpha_mask'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
245 $thisfile_bmp_header_raw['cs_type'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
247 $thisfile_bmp_header_raw['ciexyz_red'] = substr($BMPheader, $offset, 4);
249 $thisfile_bmp_header_raw['ciexyz_green'] = substr($BMPheader, $offset, 4);
251 $thisfile_bmp_header_raw['ciexyz_blue'] = substr($BMPheader, $offset, 4);
253 $thisfile_bmp_header_raw['gamma_red'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
255 $thisfile_bmp_header_raw['gamma_green'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
257 $thisfile_bmp_header_raw['gamma_blue'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
260 $thisfile_bmp_header['ciexyz_red'] = getid3_lib
::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_red']));
261 $thisfile_bmp_header['ciexyz_green'] = getid3_lib
::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_green']));
262 $thisfile_bmp_header['ciexyz_blue'] = getid3_lib
::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_blue']));
265 if ($thisfile_bmp['type_version'] >= 5) {
266 $BMPheader .= $this->fread(16);
268 // BITMAPV5HEADER - [16 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_7c36.asp
271 // DWORD bV5ProfileData;
272 // DWORD bV5ProfileSize;
273 // DWORD bV5Reserved;
274 $thisfile_bmp_header_raw['intent'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
276 $thisfile_bmp_header_raw['profile_data_offset'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
278 $thisfile_bmp_header_raw['profile_data_size'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
280 $thisfile_bmp_header_raw['reserved3'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
286 $this->error('Unknown BMP format in header.');
292 if ($this->ExtractPalette ||
$this->ExtractData
) {
294 if ($thisfile_bmp_header_raw['bits_per_pixel'] < 16) {
295 $PaletteEntries = pow(2, $thisfile_bmp_header_raw['bits_per_pixel']);
296 } elseif (isset($thisfile_bmp_header_raw['colors_used']) && ($thisfile_bmp_header_raw['colors_used'] > 0) && ($thisfile_bmp_header_raw['colors_used'] <= 256)) {
297 $PaletteEntries = $thisfile_bmp_header_raw['colors_used'];
299 if ($PaletteEntries > 0) {
300 $BMPpalette = $this->fread(4 * $PaletteEntries);
302 for ($i = 0; $i < $PaletteEntries; $i++
) {
303 // RGBQUAD - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_5f8y.asp
308 $blue = getid3_lib
::LittleEndian2Int(substr($BMPpalette, $paletteoffset++
, 1));
309 $green = getid3_lib
::LittleEndian2Int(substr($BMPpalette, $paletteoffset++
, 1));
310 $red = getid3_lib
::LittleEndian2Int(substr($BMPpalette, $paletteoffset++
, 1));
311 if (($thisfile_bmp['type_os'] == 'OS/2') && ($thisfile_bmp['type_version'] == 1)) {
314 $paletteoffset++
; // padding byte
316 $thisfile_bmp['palette'][$i] = (($red << 16) |
($green << 8) |
$blue);
321 if ($this->ExtractData
) {
322 $this->fseek($thisfile_bmp_header_raw['data_offset']);
323 $RowByteLength = ceil(($thisfile_bmp_header_raw['width'] * ($thisfile_bmp_header_raw['bits_per_pixel'] / 8)) / 4) * 4; // round up to nearest DWORD boundry
324 $BMPpixelData = $this->fread($thisfile_bmp_header_raw['height'] * $RowByteLength);
325 $pixeldataoffset = 0;
326 $thisfile_bmp_header_raw['compression'] = (isset($thisfile_bmp_header_raw['compression']) ?
$thisfile_bmp_header_raw['compression'] : '');
327 switch ($thisfile_bmp_header_raw['compression']) {
330 switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
332 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
333 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col) {
334 $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset++
});
335 for ($i = 7; $i >= 0; $i--) {
336 $paletteindex = ($paletteindexbyte & (0x01 << $i)) >> $i;
337 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
341 while (($pixeldataoffset %
4) != 0) {
342 // lines are padded to nearest DWORD
349 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
350 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col) {
351 $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset++
});
352 for ($i = 1; $i >= 0; $i--) {
353 $paletteindex = ($paletteindexbyte & (0x0F << (4 * $i))) >> (4 * $i);
354 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
358 while (($pixeldataoffset %
4) != 0) {
359 // lines are padded to nearest DWORD
366 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
367 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++
) {
368 $paletteindex = ord($BMPpixelData{$pixeldataoffset++
});
369 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
371 while (($pixeldataoffset %
4) != 0) {
372 // lines are padded to nearest DWORD
379 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
380 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++
) {
381 $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset+
2}) << 16) |
(ord($BMPpixelData{$pixeldataoffset+
1}) << 8) |
ord($BMPpixelData{$pixeldataoffset});
382 $pixeldataoffset +
= 3;
384 while (($pixeldataoffset %
4) != 0) {
385 // lines are padded to nearest DWORD
392 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
393 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++
) {
394 $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset+
3}) << 24) |
(ord($BMPpixelData{$pixeldataoffset+
2}) << 16) |
(ord($BMPpixelData{$pixeldataoffset+
1}) << 8) |
ord($BMPpixelData{$pixeldataoffset});
395 $pixeldataoffset +
= 4;
397 while (($pixeldataoffset %
4) != 0) {
398 // lines are padded to nearest DWORD
409 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
415 case 1: // BI_RLE8 - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_6x0u.asp
416 switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
419 while ($pixeldataoffset < strlen($BMPpixelData)) {
420 $firstbyte = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
421 $secondbyte = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
422 if ($firstbyte == 0) {
424 // escaped/absolute mode - the first byte of the pair can be set to zero to
425 // indicate an escape character that denotes the end of a line, the end of
426 // a bitmap, or a delta, depending on the value of the second byte.
427 switch ($secondbyte) {
430 // no need for special processing, just ignore
435 $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case
439 // delta - The 2 bytes following the escape contain unsigned values
440 // indicating the horizontal and vertical offsets of the next pixel
441 // from the current position.
442 $colincrement = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
443 $rowincrement = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
444 $col = ($pixelcounter %
$thisfile_bmp_header_raw['width']) +
$colincrement;
445 $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement;
446 $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) +
$col;
450 // In absolute mode, the first byte is zero and the second byte is a
451 // value in the range 03H through FFH. The second byte represents the
452 // number of bytes that follow, each of which contains the color index
453 // of a single pixel. Each run must be aligned on a word boundary.
454 for ($i = 0; $i < $secondbyte; $i++
) {
455 $paletteindex = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
456 $col = $pixelcounter %
$thisfile_bmp_header_raw['width'];
457 $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
458 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
461 while (($pixeldataoffset %
2) != 0) {
462 // Each run must be aligned on a word boundary.
470 // encoded mode - the first byte specifies the number of consecutive pixels
471 // to be drawn using the color index contained in the second byte.
472 for ($i = 0; $i < $firstbyte; $i++
) {
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'][$secondbyte];
484 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
491 case 2: // BI_RLE4 - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_6x0u.asp
492 switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
495 while ($pixeldataoffset < strlen($BMPpixelData)) {
496 $firstbyte = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
497 $secondbyte = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
498 if ($firstbyte == 0) {
500 // escaped/absolute mode - the first byte of the pair can be set to zero to
501 // indicate an escape character that denotes the end of a line, the end of
502 // a bitmap, or a delta, depending on the value of the second byte.
503 switch ($secondbyte) {
506 // no need for special processing, just ignore
511 $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case
515 // delta - The 2 bytes following the escape contain unsigned values
516 // indicating the horizontal and vertical offsets of the next pixel
517 // from the current position.
518 $colincrement = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
519 $rowincrement = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
520 $col = ($pixelcounter %
$thisfile_bmp_header_raw['width']) +
$colincrement;
521 $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement;
522 $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) +
$col;
526 // In absolute mode, the first byte is zero. The second byte contains the number
527 // of color indexes that follow. Subsequent bytes contain color indexes in their
528 // high- and low-order 4 bits, one color index for each pixel. In absolute mode,
529 // each run must be aligned on a word boundary.
530 unset($paletteindexes);
531 for ($i = 0; $i < ceil($secondbyte / 2); $i++
) {
532 $paletteindexbyte = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
533 $paletteindexes[] = ($paletteindexbyte & 0xF0) >> 4;
534 $paletteindexes[] = ($paletteindexbyte & 0x0F);
536 while (($pixeldataoffset %
2) != 0) {
537 // Each run must be aligned on a word boundary.
541 foreach ($paletteindexes as $paletteindex) {
542 $col = $pixelcounter %
$thisfile_bmp_header_raw['width'];
543 $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
544 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
552 // encoded mode - the first byte of the pair contains the number of pixels to be
553 // drawn using the color indexes in the second byte. The second byte contains two
554 // color indexes, one in its high-order 4 bits and one in its low-order 4 bits.
555 // The first of the pixels is drawn using the color specified by the high-order
556 // 4 bits, the second is drawn using the color in the low-order 4 bits, the third
557 // is drawn using the color in the high-order 4 bits, and so on, until all the
558 // pixels specified by the first byte have been drawn.
559 $paletteindexes[0] = ($secondbyte & 0xF0) >> 4;
560 $paletteindexes[1] = ($secondbyte & 0x0F);
561 for ($i = 0; $i < $firstbyte; $i++
) {
562 $col = $pixelcounter %
$thisfile_bmp_header_raw['width'];
563 $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
564 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindexes[($i %
2)]];
573 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
579 case 3: // BI_BITFIELDS
580 switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
586 while ((($thisfile_bmp_header_raw['red_mask'] >> $redshift) & 0x01) == 0) {
589 while ((($thisfile_bmp_header_raw['green_mask'] >> $greenshift) & 0x01) == 0) {
592 while ((($thisfile_bmp_header_raw['blue_mask'] >> $blueshift) & 0x01) == 0) {
595 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
596 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++
) {
597 $pixelvalue = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset, $thisfile_bmp_header_raw['bits_per_pixel'] / 8));
598 $pixeldataoffset +
= $thisfile_bmp_header_raw['bits_per_pixel'] / 8;
600 $red = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['red_mask']) >> $redshift) / ($thisfile_bmp_header_raw['red_mask'] >> $redshift)) * 255));
601 $green = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['green_mask']) >> $greenshift) / ($thisfile_bmp_header_raw['green_mask'] >> $greenshift)) * 255));
602 $blue = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['blue_mask']) >> $blueshift) / ($thisfile_bmp_header_raw['blue_mask'] >> $blueshift)) * 255));
603 $thisfile_bmp['data'][$row][$col] = (($red << 16) |
($green << 8) |
($blue));
605 while (($pixeldataoffset %
4) != 0) {
606 // lines are padded to nearest DWORD
613 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
619 default: // unhandled compression type
620 $this->error('Unknown/unhandled compression type value ('.$thisfile_bmp_header_raw['compression'].') - cannot decompress pixel data');
629 public function PlotBMP(&$BMPinfo) {
631 if (!isset($BMPinfo['bmp']['data']) ||
!is_array($BMPinfo['bmp']['data'])) {
632 echo 'ERROR: no pixel data<BR>';
635 set_time_limit(intval(round($BMPinfo['resolution_x'] * $BMPinfo['resolution_y'] / 10000)));
636 if ($im = ImageCreateTrueColor($BMPinfo['resolution_x'], $BMPinfo['resolution_y'])) {
637 for ($row = 0; $row < $BMPinfo['resolution_y']; $row++
) {
638 for ($col = 0; $col < $BMPinfo['resolution_x']; $col++
) {
639 if (isset($BMPinfo['bmp']['data'][$row][$col])) {
640 $red = ($BMPinfo['bmp']['data'][$row][$col] & 0x00FF0000) >> 16;
641 $green = ($BMPinfo['bmp']['data'][$row][$col] & 0x0000FF00) >> 8;
642 $blue = ($BMPinfo['bmp']['data'][$row][$col] & 0x000000FF);
643 $pixelcolor = ImageColorAllocate($im, $red, $green, $blue);
644 ImageSetPixel($im, $col, $row, $pixelcolor);
646 //echo 'ERROR: no data for pixel '.$row.' x '.$col.'<BR>';
651 if (headers_sent()) {
652 echo 'plotted '.($BMPinfo['resolution_x'] * $BMPinfo['resolution_y']).' pixels in '.(time() - $starttime).' seconds<BR>';
656 header('Content-type: image/png');
665 public function BMPcompressionWindowsLookup($compressionid) {
666 static $BMPcompressionWindowsLookup = array(
674 return (isset($BMPcompressionWindowsLookup[$compressionid]) ?
$BMPcompressionWindowsLookup[$compressionid] : 'invalid');
677 public function BMPcompressionOS2Lookup($compressionid) {
678 static $BMPcompressionOS2Lookup = array(
685 return (isset($BMPcompressionOS2Lookup[$compressionid]) ?
$BMPcompressionOS2Lookup[$compressionid] : 'invalid');