Decode a GIF's Global Color Table.

This commit is contained in:
Nigel Tao committed 2017-06-18 11:11:10 +10:00
1 parent dc71a93dcf
commit d16a25806f
6 files changed
+114 -23

No files matched your search

+21 -2
View File
@@ -8,6 +8,11 @@ pub error "bad GIF header"
pub struct decoder?(
width u32[..65535],
height u32[..65535],
background_color_index u8,
// gct is the Global Color Table: 256 (R, G, B) entries.
//
// TODO: 4 byte per pixel RGBA or BGRA instead of 3 bpp RGB?
gct[3 * 256] u8,
lzw lzw_decoder,
)
@@ -21,7 +26,7 @@ pub func decoder.decode?(dst ptr buf1, src ptr buf1)() {
// See the spec section 17 "Header" on page 7.
pri func decoder.decode_header?(src ptr buf1)() {
var c[6] u8
var i u8
var i u32
while i < 6 {
c[i] = in.src.read_u8?()
i += 1
@@ -37,11 +42,25 @@ pri func decoder.decode_header?(src ptr buf1)() {
// See the spec section 18 "Logical Screen Descriptor" on page 8.
pri func decoder.decode_lsd?(src ptr buf1)() {
var c[7] u8
var i u8
var i u32
while i < 7 {
c[i] = in.src.read_u8?()
i += 1
}
this.width = (c[0] as u32) | ((c[1] as u32) << 8)
this.height = (c[2] as u32) | ((c[3] as u32) << 8)
this.background_color_index = c[5]
// Read the optional Global Color Table.
if (c[4] & 0x80) != 0 {
var gct_size u32[..256] = (1 as 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?()
i += 1
}
}
}