mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +01:00
Decode a GIF's loop count
This commit is contained in:
1 parent
ded38a043a
commit
584fc631a8
5 files changed
+636
-60
No files matched your search
@@ -549,6 +549,14 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
|
||||
}
|
||||
b.writes("if (status) { goto suspend; }\n")
|
||||
|
||||
} else if isThisMethod(g.tm, n, "decode_ae", 1) {
|
||||
b.printf("status = %s%s__decode_ae(self, %ssrc);\n",
|
||||
g.pkgPrefix, g.currFunk.astFunc.Receiver().Str(g.tm), aPrefix)
|
||||
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes("if (status) { goto suspend; }\n")
|
||||
|
||||
} else if isThisMethod(g.tm, n, "decode_id", 2) {
|
||||
b.printf("status = %s%s__decode_id(self, %sdst, %ssrc);\n",
|
||||
g.pkgPrefix, g.currFunk.astFunc.Receiver().Str(g.tm), aPrefix, aPrefix)
|
||||
@@ -557,6 +565,14 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
|
||||
}
|
||||
b.writes("if (status) { goto suspend; }\n")
|
||||
|
||||
} else if isThisMethod(g.tm, n, "skip_blocks", 1) {
|
||||
b.printf("status = %s%s__skip_blocks(self, %ssrc);\n",
|
||||
g.pkgPrefix, g.currFunk.astFunc.Receiver().Str(g.tm), aPrefix)
|
||||
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes("if (status) { goto suspend; }\n")
|
||||
|
||||
} else if isThisMethod(g.tm, n, "decode_blocks", 2) {
|
||||
// TODO: don't hard code being inside a try call.
|
||||
if g.currFunk.tempW > maxTemp {
|
||||
|
||||
+485
-46
@@ -280,6 +280,10 @@ typedef struct {
|
||||
uint32_t f_height;
|
||||
uint8_t f_call_sequence;
|
||||
uint8_t f_background_color_index;
|
||||
uint8_t f_block_type;
|
||||
bool f_peek_block_type;
|
||||
bool f_seen_num_loops;
|
||||
uint32_t f_num_loops;
|
||||
uint8_t f_gct[768];
|
||||
wuffs_gif__lzw_decoder f_lzw;
|
||||
|
||||
@@ -288,7 +292,6 @@ typedef struct {
|
||||
} c_decode_config[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_c;
|
||||
} c_decode_frame[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
@@ -304,9 +307,18 @@ typedef struct {
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_label;
|
||||
} c_decode_extension[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_block_size;
|
||||
uint64_t scratch;
|
||||
} c_decode_extension[1];
|
||||
} c_skip_blocks[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_c;
|
||||
uint8_t v_block_size;
|
||||
uint64_t scratch;
|
||||
} c_decode_ae[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_c[9];
|
||||
@@ -746,6 +758,10 @@ const char* wuffs_gif__status__string(wuffs_gif__status s) {
|
||||
|
||||
// ---------------- Private Consts
|
||||
|
||||
static const uint8_t wuffs_gif__netscape2dot0[11] = {
|
||||
78, 69, 84, 83, 67, 65, 80, 69, 50, 46, 48,
|
||||
};
|
||||
|
||||
// ---------------- Private Initializer Prototypes
|
||||
|
||||
void wuffs_gif__lzw_decoder__initialize(wuffs_gif__lzw_decoder* self,
|
||||
@@ -766,6 +782,14 @@ static wuffs_gif__status wuffs_gif__decoder__decode_extension(
|
||||
wuffs_gif__decoder* self,
|
||||
wuffs_base__reader1 a_src);
|
||||
|
||||
static wuffs_gif__status wuffs_gif__decoder__skip_blocks(
|
||||
wuffs_gif__decoder* self,
|
||||
wuffs_base__reader1 a_src);
|
||||
|
||||
static wuffs_gif__status wuffs_gif__decoder__decode_ae(
|
||||
wuffs_gif__decoder* self,
|
||||
wuffs_base__reader1 a_src);
|
||||
|
||||
static wuffs_gif__status wuffs_gif__decoder__decode_id(
|
||||
wuffs_gif__decoder* self,
|
||||
wuffs_base__writer1 a_dst,
|
||||
@@ -813,6 +837,7 @@ void wuffs_gif__decoder__initialize(wuffs_gif__decoder* self,
|
||||
memset(self, 0, sizeof(*self));
|
||||
}
|
||||
self->private_impl.magic = WUFFS_BASE__MAGIC;
|
||||
self->private_impl.f_num_loops = 1;
|
||||
wuffs_gif__lzw_decoder__initialize(&self->private_impl.f_lzw, WUFFS_VERSION,
|
||||
WUFFS_BASE__ALREADY_ZEROED);
|
||||
}
|
||||
@@ -838,6 +863,22 @@ wuffs_gif__status wuffs_gif__decoder__decode_config(
|
||||
goto exit;
|
||||
}
|
||||
|
||||
uint8_t* b_rptr_src = NULL;
|
||||
uint8_t* b_rstart_src = NULL;
|
||||
uint8_t* b_rend_src = NULL;
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
b_rstart_src = b_rptr_src;
|
||||
uint64_t len = a_src.buf->wi - a_src.buf->ri;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
||||
len = *lim->ptr_to_len;
|
||||
}
|
||||
}
|
||||
b_rend_src = b_rptr_src + len;
|
||||
}
|
||||
|
||||
uint32_t coro_susp_point =
|
||||
self->private_impl.c_decode_config[0].coro_susp_point;
|
||||
if (coro_susp_point) {
|
||||
@@ -850,15 +891,75 @@ wuffs_gif__status wuffs_gif__decoder__decode_config(
|
||||
goto exit;
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
}
|
||||
}
|
||||
}
|
||||
status = wuffs_gif__decoder__decode_header(self, a_src);
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
}
|
||||
if (status) {
|
||||
goto suspend;
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
}
|
||||
}
|
||||
}
|
||||
status = wuffs_gif__decoder__decode_lsd(self, a_src);
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
}
|
||||
if (status) {
|
||||
goto suspend;
|
||||
}
|
||||
while (true) {
|
||||
self->private_impl.f_peek_block_type = true;
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_0 = *b_rptr_src++;
|
||||
self->private_impl.f_block_type = t_0;
|
||||
}
|
||||
if (self->private_impl.f_seen_num_loops ||
|
||||
(self->private_impl.f_block_type != 33)) {
|
||||
goto label_0_break;
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
}
|
||||
}
|
||||
}
|
||||
status = wuffs_gif__decoder__decode_extension(self, a_src);
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
}
|
||||
if (status) {
|
||||
goto suspend;
|
||||
}
|
||||
}
|
||||
label_0_break:;
|
||||
wuffs_base__image_config__initialize(a_dst, self->private_impl.f_width,
|
||||
self->private_impl.f_height, 0);
|
||||
self->private_impl.f_call_sequence = 1;
|
||||
@@ -874,8 +975,29 @@ suspend:
|
||||
self->private_impl.c_decode_config[0].coro_susp_point = coro_susp_point;
|
||||
|
||||
exit:
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
}
|
||||
}
|
||||
WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
||||
WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
||||
}
|
||||
|
||||
self->private_impl.status = status;
|
||||
return status;
|
||||
|
||||
short_read_src:
|
||||
if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) {
|
||||
status = WUFFS_GIF__ERROR_UNEXPECTED_EOF;
|
||||
goto exit;
|
||||
}
|
||||
status = WUFFS_GIF__SUSPENSION_SHORT_READ;
|
||||
goto suspend;
|
||||
}
|
||||
|
||||
wuffs_gif__status wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self,
|
||||
@@ -892,8 +1014,6 @@ wuffs_gif__status wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self,
|
||||
}
|
||||
wuffs_gif__status status = WUFFS_GIF__STATUS_OK;
|
||||
|
||||
uint8_t v_c;
|
||||
|
||||
uint8_t* b_rptr_src = NULL;
|
||||
uint8_t* b_rstart_src = NULL;
|
||||
uint8_t* b_rend_src = NULL;
|
||||
@@ -913,7 +1033,6 @@ wuffs_gif__status wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self,
|
||||
uint32_t coro_susp_point =
|
||||
self->private_impl.c_decode_frame[0].coro_susp_point;
|
||||
if (coro_susp_point) {
|
||||
v_c = self->private_impl.c_decode_frame[0].v_c;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
@@ -923,15 +1042,19 @@ wuffs_gif__status wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self,
|
||||
goto exit;
|
||||
}
|
||||
while (true) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
if (self->private_impl.f_peek_block_type) {
|
||||
self->private_impl.f_peek_block_type = false;
|
||||
} else {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_0 = *b_rptr_src++;
|
||||
self->private_impl.f_block_type = t_0;
|
||||
}
|
||||
uint8_t t_0 = *b_rptr_src++;
|
||||
v_c = t_0;
|
||||
}
|
||||
if (v_c == 33) {
|
||||
if (self->private_impl.f_block_type == 33) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
@@ -950,7 +1073,7 @@ wuffs_gif__status wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self,
|
||||
if (status) {
|
||||
goto suspend;
|
||||
}
|
||||
} else if (v_c == 44) {
|
||||
} else if (self->private_impl.f_block_type == 44) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
@@ -971,7 +1094,7 @@ wuffs_gif__status wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self,
|
||||
}
|
||||
status = WUFFS_GIF__STATUS_OK;
|
||||
goto ok;
|
||||
} else if (v_c == 59) {
|
||||
} else if (self->private_impl.f_block_type == 59) {
|
||||
goto label_0_break;
|
||||
} else {
|
||||
status = WUFFS_GIF__ERROR_BAD_GIF_BLOCK;
|
||||
@@ -993,7 +1116,6 @@ wuffs_gif__status wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self,
|
||||
goto suspend;
|
||||
suspend:
|
||||
self->private_impl.c_decode_frame[0].coro_susp_point = coro_susp_point;
|
||||
self->private_impl.c_decode_frame[0].v_c = v_c;
|
||||
|
||||
exit:
|
||||
if (a_src.buf) {
|
||||
@@ -1238,7 +1360,6 @@ static wuffs_gif__status wuffs_gif__decoder__decode_extension(
|
||||
wuffs_gif__status status = WUFFS_GIF__STATUS_OK;
|
||||
|
||||
uint8_t v_label;
|
||||
uint8_t v_block_size;
|
||||
|
||||
uint8_t* b_rptr_src = NULL;
|
||||
uint8_t* b_rstart_src = NULL;
|
||||
@@ -1260,7 +1381,6 @@ static wuffs_gif__status wuffs_gif__decoder__decode_extension(
|
||||
self->private_impl.c_decode_extension[0].coro_susp_point;
|
||||
if (coro_susp_point) {
|
||||
v_label = self->private_impl.c_decode_extension[0].v_label;
|
||||
v_block_size = self->private_impl.c_decode_extension[0].v_block_size;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
@@ -1273,40 +1393,47 @@ static wuffs_gif__status wuffs_gif__decoder__decode_extension(
|
||||
uint8_t t_0 = *b_rptr_src++;
|
||||
v_label = t_0;
|
||||
}
|
||||
if (v_label == 1) {
|
||||
} else if (v_label == 249) {
|
||||
} else if (v_label == 254) {
|
||||
if (v_label == 249) {
|
||||
} else if (v_label == 255) {
|
||||
} else {
|
||||
status = WUFFS_GIF__ERROR_BAD_GIF_EXTENSION_LABEL;
|
||||
goto exit;
|
||||
}
|
||||
while (true) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
}
|
||||
}
|
||||
uint8_t t_1 = *b_rptr_src++;
|
||||
v_block_size = t_1;
|
||||
}
|
||||
if (v_block_size == 0) {
|
||||
goto label_0_break;
|
||||
status = wuffs_gif__decoder__decode_ae(self, a_src);
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
self->private_impl.c_decode_extension[0].scratch =
|
||||
((uint32_t)(v_block_size));
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
|
||||
if (self->private_impl.c_decode_extension[0].scratch >
|
||||
b_rend_src - b_rptr_src) {
|
||||
self->private_impl.c_decode_extension[0].scratch -=
|
||||
b_rend_src - b_rptr_src;
|
||||
b_rptr_src = b_rend_src;
|
||||
goto short_read_src;
|
||||
if (status) {
|
||||
goto suspend;
|
||||
}
|
||||
b_rptr_src += self->private_impl.c_decode_extension[0].scratch;
|
||||
status = WUFFS_GIF__STATUS_OK;
|
||||
goto ok;
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
}
|
||||
}
|
||||
}
|
||||
status = wuffs_gif__decoder__skip_blocks(self, a_src);
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
}
|
||||
if (status) {
|
||||
goto suspend;
|
||||
}
|
||||
label_0_break:;
|
||||
|
||||
goto ok;
|
||||
ok:
|
||||
@@ -1318,7 +1445,319 @@ static wuffs_gif__status wuffs_gif__decoder__decode_extension(
|
||||
suspend:
|
||||
self->private_impl.c_decode_extension[0].coro_susp_point = coro_susp_point;
|
||||
self->private_impl.c_decode_extension[0].v_label = v_label;
|
||||
self->private_impl.c_decode_extension[0].v_block_size = v_block_size;
|
||||
|
||||
exit:
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
}
|
||||
}
|
||||
WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
||||
WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
||||
}
|
||||
|
||||
return status;
|
||||
|
||||
short_read_src:
|
||||
if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) {
|
||||
status = WUFFS_GIF__ERROR_UNEXPECTED_EOF;
|
||||
goto exit;
|
||||
}
|
||||
status = WUFFS_GIF__SUSPENSION_SHORT_READ;
|
||||
goto suspend;
|
||||
}
|
||||
|
||||
static wuffs_gif__status wuffs_gif__decoder__skip_blocks(
|
||||
wuffs_gif__decoder* self,
|
||||
wuffs_base__reader1 a_src) {
|
||||
wuffs_gif__status status = WUFFS_GIF__STATUS_OK;
|
||||
|
||||
uint8_t v_block_size;
|
||||
|
||||
uint8_t* b_rptr_src = NULL;
|
||||
uint8_t* b_rstart_src = NULL;
|
||||
uint8_t* b_rend_src = NULL;
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
b_rstart_src = b_rptr_src;
|
||||
uint64_t len = a_src.buf->wi - a_src.buf->ri;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
||||
len = *lim->ptr_to_len;
|
||||
}
|
||||
}
|
||||
b_rend_src = b_rptr_src + len;
|
||||
}
|
||||
|
||||
uint32_t coro_susp_point =
|
||||
self->private_impl.c_skip_blocks[0].coro_susp_point;
|
||||
if (coro_susp_point) {
|
||||
v_block_size = self->private_impl.c_skip_blocks[0].v_block_size;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
while (true) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_0 = *b_rptr_src++;
|
||||
v_block_size = t_0;
|
||||
}
|
||||
if (v_block_size == 0) {
|
||||
status = WUFFS_GIF__STATUS_OK;
|
||||
goto ok;
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
self->private_impl.c_skip_blocks[0].scratch = ((uint32_t)(v_block_size));
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
if (self->private_impl.c_skip_blocks[0].scratch >
|
||||
b_rend_src - b_rptr_src) {
|
||||
self->private_impl.c_skip_blocks[0].scratch -= b_rend_src - b_rptr_src;
|
||||
b_rptr_src = b_rend_src;
|
||||
goto short_read_src;
|
||||
}
|
||||
b_rptr_src += self->private_impl.c_skip_blocks[0].scratch;
|
||||
}
|
||||
|
||||
goto ok;
|
||||
ok:
|
||||
self->private_impl.c_skip_blocks[0].coro_susp_point = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
goto suspend;
|
||||
suspend:
|
||||
self->private_impl.c_skip_blocks[0].coro_susp_point = coro_susp_point;
|
||||
self->private_impl.c_skip_blocks[0].v_block_size = v_block_size;
|
||||
|
||||
exit:
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
}
|
||||
}
|
||||
WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
||||
WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
||||
}
|
||||
|
||||
return status;
|
||||
|
||||
short_read_src:
|
||||
if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) {
|
||||
status = WUFFS_GIF__ERROR_UNEXPECTED_EOF;
|
||||
goto exit;
|
||||
}
|
||||
status = WUFFS_GIF__SUSPENSION_SHORT_READ;
|
||||
goto suspend;
|
||||
}
|
||||
|
||||
static wuffs_gif__status wuffs_gif__decoder__decode_ae(
|
||||
wuffs_gif__decoder* self,
|
||||
wuffs_base__reader1 a_src) {
|
||||
wuffs_gif__status status = WUFFS_GIF__STATUS_OK;
|
||||
|
||||
uint8_t v_c;
|
||||
uint8_t v_block_size;
|
||||
|
||||
uint8_t* b_rptr_src = NULL;
|
||||
uint8_t* b_rstart_src = NULL;
|
||||
uint8_t* b_rend_src = NULL;
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
b_rstart_src = b_rptr_src;
|
||||
uint64_t len = a_src.buf->wi - a_src.buf->ri;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
||||
len = *lim->ptr_to_len;
|
||||
}
|
||||
}
|
||||
b_rend_src = b_rptr_src + len;
|
||||
}
|
||||
|
||||
uint32_t coro_susp_point = self->private_impl.c_decode_ae[0].coro_susp_point;
|
||||
if (coro_susp_point) {
|
||||
v_c = self->private_impl.c_decode_ae[0].v_c;
|
||||
v_block_size = self->private_impl.c_decode_ae[0].v_block_size;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
while (true) {
|
||||
v_c = 0;
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_0 = *b_rptr_src++;
|
||||
v_block_size = t_0;
|
||||
}
|
||||
if (v_block_size == 0) {
|
||||
status = WUFFS_GIF__STATUS_OK;
|
||||
goto ok;
|
||||
}
|
||||
if (v_block_size != 11) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
self->private_impl.c_decode_ae[0].scratch = ((uint32_t)(v_block_size));
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
if (self->private_impl.c_decode_ae[0].scratch >
|
||||
b_rend_src - b_rptr_src) {
|
||||
self->private_impl.c_decode_ae[0].scratch -= b_rend_src - b_rptr_src;
|
||||
b_rptr_src = b_rend_src;
|
||||
goto short_read_src;
|
||||
}
|
||||
b_rptr_src += self->private_impl.c_decode_ae[0].scratch;
|
||||
goto label_0_break;
|
||||
}
|
||||
v_block_size = 0;
|
||||
while (v_block_size < 11) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_1 = *b_rptr_src++;
|
||||
v_c = t_1;
|
||||
}
|
||||
if (v_c != wuffs_gif__netscape2dot0[v_block_size]) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
|
||||
self->private_impl.c_decode_ae[0].scratch =
|
||||
((uint32_t)((10 - v_block_size)));
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
|
||||
if (self->private_impl.c_decode_ae[0].scratch >
|
||||
b_rend_src - b_rptr_src) {
|
||||
self->private_impl.c_decode_ae[0].scratch -=
|
||||
b_rend_src - b_rptr_src;
|
||||
b_rptr_src = b_rend_src;
|
||||
goto short_read_src;
|
||||
}
|
||||
b_rptr_src += self->private_impl.c_decode_ae[0].scratch;
|
||||
goto label_0_break;
|
||||
}
|
||||
v_block_size += 1;
|
||||
}
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_2 = *b_rptr_src++;
|
||||
v_block_size = t_2;
|
||||
}
|
||||
if (v_block_size != 3) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8);
|
||||
self->private_impl.c_decode_ae[0].scratch = ((uint32_t)(v_block_size));
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9);
|
||||
if (self->private_impl.c_decode_ae[0].scratch >
|
||||
b_rend_src - b_rptr_src) {
|
||||
self->private_impl.c_decode_ae[0].scratch -= b_rend_src - b_rptr_src;
|
||||
b_rptr_src = b_rend_src;
|
||||
goto short_read_src;
|
||||
}
|
||||
b_rptr_src += self->private_impl.c_decode_ae[0].scratch;
|
||||
goto label_0_break;
|
||||
}
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_3 = *b_rptr_src++;
|
||||
v_c = t_3;
|
||||
}
|
||||
if (v_c != 1) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11);
|
||||
self->private_impl.c_decode_ae[0].scratch = 2;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(12);
|
||||
if (self->private_impl.c_decode_ae[0].scratch >
|
||||
b_rend_src - b_rptr_src) {
|
||||
self->private_impl.c_decode_ae[0].scratch -= b_rend_src - b_rptr_src;
|
||||
b_rptr_src = b_rend_src;
|
||||
goto short_read_src;
|
||||
}
|
||||
b_rptr_src += self->private_impl.c_decode_ae[0].scratch;
|
||||
goto label_0_break;
|
||||
}
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(13);
|
||||
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_ae[0].scratch = 0;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(14);
|
||||
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_ae[0].scratch >> 56;
|
||||
self->private_impl.c_decode_ae[0].scratch <<= 8;
|
||||
self->private_impl.c_decode_ae[0].scratch >>= 8;
|
||||
self->private_impl.c_decode_ae[0].scratch |=
|
||||
((uint64_t)(*b_rptr_src++)) << t_4;
|
||||
if (t_4 == 8) {
|
||||
t_5 = self->private_impl.c_decode_ae[0].scratch;
|
||||
break;
|
||||
}
|
||||
t_4 += 8;
|
||||
self->private_impl.c_decode_ae[0].scratch |= ((uint64_t)(t_4))
|
||||
<< 56;
|
||||
}
|
||||
}
|
||||
self->private_impl.f_num_loops = ((uint32_t)(t_5));
|
||||
}
|
||||
self->private_impl.f_seen_num_loops = true;
|
||||
if ((0 < self->private_impl.f_num_loops) &&
|
||||
(self->private_impl.f_num_loops <= 65535)) {
|
||||
self->private_impl.f_num_loops += 1;
|
||||
}
|
||||
goto label_0_break;
|
||||
}
|
||||
label_0_break:;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(15);
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
wuffs_base__limit1* lim;
|
||||
for (lim = &a_src.private_impl.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
}
|
||||
}
|
||||
}
|
||||
status = wuffs_gif__decoder__skip_blocks(self, a_src);
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
}
|
||||
if (status) {
|
||||
goto suspend;
|
||||
}
|
||||
|
||||
goto ok;
|
||||
ok:
|
||||
self->private_impl.c_decode_ae[0].coro_susp_point = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
goto suspend;
|
||||
suspend:
|
||||
self->private_impl.c_decode_ae[0].coro_susp_point = coro_susp_point;
|
||||
self->private_impl.c_decode_ae[0].v_c = v_c;
|
||||
self->private_impl.c_decode_ae[0].v_block_size = v_block_size;
|
||||
|
||||
exit:
|
||||
if (a_src.buf) {
|
||||
|
||||
+14
-2
@@ -280,6 +280,10 @@ typedef struct {
|
||||
uint32_t f_height;
|
||||
uint8_t f_call_sequence;
|
||||
uint8_t f_background_color_index;
|
||||
uint8_t f_block_type;
|
||||
bool f_peek_block_type;
|
||||
bool f_seen_num_loops;
|
||||
uint32_t f_num_loops;
|
||||
uint8_t f_gct[768];
|
||||
wuffs_gif__lzw_decoder f_lzw;
|
||||
|
||||
@@ -288,7 +292,6 @@ typedef struct {
|
||||
} c_decode_config[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_c;
|
||||
} c_decode_frame[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
@@ -304,9 +307,18 @@ typedef struct {
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_label;
|
||||
} c_decode_extension[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_block_size;
|
||||
uint64_t scratch;
|
||||
} c_decode_extension[1];
|
||||
} c_skip_blocks[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_c;
|
||||
uint8_t v_block_size;
|
||||
uint64_t scratch;
|
||||
} c_decode_ae[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_c[9];
|
||||
|
||||
+105
-11
@@ -34,6 +34,14 @@ pub struct decoder?(
|
||||
|
||||
background_color_index u8,
|
||||
|
||||
block_type u8,
|
||||
peek_block_type bool,
|
||||
|
||||
// Absent a NETSCAPE2.0 extension, the implicit number of animation loops
|
||||
// is 1.
|
||||
seen_num_loops bool,
|
||||
num_loops u32 = 1,
|
||||
|
||||
// gct is the Global Color Table: 256 (R, G, B) entries.
|
||||
//
|
||||
// TODO: 4 byte per pixel RGBA or BGRA instead of 3 bpp RGB?
|
||||
@@ -49,6 +57,16 @@ pub func decoder.decode_config?(dst ptr image_config, src reader1)() {
|
||||
}
|
||||
this.decode_header?(src:in.src)
|
||||
this.decode_lsd?(src:in.src)
|
||||
|
||||
while true {
|
||||
this.peek_block_type = true
|
||||
this.block_type = in.src.read_u8?()
|
||||
if this.seen_num_loops or (this.block_type != 0x21) { // The spec calls 0x21 the "Extension Introducer".
|
||||
break
|
||||
}
|
||||
this.decode_extension?(src:in.src)
|
||||
}
|
||||
|
||||
in.dst.initialize!(width:this.width, height:this.height, color_model:0)
|
||||
this.call_sequence = 1
|
||||
}
|
||||
@@ -57,11 +75,17 @@ pub func decoder.decode_frame?(dst writer1, src reader1)() {
|
||||
if this.call_sequence < 1 {
|
||||
return error "invalid call sequence"
|
||||
}
|
||||
|
||||
while true {
|
||||
var c u8 = in.src.read_u8?()
|
||||
if c == 0x21 { // The spec calls 0x21 the "Extension Introducer".
|
||||
if this.peek_block_type {
|
||||
this.peek_block_type = false
|
||||
} else {
|
||||
this.block_type = in.src.read_u8?()
|
||||
}
|
||||
|
||||
if this.block_type == 0x21 { // The spec calls 0x21 the "Extension Introducer".
|
||||
this.decode_extension?(src:in.src)
|
||||
} else if c == 0x2C { // The spec calls 0x2C the "Image Separator".
|
||||
} else if this.block_type == 0x2C { // The spec calls 0x2C the "Image Separator".
|
||||
// TODO: animated GIFs can have multiple Image Descriptors. What's
|
||||
// the API? Should the 0x3B Trailer byte result in a "no more
|
||||
// frames" suspension instead of ok? Do we need additional
|
||||
@@ -70,12 +94,13 @@ pub func decoder.decode_frame?(dst writer1, src reader1)() {
|
||||
// TODO: reset this.lzw if it's not the first ID.
|
||||
this.decode_id?(dst:in.dst, src:in.src)
|
||||
return
|
||||
} else if c == 0x3B { // The spec calls 0x3B the "Trailer".
|
||||
} else if this.block_type == 0x3B { // The spec calls 0x3B the "Trailer".
|
||||
break
|
||||
} else {
|
||||
return error "bad GIF block"
|
||||
}
|
||||
}
|
||||
|
||||
while true {
|
||||
yield suspension "end of data"
|
||||
}
|
||||
@@ -135,24 +160,93 @@ pri func decoder.decode_lsd?(src reader1)() {
|
||||
// - section 26 "Application Extension" on page 21.
|
||||
pri func decoder.decode_extension?(src reader1)() {
|
||||
var label u8 = in.src.read_u8?()
|
||||
if label == 0x01 { // The spec calls 0x01 the "Plain Text Label".
|
||||
} else if label == 0xF9 { // The spec calls 0xF9 the "Graphic Control Label".
|
||||
} else if label == 0xFE { // The spec calls 0xFE the "Comment Label".
|
||||
if label == 0xF9 { // The spec calls 0xF9 the "Graphic Control Label".
|
||||
// TODO.
|
||||
} else if label == 0xFF { // The spec calls 0xFF the "Application Extension Label".
|
||||
} else {
|
||||
return error "bad GIF extension label"
|
||||
this.decode_ae?(src:in.src)
|
||||
return
|
||||
}
|
||||
// We skip over all other extensions, including 0x01 "Plain Text Label" and
|
||||
// 0xFE "Comment Label".
|
||||
this.skip_blocks?(src:in.src)
|
||||
}
|
||||
|
||||
// Skip the data blocks.
|
||||
pri func decoder.skip_blocks?(src reader1)() {
|
||||
while true {
|
||||
var block_size u8 = in.src.read_u8?()
|
||||
if block_size == 0 {
|
||||
break
|
||||
return
|
||||
}
|
||||
in.src.skip32?(n:block_size as u32)
|
||||
}
|
||||
}
|
||||
|
||||
// netscape2dot0 is "NETSCAPE2.0" as bytes.
|
||||
pri const netscape2dot0[11] u8 = $(
|
||||
0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30,
|
||||
)
|
||||
|
||||
// decode_ae reads an Application Extension.
|
||||
pri func decoder.decode_ae?(src reader1)() {
|
||||
// This "while true" always executes exactly once, as it ends with a
|
||||
// "break", but using "break"s throughout simplifies the control flow.
|
||||
while:loop true {
|
||||
var c u8
|
||||
var block_size u8 = in.src.read_u8?()
|
||||
if block_size == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// Look only for an 11 byte "NETSCAPE2.0" extension, as per
|
||||
// http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
|
||||
if block_size != 11 {
|
||||
in.src.skip32?(n:block_size as u32)
|
||||
break
|
||||
}
|
||||
block_size = 0 // Re-purpose the block_size variable as a counter.
|
||||
while block_size < 11 {
|
||||
c = in.src.read_u8?()
|
||||
if c != netscape2dot0[block_size] {
|
||||
in.src.skip32?(n:(10 - block_size) as u32)
|
||||
break:loop
|
||||
}
|
||||
block_size += 1
|
||||
}
|
||||
|
||||
// Those 11 bytes should be followed by 0x03, 0x01 and then the loop
|
||||
// count.
|
||||
block_size = in.src.read_u8?()
|
||||
if block_size != 3 {
|
||||
in.src.skip32?(n:block_size as u32)
|
||||
break
|
||||
}
|
||||
c = in.src.read_u8?()
|
||||
if c != 0x01 {
|
||||
in.src.skip32?(n:2)
|
||||
break
|
||||
}
|
||||
this.num_loops = in.src.read_u16le?() as u32
|
||||
this.seen_num_loops = true
|
||||
|
||||
// A loop count of N, in the wire format, actually means "repeat N
|
||||
// times after the first play", if N is positive. A zero N means to
|
||||
// loop forever. Playing the frames exactly once is denoted by the
|
||||
// *absence* of this NETSCAPE2.0 application extension.
|
||||
//
|
||||
// For example, if there are four frames: A, B, C, D, and N is 2, then
|
||||
// each frame is actually played N+1 or 3 times: ABCDABCDABCD.
|
||||
//
|
||||
// Thus, we increment N if it is positive. The comparison against
|
||||
// 0xFFFF will never fail, but is necessary for the overflow checker.
|
||||
if (0 < this.num_loops) and (this.num_loops <= 0xFFFF) {
|
||||
this.num_loops += 1
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
this.skip_blocks?(src:in.src)
|
||||
}
|
||||
|
||||
// decode_id reads an Image Descriptor. The Image Separator byte has already
|
||||
// been read.
|
||||
//
|
||||
|
||||
+16
-1
@@ -403,6 +403,8 @@ bool do_test_wuffs_gif_decode(const char* filename,
|
||||
wuffs_gif__status__string(status));
|
||||
return false;
|
||||
}
|
||||
|
||||
// bricks-dither.gif is a 160 × 120 static (not animated) GIF.
|
||||
if (wuffs_base__image_config__width(&ic) != 160) {
|
||||
FAIL("width: got %" PRIu32 ", want 160",
|
||||
wuffs_base__image_config__width(&ic));
|
||||
@@ -413,6 +415,11 @@ bool do_test_wuffs_gif_decode(const char* filename,
|
||||
wuffs_base__image_config__height(&ic));
|
||||
return false;
|
||||
}
|
||||
// TODO: provide a public API for getting num_loops.
|
||||
if (dec.private_impl.f_num_loops != 1) {
|
||||
FAIL("num_loops: got %" PRIu32 ", want 1", dec.private_impl.f_num_loops);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int num_iters = 0;
|
||||
@@ -571,7 +578,15 @@ void test_wuffs_gif_decode_animated() {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: check the loop count.
|
||||
// animated-red-blue.gif's num_loops should be 3. The value explicitly in the
|
||||
// wire format is 0x0002, but that value means "repeat 2 times after the
|
||||
// first play", so the total number of loops is 3.
|
||||
//
|
||||
// TODO: provide a public API for getting num_loops.
|
||||
if (dec.private_impl.f_num_loops != 3) {
|
||||
FAIL("num_loops: got %" PRIu32 ", want 3", dec.private_impl.f_num_loops);
|
||||
return;
|
||||
}
|
||||
|
||||
// animated-red-blue.gif should have 4 frames.
|
||||
int i;
|
||||
|
||||
Reference in new issue
Block a user