Have std/gif's palettes be BGRA, not RGB

This commit is contained in:
Nigel Tao committed 2018-05-24 10:33:02 +10:00
1 parent 1444e38d8d
commit 34cfc3f863
8 files changed
+60 -53

No files matched your search

+25 -20
View File
@@ -1213,8 +1213,7 @@ typedef struct {
uint32_t f_uncompressed_ri;
uint32_t f_uncompressed_wi;
uint8_t f_uncompressed[4096];
uint8_t f_gct[768];
uint8_t f_lct[768];
uint8_t f_palettes[2][1024];
wuffs_gif__lzw_decoder f_lzw;
struct {
@@ -1236,7 +1235,7 @@ typedef struct {
uint32_t coro_susp_point;
uint8_t v_c[7];
uint32_t v_i;
uint32_t v_gct_size;
uint32_t v_num_palette_entries;
} c_decode_lsd[1];
struct {
uint32_t coro_susp_point;
@@ -1264,7 +1263,7 @@ typedef struct {
struct {
uint32_t coro_susp_point;
uint8_t v_flags;
uint32_t v_lct_size;
uint32_t v_num_palette_entries;
uint32_t v_i;
uint8_t v_lw;
uint64_t v_block_size;
@@ -2321,7 +2320,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_lsd(
uint8_t v_c[7];
uint32_t v_i;
uint32_t v_gct_size;
uint32_t v_num_palette_entries;
uint8_t* ioptr_src = NULL;
uint8_t* iobounds0orig_src = NULL;
@@ -2343,7 +2342,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_lsd(
if (coro_susp_point) {
memcpy(v_c, self->private_impl.c_decode_lsd[0].v_c, sizeof(v_c));
v_i = self->private_impl.c_decode_lsd[0].v_i;
v_gct_size = self->private_impl.c_decode_lsd[0].v_gct_size;
v_num_palette_entries =
self->private_impl.c_decode_lsd[0].v_num_palette_entries;
} else {
}
switch (coro_susp_point) {
@@ -2369,16 +2369,16 @@ static wuffs_gif__status wuffs_gif__decoder__decode_lsd(
self->private_impl.f_background_color_index = v_c[5];
self->private_impl.f_have_gct = ((v_c[4] & 128) != 0);
if (self->private_impl.f_have_gct) {
v_gct_size = (((uint32_t)(1)) << (1 + (v_c[4] & 7)));
v_num_palette_entries = (((uint32_t)(1)) << (1 + (v_c[4] & 7)));
v_i = 0;
while (v_i < v_gct_size) {
while (v_i < v_num_palette_entries) {
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
if (WUFFS_BASE__UNLIKELY(ioptr_src == iobounds1_src)) {
goto short_read_src;
}
uint8_t t_1 = *ioptr_src++;
self->private_impl.f_gct[((3 * v_i) + 0)] = t_1;
self->private_impl.f_palettes[0][((4 * v_i) + 2)] = t_1;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
@@ -2386,7 +2386,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_lsd(
goto short_read_src;
}
uint8_t t_2 = *ioptr_src++;
self->private_impl.f_gct[((3 * v_i) + 1)] = t_2;
self->private_impl.f_palettes[0][((4 * v_i) + 1)] = t_2;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
@@ -2394,8 +2394,9 @@ static wuffs_gif__status wuffs_gif__decoder__decode_lsd(
goto short_read_src;
}
uint8_t t_3 = *ioptr_src++;
self->private_impl.f_gct[((3 * v_i) + 2)] = t_3;
self->private_impl.f_palettes[0][((4 * v_i) + 0)] = t_3;
}
self->private_impl.f_palettes[0][((4 * v_i) + 3)] = 255;
v_i += 1;
}
}
@@ -2411,7 +2412,8 @@ suspend:
self->private_impl.c_decode_lsd[0].coro_susp_point = coro_susp_point;
memcpy(self->private_impl.c_decode_lsd[0].v_c, v_c, sizeof(v_c));
self->private_impl.c_decode_lsd[0].v_i = v_i;
self->private_impl.c_decode_lsd[0].v_gct_size = v_gct_size;
self->private_impl.c_decode_lsd[0].v_num_palette_entries =
v_num_palette_entries;
goto exit;
exit:
@@ -3036,7 +3038,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1(
wuffs_gif__status status = WUFFS_GIF__STATUS_OK;
uint8_t v_flags;
uint32_t v_lct_size;
uint32_t v_num_palette_entries;
uint32_t v_i;
uint8_t v_lw;
uint64_t v_block_size;
@@ -3092,7 +3094,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1(
self->private_impl.c_decode_id_part1[0].coro_susp_point;
if (coro_susp_point) {
v_flags = self->private_impl.c_decode_id_part1[0].v_flags;
v_lct_size = self->private_impl.c_decode_id_part1[0].v_lct_size;
v_num_palette_entries =
self->private_impl.c_decode_id_part1[0].v_num_palette_entries;
v_i = self->private_impl.c_decode_id_part1[0].v_i;
v_lw = self->private_impl.c_decode_id_part1[0].v_lw;
v_block_size = self->private_impl.c_decode_id_part1[0].v_block_size;
@@ -3125,16 +3128,16 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1(
self->private_impl.f_interlace = ((v_flags & 64) != 0);
self->private_impl.f_have_lct = ((v_flags & 128) != 0);
if (self->private_impl.f_have_lct) {
v_lct_size = (((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_lct_size) {
while (v_i < v_num_palette_entries) {
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
if (WUFFS_BASE__UNLIKELY(ioptr_src == iobounds1_src)) {
goto short_read_src;
}
uint8_t t_1 = *ioptr_src++;
self->private_impl.f_lct[((3 * v_i) + 0)] = t_1;
self->private_impl.f_palettes[1][((4 * v_i) + 2)] = t_1;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
@@ -3142,7 +3145,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1(
goto short_read_src;
}
uint8_t t_2 = *ioptr_src++;
self->private_impl.f_lct[((3 * v_i) + 1)] = t_2;
self->private_impl.f_palettes[1][((4 * v_i) + 1)] = t_2;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
@@ -3150,8 +3153,9 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1(
goto short_read_src;
}
uint8_t t_3 = *ioptr_src++;
self->private_impl.f_lct[((3 * v_i) + 2)] = t_3;
self->private_impl.f_palettes[1][((4 * v_i) + 0)] = t_3;
}
self->private_impl.f_palettes[1][((4 * v_i) + 3)] = 255;
v_i += 1;
}
}
@@ -3293,7 +3297,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1(
suspend:
self->private_impl.c_decode_id_part1[0].coro_susp_point = coro_susp_point;
self->private_impl.c_decode_id_part1[0].v_flags = v_flags;
self->private_impl.c_decode_id_part1[0].v_lct_size = v_lct_size;
self->private_impl.c_decode_id_part1[0].v_num_palette_entries =
v_num_palette_entries;
self->private_impl.c_decode_id_part1[0].v_i = v_i;
self->private_impl.c_decode_id_part1[0].v_lw = v_lw;
self->private_impl.c_decode_id_part1[0].v_block_size = v_block_size;
+3 -4
View File
@@ -1213,8 +1213,7 @@ typedef struct {
uint32_t f_uncompressed_ri;
uint32_t f_uncompressed_wi;
uint8_t f_uncompressed[4096];
uint8_t f_gct[768];
uint8_t f_lct[768];
uint8_t f_palettes[2][1024];
wuffs_gif__lzw_decoder f_lzw;
struct {
@@ -1236,7 +1235,7 @@ typedef struct {
uint32_t coro_susp_point;
uint8_t v_c[7];
uint32_t v_i;
uint32_t v_gct_size;
uint32_t v_num_palette_entries;
} c_decode_lsd[1];
struct {
uint32_t coro_susp_point;
@@ -1264,7 +1263,7 @@ typedef struct {
struct {
uint32_t coro_susp_point;
uint8_t v_flags;
uint32_t v_lct_size;
uint32_t v_num_palette_entries;
uint32_t v_i;
uint8_t v_lw;
uint64_t v_block_size;
+9 -7
View File
@@ -17,8 +17,9 @@
package main
// extract-palette-indexes.go extracts the 1-byte-per-pixel indexes and, for
// paletted images, the 3-byte-per-entry RGB palette of a paletted or gray GIF
// and PNG image. It checks that the GIF and PNG encode equivalent images.
// paletted images, the 4-byte-per-entry BGRA (premultiplied alpha) palette of
// a paletted or gray GIF and PNG image. It checks that the GIF and PNG encode
// equivalent images.
//
// Usage: go run extract-palette-indexes.go foo.gif foo.png
@@ -94,13 +95,14 @@ func decode(filename string) (palette []byte, indexes []byte, err error) {
case *image.Gray:
return nil, m.Pix, nil
case *image.Paletted:
palette = make([]byte, 3*256)
palette = make([]byte, 4*256)
allGray := true
for i, c := range m.Palette {
r, g, b, _ := c.RGBA()
palette[3*i+0] = uint8(r >> 8)
palette[3*i+1] = uint8(g >> 8)
palette[3*i+2] = uint8(b >> 8)
r, g, b, a := c.RGBA()
palette[4*i+0] = uint8(b >> 8)
palette[4*i+1] = uint8(g >> 8)
palette[4*i+2] = uint8(r >> 8)
palette[4*i+3] = uint8(a >> 8)
if allGray {
y := uint32(i)
allGray = r>>8 == y && g>>8 == y && b>>8 == y
+18 -17
View File
@@ -56,11 +56,8 @@ pub struct decoder?(
uncompressed_wi base.u32[..4096],
uncompressed array[4096] base.u8,
// gct and lct are the Global / Local Color Tables: 256 (R, G, B) entries.
//
// TODO: 4 byte per pixel RGBA or BGRA instead of 3 bpp RGB?
gct array[3 * 256] base.u8,
lct array[3 * 256] base.u8,
// palettes[0] and palettes[1] are the Global and Local Color Table.
palettes array[2] array[4 * 256] base.u8,
lzw lzw_decoder,
)
@@ -162,13 +159,15 @@ pri func decoder.decode_lsd?(src base.io_reader)() {
// Read the optional Global Color Table.
this.have_gct = (c[4] & 0x80) != 0
if this.have_gct {
var gct_size base.u32[..256] = (1 as base.u32) << (1 + (c[4] & 0x07))
var num_palette_entries base.u32[..256] = (1 as base.u32) << (1 + (c[4] & 0x07))
i = 0
while i < gct_size {
assert i < 256 via "a < b: a < c; c <= b"(c:gct_size)
this.gct[(3 * i) + 0] = in.src.read_u8?()
this.gct[(3 * i) + 1] = in.src.read_u8?()
this.gct[(3 * i) + 2] = in.src.read_u8?()
while i < num_palette_entries {
assert i < 256 via "a < b: a < c; c <= b"(c:num_palette_entries)
// Convert from RGB to BGRA.
this.palettes[0][(4 * i) + 2] = in.src.read_u8?()
this.palettes[0][(4 * i) + 1] = in.src.read_u8?()
this.palettes[0][(4 * i) + 0] = in.src.read_u8?()
this.palettes[0][(4 * i) + 3] = 0xFF
i += 1
}
}
@@ -319,13 +318,15 @@ pri func decoder.decode_id_part1?(ib ptr base.image_buffer, dst base.io_writer,
// Read the optional Local Color Table.
this.have_lct = (flags & 0x80) != 0
if this.have_lct {
var lct_size base.u32[..256] = (1 as base.u32) << (1 + (flags & 0x07))
var num_palette_entries base.u32[..256] = (1 as base.u32) << (1 + (flags & 0x07))
var i base.u32
while i < lct_size {
assert i < 256 via "a < b: a < c; c <= b"(c:lct_size)
this.lct[(3 * i) + 0] = in.src.read_u8?()
this.lct[(3 * i) + 1] = in.src.read_u8?()
this.lct[(3 * i) + 2] = in.src.read_u8?()
while i < num_palette_entries {
assert i < 256 via "a < b: a < c; c <= b"(c:num_palette_entries)
// Convert from RGB to BGRA.
this.palettes[1][(4 * i) + 2] = in.src.read_u8?()
this.palettes[1][(4 * i) + 1] = in.src.read_u8?()
this.palettes[1][(4 * i) + 0] = in.src.read_u8?()
this.palettes[1][(4 * i) + 3] = 0xFF
i += 1
}
}
+4 -4
View File
@@ -496,11 +496,11 @@ bool do_test_wuffs_gif_decode(const char* filename,
}
// TODO: provide a public API for getting the palette.
wuffs_base__io_buffer pal_got = {.ptr = dec.private_impl.f_gct,
.len = 3 * 256};
wuffs_base__io_buffer pal_got = {.ptr = dec.private_impl.f_palettes[0],
.len = 4 * 256};
wuffs_base__io_buffer pal_want = {.ptr = global_palette_buffer,
.len = 3 * 256};
pal_got.wi = 3 * 256;
.len = 4 * 256};
pal_got.wi = 4 * 256;
if (!read_file(&pal_want, palette_filename)) {
return false;
}
+1 -1
View File
@@ -20,7 +20,7 @@
#include <sys/time.h>
#define BUFFER_SIZE (64 * 1024 * 1024)
#define PALETTE_BUFFER_SIZE (4 * 245)
#define PALETTE_BUFFER_SIZE (4 * 256)
#define WUFFS_TESTLIB_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
Binary file not shown.
Binary file not shown.