mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Fill out the lzw_decoder implementation.
This commit is contained in:
1 parent
8e279c51bf
commit
8d6694a752
5 files changed
+78
-34
No files matched your search
+25
-9
@@ -63,7 +63,8 @@ typedef enum {
|
||||
puffs_gif_error_unexpected_eof = -10 + 1,
|
||||
puffs_gif_status_short_read = -12,
|
||||
puffs_gif_status_short_write = -14,
|
||||
puffs_gif_error_bad_gif_image = -256 + 1,
|
||||
puffs_gif_error_lzw_code_is_out_of_range = -256 + 1,
|
||||
puffs_gif_error_lzw_prefix_chain_is_cyclical = -258 + 1,
|
||||
} puffs_gif_status;
|
||||
|
||||
bool puffs_gif_status_is_error(puffs_gif_status s);
|
||||
@@ -122,7 +123,7 @@ bool puffs_gif_status_is_error(puffs_gif_status s) {
|
||||
return s & 1;
|
||||
}
|
||||
|
||||
const char* puffs_gif_status_strings[9] = {
|
||||
const char* puffs_gif_status_strings[10] = {
|
||||
"gif: ok",
|
||||
"gif: bad version",
|
||||
"gif: bad receiver",
|
||||
@@ -131,7 +132,8 @@ const char* puffs_gif_status_strings[9] = {
|
||||
"gif: unexpected EOF",
|
||||
"gif: short read",
|
||||
"gif: short write",
|
||||
"gif: bad GIF image",
|
||||
"gif: LZW code is out of range",
|
||||
"gif: LZW prefix chain is cyclical",
|
||||
};
|
||||
|
||||
const char* puffs_gif_status_string(puffs_gif_status s) {
|
||||
@@ -141,7 +143,7 @@ const char* puffs_gif_status_string(puffs_gif_status s) {
|
||||
return puffs_gif_status_strings[s];
|
||||
}
|
||||
s -= 120;
|
||||
if ((8 <= s) && (s < 9)) {
|
||||
if ((8 <= s) && (s < 10)) {
|
||||
return puffs_gif_status_strings[s];
|
||||
}
|
||||
}
|
||||
@@ -225,6 +227,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
||||
uint32_t v_n_bits;
|
||||
uint32_t v_code;
|
||||
uint32_t v_s;
|
||||
uint32_t v_c;
|
||||
|
||||
v_clear_code = (((uint32_t)(1)) << self->f_literal_width);
|
||||
v_end_code = (v_clear_code + 1);
|
||||
@@ -269,17 +272,30 @@ label_0_continue:;
|
||||
goto cleanup0;
|
||||
} else if (v_code <= v_save_code) {
|
||||
v_s = 4095;
|
||||
while (true) {
|
||||
v_c = v_code;
|
||||
if ((v_code == v_save_code) && v_use_save_code) {
|
||||
v_s -= 1;
|
||||
v_c = v_prev_code;
|
||||
}
|
||||
while (v_c >= v_clear_code) {
|
||||
self->f_stack[v_s] = self->f_suffixes[v_c];
|
||||
if (v_s == 0) {
|
||||
status = puffs_gif_error_bad_gif_image;
|
||||
status = puffs_gif_error_lzw_prefix_chain_is_cyclical;
|
||||
goto cleanup0;
|
||||
}
|
||||
v_s -= 1;
|
||||
goto label_1_break;
|
||||
v_c = ((uint32_t)(self->f_prefixes[v_c]));
|
||||
}
|
||||
self->f_stack[v_s] = ((uint8_t)(v_c));
|
||||
if ((v_code == v_save_code) && v_use_save_code) {
|
||||
self->f_stack[4095] = ((uint8_t)(v_c));
|
||||
}
|
||||
if (v_use_save_code) {
|
||||
self->f_suffixes[v_save_code] = self->f_stack[4095];
|
||||
self->f_prefixes[v_save_code] = ((uint16_t)(v_prev_code));
|
||||
}
|
||||
label_1_break:;
|
||||
} else {
|
||||
status = puffs_gif_error_bad_gif_image;
|
||||
status = puffs_gif_error_lzw_code_is_out_of_range;
|
||||
goto cleanup0;
|
||||
}
|
||||
v_use_save_code = (v_save_code < 4095);
|
||||
|
||||
+2
-1
@@ -63,7 +63,8 @@ typedef enum {
|
||||
puffs_gif_error_unexpected_eof = -10 + 1,
|
||||
puffs_gif_status_short_read = -12,
|
||||
puffs_gif_status_short_write = -14,
|
||||
puffs_gif_error_bad_gif_image = -256 + 1,
|
||||
puffs_gif_error_lzw_code_is_out_of_range = -256 + 1,
|
||||
puffs_gif_error_lzw_prefix_chain_is_cyclical = -258 + 1,
|
||||
} puffs_gif_status;
|
||||
|
||||
bool puffs_gif_status_is_error(puffs_gif_status s);
|
||||
|
||||
+1
-1
@@ -373,7 +373,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
n.String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
|
||||
}
|
||||
rTyp := rhs.MType()
|
||||
if !rTyp.IsNumType() {
|
||||
if !rTyp.IsNumTypeOrIdeal() {
|
||||
return fmt.Errorf("%s is an array-index expression but %s has type %s, not a numeric type",
|
||||
n.String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
|
||||
}
|
||||
|
||||
+29
-10
@@ -3,7 +3,8 @@
|
||||
|
||||
// TODO: some mechanism for fixing the error code's value, such as a trailing
|
||||
// "= 100", to maintain API stability.
|
||||
pub error "bad GIF image"
|
||||
pub error "LZW code is out of range"
|
||||
pub error "LZW prefix chain is cyclical"
|
||||
|
||||
pub struct lzw_decoder?(
|
||||
literal_width u32[2..8] = 8,
|
||||
@@ -63,21 +64,39 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
|
||||
|
||||
} else if code <= save_code {
|
||||
var s u32[..4095] = 4095
|
||||
var c u32[..4095] = code
|
||||
|
||||
while true,
|
||||
inv n_bits < 8,
|
||||
{
|
||||
if s == 0 {
|
||||
return error "bad GIF image"
|
||||
}
|
||||
if (code == save_code) and use_save_code {
|
||||
s -= 1
|
||||
break
|
||||
c = prev_code
|
||||
}
|
||||
|
||||
// TODO: implement.
|
||||
while c >= clear_code,
|
||||
inv n_bits < 8,
|
||||
post c < 256 via "a < b: a < c; c <= b"(c:clear_code),
|
||||
{
|
||||
this.stack[s] = this.suffixes[c]
|
||||
if s == 0 {
|
||||
return error "LZW prefix chain is cyclical"
|
||||
}
|
||||
s -= 1
|
||||
c = this.prefixes[c] as u32
|
||||
}
|
||||
this.stack[s] = c as u8
|
||||
|
||||
if (code == save_code) and use_save_code {
|
||||
this.stack[4095] = c as u8
|
||||
}
|
||||
|
||||
// TODO: in.dst.write?(this.stack[s:])
|
||||
|
||||
if use_save_code {
|
||||
this.suffixes[save_code] = this.stack[4095]
|
||||
this.prefixes[save_code] = prev_code as u16
|
||||
}
|
||||
|
||||
} else {
|
||||
return error "bad GIF image"
|
||||
return error "LZW code is out of range"
|
||||
}
|
||||
|
||||
use_save_code = save_code < 4095
|
||||
|
||||
+21
-13
@@ -93,36 +93,44 @@ void test_status_is_error() {
|
||||
FAIL("test_status_is_error: is_error(short_write) returned true");
|
||||
return;
|
||||
}
|
||||
if (!puffs_gif_status_is_error(puffs_gif_error_bad_gif_image)) {
|
||||
FAIL("test_status_is_error: is_error(bad_gif_image) returned false");
|
||||
if (!puffs_gif_status_is_error(puffs_gif_error_lzw_code_is_out_of_range)) {
|
||||
FAIL(
|
||||
"test_status_is_error: is_error(lzw_code_is_out_of_range) returned "
|
||||
"false");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void test_status_strings() {
|
||||
const char* s0 = puffs_gif_status_string(puffs_gif_status_ok);
|
||||
if (strcmp(s0, "gif: ok")) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"gif: ok\"", s0);
|
||||
const char* t0 = "gif: ok";
|
||||
if (strcmp(s0, t0)) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"%s\"", s0, t0);
|
||||
return;
|
||||
}
|
||||
const char* s1 = puffs_gif_status_string(puffs_gif_error_bad_version);
|
||||
if (strcmp(s1, "gif: bad version")) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"gif: bad version\"", s1);
|
||||
const char* t1 = "gif: bad version";
|
||||
if (strcmp(s1, t1)) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"%s\"", s1, t1);
|
||||
return;
|
||||
}
|
||||
const char* s2 = puffs_gif_status_string(puffs_gif_status_short_write);
|
||||
if (strcmp(s2, "gif: short write")) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"gif: short write\"", s2);
|
||||
const char* t2 = "gif: short write";
|
||||
if (strcmp(s2, t2)) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"%s\"", s2, t2);
|
||||
return;
|
||||
}
|
||||
const char* s3 = puffs_gif_status_string(puffs_gif_error_bad_gif_image);
|
||||
if (strcmp(s3, "gif: bad GIF image")) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"gif: bad GIF image\"", s3);
|
||||
const char* s3 =
|
||||
puffs_gif_status_string(puffs_gif_error_lzw_code_is_out_of_range);
|
||||
const char* t3 = "gif: LZW code is out of range";
|
||||
if (strcmp(s3, t3)) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"%s\"", s3, t3);
|
||||
return;
|
||||
}
|
||||
const char* s4 = puffs_gif_status_string(-254);
|
||||
if (strcmp(s4, "gif: unknown status")) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"gif: unknown status\"", s4);
|
||||
const char* t4 = "gif: unknown status";
|
||||
if (strcmp(s4, t4)) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"%s\"", s4, t4);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user