mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 08:47:28 +01:00
Have std/bmp decode BGRX
This commit is contained in:
1 parent
ac930b801b
commit
3811a2b839
2 files changed
+231
-167
No files matched your search
+60
-31
@@ -90,14 +90,26 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
|
||||
// Read the BITMAPINFOHEADER (version 3 / 4 / 5 is 40 / 108 / 124 bytes).
|
||||
|
||||
bitmap_info_len = args.src.read_u32le?()
|
||||
if (bitmap_info_len <> 40) and (bitmap_info_len <> 108) and (bitmap_info_len <> 124) {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
if this.padding < bitmap_info_len {
|
||||
return "#bad header"
|
||||
}
|
||||
this.padding -= bitmap_info_len
|
||||
|
||||
if bitmap_info_len == 40 {
|
||||
// It's poorly documented, but some "length 40" data can be silently
|
||||
// augmented with RGB masks (12 bytes) or RGBA masks (16 bytes).
|
||||
if this.padding >= 16 {
|
||||
bitmap_info_len = 56
|
||||
this.padding -= 16
|
||||
} else if this.padding >= 12 {
|
||||
bitmap_info_len = 52
|
||||
this.padding -= 12
|
||||
}
|
||||
} else if (bitmap_info_len <> 52) and (bitmap_info_len <> 56) and
|
||||
(bitmap_info_len <> 108) and (bitmap_info_len <> 124) {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
width = args.src.read_u32le?()
|
||||
if width >= 0x8000_0000 {
|
||||
return "#bad header"
|
||||
@@ -132,7 +144,46 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
|
||||
return base."@I/O redirect"
|
||||
}
|
||||
return "#unsupported BMP file"
|
||||
} else if bits_per_pixel == 24 {
|
||||
}
|
||||
|
||||
// We've already read 20 bytes from the BITMAPINFOHEADER: size (4), width
|
||||
// (4), height (4), planes (2), bpp (2), compression (4). Skip the rest of
|
||||
// the version 3 BITMAPINFOHEADER (whose total size is 40).
|
||||
args.src.skip_u32?(n: 40 - 20)
|
||||
|
||||
// Read the masks when compression is 3 (BITFIELDS) or 6 (ALPHABITFIELDS).
|
||||
if (compression == 3) or (compression == 6) {
|
||||
if bitmap_info_len >= 52 {
|
||||
this.mask_r = args.src.read_u32le?()
|
||||
this.mask_g = args.src.read_u32le?()
|
||||
this.mask_b = args.src.read_u32le?()
|
||||
if bitmap_info_len >= 56 {
|
||||
this.mask_a = args.src.read_u32le?()
|
||||
// Skip the rest of the BITMAPINFOHEADER.
|
||||
args.src.skip_u32?(n: bitmap_info_len - 56)
|
||||
}
|
||||
// If the explicit masks are what the implicit masks would be for
|
||||
// no compression, treat it as no compression.
|
||||
if (this.mask_r == 0x00FF_0000) and
|
||||
(this.mask_g == 0x0000_FF00) and
|
||||
(this.mask_b == 0x0000_00FF) {
|
||||
if bits_per_pixel == 24 {
|
||||
compression = 0
|
||||
} else if bits_per_pixel == 32 {
|
||||
if (this.mask_a == 0) or (this.mask_a == 0xFF00_0000) {
|
||||
compression = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if bitmap_info_len >= 40 {
|
||||
// Skip the rest of the BITMAPINFOHEADER.
|
||||
args.src.skip_u32?(n: bitmap_info_len - 40)
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
if bits_per_pixel == 24 {
|
||||
// 3 bytes per pixel, but row lengths are rounded up to multiples of 4.
|
||||
// The "((x + 3) >> 2) << 2" dance rounds x up.
|
||||
this.bytes_per_row = ((((this.width as base.u64) * 3) + 3) >> 2) << 2
|
||||
@@ -141,38 +192,16 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
|
||||
} else if bits_per_pixel == 32 {
|
||||
this.bytes_per_row = (this.width as base.u64) * 4
|
||||
this.pad_per_row = 0
|
||||
this.pixfmt = this.util.make_pixel_format(repr: base.PIXEL_FORMAT__BGRA_NONPREMUL)
|
||||
if this.mask_a == 0 {
|
||||
this.pixfmt = this.util.make_pixel_format(repr: base.PIXEL_FORMAT__BGRX)
|
||||
} else {
|
||||
this.pixfmt = this.util.make_pixel_format(repr: base.PIXEL_FORMAT__BGRA_NONPREMUL)
|
||||
}
|
||||
} else {
|
||||
// TODO: support other bits_per_pixel's.
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
// We've already read 20 bytes from the BITMAPINFOHEADER: size (4), width
|
||||
// (4), height (4), planes (2), bpp (2), compression (4). Skip the rest of
|
||||
// the version 3 BITMAPINFOHEADER (whose total size is 40).
|
||||
args.src.skip_u32?(n: 40 - 20)
|
||||
|
||||
if bitmap_info_len >= 108 {
|
||||
this.mask_r = args.src.read_u32le?()
|
||||
this.mask_g = args.src.read_u32le?()
|
||||
this.mask_b = args.src.read_u32le?()
|
||||
this.mask_a = args.src.read_u32le?()
|
||||
|
||||
// If compression is 3 (BITFIELDS) but the explicit masks are what the
|
||||
// implicit masks are for no compression, treat it as no compression.
|
||||
if (compression == 3) and
|
||||
(this.mask_r == 0x00FF_0000) and
|
||||
(this.mask_g == 0x0000_FF00) and
|
||||
(this.mask_b == 0x0000_00FF) and
|
||||
(this.mask_a == 0xFF00_0000) {
|
||||
compression = 0
|
||||
}
|
||||
|
||||
// Skip the rest of the BITMAPINFOHEADER. We've already read (40 + (4 *
|
||||
// 4)) bytes.
|
||||
args.src.skip_u32?(n: bitmap_info_len - 56)
|
||||
}
|
||||
|
||||
if compression <> 0 {
|
||||
// TODO: support compression.
|
||||
return "#unsupported BMP file"
|
||||
|
||||
Reference in new issue
Block a user