[SCRIPTS] ~fix nouveau chemin d installation de SPIP
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.graphic.bmp.php
1 <?php
2
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 /////////////////////////////////////////////////////////////////
10 // //
11 // module.graphic.bmp.php //
12 // module for analyzing BMP Image files //
13 // dependencies: NONE //
14 // ///
15 /////////////////////////////////////////////////////////////////
16
17
18 class getid3_bmp extends getid3_handler
19 {
20 public $ExtractPalette = false;
21 public $ExtractData = false;
22
23 /**
24 * @return bool
25 */
26 public function Analyze() {
27 $info = &$this->getid3->info;
28
29 // shortcuts
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'];
34
35 // BITMAPFILEHEADER [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp
36 // all versions
37 // WORD bfType;
38 // DWORD bfSize;
39 // WORD bfReserved1;
40 // WORD bfReserved2;
41 // DWORD bfOffBits;
42
43 $this->fseek($info['avdataoffset']);
44 $offset = 0;
45 $BMPheader = $this->fread(14 + 40);
46
47 $thisfile_bmp_header_raw['identifier'] = substr($BMPheader, $offset, 2);
48 $offset += 2;
49
50 $magic = 'BM';
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']);
54 unset($info['bmp']);
55 return false;
56 }
57
58 $thisfile_bmp_header_raw['filesize'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
59 $offset += 4;
60 $thisfile_bmp_header_raw['reserved1'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
61 $offset += 2;
62 $thisfile_bmp_header_raw['reserved2'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
63 $offset += 2;
64 $thisfile_bmp_header_raw['data_offset'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
65 $offset += 4;
66 $thisfile_bmp_header_raw['header_size'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
67 $offset += 4;
68
69
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;
91 } else {
92 $this->error('Unknown BMP subtype (or not a BMP file)');
93 unset($info['fileformat']);
94 unset($info['bmp']);
95 return false;
96 }
97
98 $info['fileformat'] = 'bmp';
99 $info['video']['dataformat'] = 'bmp';
100 $info['video']['lossless'] = true;
101 $info['video']['pixel_aspect_ratio'] = (float) 1;
102
103 if ($thisfile_bmp['type_os'] == 'OS/2') {
104
105 // OS/2-format BMP
106 // http://netghost.narod.ru/gff/graphics/summary/os2bmp.htm
107
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 */
113
114 $thisfile_bmp_header_raw['width'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
115 $offset += 2;
116 $thisfile_bmp_header_raw['height'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
117 $offset += 2;
118 $thisfile_bmp_header_raw['planes'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
119 $offset += 2;
120 $thisfile_bmp_header_raw['bits_per_pixel'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
121 $offset += 2;
122
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'];
127
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 */
143
144 $thisfile_bmp_header_raw['compression'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
145 $offset += 4;
146 $thisfile_bmp_header_raw['bmp_data_size'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
147 $offset += 4;
148 $thisfile_bmp_header_raw['resolution_h'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
149 $offset += 4;
150 $thisfile_bmp_header_raw['resolution_v'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
151 $offset += 4;
152 $thisfile_bmp_header_raw['colors_used'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
153 $offset += 4;
154 $thisfile_bmp_header_raw['colors_important'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
155 $offset += 4;
156 $thisfile_bmp_header_raw['resolution_units'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
157 $offset += 2;
158 $thisfile_bmp_header_raw['reserved1'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
159 $offset += 2;
160 $thisfile_bmp_header_raw['recording'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
161 $offset += 2;
162 $thisfile_bmp_header_raw['rendering'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
163 $offset += 2;
164 $thisfile_bmp_header_raw['size1'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
165 $offset += 4;
166 $thisfile_bmp_header_raw['size2'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
167 $offset += 4;
168 $thisfile_bmp_header_raw['color_encoding'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
169 $offset += 4;
170 $thisfile_bmp_header_raw['identifier'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
171 $offset += 4;
172
173 $thisfile_bmp_header['compression'] = $this->BMPcompressionOS2Lookup($thisfile_bmp_header_raw['compression']);
174
175 $info['video']['codec'] = $thisfile_bmp_header['compression'].' '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
176 }
177
178 } elseif ($thisfile_bmp['type_os'] == 'Windows') {
179
180 // Windows-format BMP
181
182 // BITMAPINFOHEADER - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp
183 // all versions
184 // DWORD biSize;
185 // LONG biWidth;
186 // LONG biHeight;
187 // WORD biPlanes;
188 // WORD biBitCount;
189 // DWORD biCompression;
190 // DWORD biSizeImage;
191 // LONG biXPelsPerMeter;
192 // LONG biYPelsPerMeter;
193 // DWORD biClrUsed;
194 // DWORD biClrImportant;
195
196 // possibly integrate this section and module.audio-video.riff.php::ParseBITMAPINFOHEADER() ?
197
198 $thisfile_bmp_header_raw['width'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
199 $offset += 4;
200 $thisfile_bmp_header_raw['height'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
201 $offset += 4;
202 $thisfile_bmp_header_raw['planes'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
203 $offset += 2;
204 $thisfile_bmp_header_raw['bits_per_pixel'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
205 $offset += 2;
206 $thisfile_bmp_header_raw['compression'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
207 $offset += 4;
208 $thisfile_bmp_header_raw['bmp_data_size'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
209 $offset += 4;
210 $thisfile_bmp_header_raw['resolution_h'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
211 $offset += 4;
212 $thisfile_bmp_header_raw['resolution_v'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
213 $offset += 4;
214 $thisfile_bmp_header_raw['colors_used'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
215 $offset += 4;
216 $thisfile_bmp_header_raw['colors_important'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
217 $offset += 4;
218
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'];
224
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);
228
229 // BITMAPV4HEADER - [44 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_2k1e.asp
230 // Win95+, WinNT4.0+
231 // DWORD bV4RedMask;
232 // DWORD bV4GreenMask;
233 // DWORD bV4BlueMask;
234 // DWORD bV4AlphaMask;
235 // DWORD bV4CSType;
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));
241 $offset += 4;
242 $thisfile_bmp_header_raw['green_mask'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
243 $offset += 4;
244 $thisfile_bmp_header_raw['blue_mask'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
245 $offset += 4;
246 $thisfile_bmp_header_raw['alpha_mask'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
247 $offset += 4;
248 $thisfile_bmp_header_raw['cs_type'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
249 $offset += 4;
250 $thisfile_bmp_header_raw['ciexyz_red'] = substr($BMPheader, $offset, 4);
251 $offset += 4;
252 $thisfile_bmp_header_raw['ciexyz_green'] = substr($BMPheader, $offset, 4);
253 $offset += 4;
254 $thisfile_bmp_header_raw['ciexyz_blue'] = substr($BMPheader, $offset, 4);
255 $offset += 4;
256 $thisfile_bmp_header_raw['gamma_red'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
257 $offset += 4;
258 $thisfile_bmp_header_raw['gamma_green'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
259 $offset += 4;
260 $thisfile_bmp_header_raw['gamma_blue'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
261 $offset += 4;
262
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']));
266 }
267
268 if ($thisfile_bmp['type_version'] >= 5) {
269 $BMPheader .= $this->fread(16);
270
271 // BITMAPV5HEADER - [16 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_7c36.asp
272 // Win98+, Win2000+
273 // DWORD bV5Intent;
274 // DWORD bV5ProfileData;
275 // DWORD bV5ProfileSize;
276 // DWORD bV5Reserved;
277 $thisfile_bmp_header_raw['intent'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
278 $offset += 4;
279 $thisfile_bmp_header_raw['profile_data_offset'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
280 $offset += 4;
281 $thisfile_bmp_header_raw['profile_data_size'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
282 $offset += 4;
283 $thisfile_bmp_header_raw['reserved3'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
284 $offset += 4;
285 }
286
287 } else {
288
289 $this->error('Unknown BMP format in header.');
290 return false;
291
292 }
293
294
295 if ($this->ExtractPalette || $this->ExtractData) {
296 $PaletteEntries = 0;
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'];
301 }
302 if ($PaletteEntries > 0) {
303 $BMPpalette = $this->fread(4 * $PaletteEntries);
304 $paletteoffset = 0;
305 for ($i = 0; $i < $PaletteEntries; $i++) {
306 // RGBQUAD - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_5f8y.asp
307 // BYTE rgbBlue;
308 // BYTE rgbGreen;
309 // BYTE rgbRed;
310 // BYTE rgbReserved;
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)) {
315 // no padding byte
316 } else {
317 $paletteoffset++; // padding byte
318 }
319 $thisfile_bmp['palette'][$i] = (($red << 16) | ($green << 8) | $blue);
320 }
321 }
322 }
323
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']) {
331
332 case 0: // BI_RGB
333 switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
334 case 1:
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];
341 $col++;
342 }
343 }
344 while (($pixeldataoffset % 4) != 0) {
345 // lines are padded to nearest DWORD
346 $pixeldataoffset++;
347 }
348 }
349 break;
350
351 case 4:
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];
358 $col++;
359 }
360 }
361 while (($pixeldataoffset % 4) != 0) {
362 // lines are padded to nearest DWORD
363 $pixeldataoffset++;
364 }
365 }
366 break;
367
368 case 8:
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];
373 }
374 while (($pixeldataoffset % 4) != 0) {
375 // lines are padded to nearest DWORD
376 $pixeldataoffset++;
377 }
378 }
379 break;
380
381 case 24:
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;
386 }
387 while (($pixeldataoffset % 4) != 0) {
388 // lines are padded to nearest DWORD
389 $pixeldataoffset++;
390 }
391 }
392 break;
393
394 case 32:
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;
399 }
400 while (($pixeldataoffset % 4) != 0) {
401 // lines are padded to nearest DWORD
402 $pixeldataoffset++;
403 }
404 }
405 break;
406
407 case 16:
408 // ?
409 break;
410
411 default:
412 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
413 break;
414 }
415 break;
416
417
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']) {
420 case 8:
421 $pixelcounter = 0;
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) {
426
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) {
431 case 0:
432 // end of line
433 // no need for special processing, just ignore
434 break;
435
436 case 1:
437 // end of bitmap
438 $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case
439 break;
440
441 case 2:
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;
450 break;
451
452 default:
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];
462 $pixelcounter++;
463 }
464 while (($pixeldataoffset % 2) != 0) {
465 // Each run must be aligned on a word boundary.
466 $pixeldataoffset++;
467 }
468 break;
469 }
470
471 } else {
472
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];
479 $pixelcounter++;
480 }
481
482 }
483 }
484 break;
485
486 default:
487 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
488 break;
489 }
490 break;
491
492
493
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']) {
496 case 4:
497 $pixelcounter = 0;
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) {
502
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) {
507 case 0:
508 // end of line
509 // no need for special processing, just ignore
510 break;
511
512 case 1:
513 // end of bitmap
514 $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case
515 break;
516
517 case 2:
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;
526 break;
527
528 default:
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);
539 }
540 while (($pixeldataoffset % 2) != 0) {
541 // Each run must be aligned on a word boundary.
542 $pixeldataoffset++;
543 }
544
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];
549 $pixelcounter++;
550 }
551 break;
552 }
553
554 } else {
555
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)]];
569 $pixelcounter++;
570 }
571
572 }
573 }
574 break;
575
576 default:
577 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
578 break;
579 }
580 break;
581
582
583 case 3: // BI_BITFIELDS
584 switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
585 case 16:
586 case 32:
587 $redshift = 0;
588 $greenshift = 0;
589 $blueshift = 0;
590 while ((($thisfile_bmp_header_raw['red_mask'] >> $redshift) & 0x01) == 0) {
591 $redshift++;
592 }
593 while ((($thisfile_bmp_header_raw['green_mask'] >> $greenshift) & 0x01) == 0) {
594 $greenshift++;
595 }
596 while ((($thisfile_bmp_header_raw['blue_mask'] >> $blueshift) & 0x01) == 0) {
597 $blueshift++;
598 }
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;
603
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));
608 }
609 while (($pixeldataoffset % 4) != 0) {
610 // lines are padded to nearest DWORD
611 $pixeldataoffset++;
612 }
613 }
614 break;
615
616 default:
617 $this->error('Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data');
618 break;
619 }
620 break;
621
622
623 default: // unhandled compression type
624 $this->error('Unknown/unhandled compression type value ('.$thisfile_bmp_header_raw['compression'].') - cannot decompress pixel data');
625 break;
626 }
627 }
628
629 return true;
630 }
631
632 /**
633 * @param array $BMPinfo
634 *
635 * @return bool
636 */
637 public function PlotBMP(&$BMPinfo) {
638 $starttime = time();
639 if (!isset($BMPinfo['bmp']['data']) || !is_array($BMPinfo['bmp']['data'])) {
640 echo 'ERROR: no pixel data<BR>';
641 return false;
642 }
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);
653 } else {
654 //echo 'ERROR: no data for pixel '.$row.' x '.$col.'<BR>';
655 //return false;
656 }
657 }
658 }
659 if (headers_sent()) {
660 echo 'plotted '.($BMPinfo['resolution_x'] * $BMPinfo['resolution_y']).' pixels in '.(time() - $starttime).' seconds<BR>';
661 imagedestroy($im);
662 exit;
663 } else {
664 header('Content-type: image/png');
665 imagepng($im);
666 imagedestroy($im);
667 return true;
668 }
669 }
670 return false;
671 }
672
673 /**
674 * @param int $compressionid
675 *
676 * @return string
677 */
678 public function BMPcompressionWindowsLookup($compressionid) {
679 static $BMPcompressionWindowsLookup = array(
680 0 => 'BI_RGB',
681 1 => 'BI_RLE8',
682 2 => 'BI_RLE4',
683 3 => 'BI_BITFIELDS',
684 4 => 'BI_JPEG',
685 5 => 'BI_PNG'
686 );
687 return (isset($BMPcompressionWindowsLookup[$compressionid]) ? $BMPcompressionWindowsLookup[$compressionid] : 'invalid');
688 }
689
690 /**
691 * @param int $compressionid
692 *
693 * @return string
694 */
695 public function BMPcompressionOS2Lookup($compressionid) {
696 static $BMPcompressionOS2Lookup = array(
697 0 => 'BI_RGB',
698 1 => 'BI_RLE8',
699 2 => 'BI_RLE4',
700 3 => 'Huffman 1D',
701 4 => 'BI_RLE24',
702 );
703 return (isset($BMPcompressionOS2Lookup[$compressionid]) ? $BMPcompressionOS2Lookup[$compressionid] : 'invalid');
704 }
705
706 }