mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 08:47:56 +01:00
Allow an LZW literal width of 1
This commit is contained in:
1 parent
d1133b105e
commit
5bea867f72
4 files changed
+63
-6
No files matched your search
@@ -8102,7 +8102,7 @@ wuffs_lzw__decoder__set_literal_width(wuffs_lzw__decoder* self, uint32_t a_lw) {
|
|||||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||||
return wuffs_base__make_empty_struct();
|
return wuffs_base__make_empty_struct();
|
||||||
}
|
}
|
||||||
if (a_lw < 2 || a_lw > 8) {
|
if (a_lw < 1 || a_lw > 8) {
|
||||||
self->private_impl.magic = WUFFS_BASE__DISABLED;
|
self->private_impl.magic = WUFFS_BASE__DISABLED;
|
||||||
return wuffs_base__make_empty_struct();
|
return wuffs_base__make_empty_struct();
|
||||||
}
|
}
|
||||||
@@ -8158,7 +8158,7 @@ wuffs_lzw__decoder__decode_io_writer(wuffs_lzw__decoder* self,
|
|||||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||||
|
|
||||||
self->private_impl.f_literal_width = 8;
|
self->private_impl.f_literal_width = 8;
|
||||||
if (self->private_impl.f_set_literal_width_arg >= 2) {
|
if (self->private_impl.f_set_literal_width_arg > 0) {
|
||||||
self->private_impl.f_literal_width =
|
self->private_impl.f_literal_width =
|
||||||
self->private_impl.f_set_literal_width_arg;
|
self->private_impl.f_set_literal_width_arg;
|
||||||
}
|
}
|
||||||
@@ -10702,7 +10702,7 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self,
|
|||||||
uint8_t t_2 = *iop_a_src++;
|
uint8_t t_2 = *iop_a_src++;
|
||||||
v_lw = t_2;
|
v_lw = t_2;
|
||||||
}
|
}
|
||||||
if ((v_lw < 2) || (8 < v_lw)) {
|
if ((v_lw < 1) || (8 < v_lw)) {
|
||||||
status = wuffs_gif__error__bad_literal_width;
|
status = wuffs_gif__error__bad_literal_width;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -844,8 +844,11 @@ pri func decoder.decode_id_part1?(dst ptr base.pixel_buffer, src base.io_reader)
|
|||||||
this.lzw.reset!()
|
this.lzw.reset!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Process the LZW literal width. The spec says that "images which have one
|
||||||
|
// color bit must be indicated as having a code size [i.e. literal width]
|
||||||
|
// of 2", but in practice, some encoders use a literal width of 1.
|
||||||
lw = args.src.read_u8?()
|
lw = args.src.read_u8?()
|
||||||
if (lw < 2) or (8 < lw) {
|
if (lw < 1) or (8 < lw) {
|
||||||
return "#bad literal width"
|
return "#bad literal width"
|
||||||
}
|
}
|
||||||
this.lzw.set_literal_width!(lw:lw as base.u32)
|
this.lzw.set_literal_width!(lw:lw as base.u32)
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ pub struct decoder?(
|
|||||||
output array[8192 + 7] base.u8,
|
output array[8192 + 7] base.u8,
|
||||||
)
|
)
|
||||||
|
|
||||||
pub func decoder.set_literal_width!(lw base.u32[2..8]) {
|
pub func decoder.set_literal_width!(lw base.u32[1..8]) {
|
||||||
this.set_literal_width_arg = args.lw
|
this.set_literal_width_arg = args.lw
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +82,7 @@ pub func decoder.decode_io_writer?(dst base.io_writer, src base.io_reader, workb
|
|||||||
|
|
||||||
// Initialize read_from state.
|
// Initialize read_from state.
|
||||||
this.literal_width = 8
|
this.literal_width = 8
|
||||||
if this.set_literal_width_arg >= 2 {
|
if this.set_literal_width_arg > 0 {
|
||||||
this.literal_width = this.set_literal_width_arg
|
this.literal_width = this.set_literal_width_arg
|
||||||
}
|
}
|
||||||
this.clear_code = (1 as base.u32) << this.literal_width
|
this.clear_code = (1 as base.u32) << this.literal_width
|
||||||
|
|||||||
@@ -295,6 +295,59 @@ const char* test_wuffs_lzw_decode_output_empty() {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char* test_wuffs_lzw_decode_width_1() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
|
||||||
|
wuffs_base__io_buffer got = ((wuffs_base__io_buffer){
|
||||||
|
.data = global_got_slice,
|
||||||
|
});
|
||||||
|
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
|
||||||
|
.data = global_src_slice,
|
||||||
|
});
|
||||||
|
|
||||||
|
got.data.ptr[0] = 0xFF;
|
||||||
|
got.data.ptr[1] = 0xFF;
|
||||||
|
got.data.ptr[2] = 0xFF;
|
||||||
|
got.data.ptr[3] = 0xFF;
|
||||||
|
|
||||||
|
// Set src to be:
|
||||||
|
// 0b...._...._...._..10 0x010 Clear code.
|
||||||
|
// 0b...._...._...._00.. 0x000 Literal "0".
|
||||||
|
// 0b...._...._.001_.... 0x001 Literal "1".
|
||||||
|
// 0b...._..10_0..._.... 0x100 Back-ref "01".
|
||||||
|
// 0b...0_11.._...._.... 0x011 End code.
|
||||||
|
src.meta.wi = 2;
|
||||||
|
src.data.ptr[0] = 0x12;
|
||||||
|
src.data.ptr[1] = 0x0E;
|
||||||
|
|
||||||
|
wuffs_lzw__decoder dec;
|
||||||
|
const char* status = wuffs_lzw__decoder__initialize(
|
||||||
|
&dec, sizeof dec, WUFFS_VERSION,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED);
|
||||||
|
if (status) {
|
||||||
|
RETURN_FAIL("initialize: \"%s\"", status);
|
||||||
|
}
|
||||||
|
wuffs_lzw__decoder__set_literal_width(&dec, 1);
|
||||||
|
|
||||||
|
status = wuffs_lzw__decoder__decode_io_writer(
|
||||||
|
&dec, wuffs_base__io_buffer__writer(&got),
|
||||||
|
wuffs_base__io_buffer__reader(&src), global_work_slice);
|
||||||
|
if (status) {
|
||||||
|
RETURN_FAIL("decode: \"%s\"", status);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (got.meta.wi != 4) {
|
||||||
|
RETURN_FAIL("got.meta.wi: got %d, want 4", (int)(got.meta.wi));
|
||||||
|
}
|
||||||
|
uint8_t* g = got.data.ptr;
|
||||||
|
if ((g[0] != 0x00) || (g[1] != 0x01) || (g[2] != 0x00) || (g[3] != 0x01)) {
|
||||||
|
RETURN_FAIL(
|
||||||
|
"got {0x%02x, 0x%02x, 0x%02x, 0x%02x}, want {0x00, 0x01, 0x00, 0x01}",
|
||||||
|
(int)(g[0]), (int)(g[1]), (int)(g[2]), (int)(g[3]));
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------- LZW Benches
|
// ---------------- LZW Benches
|
||||||
|
|
||||||
const char* do_bench_wuffs_lzw_decode(const char* filename,
|
const char* do_bench_wuffs_lzw_decode(const char* filename,
|
||||||
@@ -368,6 +421,7 @@ proc tests[] = {
|
|||||||
test_wuffs_lzw_decode_output_bad, //
|
test_wuffs_lzw_decode_output_bad, //
|
||||||
test_wuffs_lzw_decode_output_empty, //
|
test_wuffs_lzw_decode_output_empty, //
|
||||||
test_wuffs_lzw_decode_pi, //
|
test_wuffs_lzw_decode_pi, //
|
||||||
|
test_wuffs_lzw_decode_width_1, //
|
||||||
|
|
||||||
NULL,
|
NULL,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user