| Conditions | 84 |
| Paths | 4544 |
| Total Lines | 601 |
| Code Lines | 371 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 112 | public function getid3_bmp(&$BMPdata, &$ThisFileInfo, $ExtractPalette = false, $ExtractData = false) |
||
| 113 | { |
||
| 114 | |||
| 115 | // shortcuts |
||
| 116 | $ThisFileInfo['bmp']['header']['raw'] = []; |
||
| 117 | $thisfile_bmp =& $ThisFileInfo['bmp']; |
||
| 118 | $thisfile_bmp_header =& $thisfile_bmp['header']; |
||
| 119 | $thisfile_bmp_header_raw =& $thisfile_bmp_header['raw']; |
||
| 120 | |||
| 121 | // BITMAPFILEHEADER [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp |
||
| 122 | // all versions |
||
| 123 | // WORD bfType; |
||
| 124 | // DWORD bfSize; |
||
| 125 | // WORD bfReserved1; |
||
| 126 | // WORD bfReserved2; |
||
| 127 | // DWORD bfOffBits; |
||
| 128 | |||
| 129 | $offset = 0; |
||
| 130 | $overalloffset = 0; |
||
| 131 | $BMPheader = substr($BMPdata, $overalloffset, 14 + 40); |
||
| 132 | $overalloffset += (14 + 40); |
||
| 133 | |||
| 134 | $thisfile_bmp_header_raw['identifier'] = substr($BMPheader, $offset, 2); |
||
| 135 | $offset += 2; |
||
| 136 | |||
| 137 | if ('BM' !== $thisfile_bmp_header_raw['identifier']) { |
||
| 138 | $ThisFileInfo['error'][] = 'Expecting "BM" at offset ' . (int)(@$ThisFileInfo['avdataoffset']) . ', found "' . $thisfile_bmp_header_raw['identifier'] . '"'; |
||
| 139 | unset($ThisFileInfo['fileformat']); |
||
| 140 | unset($ThisFileInfo['bmp']); |
||
| 141 | |||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | $thisfile_bmp_header_raw['filesize'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 146 | $offset += 4; |
||
| 147 | $thisfile_bmp_header_raw['reserved1'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 148 | $offset += 2; |
||
| 149 | $thisfile_bmp_header_raw['reserved2'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 150 | $offset += 2; |
||
| 151 | $thisfile_bmp_header_raw['data_offset'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 152 | $offset += 4; |
||
| 153 | $thisfile_bmp_header_raw['header_size'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 154 | $offset += 4; |
||
| 155 | |||
| 156 | // check if the hardcoded-to-1 "planes" is at offset 22 or 26 |
||
| 157 | $planes22 = $this->LittleEndian2Int(substr($BMPheader, 22, 2)); |
||
| 158 | $planes26 = $this->LittleEndian2Int(substr($BMPheader, 26, 2)); |
||
| 159 | if ((1 == $planes22) && (1 != $planes26)) { |
||
| 160 | $thisfile_bmp['type_os'] = 'OS/2'; |
||
| 161 | $thisfile_bmp['type_version'] = 1; |
||
| 162 | } elseif ((1 == $planes26) && (1 != $planes22)) { |
||
| 163 | $thisfile_bmp['type_os'] = 'Windows'; |
||
| 164 | $thisfile_bmp['type_version'] = 1; |
||
| 165 | } elseif (12 == $thisfile_bmp_header_raw['header_size']) { |
||
| 166 | $thisfile_bmp['type_os'] = 'OS/2'; |
||
| 167 | $thisfile_bmp['type_version'] = 1; |
||
| 168 | } elseif (40 == $thisfile_bmp_header_raw['header_size']) { |
||
| 169 | $thisfile_bmp['type_os'] = 'Windows'; |
||
| 170 | $thisfile_bmp['type_version'] = 1; |
||
| 171 | } elseif (84 == $thisfile_bmp_header_raw['header_size']) { |
||
| 172 | $thisfile_bmp['type_os'] = 'Windows'; |
||
| 173 | $thisfile_bmp['type_version'] = 4; |
||
| 174 | } elseif (100 == $thisfile_bmp_header_raw['header_size']) { |
||
| 175 | $thisfile_bmp['type_os'] = 'Windows'; |
||
| 176 | $thisfile_bmp['type_version'] = 5; |
||
| 177 | } else { |
||
| 178 | $ThisFileInfo['error'][] = 'Unknown BMP subtype (or not a BMP file)'; |
||
| 179 | unset($ThisFileInfo['fileformat']); |
||
| 180 | unset($ThisFileInfo['bmp']); |
||
| 181 | |||
| 182 | return false; |
||
| 183 | } |
||
| 184 | |||
| 185 | $ThisFileInfo['fileformat'] = 'bmp'; |
||
| 186 | $ThisFileInfo['video']['dataformat'] = 'bmp'; |
||
| 187 | $ThisFileInfo['video']['lossless'] = true; |
||
| 188 | $ThisFileInfo['video']['pixel_aspect_ratio'] = (float)1; |
||
| 189 | |||
| 190 | if ('OS/2' === $thisfile_bmp['type_os']) { |
||
| 191 | |||
| 192 | // OS/2-format BMP |
||
| 193 | // http://netghost.narod.ru/gff/graphics/summary/os2bmp.htm |
||
| 194 | |||
| 195 | // DWORD Size; /* Size of this structure in bytes */ |
||
| 196 | // DWORD Width; /* Bitmap width in pixels */ |
||
| 197 | // DWORD Height; /* Bitmap height in pixel */ |
||
| 198 | // WORD NumPlanes; /* Number of bit planes (color depth) */ |
||
| 199 | // WORD BitsPerPixel; /* Number of bits per pixel per plane */ |
||
| 200 | |||
| 201 | $thisfile_bmp_header_raw['width'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 202 | $offset += 2; |
||
| 203 | $thisfile_bmp_header_raw['height'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 204 | $offset += 2; |
||
| 205 | $thisfile_bmp_header_raw['planes'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 206 | $offset += 2; |
||
| 207 | $thisfile_bmp_header_raw['bits_per_pixel'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 208 | $offset += 2; |
||
| 209 | |||
| 210 | $ThisFileInfo['video']['resolution_x'] = $thisfile_bmp_header_raw['width']; |
||
| 211 | $ThisFileInfo['video']['resolution_y'] = $thisfile_bmp_header_raw['height']; |
||
| 212 | $ThisFileInfo['video']['codec'] = 'BI_RGB ' . $thisfile_bmp_header_raw['bits_per_pixel'] . '-bit'; |
||
| 213 | $ThisFileInfo['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel']; |
||
| 214 | |||
| 215 | if ($thisfile_bmp['type_version'] >= 2) { |
||
| 216 | // DWORD Compression; /* Bitmap compression scheme */ |
||
| 217 | // DWORD ImageDataSize; /* Size of bitmap data in bytes */ |
||
| 218 | // DWORD XResolution; /* X resolution of display device */ |
||
| 219 | // DWORD YResolution; /* Y resolution of display device */ |
||
| 220 | // DWORD ColorsUsed; /* Number of color table indices used */ |
||
| 221 | // DWORD ColorsImportant; /* Number of important color indices */ |
||
| 222 | // WORD Units; /* Type of units used to measure resolution */ |
||
| 223 | // WORD Reserved; /* Pad structure to 4-byte boundary */ |
||
| 224 | // WORD Recording; /* Recording algorithm */ |
||
| 225 | // WORD Rendering; /* Halftoning algorithm used */ |
||
| 226 | // DWORD Size1; /* Reserved for halftoning algorithm use */ |
||
| 227 | // DWORD Size2; /* Reserved for halftoning algorithm use */ |
||
| 228 | // DWORD ColorEncoding; /* Color model used in bitmap */ |
||
| 229 | // DWORD Identifier; /* Reserved for application use */ |
||
| 230 | |||
| 231 | $thisfile_bmp_header_raw['compression'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 232 | $offset += 4; |
||
| 233 | $thisfile_bmp_header_raw['bmp_data_size'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 234 | $offset += 4; |
||
| 235 | $thisfile_bmp_header_raw['resolution_h'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 236 | $offset += 4; |
||
| 237 | $thisfile_bmp_header_raw['resolution_v'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 238 | $offset += 4; |
||
| 239 | $thisfile_bmp_header_raw['colors_used'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 240 | $offset += 4; |
||
| 241 | $thisfile_bmp_header_raw['colors_important'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 242 | $offset += 4; |
||
| 243 | $thisfile_bmp_header_raw['resolution_units'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 244 | $offset += 2; |
||
| 245 | $thisfile_bmp_header_raw['reserved1'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 246 | $offset += 2; |
||
| 247 | $thisfile_bmp_header_raw['recording'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 248 | $offset += 2; |
||
| 249 | $thisfile_bmp_header_raw['rendering'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 250 | $offset += 2; |
||
| 251 | $thisfile_bmp_header_raw['size1'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 252 | $offset += 4; |
||
| 253 | $thisfile_bmp_header_raw['size2'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 254 | $offset += 4; |
||
| 255 | $thisfile_bmp_header_raw['color_encoding'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 256 | $offset += 4; |
||
| 257 | $thisfile_bmp_header_raw['identifier'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 258 | $offset += 4; |
||
| 259 | |||
| 260 | $thisfile_bmp_header['compression'] = $this->BMPcompressionOS2Lookup($thisfile_bmp_header_raw['compression']); |
||
| 261 | |||
| 262 | $ThisFileInfo['video']['codec'] = $thisfile_bmp_header['compression'] . ' ' . $thisfile_bmp_header_raw['bits_per_pixel'] . '-bit'; |
||
| 263 | } |
||
| 264 | } elseif ('Windows' === $thisfile_bmp['type_os']) { |
||
| 265 | |||
| 266 | // Windows-format BMP |
||
| 267 | |||
| 268 | // BITMAPINFOHEADER - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp |
||
| 269 | // all versions |
||
| 270 | // DWORD biSize; |
||
| 271 | // LONG biWidth; |
||
| 272 | // LONG biHeight; |
||
| 273 | // WORD biPlanes; |
||
| 274 | // WORD biBitCount; |
||
| 275 | // DWORD biCompression; |
||
| 276 | // DWORD biSizeImage; |
||
| 277 | // LONG biXPelsPerMeter; |
||
| 278 | // LONG biYPelsPerMeter; |
||
| 279 | // DWORD biClrUsed; |
||
| 280 | // DWORD biClrImportant; |
||
| 281 | |||
| 282 | $thisfile_bmp_header_raw['width'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 283 | $offset += 4; |
||
| 284 | $thisfile_bmp_header_raw['height'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 285 | $offset += 4; |
||
| 286 | $thisfile_bmp_header_raw['planes'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 287 | $offset += 2; |
||
| 288 | $thisfile_bmp_header_raw['bits_per_pixel'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 2)); |
||
| 289 | $offset += 2; |
||
| 290 | $thisfile_bmp_header_raw['compression'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 291 | $offset += 4; |
||
| 292 | $thisfile_bmp_header_raw['bmp_data_size'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 293 | $offset += 4; |
||
| 294 | $thisfile_bmp_header_raw['resolution_h'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 295 | $offset += 4; |
||
| 296 | $thisfile_bmp_header_raw['resolution_v'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 297 | $offset += 4; |
||
| 298 | $thisfile_bmp_header_raw['colors_used'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 299 | $offset += 4; |
||
| 300 | $thisfile_bmp_header_raw['colors_important'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 301 | $offset += 4; |
||
| 302 | |||
| 303 | $thisfile_bmp_header['compression'] = $this->BMPcompressionWindowsLookup($thisfile_bmp_header_raw['compression']); |
||
| 304 | $ThisFileInfo['video']['resolution_x'] = $thisfile_bmp_header_raw['width']; |
||
| 305 | $ThisFileInfo['video']['resolution_y'] = $thisfile_bmp_header_raw['height']; |
||
| 306 | $ThisFileInfo['video']['codec'] = $thisfile_bmp_header['compression'] . ' ' . $thisfile_bmp_header_raw['bits_per_pixel'] . '-bit'; |
||
| 307 | $ThisFileInfo['video']['bits_per_sample'] = $thisfile_bmp_header_raw['bits_per_pixel']; |
||
| 308 | |||
| 309 | if (($thisfile_bmp['type_version'] >= 4) || (3 == $thisfile_bmp_header_raw['compression'])) { |
||
| 310 | // should only be v4+, but BMPs with type_version==1 and BI_BITFIELDS compression have been seen |
||
| 311 | $BMPheader .= substr($BMPdata, $overalloffset, 44); |
||
| 312 | $overalloffset += 44; |
||
| 313 | |||
| 314 | // BITMAPV4HEADER - [44 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_2k1e.asp |
||
| 315 | // Win95+, WinNT4.0+ |
||
| 316 | // DWORD bV4RedMask; |
||
| 317 | // DWORD bV4GreenMask; |
||
| 318 | // DWORD bV4BlueMask; |
||
| 319 | // DWORD bV4AlphaMask; |
||
| 320 | // DWORD bV4CSType; |
||
| 321 | // CIEXYZTRIPLE bV4Endpoints; |
||
| 322 | // DWORD bV4GammaRed; |
||
| 323 | // DWORD bV4GammaGreen; |
||
| 324 | // DWORD bV4GammaBlue; |
||
| 325 | $thisfile_bmp_header_raw['red_mask'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 326 | $offset += 4; |
||
| 327 | $thisfile_bmp_header_raw['green_mask'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 328 | $offset += 4; |
||
| 329 | $thisfile_bmp_header_raw['blue_mask'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 330 | $offset += 4; |
||
| 331 | $thisfile_bmp_header_raw['alpha_mask'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 332 | $offset += 4; |
||
| 333 | $thisfile_bmp_header_raw['cs_type'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 334 | $offset += 4; |
||
| 335 | $thisfile_bmp_header_raw['ciexyz_red'] = substr($BMPheader, $offset, 4); |
||
| 336 | $offset += 4; |
||
| 337 | $thisfile_bmp_header_raw['ciexyz_green'] = substr($BMPheader, $offset, 4); |
||
| 338 | $offset += 4; |
||
| 339 | $thisfile_bmp_header_raw['ciexyz_blue'] = substr($BMPheader, $offset, 4); |
||
| 340 | $offset += 4; |
||
| 341 | $thisfile_bmp_header_raw['gamma_red'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 342 | $offset += 4; |
||
| 343 | $thisfile_bmp_header_raw['gamma_green'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 344 | $offset += 4; |
||
| 345 | $thisfile_bmp_header_raw['gamma_blue'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 346 | $offset += 4; |
||
| 347 | |||
| 348 | $thisfile_bmp_header['ciexyz_red'] = $this->FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_red'])); |
||
| 349 | $thisfile_bmp_header['ciexyz_green'] = $this->FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_green'])); |
||
| 350 | $thisfile_bmp_header['ciexyz_blue'] = $this->FixedPoint2_30(strrev($thisfile_bmp_header_raw['ciexyz_blue'])); |
||
| 351 | } |
||
| 352 | |||
| 353 | if ($thisfile_bmp['type_version'] >= 5) { |
||
| 354 | $BMPheader .= substr($BMPdata, $overalloffset, 16); |
||
| 355 | $overalloffset += 16; |
||
| 356 | |||
| 357 | // BITMAPV5HEADER - [16 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_7c36.asp |
||
| 358 | // Win98+, Win2000+ |
||
| 359 | // DWORD bV5Intent; |
||
| 360 | // DWORD bV5ProfileData; |
||
| 361 | // DWORD bV5ProfileSize; |
||
| 362 | // DWORD bV5Reserved; |
||
| 363 | $thisfile_bmp_header_raw['intent'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 364 | $offset += 4; |
||
| 365 | $thisfile_bmp_header_raw['profile_data_offset'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 366 | $offset += 4; |
||
| 367 | $thisfile_bmp_header_raw['profile_data_size'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 368 | $offset += 4; |
||
| 369 | $thisfile_bmp_header_raw['reserved3'] = $this->LittleEndian2Int(substr($BMPheader, $offset, 4)); |
||
| 370 | $offset += 4; |
||
| 371 | } |
||
| 372 | } else { |
||
| 373 | $ThisFileInfo['error'][] = 'Unknown BMP format in header.'; |
||
| 374 | |||
| 375 | return false; |
||
| 376 | } |
||
| 377 | |||
| 378 | if ($ExtractPalette || $ExtractData) { |
||
| 379 | $PaletteEntries = 0; |
||
| 380 | if ($thisfile_bmp_header_raw['bits_per_pixel'] < 16) { |
||
| 381 | $PaletteEntries = 2 ** $thisfile_bmp_header_raw['bits_per_pixel']; |
||
| 382 | } elseif (isset($thisfile_bmp_header_raw['colors_used']) && ($thisfile_bmp_header_raw['colors_used'] > 0) |
||
| 383 | && ($thisfile_bmp_header_raw['colors_used'] <= 256)) { |
||
| 384 | $PaletteEntries = $thisfile_bmp_header_raw['colors_used']; |
||
| 385 | } |
||
| 386 | if ($PaletteEntries > 0) { |
||
| 387 | $BMPpalette = substr($BMPdata, $overalloffset, 4 * $PaletteEntries); |
||
| 388 | $overalloffset += 4 * $PaletteEntries; |
||
| 389 | |||
| 390 | $paletteoffset = 0; |
||
| 391 | for ($i = 0; $i < $PaletteEntries; $i++) { |
||
| 392 | // RGBQUAD - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_5f8y.asp |
||
| 393 | // BYTE rgbBlue; |
||
| 394 | // BYTE rgbGreen; |
||
| 395 | // BYTE rgbRed; |
||
| 396 | // BYTE rgbReserved; |
||
| 397 | $blue = $this->LittleEndian2Int(substr($BMPpalette, $paletteoffset++, 1)); |
||
| 398 | $green = $this->LittleEndian2Int(substr($BMPpalette, $paletteoffset++, 1)); |
||
| 399 | $red = $this->LittleEndian2Int(substr($BMPpalette, $paletteoffset++, 1)); |
||
| 400 | if (('OS/2' === $thisfile_bmp['type_os']) && (1 == $thisfile_bmp['type_version'])) { |
||
| 401 | // no padding byte |
||
| 402 | } else { |
||
| 403 | $paletteoffset++; // padding byte |
||
| 404 | } |
||
| 405 | $thisfile_bmp['palette'][$i] = (($red << 16) | ($green << 8) | $blue); |
||
| 406 | } |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | if ($ExtractData) { |
||
| 411 | $RowByteLength = ceil(($thisfile_bmp_header_raw['width'] * ($thisfile_bmp_header_raw['bits_per_pixel'] / 8)) / 4) * 4; // round up to nearest DWORD boundry |
||
| 412 | |||
| 413 | $BMPpixelData = substr($BMPdata, $thisfile_bmp_header_raw['data_offset'], $thisfile_bmp_header_raw['height'] * $RowByteLength); |
||
|
|
|||
| 414 | $overalloffset = $thisfile_bmp_header_raw['data_offset'] + ($thisfile_bmp_header_raw['height'] * $RowByteLength); |
||
| 415 | |||
| 416 | $pixeldataoffset = 0; |
||
| 417 | switch (@$thisfile_bmp_header_raw['compression']) { |
||
| 418 | |||
| 419 | case 0: // BI_RGB |
||
| 420 | switch ($thisfile_bmp_header_raw['bits_per_pixel']) { |
||
| 421 | case 1: |
||
| 422 | for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) { |
||
| 423 | for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col) { |
||
| 424 | $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset++}); |
||
| 425 | for ($i = 7; $i >= 0; $i--) { |
||
| 426 | $paletteindex = ($paletteindexbyte & (0x01 << $i)) >> $i; |
||
| 427 | $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex]; |
||
| 428 | $col++; |
||
| 429 | } |
||
| 430 | } |
||
| 431 | while (0 != ($pixeldataoffset % 4)) { |
||
| 432 | // lines are padded to nearest DWORD |
||
| 433 | $pixeldataoffset++; |
||
| 434 | } |
||
| 435 | } |
||
| 436 | break; |
||
| 437 | |||
| 438 | case 4: |
||
| 439 | for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) { |
||
| 440 | for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col = $col) { |
||
| 441 | $paletteindexbyte = ord($BMPpixelData{$pixeldataoffset++}); |
||
| 442 | for ($i = 1; $i >= 0; $i--) { |
||
| 443 | $paletteindex = ($paletteindexbyte & (0x0F << (4 * $i))) >> (4 * $i); |
||
| 444 | $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex]; |
||
| 445 | $col++; |
||
| 446 | } |
||
| 447 | } |
||
| 448 | while (0 != ($pixeldataoffset % 4)) { |
||
| 449 | // lines are padded to nearest DWORD |
||
| 450 | $pixeldataoffset++; |
||
| 451 | } |
||
| 452 | } |
||
| 453 | break; |
||
| 454 | |||
| 455 | case 8: |
||
| 456 | for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) { |
||
| 457 | for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) { |
||
| 458 | $paletteindex = ord($BMPpixelData{$pixeldataoffset++}); |
||
| 459 | $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex]; |
||
| 460 | } |
||
| 461 | while (0 != ($pixeldataoffset % 4)) { |
||
| 462 | // lines are padded to nearest DWORD |
||
| 463 | $pixeldataoffset++; |
||
| 464 | } |
||
| 465 | } |
||
| 466 | break; |
||
| 467 | |||
| 468 | case 24: |
||
| 469 | for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) { |
||
| 470 | for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) { |
||
| 471 | $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset + 2}) << 16) | (ord($BMPpixelData{$pixeldataoffset + 1}) << 8) | ord($BMPpixelData{$pixeldataoffset}); |
||
| 472 | $pixeldataoffset += 3; |
||
| 473 | } |
||
| 474 | while (0 != ($pixeldataoffset % 4)) { |
||
| 475 | // lines are padded to nearest DWORD |
||
| 476 | $pixeldataoffset++; |
||
| 477 | } |
||
| 478 | } |
||
| 479 | break; |
||
| 480 | |||
| 481 | case 32: |
||
| 482 | for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) { |
||
| 483 | for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) { |
||
| 484 | $thisfile_bmp['data'][$row][$col] = (ord($BMPpixelData{$pixeldataoffset + 3}) << 24) | (ord($BMPpixelData{$pixeldataoffset + 2}) << 16) | (ord($BMPpixelData{$pixeldataoffset + 1}) << 8) | ord($BMPpixelData{$pixeldataoffset}); |
||
| 485 | $pixeldataoffset += 4; |
||
| 486 | } |
||
| 487 | while (0 != ($pixeldataoffset % 4)) { |
||
| 488 | // lines are padded to nearest DWORD |
||
| 489 | $pixeldataoffset++; |
||
| 490 | } |
||
| 491 | } |
||
| 492 | break; |
||
| 493 | |||
| 494 | case 16: |
||
| 495 | // ? |
||
| 496 | break; |
||
| 497 | |||
| 498 | default: |
||
| 499 | $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value (' . $thisfile_bmp_header_raw['bits_per_pixel'] . ') - cannot read pixel data'; |
||
| 500 | break; |
||
| 501 | } |
||
| 502 | break; |
||
| 503 | |||
| 504 | case 1: // BI_RLE8 - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_6x0u.asp |
||
| 505 | switch ($thisfile_bmp_header_raw['bits_per_pixel']) { |
||
| 506 | case 8: |
||
| 507 | $pixelcounter = 0; |
||
| 508 | while ($pixeldataoffset < strlen($BMPpixelData)) { |
||
| 509 | $firstbyte = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1)); |
||
| 510 | $secondbyte = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1)); |
||
| 511 | if (0 == $firstbyte) { |
||
| 512 | |||
| 513 | // escaped/absolute mode - the first byte of the pair can be set to zero to |
||
| 514 | // indicate an escape character that denotes the end of a line, the end of |
||
| 515 | // a bitmap, or a delta, depending on the value of the second byte. |
||
| 516 | switch ($secondbyte) { |
||
| 517 | case 0: |
||
| 518 | // end of line |
||
| 519 | // no need for special processing, just ignore |
||
| 520 | break; |
||
| 521 | |||
| 522 | case 1: |
||
| 523 | // end of bitmap |
||
| 524 | $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case |
||
| 525 | break; |
||
| 526 | |||
| 527 | case 2: |
||
| 528 | // delta - The 2 bytes following the escape contain unsigned values |
||
| 529 | // indicating the horizontal and vertical offsets of the next pixel |
||
| 530 | // from the current position. |
||
| 531 | $colincrement = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1)); |
||
| 532 | $rowincrement = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1)); |
||
| 533 | $col = ($pixelcounter % $thisfile_bmp_header_raw['width']) + $colincrement; |
||
| 534 | $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement; |
||
| 535 | $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) + $col; |
||
| 536 | break; |
||
| 537 | |||
| 538 | default: |
||
| 539 | // In absolute mode, the first byte is zero and the second byte is a |
||
| 540 | // value in the range 03H through FFH. The second byte represents the |
||
| 541 | // number of bytes that follow, each of which contains the color index |
||
| 542 | // of a single pixel. Each run must be aligned on a word boundary. |
||
| 543 | for ($i = 0; $i < $secondbyte; $i++) { |
||
| 544 | $paletteindex = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1)); |
||
| 545 | $col = $pixelcounter % $thisfile_bmp_header_raw['width']; |
||
| 546 | $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']); |
||
| 547 | $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex]; |
||
| 548 | $pixelcounter++; |
||
| 549 | } |
||
| 550 | while (0 != ($pixeldataoffset % 2)) { |
||
| 551 | // Each run must be aligned on a word boundary. |
||
| 552 | $pixeldataoffset++; |
||
| 553 | } |
||
| 554 | break; |
||
| 555 | } |
||
| 556 | } else { |
||
| 557 | |||
| 558 | // encoded mode - the first byte specifies the number of consecutive pixels |
||
| 559 | // to be drawn using the color index contained in the second byte. |
||
| 560 | for ($i = 0; $i < $firstbyte; $i++) { |
||
| 561 | $col = $pixelcounter % $thisfile_bmp_header_raw['width']; |
||
| 562 | $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']); |
||
| 563 | $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$secondbyte]; |
||
| 564 | $pixelcounter++; |
||
| 565 | } |
||
| 566 | } |
||
| 567 | } |
||
| 568 | break; |
||
| 569 | |||
| 570 | default: |
||
| 571 | $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value (' . $thisfile_bmp_header_raw['bits_per_pixel'] . ') - cannot read pixel data'; |
||
| 572 | break; |
||
| 573 | } |
||
| 574 | break; |
||
| 575 | |||
| 576 | case 2: // BI_RLE4 - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_6x0u.asp |
||
| 577 | switch ($thisfile_bmp_header_raw['bits_per_pixel']) { |
||
| 578 | case 4: |
||
| 579 | $pixelcounter = 0; |
||
| 580 | while ($pixeldataoffset < strlen($BMPpixelData)) { |
||
| 581 | $firstbyte = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1)); |
||
| 582 | $secondbyte = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1)); |
||
| 583 | if (0 == $firstbyte) { |
||
| 584 | |||
| 585 | // escaped/absolute mode - the first byte of the pair can be set to zero to |
||
| 586 | // indicate an escape character that denotes the end of a line, the end of |
||
| 587 | // a bitmap, or a delta, depending on the value of the second byte. |
||
| 588 | switch ($secondbyte) { |
||
| 589 | case 0: |
||
| 590 | // end of line |
||
| 591 | // no need for special processing, just ignore |
||
| 592 | break; |
||
| 593 | |||
| 594 | case 1: |
||
| 595 | // end of bitmap |
||
| 596 | $pixeldataoffset = strlen($BMPpixelData); // force to exit loop just in case |
||
| 597 | break; |
||
| 598 | |||
| 599 | case 2: |
||
| 600 | // delta - The 2 bytes following the escape contain unsigned values |
||
| 601 | // indicating the horizontal and vertical offsets of the next pixel |
||
| 602 | // from the current position. |
||
| 603 | $colincrement = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1)); |
||
| 604 | $rowincrement = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1)); |
||
| 605 | $col = ($pixelcounter % $thisfile_bmp_header_raw['width']) + $colincrement; |
||
| 606 | $row = ($thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width'])) - $rowincrement; |
||
| 607 | $pixelcounter = ($row * $thisfile_bmp_header_raw['width']) + $col; |
||
| 608 | break; |
||
| 609 | |||
| 610 | default: |
||
| 611 | // In absolute mode, the first byte is zero. The second byte contains the number |
||
| 612 | // of color indexes that follow. Subsequent bytes contain color indexes in their |
||
| 613 | // high- and low-order 4 bits, one color index for each pixel. In absolute mode, |
||
| 614 | // each run must be aligned on a word boundary. |
||
| 615 | $paletteindexes = []; |
||
| 616 | for ($i = 0; $i < ceil($secondbyte / 2); $i++) { |
||
| 617 | $paletteindexbyte = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset++, 1)); |
||
| 618 | $paletteindexes[] = ($paletteindexbyte & 0xF0) >> 4; |
||
| 619 | $paletteindexes[] = ($paletteindexbyte & 0x0F); |
||
| 620 | } |
||
| 621 | while (0 != ($pixeldataoffset % 2)) { |
||
| 622 | // Each run must be aligned on a word boundary. |
||
| 623 | $pixeldataoffset++; |
||
| 624 | } |
||
| 625 | |||
| 626 | foreach ($paletteindexes as $dummy => $paletteindex) { |
||
| 627 | $col = $pixelcounter % $thisfile_bmp_header_raw['width']; |
||
| 628 | $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']); |
||
| 629 | $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex]; |
||
| 630 | $pixelcounter++; |
||
| 631 | } |
||
| 632 | break; |
||
| 633 | } |
||
| 634 | } else { |
||
| 635 | |||
| 636 | // encoded mode - the first byte of the pair contains the number of pixels to be |
||
| 637 | // drawn using the color indexes in the second byte. The second byte contains two |
||
| 638 | // color indexes, one in its high-order 4 bits and one in its low-order 4 bits. |
||
| 639 | // The first of the pixels is drawn using the color specified by the high-order |
||
| 640 | // 4 bits, the second is drawn using the color in the low-order 4 bits, the third |
||
| 641 | // is drawn using the color in the high-order 4 bits, and so on, until all the |
||
| 642 | // pixels specified by the first byte have been drawn. |
||
| 643 | $paletteindexes[0] = ($secondbyte & 0xF0) >> 4; |
||
| 644 | $paletteindexes[1] = ($secondbyte & 0x0F); |
||
| 645 | for ($i = 0; $i < $firstbyte; $i++) { |
||
| 646 | $col = $pixelcounter % $thisfile_bmp_header_raw['width']; |
||
| 647 | $row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']); |
||
| 648 | $thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindexes[$i % 2]]; |
||
| 649 | $pixelcounter++; |
||
| 650 | } |
||
| 651 | } |
||
| 652 | } |
||
| 653 | break; |
||
| 654 | |||
| 655 | default: |
||
| 656 | $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value (' . $thisfile_bmp_header_raw['bits_per_pixel'] . ') - cannot read pixel data'; |
||
| 657 | break; |
||
| 658 | } |
||
| 659 | break; |
||
| 660 | |||
| 661 | case 3: // BI_BITFIELDS |
||
| 662 | switch ($thisfile_bmp_header_raw['bits_per_pixel']) { |
||
| 663 | case 16: |
||
| 664 | case 32: |
||
| 665 | $redshift = 0; |
||
| 666 | $greenshift = 0; |
||
| 667 | $blueshift = 0; |
||
| 668 | if (!$thisfile_bmp_header_raw['red_mask'] || !$thisfile_bmp_header_raw['green_mask'] |
||
| 669 | || !$thisfile_bmp_header_raw['blue_mask']) { |
||
| 670 | $ThisFileInfo['error'][] = 'missing $thisfile_bmp_header_raw[(red|green|blue)_mask]'; |
||
| 671 | |||
| 672 | return false; |
||
| 673 | } |
||
| 674 | while (0 == (($thisfile_bmp_header_raw['red_mask'] >> $redshift) & 0x01)) { |
||
| 675 | $redshift++; |
||
| 676 | } |
||
| 677 | while (0 == (($thisfile_bmp_header_raw['green_mask'] >> $greenshift) & 0x01)) { |
||
| 678 | $greenshift++; |
||
| 679 | } |
||
| 680 | while (0 == (($thisfile_bmp_header_raw['blue_mask'] >> $blueshift) & 0x01)) { |
||
| 681 | $blueshift++; |
||
| 682 | } |
||
| 683 | for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) { |
||
| 684 | for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) { |
||
| 685 | $pixelvalue = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset, $thisfile_bmp_header_raw['bits_per_pixel'] / 8)); |
||
| 686 | $pixeldataoffset += $thisfile_bmp_header_raw['bits_per_pixel'] / 8; |
||
| 687 | |||
| 688 | $red = (int)round(((($pixelvalue & $thisfile_bmp_header_raw['red_mask']) >> $redshift) / ($thisfile_bmp_header_raw['red_mask'] >> $redshift)) * 255); |
||
| 689 | $green = (int)round(((($pixelvalue & $thisfile_bmp_header_raw['green_mask']) >> $greenshift) / ($thisfile_bmp_header_raw['green_mask'] >> $greenshift)) * 255); |
||
| 690 | $blue = (int)round(((($pixelvalue & $thisfile_bmp_header_raw['blue_mask']) >> $blueshift) / ($thisfile_bmp_header_raw['blue_mask'] >> $blueshift)) * 255); |
||
| 691 | $thisfile_bmp['data'][$row][$col] = (($red << 16) | ($green << 8) | $blue); |
||
| 692 | } |
||
| 693 | while (0 != ($pixeldataoffset % 4)) { |
||
| 694 | // lines are padded to nearest DWORD |
||
| 695 | $pixeldataoffset++; |
||
| 696 | } |
||
| 697 | } |
||
| 698 | break; |
||
| 699 | |||
| 700 | default: |
||
| 701 | $ThisFileInfo['error'][] = 'Unknown bits-per-pixel value (' . $thisfile_bmp_header_raw['bits_per_pixel'] . ') - cannot read pixel data'; |
||
| 702 | break; |
||
| 703 | } |
||
| 704 | break; |
||
| 705 | |||
| 706 | default: // unhandled compression type |
||
| 707 | $ThisFileInfo['error'][] = 'Unknown/unhandled compression type value (' . $thisfile_bmp_header_raw['compression'] . ') - cannot decompress pixel data'; |
||
| 708 | break; |
||
| 709 | } |
||
| 710 | } |
||
| 711 | |||
| 712 | return true; |
||
| 713 | } |
||
| 962 |