3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
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;
26 public function Analyze() {
27 $info = &$this->getid3
->info
;
30 $info['bmp']['header']['raw'] = array();
31 $thisfile_bmp = &$info['bmp'];
32 $thisfile_bmp_header = &$thisfile_bmp['header'];
33 $thisfile_bmp_header_raw = &$thisfile_bmp_header['raw'];
35 // BITMAPFILEHEADER [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp
43 $this->fseek($info['avdataoffset']);
45 $BMPheader = $this->fread(14 +
40);
47 $thisfile_bmp_header_raw['identifier'] = substr($BMPheader, $offset, 2);
51 if ($thisfile_bmp_header_raw['identifier'] != $magic) {
52 $this->error('Expecting "'.getid3_lib
::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes($thisfile_bmp_header_raw['identifier']).'"');
53 unset($info['fileformat']);
58 $thisfile_bmp_header_raw['filesize'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
60 $thisfile_bmp_header_raw['reserved1'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
62 $thisfile_bmp_header_raw['reserved2'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
64 $thisfile_bmp_header_raw['data_offset'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
66 $thisfile_bmp_header_raw['header_size'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
70 // check if the hardcoded-to-1 "planes" is at offset 22 or 26
71 $planes22 = getid3_lib
::LittleEndian2Int(substr($BMPheader, 22, 2));
72 $planes26 = getid3_lib
::LittleEndian2Int(substr($BMPheader, 26, 2));
73 if (($planes22 == 1) && ($planes26 != 1)) {
74 $thisfile_bmp['type_os'] = 'OS/2';
75 $thisfile_bmp['type_version'] = 1;
76 } elseif (($planes26 == 1) && ($planes22 != 1)) {
77 $thisfile_bmp['type_os'] = 'Windows';
78 $thisfile_bmp['type_version'] = 1;
79 } elseif ($thisfile_bmp_header_raw['header_size'] == 12) {
80 $thisfile_bmp['type_os'] = 'OS/2';
81 $thisfile_bmp['type_version'] = 1;
82 } elseif ($thisfile_bmp_header_raw['header_size'] == 40) {
83 $thisfile_bmp['type_os'] = 'Windows';
84 $thisfile_bmp['type_version'] = 1;
85 } elseif ($thisfile_bmp_header_raw['header_size'] == 84) {
86 $thisfile_bmp['type_os'] = 'Windows';
87 $thisfile_bmp['type_version'] = 4;
88 } elseif ($thisfile_bmp_header_raw['header_size'] == 100) {
89 $thisfile_bmp['type_os'] = 'Windows';
90 $thisfile_bmp['type_version'] = 5;
92 $this->error('Unknown BMP subtype (or not a BMP file)');
93 unset($info['fileformat']);
98 $info['fileformat'] = 'bmp';
99 $info['video']['dataformat'] = 'bmp';
100 $info['video']['lossless'] = true;
101 $info['video']['pixel_aspect_ratio'] = (float) 1;
103 if ($thisfile_bmp['type_os'] == 'OS/2') {
106 // http://netghost.narod.ru/gff/graphics/summary/os2bmp.htm
108 // DWORD Size; /* Size of this structure in bytes */
109 // DWORD Width; /* Bitmap width in pixels */
110 // DWORD Height; /* Bitmap height in pixel */
111 // WORD NumPlanes; /* Number of bit planes (color depth) */
112 // WORD BitsPerPixel; /* Number of bits per pixel per plane */
114 $thisfile_bmp_header_raw['width'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
116 $thisfile_bmp_header_raw['height'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
118 $thisfile_bmp_header_raw['planes'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
120 $thisfile_bmp_header_raw['bits_per_pixel'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
123 $info['video']['resolution_x'] = $thisfile_bmp_header_raw['width'];
124 $info['video']['resolution_y'] = $thisfile_bmp_header_raw['height'];
125 $info['video']['codec'] = 'BI_RGB '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
126 $info['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel'];
128 if ($thisfile_bmp['type_version'] >= 2) {
129 // DWORD Compression; /* Bitmap compression scheme */
130 // DWORD ImageDataSize; /* Size of bitmap data in bytes */
131 // DWORD XResolution; /* X resolution of display device */
132 // DWORD YResolution; /* Y resolution of display device */
133 // DWORD ColorsUsed; /* Number of color table indices used */
134 // DWORD ColorsImportant; /* Number of important color indices */
135 // WORD Units; /* Type of units used to measure resolution */
136 // WORD Reserved; /* Pad structure to 4-byte boundary */
137 // WORD Recording; /* Recording algorithm */
138 // WORD Rendering; /* Halftoning algorithm used */
139 // DWORD Size1; /* Reserved for halftoning algorithm use */
140 // DWORD Size2; /* Reserved for halftoning algorithm use */
141 // DWORD ColorEncoding; /* Color model used in bitmap */
142 // DWORD Identifier; /* Reserved for application use */
144 $thisfile_bmp_header_raw['compression'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
146 $thisfile_bmp_header_raw['bmp_data_size'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
148 $thisfile_bmp_header_raw['resolution_h'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
150 $thisfile_bmp_header_raw['resolution_v'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
152 $thisfile_bmp_header_raw['colors_used'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
154 $thisfile_bmp_header_raw['colors_important'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
156 $thisfile_bmp_header_raw['resolution_units'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
158 $thisfile_bmp_header_raw['reserved1'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
160 $thisfile_bmp_header_raw['recording'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
162 $thisfile_bmp_header_raw['rendering'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
164 $thisfile_bmp_header_raw['size1'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
166 $thisfile_bmp_header_raw['size2'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
168 $thisfile_bmp_header_raw['color_encoding'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
170 $thisfile_bmp_header_raw['identifier'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
173 $thisfile_bmp_header['compression'] = $this->BMPcompressionOS2Lookup($thisfile_bmp_header_raw['compression']);
175 $info['video']['codec'] = $thisfile_bmp_header['compression'].' '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
178 } elseif ($thisfile_bmp['type_os'] == 'Windows') {
180 // Windows-format BMP
182 // BITMAPINFOHEADER - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp
189 // DWORD biCompression;
190 // DWORD biSizeImage;
191 // LONG biXPelsPerMeter;
192 // LONG biYPelsPerMeter;
194 // DWORD biClrImportant;
196 // possibly integrate this section and module.audio-video.riff.php::ParseBITMAPINFOHEADER() ?
198 $thisfile_bmp_header_raw['width'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
200 $thisfile_bmp_header_raw['height'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
202 $thisfile_bmp_header_raw['planes'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
204 $thisfile_bmp_header_raw['bits_per_pixel'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 2));
206 $thisfile_bmp_header_raw['compression'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
208 $thisfile_bmp_header_raw['bmp_data_size'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
210 $thisfile_bmp_header_raw['resolution_h'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
212 $thisfile_bmp_header_raw['resolution_v'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
214 $thisfile_bmp_header_raw['colors_used'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
216 $thisfile_bmp_header_raw['colors_important'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
219 $thisfile_bmp_header['compression'] = $this->BMPcompressionWindowsLookup($thisfile_bmp_header_raw['compression']);
220 $info['video']['resolution_x'] = $thisfile_bmp_header_raw['width'];
221 $info['video']['resolution_y'] = $thisfile_bmp_header_raw['height'];
222 $info['video']['codec'] = $thisfile_bmp_header['compression'].' '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
223 $info['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel'];
225 if (($thisfile_bmp['type_version'] >= 4) ||
($thisfile_bmp_header_raw['compression'] == 3)) {
226 // should only be v4+, but BMPs with type_version==1 and BI_BITFIELDS compression have been seen
227 $BMPheader .= $this->fread(44);
229 // BITMAPV4HEADER - [44 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_2k1e.asp
232 // DWORD bV4GreenMask;
233 // DWORD bV4BlueMask;
234 // DWORD bV4AlphaMask;
236 // CIEXYZTRIPLE bV4Endpoints;
237 // DWORD bV4GammaRed;
238 // DWORD bV4GammaGreen;
239 // DWORD bV4GammaBlue;
240 $thisfile_bmp_header_raw['red_mask'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
242 $thisfile_bmp_header_raw['green_mask'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
244 $thisfile_bmp_header_raw['blue_mask'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
246 $thisfile_bmp_header_raw['alpha_mask'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
248 $thisfile_bmp_header_raw['cs_type'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
250 $thisfile_bmp_header_raw['ciexyz_red'] = substr($BMPheader, $offset, 4);
252 $thisfile_bmp_header_raw['ciexyz_green'] = substr($BMPheader, $offset, 4);
254 $thisfile_bmp_header_raw['ciexyz_blue'] = substr($BMPheader, $offset, 4);
256 $thisfile_bmp_header_raw['gamma_red'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
258 $thisfile_bmp_header_raw['gamma_green'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
260 $thisfile_bmp_header_raw['gamma_blue'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
263 $thisfile_bmp_header['ciexyz_red'] = getid3_lib
::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_red']));
264 $thisfile_bmp_header['ciexyz_green'] = getid3_lib
::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_green']));
265 $thisfile_bmp_header['ciexyz_blue'] = getid3_lib
::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_blue']));
268 if ($thisfile_bmp['type_version'] >= 5) {
269 $BMPheader .= $this->fread(16);
271 // BITMAPV5HEADER - [16 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_7c36.asp
274 // DWORD bV5ProfileData;
275 // DWORD bV5ProfileSize;
276 // DWORD bV5Reserved;
277 $thisfile_bmp_header_raw['intent'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
279 $thisfile_bmp_header_raw['profile_data_offset'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
281 $thisfile_bmp_header_raw['profile_data_size'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
283 $thisfile_bmp_header_raw['reserved3'] = getid3_lib
::LittleEndian2Int(substr($BMPheader, $offset, 4));
289 $this->error('Unknown BMP format in header.');
295 if ($this->ExtractPalette ||
$this->ExtractData
) {
297 if ($thisfile_bmp_header_raw['bits_per_pixel'] < 16) {
298 $PaletteEntries = pow(2, $thisfile_bmp_header_raw['bits_per_pixel']);
299 } elseif (isset($thisfile_bmp_header_raw['colors_used']) && ($thisfile_bmp_header_raw['colors_used'] > 0) && ($thisfile_bmp_header_raw['colors_used'] <= 256)) {
300 $PaletteEntries = $thisfile_bmp_header_raw['colors_used'];
302 if ($PaletteEntries > 0) {
303 $BMPpalette = $this->fread(4 * $PaletteEntries);
305 for ($i = 0; $i < $PaletteEntries; $i++
) {
306 // RGBQUAD - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_5f8y.asp
311 $blue = getid3_lib
::LittleEndian2Int(substr($BMPpalette, $paletteoffset++
, 1));
312 $green = getid3_lib
::LittleEndian2Int(substr($BMPpalette, $paletteoffset++
, 1));
313 $red = getid3_lib
::LittleEndian2Int(substr($BMPpalette, $paletteoffset++
, 1));
314 if (($thisfile_bmp['type_os'] == 'OS/2') && ($thisfile_bmp['type_version'] == 1)) {
317 $paletteoffset++
; // padding byte
319 $thisfile_bmp['palette'][$i] = (($red << 16) |
($green << 8) |
$blue);
324 if ($this->ExtractData
) {
325 $this->fseek($thisfile_bmp_header_raw['data_offset']);
326 $RowByteLength = ceil(($thisfile_bmp_header_raw['width'] * ($thisfile_bmp_header_raw['bits_per_pixel'] / 8)) / 4) * 4; // round up to nearest DWORD boundry
327 $BMPpixelData = $this->fread($thisfile_bmp_header_raw['height'] * $RowByteLength);
328 $pixeldataoffset = 0;
329 $thisfile_bmp_header_raw['compression'] = (isset($thisfile_bmp_header_raw['compression']) ?
$thisfile_bmp_header_raw['compression'] : '');
330 switch ($thisfile_bmp_header_raw['compression']) {
333 switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
335 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
336 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col) {
337 $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset++
});
338 for ($i = 7; $i >= 0; $i--) {
339 $paletteindex = ($paletteindexbyte & (0x01 << $i)) >> $i;
340 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
344 while (($pixeldataoffset %
4) != 0) {
345 // lines are padded to nearest DWORD
352 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
353 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col) {
354 $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset++
});
355 for ($i = 1; $i >= 0; $i--) {
356 $paletteindex = ($paletteindexbyte & (0x0F << (4 * $i))) >> (4 * $i);
357 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
361 while (($pixeldataoffset %
4) != 0) {
362 // lines are padded to nearest DWORD
369 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
370 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++
) {
371 $paletteindex = ord($BMPpixelData{$pixeldataoffset++
});
372 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
374 while (($pixeldataoffset %
4) != 0) {
375 // lines are padded to nearest DWORD
382 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
383 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++
) {
384 $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset+
2}) << 16) |
(ord($BMPpixelData{$pixeldataoffset+
1}) << 8) |
ord($BMPpixelData{$pixeldataoffset});
385 $pixeldataoffset +
= 3;
387 while (($pixeldataoffset %
4) != 0) {
388 // lines are padded to nearest DWORD
395 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
396 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++
) {
397 $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset+
3}) << 24) |
(ord($BMPpixelData{$pixeldataoffset+
2}) << 16) |
(ord($BMPpixelData{$pixeldataoffset+
1}) << 8) |
ord($BMPpixelData{$pixeldataoffset});
398 $pixeldataoffset +
= 4;
400 while (($pixeldataoffset %
4) != 0) {
401 // lines are padded to nearest DWORD
412 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
418 case 1: // BI_RLE8 - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_6x0u.asp
419 switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
422 while ($pixeldataoffset < strlen($BMPpixelData)) {
423 $firstbyte = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
424 $secondbyte = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
425 if ($firstbyte == 0) {
427 // escaped/absolute mode - the first byte of the pair can be set to zero to
428 // indicate an escape character that denotes the end of a line, the end of
429 // a bitmap, or a delta, depending on the value of the second byte.
430 switch ($secondbyte) {
433 // no need for special processing, just ignore
438 $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case
442 // delta - The 2 bytes following the escape contain unsigned values
443 // indicating the horizontal and vertical offsets of the next pixel
444 // from the current position.
445 $colincrement = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
446 $rowincrement = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
447 $col = ($pixelcounter %
$thisfile_bmp_header_raw['width']) +
$colincrement;
448 $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement;
449 $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) +
$col;
453 // In absolute mode, the first byte is zero and the second byte is a
454 // value in the range 03H through FFH. The second byte represents the
455 // number of bytes that follow, each of which contains the color index
456 // of a single pixel. Each run must be aligned on a word boundary.
457 for ($i = 0; $i < $secondbyte; $i++
) {
458 $paletteindex = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
459 $col = $pixelcounter %
$thisfile_bmp_header_raw['width'];
460 $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
461 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
464 while (($pixeldataoffset %
2) != 0) {
465 // Each run must be aligned on a word boundary.
473 // encoded mode - the first byte specifies the number of consecutive pixels
474 // to be drawn using the color index contained in the second byte.
475 for ($i = 0; $i < $firstbyte; $i++
) {
476 $col = $pixelcounter %
$thisfile_bmp_header_raw['width'];
477 $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
478 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$secondbyte];
487 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
494 case 2: // BI_RLE4 - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_6x0u.asp
495 switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
498 while ($pixeldataoffset < strlen($BMPpixelData)) {
499 $firstbyte = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
500 $secondbyte = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
501 if ($firstbyte == 0) {
503 // escaped/absolute mode - the first byte of the pair can be set to zero to
504 // indicate an escape character that denotes the end of a line, the end of
505 // a bitmap, or a delta, depending on the value of the second byte.
506 switch ($secondbyte) {
509 // no need for special processing, just ignore
514 $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case
518 // delta - The 2 bytes following the escape contain unsigned values
519 // indicating the horizontal and vertical offsets of the next pixel
520 // from the current position.
521 $colincrement = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
522 $rowincrement = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
523 $col = ($pixelcounter %
$thisfile_bmp_header_raw['width']) +
$colincrement;
524 $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement;
525 $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) +
$col;
529 // In absolute mode, the first byte is zero. The second byte contains the number
530 // of color indexes that follow. Subsequent bytes contain color indexes in their
531 // high- and low-order 4 bits, one color index for each pixel. In absolute mode,
532 // each run must be aligned on a word boundary.
533 unset($paletteindexes);
534 $paletteindexes = array();
535 for ($i = 0; $i < ceil($secondbyte / 2); $i++
) {
536 $paletteindexbyte = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++
, 1));
537 $paletteindexes[] = ($paletteindexbyte & 0xF0) >> 4;
538 $paletteindexes[] = ($paletteindexbyte & 0x0F);
540 while (($pixeldataoffset %
2) != 0) {
541 // Each run must be aligned on a word boundary.
545 foreach ($paletteindexes as $paletteindex) {
546 $col = $pixelcounter %
$thisfile_bmp_header_raw['width'];
547 $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
548 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
556 // encoded mode - the first byte of the pair contains the number of pixels to be
557 // drawn using the color indexes in the second byte. The second byte contains two
558 // color indexes, one in its high-order 4 bits and one in its low-order 4 bits.
559 // The first of the pixels is drawn using the color specified by the high-order
560 // 4 bits, the second is drawn using the color in the low-order 4 bits, the third
561 // is drawn using the color in the high-order 4 bits, and so on, until all the
562 // pixels specified by the first byte have been drawn.
563 $paletteindexes[0] = ($secondbyte & 0xF0) >> 4;
564 $paletteindexes[1] = ($secondbyte & 0x0F);
565 for ($i = 0; $i < $firstbyte; $i++
) {
566 $col = $pixelcounter %
$thisfile_bmp_header_raw['width'];
567 $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
568 $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindexes[($i %
2)]];
577 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
583 case 3: // BI_BITFIELDS
584 switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
590 while ((($thisfile_bmp_header_raw['red_mask'] >> $redshift) & 0x01) == 0) {
593 while ((($thisfile_bmp_header_raw['green_mask'] >> $greenshift) & 0x01) == 0) {
596 while ((($thisfile_bmp_header_raw['blue_mask'] >> $blueshift) & 0x01) == 0) {
599 for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
600 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++
) {
601 $pixelvalue = getid3_lib
::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset, $thisfile_bmp_header_raw['bits_per_pixel'] / 8));
602 $pixeldataoffset +
= $thisfile_bmp_header_raw['bits_per_pixel'] / 8;
604 $red = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['red_mask']) >> $redshift) / ($thisfile_bmp_header_raw['red_mask'] >> $redshift)) * 255));
605 $green = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['green_mask']) >> $greenshift) / ($thisfile_bmp_header_raw['green_mask'] >> $greenshift)) * 255));
606 $blue = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['blue_mask']) >> $blueshift) / ($thisfile_bmp_header_raw['blue_mask'] >> $blueshift)) * 255));
607 $thisfile_bmp['data'][$row][$col] = (($red << 16) |
($green << 8) |
($blue));
609 while (($pixeldataoffset %
4) != 0) {
610 // lines are padded to nearest DWORD
617 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
623 default: // unhandled compression type
624 $this->error('Unknown/unhandled compression type value ('.$thisfile_bmp_header_raw['compression'].') - cannot decompress pixel data');
633 * @param array $BMPinfo
637 public function PlotBMP(&$BMPinfo) {
639 if (!isset($BMPinfo['bmp']['data']) ||
!is_array($BMPinfo['bmp']['data'])) {
640 echo 'ERROR: no pixel data<BR>';
643 set_time_limit(intval(round($BMPinfo['resolution_x'] * $BMPinfo['resolution_y'] / 10000)));
644 if ($im = imagecreatetruecolor($BMPinfo['resolution_x'], $BMPinfo['resolution_y'])) {
645 for ($row = 0; $row < $BMPinfo['resolution_y']; $row++
) {
646 for ($col = 0; $col < $BMPinfo['resolution_x']; $col++
) {
647 if (isset($BMPinfo['bmp']['data'][$row][$col])) {
648 $red = ($BMPinfo['bmp']['data'][$row][$col] & 0x00FF0000) >> 16;
649 $green = ($BMPinfo['bmp']['data'][$row][$col] & 0x0000FF00) >> 8;
650 $blue = ($BMPinfo['bmp']['data'][$row][$col] & 0x000000FF);
651 $pixelcolor = imagecolorallocate($im, $red, $green, $blue);
652 imagesetpixel($im, $col, $row, $pixelcolor);
654 //echo 'ERROR: no data for pixel '.$row.' x '.$col.'<BR>';
659 if (headers_sent()) {
660 echo 'plotted '.($BMPinfo['resolution_x'] * $BMPinfo['resolution_y']).' pixels in '.(time() - $starttime).' seconds<BR>';
664 header('Content-type: image/png');
674 * @param int $compressionid
678 public function BMPcompressionWindowsLookup($compressionid) {
679 static $BMPcompressionWindowsLookup = array(
687 return (isset($BMPcompressionWindowsLookup[$compressionid]) ?
$BMPcompressionWindowsLookup[$compressionid] : 'invalid');
691 * @param int $compressionid
695 public function BMPcompressionOS2Lookup($compressionid) {
696 static $BMPcompressionOS2Lookup = array(
703 return (isset($BMPcompressionOS2Lookup[$compressionid]) ?
$BMPcompressionOS2Lookup[$compressionid] : 'invalid');