mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 08:47:28 +01:00
Have std/png process the tRNS chunk
This commit is contained in:
1 parent
c392a632de
commit
4d8e179279
2 files changed
+173
-34
No files matched your search
@@ -8503,7 +8503,9 @@ struct wuffs_png__decoder__struct {
|
||||
uint8_t f_depth;
|
||||
uint8_t f_color_type;
|
||||
uint8_t f_filter_distance;
|
||||
bool f_seen_palette;
|
||||
bool f_seen_plte;
|
||||
bool f_seen_trns;
|
||||
bool f_tricky;
|
||||
uint32_t f_dst_pixfmt;
|
||||
uint32_t f_src_pixfmt;
|
||||
uint32_t f_chunk_type;
|
||||
@@ -8531,6 +8533,7 @@ struct wuffs_png__decoder__struct {
|
||||
uint32_t p_decode_ihdr[1];
|
||||
uint32_t p_decode_other_chunk[1];
|
||||
uint32_t p_decode_plte[1];
|
||||
uint32_t p_decode_trns[1];
|
||||
uint32_t p_decode_frame_config[1];
|
||||
uint32_t p_decode_frame[1];
|
||||
wuffs_base__status (*choosy_filter_and_swizzle)(
|
||||
@@ -8557,10 +8560,14 @@ struct wuffs_png__decoder__struct {
|
||||
uint64_t scratch;
|
||||
} s_decode_other_chunk[1];
|
||||
struct {
|
||||
uint32_t v_num_palette_entries;
|
||||
uint32_t v_num_entries;
|
||||
uint32_t v_i;
|
||||
uint64_t scratch;
|
||||
} s_decode_plte[1];
|
||||
struct {
|
||||
uint32_t v_num_entries;
|
||||
uint32_t v_i;
|
||||
} s_decode_trns[1];
|
||||
struct {
|
||||
uint32_t v_checksum_have;
|
||||
uint64_t scratch;
|
||||
@@ -30690,6 +30697,11 @@ wuffs_png__decoder__decode_plte(
|
||||
wuffs_png__decoder* self,
|
||||
wuffs_base__io_buffer* a_src);
|
||||
|
||||
static wuffs_base__status
|
||||
wuffs_png__decoder__decode_trns(
|
||||
wuffs_png__decoder* self,
|
||||
wuffs_base__io_buffer* a_src);
|
||||
|
||||
static wuffs_base__status
|
||||
wuffs_png__decoder__filter_and_swizzle(
|
||||
wuffs_png__decoder* self,
|
||||
@@ -32227,7 +32239,7 @@ wuffs_png__decoder__decode_image_config(
|
||||
}
|
||||
}
|
||||
label__1__break:;
|
||||
if ((self->private_impl.f_color_type == 3) && ! self->private_impl.f_seen_palette) {
|
||||
if ((self->private_impl.f_color_type == 3) && ! self->private_impl.f_seen_plte) {
|
||||
status = wuffs_base__make_status(wuffs_png__error__missing_palette);
|
||||
goto exit;
|
||||
}
|
||||
@@ -32240,7 +32252,7 @@ wuffs_png__decoder__decode_image_config(
|
||||
self->private_impl.f_width,
|
||||
self->private_impl.f_height,
|
||||
self->private_impl.f_frame_config_io_position,
|
||||
false);
|
||||
((self->private_impl.f_color_type <= 3) && ! self->private_impl.f_seen_trns));
|
||||
}
|
||||
self->private_impl.f_call_sequence = 3;
|
||||
|
||||
@@ -32507,6 +32519,7 @@ wuffs_png__decoder__calculate_bytes_per_row(
|
||||
self->private_impl.f_filter_distance = 1;
|
||||
self->private_impl.f_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * 1);
|
||||
}
|
||||
} else if (self->private_impl.f_color_type == 4) {
|
||||
} else if (self->private_impl.f_color_type == 6) {
|
||||
self->private_impl.f_dst_pixfmt = 2164295816;
|
||||
self->private_impl.f_src_pixfmt = 2701166728;
|
||||
@@ -32577,7 +32590,7 @@ wuffs_png__decoder__decode_other_chunk(
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
if (self->private_impl.f_chunk_type == 1163152464) {
|
||||
if (self->private_impl.f_seen_palette) {
|
||||
if (self->private_impl.f_seen_plte || (self->private_impl.f_color_type != 3)) {
|
||||
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
|
||||
goto exit;
|
||||
}
|
||||
@@ -32592,17 +32605,24 @@ wuffs_png__decoder__decode_other_chunk(
|
||||
if (status.repr) {
|
||||
goto suspend;
|
||||
}
|
||||
self->private_impl.f_seen_palette = true;
|
||||
self->private_impl.f_seen_plte = true;
|
||||
} else if (self->private_impl.f_chunk_type == 1397641844) {
|
||||
self->private_data.s_decode_other_chunk[0].scratch = self->private_impl.f_chunk_length;
|
||||
if (self->private_impl.f_seen_trns || (self->private_impl.f_color_type > 3) || ((self->private_impl.f_color_type == 3) && ! self->private_impl.f_seen_plte)) {
|
||||
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
|
||||
goto exit;
|
||||
}
|
||||
if (a_src) {
|
||||
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
if (self->private_data.s_decode_other_chunk[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
|
||||
self->private_data.s_decode_other_chunk[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);
|
||||
status = wuffs_png__decoder__decode_trns(self, a_src);
|
||||
if (a_src) {
|
||||
iop_a_src = a_src->data.ptr + a_src->meta.ri;
|
||||
}
|
||||
if (status.repr) {
|
||||
goto suspend;
|
||||
}
|
||||
iop_a_src += self->private_data.s_decode_other_chunk[0].scratch;
|
||||
self->private_impl.f_seen_trns = true;
|
||||
} else {
|
||||
self->private_data.s_decode_other_chunk[0].scratch = self->private_impl.f_chunk_length;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
@@ -32642,7 +32662,7 @@ wuffs_png__decoder__decode_plte(
|
||||
wuffs_base__io_buffer* a_src) {
|
||||
wuffs_base__status status = wuffs_base__make_status(NULL);
|
||||
|
||||
uint32_t v_num_palette_entries = 0;
|
||||
uint32_t v_num_entries = 0;
|
||||
uint32_t v_i = 0;
|
||||
uint32_t v_argb = 0;
|
||||
|
||||
@@ -32659,18 +32679,18 @@ wuffs_png__decoder__decode_plte(
|
||||
|
||||
uint32_t coro_susp_point = self->private_impl.p_decode_plte[0];
|
||||
if (coro_susp_point) {
|
||||
v_num_palette_entries = self->private_data.s_decode_plte[0].v_num_palette_entries;
|
||||
v_num_entries = self->private_data.s_decode_plte[0].v_num_entries;
|
||||
v_i = self->private_data.s_decode_plte[0].v_i;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
if ((self->private_impl.f_chunk_length > 768) || ((self->private_impl.f_chunk_length % 3) != 0)) {
|
||||
status = wuffs_base__make_status(wuffs_png__error__bad_header);
|
||||
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
|
||||
goto exit;
|
||||
}
|
||||
v_num_palette_entries = (((uint32_t)(self->private_impl.f_chunk_length)) / 3);
|
||||
while (v_i < v_num_palette_entries) {
|
||||
v_num_entries = (((uint32_t)(self->private_impl.f_chunk_length)) / 3);
|
||||
while (v_i < v_num_entries) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
uint32_t t_0;
|
||||
@@ -32724,7 +32744,7 @@ wuffs_png__decoder__decode_plte(
|
||||
goto suspend;
|
||||
suspend:
|
||||
self->private_impl.p_decode_plte[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
|
||||
self->private_data.s_decode_plte[0].v_num_palette_entries = v_num_palette_entries;
|
||||
self->private_data.s_decode_plte[0].v_num_entries = v_num_entries;
|
||||
self->private_data.s_decode_plte[0].v_i = v_i;
|
||||
|
||||
goto exit;
|
||||
@@ -32736,6 +32756,87 @@ wuffs_png__decoder__decode_plte(
|
||||
return status;
|
||||
}
|
||||
|
||||
// -------- func png.decoder.decode_trns
|
||||
|
||||
static wuffs_base__status
|
||||
wuffs_png__decoder__decode_trns(
|
||||
wuffs_png__decoder* self,
|
||||
wuffs_base__io_buffer* a_src) {
|
||||
wuffs_base__status status = wuffs_base__make_status(NULL);
|
||||
|
||||
uint32_t v_num_entries = 0;
|
||||
uint32_t v_i = 0;
|
||||
|
||||
const uint8_t* iop_a_src = NULL;
|
||||
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
if (a_src) {
|
||||
io0_a_src = a_src->data.ptr;
|
||||
io1_a_src = io0_a_src + a_src->meta.ri;
|
||||
iop_a_src = io1_a_src;
|
||||
io2_a_src = io0_a_src + a_src->meta.wi;
|
||||
}
|
||||
|
||||
uint32_t coro_susp_point = self->private_impl.p_decode_trns[0];
|
||||
if (coro_susp_point) {
|
||||
v_num_entries = self->private_data.s_decode_trns[0].v_num_entries;
|
||||
v_i = self->private_data.s_decode_trns[0].v_i;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
if (self->private_impl.f_chunk_length > 256) {
|
||||
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
|
||||
goto exit;
|
||||
}
|
||||
v_num_entries = ((uint32_t)(self->private_impl.f_chunk_length));
|
||||
while (v_i < v_num_entries) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
|
||||
goto suspend;
|
||||
}
|
||||
uint8_t t_0 = *iop_a_src++;
|
||||
self->private_data.f_src_palette[((4 * v_i) + 3)] = t_0;
|
||||
}
|
||||
v_i += 1;
|
||||
}
|
||||
if (self->private_impl.f_color_type == 0) {
|
||||
self->private_impl.f_dst_pixfmt = 2164295816;
|
||||
self->private_impl.f_src_pixfmt = 2164308923;
|
||||
self->private_impl.f_tricky = true;
|
||||
} else if (self->private_impl.f_color_type == 2) {
|
||||
self->private_impl.f_dst_pixfmt = 2164295816;
|
||||
self->private_impl.f_src_pixfmt = 2164308923;
|
||||
self->private_impl.f_tricky = true;
|
||||
} else if (self->private_impl.f_color_type == 3) {
|
||||
self->private_impl.f_dst_pixfmt = 2164523016;
|
||||
self->private_impl.f_src_pixfmt = 2164523016;
|
||||
}
|
||||
|
||||
goto ok;
|
||||
ok:
|
||||
self->private_impl.p_decode_trns[0] = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
goto suspend;
|
||||
suspend:
|
||||
self->private_impl.p_decode_trns[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
|
||||
self->private_data.s_decode_trns[0].v_num_entries = v_num_entries;
|
||||
self->private_data.s_decode_trns[0].v_i = v_i;
|
||||
|
||||
goto exit;
|
||||
exit:
|
||||
if (a_src) {
|
||||
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
// -------- func png.decoder.decode_frame_config
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
|
||||
|
||||
+54
-16
@@ -72,7 +72,10 @@ pub struct decoder? implements base.image_decoder(
|
||||
color_type : base.u8,
|
||||
filter_distance : base.u8[..= 8],
|
||||
|
||||
seen_palette : base.bool,
|
||||
seen_plte : base.bool,
|
||||
seen_trns : base.bool,
|
||||
|
||||
tricky : base.bool,
|
||||
|
||||
dst_pixfmt : base.u32,
|
||||
src_pixfmt : base.u32,
|
||||
@@ -196,7 +199,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
|
||||
|
||||
} endwhile
|
||||
|
||||
if (this.color_type == 3) and (not this.seen_palette) {
|
||||
if (this.color_type == 3) and (not this.seen_plte) {
|
||||
return "#missing palette"
|
||||
}
|
||||
|
||||
@@ -209,7 +212,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.frame_config_io_position,
|
||||
first_frame_is_opaque: false)
|
||||
first_frame_is_opaque: (this.color_type <= 3) and (not this.seen_trns))
|
||||
}
|
||||
|
||||
this.call_sequence = 3
|
||||
@@ -282,7 +285,6 @@ pri func decoder.calculate_bytes_per_row!() {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y
|
||||
} else if this.color_type == 3 {
|
||||
// TODO: s/BINARY/NONPREMUL/ and decode the tRNS chunk.
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
} else {
|
||||
@@ -316,7 +318,6 @@ pri func decoder.calculate_bytes_per_row!() {
|
||||
}
|
||||
|
||||
} else if this.color_type == 3 {
|
||||
// TODO: s/BINARY/NONPREMUL/ and decode the tRNS chunk.
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
if this.depth == 8 {
|
||||
@@ -324,6 +325,9 @@ pri func decoder.calculate_bytes_per_row!() {
|
||||
this.bytes_per_row = (this.width as base.u64) * 1
|
||||
}
|
||||
|
||||
} else if this.color_type == 4 {
|
||||
// TODO: PIXEL_FORMAT__YA_NONPREMUL
|
||||
|
||||
} else if this.color_type == 6 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__RGBA_NONPREMUL
|
||||
@@ -358,31 +362,35 @@ pri func decoder.choose_filter_implementations!() {
|
||||
|
||||
pri func decoder.decode_other_chunk?(src: base.io_reader) {
|
||||
if this.chunk_type == 'PLTE'le {
|
||||
if this.seen_palette {
|
||||
if this.seen_plte or (this.color_type <> 3) {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_plte?(src: args.src)
|
||||
this.seen_palette = true
|
||||
this.seen_plte = true
|
||||
} else if this.chunk_type == 'tRNS'le {
|
||||
// TODO.
|
||||
args.src.skip?(n: this.chunk_length)
|
||||
if this.seen_trns or (this.color_type > 3) or
|
||||
((this.color_type == 3) and (not this.seen_plte)) {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_trns?(src: args.src)
|
||||
this.seen_trns = true
|
||||
} else {
|
||||
args.src.skip?(n: this.chunk_length)
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.decode_plte?(src: base.io_reader) {
|
||||
var num_palette_entries : base.u32[..= 256]
|
||||
var i : base.u32
|
||||
var argb : base.u32
|
||||
var num_entries : base.u32[..= 256]
|
||||
var i : base.u32
|
||||
var argb : base.u32
|
||||
|
||||
if (this.chunk_length > 768) or ((this.chunk_length % 3) <> 0) {
|
||||
return "#bad header"
|
||||
return "#bad chunk"
|
||||
}
|
||||
num_palette_entries = (this.chunk_length as base.u32) / 3
|
||||
num_entries = (this.chunk_length as base.u32) / 3
|
||||
|
||||
while i < num_palette_entries {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: num_palette_entries)
|
||||
while i < num_entries {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: num_entries)
|
||||
// Convert from RGB (in memory order) to ARGB (in native u32 order)
|
||||
// to BGRA (in memory order).
|
||||
argb = args.src.read_u24be_as_u32?()
|
||||
@@ -404,6 +412,36 @@ pri func decoder.decode_plte?(src: base.io_reader) {
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.decode_trns?(src: base.io_reader) {
|
||||
var num_entries : base.u32[..= 256]
|
||||
var i : base.u32
|
||||
|
||||
if this.chunk_length > 256 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
num_entries = this.chunk_length as base.u32
|
||||
|
||||
while i < num_entries {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: num_entries)
|
||||
this.src_palette[(4 * i) + 3] = args.src.read_u8?()
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
if this.color_type == 0 {
|
||||
// TODO: PIXEL_FORMAT__YA_NONPREMUL
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.tricky = true
|
||||
} else if this.color_type == 2 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.tricky = true
|
||||
} else if this.color_type == 3 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
if this.call_sequence < 3 {
|
||||
this.decode_image_config?(dst: nullptr, src: args.src)
|
||||
|
||||
Reference in new issue
Block a user