Add some GIF per-frame fields

This commit is contained in:
Nigel Tao committed 2018-02-15 12:41:13 +11:00
1 parent b65526cdcd
commit fb2430e765
4 files changed
+197 -65

No files matched your search

+157 -46
View File
@@ -280,8 +280,15 @@ typedef struct {
uint8_t f_background_color_index; uint8_t f_background_color_index;
uint8_t f_block_type; uint8_t f_block_type;
bool f_peek_block_type; bool f_peek_block_type;
bool f_have_gct;
bool f_have_lct;
bool f_interlace;
bool f_seen_num_loops; bool f_seen_num_loops;
uint32_t f_num_loops; uint32_t f_num_loops;
uint32_t f_frame_top;
uint32_t f_frame_left;
uint32_t f_frame_width;
uint32_t f_frame_height;
uint8_t f_gct[768]; uint8_t f_gct[768];
uint8_t f_lct[768]; uint8_t f_lct[768];
wuffs_gif__lzw_decoder f_lzw; wuffs_gif__lzw_decoder f_lzw;
@@ -320,13 +327,13 @@ typedef struct {
} c_decode_ae[1]; } c_decode_ae[1];
struct { struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
uint8_t v_c[9]; uint8_t v_flags;
uint32_t v_i;
bool v_interlace;
uint32_t v_lct_size; uint32_t v_lct_size;
uint32_t v_i;
uint8_t v_lw; uint8_t v_lw;
uint64_t v_block_size; uint64_t v_block_size;
wuffs_gif__status v_z; wuffs_gif__status v_z;
uint64_t scratch;
} c_decode_id[1]; } c_decode_id[1];
} private_impl; } private_impl;
} wuffs_gif__decoder; } wuffs_gif__decoder;
@@ -1283,7 +1290,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_lsd(
self->private_impl.f_height = self->private_impl.f_height =
(((uint32_t)(v_c[2])) | (((uint32_t)(v_c[3])) << 8)); (((uint32_t)(v_c[2])) | (((uint32_t)(v_c[3])) << 8));
self->private_impl.f_background_color_index = v_c[5]; self->private_impl.f_background_color_index = v_c[5];
if ((v_c[4] & 128) != 0) { self->private_impl.f_have_gct = ((v_c[4] & 128) != 0);
if (self->private_impl.f_have_gct) {
v_gct_size = (((uint32_t)(1)) << (1 + (v_c[4] & 7))); v_gct_size = (((uint32_t)(1)) << (1 + (v_c[4] & 7)));
v_i = 0; v_i = 0;
while (v_i < v_gct_size) { while (v_i < v_gct_size) {
@@ -1789,10 +1797,9 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
wuffs_base__reader1 a_src) { wuffs_base__reader1 a_src) {
wuffs_gif__status status = WUFFS_GIF__STATUS_OK; wuffs_gif__status status = WUFFS_GIF__STATUS_OK;
uint8_t v_c[9]; uint8_t v_flags;
uint32_t v_i;
bool v_interlace;
uint32_t v_lct_size; uint32_t v_lct_size;
uint32_t v_i;
uint8_t v_lw; uint8_t v_lw;
uint64_t v_block_size; uint64_t v_block_size;
wuffs_base__reader1 v_r; wuffs_base__reader1 v_r;
@@ -1816,10 +1823,9 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
uint32_t coro_susp_point = self->private_impl.c_decode_id[0].coro_susp_point; uint32_t coro_susp_point = self->private_impl.c_decode_id[0].coro_susp_point;
if (coro_susp_point) { if (coro_susp_point) {
memcpy(v_c, self->private_impl.c_decode_id[0].v_c, sizeof(v_c)); v_flags = self->private_impl.c_decode_id[0].v_flags;
v_i = self->private_impl.c_decode_id[0].v_i;
v_interlace = self->private_impl.c_decode_id[0].v_interlace;
v_lct_size = self->private_impl.c_decode_id[0].v_lct_size; v_lct_size = self->private_impl.c_decode_id[0].v_lct_size;
v_i = self->private_impl.c_decode_id[0].v_i;
v_lw = self->private_impl.c_decode_id[0].v_lw; v_lw = self->private_impl.c_decode_id[0].v_lw;
v_block_size = self->private_impl.c_decode_id[0].v_block_size; v_block_size = self->private_impl.c_decode_id[0].v_block_size;
v_r = ((wuffs_base__reader1){}); v_r = ((wuffs_base__reader1){});
@@ -1828,60 +1834,166 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
switch (coro_susp_point) { switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
memset(v_c, 0, sizeof(v_c)); {
v_i = 0; WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
while (v_i < 9) { uint16_t t_1;
{ if (WUFFS_BASE__LIKELY(b_rend_src - b_rptr_src >= 2)) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); t_1 = wuffs_base__load_u16le(b_rptr_src);
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) { b_rptr_src += 2;
goto short_read_src; } else {
self->private_impl.c_decode_id[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
while (true) {
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
uint32_t t_0 = self->private_impl.c_decode_id[0].scratch >> 56;
self->private_impl.c_decode_id[0].scratch <<= 8;
self->private_impl.c_decode_id[0].scratch >>= 8;
self->private_impl.c_decode_id[0].scratch |=
((uint64_t)(*b_rptr_src++)) << t_0;
if (t_0 == 8) {
t_1 = self->private_impl.c_decode_id[0].scratch;
break;
}
t_0 += 8;
self->private_impl.c_decode_id[0].scratch |= ((uint64_t)(t_0)) << 56;
} }
uint8_t t_0 = *b_rptr_src++;
v_c[v_i] = t_0;
} }
v_i += 1; self->private_impl.f_frame_left = ((uint32_t)(t_1));
} }
v_interlace = ((v_c[8] & 64) != 0); {
if (v_interlace) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
uint16_t t_3;
if (WUFFS_BASE__LIKELY(b_rend_src - b_rptr_src >= 2)) {
t_3 = wuffs_base__load_u16le(b_rptr_src);
b_rptr_src += 2;
} else {
self->private_impl.c_decode_id[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
while (true) {
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
uint32_t t_2 = self->private_impl.c_decode_id[0].scratch >> 56;
self->private_impl.c_decode_id[0].scratch <<= 8;
self->private_impl.c_decode_id[0].scratch >>= 8;
self->private_impl.c_decode_id[0].scratch |=
((uint64_t)(*b_rptr_src++)) << t_2;
if (t_2 == 8) {
t_3 = self->private_impl.c_decode_id[0].scratch;
break;
}
t_2 += 8;
self->private_impl.c_decode_id[0].scratch |= ((uint64_t)(t_2)) << 56;
}
}
self->private_impl.f_frame_top = ((uint32_t)(t_3));
} }
if ((v_c[8] & 128) != 0) { {
v_lct_size = (((uint32_t)(1)) << (1 + (v_c[8] & 7))); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
uint16_t t_5;
if (WUFFS_BASE__LIKELY(b_rend_src - b_rptr_src >= 2)) {
t_5 = wuffs_base__load_u16le(b_rptr_src);
b_rptr_src += 2;
} else {
self->private_impl.c_decode_id[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
while (true) {
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
uint32_t t_4 = self->private_impl.c_decode_id[0].scratch >> 56;
self->private_impl.c_decode_id[0].scratch <<= 8;
self->private_impl.c_decode_id[0].scratch >>= 8;
self->private_impl.c_decode_id[0].scratch |=
((uint64_t)(*b_rptr_src++)) << t_4;
if (t_4 == 8) {
t_5 = self->private_impl.c_decode_id[0].scratch;
break;
}
t_4 += 8;
self->private_impl.c_decode_id[0].scratch |= ((uint64_t)(t_4)) << 56;
}
}
self->private_impl.f_frame_width = ((uint32_t)(t_5));
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
uint16_t t_7;
if (WUFFS_BASE__LIKELY(b_rend_src - b_rptr_src >= 2)) {
t_7 = wuffs_base__load_u16le(b_rptr_src);
b_rptr_src += 2;
} else {
self->private_impl.c_decode_id[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8);
while (true) {
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
uint32_t t_6 = self->private_impl.c_decode_id[0].scratch >> 56;
self->private_impl.c_decode_id[0].scratch <<= 8;
self->private_impl.c_decode_id[0].scratch >>= 8;
self->private_impl.c_decode_id[0].scratch |=
((uint64_t)(*b_rptr_src++)) << t_6;
if (t_6 == 8) {
t_7 = self->private_impl.c_decode_id[0].scratch;
break;
}
t_6 += 8;
self->private_impl.c_decode_id[0].scratch |= ((uint64_t)(t_6)) << 56;
}
}
self->private_impl.f_frame_height = ((uint32_t)(t_7));
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9);
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
uint8_t t_8 = *b_rptr_src++;
v_flags = t_8;
}
self->private_impl.f_interlace = ((v_flags & 64) != 0);
self->private_impl.f_have_lct = ((v_flags & 128) != 0);
if (self->private_impl.f_have_lct) {
v_lct_size = (((uint32_t)(1)) << (1 + (v_flags & 7)));
v_i = 0; v_i = 0;
while (v_i < v_lct_size) { while (v_i < v_lct_size) {
{ {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10);
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) { if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src; goto short_read_src;
} }
uint8_t t_1 = *b_rptr_src++; uint8_t t_9 = *b_rptr_src++;
self->private_impl.f_lct[(3 * v_i) + 0] = t_1; self->private_impl.f_lct[(3 * v_i) + 0] = t_9;
} }
{ {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11);
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) { if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src; goto short_read_src;
} }
uint8_t t_2 = *b_rptr_src++; uint8_t t_10 = *b_rptr_src++;
self->private_impl.f_lct[(3 * v_i) + 1] = t_2; self->private_impl.f_lct[(3 * v_i) + 1] = t_10;
} }
{ {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(12);
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) { if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src; goto short_read_src;
} }
uint8_t t_3 = *b_rptr_src++; uint8_t t_11 = *b_rptr_src++;
self->private_impl.f_lct[(3 * v_i) + 2] = t_3; self->private_impl.f_lct[(3 * v_i) + 2] = t_11;
} }
v_i += 1; v_i += 1;
} }
} }
{ {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(13);
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) { if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src; goto short_read_src;
} }
uint8_t t_4 = *b_rptr_src++; uint8_t t_12 = *b_rptr_src++;
v_lw = t_4; v_lw = t_12;
} }
if ((v_lw < 2) || (8 < v_lw)) { if ((v_lw < 2) || (8 < v_lw)) {
status = WUFFS_GIF__ERROR_BAD_LZW_LITERAL_WIDTH; status = WUFFS_GIF__ERROR_BAD_LZW_LITERAL_WIDTH;
@@ -1891,12 +2003,12 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
((uint32_t)(v_lw))); ((uint32_t)(v_lw)));
while (true) { while (true) {
{ {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(14);
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) { if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src; goto short_read_src;
} }
uint8_t t_5 = *b_rptr_src++; uint8_t t_13 = *b_rptr_src++;
v_block_size = ((uint64_t)(t_5)); v_block_size = ((uint64_t)(t_13));
} }
if (v_block_size == 0) { if (v_block_size == 0) {
goto label_0_break; goto label_0_break;
@@ -1905,7 +2017,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
v_r = a_src; v_r = a_src;
wuffs_base__reader1__mark(&v_r, b_rptr_src); wuffs_base__reader1__mark(&v_r, b_rptr_src);
{ {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(15);
if (a_src.buf) { if (a_src.buf) {
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
a_src.buf->ri += n; a_src.buf->ri += n;
@@ -1917,13 +2029,13 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
} }
} }
uint64_t l_rlimit0 = v_block_size; uint64_t l_rlimit0 = v_block_size;
wuffs_gif__status t_6 = wuffs_gif__lzw_decoder__decode( wuffs_gif__status t_14 = wuffs_gif__lzw_decoder__decode(
&self->private_impl.f_lzw, a_dst, &self->private_impl.f_lzw, a_dst,
wuffs_base__reader1__limit(&v_r, &l_rlimit0)); wuffs_base__reader1__limit(&v_r, &l_rlimit0));
if (a_src.buf) { if (a_src.buf) {
b_rptr_src = a_src.buf->ptr + a_src.buf->ri; b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
} }
v_z = t_6; v_z = t_14;
} }
if (v_z == 0) { if (v_z == 0) {
goto label_1_break; goto label_1_break;
@@ -1952,7 +2064,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
goto label_1_break; goto label_1_break;
} }
status = v_z; status = v_z;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(8); WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(16);
} }
label_1_break:; label_1_break:;
} }
@@ -1967,10 +2079,9 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
goto suspend; goto suspend;
suspend: suspend:
self->private_impl.c_decode_id[0].coro_susp_point = coro_susp_point; self->private_impl.c_decode_id[0].coro_susp_point = coro_susp_point;
memcpy(self->private_impl.c_decode_id[0].v_c, v_c, sizeof(v_c)); self->private_impl.c_decode_id[0].v_flags = v_flags;
self->private_impl.c_decode_id[0].v_i = v_i;
self->private_impl.c_decode_id[0].v_interlace = v_interlace;
self->private_impl.c_decode_id[0].v_lct_size = v_lct_size; self->private_impl.c_decode_id[0].v_lct_size = v_lct_size;
self->private_impl.c_decode_id[0].v_i = v_i;
self->private_impl.c_decode_id[0].v_lw = v_lw; self->private_impl.c_decode_id[0].v_lw = v_lw;
self->private_impl.c_decode_id[0].v_block_size = v_block_size; self->private_impl.c_decode_id[0].v_block_size = v_block_size;
self->private_impl.c_decode_id[0].v_z = v_z; self->private_impl.c_decode_id[0].v_z = v_z;
+10 -3
View File
@@ -280,8 +280,15 @@ typedef struct {
uint8_t f_background_color_index; uint8_t f_background_color_index;
uint8_t f_block_type; uint8_t f_block_type;
bool f_peek_block_type; bool f_peek_block_type;
bool f_have_gct;
bool f_have_lct;
bool f_interlace;
bool f_seen_num_loops; bool f_seen_num_loops;
uint32_t f_num_loops; uint32_t f_num_loops;
uint32_t f_frame_top;
uint32_t f_frame_left;
uint32_t f_frame_width;
uint32_t f_frame_height;
uint8_t f_gct[768]; uint8_t f_gct[768];
uint8_t f_lct[768]; uint8_t f_lct[768];
wuffs_gif__lzw_decoder f_lzw; wuffs_gif__lzw_decoder f_lzw;
@@ -320,13 +327,13 @@ typedef struct {
} c_decode_ae[1]; } c_decode_ae[1];
struct { struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
uint8_t v_c[9]; uint8_t v_flags;
uint32_t v_i;
bool v_interlace;
uint32_t v_lct_size; uint32_t v_lct_size;
uint32_t v_i;
uint8_t v_lw; uint8_t v_lw;
uint64_t v_block_size; uint64_t v_block_size;
wuffs_gif__status v_z; wuffs_gif__status v_z;
uint64_t scratch;
} c_decode_id[1]; } c_decode_id[1];
} private_impl; } private_impl;
} wuffs_gif__decoder; } wuffs_gif__decoder;
+25 -16
View File
@@ -35,11 +35,21 @@ pub struct decoder?(
block_type u8, block_type u8,
peek_block_type bool, peek_block_type bool,
have_gct bool,
have_lct bool,
interlace bool,
// Absent a NETSCAPE2.0 extension, the implicit number of animation loops // Absent a NETSCAPE2.0 extension, the implicit number of animation loops
// is 1. // is 1.
seen_num_loops bool, seen_num_loops bool,
num_loops u32 = 1, num_loops u32 = 1,
frame_top u32[..65535],
frame_left u32[..65535],
frame_width u32[..65535],
frame_height u32[..65535],
// gct and lct are the Global / Local Color Tables: 256 (R, G, B) entries. // gct and lct are the Global / Local Color Tables: 256 (R, G, B) entries.
// //
// TODO: 4 byte per pixel RGBA or BGRA instead of 3 bpp RGB? // TODO: 4 byte per pixel RGBA or BGRA instead of 3 bpp RGB?
@@ -130,12 +140,14 @@ pri func decoder.decode_lsd?(src reader1)() {
c[i] = in.src.read_u8?() c[i] = in.src.read_u8?()
i += 1 i += 1
} }
// TODO: use read_u16le.
this.width = (c[0] as u32) | ((c[1] as u32) << 8) this.width = (c[0] as u32) | ((c[1] as u32) << 8)
this.height = (c[2] as u32) | ((c[3] as u32) << 8) this.height = (c[2] as u32) | ((c[3] as u32) << 8)
this.background_color_index = c[5] this.background_color_index = c[5]
// Read the optional Global Color Table. // Read the optional Global Color Table.
if (c[4] & 0x80) != 0 { this.have_gct = (c[4] & 0x80) != 0
if this.have_gct {
var gct_size u32[..256] = (1 as u32) << (1 + (c[4] & 0x07)) var gct_size u32[..256] = (1 as u32) << (1 + (c[4] & 0x07))
i = 0 i = 0
while i < gct_size { while i < gct_size {
@@ -250,24 +262,21 @@ pri func decoder.decode_ae?(src reader1)() {
// //
// See the spec section 20 "Image Descriptor" on page 11. // See the spec section 20 "Image Descriptor" on page 11.
pri func decoder.decode_id?(dst writer1, src reader1)() { pri func decoder.decode_id?(dst writer1, src reader1)() {
var c[9] u8 // TODO: check that the frame rect is inside the image rect??
var i u32 this.frame_left = in.src.read_u16le?() as u32
while i < 9 { this.frame_top = in.src.read_u16le?() as u32
c[i] = in.src.read_u8?() this.frame_width = in.src.read_u16le?() as u32
i += 1 this.frame_height = in.src.read_u16le?() as u32
}
// TODO: use image left/top/width/height. // TODO: use this.interlace.
var flags u8 = in.src.read_u8?()
// TODO: use interlace. this.interlace = (flags & 0x40) != 0
var interlace bool = (c[8] & 0x40) != 0
if interlace { // Avoid "unused variable" C compiler warning.
}
// Read the optional Local Color Table. // Read the optional Local Color Table.
if (c[8] & 0x80) != 0 { this.have_lct = (flags & 0x80) != 0
var lct_size u32[..256] = (1 as u32) << (1 + (c[8] & 0x07)) if this.have_lct {
i = 0 var lct_size u32[..256] = (1 as u32) << (1 + (flags & 0x07))
var i u32
while i < lct_size { while i < lct_size {
assert i < 256 via "a < b: a < c; c <= b"(c:lct_size) assert i < 256 via "a < b: a < c; c <= b"(c:lct_size)
this.lct[(3 * i) + 0] = in.src.read_u8?() this.lct[(3 * i) + 0] = in.src.read_u8?()
+5
View File
@@ -598,6 +598,11 @@ void test_wuffs_gif_decode_animated() {
wuffs_gif__status__string(status)); wuffs_gif__status__string(status));
return; return;
} }
// TODO: check that the frame top/left/width/height is:
// - 0, 0,64,48 for frame #0
// - 15,31,37, 9 for frame #1
// - 15, 0,49,40 for frame #2
// - 15, 0,49,40 for frame #3
} }
// There should be no more frames. // There should be no more frames.