123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685 |
- <?php
- class getid3_bmp
- {
- function getid3_bmp(&$fd, &$ThisFileInfo, $ExtractPalette=false, $ExtractData=false) {
-
- $ThisFileInfo['bmp']['header']['raw'] = array();
- $thisfile_bmp = &$ThisFileInfo['bmp'];
- $thisfile_bmp_header = &$thisfile_bmp['header'];
- $thisfile_bmp_header_raw = &$thisfile_bmp_header['raw'];
-
-
-
-
-
-
-
- fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
- $offset = 0;
- $BMPheader = fread($fd, 14 + 40);
- $thisfile_bmp_header_raw['identifier'] = substr($BMPheader, $offset, 2);
- $offset += 2;
- if ($thisfile_bmp_header_raw['identifier'] != 'BM') {
- $ThisFileInfo['error'][] = 'Expecting "BM" at offset '.$ThisFileInfo['avdataoffset'].', found "'.$thisfile_bmp_header_raw['identifier'].'"';
- unset($ThisFileInfo['fileformat']);
- unset($ThisFileInfo['bmp']);
- return false;
- }
- $thisfile_bmp_header_raw['filesize'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['reserved1'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $thisfile_bmp_header_raw['reserved2'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $thisfile_bmp_header_raw['data_offset'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['header_size'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
-
- $planes22 = getid3_lib::LittleEndian2Int(substr($BMPheader, 22, 2));
- $planes26 = getid3_lib::LittleEndian2Int(substr($BMPheader, 26, 2));
- if (($planes22 == 1) && ($planes26 != 1)) {
- $thisfile_bmp['type_os'] = 'OS/2';
- $thisfile_bmp['type_version'] = 1;
- } elseif (($planes26 == 1) && ($planes22 != 1)) {
- $thisfile_bmp['type_os'] = 'Windows';
- $thisfile_bmp['type_version'] = 1;
- } elseif ($thisfile_bmp_header_raw['header_size'] == 12) {
- $thisfile_bmp['type_os'] = 'OS/2';
- $thisfile_bmp['type_version'] = 1;
- } elseif ($thisfile_bmp_header_raw['header_size'] == 40) {
- $thisfile_bmp['type_os'] = 'Windows';
- $thisfile_bmp['type_version'] = 1;
- } elseif ($thisfile_bmp_header_raw['header_size'] == 84) {
- $thisfile_bmp['type_os'] = 'Windows';
- $thisfile_bmp['type_version'] = 4;
- } elseif ($thisfile_bmp_header_raw['header_size'] == 100) {
- $thisfile_bmp['type_os'] = 'Windows';
- $thisfile_bmp['type_version'] = 5;
- } else {
- $ThisFileInfo['error'][] = 'Unknown BMP subtype (or not a BMP file)';
- unset($ThisFileInfo['fileformat']);
- unset($ThisFileInfo['bmp']);
- return false;
- }
- $ThisFileInfo['fileformat'] = 'bmp';
- $ThisFileInfo['video']['dataformat'] = 'bmp';
- $ThisFileInfo['video']['lossless'] = true;
- $ThisFileInfo['video']['pixel_aspect_ratio'] = (float) 1;
- if ($thisfile_bmp['type_os'] == 'OS/2') {
-
-
-
-
-
-
-
- $thisfile_bmp_header_raw['width'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $thisfile_bmp_header_raw['height'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $thisfile_bmp_header_raw['planes'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $thisfile_bmp_header_raw['bits_per_pixel'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $ThisFileInfo['video']['resolution_x'] = $thisfile_bmp_header_raw['width'];
- $ThisFileInfo['video']['resolution_y'] = $thisfile_bmp_header_raw['height'];
- $ThisFileInfo['video']['codec'] = 'BI_RGB '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
- $ThisFileInfo['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel'];
- if ($thisfile_bmp['type_version'] >= 2) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $thisfile_bmp_header_raw['compression'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['bmp_data_size'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['resolution_h'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['resolution_v'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['colors_used'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['colors_important'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['resolution_units'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $thisfile_bmp_header_raw['reserved1'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $thisfile_bmp_header_raw['recording'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $thisfile_bmp_header_raw['rendering'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $thisfile_bmp_header_raw['size1'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['size2'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['color_encoding'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['identifier'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header['compression'] = $this->BMPcompressionOS2Lookup($thisfile_bmp_header_raw['compression']);
- $ThisFileInfo['video']['codec'] = $thisfile_bmp_header['compression'].' '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
- }
- } elseif ($thisfile_bmp['type_os'] == 'Windows') {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $thisfile_bmp_header_raw['width'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
- $offset += 4;
- $thisfile_bmp_header_raw['height'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
- $offset += 4;
- $thisfile_bmp_header_raw['planes'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $thisfile_bmp_header_raw['bits_per_pixel'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
- $offset += 2;
- $thisfile_bmp_header_raw['compression'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['bmp_data_size'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['resolution_h'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
- $offset += 4;
- $thisfile_bmp_header_raw['resolution_v'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
- $offset += 4;
- $thisfile_bmp_header_raw['colors_used'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['colors_important'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header['compression'] = $this->BMPcompressionWindowsLookup($thisfile_bmp_header_raw['compression']);
- $ThisFileInfo['video']['resolution_x'] = $thisfile_bmp_header_raw['width'];
- $ThisFileInfo['video']['resolution_y'] = $thisfile_bmp_header_raw['height'];
- $ThisFileInfo['video']['codec'] = $thisfile_bmp_header['compression'].' '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
- $ThisFileInfo['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel'];
- if (($thisfile_bmp['type_version'] >= 4) || ($thisfile_bmp_header_raw['compression'] == 3)) {
-
- $BMPheader .= fread($fd, 44);
-
-
-
-
-
-
-
-
-
-
-
- $thisfile_bmp_header_raw['red_mask'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['green_mask'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['blue_mask'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['alpha_mask'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['cs_type'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['ciexyz_red'] = substr($BMPheader, $offset, 4);
- $offset += 4;
- $thisfile_bmp_header_raw['ciexyz_green'] = substr($BMPheader, $offset, 4);
- $offset += 4;
- $thisfile_bmp_header_raw['ciexyz_blue'] = substr($BMPheader, $offset, 4);
- $offset += 4;
- $thisfile_bmp_header_raw['gamma_red'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['gamma_green'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['gamma_blue'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header['ciexyz_red'] = getid3_lib::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_red']));
- $thisfile_bmp_header['ciexyz_green'] = getid3_lib::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_green']));
- $thisfile_bmp_header['ciexyz_blue'] = getid3_lib::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_blue']));
- }
- if ($thisfile_bmp['type_version'] >= 5) {
- $BMPheader .= fread($fd, 16);
-
-
-
-
-
-
- $thisfile_bmp_header_raw['intent'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['profile_data_offset'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['profile_data_size'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- $thisfile_bmp_header_raw['reserved3'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
- $offset += 4;
- }
- } else {
- $ThisFileInfo['error'][] = 'Unknown BMP format in header.';
- return false;
- }
- if ($ExtractPalette || $ExtractData) {
- $PaletteEntries = 0;
- if ($thisfile_bmp_header_raw['bits_per_pixel'] < 16) {
- $PaletteEntries = pow(2, $thisfile_bmp_header_raw['bits_per_pixel']);
- } elseif (isset($thisfile_bmp_header_raw['colors_used']) && ($thisfile_bmp_header_raw['colors_used'] > 0) && ($thisfile_bmp_header_raw['colors_used'] <= 256)) {
- $PaletteEntries = $thisfile_bmp_header_raw['colors_used'];
- }
- if ($PaletteEntries > 0) {
- $BMPpalette = fread($fd, 4 * $PaletteEntries);
- $paletteoffset = 0;
- for ($i = 0; $i < $PaletteEntries; $i++) {
-
-
-
-
-
- $blue = getid3_lib::LittleEndian2Int(substr($BMPpalette, $paletteoffset++, 1));
- $green = getid3_lib::LittleEndian2Int(substr($BMPpalette, $paletteoffset++, 1));
- $red = getid3_lib::LittleEndian2Int(substr($BMPpalette, $paletteoffset++, 1));
- if (($thisfile_bmp['type_os'] == 'OS/2') && ($thisfile_bmp['type_version'] == 1)) {
-
- } else {
- $paletteoffset++;
- }
- $thisfile_bmp['palette'][$i] = (($red << 16) | ($green << 8) | $blue);
- }
- }
- }
- if ($ExtractData) {
- fseek($fd, $thisfile_bmp_header_raw['data_offset'], SEEK_SET);
- $RowByteLength = ceil(($thisfile_bmp_header_raw['width'] * ($thisfile_bmp_header_raw['bits_per_pixel'] / 8)) / 4) * 4;
- $BMPpixelData = fread($fd, $thisfile_bmp_header_raw['height'] * $RowByteLength);
- $pixeldataoffset = 0;
- switch (@$thisfile_bmp_header_raw['compression']) {
- case 0:
- switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
- case 1:
- for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
- for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col) {
- $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset++});
- for ($i = 7; $i >= 0; $i--) {
- $paletteindex = ($paletteindexbyte & (0x01 << $i)) >> $i;
- $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
- $col++;
- }
- }
- while (($pixeldataoffset % 4) != 0) {
-
- $pixeldataoffset++;
- }
- }
- break;
- case 4:
- for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
- for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col) {
- $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset++});
- for ($i = 1; $i >= 0; $i--) {
- $paletteindex = ($paletteindexbyte & (0x0F << (4 * $i))) >> (4 * $i);
- $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
- $col++;
- }
- }
- while (($pixeldataoffset % 4) != 0) {
-
- $pixeldataoffset++;
- }
- }
- break;
- case 8:
- for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
- for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) {
- $paletteindex = ord($BMPpixelData{$pixeldataoffset++});
- $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
- }
- while (($pixeldataoffset % 4) != 0) {
-
- $pixeldataoffset++;
- }
- }
- break;
- case 24:
- for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
- for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) {
- $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset+2}) << 16) | (ord($BMPpixelData{$pixeldataoffset+1}) << 8) | ord($BMPpixelData{$pixeldataoffset});
- $pixeldataoffset += 3;
- }
- while (($pixeldataoffset % 4) != 0) {
-
- $pixeldataoffset++;
- }
- }
- break;
- case 32:
- for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
- for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) {
- $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset+3}) << 24) | (ord($BMPpixelData{$pixeldataoffset+2}) << 16) | (ord($BMPpixelData{$pixeldataoffset+1}) << 8) | ord($BMPpixelData{$pixeldataoffset});
- $pixeldataoffset += 4;
- }
- while (($pixeldataoffset % 4) != 0) {
-
- $pixeldataoffset++;
- }
- }
- break;
- case 16:
-
- break;
- default:
- $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data';
- break;
- }
- break;
- case 1:
- switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
- case 8:
- $pixelcounter = 0;
- while ($pixeldataoffset < strlen($BMPpixelData)) {
- $firstbyte = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
- $secondbyte = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
- if ($firstbyte == 0) {
-
-
-
- switch ($secondbyte) {
- case 0:
-
-
- break;
- case 1:
-
- $pixeldataoffset = strlen($BMPpixelData);
- break;
- case 2:
-
-
-
- $colincrement = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
- $rowincrement = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
- $col = ($pixelcounter % $thisfile_bmp_header_raw['width']) + $colincrement;
- $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement;
- $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) + $col;
- break;
- default:
-
-
-
-
- for ($i = 0; $i < $secondbyte; $i++) {
- $paletteindex = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
- $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
- $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
- $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
- $pixelcounter++;
- }
- while (($pixeldataoffset % 2) != 0) {
-
- $pixeldataoffset++;
- }
- break;
- }
- } else {
-
-
- for ($i = 0; $i < $firstbyte; $i++) {
- $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
- $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
- $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$secondbyte];
- $pixelcounter++;
- }
- }
- }
- break;
- default:
- $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data';
- break;
- }
- break;
- case 2:
- switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
- case 4:
- $pixelcounter = 0;
- while ($pixeldataoffset < strlen($BMPpixelData)) {
- $firstbyte = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
- $secondbyte = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
- if ($firstbyte == 0) {
-
-
-
- switch ($secondbyte) {
- case 0:
-
-
- break;
- case 1:
-
- $pixeldataoffset = strlen($BMPpixelData);
- break;
- case 2:
-
-
-
- $colincrement = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
- $rowincrement = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
- $col = ($pixelcounter % $thisfile_bmp_header_raw['width']) + $colincrement;
- $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement;
- $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) + $col;
- break;
- default:
-
-
-
-
- unset($paletteindexes);
- for ($i = 0; $i < ceil($secondbyte / 2); $i++) {
- $paletteindexbyte = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
- $paletteindexes[] = ($paletteindexbyte & 0xF0) >> 4;
- $paletteindexes[] = ($paletteindexbyte & 0x0F);
- }
- while (($pixeldataoffset % 2) != 0) {
-
- $pixeldataoffset++;
- }
- foreach ($paletteindexes as $paletteindex) {
- $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
- $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
- $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
- $pixelcounter++;
- }
- break;
- }
- } else {
-
-
-
-
-
-
-
- $paletteindexes[0] = ($secondbyte & 0xF0) >> 4;
- $paletteindexes[1] = ($secondbyte & 0x0F);
- for ($i = 0; $i < $firstbyte; $i++) {
- $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
- $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
- $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindexes[($i % 2)]];
- $pixelcounter++;
- }
- }
- }
- break;
- default:
- $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data';
- break;
- }
- break;
- case 3:
- switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
- case 16:
- case 32:
- $redshift = 0;
- $greenshift = 0;
- $blueshift = 0;
- while ((($thisfile_bmp_header_raw['red_mask'] >> $redshift) & 0x01) == 0) {
- $redshift++;
- }
- while ((($thisfile_bmp_header_raw['green_mask'] >> $greenshift) & 0x01) == 0) {
- $greenshift++;
- }
- while ((($thisfile_bmp_header_raw['blue_mask'] >> $blueshift) & 0x01) == 0) {
- $blueshift++;
- }
- for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
- for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) {
- $pixelvalue = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset, $thisfile_bmp_header_raw['bits_per_pixel'] / 8));
- $pixeldataoffset += $thisfile_bmp_header_raw['bits_per_pixel'] / 8;
- $red = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['red_mask']) >> $redshift) / ($thisfile_bmp_header_raw['red_mask'] >> $redshift)) * 255));
- $green = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['green_mask']) >> $greenshift) / ($thisfile_bmp_header_raw['green_mask'] >> $greenshift)) * 255));
- $blue = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['blue_mask']) >> $blueshift) / ($thisfile_bmp_header_raw['blue_mask'] >> $blueshift)) * 255));
- $thisfile_bmp['data'][$row][$col] = (($red << 16) | ($green << 8) | ($blue));
- }
- while (($pixeldataoffset % 4) != 0) {
-
- $pixeldataoffset++;
- }
- }
- break;
- default:
- $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data';
- break;
- }
- break;
- default:
- $ThisFileInfo['error'][] = 'Unknown/unhandled compression type value ('.$thisfile_bmp_header_raw['compression'].') - cannot decompress pixel data';
- break;
- }
- }
- return true;
- }
- function PlotBMP(&$BMPinfo) {
- $starttime = time();
- if (!isset($BMPinfo['bmp']['data']) || !is_array($BMPinfo['bmp']['data'])) {
- echo 'ERROR: no pixel data<BR>';
- return false;
- }
- set_time_limit(intval(round($BMPinfo['resolution_x'] * $BMPinfo['resolution_y'] / 10000)));
- if ($im = ImageCreateTrueColor($BMPinfo['resolution_x'], $BMPinfo['resolution_y'])) {
- for ($row = 0; $row < $BMPinfo['resolution_y']; $row++) {
- for ($col = 0; $col < $BMPinfo['resolution_x']; $col++) {
- if (isset($BMPinfo['bmp']['data'][$row][$col])) {
- $red = ($BMPinfo['bmp']['data'][$row][$col] & 0x00FF0000) >> 16;
- $green = ($BMPinfo['bmp']['data'][$row][$col] & 0x0000FF00) >> 8;
- $blue = ($BMPinfo['bmp']['data'][$row][$col] & 0x000000FF);
- $pixelcolor = ImageColorAllocate($im, $red, $green, $blue);
- ImageSetPixel($im, $col, $row, $pixelcolor);
- } else {
-
-
- }
- }
- }
- if (headers_sent()) {
- echo 'plotted '.($BMPinfo['resolution_x'] * $BMPinfo['resolution_y']).' pixels in '.(time() - $starttime).' seconds<BR>';
- ImageDestroy($im);
- exit;
- } else {
- header('Content-type: image/png');
- ImagePNG($im);
- ImageDestroy($im);
- return true;
- }
- }
- return false;
- }
- function BMPcompressionWindowsLookup($compressionid) {
- static $BMPcompressionWindowsLookup = array(
- 0 => 'BI_RGB',
- 1 => 'BI_RLE8',
- 2 => 'BI_RLE4',
- 3 => 'BI_BITFIELDS',
- 4 => 'BI_JPEG',
- 5 => 'BI_PNG'
- );
- return (isset($BMPcompressionWindowsLookup[$compressionid]) ? $BMPcompressionWindowsLookup[$compressionid] : 'invalid');
- }
- function BMPcompressionOS2Lookup($compressionid) {
- static $BMPcompressionOS2Lookup = array(
- 0 => 'BI_RGB',
- 1 => 'BI_RLE8',
- 2 => 'BI_RLE4',
- 3 => 'Huffman 1D',
- 4 => 'BI_RLE24',
- );
- return (isset($BMPcompressionOS2Lookup[$compressionid]) ? $BMPcompressionOS2Lookup[$compressionid] : 'invalid');
- }
- }
- ?>
|