mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Re-org GIF Image Descriptor parsing
This commit is contained in:
1 parent
87ee928448
commit
75195af7db
3 files changed
+135
-88
No files matched your search
@@ -2460,6 +2460,7 @@ typedef struct {
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_lw;
|
||||
uint64_t scratch;
|
||||
} c_skip_frame[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
@@ -2507,16 +2508,17 @@ typedef struct {
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_flags;
|
||||
uint32_t v_num_palette_entries;
|
||||
uint32_t v_i;
|
||||
uint32_t v_argb;
|
||||
uint64_t scratch;
|
||||
} c_decode_id_part0[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint32_t v_num_palette_entries;
|
||||
uint32_t v_i;
|
||||
uint32_t v_argb;
|
||||
uint8_t v_lw;
|
||||
uint64_t v_block_size;
|
||||
wuffs_base__status v_z;
|
||||
uint64_t scratch;
|
||||
} c_decode_id_part1[1];
|
||||
} private_impl;
|
||||
|
||||
@@ -6645,8 +6647,22 @@ wuffs_gif__decoder__skip_frame(wuffs_gif__decoder* self,
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
{
|
||||
if (self->private_impl.f_local_palette_log2_size != 0) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
self->private_impl.c_skip_frame[0].scratch =
|
||||
(((uint32_t)(3)) << self->private_impl.f_local_palette_log2_size);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
if (self->private_impl.c_skip_frame[0].scratch >
|
||||
((uint64_t)(io1_a_src - iop_a_src))) {
|
||||
self->private_impl.c_skip_frame[0].scratch -= io1_a_src - iop_a_src;
|
||||
iop_a_src = io1_a_src;
|
||||
status = wuffs_base__suspension__short_read;
|
||||
goto suspend;
|
||||
}
|
||||
iop_a_src += self->private_impl.c_skip_frame[0].scratch;
|
||||
}
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) {
|
||||
status = wuffs_base__suspension__short_read;
|
||||
goto suspend;
|
||||
@@ -6654,7 +6670,7 @@ wuffs_gif__decoder__skip_frame(wuffs_gif__decoder* self,
|
||||
uint8_t t_0 = *iop_a_src++;
|
||||
v_lw = t_0;
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
|
||||
if (a_src.private_impl.buf) {
|
||||
a_src.private_impl.buf->ri = iop_a_src - a_src.private_impl.buf->ptr;
|
||||
}
|
||||
@@ -7682,9 +7698,6 @@ wuffs_gif__decoder__decode_id_part0(wuffs_gif__decoder* self,
|
||||
wuffs_base__status status = NULL;
|
||||
|
||||
uint8_t v_flags;
|
||||
uint32_t v_num_palette_entries;
|
||||
uint32_t v_i;
|
||||
uint32_t v_argb;
|
||||
|
||||
uint8_t* iop_a_src = NULL;
|
||||
uint8_t* io0_a_src = NULL;
|
||||
@@ -7706,10 +7719,6 @@ wuffs_gif__decoder__decode_id_part0(wuffs_gif__decoder* self,
|
||||
self->private_impl.c_decode_id_part0[0].coro_susp_point;
|
||||
if (coro_susp_point) {
|
||||
v_flags = self->private_impl.c_decode_id_part0[0].v_flags;
|
||||
v_num_palette_entries =
|
||||
self->private_impl.c_decode_id_part0[0].v_num_palette_entries;
|
||||
v_i = self->private_impl.c_decode_id_part0[0].v_i;
|
||||
v_argb = self->private_impl.c_decode_id_part0[0].v_argb;
|
||||
} else {
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
@@ -7859,59 +7868,6 @@ wuffs_gif__decoder__decode_id_part0(wuffs_gif__decoder* self,
|
||||
if ((v_flags & 128) != 0) {
|
||||
self->private_impl.f_local_palette_log2_size = (1 + (v_flags & 7));
|
||||
}
|
||||
if (self->private_impl.f_local_palette_log2_size != 0) {
|
||||
v_num_palette_entries =
|
||||
(((uint32_t)(1)) << self->private_impl.f_local_palette_log2_size);
|
||||
v_i = 0;
|
||||
while (v_i < v_num_palette_entries) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10);
|
||||
uint32_t t_10;
|
||||
if (WUFFS_BASE__LIKELY(io1_a_src - iop_a_src >= 3)) {
|
||||
t_10 = wuffs_base__load_u24be(iop_a_src);
|
||||
iop_a_src += 3;
|
||||
} else {
|
||||
self->private_impl.c_decode_id_part0[0].scratch = 0;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11);
|
||||
while (true) {
|
||||
if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) {
|
||||
status = wuffs_base__suspension__short_read;
|
||||
goto suspend;
|
||||
}
|
||||
uint64_t* scratch =
|
||||
&self->private_impl.c_decode_id_part0[0].scratch;
|
||||
uint32_t t_9 = *scratch & 0xFF;
|
||||
*scratch >>= 8;
|
||||
*scratch <<= 8;
|
||||
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - t_9);
|
||||
if (t_9 == 16) {
|
||||
t_10 = *scratch >> (64 - 24);
|
||||
break;
|
||||
}
|
||||
t_9 += 8;
|
||||
*scratch |= ((uint64_t)(t_9));
|
||||
}
|
||||
}
|
||||
v_argb = (t_10 | 4278190080);
|
||||
}
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 0)] =
|
||||
((uint8_t)(((v_argb >> 0) & 255)));
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 1)] =
|
||||
((uint8_t)(((v_argb >> 8) & 255)));
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 2)] =
|
||||
((uint8_t)(((v_argb >> 16) & 255)));
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 3)] =
|
||||
((uint8_t)(((v_argb >> 24) & 255)));
|
||||
v_i += 1;
|
||||
}
|
||||
while (v_i < 256) {
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 0)] = 0;
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 1)] = 0;
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 2)] = 0;
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 3)] = 255;
|
||||
v_i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
goto ok;
|
||||
ok:
|
||||
@@ -7923,10 +7879,6 @@ wuffs_gif__decoder__decode_id_part0(wuffs_gif__decoder* self,
|
||||
suspend:
|
||||
self->private_impl.c_decode_id_part0[0].coro_susp_point = coro_susp_point;
|
||||
self->private_impl.c_decode_id_part0[0].v_flags = v_flags;
|
||||
self->private_impl.c_decode_id_part0[0].v_num_palette_entries =
|
||||
v_num_palette_entries;
|
||||
self->private_impl.c_decode_id_part0[0].v_i = v_i;
|
||||
self->private_impl.c_decode_id_part0[0].v_argb = v_argb;
|
||||
|
||||
goto exit;
|
||||
exit:
|
||||
@@ -7945,6 +7897,9 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self,
|
||||
wuffs_base__io_reader a_src) {
|
||||
wuffs_base__status status = NULL;
|
||||
|
||||
uint32_t v_num_palette_entries;
|
||||
uint32_t v_i;
|
||||
uint32_t v_argb;
|
||||
uint8_t v_lw;
|
||||
uint64_t v_block_size;
|
||||
wuffs_base__io_writer v_w;
|
||||
@@ -7975,6 +7930,10 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self,
|
||||
uint32_t coro_susp_point =
|
||||
self->private_impl.c_decode_id_part1[0].coro_susp_point;
|
||||
if (coro_susp_point) {
|
||||
v_num_palette_entries =
|
||||
self->private_impl.c_decode_id_part1[0].v_num_palette_entries;
|
||||
v_i = self->private_impl.c_decode_id_part1[0].v_i;
|
||||
v_argb = self->private_impl.c_decode_id_part1[0].v_argb;
|
||||
v_lw = self->private_impl.c_decode_id_part1[0].v_lw;
|
||||
v_block_size = self->private_impl.c_decode_id_part1[0].v_block_size;
|
||||
v_w = ((wuffs_base__io_writer){});
|
||||
@@ -7985,6 +7944,59 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self,
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
if (self->private_impl.f_local_palette_log2_size != 0) {
|
||||
v_num_palette_entries =
|
||||
(((uint32_t)(1)) << self->private_impl.f_local_palette_log2_size);
|
||||
v_i = 0;
|
||||
while (v_i < v_num_palette_entries) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
uint32_t t_1;
|
||||
if (WUFFS_BASE__LIKELY(io1_a_src - iop_a_src >= 3)) {
|
||||
t_1 = wuffs_base__load_u24be(iop_a_src);
|
||||
iop_a_src += 3;
|
||||
} else {
|
||||
self->private_impl.c_decode_id_part1[0].scratch = 0;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
while (true) {
|
||||
if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) {
|
||||
status = wuffs_base__suspension__short_read;
|
||||
goto suspend;
|
||||
}
|
||||
uint64_t* scratch =
|
||||
&self->private_impl.c_decode_id_part1[0].scratch;
|
||||
uint32_t t_0 = *scratch & 0xFF;
|
||||
*scratch >>= 8;
|
||||
*scratch <<= 8;
|
||||
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - t_0);
|
||||
if (t_0 == 16) {
|
||||
t_1 = *scratch >> (64 - 24);
|
||||
break;
|
||||
}
|
||||
t_0 += 8;
|
||||
*scratch |= ((uint64_t)(t_0));
|
||||
}
|
||||
}
|
||||
v_argb = (t_1 | 4278190080);
|
||||
}
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 0)] =
|
||||
((uint8_t)(((v_argb >> 0) & 255)));
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 1)] =
|
||||
((uint8_t)(((v_argb >> 8) & 255)));
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 2)] =
|
||||
((uint8_t)(((v_argb >> 16) & 255)));
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 3)] =
|
||||
((uint8_t)(((v_argb >> 24) & 255)));
|
||||
v_i += 1;
|
||||
}
|
||||
while (v_i < 256) {
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 0)] = 0;
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 1)] = 0;
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 2)] = 0;
|
||||
self->private_impl.f_palettes[1][((4 * v_i) + 3)] = 255;
|
||||
v_i += 1;
|
||||
}
|
||||
}
|
||||
if (self->private_impl.f_gc_has_transparent_index) {
|
||||
if (self->private_impl.f_local_palette_log2_size == 0) {
|
||||
wuffs_base__slice_u8__copy_from_slice(
|
||||
@@ -8020,13 +8032,13 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self,
|
||||
wuffs_base__return_empty_struct());
|
||||
}
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) {
|
||||
status = wuffs_base__suspension__short_read;
|
||||
goto suspend;
|
||||
}
|
||||
uint8_t t_0 = *iop_a_src++;
|
||||
v_lw = t_0;
|
||||
uint8_t t_2 = *iop_a_src++;
|
||||
v_lw = t_2;
|
||||
}
|
||||
if ((v_lw < 2) || (8 < v_lw)) {
|
||||
status = wuffs_gif__error__bad_literal_width;
|
||||
@@ -8037,13 +8049,13 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self,
|
||||
self->private_impl.f_previous_lzw_decode_ended_abruptly = true;
|
||||
while (true) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
|
||||
if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) {
|
||||
status = wuffs_base__suspension__short_read;
|
||||
goto suspend;
|
||||
}
|
||||
uint8_t t_1 = *iop_a_src++;
|
||||
v_block_size = ((uint64_t)(t_1));
|
||||
uint8_t t_3 = *iop_a_src++;
|
||||
v_block_size = ((uint64_t)(t_3));
|
||||
}
|
||||
if (v_block_size == 0) {
|
||||
goto label_0_break;
|
||||
@@ -8071,14 +8083,14 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self,
|
||||
a_src.private_impl.buf->ri =
|
||||
iop_a_src - a_src.private_impl.buf->ptr;
|
||||
}
|
||||
wuffs_base__status t_2 = wuffs_lzw__decoder__decode(
|
||||
wuffs_base__status t_4 = wuffs_lzw__decoder__decode(
|
||||
&self->private_impl.f_lzw, v_w, a_src);
|
||||
iop_v_w = u_w.ptr + u_w.wi;
|
||||
if (a_src.private_impl.buf) {
|
||||
iop_a_src =
|
||||
a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri;
|
||||
}
|
||||
v_z = t_2;
|
||||
v_z = t_4;
|
||||
}
|
||||
self->private_impl.f_uncompressed_wi =
|
||||
(4096 - ((uint32_t)(wuffs_base__u64__min(
|
||||
@@ -8098,7 +8110,7 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self,
|
||||
}
|
||||
if (wuffs_base__status__is_ok(v_z) ||
|
||||
(v_z == wuffs_base__suspension__short_write)) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
|
||||
status = wuffs_gif__decoder__copy_to_image_buffer(self, a_dst);
|
||||
if (status) {
|
||||
goto suspend;
|
||||
@@ -8114,14 +8126,14 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self,
|
||||
goto label_1_break;
|
||||
}
|
||||
status = v_z;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(4);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(6);
|
||||
}
|
||||
label_1_break:;
|
||||
}
|
||||
label_0_break:;
|
||||
if (self->private_impl.f_uncompressed_ri !=
|
||||
self->private_impl.f_uncompressed_wi) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
|
||||
status = wuffs_gif__decoder__copy_to_image_buffer(self, a_dst);
|
||||
if (status) {
|
||||
goto suspend;
|
||||
@@ -8137,6 +8149,10 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self,
|
||||
goto suspend;
|
||||
suspend:
|
||||
self->private_impl.c_decode_id_part1[0].coro_susp_point = coro_susp_point;
|
||||
self->private_impl.c_decode_id_part1[0].v_num_palette_entries =
|
||||
v_num_palette_entries;
|
||||
self->private_impl.c_decode_id_part1[0].v_i = v_i;
|
||||
self->private_impl.c_decode_id_part1[0].v_argb = v_argb;
|
||||
self->private_impl.c_decode_id_part1[0].v_lw = v_lw;
|
||||
self->private_impl.c_decode_id_part1[0].v_block_size = v_block_size;
|
||||
self->private_impl.c_decode_id_part1[0].v_z = v_z;
|
||||
|
||||
@@ -225,6 +225,11 @@ pub func decoder.decode_frame_config!??(dst nptr base.frame_config, src base.io_
|
||||
}
|
||||
|
||||
pri func decoder.skip_frame!??(src base.io_reader) {
|
||||
// Skip the optional Local Color Table, 3 bytes (RGB) per entry.
|
||||
if this.local_palette_log2_size != 0 {
|
||||
args.src.skip!??(n:(3 as base.u32) << this.local_palette_log2_size)
|
||||
}
|
||||
|
||||
var lw base.u8 = args.src.read_u8!??()
|
||||
this.skip_blocks!??(src:args.src)
|
||||
|
||||
@@ -512,11 +517,14 @@ pri func decoder.decode_id_part0!??(src base.io_reader) {
|
||||
this.interlace = 0
|
||||
}
|
||||
|
||||
// Read the optional Local Color Table.
|
||||
this.local_palette_log2_size = 0
|
||||
if (flags & 0x80) != 0 {
|
||||
this.local_palette_log2_size = 1 + (flags & 0x07)
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.decode_id_part1!??(dst ptr base.pixel_buffer, src base.io_reader) {
|
||||
// Read the optional Local Color Table.
|
||||
if this.local_palette_log2_size != 0 {
|
||||
var num_palette_entries base.u32[..256] = (1 as base.u32) << this.local_palette_log2_size
|
||||
var i base.u32
|
||||
@@ -540,9 +548,7 @@ pri func decoder.decode_id_part0!??(src base.io_reader) {
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.decode_id_part1!??(dst ptr base.pixel_buffer, src base.io_reader) {
|
||||
if this.gc_has_transparent_index {
|
||||
if this.local_palette_log2_size == 0 {
|
||||
this.palettes[1][:].copy_from_slice(s:this.palettes[0][:])
|
||||
|
||||
+30
-5
@@ -809,13 +809,13 @@ bool do_test_wuffs_gif_io_position(bool chunked) {
|
||||
wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src);
|
||||
|
||||
if (chunked) {
|
||||
if (src.wi < 20) {
|
||||
if (src.wi < 50) {
|
||||
FAIL("src is too short");
|
||||
return false;
|
||||
}
|
||||
size_t saved_wi = src.wi;
|
||||
bool saved_closed = src.closed;
|
||||
src.wi = 10;
|
||||
src.wi = 30;
|
||||
src.closed = false;
|
||||
|
||||
z = wuffs_gif__decoder__decode_image_config(&dec, NULL, src_reader);
|
||||
@@ -826,7 +826,16 @@ bool do_test_wuffs_gif_io_position(bool chunked) {
|
||||
|
||||
src.wi = saved_wi;
|
||||
src.closed = saved_closed;
|
||||
|
||||
if (src.pos != 0) {
|
||||
FAIL("src.pos: got %" PRIu64 ", want zero", src.pos);
|
||||
return false;
|
||||
}
|
||||
wuffs_base__io_buffer__compact(&src);
|
||||
if (src.pos == 0) {
|
||||
FAIL("src.pos: got %" PRIu64 ", want non-zero", src.pos);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
z = wuffs_gif__decoder__decode_image_config(&dec, NULL, src_reader);
|
||||
@@ -835,7 +844,7 @@ bool do_test_wuffs_gif_io_position(bool chunked) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint64_t wants[4] = {1586, 2144, 2205, 2560};
|
||||
uint64_t wants[4] = {818, 2144, 2205, 2560};
|
||||
int i;
|
||||
for (i = 0; i < 4; i++) {
|
||||
wuffs_base__frame_config fc = ((wuffs_base__frame_config){});
|
||||
@@ -847,8 +856,24 @@ bool do_test_wuffs_gif_io_position(bool chunked) {
|
||||
uint64_t got = wuffs_base__frame_config__io_position(&fc);
|
||||
uint64_t want = wants[i];
|
||||
if (got != want) {
|
||||
FAIL("decode_frame_config #%d: got %" PRIu64 ", want %" PRIu64, i, got,
|
||||
want);
|
||||
FAIL("io_position #%d: got %" PRIu64 ", want %" PRIu64, i, got, want);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Look for the 0x2C byte that's the start of a GIF's Image Descriptor, 10
|
||||
// bytes before the frame_config's I/O position.
|
||||
if ((got < 10) || (got - 10 < src.pos)) {
|
||||
FAIL("io_position #%d: got %" PRIu64 ", was too small", i, got);
|
||||
return false;
|
||||
}
|
||||
uint64_t index = got - 10 - src.pos;
|
||||
if (index >= src.wi) {
|
||||
FAIL("io_position #%d: got %" PRIu64 ", was too large", i, got);
|
||||
return false;
|
||||
}
|
||||
uint8_t x = src.ptr[index];
|
||||
if (x != 0x2C) {
|
||||
FAIL("Image Descriptor byte #%d: got 0x%02X, want 0x2C", i, (int)x);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user