mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 00:38:14 +01:00
Have std/bmp decode BITFIELDS compression
This commit is contained in:
1 parent
cda0db2f0c
commit
960b5df3c4
2 files changed
+529
-82
No files matched your search
+244
-37
@@ -28,7 +28,7 @@ pub struct decoder? implements base.image_decoder(
|
||||
top_down : base.bool,
|
||||
pad_per_row : base.u32[..= 3],
|
||||
bytes_per_row : base.u64[..= 0x0000_0001_FFFF_FFFC], // 4 * 0x7FFF_FFFF
|
||||
pixfmt : base.pixel_format,
|
||||
src_pixfmt : base.u32,
|
||||
|
||||
io_redirect_fourcc : base.u32,
|
||||
io_redirect_pos : base.u64,
|
||||
@@ -37,9 +37,14 @@ pub struct decoder? implements base.image_decoder(
|
||||
|
||||
padding : base.u32,
|
||||
|
||||
bits_per_pixel : base.u32,
|
||||
compression : base.u32,
|
||||
|
||||
// channel_etc's indexes are: B, G, R, A. This is in Wuffs' default order,
|
||||
// which isn't the RGBA order they're listed in the wire format.
|
||||
channel_masks : array[4] base.u32,
|
||||
channel_masks : array[4] base.u32,
|
||||
channel_shifts : array[4] base.u8[..= 31],
|
||||
channel_num_bits : array[4] base.u8[..= 32],
|
||||
|
||||
dst_x : base.u32,
|
||||
dst_y : base.u32,
|
||||
@@ -50,6 +55,8 @@ pub struct decoder? implements base.image_decoder(
|
||||
|
||||
swizzler : base.pixel_swizzler,
|
||||
util : base.utility,
|
||||
)(
|
||||
scratch : array[2048] base.u8, // 2048 = 256 * (8 bytes per BGRA_NONPREMUL_4X16LE).
|
||||
)
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
@@ -61,8 +68,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
|
||||
var width : base.u32
|
||||
var height : base.u32
|
||||
var planes : base.u32
|
||||
var bits_per_pixel : base.u32
|
||||
var compression : base.u32
|
||||
var dst_pixfmt : base.u32
|
||||
|
||||
if (this.call_sequence <> 0) or (this.io_redirect_fourcc == 1) {
|
||||
return base."#bad call sequence"
|
||||
@@ -132,13 +138,13 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
bits_per_pixel = args.src.read_u16le_as_u32?()
|
||||
compression = args.src.read_u32le?()
|
||||
if bits_per_pixel == 0 {
|
||||
if compression == 4 {
|
||||
this.bits_per_pixel = args.src.read_u16le_as_u32?()
|
||||
this.compression = args.src.read_u32le?()
|
||||
if this.bits_per_pixel == 0 {
|
||||
if this.compression == 4 {
|
||||
this.io_redirect_fourcc = 'JPEG'be
|
||||
return base."@I/O redirect"
|
||||
} else if compression == 5 {
|
||||
} else if this.compression == 5 {
|
||||
this.io_redirect_fourcc = 'PNG 'be
|
||||
return base."@I/O redirect"
|
||||
}
|
||||
@@ -150,9 +156,13 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
|
||||
// the version 3 BITMAPINFOHEADER (whose total size is 40).
|
||||
args.src.skip_u32?(n: 40 - 20)
|
||||
|
||||
// Read the channel_masks when compression is 3 (BITFIELDS) or 6
|
||||
// (ALPHABITFIELDS).
|
||||
if (compression == 3) or (compression == 6) {
|
||||
// Treat 6 (ALPHABITFIELDS) the same as 3 (BITFIELDS).
|
||||
if this.compression == 6 {
|
||||
this.compression = 3
|
||||
}
|
||||
|
||||
// Read the channel_masks when this.compression is 3 (BITFIELDS).
|
||||
if this.compression == 3 {
|
||||
if bitmap_info_len >= 52 {
|
||||
this.channel_masks[2] = args.src.read_u32le?()
|
||||
this.channel_masks[1] = args.src.read_u32le?()
|
||||
@@ -167,15 +177,16 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
|
||||
if (this.channel_masks[0] == 0x0000_00FF) and
|
||||
(this.channel_masks[1] == 0x0000_FF00) and
|
||||
(this.channel_masks[2] == 0x00FF_0000) {
|
||||
if bits_per_pixel == 24 {
|
||||
compression = 0
|
||||
} else if bits_per_pixel == 32 {
|
||||
if this.bits_per_pixel == 24 {
|
||||
this.compression = 0
|
||||
} else if this.bits_per_pixel == 32 {
|
||||
if (this.channel_masks[3] == 0) or
|
||||
(this.channel_masks[3] == 0xFF00_0000) {
|
||||
compression = 0
|
||||
this.compression = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
this.process_masks?()
|
||||
}
|
||||
} else if bitmap_info_len >= 40 {
|
||||
// Skip the rest of the BITMAPINFOHEADER.
|
||||
@@ -184,40 +195,69 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
|
||||
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
|
||||
this.pad_per_row = this.width & 3
|
||||
this.pixfmt = this.util.make_pixel_format(repr: base.PIXEL_FORMAT__BGR)
|
||||
} else if bits_per_pixel == 32 {
|
||||
this.bytes_per_row = (this.width as base.u64) * 4
|
||||
this.pad_per_row = 0
|
||||
if this.channel_masks[3] == 0 {
|
||||
this.pixfmt = this.util.make_pixel_format(repr: base.PIXEL_FORMAT__BGRX)
|
||||
if this.compression == 0 { // 0 means no compression.
|
||||
if this.bits_per_pixel == 16 {
|
||||
// Implement BMP's 16-bit default (BGRX_5551) as BITFIELDS.
|
||||
this.compression = 3
|
||||
this.channel_masks[0] = 0x001F
|
||||
this.channel_masks[1] = 0x03E0
|
||||
this.channel_masks[2] = 0x7C00
|
||||
this.channel_masks[3] = 0x0000
|
||||
this.process_masks?()
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
} else if this.bits_per_pixel == 24 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGR
|
||||
} else if this.bits_per_pixel == 32 {
|
||||
if this.channel_masks[3] == 0 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRX
|
||||
} else {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
}
|
||||
} else {
|
||||
this.pixfmt = this.util.make_pixel_format(repr: base.PIXEL_FORMAT__BGRA_NONPREMUL)
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
} else if this.compression == 3 { // 3 means BITFIELDS.
|
||||
if (this.bits_per_pixel == 16) or (this.bits_per_pixel == 32) {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
} else {
|
||||
// TODO: support other bits_per_pixel's.
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
if compression <> 0 {
|
||||
// TODO: support compression.
|
||||
return "#unsupported BMP file"
|
||||
// The "((x + 3) >> 2) << 2" dance rounds x up to a multiple of 4.
|
||||
if this.bits_per_pixel == 16 {
|
||||
this.bytes_per_row = ((((this.width as base.u64) * 2) + 3) >> 2) << 2
|
||||
this.pad_per_row = (this.width & 1) * 2
|
||||
} else if this.bits_per_pixel == 24 {
|
||||
this.bytes_per_row = ((((this.width as base.u64) * 3) + 3) >> 2) << 2
|
||||
this.pad_per_row = this.width & 3
|
||||
} else if this.bits_per_pixel == 32 {
|
||||
this.bytes_per_row = (this.width as base.u64) * 4
|
||||
this.pad_per_row = 0
|
||||
}
|
||||
|
||||
this.frame_config_io_position = args.src.position()
|
||||
|
||||
if args.dst <> nullptr {
|
||||
dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
if (this.channel_num_bits[0] > 8) or
|
||||
(this.channel_num_bits[1] > 8) or
|
||||
(this.channel_num_bits[2] > 8) or
|
||||
(this.channel_num_bits[3] > 8) {
|
||||
dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
}
|
||||
|
||||
args.dst.set!(
|
||||
pixfmt: base.PIXEL_FORMAT__BGRA_NONPREMUL, // TODO: this.pixfmt instead?
|
||||
pixfmt: dst_pixfmt,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.frame_config_io_position,
|
||||
first_frame_is_opaque: true)
|
||||
first_frame_is_opaque: this.channel_masks[3] == 0)
|
||||
}
|
||||
|
||||
this.call_sequence = 1
|
||||
@@ -283,7 +323,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette(),
|
||||
src_pixfmt: this.pixfmt,
|
||||
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
|
||||
src_palette: this.util.empty_slice_u8(),
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
@@ -291,7 +331,12 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
|
||||
}
|
||||
|
||||
while true {
|
||||
status = this.swizzle!(dst: args.dst, src: args.src)
|
||||
if this.compression == 0 {
|
||||
status = this.swizzle_compress0!(dst: args.dst, src: args.src)
|
||||
} else {
|
||||
status = this.swizzle_compress3!(dst: args.dst, src: args.src)
|
||||
}
|
||||
|
||||
if status.is_ok() {
|
||||
break
|
||||
} else if status <> "@internal note: short read" {
|
||||
@@ -304,7 +349,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
|
||||
this.call_sequence = 3
|
||||
}
|
||||
|
||||
pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status {
|
||||
pri func decoder.swizzle_compress0!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status {
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
@@ -370,6 +415,132 @@ pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: base.io_reader) base.
|
||||
return ok
|
||||
}
|
||||
|
||||
pri func decoder.swizzle_compress3!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status {
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row : base.u64
|
||||
var tab : table base.u8
|
||||
var dst : slice base.u8
|
||||
var i : base.u64
|
||||
var n : base.u64
|
||||
|
||||
var p0 : base.u32[..= 256]
|
||||
var p1 : base.u32[..= 256]
|
||||
var p1_temp : base.u32
|
||||
|
||||
var num_bits : base.u32[..= 32]
|
||||
var c : base.u32
|
||||
var c32 : base.u32
|
||||
var channel : base.u32[..= 4]
|
||||
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
|
||||
tab = args.dst.plane(p: 0)
|
||||
|
||||
while.outer true {
|
||||
while this.pending_pad > 0 {
|
||||
if args.src.length() <= 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.pending_pad -= 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} endwhile
|
||||
|
||||
while.inner true {
|
||||
if this.dst_x == this.width {
|
||||
this.dst_x = 0
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
if this.dst_y == this.dst_y_end {
|
||||
break.outer
|
||||
} else if this.pad_per_row <> 0 {
|
||||
this.pending_pad = this.pad_per_row
|
||||
continue.outer
|
||||
}
|
||||
}
|
||||
|
||||
// -------- BEGIN convert to PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE.
|
||||
p1_temp = this.width ~mod- this.dst_x
|
||||
p1 = p1_temp.min(a: 256)
|
||||
p0 = 0
|
||||
while (p0 < p1) and (args.src.length() >= 2) {
|
||||
assert p0 < 256 via "a < b: a < c; c <= b"(c: p1)
|
||||
if this.bits_per_pixel == 16 {
|
||||
if args.src.length() < 2 {
|
||||
break
|
||||
}
|
||||
c32 = args.src.peek_u16le_as_u32()
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
} else {
|
||||
if args.src.length() < 4 {
|
||||
break
|
||||
}
|
||||
c32 = args.src.peek_u32le()
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
}
|
||||
|
||||
channel = 0
|
||||
while channel < 4,
|
||||
inv p0 < 256,
|
||||
{
|
||||
if this.channel_num_bits[channel] == 0 {
|
||||
this.scratch[(8 * p0) + (2 * channel) + 0] = 0xFF
|
||||
this.scratch[(8 * p0) + (2 * channel) + 1] = 0xFF
|
||||
} else {
|
||||
c = (c32 & this.channel_masks[channel]) >> this.channel_shifts[channel]
|
||||
num_bits = this.channel_num_bits[channel] as base.u32
|
||||
while num_bits < 16,
|
||||
inv p0 < 256,
|
||||
inv channel < 4,
|
||||
post num_bits >= 16,
|
||||
{
|
||||
c |= c ~mod<< num_bits
|
||||
num_bits *= 2
|
||||
} endwhile
|
||||
c >>= num_bits - 16
|
||||
this.scratch[(8 * p0) + (2 * channel) + 0] = (0xFF & (c >> 0)) as base.u8
|
||||
this.scratch[(8 * p0) + (2 * channel) + 1] = (0xFF & (c >> 8)) as base.u8
|
||||
}
|
||||
|
||||
channel += 1
|
||||
} endwhile
|
||||
|
||||
p0 += 1
|
||||
} endwhile
|
||||
// -------- END convert to PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE.
|
||||
|
||||
dst = tab.row(y: this.dst_y)
|
||||
if dst_bytes_per_row < dst.length() {
|
||||
dst = dst[.. dst_bytes_per_row]
|
||||
}
|
||||
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if i >= dst.length() {
|
||||
// TODO: advance args.src if the dst pixel_buffer bounds is
|
||||
// smaller than this BMP's image bounds?
|
||||
continue.inner
|
||||
}
|
||||
|
||||
n = this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: this.util.empty_slice_u8(),
|
||||
src: this.scratch[.. 8 * p0])
|
||||
if n == 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
} endwhile.inner
|
||||
} endwhile.outer
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
pri func decoder.skip_frame?(src: base.io_reader) {
|
||||
args.src.skip_u32?(n: this.padding)
|
||||
args.src.skip?(n: this.bytes_per_row * (this.height as base.u64))
|
||||
@@ -439,3 +610,39 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
|
||||
}
|
||||
|
||||
pri func decoder.process_masks?() {
|
||||
var i : base.u32
|
||||
var mask : base.u32
|
||||
var n : base.u32
|
||||
|
||||
while i < 4 {
|
||||
mask = this.channel_masks[i]
|
||||
if mask <> 0 {
|
||||
n = 0
|
||||
while (mask & 1) == 0,
|
||||
inv i < 4,
|
||||
{
|
||||
n ~mod+= 1
|
||||
mask >>= 1
|
||||
} endwhile
|
||||
this.channel_shifts[i] = (n & 31) as base.u8
|
||||
|
||||
n = 0
|
||||
while (mask & 1) == 1,
|
||||
inv i < 4,
|
||||
{
|
||||
n ~mod+= 1
|
||||
mask >>= 1
|
||||
} endwhile
|
||||
if (mask <> 0) or (n > 32) {
|
||||
return "#bad header"
|
||||
}
|
||||
this.channel_num_bits[i] = n as base.u8
|
||||
} else if i <> 3 {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
Reference in new issue
Block a user