mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 00:47:56 +01:00
Add wuffs_gif__quirk_reject_empty_palette
This commit is contained in:
1 parent
1311445408
commit
746681ea68
6 files changed
+177
-21
No files matched your search
@@ -3268,6 +3268,7 @@ extern const char* wuffs_gif__error__bad_extension_label;
|
|||||||
extern const char* wuffs_gif__error__bad_graphic_control;
|
extern const char* wuffs_gif__error__bad_graphic_control;
|
||||||
extern const char* wuffs_gif__error__bad_header;
|
extern const char* wuffs_gif__error__bad_header;
|
||||||
extern const char* wuffs_gif__error__bad_literal_width;
|
extern const char* wuffs_gif__error__bad_literal_width;
|
||||||
|
extern const char* wuffs_gif__error__bad_palette;
|
||||||
|
|
||||||
// ---------------- Public Consts
|
// ---------------- Public Consts
|
||||||
|
|
||||||
@@ -3420,6 +3421,7 @@ struct wuffs_gif__decoder__struct {
|
|||||||
bool f_end_of_data;
|
bool f_end_of_data;
|
||||||
bool f_restarted;
|
bool f_restarted;
|
||||||
bool f_previous_lzw_decode_ended_abruptly;
|
bool f_previous_lzw_decode_ended_abruptly;
|
||||||
|
bool f_has_global_palette;
|
||||||
uint8_t f_which_palette;
|
uint8_t f_which_palette;
|
||||||
uint8_t f_interlace;
|
uint8_t f_interlace;
|
||||||
bool f_seen_num_loops;
|
bool f_seen_num_loops;
|
||||||
@@ -8531,6 +8533,7 @@ const char* wuffs_gif__error__bad_extension_label = "#gif: bad extension label";
|
|||||||
const char* wuffs_gif__error__bad_graphic_control = "#gif: bad graphic control";
|
const char* wuffs_gif__error__bad_graphic_control = "#gif: bad graphic control";
|
||||||
const char* wuffs_gif__error__bad_header = "#gif: bad header";
|
const char* wuffs_gif__error__bad_header = "#gif: bad header";
|
||||||
const char* wuffs_gif__error__bad_literal_width = "#gif: bad literal width";
|
const char* wuffs_gif__error__bad_literal_width = "#gif: bad literal width";
|
||||||
|
const char* wuffs_gif__error__bad_palette = "#gif: bad palette";
|
||||||
const char* wuffs_gif__error__internal_error_inconsistent_ri_wi =
|
const char* wuffs_gif__error__internal_error_inconsistent_ri_wi =
|
||||||
"#gif: internal error: inconsistent ri/wi";
|
"#gif: internal error: inconsistent ri/wi";
|
||||||
|
|
||||||
@@ -9692,9 +9695,10 @@ wuffs_gif__decoder__decode_lsd(wuffs_gif__decoder* self,
|
|||||||
goto suspend;
|
goto suspend;
|
||||||
}
|
}
|
||||||
iop_a_src += self->private_data.s_decode_lsd[0].scratch;
|
iop_a_src += self->private_data.s_decode_lsd[0].scratch;
|
||||||
if ((v_flags & 128) != 0) {
|
v_i = 0;
|
||||||
|
self->private_impl.f_has_global_palette = ((v_flags & 128) != 0);
|
||||||
|
if (self->private_impl.f_has_global_palette) {
|
||||||
v_num_palette_entries = (((uint32_t)(1)) << (1 + (v_flags & 7)));
|
v_num_palette_entries = (((uint32_t)(1)) << (1 + (v_flags & 7)));
|
||||||
v_i = 0;
|
|
||||||
while (v_i < v_num_palette_entries) {
|
while (v_i < v_num_palette_entries) {
|
||||||
{
|
{
|
||||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
|
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
|
||||||
@@ -9736,13 +9740,13 @@ wuffs_gif__decoder__decode_lsd(wuffs_gif__decoder* self,
|
|||||||
((uint8_t)(((v_argb >> 24) & 255)));
|
((uint8_t)(((v_argb >> 24) & 255)));
|
||||||
v_i += 1;
|
v_i += 1;
|
||||||
}
|
}
|
||||||
while (v_i < 256) {
|
}
|
||||||
self->private_data.f_palettes[0][((4 * v_i) + 0)] = 0;
|
while (v_i < 256) {
|
||||||
self->private_data.f_palettes[0][((4 * v_i) + 1)] = 0;
|
self->private_data.f_palettes[0][((4 * v_i) + 0)] = 0;
|
||||||
self->private_data.f_palettes[0][((4 * v_i) + 2)] = 0;
|
self->private_data.f_palettes[0][((4 * v_i) + 1)] = 0;
|
||||||
self->private_data.f_palettes[0][((4 * v_i) + 3)] = 255;
|
self->private_data.f_palettes[0][((4 * v_i) + 2)] = 0;
|
||||||
v_i += 1;
|
self->private_data.f_palettes[0][((4 * v_i) + 3)] = 255;
|
||||||
}
|
v_i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
goto ok;
|
goto ok;
|
||||||
@@ -10646,6 +10650,10 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self,
|
|||||||
self->private_data.f_palettes[1][((4 * v_i) + 3)] = 255;
|
self->private_data.f_palettes[1][((4 * v_i) + 3)] = 255;
|
||||||
v_i += 1;
|
v_i += 1;
|
||||||
}
|
}
|
||||||
|
} else if (self->private_impl.f_quirk_enabled_reject_empty_palette &&
|
||||||
|
!self->private_impl.f_has_global_palette) {
|
||||||
|
status = wuffs_gif__error__bad_palette;
|
||||||
|
goto exit;
|
||||||
} else if (self->private_impl.f_gc_has_transparent_index) {
|
} else if (self->private_impl.f_gc_has_transparent_index) {
|
||||||
wuffs_base__slice_u8__copy_from_slice(
|
wuffs_base__slice_u8__copy_from_slice(
|
||||||
wuffs_base__make_slice_u8(self->private_data.f_palettes[1], 1024),
|
wuffs_base__make_slice_u8(self->private_data.f_palettes[1], 1024),
|
||||||
|
|||||||
@@ -508,6 +508,7 @@ var gifGlobals struct {
|
|||||||
frameHeight uint32
|
frameHeight uint32
|
||||||
|
|
||||||
globalPalette [][4]uint8
|
globalPalette [][4]uint8
|
||||||
|
localPalette [][4]uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
func stateGif(line string) (stateFunc, error) {
|
func stateGif(line string) (stateFunc, error) {
|
||||||
@@ -519,6 +520,7 @@ func stateGif(line string) (stateFunc, error) {
|
|||||||
outer:
|
outer:
|
||||||
switch {
|
switch {
|
||||||
case line == "frame {":
|
case line == "frame {":
|
||||||
|
gifGlobals.localPalette = nil
|
||||||
out = append(out, 0x2C)
|
out = append(out, 0x2C)
|
||||||
return stateGifFrame, nil
|
return stateGifFrame, nil
|
||||||
|
|
||||||
@@ -661,12 +663,22 @@ func stateGifFrame(line string) (stateFunc, error) {
|
|||||||
out = appendU16LE(out, uint16(g.frameTop))
|
out = appendU16LE(out, uint16(g.frameTop))
|
||||||
out = appendU16LE(out, uint16(g.frameWidth))
|
out = appendU16LE(out, uint16(g.frameWidth))
|
||||||
out = appendU16LE(out, uint16(g.frameHeight))
|
out = appendU16LE(out, uint16(g.frameHeight))
|
||||||
out = append(out, 0x00) // TODO: flags.
|
if g.localPalette == nil {
|
||||||
|
out = append(out, 0x00)
|
||||||
|
} else if n := log2(uint32(len(g.localPalette))); n < 2 || 8 < n {
|
||||||
|
return nil, fmt.Errorf("bad len(g.localPalette): %d", len(g.localPalette))
|
||||||
|
} else {
|
||||||
|
out = append(out, 0x80|uint8(n-1))
|
||||||
|
}
|
||||||
|
for _, x := range g.localPalette {
|
||||||
|
out = append(out, x[0], x[1], x[2])
|
||||||
|
}
|
||||||
return stateGif, nil
|
return stateGif, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
cmdFLTWH = "frameLeftTopWidthHeight "
|
cmdFLTWH = "frameLeftTopWidthHeight "
|
||||||
|
cmdP = "palette {"
|
||||||
)
|
)
|
||||||
switch {
|
switch {
|
||||||
case strings.HasPrefix(line, cmdFLTWH):
|
case strings.HasPrefix(line, cmdFLTWH):
|
||||||
@@ -684,6 +696,9 @@ func stateGifFrame(line string) (stateFunc, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case strings.HasPrefix(line, cmdP):
|
||||||
|
return stateGifFramePalette, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("bad stateGifFrame command: %q", line)
|
return nil, fmt.Errorf("bad stateGifFrame command: %q", line)
|
||||||
@@ -708,3 +723,23 @@ func stateGifImagePalette(line string) (stateFunc, error) {
|
|||||||
|
|
||||||
return nil, fmt.Errorf("bad stateGifImagePalette command: %q", line)
|
return nil, fmt.Errorf("bad stateGifImagePalette command: %q", line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func stateGifFramePalette(line string) (stateFunc, error) {
|
||||||
|
g := &gifGlobals
|
||||||
|
if line == "}" {
|
||||||
|
return stateGifFrame, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
s := line
|
||||||
|
if rgb0, s, ok := parseHex(s); ok {
|
||||||
|
if rgb1, s, ok := parseHex(s); ok {
|
||||||
|
if rgb2, _, ok := parseHex(s); ok {
|
||||||
|
g.localPalette = append(g.localPalette,
|
||||||
|
[4]uint8{uint8(rgb0), uint8(rgb1), uint8(rgb2), 0xFF})
|
||||||
|
return stateGifFramePalette, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("bad stateGifFramePalette command: %q", line)
|
||||||
|
}
|
||||||
+17
-11
@@ -19,6 +19,7 @@ pub status "#bad extension label"
|
|||||||
pub status "#bad graphic control"
|
pub status "#bad graphic control"
|
||||||
pub status "#bad header"
|
pub status "#bad header"
|
||||||
pub status "#bad literal width"
|
pub status "#bad literal width"
|
||||||
|
pub status "#bad palette"
|
||||||
|
|
||||||
pri status "#internal error: inconsistent ri/wi"
|
pri status "#internal error: inconsistent ri/wi"
|
||||||
|
|
||||||
@@ -135,7 +136,8 @@ pub struct decoder?(
|
|||||||
restarted base.bool,
|
restarted base.bool,
|
||||||
previous_lzw_decode_ended_abruptly base.bool,
|
previous_lzw_decode_ended_abruptly base.bool,
|
||||||
|
|
||||||
which_palette base.u8[..1],
|
has_global_palette base.bool,
|
||||||
|
which_palette base.u8[..1],
|
||||||
|
|
||||||
// interlace indexes the interlace_start and interlace_delta arrays.
|
// interlace indexes the interlace_start and interlace_delta arrays.
|
||||||
interlace base.u8[..4],
|
interlace base.u8[..4],
|
||||||
@@ -482,9 +484,10 @@ pri func decoder.decode_lsd?(src base.io_reader) {
|
|||||||
args.src.skip?(n:2)
|
args.src.skip?(n:2)
|
||||||
|
|
||||||
// Read the optional Global Color Table.
|
// Read the optional Global Color Table.
|
||||||
if (flags & 0x80) <> 0 {
|
i = 0
|
||||||
|
this.has_global_palette = (flags & 0x80) <> 0
|
||||||
|
if this.has_global_palette {
|
||||||
num_palette_entries = (1 as base.u32) << (1 + (flags & 0x07))
|
num_palette_entries = (1 as base.u32) << (1 + (flags & 0x07))
|
||||||
i = 0
|
|
||||||
while i < num_palette_entries {
|
while i < num_palette_entries {
|
||||||
assert i < 256 via "a < b: a < c; c <= b"(c:num_palette_entries)
|
assert i < 256 via "a < b: a < c; c <= b"(c:num_palette_entries)
|
||||||
// Convert from RGB (in memory order) to ARGB (in native u32 order)
|
// Convert from RGB (in memory order) to ARGB (in native u32 order)
|
||||||
@@ -497,14 +500,15 @@ pri func decoder.decode_lsd?(src base.io_reader) {
|
|||||||
this.palettes[0][(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
this.palettes[0][(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
// Set the remaining palette entries to opaque black.
|
}
|
||||||
while i < 256 {
|
|
||||||
this.palettes[0][(4 * i) + 0] = 0x00
|
// Set the remaining palette entries to opaque black.
|
||||||
this.palettes[0][(4 * i) + 1] = 0x00
|
while i < 256 {
|
||||||
this.palettes[0][(4 * i) + 2] = 0x00
|
this.palettes[0][(4 * i) + 0] = 0x00
|
||||||
this.palettes[0][(4 * i) + 3] = 0xFF
|
this.palettes[0][(4 * i) + 1] = 0x00
|
||||||
i += 1
|
this.palettes[0][(4 * i) + 2] = 0x00
|
||||||
}
|
this.palettes[0][(4 * i) + 3] = 0xFF
|
||||||
|
i += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -792,6 +796,8 @@ pri func decoder.decode_id_part1?(dst ptr base.pixel_buffer, src base.io_reader)
|
|||||||
this.palettes[1][(4 * i) + 3] = 0xFF
|
this.palettes[1][(4 * i) + 3] = 0xFF
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
|
} else if this.quirk_enabled_reject_empty_palette and (not this.has_global_palette) {
|
||||||
|
return "#bad palette"
|
||||||
} else if this.gc_has_transparent_index {
|
} else if this.gc_has_transparent_index {
|
||||||
this.palettes[1][:].copy_from_slice!(s:this.palettes[0][:])
|
this.palettes[1][:].copy_from_slice!(s:this.palettes[0][:])
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -720,6 +720,79 @@ const char* test_wuffs_gif_decode_animated_small() {
|
|||||||
want_frame_config_bounds);
|
want_frame_config_bounds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char* test_wuffs_gif_decode_empty_palette() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
|
||||||
|
.data = global_src_slice,
|
||||||
|
});
|
||||||
|
const char* status =
|
||||||
|
read_file(&src, "test/data/artificial/gif-empty-palette.gif");
|
||||||
|
if (status) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
int q;
|
||||||
|
for (q = 0; q < 2; q++) {
|
||||||
|
src.meta.ri = 0;
|
||||||
|
|
||||||
|
wuffs_gif__decoder dec;
|
||||||
|
status = wuffs_gif__decoder__initialize(
|
||||||
|
&dec, sizeof dec, WUFFS_VERSION,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED);
|
||||||
|
if (status) {
|
||||||
|
RETURN_FAIL("q=%d: initialize: \"%s\"", q, status);
|
||||||
|
}
|
||||||
|
wuffs_gif__decoder__set_quirk_enabled(
|
||||||
|
&dec, wuffs_gif__quirk_reject_empty_palette, q);
|
||||||
|
|
||||||
|
wuffs_base__image_config ic = ((wuffs_base__image_config){});
|
||||||
|
status = wuffs_gif__decoder__decode_image_config(
|
||||||
|
&dec, &ic, wuffs_base__io_buffer__reader(&src));
|
||||||
|
if (status) {
|
||||||
|
RETURN_FAIL("q=%d: decode_image_config: \"%s\"", q, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){});
|
||||||
|
status = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg,
|
||||||
|
global_pixel_slice);
|
||||||
|
if (status) {
|
||||||
|
RETURN_FAIL("q=%d: set_from_slice: \"%s\"", q, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 2; i++) {
|
||||||
|
status = wuffs_gif__decoder__decode_frame(
|
||||||
|
&dec, &pb, wuffs_base__io_buffer__reader(&src), global_work_slice,
|
||||||
|
NULL);
|
||||||
|
if ((q == 1) && (i == 1)) {
|
||||||
|
if (status != wuffs_gif__error__bad_palette) {
|
||||||
|
RETURN_FAIL("q=%d: i=%d: decode_frame: got \"%s\", want \"%s\"", q, i,
|
||||||
|
status, wuffs_gif__error__bad_palette);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (status) {
|
||||||
|
RETURN_FAIL("q=%d: i=%d: decode_frame: \"%s\"", q, i, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
wuffs_base__slice_u8 pal = wuffs_base__pixel_buffer__palette(&pb);
|
||||||
|
if (pal.len < 4) {
|
||||||
|
RETURN_FAIL("q=%d: i=%d: palette length: got %d, want >= 4", q, i,
|
||||||
|
(int)(pal.len));
|
||||||
|
}
|
||||||
|
uint8_t* p = pal.ptr;
|
||||||
|
int blue = i ? 0x00 : 0xFF;
|
||||||
|
if ((p[0] != blue) || (p[1] != 0x00) || (p[2] != 0x00) ||
|
||||||
|
(p[3] != 0xFF)) {
|
||||||
|
RETURN_FAIL(
|
||||||
|
"q=%d: i=%d: palette[0] BGRA: got (0x%02X, 0x%02X, 0x%02X, "
|
||||||
|
"0x%02X), want (0x%02X, 0x00, 0x00, 0xFF)",
|
||||||
|
q, i, (int)(p[0]), (int)(p[1]), (int)(p[2]), (int)(p[3]), blue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
const char* test_wuffs_gif_decode_first_frame_is_opaque() {
|
const char* test_wuffs_gif_decode_first_frame_is_opaque() {
|
||||||
CHECK_FOCUS(__func__);
|
CHECK_FOCUS(__func__);
|
||||||
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
|
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
|
||||||
@@ -1921,6 +1994,7 @@ proc tests[] = {
|
|||||||
test_wuffs_gif_decode_animated_medium, //
|
test_wuffs_gif_decode_animated_medium, //
|
||||||
test_wuffs_gif_decode_animated_small, //
|
test_wuffs_gif_decode_animated_small, //
|
||||||
test_wuffs_gif_decode_bgra_nonpremul, //
|
test_wuffs_gif_decode_bgra_nonpremul, //
|
||||||
|
test_wuffs_gif_decode_empty_palette, //
|
||||||
test_wuffs_gif_decode_first_frame_is_opaque, //
|
test_wuffs_gif_decode_first_frame_is_opaque, //
|
||||||
test_wuffs_gif_decode_frame_out_of_bounds, //
|
test_wuffs_gif_decode_frame_out_of_bounds, //
|
||||||
test_wuffs_gif_decode_input_is_a_gif_just_one_read, //
|
test_wuffs_gif_decode_input_is_a_gif_just_one_read, //
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 54 B |
@@ -0,0 +1,33 @@
|
|||||||
|
# Feed this file to script/make-artificial.go
|
||||||
|
|
||||||
|
# This GIF image has two frames. There is no global palette, and only the first
|
||||||
|
# frame has a local palette.
|
||||||
|
#
|
||||||
|
# Wuffs implicitly gives the second frame an all-opaque-black palette by
|
||||||
|
# default. Wuffs can be configured to reject it instead, by enabling a 'quirk'.
|
||||||
|
|
||||||
|
make gif
|
||||||
|
|
||||||
|
header
|
||||||
|
|
||||||
|
image {
|
||||||
|
imageWidthHeight 1 1
|
||||||
|
}
|
||||||
|
|
||||||
|
frame {
|
||||||
|
frameLeftTopWidthHeight 0 0 1 1
|
||||||
|
palette {
|
||||||
|
0x00 0x00 0xFF
|
||||||
|
0x11 0x00 0xFF
|
||||||
|
0x22 0x00 0xFF
|
||||||
|
0x33 0x00 0xFF
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lzw 2 0x00
|
||||||
|
|
||||||
|
frame {
|
||||||
|
frameLeftTopWidthHeight 0 0 1 1
|
||||||
|
}
|
||||||
|
lzw 2 0x00
|
||||||
|
|
||||||
|
trailer
|
||||||
Reference in new issue
Block a user