mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +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
+34
-2
@@ -112,6 +112,8 @@ typedef struct {
|
||||
uint32_t magic;
|
||||
uint32_t f_width;
|
||||
uint32_t f_height;
|
||||
uint8_t f_background_color_index;
|
||||
uint8_t f_gct[768];
|
||||
puffs_gif_lzw_decoder f_lzw;
|
||||
} private_impl;
|
||||
} puffs_gif_decoder;
|
||||
@@ -317,7 +319,7 @@ puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self,
|
||||
puffs_gif_status status = self->private_impl.status;
|
||||
|
||||
uint8_t v_c[6];
|
||||
uint8_t v_i;
|
||||
uint32_t v_i;
|
||||
|
||||
for (size_t i = 0; i < 6; i++) {
|
||||
v_c[i] = 0;
|
||||
@@ -346,7 +348,8 @@ puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self,
|
||||
puffs_gif_status status = self->private_impl.status;
|
||||
|
||||
uint8_t v_c[7];
|
||||
uint8_t v_i;
|
||||
uint32_t v_i;
|
||||
uint32_t v_gct_size;
|
||||
|
||||
for (size_t i = 0; i < 7; i++) {
|
||||
v_c[i] = 0;
|
||||
@@ -366,6 +369,35 @@ puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self,
|
||||
(((uint32_t)(v_c[0])) | (((uint32_t)(v_c[1])) << 8));
|
||||
self->private_impl.f_height =
|
||||
(((uint32_t)(v_c[2])) | (((uint32_t)(v_c[3])) << 8));
|
||||
self->private_impl.f_background_color_index = v_c[5];
|
||||
if ((v_c[4] & 128) != 0) {
|
||||
v_gct_size = (((uint32_t)(1)) << (1 + (v_c[4] & 7)));
|
||||
v_i = 0;
|
||||
while (v_i < v_gct_size) {
|
||||
if (a_src->ri >= a_src->wi) {
|
||||
status = a_src->closed ? puffs_gif_error_unexpected_eof
|
||||
: puffs_gif_status_short_read;
|
||||
return status;
|
||||
}
|
||||
uint8_t t_1 = a_src->ptr[a_src->ri++];
|
||||
self->private_impl.f_gct[(3 * v_i) + 0] = t_1;
|
||||
if (a_src->ri >= a_src->wi) {
|
||||
status = a_src->closed ? puffs_gif_error_unexpected_eof
|
||||
: puffs_gif_status_short_read;
|
||||
return status;
|
||||
}
|
||||
uint8_t t_2 = a_src->ptr[a_src->ri++];
|
||||
self->private_impl.f_gct[(3 * v_i) + 1] = t_2;
|
||||
if (a_src->ri >= a_src->wi) {
|
||||
status = a_src->closed ? puffs_gif_error_unexpected_eof
|
||||
: puffs_gif_status_short_read;
|
||||
return status;
|
||||
}
|
||||
uint8_t t_3 = a_src->ptr[a_src->ri++];
|
||||
self->private_impl.f_gct[(3 * v_i) + 2] = t_3;
|
||||
v_i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -112,6 +112,8 @@ typedef struct {
|
||||
uint32_t magic;
|
||||
uint32_t f_width;
|
||||
uint32_t f_height;
|
||||
uint8_t f_background_color_index;
|
||||
uint8_t f_gct[768];
|
||||
puffs_gif_lzw_decoder f_lzw;
|
||||
} private_impl;
|
||||
} puffs_gif_decoder;
|
||||
|
||||
+13
-5
@@ -738,14 +738,18 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
|
||||
return nMin, nMax, nil
|
||||
|
||||
case t.KeyXBinaryStar:
|
||||
// TODO.
|
||||
if cv := lhs.ConstValue(); cv != nil && cv.Cmp(zero) == 0 {
|
||||
return zero, zero, nil
|
||||
// TODO: handle multiplication by negative numbers. Note that this
|
||||
// might reverse the inequality: if 0 < a < b but c < 0 then a*c > b*c.
|
||||
if lMin.Cmp(zero) < 0 {
|
||||
return nil, nil, fmt.Errorf("check: multiply op argument %q is possibly negative", lhs.String(q.tm))
|
||||
}
|
||||
if cv := rhs.ConstValue(); cv != nil && cv.Cmp(zero) == 0 {
|
||||
return zero, zero, nil
|
||||
if rMin.Cmp(zero) < 0 {
|
||||
return nil, nil, fmt.Errorf("check: multiply op argument %q is possibly negative", rhs.String(q.tm))
|
||||
}
|
||||
return big.NewInt(0).Mul(lMin, rMin), big.NewInt(0).Mul(lMax, rMax), nil
|
||||
|
||||
case t.KeyXBinarySlash:
|
||||
// TODO.
|
||||
|
||||
case t.KeyXBinaryShiftL:
|
||||
if lMin.Cmp(zero) < 0 {
|
||||
@@ -801,13 +805,17 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
|
||||
return zero, bitMask(z.BitLen()), nil
|
||||
|
||||
case t.KeyXBinaryAmpHat:
|
||||
// TODO.
|
||||
|
||||
case t.KeyXBinaryHat:
|
||||
// TODO.
|
||||
|
||||
case t.KeyXBinaryNotEq, t.KeyXBinaryLessThan, t.KeyXBinaryLessEq, t.KeyXBinaryEqEq,
|
||||
t.KeyXBinaryGreaterEq, t.KeyXBinaryGreaterThan, t.KeyXBinaryAnd, t.KeyXBinaryOr:
|
||||
return zero, one, nil
|
||||
|
||||
case t.KeyXBinaryAs:
|
||||
// Unreachable, as this is checked by the caller.
|
||||
}
|
||||
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprBinaryOp", op)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,11 @@ pub error "bad GIF header"
|
||||
pub struct decoder?(
|
||||
width u32[..65535],
|
||||
height u32[..65535],
|
||||
background_color_index u8,
|
||||
// gct is the Global Color Table: 256 (R, G, B) entries.
|
||||
//
|
||||
// TODO: 4 byte per pixel RGBA or BGRA instead of 3 bpp RGB?
|
||||
gct[3 * 256] u8,
|
||||
lzw lzw_decoder,
|
||||
)
|
||||
|
||||
@@ -21,7 +26,7 @@ pub func decoder.decode?(dst ptr buf1, src ptr buf1)() {
|
||||
// See the spec section 17 "Header" on page 7.
|
||||
pri func decoder.decode_header?(src ptr buf1)() {
|
||||
var c[6] u8
|
||||
var i u8
|
||||
var i u32
|
||||
while i < 6 {
|
||||
c[i] = in.src.read_u8?()
|
||||
i += 1
|
||||
@@ -37,11 +42,25 @@ pri func decoder.decode_header?(src ptr buf1)() {
|
||||
// See the spec section 18 "Logical Screen Descriptor" on page 8.
|
||||
pri func decoder.decode_lsd?(src ptr buf1)() {
|
||||
var c[7] u8
|
||||
var i u8
|
||||
var i u32
|
||||
while i < 7 {
|
||||
c[i] = in.src.read_u8?()
|
||||
i += 1
|
||||
}
|
||||
this.width = (c[0] as u32) | ((c[1] as u32) << 8)
|
||||
this.height = (c[2] as u32) | ((c[3] as u32) << 8)
|
||||
this.background_color_index = c[5]
|
||||
|
||||
// Read the optional Global Color Table.
|
||||
if (c[4] & 0x80) != 0 {
|
||||
var gct_size u32[..256] = (1 as u32) << (1 + (c[4] & 0x07))
|
||||
i = 0
|
||||
while i < gct_size {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c:gct_size)
|
||||
this.gct[(3 * i) + 0] = in.src.read_u8?()
|
||||
this.gct[(3 * i) + 1] = in.src.read_u8?()
|
||||
this.gct[(3 * i) + 2] = in.src.read_u8?()
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
+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