Decode interlaced GIFs

This commit is contained in:
Nigel Tao committed 2018-06-09 07:46:07 +10:00
1 parent 2e14f0fc6d
commit b2b3593537
8 files changed
+106 -49

No files matched your search

+46 -23
View File
@@ -1355,8 +1355,7 @@ typedef int32_t wuffs_gif__status;
#define WUFFS_GIF__ERROR_NOT_ENOUGH_PIXEL_DATA -1105848315 // 0xBE161805
#define WUFFS_GIF__ERROR_TOO_MUCH_PIXEL_DATA -1105848314 // 0xBE161806
#define WUFFS_GIF__ERROR_INTERNAL_ERROR_INCONSISTENT_RI_WI \
-1105848313 // 0xBE161807
#define WUFFS_GIF__ERROR_TODO_UNSUPPORTED_INTERLACING -1105848312 // 0xBE161808
-1105848313 // 0xBE161807
bool wuffs_gif__status__is_error(wuffs_gif__status s);
@@ -1385,7 +1384,7 @@ typedef struct {
bool f_previous_lzw_decode_ended_abruptly;
bool f_previous_use_global_palette;
uint8_t f_background_color_index;
bool f_interlace;
uint8_t f_interlace;
bool f_seen_num_loops;
uint32_t f_num_loops;
bool f_seen_graphic_control;
@@ -1397,6 +1396,7 @@ typedef struct {
uint32_t f_dst_x;
uint32_t f_dst_y;
uint32_t f_dst_x0;
uint32_t f_dst_y0;
uint32_t f_dst_x1;
uint32_t f_dst_y1;
uint32_t f_uncompressed_ri;
@@ -2091,16 +2091,11 @@ bool wuffs_gif__status__is_error(wuffs_gif__status s) {
return s < 0;
}
const char* wuffs_gif__status__strings[9] = {
"gif: bad block",
"gif: bad extension label",
"gif: bad graphic control",
"gif: bad header",
"gif: bad literal width",
"gif: not enough pixel data",
"gif: too much pixel data",
"gif: internal error: inconsistent ri/wi",
"gif: TODO: unsupported interlacing",
const char* wuffs_gif__status__strings[8] = {
"gif: bad block", "gif: bad extension label",
"gif: bad graphic control", "gif: bad header",
"gif: bad literal width", "gif: not enough pixel data",
"gif: too much pixel data", "gif: internal error: inconsistent ri/wi",
};
const char* wuffs_gif__status__string(wuffs_gif__status s) {
@@ -2113,7 +2108,7 @@ const char* wuffs_gif__status__string(wuffs_gif__status s) {
break;
case wuffs_gif__packageid:
a = wuffs_gif__status__strings;
n = 9;
n = 8;
break;
case wuffs_lzw__packageid:
return wuffs_lzw__status__string(s);
@@ -2124,6 +2119,14 @@ const char* wuffs_gif__status__string(wuffs_gif__status s) {
// ---------------- Private Consts
static const uint32_t wuffs_gif__interlace_start[5] = {
4294967295, 1, 2, 4, 0,
};
static const uint8_t wuffs_gif__interlace_delta[5] = {
1, 2, 4, 8, 8,
};
static const uint8_t wuffs_gif__animexts1dot0[11] = {
65, 78, 73, 77, 69, 88, 84, 83, 49, 46, 48,
};
@@ -3346,6 +3349,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part0(
self->private_impl.f_dst_x = v_frame_x;
self->private_impl.f_dst_y = v_frame_y;
self->private_impl.f_dst_x0 = v_frame_x;
self->private_impl.f_dst_y0 = v_frame_y;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
uint16_t t_5;
@@ -3516,7 +3520,11 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1(
uint8_t t_0 = *ioptr_src++;
v_flags = t_0;
}
self->private_impl.f_interlace = ((v_flags & 64) != 0);
if ((v_flags & 64) != 0) {
self->private_impl.f_interlace = 4;
} else {
self->private_impl.f_interlace = 0;
}
v_use_local_palette = ((v_flags & 128) != 0);
if (v_use_local_palette) {
v_num_palette_entries = (((uint32_t)(1)) << (1 + (v_flags & 7)));
@@ -3788,12 +3796,7 @@ label_0_continue:;
status = WUFFS_GIF__ERROR_TOO_MUCH_PIXEL_DATA;
goto exit;
}
if (self->private_impl.f_interlace) {
status = WUFFS_GIF__ERROR_TODO_UNSUPPORTED_INTERLACING;
goto exit;
} else {
v_dst = wuffs_base__table_u8__row(v_tab, self->private_impl.f_dst_y);
}
v_dst = wuffs_base__table_u8__row(v_tab, self->private_impl.f_dst_y);
if (((uint64_t)(self->private_impl.f_dst_x)) < ((uint64_t)(v_dst.len))) {
if ((((uint64_t)(self->private_impl.f_dst_x)) <=
((uint64_t)(self->private_impl.f_dst_x1))) &&
@@ -3816,7 +3819,17 @@ label_0_continue:;
}
if (self->private_impl.f_dst_x1 <= self->private_impl.f_dst_x) {
self->private_impl.f_dst_x = self->private_impl.f_dst_x0;
self->private_impl.f_dst_y += 1;
wuffs_base__u32__sat_add_indirect(
&self->private_impl.f_dst_y,
((uint32_t)(
wuffs_gif__interlace_delta[self->private_impl.f_interlace])));
while ((self->private_impl.f_interlace > 0) &&
(self->private_impl.f_dst_y >= self->private_impl.f_dst_y1)) {
self->private_impl.f_interlace -= 1;
self->private_impl.f_dst_y = wuffs_base__u32__sat_add(
self->private_impl.f_dst_y0,
wuffs_gif__interlace_start[self->private_impl.f_interlace]);
}
goto label_0_continue;
}
if (self->private_impl.f_uncompressed_wi ==
@@ -3836,7 +3849,17 @@ label_0_continue:;
wuffs_base__u32__sat_add_indirect(&self->private_impl.f_dst_x, v_n);
if (self->private_impl.f_dst_x1 <= self->private_impl.f_dst_x) {
self->private_impl.f_dst_x = self->private_impl.f_dst_x0;
self->private_impl.f_dst_y += 1;
wuffs_base__u32__sat_add_indirect(
&self->private_impl.f_dst_y,
((uint32_t)(
wuffs_gif__interlace_delta[self->private_impl.f_interlace])));
while ((self->private_impl.f_interlace > 0) &&
(self->private_impl.f_dst_y >= self->private_impl.f_dst_y1)) {
self->private_impl.f_interlace -= 1;
self->private_impl.f_dst_y = wuffs_base__u32__sat_add(
self->private_impl.f_dst_y0,
wuffs_gif__interlace_start[self->private_impl.f_interlace]);
}
goto label_0_continue;
}
if (self->private_impl.f_uncompressed_ri !=
+3 -3
View File
@@ -1355,8 +1355,7 @@ typedef int32_t wuffs_gif__status;
#define WUFFS_GIF__ERROR_NOT_ENOUGH_PIXEL_DATA -1105848315 // 0xBE161805
#define WUFFS_GIF__ERROR_TOO_MUCH_PIXEL_DATA -1105848314 // 0xBE161806
#define WUFFS_GIF__ERROR_INTERNAL_ERROR_INCONSISTENT_RI_WI \
-1105848313 // 0xBE161807
#define WUFFS_GIF__ERROR_TODO_UNSUPPORTED_INTERLACING -1105848312 // 0xBE161808
-1105848313 // 0xBE161807
bool wuffs_gif__status__is_error(wuffs_gif__status s);
@@ -1385,7 +1384,7 @@ typedef struct {
bool f_previous_lzw_decode_ended_abruptly;
bool f_previous_use_global_palette;
uint8_t f_background_color_index;
bool f_interlace;
uint8_t f_interlace;
bool f_seen_num_loops;
uint32_t f_num_loops;
bool f_seen_graphic_control;
@@ -1397,6 +1396,7 @@ typedef struct {
uint32_t f_dst_x;
uint32_t f_dst_y;
uint32_t f_dst_x0;
uint32_t f_dst_y0;
uint32_t f_dst_x1;
uint32_t f_dst_y1;
uint32_t f_uncompressed_ri;
+35 -13
View File
@@ -26,7 +26,21 @@ pub error "too much pixel data"
pri error "internal error: inconsistent ri/wi"
pub error "TODO: unsupported interlacing"
// See the spec appendix E "Interlaced Images" on page 29. The first element
// represents either that the frame was non-interlaced, or that all interlace
// stages are complete. Otherwise, the four interlace stages are elements 4, 3,
// 2 and 1 in descending order. For example, the start and delta for the first
// interlace stage is 0 and 8, for the second interlace stage is 4 and 8, etc.
// For interlaced frames, the decoder.interlace field starts at 4 and is
// decremented towards 0.
//
// interlace_start[0] is a special case. For non-interlaced frames, that
// element is never accessed. For interlaced frames, that element is only
// accessed after all interlace stages are complete. Being the maximum base.u32
// value means that, after all interlace stages are complete, dst_y will be set
// to that maximum value (and therefore outside the frame rect).
pri const interlace_start array[5] base.u32 = $(0xFFFFFFFF, 1, 2, 4, 0)
pri const interlace_delta array[5] base.u8 = $(1, 2, 4, 8, 8)
pub struct decoder?(
width base.u32,
@@ -50,7 +64,8 @@ pub struct decoder?(
background_color_index base.u8,
interlace base.bool,
// interlace indexes the interlace_start and interlace_delta arrays.
interlace base.u8[..4],
// Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of
// animation loops is 1.
@@ -71,6 +86,7 @@ pub struct decoder?(
dst_y base.u32,
// TODO: these are just frame_rect fields.
dst_x0 base.u32,
dst_y0 base.u32,
dst_x1 base.u32,
dst_y1 base.u32,
@@ -374,6 +390,7 @@ pri func decoder.decode_id_part0?(src base.io_reader)() {
this.dst_x = frame_x
this.dst_y = frame_y
this.dst_x0 = frame_x
this.dst_y0 = frame_y
frame_x ~mod+= in.src.read_u16le?() as base.u32
frame_y ~mod+= in.src.read_u16le?() as base.u32
@@ -394,9 +411,12 @@ pri func decoder.decode_id_part0?(src base.io_reader)() {
}
pri func decoder.decode_id_part1?(dst ptr base.image_buffer, src base.io_reader)() {
// TODO: use this.interlace.
var flags base.u8 = in.src.read_u8?()
this.interlace = (flags & 0x40) != 0
if (flags & 0x40) != 0 {
this.interlace = 4
} else {
this.interlace = 0
}
// Read the optional Local Color Table.
var use_local_palette base.bool = (flags & 0x80) != 0
@@ -550,17 +570,11 @@ pri func decoder.copy_to_image_buffer?(ib ptr base.image_buffer)() {
if this.dst_y >= this.dst_y1 {
return error "too much pixel data"
}
assert this.dst_y < 0xFFFFFFFF via "a < b: a < c; c <= b"(c:this.dst_y1)
// First, copy from src to that part of this.frame_rect that is inside
// in.ib's bounds.
if this.interlace {
return error "TODO: unsupported interlacing"
} else {
dst = tab.row(y:this.dst_y)
}
dst = tab.row(y:this.dst_y)
if (this.dst_x as base.u64) < dst.length() {
if ((this.dst_x as base.u64) <= (this.dst_x1 as base.u64)) and
((this.dst_x1 as base.u64) <= dst.length()) {
@@ -578,7 +592,11 @@ pri func decoder.copy_to_image_buffer?(ib ptr base.image_buffer)() {
if this.dst_x1 <= this.dst_x {
this.dst_x = this.dst_x0
this.dst_y += 1
this.dst_y ~sat+= interlace_delta[this.interlace] as base.u32
while (this.interlace > 0) and (this.dst_y >= this.dst_y1) {
this.interlace -= 1
this.dst_y = this.dst_y0 ~sat+ interlace_start[this.interlace]
}
continue
}
@@ -601,7 +619,11 @@ pri func decoder.copy_to_image_buffer?(ib ptr base.image_buffer)() {
if this.dst_x1 <= this.dst_x {
this.dst_x = this.dst_x0
this.dst_y += 1
this.dst_y ~sat+= interlace_delta[this.interlace] as base.u32
while (this.interlace > 0) and (this.dst_y >= this.dst_y1) {
this.interlace -= 1
this.dst_y = this.dst_y0 ~sat+ interlace_start[this.interlace]
}
continue
}
+22 -10
View File
@@ -681,6 +681,16 @@ void test_mimic_gif_decode_hibiscus() {
do_test_mimic_gif_decode("../../data/hibiscus.gif");
}
void test_mimic_gif_decode_hippopotamus_interlaced() {
CHECK_FOCUS(__func__);
do_test_mimic_gif_decode("../../data/hippopotamus.interlaced.gif");
}
void test_mimic_gif_decode_hippopotamus_regular() {
CHECK_FOCUS(__func__);
do_test_mimic_gif_decode("../../data/hippopotamus.regular.gif");
}
void test_mimic_gif_decode_muybridge() {
CHECK_FOCUS(__func__);
do_test_mimic_gif_decode("../../data/muybridge.gif");
@@ -812,16 +822,18 @@ proc tests[] = {
#ifdef WUFFS_MIMIC
test_mimic_gif_decode_animated_small, //
test_mimic_gif_decode_bricks_dither, //
test_mimic_gif_decode_bricks_gray, //
test_mimic_gif_decode_bricks_nodither, //
test_mimic_gif_decode_gifplayer_muybridge, //
test_mimic_gif_decode_harvesters, //
test_mimic_gif_decode_hat, //
test_mimic_gif_decode_hibiscus, //
test_mimic_gif_decode_muybridge, //
test_mimic_gif_decode_pjw_thumbnail, //
test_mimic_gif_decode_animated_small, //
test_mimic_gif_decode_bricks_dither, //
test_mimic_gif_decode_bricks_gray, //
test_mimic_gif_decode_bricks_nodither, //
test_mimic_gif_decode_gifplayer_muybridge, //
test_mimic_gif_decode_harvesters, //
test_mimic_gif_decode_hat, //
test_mimic_gif_decode_hibiscus, //
test_mimic_gif_decode_hippopotamus_interlaced, //
test_mimic_gif_decode_hippopotamus_regular, //
test_mimic_gif_decode_muybridge, //
test_mimic_gif_decode_pjw_thumbnail, //
#endif // WUFFS_MIMIC
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

File renamed without changes.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

File renamed without changes.

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB