Allow GIF decoding to non-indexed pixel formats

This commit is contained in:
Nigel Tao committed 2018-12-08 16:08:02 +11:00
1 parent 90193cb6e2
commit 45ab79b451
9 files changed
+355 -115

No files matched your search

+64 -22
View File
@@ -237,7 +237,8 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst,
const char* do_test_wuffs_gif_decode(const char* filename,
const char* palette_filename,
const char* indexes_filename,
uint64_t rlimit) {
uint64_t rlimit,
wuffs_base__pixel_format dst_pixfmt) {
wuffs_base__io_buffer got = ((wuffs_base__io_buffer){
.data = global_got_slice,
});
@@ -285,6 +286,10 @@ const char* do_test_wuffs_gif_decode(const char* filename,
if (!wuffs_base__image_config__first_frame_is_opaque(&ic)) {
RETURN_FAIL("first_frame_is_opaque: got false, want true");
}
wuffs_base__pixel_config__initialize(
&ic.pixcfg, dst_pixfmt, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, 160, 120);
z = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg,
global_pixel_slice);
if (z) {
@@ -372,10 +377,6 @@ const char* do_test_wuffs_gif_decode(const char* filename,
}
}
wuffs_base__io_buffer pal_got = ((wuffs_base__io_buffer){
.data = wuffs_base__pixel_buffer__palette(&pb),
});
pal_got.meta.wi = pal_got.data.len;
uint8_t pal_want_array[1024];
wuffs_base__io_buffer pal_want = ((wuffs_base__io_buffer){
.data = ((wuffs_base__slice_u8){
@@ -387,9 +388,15 @@ const char* do_test_wuffs_gif_decode(const char* filename,
if (z) {
return z;
}
z = check_io_buffers_equal("palette ", &pal_got, &pal_want);
if (z) {
return z;
if (dst_pixfmt == WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL) {
wuffs_base__io_buffer pal_got = ((wuffs_base__io_buffer){
.data = wuffs_base__pixel_buffer__palette(&pb),
});
pal_got.meta.wi = pal_got.data.len;
z = check_io_buffers_equal("palette ", &pal_got, &pal_want);
if (z) {
return z;
}
}
wuffs_base__io_buffer ind_want = ((wuffs_base__io_buffer){
@@ -399,9 +406,31 @@ const char* do_test_wuffs_gif_decode(const char* filename,
if (z) {
return z;
}
z = check_io_buffers_equal("indexes ", &got, &ind_want);
if (z) {
return z;
if (dst_pixfmt == WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL) {
z = check_io_buffers_equal("indexes ", &got, &ind_want);
if (z) {
return z;
}
} else {
wuffs_base__io_buffer expanded_want = ((wuffs_base__io_buffer){
.data = global_work_slice,
});
if (ind_want.meta.wi > (expanded_want.data.len / 4)) {
RETURN_FAIL("indexes are too long to expand into the work buffer");
}
size_t i;
for (i = 0; i < ind_want.meta.wi; i++) {
uint8_t index = ind_want.data.ptr[i];
expanded_want.data.ptr[4 * i + 0] = pal_want_array[4 * index + 0];
expanded_want.data.ptr[4 * i + 1] = pal_want_array[4 * index + 1];
expanded_want.data.ptr[4 * i + 2] = pal_want_array[4 * index + 2];
expanded_want.data.ptr[4 * i + 3] = pal_want_array[4 * index + 3];
}
expanded_want.meta.wi = 4 * ind_want.meta.wi;
z = check_io_buffers_equal("pixels ", &got, &expanded_want);
if (z) {
return z;
}
}
{
@@ -701,25 +730,36 @@ const char* test_wuffs_gif_decode_frame_out_of_bounds() {
return NULL;
}
const char* test_wuffs_gif_decode_input_is_a_gif() {
const char* test_wuffs_gif_decode_bgra_nonpremul() {
CHECK_FOCUS(__func__);
return do_test_wuffs_gif_decode("test/data/bricks-dither.gif",
"test/data/bricks-dither.palette",
"test/data/bricks-dither.indexes", 0);
"test/data/bricks-dither.indexes", 0,
WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL);
}
const char* test_wuffs_gif_decode_input_is_a_gif() {
CHECK_FOCUS(__func__);
return do_test_wuffs_gif_decode(
"test/data/bricks-dither.gif", "test/data/bricks-dither.palette",
"test/data/bricks-dither.indexes", 0,
WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL);
}
const char* test_wuffs_gif_decode_input_is_a_gif_many_big_reads() {
CHECK_FOCUS(__func__);
return do_test_wuffs_gif_decode("test/data/bricks-dither.gif",
"test/data/bricks-dither.palette",
"test/data/bricks-dither.indexes", 4096);
return do_test_wuffs_gif_decode(
"test/data/bricks-dither.gif", "test/data/bricks-dither.palette",
"test/data/bricks-dither.indexes", 4096,
WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL);
}
const char* test_wuffs_gif_decode_input_is_a_gif_many_medium_reads() {
CHECK_FOCUS(__func__);
return do_test_wuffs_gif_decode("test/data/bricks-dither.gif",
"test/data/bricks-dither.palette",
"test/data/bricks-dither.indexes", 787);
return do_test_wuffs_gif_decode(
"test/data/bricks-dither.gif", "test/data/bricks-dither.palette",
"test/data/bricks-dither.indexes", 787,
WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL);
// The magic 787 tickles being in the middle of a decode_extension skip
// call.
//
@@ -728,9 +768,10 @@ const char* test_wuffs_gif_decode_input_is_a_gif_many_medium_reads() {
const char* test_wuffs_gif_decode_input_is_a_gif_many_small_reads() {
CHECK_FOCUS(__func__);
return do_test_wuffs_gif_decode("test/data/bricks-dither.gif",
"test/data/bricks-dither.palette",
"test/data/bricks-dither.indexes", 13);
return do_test_wuffs_gif_decode(
"test/data/bricks-dither.gif", "test/data/bricks-dither.palette",
"test/data/bricks-dither.indexes", 13,
WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL);
}
const char* test_wuffs_gif_decode_input_is_a_png() {
@@ -1384,6 +1425,7 @@ proc tests[] = {
test_wuffs_gif_decode_animated_big, //
test_wuffs_gif_decode_animated_medium, //
test_wuffs_gif_decode_animated_small, //
test_wuffs_gif_decode_bgra_nonpremul, //
test_wuffs_gif_decode_frame_out_of_bounds, //
test_wuffs_gif_decode_input_is_a_gif, //
test_wuffs_gif_decode_input_is_a_gif_many_big_reads, //
+34 -4
View File
@@ -231,7 +231,8 @@ const char* chdir_to_the_wuffs_root_directory() {
return "could not chdir(\"..\")";
}
}
return "could not find Wuffs root directory; chdir there before running this program";
return "could not find Wuffs root directory; chdir there before running this "
"program";
}
typedef const char* (*proc)();
@@ -393,17 +394,46 @@ void set_writer_limit(wuffs_base__io_writer* o, uint64_t limit) {
const char* copy_to_io_buffer_from_pixel_buffer(wuffs_base__io_buffer* dst,
wuffs_base__pixel_buffer* src,
wuffs_base__rect_ie_u32 r) {
// TODO: don't assume 1 plane or WUFFS_BASE__PIXEL_SUBSAMPLING__NONE.
if (!src) {
return "copy_to_io_buffer_from_pixel_buffer: NULL src";
}
// TODO: don't assume 1 plane or WUFFS_BASE__PIXEL_SUBSAMPLING__NONE. Also,
// support more pixel formats.
size_t bytes_per_pixel = 0;
wuffs_base__pixel_format pixfmt =
wuffs_base__pixel_config__pixel_format(&src->pixcfg);
switch (pixfmt) {
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL:
bytes_per_pixel = 1;
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL:
bytes_per_pixel = 4;
break;
default:
RETURN_FAIL(
"copy_to_io_buffer_from_pixel_buffer: unsupported src pixfmt: 0x%08x",
(int)(pixfmt));
}
uint32_t p;
for (p = 0; p < 1; p++) {
wuffs_base__table_u8 tab = wuffs_base__pixel_buffer__plane(src, p);
uint32_t y;
for (y = r.min_incl_y; y < r.max_excl_y; y++) {
wuffs_base__slice_u8 row = wuffs_base__table_u8__row(tab, y);
if ((r.min_incl_x >= r.max_excl_x) || (r.max_excl_x > row.len)) {
if ((r.min_incl_x >= r.max_excl_x) ||
(r.max_excl_x > (row.len / bytes_per_pixel))) {
break;
}
uint32_t n = r.max_excl_x - r.min_incl_x;
size_t n = r.max_excl_x - r.min_incl_x;
if (n > (SIZE_MAX / bytes_per_pixel)) {
return "copy_to_io_buffer_from_pixel_buffer: n is too large";
}
n *= bytes_per_pixel;
if (n > (dst->data.len - dst->meta.wi)) {
return "copy_to_io_buffer_from_pixel_buffer: dst buffer is too small";
}