Have std/png decode interlaced PNG images

This commit is contained in:
Nigel Tao committed 2021-02-05 22:53:05 +11:00
1 parent 4053c2abe6
commit baad01d50f
4 files changed
+572 -188

No files matched your search

+300 -131
View File
@@ -8513,14 +8513,16 @@ struct wuffs_png__decoder__struct {
uint32_t f_width; uint32_t f_width;
uint32_t f_height; uint32_t f_height;
uint64_t f_bytes_per_row; uint64_t f_pass_bytes_per_row;
uint64_t f_workbuf_wi; uint64_t f_workbuf_wi;
uint64_t f_workbuf_length; uint64_t f_overall_workbuf_length;
uint64_t f_pass_workbuf_length;
uint8_t f_call_sequence; uint8_t f_call_sequence;
bool f_ignore_checksum; bool f_ignore_checksum;
uint8_t f_depth; uint8_t f_depth;
uint8_t f_color_type; uint8_t f_color_type;
uint8_t f_filter_distance; uint8_t f_filter_distance;
uint8_t f_interlace_pass;
bool f_seen_plte; bool f_seen_plte;
bool f_seen_trns; bool f_seen_trns;
bool f_tricky; bool f_tricky;
@@ -30739,10 +30741,36 @@ const char wuffs_png__error__internal_error_zlib_decoder_did_not_exhaust_its_inp
// ---------------- Private Consts // ---------------- Private Consts
static const uint8_t static const uint8_t
WUFFS_PNG__LOW_BIT_DEPTH_MULTIPLIER[8]WUFFS_BASE__POTENTIALLY_UNUSED = { WUFFS_PNG__INTERLACING[8][6]WUFFS_BASE__POTENTIALLY_UNUSED = {
{
0, 0, 0, 0, 0, 0,
}, {
3, 7, 0, 3, 7, 0,
}, {
3, 3, 4, 3, 7, 0,
}, {
2, 3, 0, 3, 3, 4,
}, {
2, 1, 2, 2, 3, 0,
}, {
1, 1, 0, 2, 1, 2,
}, {
1, 0, 1, 1, 1, 0,
}, {
0, 0, 0, 1, 0, 1,
},
};
static const uint8_t
WUFFS_PNG__LOW_BIT_DEPTH_MULTIPLIERS[8]WUFFS_BASE__POTENTIALLY_UNUSED = {
0, 255, 85, 0, 17, 0, 0, 0, 0, 255, 85, 0, 17, 0, 0, 0,
}; };
static const uint8_t
WUFFS_PNG__LOW_BIT_DEPTH_NUM_PACKS[8]WUFFS_BASE__POTENTIALLY_UNUSED = {
0, 8, 4, 0, 2, 0, 0, 0,
};
static const uint8_t static const uint8_t
WUFFS_PNG__NUM_CHANNELS[8]WUFFS_BASE__POTENTIALLY_UNUSED = { WUFFS_PNG__NUM_CHANNELS[8]WUFFS_BASE__POTENTIALLY_UNUSED = {
1, 0, 3, 1, 2, 0, 4, 0, 1, 0, 3, 1, 2, 0, 4, 0,
@@ -30928,6 +30956,12 @@ wuffs_png__decoder__swizzle_low_bit_depth(
uint64_t a_dst_bytes_per_pixel, uint64_t a_dst_bytes_per_pixel,
wuffs_base__slice_u8 a_curr_row); wuffs_base__slice_u8 a_curr_row);
static wuffs_base__status
wuffs_png__decoder__filter_and_swizzle_tricky(
wuffs_png__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__slice_u8 a_workbuf);
// ---------------- VTables // ---------------- VTables
const wuffs_base__image_decoder__func_ptrs const wuffs_base__image_decoder__func_ptrs
@@ -32643,10 +32677,9 @@ wuffs_png__decoder__decode_ihdr(
uint8_t t_6 = *iop_a_src++; uint8_t t_6 = *iop_a_src++;
v_a8 = t_6; v_a8 = t_6;
} }
if (v_a8 == 0) { if (v_a8 <= 1) {
} else if (v_a8 == 1) { self->private_impl.f_interlace_pass = v_a8;
status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); self->private_impl.f_tricky = (self->private_impl.f_tricky || (v_a8 >= 1));
goto exit;
} else { } else {
status = wuffs_base__make_status(wuffs_png__error__bad_header); status = wuffs_base__make_status(wuffs_png__error__bad_header);
goto exit; goto exit;
@@ -32657,9 +32690,12 @@ wuffs_png__decoder__decode_ihdr(
status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file);
goto exit; goto exit;
} }
self->private_impl.f_bytes_per_row = wuffs_png__decoder__calculate_bytes_per_row(self, self->private_impl.f_width); self->private_impl.f_overall_workbuf_length = (((uint64_t)(self->private_impl.f_height)) * (1 + wuffs_png__decoder__calculate_bytes_per_row(self, self->private_impl.f_width)));
self->private_impl.f_workbuf_length = (((uint64_t)(self->private_impl.f_height)) * (1 + self->private_impl.f_bytes_per_row));
wuffs_png__decoder__choose_filter_implementations(self); wuffs_png__decoder__choose_filter_implementations(self);
if (self->private_impl.f_tricky) {
self->private_impl.choosy_filter_and_swizzle = (
&wuffs_png__decoder__filter_and_swizzle_tricky);
}
goto ok; goto ok;
ok: ok:
@@ -33196,7 +33232,9 @@ wuffs_png__decoder__decode_frame(
self->private_impl.active_coroutine = 0; self->private_impl.active_coroutine = 0;
wuffs_base__status status = wuffs_base__make_status(NULL); wuffs_base__status status = wuffs_base__make_status(NULL);
wuffs_base__status v_swizzler_status = wuffs_base__make_status(NULL); wuffs_base__status v_status = wuffs_base__make_status(NULL);
uint32_t v_pass_width = 0;
uint32_t v_pass_height = 0;
uint32_t coro_susp_point = self->private_impl.p_decode_frame[0]; uint32_t coro_susp_point = self->private_impl.p_decode_frame[0];
switch (coro_susp_point) { switch (coro_susp_point) {
@@ -33213,18 +33251,14 @@ wuffs_png__decoder__decode_frame(
status = wuffs_base__make_status(wuffs_base__note__end_of_data); status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok; goto ok;
} }
if ((self->private_impl.f_width <= 0) || (self->private_impl.f_height <= 0)) { v_status = wuffs_base__pixel_swizzler__prepare(&self->private_impl.f_swizzler,
status = wuffs_base__make_status(NULL);
goto ok;
}
v_swizzler_status = wuffs_base__pixel_swizzler__prepare(&self->private_impl.f_swizzler,
wuffs_base__pixel_buffer__pixel_format(a_dst), wuffs_base__pixel_buffer__pixel_format(a_dst),
wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)), wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)),
wuffs_base__utility__make_pixel_format(self->private_impl.f_src_pixfmt), wuffs_base__utility__make_pixel_format(self->private_impl.f_src_pixfmt),
wuffs_base__make_slice_u8(self->private_data.f_src_palette, 1024), wuffs_base__make_slice_u8(self->private_data.f_src_palette, 1024),
a_blend); a_blend);
if ( ! wuffs_base__status__is_ok(&v_swizzler_status)) { if ( ! wuffs_base__status__is_ok(&v_status)) {
status = v_swizzler_status; status = v_status;
if (wuffs_base__status__is_error(&status)) { if (wuffs_base__status__is_error(&status)) {
goto exit; goto exit;
} else if (wuffs_base__status__is_suspension(&status)) { } else if (wuffs_base__status__is_suspension(&status)) {
@@ -33233,22 +33267,42 @@ wuffs_png__decoder__decode_frame(
} }
goto ok; goto ok;
} }
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); while (true) {
status = wuffs_png__decoder__decode_pass(self, a_src, a_workbuf); 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]));
if (status.repr) { 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]));
goto suspend; 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);
v_swizzler_status = wuffs_png__decoder__filter_and_swizzle(self, a_dst, a_workbuf); self->private_impl.f_pass_workbuf_length = (((uint64_t)(v_pass_height)) * (1 + self->private_impl.f_pass_bytes_per_row));
if ( ! wuffs_base__status__is_ok(&v_swizzler_status)) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
status = v_swizzler_status; status = wuffs_png__decoder__decode_pass(self, a_src, a_workbuf);
if (wuffs_base__status__is_error(&status)) { if (status.repr) {
goto exit; goto suspend;
} else if (wuffs_base__status__is_suspension(&status)) { }
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension); v_status = wuffs_png__decoder__filter_and_swizzle(self, a_dst, a_workbuf);
goto exit; if ( ! wuffs_base__status__is_ok(&v_status)) {
status = v_status;
if (wuffs_base__status__is_error(&status)) {
goto exit;
} else if (wuffs_base__status__is_suspension(&status)) {
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);
goto exit;
}
goto ok;
}
} }
goto ok; if ((self->private_impl.f_interlace_pass == 0) || (self->private_impl.f_interlace_pass >= 7)) {
goto label__0__break;
}
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
self->private_impl.f_interlace_pass += 1;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
} }
label__0__break:;
self->private_impl.f_call_sequence = 255; self->private_impl.f_call_sequence = 255;
goto ok; goto ok;
@@ -33312,7 +33366,7 @@ wuffs_png__decoder__decode_pass(
self->private_impl.f_workbuf_wi = 0; self->private_impl.f_workbuf_wi = 0;
label__0__continue:; label__0__continue:;
while (true) { while (true) {
if ((self->private_impl.f_workbuf_wi > self->private_impl.f_workbuf_length) || (self->private_impl.f_workbuf_length > ((uint64_t)(a_workbuf.len)))) { if ((self->private_impl.f_workbuf_wi > self->private_impl.f_pass_workbuf_length) || (self->private_impl.f_pass_workbuf_length > ((uint64_t)(a_workbuf.len)))) {
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length); status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
goto exit; goto exit;
} }
@@ -33330,7 +33384,7 @@ wuffs_png__decoder__decode_pass(
&io2_v_w, &io2_v_w,
wuffs_base__slice_u8__subslice_ij(a_workbuf, wuffs_base__slice_u8__subslice_ij(a_workbuf,
self->private_impl.f_workbuf_wi, self->private_impl.f_workbuf_wi,
self->private_impl.f_workbuf_length)); self->private_impl.f_pass_workbuf_length));
{ {
const uint8_t *o_1_io2_a_src = io2_a_src; const uint8_t *o_1_io2_a_src = io2_a_src;
wuffs_base__io_reader__limit(&io2_a_src, iop_a_src, wuffs_base__io_reader__limit(&io2_a_src, iop_a_src,
@@ -33369,8 +33423,51 @@ wuffs_png__decoder__decode_pass(
io2_v_w = o_0_io2_v_w; io2_v_w = o_0_io2_v_w;
} }
if (wuffs_base__status__is_ok(&v_zlib_status)) { 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));
}
}
v_checksum_want = t_1;
}
if (v_checksum_have != v_checksum_want) {
status = wuffs_base__make_status(wuffs_png__error__bad_checksum);
goto exit;
}
}
goto label__0__break; goto label__0__break;
} else if (v_zlib_status.repr == wuffs_base__suspension__short_write) { } else if (v_zlib_status.repr == wuffs_base__suspension__short_write) {
if ((1 <= self->private_impl.f_interlace_pass) && (self->private_impl.f_interlace_pass <= 6)) {
goto label__0__break;
}
status = wuffs_base__make_status(wuffs_base__error__too_much_data); status = wuffs_base__make_status(wuffs_base__error__too_much_data);
goto exit; goto exit;
} else if (v_zlib_status.repr != wuffs_base__suspension__short_read) { } else if (v_zlib_status.repr != wuffs_base__suspension__short_read) {
@@ -33383,47 +33480,11 @@ wuffs_png__decoder__decode_pass(
} }
goto ok; goto ok;
} else if (self->private_impl.f_chunk_length == 0) { } else if (self->private_impl.f_chunk_length == 0) {
{
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;
}
if ( ! self->private_impl.f_ignore_checksum) {
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;
}
}
{ {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
uint64_t t_2; uint32_t t_2;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_2 = ((uint64_t)(wuffs_base__peek_u32be__no_bounds_check(iop_a_src))); t_2 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
iop_a_src += 4; iop_a_src += 4;
} else { } else {
self->private_data.s_decode_pass[0].scratch = 0; self->private_data.s_decode_pass[0].scratch = 0;
@@ -33439,20 +33500,27 @@ wuffs_png__decoder__decode_pass(
*scratch <<= 8; *scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_2); *scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_2);
if (num_bits_2 == 24) { if (num_bits_2 == 24) {
t_2 = ((uint64_t)(*scratch >> 32)); t_2 = ((uint32_t)(*scratch >> 32));
break; break;
} }
num_bits_2 += 8; num_bits_2 += 8;
*scratch |= ((uint64_t)(num_bits_2)); *scratch |= ((uint64_t)(num_bits_2));
} }
} }
self->private_impl.f_chunk_length = t_2; v_checksum_want = t_2;
}
if ( ! self->private_impl.f_ignore_checksum) {
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;
}
} }
{ {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
uint32_t t_3; uint64_t t_3;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_3 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src); t_3 = ((uint64_t)(wuffs_base__peek_u32be__no_bounds_check(iop_a_src)));
iop_a_src += 4; iop_a_src += 4;
} else { } else {
self->private_data.s_decode_pass[0].scratch = 0; self->private_data.s_decode_pass[0].scratch = 0;
@@ -33463,19 +33531,48 @@ wuffs_png__decoder__decode_pass(
goto suspend; goto suspend;
} }
uint64_t* scratch = &self->private_data.s_decode_pass[0].scratch; uint64_t* scratch = &self->private_data.s_decode_pass[0].scratch;
uint32_t num_bits_3 = ((uint32_t)(*scratch >> 56)); uint32_t num_bits_3 = ((uint32_t)(*scratch & 0xFF));
*scratch <<= 8;
*scratch >>= 8; *scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_3; *scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_3);
if (num_bits_3 == 24) { if (num_bits_3 == 24) {
t_3 = ((uint32_t)(*scratch)); t_3 = ((uint64_t)(*scratch >> 32));
break; break;
} }
num_bits_3 += 8; num_bits_3 += 8;
*scratch |= ((uint64_t)(num_bits_3)) << 56; *scratch |= ((uint64_t)(num_bits_3));
} }
} }
self->private_impl.f_chunk_type = t_3; self->private_impl.f_chunk_length = t_3;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
uint32_t t_4;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
t_4 = wuffs_base__peek_u32le__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(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_pass[0].scratch;
uint32_t num_bits_4 = ((uint32_t)(*scratch >> 56));
*scratch <<= 8;
*scratch >>= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_4;
if (num_bits_4 == 24) {
t_4 = ((uint32_t)(*scratch));
break;
}
num_bits_4 += 8;
*scratch |= ((uint64_t)(num_bits_4)) << 56;
}
}
self->private_impl.f_chunk_type = t_4;
} }
if (self->private_impl.f_chunk_type != 1413563465) { if (self->private_impl.f_chunk_type != 1413563465) {
status = wuffs_base__make_status(wuffs_png__error__bad_chunk); status = wuffs_base__make_status(wuffs_png__error__bad_chunk);
@@ -33495,50 +33592,10 @@ wuffs_png__decoder__decode_pass(
goto exit; goto exit;
} }
status = wuffs_base__make_status(wuffs_base__suspension__short_read); status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(7); WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(9);
} }
label__0__break:; label__0__break:;
if ( ! self->private_impl.f_ignore_checksum) { if (self->private_impl.f_workbuf_wi != self->private_impl.f_pass_workbuf_length) {
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(8);
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_pass[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9);
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_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_checksum_want = t_4;
}
if (v_checksum_have != v_checksum_want) {
status = wuffs_base__make_status(wuffs_png__error__bad_checksum);
goto exit;
}
}
if (self->private_impl.f_workbuf_wi != self->private_impl.f_workbuf_length) {
status = wuffs_base__make_status(wuffs_base__error__not_enough_data); status = wuffs_base__make_status(wuffs_base__error__not_enough_data);
goto exit; goto exit;
} else if (0 < ((uint64_t)(a_workbuf.len))) { } else if (0 < ((uint64_t)(a_workbuf.len))) {
@@ -33613,11 +33670,11 @@ wuffs_png__decoder__filter_and_swizzle__choosy_default(
} }
v_filter = a_workbuf.ptr[0]; v_filter = a_workbuf.ptr[0];
a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, 1); a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, 1);
if (self->private_impl.f_bytes_per_row > ((uint64_t)(a_workbuf.len))) { if (self->private_impl.f_pass_bytes_per_row > ((uint64_t)(a_workbuf.len))) {
return wuffs_base__make_status(wuffs_png__error__internal_error_inconsistent_workbuf_length); return wuffs_base__make_status(wuffs_png__error__internal_error_inconsistent_workbuf_length);
} }
v_curr_row = wuffs_base__slice_u8__subslice_j(a_workbuf, self->private_impl.f_bytes_per_row); v_curr_row = wuffs_base__slice_u8__subslice_j(a_workbuf, self->private_impl.f_pass_bytes_per_row);
a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, self->private_impl.f_bytes_per_row); a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, self->private_impl.f_pass_bytes_per_row);
if (v_filter == 0) { if (v_filter == 0) {
} else if (v_filter == 1) { } else if (v_filter == 1) {
wuffs_png__decoder__filter_1(self, v_curr_row); wuffs_png__decoder__filter_1(self, v_curr_row);
@@ -33675,11 +33732,11 @@ wuffs_png__decoder__filter_and_swizzle_low_bit_depth(
} }
v_filter = a_workbuf.ptr[0]; v_filter = a_workbuf.ptr[0];
a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, 1); a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, 1);
if (self->private_impl.f_bytes_per_row > ((uint64_t)(a_workbuf.len))) { if (self->private_impl.f_pass_bytes_per_row > ((uint64_t)(a_workbuf.len))) {
return wuffs_base__make_status(wuffs_png__error__internal_error_inconsistent_workbuf_length); return wuffs_base__make_status(wuffs_png__error__internal_error_inconsistent_workbuf_length);
} }
v_curr_row = wuffs_base__slice_u8__subslice_j(a_workbuf, self->private_impl.f_bytes_per_row); v_curr_row = wuffs_base__slice_u8__subslice_j(a_workbuf, self->private_impl.f_pass_bytes_per_row);
a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, self->private_impl.f_bytes_per_row); a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, self->private_impl.f_pass_bytes_per_row);
if (v_filter == 0) { if (v_filter == 0) {
} else if (v_filter == 1) { } else if (v_filter == 1) {
wuffs_png__decoder__filter_1(self, v_curr_row); wuffs_png__decoder__filter_1(self, v_curr_row);
@@ -33721,7 +33778,7 @@ wuffs_png__decoder__swizzle_low_bit_depth(
v_multiplier = 1; v_multiplier = 1;
if (self->private_impl.f_color_type == 0) { if (self->private_impl.f_color_type == 0) {
v_multiplier = WUFFS_PNG__LOW_BIT_DEPTH_MULTIPLIER[(self->private_impl.f_depth & 7)]; v_multiplier = WUFFS_PNG__LOW_BIT_DEPTH_MULTIPLIERS[(self->private_impl.f_depth & 7)];
} }
v_remaining = a_curr_row; v_remaining = a_curr_row;
while (((uint64_t)(a_dst.len)) > 0) { while (((uint64_t)(a_dst.len)) > 0) {
@@ -33881,6 +33938,9 @@ wuffs_png__decoder__restart_frame(
return wuffs_base__make_status(wuffs_base__error__bad_argument); return wuffs_base__make_status(wuffs_base__error__bad_argument);
} }
self->private_impl.f_call_sequence = 3; self->private_impl.f_call_sequence = 3;
if (self->private_impl.f_interlace_pass >= 1) {
self->private_impl.f_interlace_pass = 1;
}
self->private_impl.f_frame_config_io_position = a_io_position; self->private_impl.f_frame_config_io_position = a_io_position;
return wuffs_base__make_status(NULL); return wuffs_base__make_status(NULL);
} }
@@ -33950,7 +34010,116 @@ wuffs_png__decoder__workbuf_len(
return wuffs_base__utility__empty_range_ii_u64(); return wuffs_base__utility__empty_range_ii_u64();
} }
return wuffs_base__utility__make_range_ii_u64(self->private_impl.f_workbuf_length, self->private_impl.f_workbuf_length); return wuffs_base__utility__make_range_ii_u64(self->private_impl.f_overall_workbuf_length, self->private_impl.f_overall_workbuf_length);
}
// -------- func png.decoder.filter_and_swizzle_tricky
static wuffs_base__status
wuffs_png__decoder__filter_and_swizzle_tricky(
wuffs_png__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__slice_u8 a_workbuf) {
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;
wuffs_base__slice_u8 v_dst_palette = {0};
wuffs_base__table_u8 v_tab = {0};
uint64_t v_src_bytes_per_pixel = 0;
uint32_t v_x = 0;
uint32_t v_y = 0;
uint64_t v_i = 0;
wuffs_base__slice_u8 v_dst = {0};
uint8_t v_filter = 0;
wuffs_base__slice_u8 v_s = {0};
wuffs_base__slice_u8 v_curr_row = {0};
wuffs_base__slice_u8 v_prev_row = {0};
uint8_t v_bits_packed = 0;
uint8_t v_bits_unpacked[1] = {0};
uint8_t v_packs_remaining = 0;
uint8_t v_multiplier = 0;
uint8_t v_shift = 0;
v_dst_pixfmt = wuffs_base__pixel_buffer__pixel_format(a_dst);
v_dst_bits_per_pixel = wuffs_base__pixel_format__bits_per_pixel(&v_dst_pixfmt);
if ((v_dst_bits_per_pixel & 7) != 0) {
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_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) {
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 (1 > ((uint64_t)(a_workbuf.len))) {
return wuffs_base__make_status(wuffs_png__error__internal_error_inconsistent_workbuf_length);
}
v_filter = a_workbuf.ptr[0];
a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, 1);
if (self->private_impl.f_pass_bytes_per_row > ((uint64_t)(a_workbuf.len))) {
return wuffs_base__make_status(wuffs_png__error__internal_error_inconsistent_workbuf_length);
}
v_curr_row = wuffs_base__slice_u8__subslice_j(a_workbuf, self->private_impl.f_pass_bytes_per_row);
a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, self->private_impl.f_pass_bytes_per_row);
if (v_filter == 0) {
} else if (v_filter == 1) {
wuffs_png__decoder__filter_1(self, v_curr_row);
} else if (v_filter == 2) {
wuffs_png__decoder__filter_2(self, v_curr_row, v_prev_row);
} else if (v_filter == 3) {
wuffs_png__decoder__filter_3(self, v_curr_row, v_prev_row);
} else if (v_filter == 4) {
wuffs_png__decoder__filter_4(self, v_curr_row, v_prev_row);
} else {
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_depth >= 8) {
while (v_x < self->private_impl.f_width) {
v_i = (((uint64_t)(v_x)) * v_dst_bytes_per_pixel);
if ((v_i <= ((uint64_t)(v_dst.len))) && (v_src_bytes_per_pixel <= ((uint64_t)(v_s.len)))) {
wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_i), v_dst_palette, wuffs_base__slice_u8__subslice_j(v_s, v_src_bytes_per_pixel));
v_s = wuffs_base__slice_u8__subslice_i(v_s, v_src_bytes_per_pixel);
}
v_x += (((uint32_t)(1)) << WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][0]);
}
} else {
v_multiplier = 1;
if (self->private_impl.f_color_type == 0) {
v_multiplier = WUFFS_PNG__LOW_BIT_DEPTH_MULTIPLIERS[self->private_impl.f_depth];
}
v_shift = ((8 - self->private_impl.f_depth) & 7);
v_packs_remaining = 0;
while (v_x < self->private_impl.f_width) {
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)))) {
v_packs_remaining = WUFFS_PNG__LOW_BIT_DEPTH_NUM_PACKS[self->private_impl.f_depth];
v_bits_packed = v_s.ptr[0];
v_s = wuffs_base__slice_u8__subslice_i(v_s, 1);
}
v_bits_unpacked[0] = ((uint8_t)((v_bits_packed >> v_shift) * v_multiplier));
v_bits_packed <<= self->private_impl.f_depth;
v_packs_remaining -= 1;
wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_i), v_dst_palette, wuffs_base__make_slice_u8(v_bits_unpacked, 1));
}
v_x += (((uint32_t)(1)) << WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][0]);
}
}
v_prev_row = v_curr_row;
v_y += (((uint32_t)(1)) << WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][3]);
}
return wuffs_base__make_status(NULL);
} }
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__PNG) #endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__PNG)
+123 -55
View File
@@ -33,11 +33,12 @@ pub struct decoder? implements base.image_decoder(
width : base.u32[..= 0x00FF_FFFF], width : base.u32[..= 0x00FF_FFFF],
height : base.u32[..= 0x00FF_FFFF], height : base.u32[..= 0x00FF_FFFF],
// bytes_per_row doesn't include the 1 byte for the per-row filter. // pass_bytes_per_row doesn't include the 1 byte for the per-row filter.
bytes_per_row : base.u64[..= 0x07FF_FFF8], pass_bytes_per_row : base.u64[..= 0x07FF_FFF8],
workbuf_wi : base.u64, workbuf_wi : base.u64,
workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007], overall_workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007],
pass_workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007],
// Call sequence states: // Call sequence states:
// - 0x00: initial state. // - 0x00: initial state.
@@ -71,6 +72,7 @@ pub struct decoder? implements base.image_decoder(
depth : base.u8[..= 16], depth : base.u8[..= 16],
color_type : base.u8[..= 6], color_type : base.u8[..= 6],
filter_distance : base.u8[..= 8], filter_distance : base.u8[..= 8],
interlace_pass : base.u8[..= 7],
seen_plte : base.bool, seen_plte : base.bool,
seen_trns : base.bool, seen_trns : base.bool,
@@ -266,10 +268,9 @@ pri func decoder.decode_ihdr?(src: base.io_reader) {
// Interlace. // Interlace.
a8 = args.src.read_u8?() a8 = args.src.read_u8?()
if a8 == 0 { if a8 <= 1 {
// No-op. this.interlace_pass = a8
} else if a8 == 1 { this.tricky = this.tricky or (a8 >= 1)
return "#unsupported PNG file"
} else { } else {
return "#bad header" return "#bad header"
} }
@@ -280,9 +281,12 @@ pri func decoder.decode_ihdr?(src: base.io_reader) {
if this.filter_distance == 0 { if this.filter_distance == 0 {
return "#unsupported PNG file" return "#unsupported PNG file"
} }
this.bytes_per_row = this.calculate_bytes_per_row(width: this.width) this.overall_workbuf_length = (this.height as base.u64) *
this.workbuf_length = (this.height as base.u64) * (1 + this.bytes_per_row) (1 + this.calculate_bytes_per_row(width: this.width))
this.choose_filter_implementations!() this.choose_filter_implementations!()
if this.tricky {
choose filter_and_swizzle = [filter_and_swizzle_tricky]
}
} }
pri func decoder.assign_filter_distance!() { pri func decoder.assign_filter_distance!() {
@@ -489,7 +493,9 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_
} }
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) { 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 swizzler_status : base.status var status : base.status
var pass_width : base.u32[..= 0x00FF_FFFF]
var pass_height : base.u32[..= 0x00FF_FFFF]
if this.call_sequence < 4 { if this.call_sequence < 4 {
this.decode_frame_config?(dst: nullptr, src: args.src) this.decode_frame_config?(dst: nullptr, src: args.src)
@@ -499,26 +505,39 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
return base."@end of data" return base."@end of data"
} }
if (this.width <= 0) or (this.height <= 0) { status = this.swizzler.prepare!(
return ok
}
swizzler_status = this.swizzler.prepare!(
dst_pixfmt: args.dst.pixel_format(), dst_pixfmt: args.dst.pixel_format(),
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]), dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt), src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
src_palette: this.src_palette[..], src_palette: this.src_palette[..],
blend: args.blend) blend: args.blend)
if not swizzler_status.is_ok() { if not status.is_ok() {
return swizzler_status return status
} }
this.decode_pass?(src: args.src, workbuf: args.workbuf) 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])
swizzler_status = this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf) if (pass_width > 0) and (pass_height > 0) {
if not swizzler_status.is_ok() { this.pass_bytes_per_row = this.calculate_bytes_per_row(width: pass_width)
return swizzler_status this.pass_workbuf_length = (pass_height as base.u64) * (1 + this.pass_bytes_per_row)
} this.decode_pass?(src: args.src, workbuf: args.workbuf)
status = this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf)
if not status.is_ok() {
return status
}
}
if (this.interlace_pass == 0) or (this.interlace_pass >= 7) {
break
}
this.interlace_pass += 1
} endwhile
this.call_sequence = 0xFF this.call_sequence = 0xFF
} }
@@ -533,11 +552,11 @@ pri func decoder.decode_pass?(src: base.io_reader, workbuf: slice base.u8) {
this.workbuf_wi = 0 this.workbuf_wi = 0
while true { while true {
if (this.workbuf_wi > this.workbuf_length) or ( if (this.workbuf_wi > this.pass_workbuf_length) or (
this.workbuf_length > args.workbuf.length()) { this.pass_workbuf_length > args.workbuf.length()) {
return base."#bad workbuf length" return base."#bad workbuf length"
} }
io_bind (io: w, data: args.workbuf[this.workbuf_wi .. this.workbuf_length]) { io_bind (io: w, data: args.workbuf[this.workbuf_wi .. this.pass_workbuf_length]) {
io_limit (io: args.src, limit: this.chunk_length) { io_limit (io: args.src, limit: this.chunk_length) {
w_mark = w.mark() w_mark = w.mark()
r_mark = args.src.mark() r_mark = args.src.mark()
@@ -552,8 +571,23 @@ pri func decoder.decode_pass?(src: base.io_reader, workbuf: slice base.u8) {
} }
if zlib_status.is_ok() { if zlib_status.is_ok() {
// 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"
}
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"
}
}
break break
} else if zlib_status == base."$short write" { } else if zlib_status == base."$short write" {
if (1 <= this.interlace_pass) and (this.interlace_pass <= 6) {
break
}
return base."#too much data" return base."#too much data"
} else if zlib_status <> base."$short read" { } else if zlib_status <> base."$short read" {
return zlib_status return zlib_status
@@ -590,20 +624,7 @@ pri func decoder.decode_pass?(src: base.io_reader, workbuf: slice base.u8) {
yield? base."$short read" yield? base."$short read"
} endwhile } endwhile
// Verify the final IDAT chunk's CRC-32 checksum. if this.workbuf_wi <> this.pass_workbuf_length {
if not this.ignore_checksum {
if this.chunk_length > 0 {
// TODO: should this be a fatal error?
return base."#too much data"
}
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"
}
}
if this.workbuf_wi <> this.workbuf_length {
return base."#not enough data" return base."#not enough data"
} else if 0 < args.workbuf.length() { } else if 0 < args.workbuf.length() {
// For the top row, the Paeth filter (4) is equivalent to the Sub // For the top row, the Paeth filter (4) is equivalent to the Sub
@@ -644,7 +665,7 @@ pri func decoder.filter_and_swizzle!(dst: ptr base.pixel_buffer, workbuf: slice
tab = args.dst.plane(p: 0) tab = args.dst.plane(p: 0)
while y < this.height { while y < this.height {
assert y < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: this.height) assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.height)
dst = tab.row(y: y) dst = tab.row(y: y)
if dst_bytes_per_row < dst.length() { if dst_bytes_per_row < dst.length() {
dst = dst[.. dst_bytes_per_row] dst = dst[.. dst_bytes_per_row]
@@ -655,11 +676,11 @@ pri func decoder.filter_and_swizzle!(dst: ptr base.pixel_buffer, workbuf: slice
} }
filter = args.workbuf[0] filter = args.workbuf[0]
args.workbuf = args.workbuf[1 ..] args.workbuf = args.workbuf[1 ..]
if this.bytes_per_row > args.workbuf.length() { if this.pass_bytes_per_row > args.workbuf.length() {
return "#internal error: inconsistent workbuf length" return "#internal error: inconsistent workbuf length"
} }
curr_row = args.workbuf[.. this.bytes_per_row] curr_row = args.workbuf[.. this.pass_bytes_per_row]
args.workbuf = args.workbuf[this.bytes_per_row ..] args.workbuf = args.workbuf[this.pass_bytes_per_row ..]
if filter == 0 { if filter == 0 {
// No-op. // No-op.
@@ -720,7 +741,7 @@ pri func decoder.filter_and_swizzle_low_bit_depth!(dst: ptr base.pixel_buffer, w
tab = args.dst.plane(p: 0) tab = args.dst.plane(p: 0)
while y < this.height { while y < this.height {
assert y < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: this.height) assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.height)
dst = tab.row(y: y) dst = tab.row(y: y)
if dst_bytes_per_row < dst.length() { if dst_bytes_per_row < dst.length() {
dst = dst[.. dst_bytes_per_row] dst = dst[.. dst_bytes_per_row]
@@ -731,11 +752,11 @@ pri func decoder.filter_and_swizzle_low_bit_depth!(dst: ptr base.pixel_buffer, w
} }
filter = args.workbuf[0] filter = args.workbuf[0]
args.workbuf = args.workbuf[1 ..] args.workbuf = args.workbuf[1 ..]
if this.bytes_per_row > args.workbuf.length() { if this.pass_bytes_per_row > args.workbuf.length() {
return "#internal error: inconsistent workbuf length" return "#internal error: inconsistent workbuf length"
} }
curr_row = args.workbuf[.. this.bytes_per_row] curr_row = args.workbuf[.. this.pass_bytes_per_row]
args.workbuf = args.workbuf[this.bytes_per_row ..] args.workbuf = args.workbuf[this.pass_bytes_per_row ..]
if filter == 0 { if filter == 0 {
// No-op. // No-op.
@@ -774,7 +795,7 @@ pri func decoder.swizzle_low_bit_depth!(dst: slice base.u8, dst_palette: slice b
multiplier = 1 multiplier = 1
if this.color_type == 0 { // Color type 0 means base.PIXEL_FORMAT__Y. if this.color_type == 0 { // Color type 0 means base.PIXEL_FORMAT__Y.
multiplier = LOW_BIT_DEPTH_MULTIPLIER[this.depth & 7] multiplier = LOW_BIT_DEPTH_MULTIPLIERS[this.depth & 7]
} }
remaining = args.curr_row remaining = args.curr_row
@@ -890,6 +911,9 @@ pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.sta
return base."#bad argument" return base."#bad argument"
} }
this.call_sequence = 3 this.call_sequence = 3
if this.interlace_pass >= 1 {
this.interlace_pass = 1
}
this.frame_config_io_position = args.io_position this.frame_config_io_position = args.io_position
return ok return ok
} }
@@ -904,11 +928,41 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
pub func decoder.workbuf_len() base.range_ii_u64 { pub func decoder.workbuf_len() base.range_ii_u64 {
return this.util.make_range_ii_u64( return this.util.make_range_ii_u64(
min_incl: this.workbuf_length, min_incl: this.overall_workbuf_length,
max_incl: this.workbuf_length) max_incl: this.overall_workbuf_length)
} }
pri const LOW_BIT_DEPTH_MULTIPLIER : array[8] base.u8 = [ // INTERLACING holds the Adam7 interlacing pattern, involving 7 passes:
// 1 6 4 6 2 6 4 6
// 7 7 7 7 7 7 7 7
// 5 6 5 6 5 6 5 6
// 7 7 7 7 7 7 7 7
// 3 6 4 6 3 6 4 6
// 7 7 7 7 7 7 7 7
// 5 6 5 6 5 6 5 6
// 7 7 7 7 7 7 7 7
//
// The six elements of each inner array are:
// 0: log2(x_stride)
// 1: x_stride - x_offset - 1
// 2: x_offset
// 3: log2(y_stride)
// 4: y_stride - y_offset - 1
// 5: y_offset
pri const INTERLACING : array[8] array[6] base.u8[..= 8] = [
[0, 0, 0, 0, 0, 0], // non-interlaced; xy_stride=1, xy_offset=0
[3, 7, 0, 3, 7, 0], // interlace_pass == 1
[3, 3, 4, 3, 7, 0], // interlace_pass == 2
[2, 3, 0, 3, 3, 4], // interlace_pass == 3
[2, 1, 2, 2, 3, 0], // interlace_pass == 4
[1, 1, 0, 2, 1, 2], // interlace_pass == 5
[1, 0, 1, 1, 1, 0], // interlace_pass == 6
[0, 0, 0, 1, 0, 1], // interlace_pass == 7
]
// LOW_BIT_DEPTH_MULTIPLIERS holds multipliers that convert D-bit values into
// 8-bit values, for depth D.
pri const LOW_BIT_DEPTH_MULTIPLIERS : array[8] base.u8 = [
0, 0,
0b1111_1111, // depth == 1 0b1111_1111, // depth == 1
0b0101_0101, // depth == 2 0b0101_0101, // depth == 2
@@ -919,13 +973,27 @@ pri const LOW_BIT_DEPTH_MULTIPLIER : array[8] base.u8 = [
0, 0,
] ]
// LOW_BIT_DEPTH_NUM_PACKS holds the number of smaller-than-1-byte units that
// are packed into each byte.
pri const LOW_BIT_DEPTH_NUM_PACKS : array[8] base.u8 = [
0,
8, // depth == 1
4, // depth == 2
0,
2, // depth == 4
0,
0,
0,
]
// NUM_CHANNELS holds the number of channels for each valid color type.
pri const NUM_CHANNELS : array[8] base.u8[..= 4] = [ pri const NUM_CHANNELS : array[8] base.u8[..= 4] = [
1, // color_type == 0: Y 1, // color_type == 0: Y
0, // Unused. 0,
3, // color_type == 2: RGB 3, // color_type == 2: RGB
1, // color_type == 3: indexed 1, // color_type == 3: indexed
2, // color_type == 4: YA 2, // color_type == 4: YA
0, // Unused. 0,
4, // color_type == 6: RGBA 4, // color_type == 6: RGBA
0, // Unused. 0,
] ]
+147
View File
@@ -0,0 +1,147 @@
// Copyright 2021 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) base.status {
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u64[..= 32]
var dst_bytes_per_row : base.u64
var dst_palette : slice base.u8
var tab : table base.u8
var src_bytes_per_pixel : base.u64[..= 8]
var x : base.u32
var y : base.u32
var i : base.u64[..= 0x1FFF_FFC0]
var dst : slice base.u8
var filter : base.u8
var s : slice base.u8
var curr_row : slice base.u8
var prev_row : slice base.u8
var bits_packed : base.u8
var bits_unpacked : array[1] base.u8
var packs_remaining : base.u8
var multiplier : base.u8
var shift : base.u8[..= 7]
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
dst_pixfmt = args.dst.pixel_format()
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
if (dst_bits_per_pixel & 7) <> 0 {
return base."#unsupported option"
}
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
tab = args.dst.plane(p: 0)
src_bytes_per_pixel = 1
if this.depth >= 8 {
src_bytes_per_pixel = (NUM_CHANNELS[this.color_type] as base.u64) *
((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)
dst = tab.row(y: y)
if dst_bytes_per_row < dst.length() {
dst = dst[.. dst_bytes_per_row]
}
if 1 > args.workbuf.length() {
return "#internal error: inconsistent workbuf length"
}
filter = args.workbuf[0]
args.workbuf = args.workbuf[1 ..]
if this.pass_bytes_per_row > args.workbuf.length() {
return "#internal error: inconsistent workbuf length"
}
curr_row = args.workbuf[.. this.pass_bytes_per_row]
args.workbuf = args.workbuf[this.pass_bytes_per_row ..]
if filter == 0 {
// No-op.
} else if filter == 1 {
this.filter_1!(curr: curr_row)
} else if filter == 2 {
this.filter_2!(curr: curr_row, prev: prev_row)
} else if filter == 3 {
this.filter_3!(curr: curr_row, prev: prev_row)
} else if filter == 4 {
this.filter_4!(curr: curr_row, prev: prev_row)
} else {
return "#bad filter"
}
s = curr_row
x = INTERLACING[this.interlace_pass][2] as base.u32
if this.depth >= 8 {
while x < this.width,
inv y < 0x00FF_FFFF,
{
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.width)
i = (x as base.u64) * dst_bytes_per_pixel
if (i <= dst.length()) and (src_bytes_per_pixel <= s.length()) {
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: s[.. src_bytes_per_pixel])
s = s[src_bytes_per_pixel ..]
}
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
} endwhile
} else {
multiplier = 1
if this.color_type == 0 { // Color type 0 means base.PIXEL_FORMAT__Y.
multiplier = LOW_BIT_DEPTH_MULTIPLIERS[this.depth]
}
shift = (8 - this.depth) & 7
packs_remaining = 0
while x < this.width,
inv y < 0x00FF_FFFF,
inv this.depth < 8,
{
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.width)
i = (x as base.u64) * dst_bytes_per_pixel
if i <= dst.length() {
if (packs_remaining == 0) and (1 <= s.length()) {
packs_remaining = LOW_BIT_DEPTH_NUM_PACKS[this.depth]
bits_packed = s[0]
s = s[1 ..]
}
bits_unpacked[0] = (bits_packed >> shift) ~mod* multiplier
bits_packed ~mod<<= this.depth
packs_remaining ~mod-= 1
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 1])
}
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
} endwhile
}
prev_row = curr_row
y += (1 as base.u32) << INTERLACING[this.interlace_pass][3]
} endwhile
return ok
}
+2 -2
View File
@@ -151,7 +151,7 @@ do_wuffs_png_swizzle(uint32_t width,
WUFFS_INITIALIZE__DEFAULT_OPTIONS)); WUFFS_INITIALIZE__DEFAULT_OPTIONS));
dec.private_impl.f_width = width; dec.private_impl.f_width = width;
dec.private_impl.f_height = height; dec.private_impl.f_height = height;
dec.private_impl.f_bytes_per_row = width; dec.private_impl.f_pass_bytes_per_row = width;
dec.private_impl.f_filter_distance = filter_distance; dec.private_impl.f_filter_distance = filter_distance;
wuffs_png__decoder__choose_filter_implementations(&dec); wuffs_png__decoder__choose_filter_implementations(&dec);
@@ -653,7 +653,7 @@ do_bench_wuffs_png_decode_filter(uint8_t filter,
WUFFS_INITIALIZE__DEFAULT_OPTIONS)); WUFFS_INITIALIZE__DEFAULT_OPTIONS));
dec.private_impl.f_width = width; dec.private_impl.f_width = width;
dec.private_impl.f_height = height; dec.private_impl.f_height = height;
dec.private_impl.f_bytes_per_row = bytes_per_row; dec.private_impl.f_pass_bytes_per_row = bytes_per_row;
dec.private_impl.f_filter_distance = filter_distance; dec.private_impl.f_filter_distance = filter_distance;
wuffs_png__decoder__choose_filter_implementations(&dec); wuffs_png__decoder__choose_filter_implementations(&dec);