From 3811a2b83989b121a329554a568d442f350e4a41 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 18 Nov 2020 23:44:04 +1100 Subject: [PATCH] Have std/bmp decode BGRX --- release/c/wuffs-unsupported-snapshot.c | 307 ++++++++++++++----------- std/bmp/decode_bmp.wuffs | 91 +++++--- 2 files changed, 231 insertions(+), 167 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 9ace6d50..7583d3c6 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -16261,15 +16261,26 @@ wuffs_bmp__decoder__decode_image_config( } v_bitmap_info_len = t_2; } - if ((v_bitmap_info_len != 40) && (v_bitmap_info_len != 108) && (v_bitmap_info_len != 124)) { - status = wuffs_base__make_status(wuffs_bmp__error__unsupported_bmp_file); - goto exit; - } if (self->private_impl.f_padding < v_bitmap_info_len) { status = wuffs_base__make_status(wuffs_bmp__error__bad_header); goto exit; } self->private_impl.f_padding -= v_bitmap_info_len; + if (v_bitmap_info_len == 40) { + if (self->private_impl.f_padding >= 16) { + v_bitmap_info_len = 56; + self->private_impl.f_padding -= 16; + } else if (self->private_impl.f_padding >= 12) { + v_bitmap_info_len = 52; + self->private_impl.f_padding -= 12; + } + } else if ((v_bitmap_info_len != 52) && + (v_bitmap_info_len != 56) && + (v_bitmap_info_len != 108) && + (v_bitmap_info_len != 124)) { + status = wuffs_base__make_status(wuffs_bmp__error__unsupported_bmp_file); + goto exit; + } { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8); uint32_t t_3; @@ -16445,17 +16456,6 @@ wuffs_bmp__decoder__decode_image_config( } status = wuffs_base__make_status(wuffs_bmp__error__unsupported_bmp_file); goto exit; - } else if (v_bits_per_pixel == 24) { - self->private_impl.f_bytes_per_row = ((((((uint64_t)(self->private_impl.f_width)) * 3) + 3) >> 2) << 2); - self->private_impl.f_pad_per_row = (self->private_impl.f_width & 3); - self->private_impl.f_pixfmt = wuffs_base__utility__make_pixel_format(2147485832); - } else if (v_bits_per_pixel == 32) { - self->private_impl.f_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * 4); - self->private_impl.f_pad_per_row = 0; - self->private_impl.f_pixfmt = wuffs_base__utility__make_pixel_format(2164295816); - } else { - status = wuffs_base__make_status(wuffs_bmp__error__unsupported_bmp_file); - goto exit; } self->private_data.s_decode_image_config[0].scratch = 20; WUFFS_BASE__COROUTINE_SUSPENSION_POINT(18); @@ -16466,132 +16466,148 @@ wuffs_bmp__decoder__decode_image_config( goto suspend; } iop_a_src += self->private_data.s_decode_image_config[0].scratch; - if (v_bitmap_info_len >= 108) { - { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(19); - uint32_t t_8; - if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { - t_8 = wuffs_base__load_u32le__no_bounds_check(iop_a_src); - iop_a_src += 4; - } else { - self->private_data.s_decode_image_config[0].scratch = 0; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(20); - while (true) { - if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { - status = wuffs_base__make_status(wuffs_base__suspension__short_read); - goto suspend; + if ((v_compression == 3) || (v_compression == 6)) { + if (v_bitmap_info_len >= 52) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(19); + uint32_t t_8; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_8 = wuffs_base__load_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(20); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_8 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_8; + if (num_bits_8 == 24) { + t_8 = ((uint32_t)(*scratch)); + break; + } + num_bits_8 += 8; + *scratch |= ((uint64_t)(num_bits_8)) << 56; } - uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; - uint32_t num_bits_8 = ((uint32_t)(*scratch >> 56)); - *scratch <<= 8; - *scratch >>= 8; - *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_8; - if (num_bits_8 == 24) { - t_8 = ((uint32_t)(*scratch)); - break; + } + self->private_impl.f_mask_r = t_8; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(21); + uint32_t t_9; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_9 = wuffs_base__load_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(22); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_9 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_9; + if (num_bits_9 == 24) { + t_9 = ((uint32_t)(*scratch)); + break; + } + num_bits_9 += 8; + *scratch |= ((uint64_t)(num_bits_9)) << 56; + } + } + self->private_impl.f_mask_g = t_9; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(23); + uint32_t t_10; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_10 = wuffs_base__load_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(24); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_10 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_10; + if (num_bits_10 == 24) { + t_10 = ((uint32_t)(*scratch)); + break; + } + num_bits_10 += 8; + *scratch |= ((uint64_t)(num_bits_10)) << 56; + } + } + self->private_impl.f_mask_b = t_10; + } + if (v_bitmap_info_len >= 56) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(25); + uint32_t t_11; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_11 = wuffs_base__load_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(26); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_11 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_11; + if (num_bits_11 == 24) { + t_11 = ((uint32_t)(*scratch)); + break; + } + num_bits_11 += 8; + *scratch |= ((uint64_t)(num_bits_11)) << 56; + } + } + self->private_impl.f_mask_a = t_11; + } + self->private_data.s_decode_image_config[0].scratch = (v_bitmap_info_len - 56); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(27); + if (self->private_data.s_decode_image_config[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) { + self->private_data.s_decode_image_config[0].scratch -= ((uint64_t)(io2_a_src - iop_a_src)); + iop_a_src = io2_a_src; + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + iop_a_src += self->private_data.s_decode_image_config[0].scratch; + } + if ((self->private_impl.f_mask_r == 16711680) && (self->private_impl.f_mask_g == 65280) && (self->private_impl.f_mask_b == 255)) { + if (v_bits_per_pixel == 24) { + v_compression = 0; + } else if (v_bits_per_pixel == 32) { + if ((self->private_impl.f_mask_a == 0) || (self->private_impl.f_mask_a == 4278190080)) { + v_compression = 0; } - num_bits_8 += 8; - *scratch |= ((uint64_t)(num_bits_8)) << 56; } } - self->private_impl.f_mask_r = t_8; } - { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(21); - uint32_t t_9; - if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { - t_9 = wuffs_base__load_u32le__no_bounds_check(iop_a_src); - iop_a_src += 4; - } else { - self->private_data.s_decode_image_config[0].scratch = 0; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(22); - while (true) { - if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { - status = wuffs_base__make_status(wuffs_base__suspension__short_read); - goto suspend; - } - uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; - uint32_t num_bits_9 = ((uint32_t)(*scratch >> 56)); - *scratch <<= 8; - *scratch >>= 8; - *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_9; - if (num_bits_9 == 24) { - t_9 = ((uint32_t)(*scratch)); - break; - } - num_bits_9 += 8; - *scratch |= ((uint64_t)(num_bits_9)) << 56; - } - } - self->private_impl.f_mask_g = t_9; - } - { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(23); - uint32_t t_10; - if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { - t_10 = wuffs_base__load_u32le__no_bounds_check(iop_a_src); - iop_a_src += 4; - } else { - self->private_data.s_decode_image_config[0].scratch = 0; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(24); - while (true) { - if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { - status = wuffs_base__make_status(wuffs_base__suspension__short_read); - goto suspend; - } - uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; - uint32_t num_bits_10 = ((uint32_t)(*scratch >> 56)); - *scratch <<= 8; - *scratch >>= 8; - *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_10; - if (num_bits_10 == 24) { - t_10 = ((uint32_t)(*scratch)); - break; - } - num_bits_10 += 8; - *scratch |= ((uint64_t)(num_bits_10)) << 56; - } - } - self->private_impl.f_mask_b = t_10; - } - { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(25); - uint32_t t_11; - if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { - t_11 = wuffs_base__load_u32le__no_bounds_check(iop_a_src); - iop_a_src += 4; - } else { - self->private_data.s_decode_image_config[0].scratch = 0; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(26); - while (true) { - if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { - status = wuffs_base__make_status(wuffs_base__suspension__short_read); - goto suspend; - } - uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; - uint32_t num_bits_11 = ((uint32_t)(*scratch >> 56)); - *scratch <<= 8; - *scratch >>= 8; - *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_11; - if (num_bits_11 == 24) { - t_11 = ((uint32_t)(*scratch)); - break; - } - num_bits_11 += 8; - *scratch |= ((uint64_t)(num_bits_11)) << 56; - } - } - self->private_impl.f_mask_a = t_11; - } - if ((v_compression == 3) && - (self->private_impl.f_mask_r == 16711680) && - (self->private_impl.f_mask_g == 65280) && - (self->private_impl.f_mask_b == 255) && - (self->private_impl.f_mask_a == 4278190080)) { - v_compression = 0; - } - self->private_data.s_decode_image_config[0].scratch = (v_bitmap_info_len - 56); - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(27); + } else if (v_bitmap_info_len >= 40) { + self->private_data.s_decode_image_config[0].scratch = (v_bitmap_info_len - 40); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(28); if (self->private_data.s_decode_image_config[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) { self->private_data.s_decode_image_config[0].scratch -= ((uint64_t)(io2_a_src - iop_a_src)); iop_a_src = io2_a_src; @@ -16599,6 +16615,25 @@ wuffs_bmp__decoder__decode_image_config( goto suspend; } iop_a_src += self->private_data.s_decode_image_config[0].scratch; + } else { + status = wuffs_base__make_status(wuffs_bmp__error__unsupported_bmp_file); + goto exit; + } + if (v_bits_per_pixel == 24) { + self->private_impl.f_bytes_per_row = ((((((uint64_t)(self->private_impl.f_width)) * 3) + 3) >> 2) << 2); + self->private_impl.f_pad_per_row = (self->private_impl.f_width & 3); + self->private_impl.f_pixfmt = wuffs_base__utility__make_pixel_format(2147485832); + } else if (v_bits_per_pixel == 32) { + self->private_impl.f_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * 4); + self->private_impl.f_pad_per_row = 0; + if (self->private_impl.f_mask_a == 0) { + self->private_impl.f_pixfmt = wuffs_base__utility__make_pixel_format(2415954056); + } else { + self->private_impl.f_pixfmt = wuffs_base__utility__make_pixel_format(2164295816); + } + } else { + status = wuffs_base__make_status(wuffs_bmp__error__unsupported_bmp_file); + goto exit; } if (v_compression != 0) { status = wuffs_base__make_status(wuffs_bmp__error__unsupported_bmp_file); diff --git a/std/bmp/decode_bmp.wuffs b/std/bmp/decode_bmp.wuffs index 589679e5..26e6a9d7 100644 --- a/std/bmp/decode_bmp.wuffs +++ b/std/bmp/decode_bmp.wuffs @@ -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"