Files
wuffs/std/gif/decode_gif.wuffs
T
2018-04-28 13:56:37 +10:00

338 lines
9.9 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"
pub struct decoder?(
width base.u32[..65535],
height base.u32[..65535],
// Call sequence state transitions:
// - 0 -> 1: via decode_config.
// - 1 -> 1: via decode_frame.
call_sequence base.u8,
background_color_index base.u8,
block_type base.u8,
peek_block_type base.bool,
have_gct base.bool,
have_lct base.bool,
interlace base.bool,
// Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of
// animation loops is 1.
seen_num_loops base.bool,
num_loops base.u32 = 1,
frame_top base.u32[..65535],
frame_left base.u32[..65535],
frame_width base.u32[..65535],
frame_height base.u32[..65535],
// 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,
lzw lzw_decoder,
)
// TODO: should dst be an nptr instead of a ptr?
pub func decoder.decode_config?(dst ptr base.image_config, src base.io_reader)() {
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?
//
// TODO: a Wuffs (not just C) name for the
// WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_INDEXED magic pixfmt constant.
in.dst.initialize!(pixfmt:0x22088888, pixsub:0,
width:this.width, height:this.height, num_loops:this.num_loops)
this.call_sequence = 1
}
pub func decoder.decode_frame?(dst base.io_writer, src base.io_reader)() {
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 base.io_reader)() {
var c array[6] base.u8
var i base.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 base.io_reader)() {
var c array[7] base.u8
var i base.u32
while i < 7 {
c[i] = in.src.read_u8?()
i += 1
}
// TODO: use read_u16le.
this.width = (c[0] as base.u32) | ((c[1] as base.u32) << 8)
this.height = (c[2] as base.u32) | ((c[3] as base.u32) << 8)
this.background_color_index = c[5]
// 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))
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 base.io_reader)() {
var label base.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 base.io_reader)() {
while true {
var block_size base.u8 = in.src.read_u8?()
if block_size == 0 {
return
}
in.src.skip32?(n:block_size as base.u32)
}
}
// animexts1dot0 is "ANIMEXTS1.0" as bytes.
pri const animexts1dot0 array[11] base.u8 = $(
0x41, 0x4E, 0x49, 0x4D, 0x45, 0x58, 0x54, 0x53, 0x31, 0x2E, 0x30,
)
// netscape2dot0 is "NETSCAPE2.0" as bytes.
pri const netscape2dot0 array[11] base.u8 = $(
0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30,
)
// decode_ae reads an Application Extension.
pri func decoder.decode_ae?(src base.io_reader)() {
// This "while true" always executes exactly once, as it ends with a
// "break", but using "break"s throughout simplifies the control flow.
while true {
var c base.u8
var block_size base.u8 = in.src.read_u8?()
if block_size == 0 {
return
}
// Look only for an 11 byte "ANIMEXTS1.0" or "NETSCAPE2.0" extension,
// as per:
// - http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension
// - http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
if block_size != 11 {
in.src.skip32?(n:block_size as base.u32)
break
}
var not_animexts base.bool
var not_netscape base.bool
block_size = 0 // Re-purpose the block_size variable as a counter.
while block_size < 11 {
c = in.src.read_u8?()
not_animexts = not_animexts or (c != animexts1dot0[block_size])
not_netscape = not_netscape or (c != netscape2dot0[block_size])
block_size += 1
}
if not_animexts and not_netscape {
break
}
// 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 base.u32)
break
}
c = in.src.read_u8?()
if c != 0x01 {
in.src.skip32?(n:2)
break
}
this.num_loops = in.src.read_u16le?() as base.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 base.io_writer, src base.io_reader)() {
// TODO: check that the frame rect is inside the image rect??
this.frame_left = in.src.read_u16le?() as base.u32
this.frame_top = in.src.read_u16le?() as base.u32
this.frame_width = in.src.read_u16le?() as base.u32
this.frame_height = in.src.read_u16le?() as base.u32
// TODO: use this.interlace.
var flags base.u8 = in.src.read_u8?()
this.interlace = (flags & 0x40) != 0
// 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 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?()
i += 1
}
}
var lw base.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 base.u32)
while true {
var block_size base.u64 = in.src.read_u8?() as base.u64
if block_size == 0 {
break
}
while true {
// TODO: should "mark" be "set_mark" or "set_mark_here"? Unlike
// "limit", "mark" does not return a different io_reader.
in.src.mark()
io_bind (in.src) {
in.src.set_limit(l:block_size)
var r base.io_reader = in.src
// 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 base.status = try this.lzw.decode?(dst:in.dst, src:r.limit(l:block_size), dummy:in.src)
}
if z.is_ok() {
break
}
block_size ~sat-= in.src.since_mark().length()
if (block_size == 0) and (z == suspension "short read") {
break
}
yield z
}
}
}