Files
wuffs/std/gif/decode_gif.wuffs
T

316 lines
8.7 KiB
Plaintext

// Copyright 2017 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
packageid "gif "
pub error "bad GIF block"
pub error "bad GIF extension label"
pub error "bad GIF header"
pub error "bad LZW literal width"
pri error "internal error: inconsistent limited read"
pub struct decoder?(
width u32[..65535],
height u32[..65535],
// Call sequence state transitions:
// - 0 -> 1: via decode_config.
// - 1 -> 1: via decode_frame.
call_sequence u8,
background_color_index u8,
block_type u8,
peek_block_type bool,
// Absent a NETSCAPE2.0 extension, the implicit number of animation loops
// is 1.
seen_num_loops bool,
num_loops u32 = 1,
// 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[3 * 256] u8,
lct[3 * 256] u8,
lzw lzw_decoder,
)
// TODO: should dst be an nptr instead of a ptr?
pub func decoder.decode_config?(dst ptr image_config, src reader1)() {
if this.call_sequence >= 1 {
return error "invalid call sequence"
}
this.decode_header?(src:in.src)
this.decode_lsd?(src:in.src)
while true {
this.peek_block_type = true
this.block_type = in.src.read_u8?()
if this.seen_num_loops or (this.block_type != 0x21) { // The spec calls 0x21 the "Extension Introducer".
break
}
this.decode_extension?(src:in.src)
}
// TODO: rename initialize to set?
in.dst.initialize!(width:this.width, height:this.height, color_model:0)
this.call_sequence = 1
}
pub func decoder.decode_frame?(dst writer1, src reader1)() {
if this.call_sequence < 1 {
return error "invalid call sequence"
}
while true {
if this.peek_block_type {
this.peek_block_type = false
} else {
this.block_type = in.src.read_u8?()
}
if this.block_type == 0x21 { // The spec calls 0x21 the "Extension Introducer".
this.decode_extension?(src:in.src)
} else if this.block_type == 0x2C { // The spec calls 0x2C the "Image Separator".
// TODO: animated GIFs can have multiple Image Descriptors. What's
// the API? Should the 0x3B Trailer byte result in a "no more
// frames" suspension instead of ok? Do we need additional
// destination arguments for timing and palette?
this.decode_id?(dst:in.dst, src:in.src)
return
} else if this.block_type == 0x3B { // The spec calls 0x3B the "Trailer".
break
} else {
return error "bad GIF block"
}
}
while true {
yield suspension "end of data"
}
}
// decode_header reads either "GIF87a" or "GIF89a".
//
// See the spec section 17 "Header" on page 7.
pri func decoder.decode_header?(src reader1)() {
var c[6] u8
var i u32
while i < 6 {
c[i] = in.src.read_u8?()
i += 1
}
if (c[0] != 0x47) or (c[1] != 0x49) or (c[2] != 0x46) or (c[3] != 0x38) or
((c[4] != 0x37) and (c[4] != 0x39)) or (c[5] != 0x61) {
return error "bad GIF header"
}
}
// decode_lsd reads the Logical Screen Descriptor.
//
// See the spec section 18 "Logical Screen Descriptor" on page 8.
pri func decoder.decode_lsd?(src reader1)() {
var c[7] 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
}
}
}
// decode_extension reads an extension. The Extension Introducer byte has
// already been read.
//
// See the spec:
// - section 23 "Graphic Control Extension" on page 15.
// - section 24 "Comment Extension" on page 17.
// - section 25 "Plain Text Extension" on page 18.
// - section 26 "Application Extension" on page 21.
pri func decoder.decode_extension?(src reader1)() {
var label u8 = in.src.read_u8?()
if label == 0xF9 { // The spec calls 0xF9 the "Graphic Control Label".
// TODO.
} else if label == 0xFF { // The spec calls 0xFF the "Application Extension Label".
this.decode_ae?(src:in.src)
return
}
// We skip over all other extensions, including 0x01 "Plain Text Label" and
// 0xFE "Comment Label".
this.skip_blocks?(src:in.src)
}
pri func decoder.skip_blocks?(src reader1)() {
while true {
var block_size u8 = in.src.read_u8?()
if block_size == 0 {
return
}
in.src.skip32?(n:block_size as u32)
}
}
// netscape2dot0 is "NETSCAPE2.0" as bytes.
pri const netscape2dot0[11] u8 = $(
0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30,
)
// decode_ae reads an Application Extension.
pri func decoder.decode_ae?(src reader1)() {
// This "while true" always executes exactly once, as it ends with a
// "break", but using "break"s throughout simplifies the control flow.
while:loop true {
var c u8
var block_size u8 = in.src.read_u8?()
if block_size == 0 {
return
}
// Look only for an 11 byte "NETSCAPE2.0" extension, as per
// http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
if block_size != 11 {
in.src.skip32?(n:block_size as u32)
break
}
block_size = 0 // Re-purpose the block_size variable as a counter.
while block_size < 11 {
c = in.src.read_u8?()
if c != netscape2dot0[block_size] {
in.src.skip32?(n:(10 - block_size) as u32)
break:loop
}
block_size += 1
}
// Those 11 bytes should be followed by 0x03, 0x01 and then the loop
// count.
block_size = in.src.read_u8?()
if block_size != 3 {
in.src.skip32?(n:block_size as u32)
break
}
c = in.src.read_u8?()
if c != 0x01 {
in.src.skip32?(n:2)
break
}
this.num_loops = in.src.read_u16le?() as u32
this.seen_num_loops = true
// A loop count of N, in the wire format, actually means "repeat N
// times after the first play", if N is positive. A zero N means to
// loop forever. Playing the frames exactly once is denoted by the
// *absence* of this NETSCAPE2.0 application extension.
//
// For example, if there are four frames: A, B, C, D, and N is 2, then
// each frame is actually played N+1 or 3 times: ABCDABCDABCD.
//
// Thus, we increment N if it is positive. The comparison against
// 0xFFFF will never fail, but is necessary for the overflow checker.
if (0 < this.num_loops) and (this.num_loops <= 0xFFFF) {
this.num_loops += 1
}
break
}
this.skip_blocks?(src:in.src)
}
// decode_id reads an Image Descriptor. The Image Separator byte has already
// been read.
//
// See the spec section 20 "Image Descriptor" on page 11.
pri func decoder.decode_id?(dst writer1, src reader1)() {
var c[9] u8
var i u32
while i < 9 {
c[i] = in.src.read_u8?()
i += 1
}
// TODO: use image left/top/width/height.
// TODO: use interlace.
var interlace bool = (c[8] & 0x40) != 0
if interlace { // Avoid "unused variable" C compiler warning.
}
// Read the optional Local Color Table.
if (c[8] & 0x80) != 0 {
var lct_size u32[..256] = (1 as u32) << (1 + (c[8] & 0x07))
i = 0
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?()
i += 1
}
}
var lw u8 = in.src.read_u8?()
if (lw < 2) or (8 < lw) {
return error "bad LZW literal width"
}
this.lzw.set_literal_width!(lw:lw as u32)
while true {
var block_size u64 = in.src.read_u8?() as u64
if block_size == 0 {
break
}
while true {
var r reader1 = in.src
// TODO: should "mark" be "set_mark"? Unlike "limit", "mark" does
// not return a different reader1.
r.mark()
// TODO: remove the dummy param. It's needed for now so that
// writeSaveExprDerivedVars updates e.g. the b_rptr_src derived
// variables.
//
// TODO: enforce that limit can only be called in a "foo?" call?
var z status = try this.lzw.decode?(dst:in.dst, src:r.limit(l:block_size), dummy:in.src)
if z.is_ok() {
break
}
if block_size < r.since_mark().length() {
return error "internal error: inconsistent limited read"
}
block_size -= r.since_mark().length()
if (block_size == 0) and (z == suspension "short read") {
break
}
yield z
}
}
}