File indexing completed on 2024-05-12 17:26:00

0001 <?php
0002 /////////////////////////////////////////////////////////////////
0003 /// getID3() by James Heinrich <info@getid3.org>               //
0004 //  available at http://getid3.sourceforge.net                 //
0005 //            or http://www.getid3.org                         //
0006 //          also https://github.com/JamesHeinrich/getID3       //
0007 /////////////////////////////////////////////////////////////////
0008 // See readme.txt for more details                             //
0009 /////////////////////////////////////////////////////////////////
0010 //                                                             //
0011 // module.graphic.bmp.php                                      //
0012 // module for analyzing BMP Image files                        //
0013 // dependencies: NONE                                          //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 
0018 class getid3_bmp extends getid3_handler
0019 {
0020   public $ExtractPalette = false;
0021   public $ExtractData    = false;
0022 
0023   public function Analyze() {
0024     $info = &$this->getid3->info;
0025 
0026     // shortcuts
0027     $info['bmp']['header']['raw'] = array();
0028     $thisfile_bmp                         = &$info['bmp'];
0029     $thisfile_bmp_header                  = &$thisfile_bmp['header'];
0030     $thisfile_bmp_header_raw              = &$thisfile_bmp_header['raw'];
0031 
0032     // BITMAPFILEHEADER [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp
0033     // all versions
0034     // WORD    bfType;
0035     // DWORD   bfSize;
0036     // WORD    bfReserved1;
0037     // WORD    bfReserved2;
0038     // DWORD   bfOffBits;
0039 
0040     $this->fseek($info['avdataoffset']);
0041     $offset = 0;
0042     $BMPheader = $this->fread(14 + 40);
0043 
0044     $thisfile_bmp_header_raw['identifier']  = substr($BMPheader, $offset, 2);
0045     $offset += 2;
0046 
0047     $magic = 'BM';
0048     if ($thisfile_bmp_header_raw['identifier'] != $magic) {
0049       $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($thisfile_bmp_header_raw['identifier']).'"';
0050       unset($info['fileformat']);
0051       unset($info['bmp']);
0052       return false;
0053     }
0054 
0055     $thisfile_bmp_header_raw['filesize']    = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0056     $offset += 4;
0057     $thisfile_bmp_header_raw['reserved1']   = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0058     $offset += 2;
0059     $thisfile_bmp_header_raw['reserved2']   = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0060     $offset += 2;
0061     $thisfile_bmp_header_raw['data_offset'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0062     $offset += 4;
0063     $thisfile_bmp_header_raw['header_size'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0064     $offset += 4;
0065 
0066 
0067     // check if the hardcoded-to-1 "planes" is at offset 22 or 26
0068     $planes22 = getid3_lib::LittleEndian2Int(substr($BMPheader, 22, 2));
0069     $planes26 = getid3_lib::LittleEndian2Int(substr($BMPheader, 26, 2));
0070     if (($planes22 == 1) && ($planes26 != 1)) {
0071       $thisfile_bmp['type_os']      = 'OS/2';
0072       $thisfile_bmp['type_version'] = 1;
0073     } elseif (($planes26 == 1) && ($planes22 != 1)) {
0074       $thisfile_bmp['type_os']      = 'Windows';
0075       $thisfile_bmp['type_version'] = 1;
0076     } elseif ($thisfile_bmp_header_raw['header_size'] == 12) {
0077       $thisfile_bmp['type_os']      = 'OS/2';
0078       $thisfile_bmp['type_version'] = 1;
0079     } elseif ($thisfile_bmp_header_raw['header_size'] == 40) {
0080       $thisfile_bmp['type_os']      = 'Windows';
0081       $thisfile_bmp['type_version'] = 1;
0082     } elseif ($thisfile_bmp_header_raw['header_size'] == 84) {
0083       $thisfile_bmp['type_os']      = 'Windows';
0084       $thisfile_bmp['type_version'] = 4;
0085     } elseif ($thisfile_bmp_header_raw['header_size'] == 100) {
0086       $thisfile_bmp['type_os']      = 'Windows';
0087       $thisfile_bmp['type_version'] = 5;
0088     } else {
0089       $info['error'][] = 'Unknown BMP subtype (or not a BMP file)';
0090       unset($info['fileformat']);
0091       unset($info['bmp']);
0092       return false;
0093     }
0094 
0095     $info['fileformat']                  = 'bmp';
0096     $info['video']['dataformat']         = 'bmp';
0097     $info['video']['lossless']           = true;
0098     $info['video']['pixel_aspect_ratio'] = (float) 1;
0099 
0100     if ($thisfile_bmp['type_os'] == 'OS/2') {
0101 
0102       // OS/2-format BMP
0103       // http://netghost.narod.ru/gff/graphics/summary/os2bmp.htm
0104 
0105       // DWORD  Size;             /* Size of this structure in bytes */
0106       // DWORD  Width;            /* Bitmap width in pixels */
0107       // DWORD  Height;           /* Bitmap height in pixel */
0108       // WORD   NumPlanes;        /* Number of bit planes (color depth) */
0109       // WORD   BitsPerPixel;     /* Number of bits per pixel per plane */
0110 
0111       $thisfile_bmp_header_raw['width']          = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0112       $offset += 2;
0113       $thisfile_bmp_header_raw['height']         = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0114       $offset += 2;
0115       $thisfile_bmp_header_raw['planes']         = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0116       $offset += 2;
0117       $thisfile_bmp_header_raw['bits_per_pixel'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0118       $offset += 2;
0119 
0120       $info['video']['resolution_x']    = $thisfile_bmp_header_raw['width'];
0121       $info['video']['resolution_y']    = $thisfile_bmp_header_raw['height'];
0122       $info['video']['codec']           = 'BI_RGB '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
0123       $info['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel'];
0124 
0125       if ($thisfile_bmp['type_version'] >= 2) {
0126         // DWORD  Compression;      /* Bitmap compression scheme */
0127         // DWORD  ImageDataSize;    /* Size of bitmap data in bytes */
0128         // DWORD  XResolution;      /* X resolution of display device */
0129         // DWORD  YResolution;      /* Y resolution of display device */
0130         // DWORD  ColorsUsed;       /* Number of color table indices used */
0131         // DWORD  ColorsImportant;  /* Number of important color indices */
0132         // WORD   Units;            /* Type of units used to measure resolution */
0133         // WORD   Reserved;         /* Pad structure to 4-byte boundary */
0134         // WORD   Recording;        /* Recording algorithm */
0135         // WORD   Rendering;        /* Halftoning algorithm used */
0136         // DWORD  Size1;            /* Reserved for halftoning algorithm use */
0137         // DWORD  Size2;            /* Reserved for halftoning algorithm use */
0138         // DWORD  ColorEncoding;    /* Color model used in bitmap */
0139         // DWORD  Identifier;       /* Reserved for application use */
0140 
0141         $thisfile_bmp_header_raw['compression']      = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0142         $offset += 4;
0143         $thisfile_bmp_header_raw['bmp_data_size']    = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0144         $offset += 4;
0145         $thisfile_bmp_header_raw['resolution_h']     = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0146         $offset += 4;
0147         $thisfile_bmp_header_raw['resolution_v']     = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0148         $offset += 4;
0149         $thisfile_bmp_header_raw['colors_used']      = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0150         $offset += 4;
0151         $thisfile_bmp_header_raw['colors_important'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0152         $offset += 4;
0153         $thisfile_bmp_header_raw['resolution_units'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0154         $offset += 2;
0155         $thisfile_bmp_header_raw['reserved1']        = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0156         $offset += 2;
0157         $thisfile_bmp_header_raw['recording']        = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0158         $offset += 2;
0159         $thisfile_bmp_header_raw['rendering']        = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0160         $offset += 2;
0161         $thisfile_bmp_header_raw['size1']            = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0162         $offset += 4;
0163         $thisfile_bmp_header_raw['size2']            = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0164         $offset += 4;
0165         $thisfile_bmp_header_raw['color_encoding']   = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0166         $offset += 4;
0167         $thisfile_bmp_header_raw['identifier']       = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0168         $offset += 4;
0169 
0170         $thisfile_bmp_header['compression'] = $this->BMPcompressionOS2Lookup($thisfile_bmp_header_raw['compression']);
0171 
0172         $info['video']['codec'] = $thisfile_bmp_header['compression'].' '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
0173       }
0174 
0175     } elseif ($thisfile_bmp['type_os'] == 'Windows') {
0176 
0177       // Windows-format BMP
0178 
0179       // BITMAPINFOHEADER - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp
0180       // all versions
0181       // DWORD  biSize;
0182       // LONG   biWidth;
0183       // LONG   biHeight;
0184       // WORD   biPlanes;
0185       // WORD   biBitCount;
0186       // DWORD  biCompression;
0187       // DWORD  biSizeImage;
0188       // LONG   biXPelsPerMeter;
0189       // LONG   biYPelsPerMeter;
0190       // DWORD  biClrUsed;
0191       // DWORD  biClrImportant;
0192 
0193       // possibly integrate this section and module.audio-video.riff.php::ParseBITMAPINFOHEADER() ?
0194 
0195       $thisfile_bmp_header_raw['width']            = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
0196       $offset += 4;
0197       $thisfile_bmp_header_raw['height']           = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
0198       $offset += 4;
0199       $thisfile_bmp_header_raw['planes']           = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0200       $offset += 2;
0201       $thisfile_bmp_header_raw['bits_per_pixel']   = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 2));
0202       $offset += 2;
0203       $thisfile_bmp_header_raw['compression']      = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0204       $offset += 4;
0205       $thisfile_bmp_header_raw['bmp_data_size']    = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0206       $offset += 4;
0207       $thisfile_bmp_header_raw['resolution_h']     = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
0208       $offset += 4;
0209       $thisfile_bmp_header_raw['resolution_v']     = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4), true);
0210       $offset += 4;
0211       $thisfile_bmp_header_raw['colors_used']      = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0212       $offset += 4;
0213       $thisfile_bmp_header_raw['colors_important'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0214       $offset += 4;
0215 
0216       $thisfile_bmp_header['compression']  = $this->BMPcompressionWindowsLookup($thisfile_bmp_header_raw['compression']);
0217       $info['video']['resolution_x']    = $thisfile_bmp_header_raw['width'];
0218       $info['video']['resolution_y']    = $thisfile_bmp_header_raw['height'];
0219       $info['video']['codec']           = $thisfile_bmp_header['compression'].' '.$thisfile_bmp_header_raw['bits_per_pixel'].'-bit';
0220       $info['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel'];
0221 
0222       if (($thisfile_bmp['type_version'] >= 4) || ($thisfile_bmp_header_raw['compression'] == 3)) {
0223         // should only be v4+, but BMPs with type_version==1 and BI_BITFIELDS compression have been seen
0224         $BMPheader .= $this->fread(44);
0225 
0226         // BITMAPV4HEADER - [44 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_2k1e.asp
0227         // Win95+, WinNT4.0+
0228         // DWORD        bV4RedMask;
0229         // DWORD        bV4GreenMask;
0230         // DWORD        bV4BlueMask;
0231         // DWORD        bV4AlphaMask;
0232         // DWORD        bV4CSType;
0233         // CIEXYZTRIPLE bV4Endpoints;
0234         // DWORD        bV4GammaRed;
0235         // DWORD        bV4GammaGreen;
0236         // DWORD        bV4GammaBlue;
0237         $thisfile_bmp_header_raw['red_mask']     = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0238         $offset += 4;
0239         $thisfile_bmp_header_raw['green_mask']   = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0240         $offset += 4;
0241         $thisfile_bmp_header_raw['blue_mask']    = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0242         $offset += 4;
0243         $thisfile_bmp_header_raw['alpha_mask']   = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0244         $offset += 4;
0245         $thisfile_bmp_header_raw['cs_type']      = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0246         $offset += 4;
0247         $thisfile_bmp_header_raw['ciexyz_red']   =                  substr($BMPheader, $offset, 4);
0248         $offset += 4;
0249         $thisfile_bmp_header_raw['ciexyz_green'] =                  substr($BMPheader, $offset, 4);
0250         $offset += 4;
0251         $thisfile_bmp_header_raw['ciexyz_blue']  =                  substr($BMPheader, $offset, 4);
0252         $offset += 4;
0253         $thisfile_bmp_header_raw['gamma_red']    = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0254         $offset += 4;
0255         $thisfile_bmp_header_raw['gamma_green']  = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0256         $offset += 4;
0257         $thisfile_bmp_header_raw['gamma_blue']   = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0258         $offset += 4;
0259 
0260         $thisfile_bmp_header['ciexyz_red']   = getid3_lib::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_red']));
0261         $thisfile_bmp_header['ciexyz_green'] = getid3_lib::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_green']));
0262         $thisfile_bmp_header['ciexyz_blue']  = getid3_lib::FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_blue']));
0263       }
0264 
0265       if ($thisfile_bmp['type_version'] >= 5) {
0266         $BMPheader .= $this->fread(16);
0267 
0268         // BITMAPV5HEADER - [16 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_7c36.asp
0269         // Win98+, Win2000+
0270         // DWORD        bV5Intent;
0271         // DWORD        bV5ProfileData;
0272         // DWORD        bV5ProfileSize;
0273         // DWORD        bV5Reserved;
0274         $thisfile_bmp_header_raw['intent']              = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0275         $offset += 4;
0276         $thisfile_bmp_header_raw['profile_data_offset'] = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0277         $offset += 4;
0278         $thisfile_bmp_header_raw['profile_data_size']   = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0279         $offset += 4;
0280         $thisfile_bmp_header_raw['reserved3']           = getid3_lib::LittleEndian2Int(substr($BMPheader, $offset, 4));
0281         $offset += 4;
0282       }
0283 
0284     } else {
0285 
0286       $info['error'][] = 'Unknown BMP format in header.';
0287       return false;
0288 
0289     }
0290 
0291 
0292     if ($this->ExtractPalette || $this->ExtractData) {
0293       $PaletteEntries = 0;
0294       if ($thisfile_bmp_header_raw['bits_per_pixel'] < 16) {
0295         $PaletteEntries = pow(2, $thisfile_bmp_header_raw['bits_per_pixel']);
0296       } elseif (isset($thisfile_bmp_header_raw['colors_used']) && ($thisfile_bmp_header_raw['colors_used'] > 0) && ($thisfile_bmp_header_raw['colors_used'] <= 256)) {
0297         $PaletteEntries = $thisfile_bmp_header_raw['colors_used'];
0298       }
0299       if ($PaletteEntries > 0) {
0300         $BMPpalette = $this->fread(4 * $PaletteEntries);
0301         $paletteoffset = 0;
0302         for ($i = 0; $i < $PaletteEntries; $i++) {
0303           // RGBQUAD          - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_5f8y.asp
0304           // BYTE    rgbBlue;
0305           // BYTE    rgbGreen;
0306           // BYTE    rgbRed;
0307           // BYTE    rgbReserved;
0308           $blue  = getid3_lib::LittleEndian2Int(substr($BMPpalette, $paletteoffset++, 1));
0309           $green = getid3_lib::LittleEndian2Int(substr($BMPpalette, $paletteoffset++, 1));
0310           $red   = getid3_lib::LittleEndian2Int(substr($BMPpalette, $paletteoffset++, 1));
0311           if (($thisfile_bmp['type_os'] == 'OS/2') && ($thisfile_bmp['type_version'] == 1)) {
0312             // no padding byte
0313           } else {
0314             $paletteoffset++; // padding byte
0315           }
0316           $thisfile_bmp['palette'][$i] = (($red << 16) | ($green << 8) | $blue);
0317         }
0318       }
0319     }
0320 
0321     if ($this->ExtractData) {
0322       $this->fseek($thisfile_bmp_header_raw['data_offset']);
0323       $RowByteLength = ceil(($thisfile_bmp_header_raw['width'] * ($thisfile_bmp_header_raw['bits_per_pixel'] / 8)) / 4) * 4; // round up to nearest DWORD boundry
0324       $BMPpixelData = $this->fread($thisfile_bmp_header_raw['height'] * $RowByteLength);
0325       $pixeldataoffset = 0;
0326       $thisfile_bmp_header_raw['compression'] = (isset($thisfile_bmp_header_raw['compression']) ? $thisfile_bmp_header_raw['compression'] : '');
0327       switch ($thisfile_bmp_header_raw['compression']) {
0328 
0329         case 0: // BI_RGB
0330           switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
0331             case 1:
0332               for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
0333                 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col) {
0334                   $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset++});
0335                   for ($i = 7; $i >= 0; $i--) {
0336                     $paletteindex = ($paletteindexbyte & (0x01 << $i)) >> $i;
0337                     $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
0338                     $col++;
0339                   }
0340                 }
0341                 while (($pixeldataoffset % 4) != 0) {
0342                   // lines are padded to nearest DWORD
0343                   $pixeldataoffset++;
0344                 }
0345               }
0346               break;
0347 
0348             case 4:
0349               for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
0350                 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col) {
0351                   $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset++});
0352                   for ($i = 1; $i >= 0; $i--) {
0353                     $paletteindex = ($paletteindexbyte & (0x0F << (4 * $i))) >> (4 * $i);
0354                     $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
0355                     $col++;
0356                   }
0357                 }
0358                 while (($pixeldataoffset % 4) != 0) {
0359                   // lines are padded to nearest DWORD
0360                   $pixeldataoffset++;
0361                 }
0362               }
0363               break;
0364 
0365             case 8:
0366               for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
0367                 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) {
0368                   $paletteindex = ord($BMPpixelData{$pixeldataoffset++});
0369                   $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
0370                 }
0371                 while (($pixeldataoffset % 4) != 0) {
0372                   // lines are padded to nearest DWORD
0373                   $pixeldataoffset++;
0374                 }
0375               }
0376               break;
0377 
0378             case 24:
0379               for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
0380                 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) {
0381                   $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset+2}) << 16) | (ord($BMPpixelData{$pixeldataoffset+1}) << 8) | ord($BMPpixelData{$pixeldataoffset});
0382                   $pixeldataoffset += 3;
0383                 }
0384                 while (($pixeldataoffset % 4) != 0) {
0385                   // lines are padded to nearest DWORD
0386                   $pixeldataoffset++;
0387                 }
0388               }
0389               break;
0390 
0391             case 32:
0392               for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
0393                 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) {
0394                   $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset+3}) << 24) | (ord($BMPpixelData{$pixeldataoffset+2}) << 16) | (ord($BMPpixelData{$pixeldataoffset+1}) << 8) | ord($BMPpixelData{$pixeldataoffset});
0395                   $pixeldataoffset += 4;
0396                 }
0397                 while (($pixeldataoffset % 4) != 0) {
0398                   // lines are padded to nearest DWORD
0399                   $pixeldataoffset++;
0400                 }
0401               }
0402               break;
0403 
0404             case 16:
0405               // ?
0406               break;
0407 
0408             default:
0409               $info['error'][] = 'Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data';
0410               break;
0411           }
0412           break;
0413 
0414 
0415         case 1: // BI_RLE8 - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_6x0u.asp
0416           switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
0417             case 8:
0418               $pixelcounter = 0;
0419               while ($pixeldataoffset < strlen($BMPpixelData)) {
0420                 $firstbyte  = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
0421                 $secondbyte = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
0422                 if ($firstbyte == 0) {
0423 
0424                   // escaped/absolute mode - the first byte of the pair can be set to zero to
0425                   // indicate an escape character that denotes the end of a line, the end of
0426                   // a bitmap, or a delta, depending on the value of the second byte.
0427                   switch ($secondbyte) {
0428                     case 0:
0429                       // end of line
0430                       // no need for special processing, just ignore
0431                       break;
0432 
0433                     case 1:
0434                       // end of bitmap
0435                       $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case
0436                       break;
0437 
0438                     case 2:
0439                       // delta - The 2 bytes following the escape contain unsigned values
0440                       // indicating the horizontal and vertical offsets of the next pixel
0441                       // from the current position.
0442                       $colincrement = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
0443                       $rowincrement = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
0444                       $col = ($pixelcounter % $thisfile_bmp_header_raw['width']) + $colincrement;
0445                       $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement;
0446                       $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) + $col;
0447                       break;
0448 
0449                     default:
0450                       // In absolute mode, the first byte is zero and the second byte is a
0451                       // value in the range 03H through FFH. The second byte represents the
0452                       // number of bytes that follow, each of which contains the color index
0453                       // of a single pixel. Each run must be aligned on a word boundary.
0454                       for ($i = 0; $i < $secondbyte; $i++) {
0455                         $paletteindex = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
0456                         $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
0457                         $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
0458                         $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
0459                         $pixelcounter++;
0460                       }
0461                       while (($pixeldataoffset % 2) != 0) {
0462                         // Each run must be aligned on a word boundary.
0463                         $pixeldataoffset++;
0464                       }
0465                       break;
0466                   }
0467 
0468                 } else {
0469 
0470                   // encoded mode - the first byte specifies the number of consecutive pixels
0471                   // to be drawn using the color index contained in the second byte.
0472                   for ($i = 0; $i < $firstbyte; $i++) {
0473                     $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
0474                     $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
0475                     $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$secondbyte];
0476                     $pixelcounter++;
0477                   }
0478 
0479                 }
0480               }
0481               break;
0482 
0483             default:
0484               $info['error'][] = 'Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data';
0485               break;
0486           }
0487           break;
0488 
0489 
0490 
0491         case 2: // BI_RLE4 - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_6x0u.asp
0492           switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
0493             case 4:
0494               $pixelcounter = 0;
0495               while ($pixeldataoffset < strlen($BMPpixelData)) {
0496                 $firstbyte  = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
0497                 $secondbyte = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
0498                 if ($firstbyte == 0) {
0499 
0500                   // escaped/absolute mode - the first byte of the pair can be set to zero to
0501                   // indicate an escape character that denotes the end of a line, the end of
0502                   // a bitmap, or a delta, depending on the value of the second byte.
0503                   switch ($secondbyte) {
0504                     case 0:
0505                       // end of line
0506                       // no need for special processing, just ignore
0507                       break;
0508 
0509                     case 1:
0510                       // end of bitmap
0511                       $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case
0512                       break;
0513 
0514                     case 2:
0515                       // delta - The 2 bytes following the escape contain unsigned values
0516                       // indicating the horizontal and vertical offsets of the next pixel
0517                       // from the current position.
0518                       $colincrement = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
0519                       $rowincrement = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
0520                       $col = ($pixelcounter % $thisfile_bmp_header_raw['width']) + $colincrement;
0521                       $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement;
0522                       $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) + $col;
0523                       break;
0524 
0525                     default:
0526                       // In absolute mode, the first byte is zero. The second byte contains the number
0527                       // of color indexes that follow. Subsequent bytes contain color indexes in their
0528                       // high- and low-order 4 bits, one color index for each pixel. In absolute mode,
0529                       // each run must be aligned on a word boundary.
0530                       unset($paletteindexes);
0531                       for ($i = 0; $i < ceil($secondbyte / 2); $i++) {
0532                         $paletteindexbyte = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1));
0533                         $paletteindexes[] = ($paletteindexbyte & 0xF0) >> 4;
0534                         $paletteindexes[] = ($paletteindexbyte & 0x0F);
0535                       }
0536                       while (($pixeldataoffset % 2) != 0) {
0537                         // Each run must be aligned on a word boundary.
0538                         $pixeldataoffset++;
0539                       }
0540 
0541                       foreach ($paletteindexes as $paletteindex) {
0542                         $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
0543                         $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
0544                         $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
0545                         $pixelcounter++;
0546                       }
0547                       break;
0548                   }
0549 
0550                 } else {
0551 
0552                   // encoded mode - the first byte of the pair contains the number of pixels to be
0553                   // drawn using the color indexes in the second byte. The second byte contains two
0554                   // color indexes, one in its high-order 4 bits and one in its low-order 4 bits.
0555                   // The first of the pixels is drawn using the color specified by the high-order
0556                   // 4 bits, the second is drawn using the color in the low-order 4 bits, the third
0557                   // is drawn using the color in the high-order 4 bits, and so on, until all the
0558                   // pixels specified by the first byte have been drawn.
0559                   $paletteindexes[0] = ($secondbyte & 0xF0) >> 4;
0560                   $paletteindexes[1] = ($secondbyte & 0x0F);
0561                   for ($i = 0; $i < $firstbyte; $i++) {
0562                     $col = $pixelcounter % $thisfile_bmp_header_raw['width'];
0563                     $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
0564                     $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindexes[($i % 2)]];
0565                     $pixelcounter++;
0566                   }
0567 
0568                 }
0569               }
0570               break;
0571 
0572             default:
0573               $info['error'][] = 'Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data';
0574               break;
0575           }
0576           break;
0577 
0578 
0579         case 3: // BI_BITFIELDS
0580           switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
0581             case 16:
0582             case 32:
0583               $redshift   = 0;
0584               $greenshift = 0;
0585               $blueshift  = 0;
0586               while ((($thisfile_bmp_header_raw['red_mask'] >> $redshift) & 0x01) == 0) {
0587                 $redshift++;
0588               }
0589               while ((($thisfile_bmp_header_raw['green_mask'] >> $greenshift) & 0x01) == 0) {
0590                 $greenshift++;
0591               }
0592               while ((($thisfile_bmp_header_raw['blue_mask'] >> $blueshift) & 0x01) == 0) {
0593                 $blueshift++;
0594               }
0595               for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
0596                 for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) {
0597                   $pixelvalue = getid3_lib::LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset, $thisfile_bmp_header_raw['bits_per_pixel'] / 8));
0598                   $pixeldataoffset += $thisfile_bmp_header_raw['bits_per_pixel'] / 8;
0599 
0600                   $red   = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['red_mask'])   >> $redshift)   / ($thisfile_bmp_header_raw['red_mask']   >> $redshift))   * 255));
0601                   $green = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['green_mask']) >> $greenshift) / ($thisfile_bmp_header_raw['green_mask'] >> $greenshift)) * 255));
0602                   $blue  = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['blue_mask'])  >> $blueshift)  / ($thisfile_bmp_header_raw['blue_mask']  >> $blueshift))  * 255));
0603                   $thisfile_bmp['data'][$row][$col] = (($red << 16) | ($green << 8) | ($blue));
0604                 }
0605                 while (($pixeldataoffset % 4) != 0) {
0606                   // lines are padded to nearest DWORD
0607                   $pixeldataoffset++;
0608                 }
0609               }
0610               break;
0611 
0612             default:
0613               $info['error'][] = 'Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data';
0614               break;
0615           }
0616           break;
0617 
0618 
0619         default: // unhandled compression type
0620           $info['error'][] = 'Unknown/unhandled compression type value ('.$thisfile_bmp_header_raw['compression'].') - cannot decompress pixel data';
0621           break;
0622       }
0623     }
0624 
0625     return true;
0626   }
0627 
0628 
0629   public function PlotBMP(&$BMPinfo) {
0630     $starttime = time();
0631     if (!isset($BMPinfo['bmp']['data']) || !is_array($BMPinfo['bmp']['data'])) {
0632       echo 'ERROR: no pixel data<BR>';
0633       return false;
0634     }
0635     set_time_limit(intval(round($BMPinfo['resolution_x'] * $BMPinfo['resolution_y'] / 10000)));
0636     if ($im = ImageCreateTrueColor($BMPinfo['resolution_x'], $BMPinfo['resolution_y'])) {
0637       for ($row = 0; $row < $BMPinfo['resolution_y']; $row++) {
0638         for ($col = 0; $col < $BMPinfo['resolution_x']; $col++) {
0639           if (isset($BMPinfo['bmp']['data'][$row][$col])) {
0640             $red   = ($BMPinfo['bmp']['data'][$row][$col] & 0x00FF0000) >> 16;
0641             $green = ($BMPinfo['bmp']['data'][$row][$col] & 0x0000FF00) >> 8;
0642             $blue  = ($BMPinfo['bmp']['data'][$row][$col] & 0x000000FF);
0643             $pixelcolor = ImageColorAllocate($im, $red, $green, $blue);
0644             ImageSetPixel($im, $col, $row, $pixelcolor);
0645           } else {
0646             //echo 'ERROR: no data for pixel '.$row.' x '.$col.'<BR>';
0647             //return false;
0648           }
0649         }
0650       }
0651       if (headers_sent()) {
0652         echo 'plotted '.($BMPinfo['resolution_x'] * $BMPinfo['resolution_y']).' pixels in '.(time() - $starttime).' seconds<BR>';
0653         ImageDestroy($im);
0654         exit;
0655       } else {
0656         header('Content-type: image/png');
0657         ImagePNG($im);
0658         ImageDestroy($im);
0659         return true;
0660       }
0661     }
0662     return false;
0663   }
0664 
0665   public function BMPcompressionWindowsLookup($compressionid) {
0666     static $BMPcompressionWindowsLookup = array(
0667       0 => 'BI_RGB',
0668       1 => 'BI_RLE8',
0669       2 => 'BI_RLE4',
0670       3 => 'BI_BITFIELDS',
0671       4 => 'BI_JPEG',
0672       5 => 'BI_PNG'
0673     );
0674     return (isset($BMPcompressionWindowsLookup[$compressionid]) ? $BMPcompressionWindowsLookup[$compressionid] : 'invalid');
0675   }
0676 
0677   public function BMPcompressionOS2Lookup($compressionid) {
0678     static $BMPcompressionOS2Lookup = array(
0679       0 => 'BI_RGB',
0680       1 => 'BI_RLE8',
0681       2 => 'BI_RLE4',
0682       3 => 'Huffman 1D',
0683       4 => 'BI_RLE24',
0684     );
0685     return (isset($BMPcompressionOS2Lookup[$compressionid]) ? $BMPcompressionOS2Lookup[$compressionid] : 'invalid');
0686   }
0687 
0688 }