mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 08:47:56 +01:00
Rename u32, io_reader, etc to base.u32, etc
This commit is contained in:
1 parent
fee97df698
commit
f95239f6e0
24 files changed
+394
-392
No files matched your search
+56
-56
@@ -22,45 +22,45 @@ pub error "bad LZW literal width"
|
||||
pri error "internal error: inconsistent limited read"
|
||||
|
||||
pub struct decoder?(
|
||||
width u32[..65535],
|
||||
height u32[..65535],
|
||||
width base.u32[..65535],
|
||||
height base.u32[..65535],
|
||||
|
||||
// Call sequence state transitions:
|
||||
// - 0 -> 1: via decode_config.
|
||||
// - 1 -> 1: via decode_frame.
|
||||
call_sequence u8,
|
||||
call_sequence base.u8,
|
||||
|
||||
background_color_index u8,
|
||||
background_color_index base.u8,
|
||||
|
||||
block_type u8,
|
||||
peek_block_type bool,
|
||||
block_type base.u8,
|
||||
peek_block_type base.bool,
|
||||
|
||||
have_gct bool,
|
||||
have_lct bool,
|
||||
have_gct base.bool,
|
||||
have_lct base.bool,
|
||||
|
||||
interlace bool,
|
||||
interlace base.bool,
|
||||
|
||||
// Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of
|
||||
// animation loops is 1.
|
||||
seen_num_loops bool,
|
||||
num_loops u32 = 1,
|
||||
seen_num_loops base.bool,
|
||||
num_loops base.u32 = 1,
|
||||
|
||||
frame_top u32[..65535],
|
||||
frame_left u32[..65535],
|
||||
frame_width u32[..65535],
|
||||
frame_height u32[..65535],
|
||||
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] u8,
|
||||
lct array[3 * 256] u8,
|
||||
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 image_config, src io_reader)() {
|
||||
pub func decoder.decode_config?(dst ptr base.image_config, src base.io_reader)() {
|
||||
if this.call_sequence >= 1 {
|
||||
return error "invalid call sequence"
|
||||
}
|
||||
@@ -85,7 +85,7 @@ pub func decoder.decode_config?(dst ptr image_config, src io_reader)() {
|
||||
this.call_sequence = 1
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame?(dst io_writer, src io_reader)() {
|
||||
pub func decoder.decode_frame?(dst base.io_writer, src base.io_reader)() {
|
||||
if this.call_sequence < 1 {
|
||||
return error "invalid call sequence"
|
||||
}
|
||||
@@ -121,9 +121,9 @@ pub func decoder.decode_frame?(dst io_writer, src io_reader)() {
|
||||
// decode_header reads either "GIF87a" or "GIF89a".
|
||||
//
|
||||
// See the spec section 17 "Header" on page 7.
|
||||
pri func decoder.decode_header?(src io_reader)() {
|
||||
var c array[6] u8
|
||||
var i u32
|
||||
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
|
||||
@@ -137,22 +137,22 @@ pri func decoder.decode_header?(src io_reader)() {
|
||||
// decode_lsd reads the Logical Screen Descriptor.
|
||||
//
|
||||
// See the spec section 18 "Logical Screen Descriptor" on page 8.
|
||||
pri func decoder.decode_lsd?(src io_reader)() {
|
||||
var c array[7] u8
|
||||
var i u32
|
||||
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 u32) | ((c[1] as u32) << 8)
|
||||
this.height = (c[2] as u32) | ((c[3] as u32) << 8)
|
||||
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 u32[..256] = (1 as u32) << (1 + (c[4] & 0x07))
|
||||
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)
|
||||
@@ -172,8 +172,8 @@ pri func decoder.decode_lsd?(src io_reader)() {
|
||||
// - 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 io_reader)() {
|
||||
var label u8 = in.src.read_u8?()
|
||||
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".
|
||||
@@ -185,33 +185,33 @@ pri func decoder.decode_extension?(src io_reader)() {
|
||||
this.skip_blocks?(src:in.src)
|
||||
}
|
||||
|
||||
pri func decoder.skip_blocks?(src io_reader)() {
|
||||
pri func decoder.skip_blocks?(src base.io_reader)() {
|
||||
while true {
|
||||
var block_size u8 = in.src.read_u8?()
|
||||
var block_size base.u8 = in.src.read_u8?()
|
||||
if block_size == 0 {
|
||||
return
|
||||
}
|
||||
in.src.skip32?(n:block_size as u32)
|
||||
in.src.skip32?(n:block_size as base.u32)
|
||||
}
|
||||
}
|
||||
|
||||
// animexts1dot0 is "ANIMEXTS1.0" as bytes.
|
||||
pri const animexts1dot0 array[11] u8 = $(
|
||||
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] u8 = $(
|
||||
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 io_reader)() {
|
||||
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 u8
|
||||
var block_size u8 = in.src.read_u8?()
|
||||
var c base.u8
|
||||
var block_size base.u8 = in.src.read_u8?()
|
||||
if block_size == 0 {
|
||||
return
|
||||
}
|
||||
@@ -221,11 +221,11 @@ pri func decoder.decode_ae?(src io_reader)() {
|
||||
// - 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 u32)
|
||||
in.src.skip32?(n:block_size as base.u32)
|
||||
break
|
||||
}
|
||||
var not_animexts bool
|
||||
var not_netscape bool
|
||||
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?()
|
||||
@@ -241,7 +241,7 @@ pri func decoder.decode_ae?(src io_reader)() {
|
||||
// count.
|
||||
block_size = in.src.read_u8?()
|
||||
if block_size != 3 {
|
||||
in.src.skip32?(n:block_size as u32)
|
||||
in.src.skip32?(n:block_size as base.u32)
|
||||
break
|
||||
}
|
||||
c = in.src.read_u8?()
|
||||
@@ -249,7 +249,7 @@ pri func decoder.decode_ae?(src io_reader)() {
|
||||
in.src.skip32?(n:2)
|
||||
break
|
||||
}
|
||||
this.num_loops = in.src.read_u16le?() as u32
|
||||
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
|
||||
@@ -275,22 +275,22 @@ pri func decoder.decode_ae?(src io_reader)() {
|
||||
// been read.
|
||||
//
|
||||
// See the spec section 20 "Image Descriptor" on page 11.
|
||||
pri func decoder.decode_id?(dst io_writer, src io_reader)() {
|
||||
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 u32
|
||||
this.frame_top = in.src.read_u16le?() as u32
|
||||
this.frame_width = in.src.read_u16le?() as u32
|
||||
this.frame_height = in.src.read_u16le?() as u32
|
||||
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 u8 = in.src.read_u8?()
|
||||
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 u32[..256] = (1 as u32) << (1 + (flags & 0x07))
|
||||
var i u32
|
||||
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?()
|
||||
@@ -300,19 +300,19 @@ pri func decoder.decode_id?(dst io_writer, src io_reader)() {
|
||||
}
|
||||
}
|
||||
|
||||
var lw u8 = in.src.read_u8?()
|
||||
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 u32)
|
||||
this.lzw.set_literal_width!(lw:lw as base.u32)
|
||||
|
||||
while true {
|
||||
var block_size u64 = in.src.read_u8?() as u64
|
||||
var block_size base.u64 = in.src.read_u8?() as base.u64
|
||||
if block_size == 0 {
|
||||
break
|
||||
}
|
||||
while true {
|
||||
var r io_reader = in.src
|
||||
var r base.io_reader = in.src
|
||||
// TODO: should "mark" be "set_mark"? Unlike "limit", "mark" does
|
||||
// not return a different io_reader.
|
||||
r.mark()
|
||||
@@ -321,7 +321,7 @@ pri func decoder.decode_id?(dst io_writer, src io_reader)() {
|
||||
// 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)
|
||||
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
|
||||
}
|
||||
|
||||
+29
-29
@@ -18,33 +18,33 @@ pub error "LZW code is out of range"
|
||||
pub error "LZW prefix chain is cyclical"
|
||||
|
||||
pri struct lzw_decoder?(
|
||||
literal_width u32[2..8] = 8,
|
||||
stack array[4096] u8,
|
||||
suffixes array[4096] u8,
|
||||
prefixes array[4096] u16[..4095],
|
||||
literal_width base.u32[2..8] = 8,
|
||||
stack array[4096] base.u8,
|
||||
suffixes array[4096] base.u8,
|
||||
prefixes array[4096] base.u16[..4095],
|
||||
)
|
||||
|
||||
pri func lzw_decoder.set_literal_width!(lw u32[2..8] = 8)() {
|
||||
pri func lzw_decoder.set_literal_width!(lw base.u32[2..8] = 8)() {
|
||||
this.literal_width = in.lw
|
||||
}
|
||||
|
||||
pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
|
||||
pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() {
|
||||
// These variables don't change over the lifetime of this func.
|
||||
var clear_code u32[4..256] = (1 as u32) << this.literal_width
|
||||
var end_code u32[5..257] = clear_code + 1
|
||||
var clear_code base.u32[4..256] = (1 as base.u32) << this.literal_width
|
||||
var end_code base.u32[5..257] = clear_code + 1
|
||||
|
||||
// These variables do change.
|
||||
//
|
||||
// save_code is the code for which, after decoding a code, we save what the
|
||||
// next back-reference expands to. The README.md file also calls this value
|
||||
// `max`. 4096 means do not save.
|
||||
var save_code u32[..4096] = end_code
|
||||
var prev_code u32[..4095]
|
||||
var width u32[..12] = this.literal_width + 1
|
||||
var save_code base.u32[..4096] = end_code
|
||||
var prev_code base.u32[..4095]
|
||||
var width base.u32[..12] = this.literal_width + 1
|
||||
|
||||
// These variables yield src's bits in Least Significant Bits order.
|
||||
var bits u32
|
||||
var n_bits u32
|
||||
var bits base.u32
|
||||
var n_bits base.u32
|
||||
|
||||
while true,
|
||||
pre n_bits < 8,
|
||||
@@ -55,19 +55,19 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
|
||||
post n_bits >= width,
|
||||
{
|
||||
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
var code u32[..4095] = bits.low_bits(n:width)
|
||||
var code base.u32[..4095] = bits.low_bits(n:width)
|
||||
bits >>= width
|
||||
n_bits -= width
|
||||
|
||||
if code < clear_code {
|
||||
assert code < 256 via "a < b: a < c; c <= b"(c:clear_code)
|
||||
in.dst.write_u8?(x:code as u8)
|
||||
in.dst.write_u8?(x:code as base.u8)
|
||||
if save_code <= 4095 {
|
||||
this.suffixes[save_code] = code as u8
|
||||
this.prefixes[save_code] = prev_code as u16
|
||||
this.suffixes[save_code] = code as base.u8
|
||||
this.prefixes[save_code] = prev_code as base.u16
|
||||
}
|
||||
|
||||
} else if code == clear_code {
|
||||
@@ -80,8 +80,8 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
|
||||
return
|
||||
|
||||
} else if code <= save_code {
|
||||
var s u32[..4095] = 4095
|
||||
var c u32[..4095] = code
|
||||
var s base.u32[..4095] = 4095
|
||||
var c base.u32[..4095] = code
|
||||
|
||||
if code == save_code {
|
||||
s -= 1
|
||||
@@ -97,31 +97,31 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
|
||||
return error "LZW prefix chain is cyclical"
|
||||
}
|
||||
s -= 1
|
||||
c = this.prefixes[c] as u32
|
||||
c = this.prefixes[c] as base.u32
|
||||
}
|
||||
this.stack[s] = c as u8
|
||||
this.stack[s] = c as base.u8
|
||||
|
||||
if code == save_code {
|
||||
this.stack[4095] = c as u8
|
||||
this.stack[4095] = c as base.u8
|
||||
}
|
||||
|
||||
while true,
|
||||
inv n_bits < 8,
|
||||
inv c < 256,
|
||||
{
|
||||
var expansion slice u8 = this.stack[s:]
|
||||
var n_copied u64 = in.dst.copy_from_slice(s:expansion)
|
||||
var expansion slice base.u8 = this.stack[s:]
|
||||
var n_copied base.u64 = in.dst.copy_from_slice(s:expansion)
|
||||
if n_copied == expansion.length() {
|
||||
break
|
||||
}
|
||||
s = (s + ((n_copied & 4095) as u32)) & 4095
|
||||
s = (s + ((n_copied & 4095) as base.u32)) & 4095
|
||||
// TODO: "closed for writes" instead?
|
||||
yield suspension "short write"
|
||||
}
|
||||
|
||||
if save_code <= 4095 {
|
||||
this.suffixes[save_code] = c as u8
|
||||
this.prefixes[save_code] = prev_code as u16
|
||||
this.suffixes[save_code] = c as base.u8
|
||||
this.prefixes[save_code] = prev_code as base.u16
|
||||
}
|
||||
|
||||
} else {
|
||||
@@ -130,7 +130,7 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
|
||||
|
||||
if save_code <= 4095 {
|
||||
save_code += 1
|
||||
if (save_code == ((1 as u32) << width)) and (width < 12) {
|
||||
if (save_code == ((1 as base.u32) << width)) and (width < 12) {
|
||||
width += 1
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user