mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 00:47:28 +01:00
Decode a GIF's Global Color Table.
This commit is contained in:
1 parent
dc71a93dcf
commit
d16a25806f
6 files changed
+114
-23
No files matched your search
+16
-2
@@ -22,6 +22,7 @@ const char* test_filename = "std/gif.c";
|
||||
uint8_t global_got_buffer[BUFFER_SIZE];
|
||||
uint8_t global_want_buffer[BUFFER_SIZE];
|
||||
uint8_t global_src_buffer[BUFFER_SIZE];
|
||||
uint8_t global_palette_buffer[3 * 256];
|
||||
|
||||
// ---------------- Basic Tests
|
||||
|
||||
@@ -229,7 +230,7 @@ void test_lzw_decode() {
|
||||
goto cleanup1;
|
||||
}
|
||||
|
||||
if (!buf1s_equal(&got, &want)) {
|
||||
if (!buf1s_equal("", &got, &want)) {
|
||||
goto cleanup1;
|
||||
}
|
||||
// As a sanity check, the first decoded byte should be 0xDC.
|
||||
@@ -247,6 +248,7 @@ cleanup0:;
|
||||
// ---------------- GIF Tests
|
||||
|
||||
void test_gif_decode_input_is_a_xxx(const char* filename,
|
||||
const char* palette_filename,
|
||||
puffs_gif_status want) {
|
||||
puffs_base_buf1 dst = {.ptr = global_got_buffer, .len = BUFFER_SIZE};
|
||||
puffs_base_buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE};
|
||||
@@ -277,6 +279,17 @@ void test_gif_decode_input_is_a_xxx(const char* filename,
|
||||
goto cleanup1;
|
||||
}
|
||||
|
||||
// TODO: provide a public API for getting the palette.
|
||||
puffs_base_buf1 pal_got = {.ptr = dec.private_impl.f_gct, .len = 3 * 256};
|
||||
puffs_base_buf1 pal_want = {.ptr = global_palette_buffer, .len = 3 * 256};
|
||||
pal_got.wi = 3 * 256;
|
||||
if (!read_file(&pal_want, palette_filename)) {
|
||||
goto cleanup1;
|
||||
}
|
||||
if (!buf1s_equal("palette ", &pal_got, &pal_want)) {
|
||||
goto cleanup1;
|
||||
}
|
||||
|
||||
cleanup1:
|
||||
puffs_gif_decoder_destructor(&dec);
|
||||
cleanup0:;
|
||||
@@ -285,12 +298,13 @@ cleanup0:;
|
||||
void test_gif_decode_input_is_a_gif() {
|
||||
test_funcname = __func__;
|
||||
test_gif_decode_input_is_a_xxx("../../testdata/bricks-dither.gif",
|
||||
"../../testdata/bricks-dither.palette",
|
||||
puffs_gif_status_ok);
|
||||
}
|
||||
|
||||
void test_gif_decode_input_is_a_png() {
|
||||
test_funcname = __func__;
|
||||
test_gif_decode_input_is_a_xxx("../../testdata/bricks-dither.png",
|
||||
test_gif_decode_input_is_a_xxx("../../testdata/bricks-dither.png", "",
|
||||
puffs_gif_error_bad_gif_header);
|
||||
}
|
||||
|
||||
|
||||
+28
-12
@@ -63,26 +63,39 @@ bool read_file(puffs_base_buf1* dst, const char* path) {
|
||||
FAIL("read_file(\"%s\"): %s (errno=%d)", path, strerror(errno), errno);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t dummy[1];
|
||||
uint8_t* ptr = dst->ptr + dst->wi;
|
||||
size_t len = dst->len - dst->wi;
|
||||
while (true) {
|
||||
if (!len) {
|
||||
// We have read all that dst can hold. Check that we have read the full
|
||||
// file by trying to read one more byte, which should fail with EOF.
|
||||
ptr = dummy;
|
||||
len = 1;
|
||||
}
|
||||
size_t n = fread(ptr, 1, len, f);
|
||||
ptr += n;
|
||||
len -= n;
|
||||
dst->wi += n;
|
||||
if (ptr != dummy) {
|
||||
ptr += n;
|
||||
len -= n;
|
||||
dst->wi += n;
|
||||
} else if (n) {
|
||||
FAIL("read_file(\"%s\"): EOF not reached", path);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
if (feof(f)) {
|
||||
break;
|
||||
}
|
||||
int err = ferror(f);
|
||||
if (!err) {
|
||||
continue;
|
||||
}
|
||||
if (err == EINTR) {
|
||||
clearerr(f);
|
||||
continue;
|
||||
}
|
||||
if (err) {
|
||||
FAIL("read_file(\"%s\"): %s (errno=%d)", path, strerror(err), err);
|
||||
} else {
|
||||
FAIL("read_file(\"%s\"): EOF not reached", path);
|
||||
}
|
||||
FAIL("read_file(\"%s\"): %s (errno=%d)", path, strerror(err), err);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
@@ -139,9 +152,11 @@ char* hex_dump(char* msg, puffs_base_buf1* buf, size_t i) {
|
||||
return msg;
|
||||
}
|
||||
|
||||
bool buf1s_equal(puffs_base_buf1* got, puffs_base_buf1* want) {
|
||||
bool buf1s_equal(const char* prefix,
|
||||
puffs_base_buf1* got,
|
||||
puffs_base_buf1* want) {
|
||||
if (!got || !want) {
|
||||
FAIL("bufs1_equal: NULL argument");
|
||||
FAIL("%sbufs1_equal: NULL argument", prefix);
|
||||
return false;
|
||||
}
|
||||
char* msg = fail_msg;
|
||||
@@ -152,9 +167,10 @@ bool buf1s_equal(puffs_base_buf1* got, puffs_base_buf1* want) {
|
||||
}
|
||||
}
|
||||
if (got->wi != want->wi) {
|
||||
INCR_FAIL(msg, "bufs1_equal: wi: got %zu, want %zu.\n", got->wi, want->wi);
|
||||
INCR_FAIL(msg, "%sbufs1_equal: wi: got %zu, want %zu.\n", prefix, got->wi,
|
||||
want->wi);
|
||||
} else if (i < got->wi) {
|
||||
INCR_FAIL(msg, "bufs1_equal:\n");
|
||||
INCR_FAIL(msg, "%sbufs1_equal:\n", prefix);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user