| 5443 | } |
| 5444 | |
| 5445 | static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) |
| 5446 | { |
| 5447 | int hsz; |
| 5448 | if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP"); |
| 5449 | stbi__get32le(s); // discard filesize |
| 5450 | stbi__get16le(s); // discard reserved |
| 5451 | stbi__get16le(s); // discard reserved |
| 5452 | info->offset = stbi__get32le(s); |
| 5453 | info->hsz = hsz = stbi__get32le(s); |
| 5454 | info->mr = info->mg = info->mb = info->ma = 0; |
| 5455 | info->extra_read = 14; |
| 5456 | |
| 5457 | if (info->offset < 0) return stbi__errpuc("bad BMP", "bad BMP"); |
| 5458 | |
| 5459 | if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown"); |
| 5460 | if (hsz == 12) { |
| 5461 | s->img_x = stbi__get16le(s); |
| 5462 | s->img_y = stbi__get16le(s); |
| 5463 | } else { |
| 5464 | s->img_x = stbi__get32le(s); |
| 5465 | s->img_y = stbi__get32le(s); |
| 5466 | } |
| 5467 | if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP"); |
| 5468 | info->bpp = stbi__get16le(s); |
| 5469 | if (hsz != 12) { |
| 5470 | int compress = stbi__get32le(s); |
| 5471 | if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE"); |
| 5472 | if (compress >= 4) return stbi__errpuc("BMP JPEG/PNG", "BMP type not supported: unsupported compression"); // this includes PNG/JPEG modes |
| 5473 | if (compress == 3 && info->bpp != 16 && info->bpp != 32) return stbi__errpuc("bad BMP", "bad BMP"); // bitfields requires 16 or 32 bits/pixel |
| 5474 | stbi__get32le(s); // discard sizeof |
| 5475 | stbi__get32le(s); // discard hres |
| 5476 | stbi__get32le(s); // discard vres |
| 5477 | stbi__get32le(s); // discard colorsused |
| 5478 | stbi__get32le(s); // discard max important |
| 5479 | if (hsz == 40 || hsz == 56) { |
| 5480 | if (hsz == 56) { |
| 5481 | stbi__get32le(s); |
| 5482 | stbi__get32le(s); |
| 5483 | stbi__get32le(s); |
| 5484 | stbi__get32le(s); |
| 5485 | } |
| 5486 | if (info->bpp == 16 || info->bpp == 32) { |
| 5487 | if (compress == 0) { |
| 5488 | stbi__bmp_set_mask_defaults(info, compress); |
| 5489 | } else if (compress == 3) { |
| 5490 | info->mr = stbi__get32le(s); |
| 5491 | info->mg = stbi__get32le(s); |
| 5492 | info->mb = stbi__get32le(s); |
| 5493 | info->extra_read += 12; |
| 5494 | // not documented, but generated by photoshop and handled by mspaint |
| 5495 | if (info->mr == info->mg && info->mg == info->mb) { |
| 5496 | // ?!?!? |
| 5497 | return stbi__errpuc("bad BMP", "bad BMP"); |
| 5498 | } |
| 5499 | } else |
| 5500 | return stbi__errpuc("bad BMP", "bad BMP"); |
| 5501 | } |
| 5502 | } else { |
no test coverage detected