mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 00:47:28 +01:00
std/vp8: decode VP8X ALPH chunk
This commit is contained in:
1 parent
2a97911e02
commit
e7f6c79752
14 files changed
+1246
-154
No files matched your search
@@ -0,0 +1,242 @@
|
||||
// Copyright 2026 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
pri func decoder.decode_alph?(src: base.io_reader, workbuf: slice base.u8) {
|
||||
var r_mark : base.u64
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) {
|
||||
r_mark = args.src.mark()
|
||||
status =? this.decode_alph_limited?(src: args.src, workbuf: args.workbuf)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.decode_alph_limited?(src: base.io_reader, workbuf: slice base.u8) {
|
||||
var n : base.u64
|
||||
|
||||
this.vp8x_alph_flags = args.src.read_u8?()
|
||||
|
||||
if (this.vp8x_alph_flags & 3) == 0 {
|
||||
args.src.limited_copy_u32_to_slice!(up_to: this.width * this.height, s: args.workbuf)
|
||||
} else if (this.vp8x_alph_flags & 3) == 1 {
|
||||
this.do_decode_frame_vp8l?(
|
||||
dst: nullptr,
|
||||
src: args.src,
|
||||
blend: this.util.make_pixel_blend(repr: base.PIXEL_BLEND__SRC),
|
||||
workbuf: args.workbuf,
|
||||
opts: nullptr)
|
||||
|
||||
n = (this.width * this.height * 4) as base.u64
|
||||
if n <= args.workbuf.length() {
|
||||
this.compact_alph!(s: args.workbuf[.. n])
|
||||
}
|
||||
} else {
|
||||
return "#bad VP8X chunk"
|
||||
}
|
||||
this.vp8x_alph_flags |= 2
|
||||
|
||||
n = (this.width * this.height) as base.u64
|
||||
if n <= args.workbuf.length() {
|
||||
if (this.vp8x_alph_flags & 0x0C) == 0x04 {
|
||||
this.filter_alph_horizontal!(s: args.workbuf[.. n])
|
||||
} else if (this.vp8x_alph_flags & 0x0C) == 0x08 {
|
||||
this.filter_alph_vertical!(s: args.workbuf[.. n])
|
||||
} else if (this.vp8x_alph_flags & 0x0C) == 0x0C {
|
||||
this.filter_alph_gradient!(s: args.workbuf[.. n])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.compact_alph!(s: slice base.u8) {
|
||||
var i : base.u64[..= 0x4000_0004]
|
||||
var j : base.u64[..= 0x4000_0004]
|
||||
|
||||
if args.s.length() > 0x4000_0000 {
|
||||
return nothing
|
||||
}
|
||||
|
||||
i = 0
|
||||
j = 1
|
||||
while (i < args.s.length()) and (j < args.s.length()),
|
||||
inv args.s.length() <= 0x4000_0000,
|
||||
{
|
||||
assert i <= 0x4000_0000 via "a <= b: a <= c; c <= b"(c: args.s.length())
|
||||
assert j <= 0x4000_0000 via "a <= b: a <= c; c <= b"(c: args.s.length())
|
||||
args.s[i] = args.s[j]
|
||||
i += 1
|
||||
j += 4
|
||||
}
|
||||
}
|
||||
|
||||
// libwebp calls this HorizontalUnfilter_C.
|
||||
pri func decoder.filter_alph_horizontal!(s: slice base.u8) {
|
||||
var y : base.u32
|
||||
var x : base.u32
|
||||
var i : base.u64
|
||||
var pred : base.u8
|
||||
|
||||
y = 0
|
||||
while y < this.height {
|
||||
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
|
||||
|
||||
pred = 0
|
||||
if y > 0 {
|
||||
i = ((y - 1) * this.width) as base.u64
|
||||
if i >= args.s.length() {
|
||||
break
|
||||
}
|
||||
pred = args.s[i]
|
||||
}
|
||||
|
||||
x = 0
|
||||
while x < this.width,
|
||||
inv y < 0x4000,
|
||||
{
|
||||
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
|
||||
|
||||
i = ((y * this.width) + x) as base.u64
|
||||
if i >= args.s.length() {
|
||||
break
|
||||
}
|
||||
args.s[i] ~mod+= pred
|
||||
pred = args.s[i]
|
||||
|
||||
x += 1
|
||||
}
|
||||
y += 1
|
||||
}
|
||||
}
|
||||
|
||||
// libwebp calls this VerticalUnfilter_C.
|
||||
pri func decoder.filter_alph_vertical!(s: slice base.u8) {
|
||||
var y : base.u32
|
||||
var x : base.u32
|
||||
var i : base.u64
|
||||
var pred : base.u8
|
||||
|
||||
x = 0
|
||||
while x < this.width {
|
||||
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
|
||||
|
||||
pred = 0
|
||||
if x > 0 {
|
||||
i = (x - 1) as base.u64
|
||||
if i >= args.s.length() {
|
||||
break
|
||||
}
|
||||
pred = args.s[i]
|
||||
}
|
||||
|
||||
y = 0
|
||||
while y < this.height,
|
||||
inv x < 0x4000,
|
||||
{
|
||||
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
|
||||
|
||||
i = ((y * this.width) + x) as base.u64
|
||||
if i >= args.s.length() {
|
||||
break
|
||||
}
|
||||
args.s[i] ~mod+= pred
|
||||
pred = args.s[i]
|
||||
|
||||
y += 1
|
||||
}
|
||||
x += 1
|
||||
}
|
||||
}
|
||||
|
||||
// libwebp calls this GradientUnfilter_C.
|
||||
pri func decoder.filter_alph_gradient!(s: slice base.u8) {
|
||||
var y : base.u32
|
||||
var x : base.u32
|
||||
var i : base.u64
|
||||
var pred : base.u8
|
||||
var grad : base.u32
|
||||
var l : base.u8 // Left sample.
|
||||
var t : base.u8 // Top sample.
|
||||
var tl : base.u8 // Top-Left sample.
|
||||
|
||||
// First row.
|
||||
x = 0
|
||||
while x < this.width {
|
||||
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
|
||||
|
||||
i = x as base.u64
|
||||
if i >= args.s.length() {
|
||||
break
|
||||
}
|
||||
args.s[i] ~mod+= pred
|
||||
pred = args.s[i]
|
||||
|
||||
x += 1
|
||||
}
|
||||
|
||||
// Other rows.
|
||||
y = 1
|
||||
while y < this.height,
|
||||
inv y >= 1,
|
||||
{
|
||||
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
|
||||
|
||||
i = (((y - 1) * this.width) + 0) as base.u64
|
||||
if i >= args.s.length() {
|
||||
break
|
||||
}
|
||||
t = args.s[i]
|
||||
tl = t
|
||||
l = t
|
||||
|
||||
x = 0
|
||||
while x < this.width,
|
||||
inv y >= 1,
|
||||
inv y < 0x4000,
|
||||
{
|
||||
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
|
||||
|
||||
i = (((y - 1) * this.width) + x) as base.u64
|
||||
if i >= args.s.length() {
|
||||
break
|
||||
}
|
||||
t = args.s[i]
|
||||
|
||||
i = ((y * this.width) + x) as base.u64
|
||||
if i >= args.s.length() {
|
||||
break
|
||||
}
|
||||
grad = ((l as base.u32) ~mod+ (t as base.u32)) ~mod- (tl as base.u32)
|
||||
if grad > 0x7FFF_FFFF {
|
||||
grad = 0x00
|
||||
} else if grad > 0xFF {
|
||||
grad = 0xFF
|
||||
}
|
||||
l = args.s[i] ~mod+ ((grad & 0xFF) as base.u8)
|
||||
args.s[i] = l
|
||||
tl = t
|
||||
|
||||
x += 1
|
||||
}
|
||||
|
||||
y += 1
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
pri func decoder.do_decode_frame_vp8l?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
pri func decoder.do_decode_frame_vp8l?(dst: nptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var c8 : base.u8
|
||||
var has_more : base.u32[..= 1]
|
||||
var width : base.u32[..= 0x4000]
|
||||
@@ -426,7 +426,7 @@ pri func decoder.decode_pixels?(dst: slice base.u8, src: base.io_reader, width:
|
||||
tile_size_log2: args.tile_size_log2)
|
||||
}
|
||||
|
||||
pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: roslice base.u8, blend: base.pixel_blend) base.status {
|
||||
pri func decoder.swizzle!(dst: nptr base.pixel_buffer, src: roslice base.u8, blend: base.pixel_blend) base.status {
|
||||
var status : base.status
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
@@ -438,6 +438,10 @@ pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: roslice base.u8, blen
|
||||
var dst : slice base.u8
|
||||
var y : base.u32
|
||||
|
||||
if args.dst == nullptr {
|
||||
return ok
|
||||
}
|
||||
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette_or_else(fallback: this.palette[..]),
|
||||
|
||||
+92
-26
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user