Let std/png decode (animated) APNG

The test/nia-checksums-of-data.txt checksums for the *.apng files now
match the (multiple frame) *.gif files. Previously, they matched the
(single frame) *.000000.nie files.

Fixes #41
This commit is contained in:
Nigel Tao committed 2021-11-14 18:20:15 +11:00
1 parent 22eed1fd8d
commit a4fa6a2554
6 files changed
+1645 -288

No files matched your search

+1092 -173
View File
@@ -9132,6 +9132,7 @@ struct wuffs_zlib__decoder__struct {
// ---------------- Status Codes
extern const char wuffs_png__error__bad_animation_sequence_number[];
extern const char wuffs_png__error__bad_checksum[];
extern const char wuffs_png__error__bad_chunk[];
extern const char wuffs_png__error__bad_filter[];
@@ -9143,6 +9144,8 @@ extern const char wuffs_png__error__unsupported_png_file[];
#define WUFFS_PNG__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 0
#define WUFFS_PNG__DECODER_SRC_IO_BUFFER_LENGTH_MIN_INCL 8
// ---------------- Struct Declarations
typedef struct wuffs_png__decoder__struct wuffs_png__decoder;
@@ -9305,7 +9308,9 @@ struct wuffs_png__decoder__struct {
uint8_t f_color_type;
uint8_t f_filter_distance;
uint8_t f_interlace_pass;
bool f_seen_actl;
bool f_seen_chrm;
bool f_seen_fctl;
bool f_seen_gama;
bool f_seen_iccp;
bool f_seen_plte;
@@ -9318,7 +9323,27 @@ struct wuffs_png__decoder__struct {
uint32_t f_chunk_type;
uint8_t f_chunk_type_array[4];
uint64_t f_chunk_length;
uint32_t f_num_animation_frames_value;
uint32_t f_num_animation_loops_value;
uint32_t f_num_decoded_frame_configs_value;
uint32_t f_num_decoded_frames_value;
uint32_t f_frame_rect_x0;
uint32_t f_frame_rect_y0;
uint32_t f_frame_rect_x1;
uint32_t f_frame_rect_y1;
uint32_t f_first_rect_x0;
uint32_t f_first_rect_y0;
uint32_t f_first_rect_x1;
uint32_t f_first_rect_y1;
uint64_t f_frame_config_io_position;
uint64_t f_first_config_io_position;
uint64_t f_frame_duration;
uint64_t f_first_duration;
uint8_t f_frame_disposal;
uint8_t f_first_disposal;
bool f_frame_overwrite_instead_of_blend;
bool f_first_overwrite_instead_of_blend;
uint32_t f_next_animation_seq_num;
uint32_t f_metadata_flavor;
uint32_t f_metadata_fourcc;
uint64_t f_metadata_x;
@@ -9340,13 +9365,16 @@ struct wuffs_png__decoder__struct {
uint32_t p_decode_image_config[1];
uint32_t p_decode_ihdr[1];
uint32_t p_decode_other_chunk[1];
uint32_t p_decode_actl[1];
uint32_t p_decode_chrm[1];
uint32_t p_decode_fctl[1];
uint32_t p_decode_gama[1];
uint32_t p_decode_iccp[1];
uint32_t p_decode_plte[1];
uint32_t p_decode_srgb[1];
uint32_t p_decode_trns[1];
uint32_t p_decode_frame_config[1];
uint32_t p_skip_frame[1];
uint32_t p_decode_frame[1];
uint32_t p_decode_pass[1];
uint32_t p_tell_me_more[1];
@@ -9372,9 +9400,18 @@ struct wuffs_png__decoder__struct {
struct {
uint64_t scratch;
} s_decode_other_chunk[1];
struct {
uint64_t scratch;
} s_decode_actl[1];
struct {
uint64_t scratch;
} s_decode_chrm[1];
struct {
uint32_t v_x0;
uint32_t v_x1;
uint32_t v_y1;
uint64_t scratch;
} s_decode_fctl[1];
struct {
uint64_t scratch;
} s_decode_gama[1];
@@ -9388,11 +9425,16 @@ struct wuffs_png__decoder__struct {
uint32_t v_n;
uint64_t scratch;
} s_decode_trns[1];
struct {
uint64_t scratch;
} s_decode_frame_config[1];
struct {
uint64_t scratch;
} s_skip_frame[1];
struct {
uint64_t scratch;
} s_decode_frame[1];
struct {
uint32_t v_checksum_have;
uint64_t scratch;
} s_decode_pass[1];
struct {
@@ -35239,12 +35281,14 @@ wuffs_zlib__decoder__transform_io(
// ---------------- Status Codes Implementations
const char wuffs_png__error__bad_animation_sequence_number[] = "#png: bad animation sequence number";
const char wuffs_png__error__bad_checksum[] = "#png: bad checksum";
const char wuffs_png__error__bad_chunk[] = "#png: bad chunk";
const char wuffs_png__error__bad_filter[] = "#png: bad filter";
const char wuffs_png__error__bad_header[] = "#png: bad header";
const char wuffs_png__error__missing_palette[] = "#png: missing palette";
const char wuffs_png__error__unsupported_png_file[] = "#png: unsupported PNG file";
const char wuffs_png__error__internal_error_inconsistent_frame_bounds[] = "#png: internal error: inconsistent frame bounds";
const char wuffs_png__error__internal_error_inconsistent_workbuf_length[] = "#png: internal error: inconsistent workbuf length";
const char wuffs_png__error__internal_error_zlib_decoder_did_not_exhaust_its_input[] = "#png: internal error: zlib decoder did not exhaust its input";
@@ -35449,11 +35493,21 @@ wuffs_png__decoder__decode_other_chunk(
wuffs_png__decoder* self,
wuffs_base__io_buffer* a_src);
static wuffs_base__status
wuffs_png__decoder__decode_actl(
wuffs_png__decoder* self,
wuffs_base__io_buffer* a_src);
static wuffs_base__status
wuffs_png__decoder__decode_chrm(
wuffs_png__decoder* self,
wuffs_base__io_buffer* a_src);
static wuffs_base__status
wuffs_png__decoder__decode_fctl(
wuffs_png__decoder* self,
wuffs_base__io_buffer* a_src);
static wuffs_base__status
wuffs_png__decoder__decode_gama(
wuffs_png__decoder* self,
@@ -35479,6 +35533,11 @@ wuffs_png__decoder__decode_trns(
wuffs_png__decoder* self,
wuffs_base__io_buffer* a_src);
static wuffs_base__status
wuffs_png__decoder__skip_frame(
wuffs_png__decoder* self,
wuffs_base__io_buffer* a_src);
static wuffs_base__status
wuffs_png__decoder__decode_pass(
wuffs_png__decoder* self,
@@ -37248,15 +37307,19 @@ wuffs_png__decoder__decode_image_config(
self->private_impl.f_chunk_length = ((uint64_t)(wuffs_base__peek_u32be__no_bounds_check(iop_a_src)));
self->private_impl.f_chunk_type = ((uint32_t)((wuffs_base__peek_u64le__no_bounds_check(iop_a_src) >> 32)));
if (self->private_impl.f_chunk_type == 1413563465) {
if (self->private_impl.f_seen_actl && ! self->private_impl.f_seen_fctl) {
status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file);
goto exit;
}
goto label__1__break;
}
iop_a_src += 8;
if ( ! self->private_impl.f_ignore_checksum && (self->private_impl.f_chunk_type == 1163152464)) {
wuffs_base__ignore_status(wuffs_crc32__ieee_hasher__initialize(&self->private_data.f_crc32, sizeof (wuffs_crc32__ieee_hasher), WUFFS_VERSION, 0));
self->private_impl.f_chunk_type_array[0] = ((uint8_t)(((self->private_impl.f_chunk_type >> 0) & 255)));
self->private_impl.f_chunk_type_array[1] = ((uint8_t)(((self->private_impl.f_chunk_type >> 8) & 255)));
self->private_impl.f_chunk_type_array[2] = ((uint8_t)(((self->private_impl.f_chunk_type >> 16) & 255)));
self->private_impl.f_chunk_type_array[3] = ((uint8_t)(((self->private_impl.f_chunk_type >> 24) & 255)));
self->private_impl.f_chunk_type_array[0] = 80;
self->private_impl.f_chunk_type_array[1] = 76;
self->private_impl.f_chunk_type_array[2] = 84;
self->private_impl.f_chunk_type_array[3] = 69;
wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__make_slice_u8(self->private_impl.f_chunk_type_array, 4));
}
while (true) {
@@ -37326,6 +37389,7 @@ wuffs_png__decoder__decode_image_config(
goto exit;
}
self->private_impl.f_frame_config_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)));
self->private_impl.f_first_config_io_position = self->private_impl.f_frame_config_io_position;
if (a_dst != NULL) {
wuffs_base__image_config__set(
a_dst,
@@ -37333,9 +37397,19 @@ wuffs_png__decoder__decode_image_config(
0,
self->private_impl.f_width,
self->private_impl.f_height,
self->private_impl.f_frame_config_io_position,
self->private_impl.f_first_config_io_position,
((self->private_impl.f_color_type <= 3) && ! self->private_impl.f_seen_trns));
}
if ( ! self->private_impl.f_seen_actl) {
self->private_impl.f_num_animation_frames_value = 1;
self->private_impl.f_first_rect_x0 = 0;
self->private_impl.f_first_rect_y0 = 0;
self->private_impl.f_first_rect_x1 = self->private_impl.f_width;
self->private_impl.f_first_rect_y1 = self->private_impl.f_height;
self->private_impl.f_first_duration = 0;
self->private_impl.f_first_disposal = 0;
self->private_impl.f_first_overwrite_instead_of_blend = false;
}
self->private_impl.f_call_sequence = 3;
ok:
@@ -37746,6 +37820,23 @@ wuffs_png__decoder__decode_other_chunk(
goto suspend;
}
self->private_impl.f_seen_plte = true;
} else if (self->private_impl.f_chunk_type == 1280598881) {
if (self->private_impl.f_seen_actl) {
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);
status = wuffs_png__decoder__decode_actl(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
self->private_impl.f_seen_actl = true;
} else if (self->private_impl.f_chunk_type == 1297238115) {
if (self->private_impl.f_report_metadata_chrm) {
if (self->private_impl.f_seen_chrm) {
@@ -37755,7 +37846,7 @@ wuffs_png__decoder__decode_other_chunk(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
status = wuffs_png__decoder__decode_chrm(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -37765,6 +37856,26 @@ wuffs_png__decoder__decode_other_chunk(
}
self->private_impl.f_seen_chrm = true;
}
} else if (self->private_impl.f_chunk_type == 1280598886) {
if (self->private_impl.f_seen_fctl) {
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(4);
status = wuffs_png__decoder__decode_fctl(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
self->private_impl.f_seen_fctl = true;
} else if (self->private_impl.f_chunk_type == 1413571686) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
} else if (self->private_impl.f_chunk_type == 1095582055) {
if (self->private_impl.f_report_metadata_gama) {
if (self->private_impl.f_seen_gama) {
@@ -37774,7 +37885,7 @@ wuffs_png__decoder__decode_other_chunk(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
status = wuffs_png__decoder__decode_gama(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -37793,7 +37904,7 @@ wuffs_png__decoder__decode_other_chunk(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
status = wuffs_png__decoder__decode_iccp(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -37812,7 +37923,7 @@ wuffs_png__decoder__decode_other_chunk(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
status = wuffs_png__decoder__decode_srgb(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -37830,7 +37941,7 @@ wuffs_png__decoder__decode_other_chunk(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8);
status = wuffs_png__decoder__decode_trns(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -37842,7 +37953,7 @@ wuffs_png__decoder__decode_other_chunk(
}
if (self->private_impl.f_metadata_fourcc == 0) {
self->private_data.s_decode_other_chunk[0].scratch = self->private_impl.f_chunk_length;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9);
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;
@@ -37871,6 +37982,119 @@ wuffs_png__decoder__decode_other_chunk(
return status;
}
// -------- func png.decoder.decode_actl
static wuffs_base__status
wuffs_png__decoder__decode_actl(
wuffs_png__decoder* self,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
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_actl[0];
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_chunk_length != 8) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
} else if (self->private_impl.f_interlace_pass > 0) {
status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file);
goto exit;
}
self->private_impl.f_chunk_length = 0;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
uint32_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_actl[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
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_actl[0].scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_0);
if (num_bits_0 == 24) {
t_0 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_0 += 8;
*scratch |= ((uint64_t)(num_bits_0));
}
}
self->private_impl.f_num_animation_frames_value = t_0;
}
if (self->private_impl.f_num_animation_frames_value == 0) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_actl[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
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_actl[0].scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_1);
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_1 += 8;
*scratch |= ((uint64_t)(num_bits_1));
}
}
self->private_impl.f_num_animation_loops_value = t_1;
}
goto ok;
ok:
self->private_impl.p_decode_actl[0] = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_actl[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
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_chrm
static wuffs_base__status
@@ -38168,6 +38392,343 @@ wuffs_png__decoder__decode_chrm(
return status;
}
// -------- func png.decoder.decode_fctl
static wuffs_base__status
wuffs_png__decoder__decode_fctl(
wuffs_png__decoder* self,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_x0 = 0;
uint32_t v_y0 = 0;
uint32_t v_x1 = 0;
uint32_t v_y1 = 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_fctl[0];
if (coro_susp_point) {
v_x0 = self->private_data.s_decode_fctl[0].v_x0;
v_x1 = self->private_data.s_decode_fctl[0].v_x1;
v_y1 = self->private_data.s_decode_fctl[0].v_y1;
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_chunk_length != 26) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
self->private_impl.f_chunk_length = 0;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
uint32_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_fctl[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
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_fctl[0].scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_0);
if (num_bits_0 == 24) {
t_0 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_0 += 8;
*scratch |= ((uint64_t)(num_bits_0));
}
}
v_x0 = t_0;
}
if (v_x0 != self->private_impl.f_next_animation_seq_num) {
status = wuffs_base__make_status(wuffs_png__error__bad_animation_sequence_number);
goto exit;
} else if (self->private_impl.f_next_animation_seq_num >= 4294967295) {
status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file);
goto exit;
}
self->private_impl.f_next_animation_seq_num += 1;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_fctl[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
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_fctl[0].scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_1);
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_1 += 8;
*scratch |= ((uint64_t)(num_bits_1));
}
}
v_x1 = t_1;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
uint32_t t_2;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_2 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_fctl[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
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_fctl[0].scratch;
uint32_t num_bits_2 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_2);
if (num_bits_2 == 24) {
t_2 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_2 += 8;
*scratch |= ((uint64_t)(num_bits_2));
}
}
v_y1 = t_2;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
uint32_t t_3;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_3 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_fctl[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8);
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_fctl[0].scratch;
uint32_t num_bits_3 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_3);
if (num_bits_3 == 24) {
t_3 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_3 += 8;
*scratch |= ((uint64_t)(num_bits_3));
}
}
v_x0 = t_3;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9);
uint32_t t_4;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_4 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_fctl[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10);
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_fctl[0].scratch;
uint32_t num_bits_4 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_4);
if (num_bits_4 == 24) {
t_4 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_4 += 8;
*scratch |= ((uint64_t)(num_bits_4));
}
}
v_y0 = t_4;
}
v_x1 += v_x0;
v_y1 += v_y0;
if ((v_x0 >= v_x1) ||
(v_x0 > self->private_impl.f_width) ||
(v_x1 > self->private_impl.f_width) ||
(v_y0 >= v_y1) ||
(v_y0 > self->private_impl.f_height) ||
(v_y1 > self->private_impl.f_height)) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
self->private_impl.f_frame_rect_x0 = v_x0;
self->private_impl.f_frame_rect_y0 = v_y0;
self->private_impl.f_frame_rect_x1 = v_x1;
self->private_impl.f_frame_rect_y1 = v_y1;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11);
uint32_t t_5;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 2)) {
t_5 = ((uint32_t)(wuffs_base__peek_u16be__no_bounds_check(iop_a_src)));
iop_a_src += 2;
} else {
self->private_data.s_decode_fctl[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(12);
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_fctl[0].scratch;
uint32_t num_bits_5 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_5);
if (num_bits_5 == 8) {
t_5 = ((uint32_t)(*scratch >> 48));
break;
}
num_bits_5 += 8;
*scratch |= ((uint64_t)(num_bits_5));
}
}
v_x0 = t_5;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(13);
uint32_t t_6;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 2)) {
t_6 = ((uint32_t)(wuffs_base__peek_u16be__no_bounds_check(iop_a_src)));
iop_a_src += 2;
} else {
self->private_data.s_decode_fctl[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(14);
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_fctl[0].scratch;
uint32_t num_bits_6 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_6);
if (num_bits_6 == 8) {
t_6 = ((uint32_t)(*scratch >> 48));
break;
}
num_bits_6 += 8;
*scratch |= ((uint64_t)(num_bits_6));
}
}
v_x1 = t_6;
}
if (v_x1 <= 0) {
self->private_impl.f_frame_duration = (((uint64_t)(v_x0)) * 7056000);
} else {
self->private_impl.f_frame_duration = ((((uint64_t)(v_x0)) * 705600000) / ((uint64_t)(v_x1)));
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(15);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint32_t t_7 = *iop_a_src++;
v_x0 = t_7;
}
if (v_x0 == 0) {
self->private_impl.f_frame_disposal = 0;
} else if (v_x0 == 1) {
self->private_impl.f_frame_disposal = 1;
} else if (v_x0 == 2) {
self->private_impl.f_frame_disposal = 2;
} else {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(16);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint32_t t_8 = *iop_a_src++;
v_x0 = t_8;
}
if (v_x0 == 0) {
self->private_impl.f_frame_overwrite_instead_of_blend = true;
} else if (v_x0 == 1) {
self->private_impl.f_frame_overwrite_instead_of_blend = false;
} else {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
if (self->private_impl.f_num_decoded_frame_configs_value == 0) {
self->private_impl.f_first_rect_x0 = self->private_impl.f_frame_rect_x0;
self->private_impl.f_first_rect_y0 = self->private_impl.f_frame_rect_y0;
self->private_impl.f_first_rect_x1 = self->private_impl.f_frame_rect_x1;
self->private_impl.f_first_rect_y1 = self->private_impl.f_frame_rect_y1;
self->private_impl.f_first_duration = self->private_impl.f_frame_duration;
self->private_impl.f_first_disposal = self->private_impl.f_frame_disposal;
self->private_impl.f_first_overwrite_instead_of_blend = self->private_impl.f_frame_overwrite_instead_of_blend;
}
goto ok;
ok:
self->private_impl.p_decode_fctl[0] = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_fctl[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_data.s_decode_fctl[0].v_x0 = v_x0;
self->private_data.s_decode_fctl[0].v_x1 = v_x1;
self->private_data.s_decode_fctl[0].v_y1 = v_y1;
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_gama
static wuffs_base__status
@@ -38729,7 +39290,10 @@ wuffs_png__decoder__decode_frame_config(
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_call_sequence < 3) {
if (self->private_impl.f_call_sequence == 255) {
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
} else if (self->private_impl.f_call_sequence < 3) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
@@ -38747,29 +39311,148 @@ wuffs_png__decoder__decode_frame_config(
goto exit;
}
} else if (self->private_impl.f_call_sequence == 4) {
self->private_impl.f_call_sequence = 255;
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
status = wuffs_png__decoder__skip_frame(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
if (self->private_impl.f_call_sequence == 255) {
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
}
}
if (self->private_impl.f_num_decoded_frame_configs_value == 0) {
self->private_impl.f_frame_rect_x0 = self->private_impl.f_first_rect_x0;
self->private_impl.f_frame_rect_y0 = self->private_impl.f_first_rect_y0;
self->private_impl.f_frame_rect_x1 = self->private_impl.f_first_rect_x1;
self->private_impl.f_frame_rect_y1 = self->private_impl.f_first_rect_y1;
self->private_impl.f_frame_config_io_position = self->private_impl.f_first_config_io_position;
self->private_impl.f_frame_duration = self->private_impl.f_first_duration;
self->private_impl.f_frame_disposal = self->private_impl.f_first_disposal;
self->private_impl.f_frame_overwrite_instead_of_blend = self->private_impl.f_first_overwrite_instead_of_blend;
} else {
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
while (true) {
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
uint64_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = ((uint64_t)(wuffs_base__peek_u32be__no_bounds_check(iop_a_src)));
iop_a_src += 4;
} else {
self->private_data.s_decode_frame_config[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
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_frame_config[0].scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_0);
if (num_bits_0 == 24) {
t_0 = ((uint64_t)(*scratch >> 32));
break;
}
num_bits_0 += 8;
*scratch |= ((uint64_t)(num_bits_0));
}
}
self->private_impl.f_chunk_length = t_0;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_frame_config[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
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_frame_config[0].scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1;
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch));
break;
}
num_bits_1 += 8;
*scratch |= ((uint64_t)(num_bits_1)) << 56;
}
}
self->private_impl.f_chunk_type = t_1;
}
if (self->private_impl.f_chunk_type == 1413571686) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
} else if (self->private_impl.f_chunk_type == 1280598886) {
self->private_impl.f_frame_config_io_position = ((uint64_t)(wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src))) - 8));
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
status = wuffs_png__decoder__decode_fctl(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
self->private_data.s_decode_frame_config[0].scratch = 4;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8);
if (self->private_data.s_decode_frame_config[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
self->private_data.s_decode_frame_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_frame_config[0].scratch;
goto label__0__break;
}
self->private_data.s_decode_frame_config[0].scratch = (self->private_impl.f_chunk_length + 4);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9);
if (self->private_data.s_decode_frame_config[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
self->private_data.s_decode_frame_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_frame_config[0].scratch;
self->private_impl.f_chunk_length = 0;
}
label__0__break:;
}
if (a_dst != NULL) {
wuffs_base__frame_config__set(
a_dst,
wuffs_base__utility__make_rect_ie_u32(
0,
0,
self->private_impl.f_width,
self->private_impl.f_height),
((wuffs_base__flicks)(0)),
0,
self->private_impl.f_frame_rect_x0,
self->private_impl.f_frame_rect_y0,
self->private_impl.f_frame_rect_x1,
self->private_impl.f_frame_rect_y1),
((wuffs_base__flicks)(self->private_impl.f_frame_duration)),
((uint64_t)(self->private_impl.f_num_decoded_frame_configs_value)),
self->private_impl.f_frame_config_io_position,
0,
false,
false,
self->private_impl.f_frame_disposal,
((self->private_impl.f_color_type <= 3) && ! self->private_impl.f_seen_trns),
self->private_impl.f_frame_overwrite_instead_of_blend,
0);
}
wuffs_base__u32__sat_add_indirect(&self->private_impl.f_num_decoded_frame_configs_value, 1);
self->private_impl.f_call_sequence = 4;
ok:
@@ -38794,6 +39477,162 @@ wuffs_png__decoder__decode_frame_config(
return status;
}
// -------- func png.decoder.skip_frame
static wuffs_base__status
wuffs_png__decoder__skip_frame(
wuffs_png__decoder* self,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_seq_num = 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_skip_frame[0];
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
self->private_impl.f_chunk_type_array[0] = 0;
self->private_impl.f_chunk_type_array[1] = 0;
self->private_impl.f_chunk_type_array[2] = 0;
self->private_impl.f_chunk_type_array[3] = 0;
label__0__continue:;
while (true) {
while (((uint64_t)(io2_a_src - iop_a_src)) < 8) {
if (a_src && a_src->meta.closed) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
}
self->private_impl.f_chunk_length = ((uint64_t)(wuffs_base__peek_u32be__no_bounds_check(iop_a_src)));
self->private_impl.f_chunk_type = ((uint32_t)((wuffs_base__peek_u64le__no_bounds_check(iop_a_src) >> 32)));
if (self->private_impl.f_chunk_type == 1413563465) {
if (self->private_impl.f_chunk_type_array[0] == 102) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
self->private_impl.f_chunk_type_array[0] = 73;
self->private_impl.f_chunk_type_array[1] = 68;
self->private_impl.f_chunk_type_array[2] = 65;
self->private_impl.f_chunk_type_array[3] = 84;
} else if (self->private_impl.f_chunk_type == 1413571686) {
if (self->private_impl.f_chunk_type_array[0] == 73) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
self->private_impl.f_chunk_type_array[0] = 102;
self->private_impl.f_chunk_type_array[1] = 100;
self->private_impl.f_chunk_type_array[2] = 65;
self->private_impl.f_chunk_type_array[3] = 84;
if (self->private_impl.f_chunk_length < 4) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
self->private_impl.f_chunk_length -= 4;
iop_a_src += 8;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
uint32_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_skip_frame[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
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_skip_frame[0].scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_0);
if (num_bits_0 == 24) {
t_0 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_0 += 8;
*scratch |= ((uint64_t)(num_bits_0));
}
}
v_seq_num = t_0;
}
if (v_seq_num != self->private_impl.f_next_animation_seq_num) {
status = wuffs_base__make_status(wuffs_png__error__bad_animation_sequence_number);
goto exit;
} else if (self->private_impl.f_next_animation_seq_num >= 4294967295) {
status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file);
goto exit;
}
self->private_impl.f_next_animation_seq_num += 1;
self->private_data.s_skip_frame[0].scratch = (self->private_impl.f_chunk_length + 4);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
if (self->private_data.s_skip_frame[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
self->private_data.s_skip_frame[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_skip_frame[0].scratch;
self->private_impl.f_chunk_length = 0;
goto label__0__continue;
} else if (self->private_impl.f_chunk_type_array[0] != 0) {
goto label__0__break;
} else if (self->private_impl.f_chunk_type == 1280598886) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
self->private_data.s_skip_frame[0].scratch = (self->private_impl.f_chunk_length + 12);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
if (self->private_data.s_skip_frame[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
self->private_data.s_skip_frame[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_skip_frame[0].scratch;
self->private_impl.f_chunk_length = 0;
}
label__0__break:;
wuffs_base__u32__sat_add_indirect(&self->private_impl.f_num_decoded_frames_value, 1);
if (self->private_impl.f_num_decoded_frames_value < self->private_impl.f_num_animation_frames_value) {
self->private_impl.f_call_sequence = 5;
} else {
self->private_impl.f_call_sequence = 255;
}
ok:
self->private_impl.p_skip_frame[0] = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_skip_frame[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
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
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
@@ -38825,6 +39664,7 @@ wuffs_png__decoder__decode_frame(
self->private_impl.active_coroutine = 0;
wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_seq_num = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
uint32_t v_pass_width = 0;
uint32_t v_pass_height = 0;
@@ -38844,7 +39684,10 @@ wuffs_png__decoder__decode_frame(
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_call_sequence < 4) {
if (self->private_impl.f_call_sequence == 255) {
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
} else if (self->private_impl.f_call_sequence != 4) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
@@ -38856,77 +39699,94 @@ wuffs_png__decoder__decode_frame(
if (status.repr) {
goto suspend;
}
} else if (self->private_impl.f_call_sequence == 4) {
} else {
status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
uint64_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = ((uint64_t)(wuffs_base__peek_u32be__no_bounds_check(iop_a_src)));
iop_a_src += 4;
} else {
self->private_data.s_decode_frame[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
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_frame[0].scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_0);
if (num_bits_0 == 24) {
t_0 = ((uint64_t)(*scratch >> 32));
break;
}
num_bits_0 += 8;
*scratch |= ((uint64_t)(num_bits_0));
while (true) {
while (((uint64_t)(io2_a_src - iop_a_src)) < 8) {
if (a_src && a_src->meta.closed) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2);
}
self->private_impl.f_chunk_length = t_0;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_frame[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
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_frame[0].scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1;
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch));
break;
}
num_bits_1 += 8;
*scratch |= ((uint64_t)(num_bits_1)) << 56;
self->private_impl.f_chunk_length = ((uint64_t)(wuffs_base__peek_u32be__no_bounds_check(iop_a_src)));
self->private_impl.f_chunk_type = ((uint32_t)((wuffs_base__peek_u64le__no_bounds_check(iop_a_src) >> 32)));
if (self->private_impl.f_chunk_type == 1413563465) {
self->private_impl.f_chunk_type_array[0] = 73;
self->private_impl.f_chunk_type_array[1] = 68;
self->private_impl.f_chunk_type_array[2] = 65;
self->private_impl.f_chunk_type_array[3] = 84;
iop_a_src += 8;
if ( ! self->private_impl.f_ignore_checksum) {
wuffs_base__ignore_status(wuffs_crc32__ieee_hasher__initialize(&self->private_data.f_crc32, sizeof (wuffs_crc32__ieee_hasher), WUFFS_VERSION, 0));
wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__make_slice_u8(self->private_impl.f_chunk_type_array, 4));
}
goto label__0__break;
} else if (self->private_impl.f_chunk_type == 1413571686) {
self->private_impl.f_chunk_type_array[0] = 102;
self->private_impl.f_chunk_type_array[1] = 100;
self->private_impl.f_chunk_type_array[2] = 65;
self->private_impl.f_chunk_type_array[3] = 84;
if (self->private_impl.f_chunk_length < 4) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
self->private_impl.f_chunk_length -= 4;
iop_a_src += 8;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
uint32_t t_0;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_0 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_frame[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
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_frame[0].scratch;
uint32_t num_bits_0 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_0);
if (num_bits_0 == 24) {
t_0 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_0 += 8;
*scratch |= ((uint64_t)(num_bits_0));
}
}
v_seq_num = t_0;
}
if (v_seq_num != self->private_impl.f_next_animation_seq_num) {
status = wuffs_base__make_status(wuffs_png__error__bad_animation_sequence_number);
goto exit;
} else if (self->private_impl.f_next_animation_seq_num >= 4294967295) {
status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file);
goto exit;
}
self->private_impl.f_next_animation_seq_num += 1;
goto label__0__break;
} else if (self->private_impl.f_chunk_type == 1280598886) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
self->private_impl.f_chunk_type = t_1;
}
if ( ! self->private_impl.f_ignore_checksum && (self->private_impl.f_chunk_type == 1413563465)) {
wuffs_base__ignore_status(wuffs_crc32__ieee_hasher__initialize(&self->private_data.f_crc32, sizeof (wuffs_crc32__ieee_hasher), WUFFS_VERSION, 0));
self->private_impl.f_chunk_type_array[0] = ((uint8_t)(((self->private_impl.f_chunk_type >> 0) & 255)));
self->private_impl.f_chunk_type_array[1] = ((uint8_t)(((self->private_impl.f_chunk_type >> 8) & 255)));
self->private_impl.f_chunk_type_array[2] = ((uint8_t)(((self->private_impl.f_chunk_type >> 16) & 255)));
self->private_impl.f_chunk_type_array[3] = ((uint8_t)(((self->private_impl.f_chunk_type >> 24) & 255)));
wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__make_slice_u8(self->private_impl.f_chunk_type_array, 4));
self->private_data.s_decode_frame[0].scratch = (self->private_impl.f_chunk_length + 12);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
if (self->private_data.s_decode_frame[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
self->private_data.s_decode_frame[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_frame[0].scratch;
self->private_impl.f_chunk_length = 0;
}
label__0__break:;
if (self->private_impl.f_zlib_is_dirty) {
wuffs_base__ignore_status(wuffs_zlib__decoder__initialize(&self->private_data.f_zlib, sizeof (wuffs_zlib__decoder), WUFFS_VERSION, 0));
if (self->private_impl.f_ignore_checksum) {
@@ -38952,8 +39812,13 @@ wuffs_png__decoder__decode_frame(
}
self->private_impl.f_workbuf_hist_pos_base = 0;
while (true) {
v_pass_width = (16777215 & ((((uint32_t)(WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][1])) + self->private_impl.f_width) >> WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][0]));
v_pass_height = (16777215 & ((((uint32_t)(WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][4])) + self->private_impl.f_height) >> WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][3]));
if (self->private_impl.f_chunk_type_array[0] == 73) {
v_pass_width = (16777215 & ((((uint32_t)(WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][1])) + self->private_impl.f_width) >> WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][0]));
v_pass_height = (16777215 & ((((uint32_t)(WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][4])) + self->private_impl.f_height) >> WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][3]));
} else {
v_pass_width = (16777215 & ((uint32_t)(self->private_impl.f_frame_rect_x1 - self->private_impl.f_frame_rect_x0)));
v_pass_height = (16777215 & ((uint32_t)(self->private_impl.f_frame_rect_y1 - self->private_impl.f_frame_rect_y0)));
}
if ((v_pass_width > 0) && (v_pass_height > 0)) {
self->private_impl.f_pass_bytes_per_row = wuffs_png__decoder__calculate_bytes_per_row(self, v_pass_width);
self->private_impl.f_pass_workbuf_length = (((uint64_t)(v_pass_height)) * (1 + self->private_impl.f_pass_bytes_per_row));
@@ -38982,7 +39847,7 @@ wuffs_png__decoder__decode_frame(
self->private_impl.f_workbuf_hist_pos_base += self->private_impl.f_pass_workbuf_length;
}
if ((self->private_impl.f_interlace_pass == 0) || (self->private_impl.f_interlace_pass >= 7)) {
goto label__0__break;
goto label__1__break;
}
#if defined(__GNUC__)
#pragma GCC diagnostic push
@@ -38993,8 +39858,13 @@ wuffs_png__decoder__decode_frame(
#pragma GCC diagnostic pop
#endif
}
label__0__break:;
self->private_impl.f_call_sequence = 255;
label__1__break:;
wuffs_base__u32__sat_add_indirect(&self->private_impl.f_num_decoded_frames_value, 1);
if (self->private_impl.f_num_decoded_frames_value < self->private_impl.f_num_animation_frames_value) {
self->private_impl.f_call_sequence = 5;
} else {
self->private_impl.f_call_sequence = 255;
}
ok:
self->private_impl.p_decode_frame[0] = 0;
@@ -39038,6 +39908,7 @@ wuffs_png__decoder__decode_pass(
wuffs_base__status v_zlib_status = wuffs_base__make_status(NULL);
uint32_t v_checksum_have = 0;
uint32_t v_checksum_want = 0;
uint32_t v_seq_num = 0;
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
@@ -39051,9 +39922,6 @@ wuffs_png__decoder__decode_pass(
}
uint32_t coro_susp_point = self->private_impl.p_decode_pass[0];
if (coro_susp_point) {
v_checksum_have = self->private_data.s_decode_pass[0].v_checksum_have;
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
@@ -39118,41 +39986,41 @@ wuffs_png__decoder__decode_pass(
io2_v_w = o_0_io2_v_w;
}
if (wuffs_base__status__is_ok(&v_zlib_status)) {
if ( ! self->private_impl.f_ignore_checksum) {
if (self->private_impl.f_chunk_length > 0) {
status = wuffs_base__make_status(wuffs_base__error__too_much_data);
goto exit;
}
v_checksum_have = wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__utility__empty_slice_u8());
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_pass[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
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_pass[0].scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_1);
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_1 += 8;
*scratch |= ((uint64_t)(num_bits_1));
if (self->private_impl.f_chunk_length > 0) {
status = wuffs_base__make_status(wuffs_base__error__too_much_data);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
uint32_t t_1;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_1 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_pass[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
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_pass[0].scratch;
uint32_t num_bits_1 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_1);
if (num_bits_1 == 24) {
t_1 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_1 += 8;
*scratch |= ((uint64_t)(num_bits_1));
}
v_checksum_want = t_1;
}
v_checksum_want = t_1;
}
if ( ! self->private_impl.f_ignore_checksum && (self->private_impl.f_chunk_type_array[0] == 73)) {
v_checksum_have = wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__utility__empty_slice_u8());
if (v_checksum_have != v_checksum_want) {
status = wuffs_base__make_status(wuffs_png__error__bad_checksum);
goto exit;
@@ -39204,7 +40072,7 @@ wuffs_png__decoder__decode_pass(
}
v_checksum_want = t_2;
}
if ( ! self->private_impl.f_ignore_checksum) {
if ( ! self->private_impl.f_ignore_checksum && (self->private_impl.f_chunk_type_array[0] == 73)) {
v_checksum_have = wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__utility__empty_slice_u8());
if (v_checksum_have != v_checksum_want) {
status = wuffs_base__make_status(wuffs_png__error__bad_checksum);
@@ -39269,17 +40137,58 @@ wuffs_png__decoder__decode_pass(
}
self->private_impl.f_chunk_type = t_4;
}
if (self->private_impl.f_chunk_type != 1413563465) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
if ( ! self->private_impl.f_ignore_checksum) {
wuffs_base__ignore_status(wuffs_crc32__ieee_hasher__initialize(&self->private_data.f_crc32, sizeof (wuffs_crc32__ieee_hasher), WUFFS_VERSION, 0));
self->private_impl.f_chunk_type_array[0] = 73;
self->private_impl.f_chunk_type_array[1] = 68;
self->private_impl.f_chunk_type_array[2] = 65;
self->private_impl.f_chunk_type_array[3] = 84;
wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__make_slice_u8(self->private_impl.f_chunk_type_array, 4));
if (self->private_impl.f_chunk_type_array[0] == 73) {
if (self->private_impl.f_chunk_type != 1413563465) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
if ( ! self->private_impl.f_ignore_checksum) {
wuffs_base__ignore_status(wuffs_crc32__ieee_hasher__initialize(&self->private_data.f_crc32, sizeof (wuffs_crc32__ieee_hasher), WUFFS_VERSION, 0));
wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__make_slice_u8(self->private_impl.f_chunk_type_array, 4));
}
} else {
if ((self->private_impl.f_chunk_type != 1413571686) || (self->private_impl.f_chunk_length < 4)) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
goto exit;
}
self->private_impl.f_chunk_length -= 4;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9);
uint32_t t_5;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_5 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4;
} else {
self->private_data.s_decode_pass[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10);
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_pass[0].scratch;
uint32_t num_bits_5 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_5);
if (num_bits_5 == 24) {
t_5 = ((uint32_t)(*scratch >> 32));
break;
}
num_bits_5 += 8;
*scratch |= ((uint64_t)(num_bits_5));
}
}
v_seq_num = t_5;
}
if (v_seq_num != self->private_impl.f_next_animation_seq_num) {
status = wuffs_base__make_status(wuffs_png__error__bad_animation_sequence_number);
goto exit;
} else if (self->private_impl.f_next_animation_seq_num >= 4294967295) {
status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file);
goto exit;
}
self->private_impl.f_next_animation_seq_num += 1;
}
goto label__0__continue;
} else if (((uint64_t)(io2_a_src - iop_a_src)) > 0) {
@@ -39287,7 +40196,7 @@ wuffs_png__decoder__decode_pass(
goto exit;
}
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(9);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(11);
}
label__0__break:;
if (self->private_impl.f_workbuf_wi != self->private_impl.f_pass_workbuf_length) {
@@ -39307,7 +40216,6 @@ wuffs_png__decoder__decode_pass(
goto suspend;
suspend:
self->private_impl.p_decode_pass[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_data.s_decode_pass[0].v_checksum_have = v_checksum_have;
goto exit;
exit:
@@ -39351,7 +40259,7 @@ wuffs_png__decoder__num_animation_loops(
return 0;
}
return 0;
return self->private_impl.f_num_animation_loops_value;
}
// -------- func png.decoder.num_decoded_frame_configs
@@ -39367,10 +40275,7 @@ wuffs_png__decoder__num_decoded_frame_configs(
return 0;
}
if (self->private_impl.f_call_sequence > 3) {
return 1;
}
return 0;
return ((uint64_t)(self->private_impl.f_num_decoded_frame_configs_value));
}
// -------- func png.decoder.num_decoded_frames
@@ -39386,10 +40291,7 @@ wuffs_png__decoder__num_decoded_frames(
return 0;
}
if (self->private_impl.f_call_sequence > 4) {
return 1;
}
return 0;
return ((uint64_t)(self->private_impl.f_num_decoded_frames_value));
}
// -------- func png.decoder.restart_frame
@@ -39411,8 +40313,7 @@ wuffs_png__decoder__restart_frame(
if (self->private_impl.f_call_sequence < 3) {
return wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
}
if (a_index != 0) {
} else if ((a_index >= ((uint64_t)(self->private_impl.f_num_animation_frames_value))) || ((a_index == 0) && (a_io_position != self->private_impl.f_first_config_io_position))) {
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
self->private_impl.f_call_sequence = 3;
@@ -39420,6 +40321,8 @@ wuffs_png__decoder__restart_frame(
self->private_impl.f_interlace_pass = 1;
}
self->private_impl.f_frame_config_io_position = a_io_position;
self->private_impl.f_num_decoded_frame_configs_value = ((uint32_t)((a_index & 4294967295)));
self->private_impl.f_num_decoded_frames_value = self->private_impl.f_num_decoded_frame_configs_value;
return wuffs_base__make_status(NULL);
}
@@ -39643,7 +40546,8 @@ wuffs_png__decoder__filter_and_swizzle__choosy_default(
wuffs_base__pixel_format v_dst_pixfmt = {0};
uint32_t v_dst_bits_per_pixel = 0;
uint64_t v_dst_bytes_per_pixel = 0;
uint64_t v_dst_bytes_per_row = 0;
uint64_t v_dst_bytes_per_row0 = 0;
uint64_t v_dst_bytes_per_row1 = 0;
wuffs_base__slice_u8 v_dst_palette = {0};
wuffs_base__table_u8 v_tab = {0};
uint32_t v_y = 0;
@@ -39658,13 +40562,20 @@ wuffs_png__decoder__filter_and_swizzle__choosy_default(
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
v_dst_bytes_per_pixel = ((uint64_t)((v_dst_bits_per_pixel / 8)));
v_dst_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * v_dst_bytes_per_pixel);
v_dst_bytes_per_row0 = (((uint64_t)(self->private_impl.f_frame_rect_x0)) * v_dst_bytes_per_pixel);
v_dst_bytes_per_row1 = (((uint64_t)(self->private_impl.f_frame_rect_x1)) * v_dst_bytes_per_pixel);
v_dst_palette = wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024));
v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0);
while (v_y < self->private_impl.f_height) {
v_y = self->private_impl.f_frame_rect_y0;
while (v_y < self->private_impl.f_frame_rect_y1) {
v_dst = wuffs_base__table_u8__row(v_tab, v_y);
if (v_dst_bytes_per_row < ((uint64_t)(v_dst.len))) {
v_dst = wuffs_base__slice_u8__subslice_j(v_dst, v_dst_bytes_per_row);
if (v_dst_bytes_per_row1 < ((uint64_t)(v_dst.len))) {
v_dst = wuffs_base__slice_u8__subslice_j(v_dst, v_dst_bytes_per_row1);
}
if (v_dst_bytes_per_row0 < ((uint64_t)(v_dst.len))) {
v_dst = wuffs_base__slice_u8__subslice_i(v_dst, v_dst_bytes_per_row0);
} else {
v_dst = wuffs_base__utility__empty_slice_u8();
}
if (1 > ((uint64_t)(a_workbuf.len))) {
return wuffs_base__make_status(wuffs_png__error__internal_error_inconsistent_workbuf_length);
@@ -39705,7 +40616,7 @@ wuffs_png__decoder__filter_and_swizzle_tricky(
wuffs_base__pixel_format v_dst_pixfmt = {0};
uint32_t v_dst_bits_per_pixel = 0;
uint64_t v_dst_bytes_per_pixel = 0;
uint64_t v_dst_bytes_per_row = 0;
uint64_t v_dst_bytes_per_row1 = 0;
wuffs_base__slice_u8 v_dst_palette = {0};
wuffs_base__table_u8 v_tab = {0};
uint64_t v_src_bytes_per_pixel = 0;
@@ -39729,18 +40640,22 @@ wuffs_png__decoder__filter_and_swizzle_tricky(
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
v_dst_bytes_per_pixel = ((uint64_t)((v_dst_bits_per_pixel / 8)));
v_dst_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * v_dst_bytes_per_pixel);
v_dst_bytes_per_row1 = (((uint64_t)(self->private_impl.f_frame_rect_x1)) * v_dst_bytes_per_pixel);
v_dst_palette = wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024));
v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0);
v_src_bytes_per_pixel = 1;
if (self->private_impl.f_depth >= 8) {
v_src_bytes_per_pixel = (((uint64_t)(WUFFS_PNG__NUM_CHANNELS[self->private_impl.f_color_type])) * ((uint64_t)((self->private_impl.f_depth >> 3))));
}
v_y = ((uint32_t)(WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][5]));
while (v_y < self->private_impl.f_height) {
if (self->private_impl.f_chunk_type_array[0] == 73) {
v_y = ((uint32_t)(WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][5]));
} else {
v_y = self->private_impl.f_frame_rect_y0;
}
while (v_y < self->private_impl.f_frame_rect_y1) {
v_dst = wuffs_base__table_u8__row(v_tab, v_y);
if (v_dst_bytes_per_row < ((uint64_t)(v_dst.len))) {
v_dst = wuffs_base__slice_u8__subslice_j(v_dst, v_dst_bytes_per_row);
if (v_dst_bytes_per_row1 < ((uint64_t)(v_dst.len))) {
v_dst = wuffs_base__slice_u8__subslice_j(v_dst, v_dst_bytes_per_row1);
}
if (1 > ((uint64_t)(a_workbuf.len))) {
return wuffs_base__make_status(wuffs_png__error__internal_error_inconsistent_workbuf_length);
@@ -39765,9 +40680,13 @@ wuffs_png__decoder__filter_and_swizzle_tricky(
return wuffs_base__make_status(wuffs_png__error__bad_filter);
}
v_s = v_curr_row;
v_x = ((uint32_t)(WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][2]));
if (self->private_impl.f_chunk_type_array[0] == 73) {
v_x = ((uint32_t)(WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][2]));
} else {
v_x = self->private_impl.f_frame_rect_x0;
}
if (self->private_impl.f_depth == 8) {
while (v_x < self->private_impl.f_width) {
while (v_x < self->private_impl.f_frame_rect_x1) {
v_i = (((uint64_t)(v_x)) * v_dst_bytes_per_pixel);
if (v_i <= ((uint64_t)(v_dst.len))) {
if (self->private_impl.f_color_type == 4) {
@@ -39831,7 +40750,7 @@ wuffs_png__decoder__filter_and_swizzle_tricky(
}
v_shift = ((8 - self->private_impl.f_depth) & 7);
v_packs_remaining = 0;
while (v_x < self->private_impl.f_width) {
while (v_x < self->private_impl.f_frame_rect_x1) {
v_i = (((uint64_t)(v_x)) * v_dst_bytes_per_pixel);
if (v_i <= ((uint64_t)(v_dst.len))) {
if ((v_packs_remaining == 0) && (1 <= ((uint64_t)(v_s.len)))) {
@@ -39863,7 +40782,7 @@ wuffs_png__decoder__filter_and_swizzle_tricky(
v_x += (((uint32_t)(1)) << WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][0]);
}
} else {
while (v_x < self->private_impl.f_width) {
while (v_x < self->private_impl.f_frame_rect_x1) {
v_i = (((uint64_t)(v_x)) * v_dst_bytes_per_pixel);
if (v_i <= ((uint64_t)(v_dst.len))) {
if (self->private_impl.f_color_type == 0) {
+424 -76
View File
@@ -15,6 +15,7 @@
use "std/crc32"
use "std/zlib"
pub status "#bad animation sequence number"
pub status "#bad checksum"
pub status "#bad chunk"
pub status "#bad filter"
@@ -22,11 +23,16 @@ pub status "#bad header"
pub status "#missing palette"
pub status "#unsupported PNG file"
pri status "#internal error: inconsistent frame bounds"
pri status "#internal error: inconsistent workbuf length"
pri status "#internal error: zlib decoder did not exhaust its input"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
// DECODER_SRC_IO_BUFFER_LENGTH_MIN_INCL is the minimum length of the src
// wuffs_base__io_buffer passed to the decoder.
pub const DECODER_SRC_IO_BUFFER_LENGTH_MIN_INCL : base.u64 = 8
pub struct decoder? implements base.image_decoder(
// The 0x00FF_FFFF limit is arbitrary (the PNG spec says 0x7FFF_FFFF) but
// it means that (width * height * bytes_per_pixel) doesn't overflow.
@@ -47,13 +53,15 @@ pub struct decoder? implements base.image_decoder(
// - 0x02: metadata finished; image config decode is in progress.
// - 0x03: image config decoded.
// - 0x04: frame config decoded.
// - 0xFF: end-of-data, usually after (the non-animated) frame decoded.
// - 0x05: frame decoded.
// - 0xFF: end-of-data, after the final frame decoded.
//
// State transitions:
//
// - 0x00 -> 0x01: via DIC (metadata reported)
// - 0x00 -> 0x03: via DIC (metadata not reported)
// - 0x00 -> 0x04: via DFC with implicit DIC
// - 0x00 -> 0x05: via DF with implicit DIC and DFC
// - 0x00 -> 0xFF: via DF with implicit DIC and DFC
//
// - 0x01 -> 0x02: via TMM
@@ -62,15 +70,22 @@ pub struct decoder? implements base.image_decoder(
// - 0x02 -> 0x03: via DIC (metadata not reported)
//
// - 0x03 -> 0x04: via DFC
// - 0x03 -> 0x05: via DF with implicit DFC
// - 0x03 -> 0xFF: via DF with implicit DFC
//
// - 0x04 -> 0xFF: via DFC
// - 0x04 -> 0x04: via DFC with implicit DF
// - 0x04 -> 0x05: via DF
// - 0x04 -> 0xFF: via DFC with implicit DF
// - 0x04 -> 0xFF: via DF
//
// - 0x05 -> 0x04: via DFC
// - 0x05 -> 0x05: via DF with implicit DFC
// - 0x05 -> 0xFF: via DF with implicit DFC
//
// - ???? -> 0x03: via RF for ???? > 0x00
//
// Where:
// - DF is decode_frame
// - DF is decode_frame, implicit means skip_frame
// - DFC is decode_frame_config, implicit means nullptr args.dst
// - DIC is decode_image_config, implicit means nullptr args.dst
// - RF is restart_frame
@@ -89,7 +104,9 @@ pub struct decoder? implements base.image_decoder(
filter_distance : base.u8[..= 8],
interlace_pass : base.u8[..= 7],
seen_actl : base.bool,
seen_chrm : base.bool,
seen_fctl : base.bool,
seen_gama : base.bool,
seen_iccp : base.bool,
seen_plte : base.bool,
@@ -114,9 +131,48 @@ pub struct decoder? implements base.image_decoder(
chunk_type : base.u32,
chunk_type_array : array[4] base.u8,
chunk_length : base.u64,
chunk_length : base.u64[..= 0xFFFF_FFFF],
frame_config_io_position : base.u64,
num_animation_frames_value : base.u32,
num_animation_loops_value : base.u32,
num_decoded_frame_configs_value : base.u32,
num_decoded_frames_value : base.u32,
// The frame_etc fields correspond to the base.frame_config argument passed
// to decode_frame_config. For animated APNG, these are explicitly given in
// the file's fcTL chunks. For still PNG, these are implicitly given in the
// file's IHDR chunk.
//
// Either way, decode_image_config has to read all the way to the start of
// the first IDAT / fdAT chunk (e.g. to see if there's a PLTE chunk). While
// later frame's restart io_positions are located just before fcTL chunks,
// the first frame's restart io_position is located just before the IDAT /
// fdAT chunk, so we also have to cache the first frame's configuration as
// we cannot re-read the first fcTL. Hence, there are first_etc fields for
// every frame_etc field.
//
// For the etc_duration fields, there are 705_600000 flicks per second and
// the maximum frame duration is 65535 seconds.
//
// The fields are ordered to minimize alignment wastage.
frame_rect_x0 : base.u32[..= 0x00FF_FFFF],
frame_rect_y0 : base.u32[..= 0x00FF_FFFF],
frame_rect_x1 : base.u32[..= 0x00FF_FFFF],
frame_rect_y1 : base.u32[..= 0x00FF_FFFF],
first_rect_x0 : base.u32[..= 0x00FF_FFFF],
first_rect_y0 : base.u32[..= 0x00FF_FFFF],
first_rect_x1 : base.u32[..= 0x00FF_FFFF],
first_rect_y1 : base.u32[..= 0x00FF_FFFF],
frame_config_io_position : base.u64,
first_config_io_position : base.u64,
frame_duration : base.u64[..= 0x2A0E_6FF1_6600],
first_duration : base.u64[..= 0x2A0E_6FF1_6600],
frame_disposal : base.u8,
first_disposal : base.u8,
frame_overwrite_instead_of_blend : base.bool,
first_overwrite_instead_of_blend : base.bool,
next_animation_seq_num : base.u32,
metadata_flavor : base.u32,
metadata_fourcc : base.u32,
@@ -212,16 +268,21 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
this.chunk_length = args.src.peek_u32be_as_u64()
this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32
if this.chunk_type == 'IDAT'le {
// TODO: support APNG images whose first frame is not the
// IDAT-fallback frame.
if this.seen_actl and (not this.seen_fctl) {
return "#unsupported PNG file"
}
break
}
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
if (not this.ignore_checksum) and (this.chunk_type == 'PLTE'le) {
this.crc32.reset!()
this.chunk_type_array[0] = ((this.chunk_type >> 0) & 0xFF) as base.u8
this.chunk_type_array[1] = ((this.chunk_type >> 8) & 0xFF) as base.u8
this.chunk_type_array[2] = ((this.chunk_type >> 16) & 0xFF) as base.u8
this.chunk_type_array[3] = ((this.chunk_type >> 24) & 0xFF) as base.u8
this.chunk_type_array[0] = 'P'
this.chunk_type_array[1] = 'L'
this.chunk_type_array[2] = 'T'
this.chunk_type_array[3] = 'E'
this.crc32.update_u32!(x: this.chunk_type_array[..])
}
@@ -256,6 +317,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
}
this.frame_config_io_position = args.src.position()
this.first_config_io_position = this.frame_config_io_position
if args.dst <> nullptr {
args.dst.set!(
@@ -263,10 +325,23 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
pixsub: 0,
width: this.width,
height: this.height,
first_frame_io_position: this.frame_config_io_position,
first_frame_io_position: this.first_config_io_position,
first_frame_is_opaque: (this.color_type <= 3) and (not this.seen_trns))
}
// For still (non-animated) PNGs, the first and only frame's configuration
// is implied by the IHDR chunk instead of an explicit fcTL chunk.
if not this.seen_actl {
this.num_animation_frames_value = 1
this.first_rect_x0 = 0
this.first_rect_y0 = 0
this.first_rect_x1 = this.width
this.first_rect_y1 = this.height
this.first_duration = 0
this.first_disposal = base.ANIMATION_DISPOSAL__NONE
this.first_overwrite_instead_of_blend = false
}
this.call_sequence = 3
}
@@ -461,6 +536,13 @@ pri func decoder.decode_other_chunk?(src: base.io_reader) {
this.decode_plte?(src: args.src)
this.seen_plte = true
} else if this.chunk_type == 'acTL'le {
if this.seen_actl {
return "#bad chunk"
}
this.decode_actl?(src: args.src)
this.seen_actl = true
} else if this.chunk_type == 'cHRM'le {
if this.report_metadata_chrm {
if this.seen_chrm {
@@ -470,6 +552,16 @@ pri func decoder.decode_other_chunk?(src: base.io_reader) {
this.seen_chrm = true
}
} else if this.chunk_type == 'fcTL'le {
if this.seen_fctl {
return "#bad chunk"
}
this.decode_fctl?(src: args.src)
this.seen_fctl = true
} else if this.chunk_type == 'fdAT'le {
return "#bad chunk"
} else if this.chunk_type == 'gAMA'le {
if this.report_metadata_gama {
if this.seen_gama {
@@ -511,6 +603,23 @@ pri func decoder.decode_other_chunk?(src: base.io_reader) {
}
}
pri func decoder.decode_actl?(src: base.io_reader) {
if this.chunk_length <> 8 {
return "#bad chunk"
} else if this.interlace_pass > 0 {
// https://wiki.mozilla.org/APNG_Specification doesn't say whether the
// interlace pattern starts at the image or frame top-left corner. We
// avoid the question by returning "unsupported" for now.
return "#unsupported PNG file"
}
this.chunk_length = 0
this.num_animation_frames_value = args.src.read_u32be?()
if this.num_animation_frames_value == 0 {
return "#bad chunk"
}
this.num_animation_loops_value = args.src.read_u32be?()
}
pri func decoder.decode_chrm?(src: base.io_reader) {
var u : base.u64
@@ -548,6 +657,85 @@ pri func decoder.decode_chrm?(src: base.io_reader) {
this.metadata_z |= (0xFF_FFFF & u) << 40
}
pri func decoder.decode_fctl?(src: base.io_reader) {
var x0 : base.u32
var y0 : base.u32
var x1 : base.u32
var y1 : base.u32
if this.chunk_length <> 26 {
return "#bad chunk"
}
this.chunk_length = 0
x0 = args.src.read_u32be?()
if x0 <> this.next_animation_seq_num {
return "#bad animation sequence number"
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
return "#unsupported PNG file"
}
this.next_animation_seq_num += 1
x1 = args.src.read_u32be?()
y1 = args.src.read_u32be?()
x0 = args.src.read_u32be?()
y0 = args.src.read_u32be?()
x1 ~mod+= x0
y1 ~mod+= y0
if (x0 >= x1) or (x0 > this.width) or (x1 > this.width) or
(y0 >= y1) or (y0 > this.height) or (y1 > this.height) {
return "#bad chunk"
}
assert x1 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: this.width)
assert y1 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: this.height)
assert x0 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: x1)
assert y0 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: y1)
this.frame_rect_x0 = x0
this.frame_rect_y0 = y0
this.frame_rect_x1 = x1
this.frame_rect_y1 = y1
// There are 705_600000 flicks per second. A nominally zero denominator
// means to use 100 instead (the units are centiseconds, the same as GIF).
x0 = args.src.read_u16be_as_u32?()
x1 = args.src.read_u16be_as_u32?()
if x1 <= 0 {
this.frame_duration = (x0 as base.u64) * 7_056000
} else {
this.frame_duration = ((x0 as base.u64) * 705_600000) / (x1 as base.u64)
}
x0 = args.src.read_u8_as_u32?()
if x0 == 0 {
this.frame_disposal = base.ANIMATION_DISPOSAL__NONE
} else if x0 == 1 {
this.frame_disposal = base.ANIMATION_DISPOSAL__RESTORE_BACKGROUND
} else if x0 == 2 {
this.frame_disposal = base.ANIMATION_DISPOSAL__RESTORE_PREVIOUS
} else {
return "#bad chunk"
}
x0 = args.src.read_u8_as_u32?()
if x0 == 0 {
this.frame_overwrite_instead_of_blend = true
} else if x0 == 1 {
this.frame_overwrite_instead_of_blend = false
} else {
return "#bad chunk"
}
if this.num_decoded_frame_configs_value == 0 {
this.first_rect_x0 = this.frame_rect_x0
this.first_rect_y0 = this.frame_rect_y0
this.first_rect_x1 = this.frame_rect_x1
this.first_rect_y1 = this.frame_rect_y1
this.first_duration = this.frame_duration
this.first_disposal = this.frame_disposal
this.first_overwrite_instead_of_blend = this.frame_overwrite_instead_of_blend
}
}
pri func decoder.decode_gama?(src: base.io_reader) {
if this.chunk_length <> 4 {
return "#bad chunk"
@@ -715,60 +903,204 @@ pri func decoder.decode_trns?(src: base.io_reader) {
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
if this.call_sequence < 3 {
if this.call_sequence == 0xFF {
return base."@end of data"
} else if this.call_sequence < 3 {
this.decode_image_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 3 {
if this.frame_config_io_position <> args.src.position() {
return base."#bad restart"
}
} else if this.call_sequence == 4 {
this.call_sequence = 0xFF
return base."@end of data"
this.skip_frame?(src: args.src)
if this.call_sequence == 0xFF {
return base."@end of data"
}
}
if this.num_decoded_frame_configs_value == 0 {
this.frame_rect_x0 = this.first_rect_x0
this.frame_rect_y0 = this.first_rect_y0
this.frame_rect_x1 = this.first_rect_x1
this.frame_rect_y1 = this.first_rect_y1
this.frame_config_io_position = this.first_config_io_position
this.frame_duration = this.first_duration
this.frame_disposal = this.first_disposal
this.frame_overwrite_instead_of_blend = this.first_overwrite_instead_of_blend
} else {
return base."@end of data"
// Decode the next fcTL chunk.
while true {
this.chunk_length = args.src.read_u32be_as_u64?()
this.chunk_type = args.src.read_u32le?()
if this.chunk_type == 'fdAT'le {
return "#bad chunk"
} else if this.chunk_type == 'fcTL'le {
this.frame_config_io_position = args.src.position() ~mod- 8
this.decode_fctl?(src: args.src)
args.src.skip?(n: 4) // Skip the checksum.
break
}
args.src.skip?(n: this.chunk_length + 4) // +4 for the checksum.
this.chunk_length = 0
} endwhile
}
if args.dst <> nullptr {
args.dst.set!(bounds: this.util.make_rect_ie_u32(
min_incl_x: 0,
min_incl_y: 0,
max_excl_x: this.width,
max_excl_y: this.height),
duration: 0,
index: 0,
min_incl_x: this.frame_rect_x0,
min_incl_y: this.frame_rect_y0,
max_excl_x: this.frame_rect_x1,
max_excl_y: this.frame_rect_y1),
duration: this.frame_duration,
index: this.num_decoded_frame_configs_value as base.u64,
io_position: this.frame_config_io_position,
disposal: 0,
opaque_within_bounds: false,
overwrite_instead_of_blend: false,
disposal: this.frame_disposal,
opaque_within_bounds: (this.color_type <= 3) and (not this.seen_trns),
overwrite_instead_of_blend: this.frame_overwrite_instead_of_blend,
background_color: 0x0000_0000)
}
this.num_decoded_frame_configs_value ~sat+= 1
this.call_sequence = 4
}
pri func decoder.skip_frame?(src: base.io_reader) {
var seq_num : base.u32
this.chunk_type_array[0] = 0
this.chunk_type_array[1] = 0
this.chunk_type_array[2] = 0
this.chunk_type_array[3] = 0
while true {
while args.src.length() < 8,
post args.src.length() >= 8,
{
if args.src.is_closed() {
return "#bad chunk"
}
yield? base."$short read"
} endwhile
this.chunk_length = args.src.peek_u32be_as_u64()
this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32
if this.chunk_type == 'IDAT'le {
if this.chunk_type_array[0] == 'f' {
return "#bad chunk"
}
this.chunk_type_array[0] = 'I'
this.chunk_type_array[1] = 'D'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
} else if this.chunk_type == 'fdAT'le {
if this.chunk_type_array[0] == 'I' {
return "#bad chunk"
}
this.chunk_type_array[0] = 'f'
this.chunk_type_array[1] = 'd'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
if this.chunk_length < 4 {
return "#bad chunk"
}
this.chunk_length -= 4
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
seq_num = args.src.read_u32be?()
if seq_num <> this.next_animation_seq_num {
return "#bad animation sequence number"
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
return "#unsupported PNG file"
}
this.next_animation_seq_num += 1
args.src.skip?(n: this.chunk_length + 4) // +4 for the checksum.
this.chunk_length = 0
continue
} else if this.chunk_type_array[0] <> 0 {
break
} else if this.chunk_type == 'fcTL'le {
return "#bad chunk"
}
args.src.skip?(n: this.chunk_length + 12) // +12 for chunk length, type and checksum.
this.chunk_length = 0
} endwhile
this.num_decoded_frames_value ~sat+= 1
if this.num_decoded_frames_value < this.num_animation_frames_value {
this.call_sequence = 5
} else {
this.call_sequence = 0xFF
}
}
pub func decoder.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 seq_num : base.u32
var status : base.status
var pass_width : base.u32[..= 0x00FF_FFFF]
var pass_height : base.u32[..= 0x00FF_FFFF]
if this.call_sequence < 4 {
this.decode_frame_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 4 {
// No-op.
} else {
if this.call_sequence == 0xFF {
return base."@end of data"
} else if this.call_sequence <> 4 {
this.decode_frame_config?(dst: nullptr, src: args.src)
}
this.chunk_length = args.src.read_u32be_as_u64?()
this.chunk_type = args.src.read_u32le?()
if (not this.ignore_checksum) and (this.chunk_type == 'IDAT'le) {
this.crc32.reset!()
this.chunk_type_array[0] = ((this.chunk_type >> 0) & 0xFF) as base.u8
this.chunk_type_array[1] = ((this.chunk_type >> 8) & 0xFF) as base.u8
this.chunk_type_array[2] = ((this.chunk_type >> 16) & 0xFF) as base.u8
this.chunk_type_array[3] = ((this.chunk_type >> 24) & 0xFF) as base.u8
this.crc32.update_u32!(x: this.chunk_type_array[..])
}
while true {
while args.src.length() < 8,
post args.src.length() >= 8,
{
if args.src.is_closed() {
return "#bad chunk"
}
yield? base."$short read"
} endwhile
this.chunk_length = args.src.peek_u32be_as_u64()
this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32
if this.chunk_type == 'IDAT'le {
this.chunk_type_array[0] = 'I'
this.chunk_type_array[1] = 'D'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
if not this.ignore_checksum {
this.crc32.reset!()
this.crc32.update_u32!(x: this.chunk_type_array[..])
}
break
} else if this.chunk_type == 'fdAT'le {
this.chunk_type_array[0] = 'f'
this.chunk_type_array[1] = 'd'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
if this.chunk_length < 4 {
return "#bad chunk"
}
this.chunk_length -= 4
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
seq_num = args.src.read_u32be?()
if seq_num <> this.next_animation_seq_num {
return "#bad animation sequence number"
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
return "#unsupported PNG file"
}
this.next_animation_seq_num += 1
break
} else if this.chunk_type == 'fcTL'le {
return "#bad chunk"
}
args.src.skip?(n: this.chunk_length + 12) // +12 for chunk length, type and checksum.
this.chunk_length = 0
} endwhile
if this.zlib_is_dirty {
this.zlib.reset!()
@@ -790,12 +1122,17 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
this.workbuf_hist_pos_base = 0
while true {
pass_width = 0x00FF_FFFF &
(((INTERLACING[this.interlace_pass][1] as base.u32) + this.width) >>
INTERLACING[this.interlace_pass][0])
pass_height = 0x00FF_FFFF &
(((INTERLACING[this.interlace_pass][4] as base.u32) + this.height) >>
INTERLACING[this.interlace_pass][3])
if (this.chunk_type_array[0] == 'I') {
pass_width = 0x00FF_FFFF &
(((INTERLACING[this.interlace_pass][1] as base.u32) + this.width) >>
INTERLACING[this.interlace_pass][0])
pass_height = 0x00FF_FFFF &
(((INTERLACING[this.interlace_pass][4] as base.u32) + this.height) >>
INTERLACING[this.interlace_pass][3])
} else {
pass_width = 0x00FF_FFFF & (this.frame_rect_x1 ~mod- this.frame_rect_x0)
pass_height = 0x00FF_FFFF & (this.frame_rect_y1 ~mod- this.frame_rect_y0)
}
if (pass_width > 0) and (pass_height > 0) {
this.pass_bytes_per_row = this.calculate_bytes_per_row(width: pass_width)
@@ -814,7 +1151,12 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
this.interlace_pass += 1
} endwhile
this.call_sequence = 0xFF
this.num_decoded_frames_value ~sat+= 1
if this.num_decoded_frames_value < this.num_animation_frames_value {
this.call_sequence = 5
} else {
this.call_sequence = 0xFF
}
}
pri func decoder.decode_pass?(src: base.io_reader, workbuf: slice base.u8) {
@@ -824,6 +1166,7 @@ pri func decoder.decode_pass?(src: base.io_reader, workbuf: slice base.u8) {
var zlib_status : base.status
var checksum_have : base.u32
var checksum_want : base.u32
var seq_num : base.u32
this.workbuf_wi = 0
while true {
@@ -846,14 +1189,14 @@ pri func decoder.decode_pass?(src: base.io_reader, workbuf: slice base.u8) {
}
if zlib_status.is_ok() {
if this.chunk_length > 0 {
// TODO: should this really be a fatal error?
return base."#too much data"
}
checksum_want = args.src.read_u32be?()
// Verify the final IDAT chunk's CRC-32 checksum.
if not this.ignore_checksum {
if this.chunk_length > 0 {
// TODO: should this really be a fatal error?
return base."#too much data"
}
if (not this.ignore_checksum) and (this.chunk_type_array[0] == 'I') {
checksum_have = this.crc32.update_u32!(x: this.util.empty_slice_u8())
checksum_want = args.src.read_u32be?()
if checksum_have <> checksum_want {
return "#bad checksum"
}
@@ -869,28 +1212,37 @@ pri func decoder.decode_pass?(src: base.io_reader, workbuf: slice base.u8) {
} else if this.chunk_length == 0 {
// Verify the non-final IDAT chunk's CRC-32 checksum.
checksum_want = args.src.read_u32be?()
if not this.ignore_checksum {
if (not this.ignore_checksum) and (this.chunk_type_array[0] == 'I') {
checksum_have = this.crc32.update_u32!(x: this.util.empty_slice_u8())
if checksum_have <> checksum_want {
return "#bad checksum"
}
}
// The next chunk should be another IDAT.
// The next chunk should be another IDAT or fdAT.
this.chunk_length = args.src.read_u32be_as_u64?()
this.chunk_type = args.src.read_u32le?()
if this.chunk_type <> 'IDAT'le {
return "#bad chunk"
}
// The 'IDAT'be is part of the next CRC-32 checksum's input.
if not this.ignore_checksum {
this.crc32.reset!()
this.chunk_type_array[0] = 'I'
this.chunk_type_array[1] = 'D'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
this.crc32.update_u32!(x: this.chunk_type_array[..])
if (this.chunk_type_array[0] == 'I') {
if this.chunk_type <> 'IDAT'le {
return "#bad chunk"
}
// The IDAT is part of the next CRC-32 checksum's input.
if not this.ignore_checksum {
this.crc32.reset!()
this.crc32.update_u32!(x: this.chunk_type_array[..])
}
} else {
if (this.chunk_type <> 'fdAT'le) or (this.chunk_length < 4) {
return "#bad chunk"
}
this.chunk_length -= 4
seq_num = args.src.read_u32be?()
if seq_num <> this.next_animation_seq_num {
return "#bad animation sequence number"
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
return "#unsupported PNG file"
}
this.next_animation_seq_num += 1
}
continue
} else if args.src.length() > 0 {
@@ -920,28 +1272,22 @@ pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
}
pub func decoder.num_animation_loops() base.u32 {
return 0
return this.num_animation_loops_value
}
pub func decoder.num_decoded_frame_configs() base.u64 {
if this.call_sequence > 3 {
return 1
}
return 0
return this.num_decoded_frame_configs_value as base.u64
}
pub func decoder.num_decoded_frames() base.u64 {
if this.call_sequence > 4 {
return 1
}
return 0
return this.num_decoded_frames_value as base.u64
}
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
if this.call_sequence < 3 {
return base."#bad call sequence"
}
if args.index <> 0 {
} else if (args.index >= (this.num_animation_frames_value as base.u64)) or
((args.index == 0) and (args.io_position <> this.first_config_io_position)) {
return base."#bad argument"
}
this.call_sequence = 3
@@ -949,6 +1295,8 @@ pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.sta
this.interlace_pass = 1
}
this.frame_config_io_position = args.io_position
this.num_decoded_frame_configs_value = (args.index & 0xFFFF_FFFF) as base.u32
this.num_decoded_frames_value = this.num_decoded_frame_configs_value
return ok
}
+14 -6
View File
@@ -18,7 +18,8 @@ pri func decoder.filter_and_swizzle!(dst: ptr base.pixel_buffer, workbuf: slice
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 dst_bytes_per_row0 : base.u64
var dst_bytes_per_row1 : base.u64
var dst_palette : slice base.u8
var tab : table base.u8
@@ -36,15 +37,22 @@ pri func decoder.filter_and_swizzle!(dst: ptr base.pixel_buffer, workbuf: slice
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
dst_bytes_per_row0 = (this.frame_rect_x0 as base.u64) * dst_bytes_per_pixel
dst_bytes_per_row1 = (this.frame_rect_x1 as base.u64) * dst_bytes_per_pixel
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
tab = args.dst.plane(p: 0)
while y < this.height {
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.height)
y = this.frame_rect_y0
while y < this.frame_rect_y1 {
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_y1)
dst = tab.row(y: y)
if dst_bytes_per_row < dst.length() {
dst = dst[.. dst_bytes_per_row]
if dst_bytes_per_row1 < dst.length() {
dst = dst[.. dst_bytes_per_row1]
}
if dst_bytes_per_row0 < dst.length() {
dst = dst[dst_bytes_per_row0 ..]
} else {
dst = this.util.empty_slice_u8()
}
if 1 > args.workbuf.length() {
+22 -14
View File
@@ -16,7 +16,7 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
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 dst_bytes_per_row1 : base.u64
var dst_palette : slice base.u8
var tab : table base.u8
@@ -45,7 +45,7 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
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
dst_bytes_per_row1 = (this.frame_rect_x1 as base.u64) * dst_bytes_per_pixel
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
tab = args.dst.plane(p: 0)
@@ -55,12 +55,16 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
((this.depth >> 3) as base.u64)
}
y = INTERLACING[this.interlace_pass][5] as base.u32
while y < this.height {
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.height)
if (this.chunk_type_array[0] == 'I') {
y = INTERLACING[this.interlace_pass][5] as base.u32
} else {
y = this.frame_rect_y0
}
while y < this.frame_rect_y1 {
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_y1)
dst = tab.row(y: y)
if dst_bytes_per_row < dst.length() {
dst = dst[.. dst_bytes_per_row]
if dst_bytes_per_row1 < dst.length() {
dst = dst[.. dst_bytes_per_row1]
}
if 1 > args.workbuf.length() {
@@ -89,12 +93,16 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
}
s = curr_row
x = INTERLACING[this.interlace_pass][2] as base.u32
if (this.chunk_type_array[0] == 'I') {
x = INTERLACING[this.interlace_pass][2] as base.u32
} else {
x = this.frame_rect_x0
}
if this.depth == 8 {
while x < this.width,
while x < this.frame_rect_x1,
inv y < 0x00FF_FFFF,
{
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.width)
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
i = (x as base.u64) * dst_bytes_per_pixel
if i <= dst.length() {
if this.color_type == 4 {
@@ -176,11 +184,11 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
shift = (8 - this.depth) & 7
packs_remaining = 0
while x < this.width,
while x < this.frame_rect_x1,
inv y < 0x00FF_FFFF,
inv this.depth < 8,
{
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.width)
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
i = (x as base.u64) * dst_bytes_per_pixel
if i <= dst.length() {
if (packs_remaining == 0) and (1 <= s.length()) {
@@ -222,10 +230,10 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
} endwhile
} else {
while x < this.width,
while x < this.frame_rect_x1,
inv y < 0x00FF_FFFF,
{
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.width)
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
i = (x as base.u64) * dst_bytes_per_pixel
if i <= dst.length() {
if this.color_type == 0 {
+91 -17
View File
@@ -148,6 +148,10 @@ do_wuffs_png_swizzle(uint32_t width,
CHECK_STATUS("initialize", wuffs_png__decoder__initialize(
&dec, sizeof dec, WUFFS_VERSION,
WUFFS_INITIALIZE__DEFAULT_OPTIONS));
dec.private_impl.f_frame_rect_x0 = 0;
dec.private_impl.f_frame_rect_y0 = 0;
dec.private_impl.f_frame_rect_x1 = width;
dec.private_impl.f_frame_rect_y1 = height;
dec.private_impl.f_width = width;
dec.private_impl.f_height = height;
dec.private_impl.f_pass_bytes_per_row = width;
@@ -446,26 +450,96 @@ test_wuffs_png_decode_filters_round_trip() {
const char* //
test_wuffs_png_decode_frame_config() {
CHECK_FOCUS(__func__);
wuffs_png__decoder dec;
CHECK_STATUS("initialize",
wuffs_png__decoder__initialize(
&dec, sizeof dec, WUFFS_VERSION,
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED));
wuffs_base__frame_config fc = ((wuffs_base__frame_config){});
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
.data = g_src_slice_u8,
});
CHECK_STRING(read_file(&src, "test/data/hibiscus.regular.png"));
CHECK_STATUS("decode_frame_config #0",
wuffs_png__decoder__decode_frame_config(&dec, &fc, &src));
static const uint64_t hibiscus_regular_want_areas[] = {
312 * 442,
};
static const uint64_t hibiscus_regular_want_io_ps[] = {
0x0021,
};
static const uint64_t animated_red_blue_want_areas[] = {
64 * 48,
37 * 9,
49 * 40,
37 * 9,
};
static const uint64_t animated_red_blue_want_io_ps[] = {
0x006D,
0x044A,
0x04D1,
0x0720,
};
wuffs_base__status status =
wuffs_png__decoder__decode_frame_config(&dec, &fc, &src);
if (status.repr != wuffs_base__note__end_of_data) {
RETURN_FAIL("decode_frame_config #1: have \"%s\", want \"%s\"", status.repr,
wuffs_base__note__end_of_data);
struct {
const char* filename;
uint64_t want_count;
const uint64_t* want_areas;
const uint64_t* want_io_ps;
} test_cases[] = {
{
.filename = "test/data/hibiscus.regular.png",
.want_count = WUFFS_TESTLIB_ARRAY_SIZE(hibiscus_regular_want_areas),
.want_areas = hibiscus_regular_want_areas,
.want_io_ps = hibiscus_regular_want_io_ps,
},
{
.filename = "test/data/animated-red-blue.apng",
.want_count = WUFFS_TESTLIB_ARRAY_SIZE(animated_red_blue_want_areas),
.want_areas = animated_red_blue_want_areas,
.want_io_ps = animated_red_blue_want_io_ps,
},
};
for (size_t tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) {
wuffs_png__decoder dec;
CHECK_STATUS("initialize",
wuffs_png__decoder__initialize(
&dec, sizeof dec, WUFFS_VERSION,
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED));
wuffs_base__frame_config fc = ((wuffs_base__frame_config){});
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
.data = g_src_slice_u8,
});
CHECK_STRING(read_file(&src, test_cases[tc].filename));
uint64_t have_count = 0;
while (true) {
wuffs_base__status status =
wuffs_png__decoder__decode_frame_config(&dec, &fc, &src);
if (status.repr == wuffs_base__note__end_of_data) {
break;
} else if (!wuffs_base__status__is_ok(&status)) {
RETURN_FAIL("decode_frame_config tc=%zu #%" PRIu64 ": %s", tc,
have_count, status.repr);
}
if (have_count < test_cases[tc].want_count) {
uint64_t have_area = ((uint64_t)wuffs_base__frame_config__width(&fc)) *
((uint64_t)wuffs_base__frame_config__height(&fc));
if (have_area != test_cases[tc].want_areas[have_count]) {
RETURN_FAIL(
"area tc=%zu #%" PRIu64 ": have %" PRIu64 ", want %" PRIu64, tc,
have_count, have_area, test_cases[tc].want_areas[have_count]);
}
uint64_t have_io_p = wuffs_base__frame_config__io_position(&fc);
if (have_io_p != test_cases[tc].want_io_ps[have_count]) {
RETURN_FAIL("io_position tc=%zu #%" PRIu64 ": have %" PRIu64
", want %" PRIu64,
tc, have_count, have_io_p,
test_cases[tc].want_io_ps[have_count]);
}
}
have_count++;
}
if (have_count != test_cases[tc].want_count) {
RETURN_FAIL("count tc=%zu: have %" PRIu64 ", want %" PRIu64, tc,
have_count, test_cases[tc].want_count);
}
}
return NULL;
}
+2 -2
View File
@@ -4,7 +4,7 @@ d3bb0beb test/data/DCI-P3-D65.icc
181c6916 test/data/animated-red-blue.000001.nie
e651a889 test/data/animated-red-blue.000002.nie
14a5b4b0 test/data/animated-red-blue.000003.nie
646e081a test/data/animated-red-blue.apng
76e40058 test/data/animated-red-blue.apng
76e40058 test/data/animated-red-blue.gif
f9a10dac test/data/artificial/gif-background-color.gif
36e7d9c7 test/data/artificial/gif-empty-palette.gif
@@ -60,7 +60,7 @@ dcbb225a test/data/hippopotamus.nie
21e17862 test/data/hippopotamus.regular.gif
dcbb225a test/data/hippopotamus.regular.png
2dccfe56 test/data/muybridge-frame-000.wbmp
3a4145fa test/data/muybridge.apng
db2733f5 test/data/muybridge.apng
db2733f5 test/data/muybridge.gif
bf7e8c96 test/data/pjw-thumbnail.bmp
73d48c08 test/data/pjw-thumbnail.gif