std/vp8: decode VP8X ALPH chunk

This commit is contained in:
Nigel Tao committed 2026-07-29 15:58:52 +10:00
1 parent 2a97911e02
commit e7f6c79752
14 files changed
+1246 -154

No files matched your search

+92 -26
View File
@@ -133,10 +133,18 @@ pub struct decoder? implements base.image_decoder(
// ICCP_FLAG = 0x00000020,
vp8x_header_flags : base.u8,
// The "Rsv| P | F | C" bits from
// https://developers.google.com/speed/webp/docs/riff_container#alpha
//
// We also re-purpose the otherwise-unused 0x02 bit to mean that we
// have seen the ALPH chunk.
vp8x_alph_flags : base.u8,
frame_config_io_position : base.u64,
riff_chunk_length : base.u32,
sub_chunk_length : base.u32,
sub_chunk_type : base.u32,
bits : base.u32,
n_bits : base.u32[..= 31],
@@ -323,9 +331,6 @@ pri func decoder.do_decode_image_config_limited?(dst: nptr base.image_config, sr
}
this.sub_chunk_length = args.src.read_u32le?()
if this.sub_chunk_length < 4 {
return "#bad header"
}
this.sub_chunk_has_padding = (this.sub_chunk_length & 1) <> 0
if this.variant == 0x01 {
@@ -513,8 +518,8 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
}
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
var chunk_type : base.u32
var chunk_length : base.u32
var r_mark : base.u64
var status : base.status
if this.call_sequence == 0x40 {
// No-op.
@@ -525,10 +530,14 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
}
while (this.variant & 0x03) == 0 {
chunk_type = args.src.read_u32le?()
chunk_length = args.src.read_u32le?()
this.sub_chunk_type = args.src.read_u32le?()
this.sub_chunk_length = args.src.read_u32le?()
this.sub_chunk_has_padding = (this.sub_chunk_length & 1) <> 0
if this.sub_chunk_length > 0x7FFF_FFFF {
return "#unsupported WebP file"
}
if chunk_type == 'ICCP'le {
if this.sub_chunk_type == 'ICCP'le {
if this.variant > 0x04 {
return "#bad chunk order"
} else if (this.vp8x_header_flags & 0x20) == 0 {
@@ -536,7 +545,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
}
this.variant = 0x08
} else if chunk_type == 'ANIM'le {
} else if this.sub_chunk_type == 'ANIM'le {
if this.variant > 0x08 {
return "#bad chunk order"
} else if (this.vp8x_header_flags & 0x02) == 0 {
@@ -544,7 +553,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
}
this.variant = 0x10
} else if chunk_type == 'ANMF'le {
} else if this.sub_chunk_type == 'ANMF'le {
if this.variant > 0x10 {
return "#bad chunk order"
} else if (this.vp8x_header_flags & 0x02) == 0 {
@@ -552,54 +561,111 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
}
this.variant = 0x20
} else if chunk_type == 'ALPH'le {
} else if this.sub_chunk_type == 'ALPH'le {
if this.variant > 0x20 {
return "#bad chunk order"
} else if (this.vp8x_header_flags & 0x10) == 0 {
return "#bad VP8X chunk"
}
this.variant = 0x40
this.decode_alph?(src: args.src, workbuf: args.workbuf)
this.vp8.set_quirk!(
key: vp8.QUIRK_HAS_ALPHA_SIDE_CHANNEL,
value: 1)
} else if chunk_type == 'VP8 'le {
if (chunk_length as base.u64) > VP8X_MAX_INCL_VP8_CHUNK_LENGTH_DEFAULT {
} else if this.sub_chunk_type == 'VP8 'le {
if (this.sub_chunk_length as base.u64) > VP8X_MAX_INCL_VP8_CHUNK_LENGTH_DEFAULT {
return "#unsupported VP8X/VP8 chunk length"
}
this.vp8.set_quirk!(
key: base.QUIRK_SOURCE_LENGTH,
value: ((chunk_length as base.u64) << 1) | 1)
value: ((this.sub_chunk_length as base.u64) << 1) | 1)
this.variant = 0x81
break
} else if chunk_type == 'VP8L'le {
} else if this.sub_chunk_type == 'VP8L'le {
if (this.vp8x_header_flags & 0x10) <> 0 {
return "#bad ALPH+VP8L combination"
}
this.variant = 0x82
break
} else if chunk_type == 'XMP 'le {
} else if this.sub_chunk_type == 'XMP 'le {
if (this.vp8x_header_flags & 0x04) == 0 {
return "#bad VP8X chunk"
}
} else if chunk_type == 'EXIF'le {
} else if this.sub_chunk_type == 'EXIF'le {
if (this.vp8x_header_flags & 0x08) == 0 {
return "#bad VP8X chunk"
}
}
if chunk_length > 0x7FFF_FFFF {
return "#unsupported WebP file"
} else if (chunk_length & 1) <> 0 {
chunk_length += 1
args.src.skip_u32?(n: this.sub_chunk_length)
if this.sub_chunk_has_padding {
args.src.skip_u32?(n: 1)
}
args.src.skip_u32?(n: chunk_length)
}
if (this.variant & 0x01) <> 0 {
this.vp8.decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
} else {
this.do_decode_frame_vp8l?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
while true {
io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) {
r_mark = args.src.mark()
if (this.variant & 0x01) <> 0 {
status =? this.vp8.decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
} else {
status =? this.do_decode_frame_vp8l?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
}
this.sub_chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
if status.is_ok() {
break
} else if not status.is_suspension() {
return status
} else if (status == base."$short read") and (this.sub_chunk_length == 0) {
return "#short chunk"
}
yield? status
}
if this.vp8x_alph_flags <> 0 {
this.apply_alph_chunk!(dst: args.dst, workbuf: args.workbuf)
}
}
// TODO: don't assume PIXEL_BLEND__SRC and PIXEL_FORMAT__BGRA_NONPREMUL.
pri func decoder.apply_alph_chunk!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) {
var y : base.u32
var x : base.u32
var tab : table base.u8
var row : slice base.u8
var d : base.u64
var s : base.u64
tab = args.dst.plane(p: 0)
y = 0
while y < this.height {
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
row = tab.row_u32(y: y)
x = 0
while x < this.width,
inv y < 0x4000,
{
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
d = ((x * 4) + 3) as base.u64
s = ((y * this.width) + x) as base.u64
if (d >= row.length()) or (s >= args.workbuf.length()) {
break
}
row[d] = args.workbuf[s]
x += 1
}
y += 1
}
}