mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Decode a GIF local color table
This commit is contained in:
1 parent
584fc631a8
commit
0d33d3ef3a
4 files changed
+77
-33
No files matched your search
+49
-20
@@ -216,11 +216,9 @@ typedef int32_t wuffs_gif__status;
|
||||
#define WUFFS_GIF__ERROR_BAD_GIF_HEADER -1105848318 // 0xBE161802
|
||||
#define WUFFS_GIF__ERROR_BAD_LZW_LITERAL_WIDTH -1105848317 // 0xBE161803
|
||||
#define WUFFS_GIF__ERROR_INTERNAL_ERROR_INCONSISTENT_LIMITED_READ \
|
||||
-1105848316 // 0xBE161804
|
||||
#define WUFFS_GIF__ERROR_TODO_UNSUPPORTED_LOCAL_COLOR_TABLE \
|
||||
-1105848315 // 0xBE161805
|
||||
#define WUFFS_GIF__ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848314 // 0xBE161806
|
||||
#define WUFFS_GIF__ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848313 // 0xBE161807
|
||||
-1105848316 // 0xBE161804
|
||||
#define WUFFS_GIF__ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848315 // 0xBE161805
|
||||
#define WUFFS_GIF__ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848314 // 0xBE161806
|
||||
|
||||
bool wuffs_gif__status__is_error(wuffs_gif__status s);
|
||||
|
||||
@@ -285,6 +283,7 @@ typedef struct {
|
||||
bool f_seen_num_loops;
|
||||
uint32_t f_num_loops;
|
||||
uint8_t f_gct[768];
|
||||
uint8_t f_lct[768];
|
||||
wuffs_gif__lzw_decoder f_lzw;
|
||||
|
||||
struct {
|
||||
@@ -324,6 +323,7 @@ typedef struct {
|
||||
uint8_t v_c[9];
|
||||
uint32_t v_i;
|
||||
bool v_interlace;
|
||||
uint32_t v_lct_size;
|
||||
uint8_t v_lw;
|
||||
uint64_t v_block_size;
|
||||
wuffs_gif__status v_z;
|
||||
@@ -728,13 +728,12 @@ bool wuffs_gif__status__is_error(wuffs_gif__status s) {
|
||||
return s < 0;
|
||||
}
|
||||
|
||||
const char* wuffs_gif__status__strings[8] = {
|
||||
const char* wuffs_gif__status__strings[7] = {
|
||||
"gif: bad GIF block",
|
||||
"gif: bad GIF extension label",
|
||||
"gif: bad GIF header",
|
||||
"gif: bad LZW literal width",
|
||||
"gif: internal error: inconsistent limited read",
|
||||
"gif: TODO: unsupported Local Color Table",
|
||||
"gif: LZW code is out of range",
|
||||
"gif: LZW prefix chain is cyclical",
|
||||
};
|
||||
@@ -749,7 +748,7 @@ const char* wuffs_gif__status__string(wuffs_gif__status s) {
|
||||
break;
|
||||
case wuffs_gif__packageid:
|
||||
a = wuffs_gif__status__strings;
|
||||
n = 8;
|
||||
n = 7;
|
||||
break;
|
||||
}
|
||||
uint32_t i = s & 0xFF;
|
||||
@@ -1793,6 +1792,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
|
||||
uint8_t v_c[9];
|
||||
uint32_t v_i;
|
||||
bool v_interlace;
|
||||
uint32_t v_lct_size;
|
||||
uint8_t v_lw;
|
||||
uint64_t v_block_size;
|
||||
wuffs_base__reader1 v_r;
|
||||
@@ -1819,6 +1819,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
|
||||
memcpy(v_c, self->private_impl.c_decode_id[0].v_c, sizeof(v_c));
|
||||
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_lw = self->private_impl.c_decode_id[0].v_lw;
|
||||
v_block_size = self->private_impl.c_decode_id[0].v_block_size;
|
||||
v_r = ((wuffs_base__reader1){});
|
||||
@@ -1844,16 +1845,43 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
|
||||
if (v_interlace) {
|
||||
}
|
||||
if ((v_c[8] & 128) != 0) {
|
||||
status = WUFFS_GIF__ERROR_TODO_UNSUPPORTED_LOCAL_COLOR_TABLE;
|
||||
goto exit;
|
||||
v_lct_size = (((uint32_t)(1)) << (1 + (v_c[8] & 7)));
|
||||
v_i = 0;
|
||||
while (v_i < v_lct_size) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_1 = *b_rptr_src++;
|
||||
self->private_impl.f_lct[(3 * v_i) + 0] = t_1;
|
||||
}
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_2 = *b_rptr_src++;
|
||||
self->private_impl.f_lct[(3 * v_i) + 1] = t_2;
|
||||
}
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_3 = *b_rptr_src++;
|
||||
self->private_impl.f_lct[(3 * v_i) + 2] = t_3;
|
||||
}
|
||||
v_i += 1;
|
||||
}
|
||||
}
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_1 = *b_rptr_src++;
|
||||
v_lw = t_1;
|
||||
uint8_t t_4 = *b_rptr_src++;
|
||||
v_lw = t_4;
|
||||
}
|
||||
if ((v_lw < 2) || (8 < v_lw)) {
|
||||
status = WUFFS_GIF__ERROR_BAD_LZW_LITERAL_WIDTH;
|
||||
@@ -1863,12 +1891,12 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
|
||||
((uint32_t)(v_lw)));
|
||||
while (true) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
|
||||
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_2 = *b_rptr_src++;
|
||||
v_block_size = ((uint64_t)(t_2));
|
||||
uint8_t t_5 = *b_rptr_src++;
|
||||
v_block_size = ((uint64_t)(t_5));
|
||||
}
|
||||
if (v_block_size == 0) {
|
||||
goto label_0_break;
|
||||
@@ -1877,7 +1905,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
|
||||
v_r = a_src;
|
||||
wuffs_base__reader1__mark(&v_r, b_rptr_src);
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
@@ -1889,13 +1917,13 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
|
||||
}
|
||||
}
|
||||
uint64_t l_rlimit0 = v_block_size;
|
||||
wuffs_gif__status t_3 = wuffs_gif__lzw_decoder__decode(
|
||||
wuffs_gif__status t_6 = wuffs_gif__lzw_decoder__decode(
|
||||
&self->private_impl.f_lzw, a_dst,
|
||||
wuffs_base__reader1__limit(&v_r, &l_rlimit0));
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
}
|
||||
v_z = t_3;
|
||||
v_z = t_6;
|
||||
}
|
||||
if (v_z == 0) {
|
||||
goto label_1_break;
|
||||
@@ -1924,7 +1952,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
|
||||
goto label_1_break;
|
||||
}
|
||||
status = v_z;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(5);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(8);
|
||||
}
|
||||
label_1_break:;
|
||||
}
|
||||
@@ -1942,6 +1970,7 @@ suspend:
|
||||
memcpy(self->private_impl.c_decode_id[0].v_c, v_c, sizeof(v_c));
|
||||
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_lw = v_lw;
|
||||
self->private_impl.c_decode_id[0].v_block_size = v_block_size;
|
||||
self->private_impl.c_decode_id[0].v_z = v_z;
|
||||
|
||||
+5
-5
@@ -216,11 +216,9 @@ typedef int32_t wuffs_gif__status;
|
||||
#define WUFFS_GIF__ERROR_BAD_GIF_HEADER -1105848318 // 0xBE161802
|
||||
#define WUFFS_GIF__ERROR_BAD_LZW_LITERAL_WIDTH -1105848317 // 0xBE161803
|
||||
#define WUFFS_GIF__ERROR_INTERNAL_ERROR_INCONSISTENT_LIMITED_READ \
|
||||
-1105848316 // 0xBE161804
|
||||
#define WUFFS_GIF__ERROR_TODO_UNSUPPORTED_LOCAL_COLOR_TABLE \
|
||||
-1105848315 // 0xBE161805
|
||||
#define WUFFS_GIF__ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848314 // 0xBE161806
|
||||
#define WUFFS_GIF__ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848313 // 0xBE161807
|
||||
-1105848316 // 0xBE161804
|
||||
#define WUFFS_GIF__ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848315 // 0xBE161805
|
||||
#define WUFFS_GIF__ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848314 // 0xBE161806
|
||||
|
||||
bool wuffs_gif__status__is_error(wuffs_gif__status s);
|
||||
|
||||
@@ -285,6 +283,7 @@ typedef struct {
|
||||
bool f_seen_num_loops;
|
||||
uint32_t f_num_loops;
|
||||
uint8_t f_gct[768];
|
||||
uint8_t f_lct[768];
|
||||
wuffs_gif__lzw_decoder f_lzw;
|
||||
|
||||
struct {
|
||||
@@ -324,6 +323,7 @@ typedef struct {
|
||||
uint8_t v_c[9];
|
||||
uint32_t v_i;
|
||||
bool v_interlace;
|
||||
uint32_t v_lct_size;
|
||||
uint8_t v_lw;
|
||||
uint64_t v_block_size;
|
||||
wuffs_gif__status v_z;
|
||||
|
||||
@@ -21,8 +21,6 @@ pub error "bad LZW literal width"
|
||||
|
||||
pri error "internal error: inconsistent limited read"
|
||||
|
||||
pub error "TODO: unsupported Local Color Table"
|
||||
|
||||
pub struct decoder?(
|
||||
width u32[..65535],
|
||||
height u32[..65535],
|
||||
@@ -42,10 +40,11 @@ pub struct decoder?(
|
||||
seen_num_loops bool,
|
||||
num_loops u32 = 1,
|
||||
|
||||
// gct is the Global Color Table: 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?
|
||||
gct[3 * 256] u8,
|
||||
lct[3 * 256] u8,
|
||||
|
||||
lzw lzw_decoder,
|
||||
)
|
||||
@@ -90,8 +89,6 @@ pub func decoder.decode_frame?(dst writer1, src reader1)() {
|
||||
// the API? Should the 0x3B Trailer byte result in a "no more
|
||||
// frames" suspension instead of ok? Do we need additional
|
||||
// destination arguments for timing and palette?
|
||||
//
|
||||
// TODO: reset this.lzw if it's not the first ID.
|
||||
this.decode_id?(dst:in.dst, src:in.src)
|
||||
return
|
||||
} else if this.block_type == 0x3B { // The spec calls 0x3B the "Trailer".
|
||||
@@ -266,9 +263,17 @@ pri func decoder.decode_id?(dst writer1, src reader1)() {
|
||||
if interlace { // Avoid "unused variable" C compiler warning.
|
||||
}
|
||||
|
||||
// TODO: read the optional Local Color Table.
|
||||
// Read the optional Local Color Table.
|
||||
if (c[8] & 0x80) != 0 {
|
||||
return error "TODO: unsupported Local Color Table"
|
||||
var lct_size u32[..256] = (1 as u32) << (1 + (c[8] & 0x07))
|
||||
i = 0
|
||||
while i < 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) + 1] = in.src.read_u8?()
|
||||
this.lct[(3 * i) + 2] = in.src.read_u8?()
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
|
||||
var lw u8 = in.src.read_u8?()
|
||||
|
||||
+11
-1
@@ -590,7 +590,7 @@ void test_wuffs_gif_decode_animated() {
|
||||
|
||||
// animated-red-blue.gif should have 4 frames.
|
||||
int i;
|
||||
for (i = 0; i < 0; i++) { // TODO: increase the 0 in "i < 0" to 4.
|
||||
for (i = 0; i < 4; i++) {
|
||||
got.wi = 0;
|
||||
status = wuffs_gif__decoder__decode_frame(&dec, got_writer, src_reader);
|
||||
if (status != WUFFS_GIF__STATUS_OK) {
|
||||
@@ -599,6 +599,16 @@ void test_wuffs_gif_decode_animated() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// There should be no more frames.
|
||||
got.wi = 0;
|
||||
status = wuffs_gif__decoder__decode_frame(&dec, got_writer, src_reader);
|
||||
if (status != WUFFS_GIF__SUSPENSION_END_OF_DATA) {
|
||||
FAIL("decode_frame: got %" PRIi32 " (%s), want %" PRIi32 " (%s)", status,
|
||||
wuffs_gif__status__string(status), WUFFS_GIF__SUSPENSION_END_OF_DATA,
|
||||
wuffs_gif__status__string(WUFFS_GIF__SUSPENSION_END_OF_DATA));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void test_wuffs_gif_decode_input_is_a_gif() {
|
||||
|
||||
Reference in new issue
Block a user