160 lines
4.6 KiB
Plaintext
160 lines
4.6 KiB
Plaintext
#! Contains JPEG's Huffman tables and the bits of a scan's entropy-coded data:
|
|
#! tables read as stb_image reads them, codes up to 9 bits long by a table and
|
|
#! longer ones by where each length's codes end, and the bits read the first
|
|
#! the most significant, the zero after each `0xFF` taken out.
|
|
|
|
module malunal::image::jpeg;
|
|
|
|
|
|
# How many bits of a code the fast table reads at once.
|
|
const fast_bits: u32 = 9;
|
|
|
|
# A Huffman table, read as stb_image reads them: codes up to 9 bits long by a
|
|
# table, and longer ones by where each length's codes end.
|
|
struct huffman {
|
|
var fast: u8[512]; #! The index of a code by its first 9 bits, or 255 for a longer one.
|
|
var code: u16[256];
|
|
var values: u8[256];
|
|
var size: u8[257];
|
|
var max_code: u32[18]; #! Just past the last code of each length, shifted to be 16 bits long.
|
|
var delta: i32[17]; #! What makes a code of each length its index.
|
|
}
|
|
|
|
# Reads the entropy-coded data of a scan, the first bit the most significant,
|
|
# taking out the zero after each `0xFF`, and stopping at a marker.
|
|
struct bit_reader {
|
|
var data: span<u8>;
|
|
var at: u64;
|
|
var buffer: u32; #! The bits read, from the most significant.
|
|
var count: u32;
|
|
var marker: u8; #! The marker found, which ends the data, or 0.
|
|
}
|
|
|
|
# Builds a table from how many codes there are of each length, and the values of
|
|
# them in order.
|
|
def huffman::build(&mut this, counts: span<u8>, values: span<u8>): bool {
|
|
var index: u64 = 0;
|
|
for length in 0u..16u {
|
|
for _ in 0..counts[length] as u64 {
|
|
this.size[index] = (length + 1) as u8;
|
|
index++;
|
|
}
|
|
}
|
|
this.size[index] = 0;
|
|
|
|
var code: u32 = 0;
|
|
index = 0;
|
|
for length in 1u..17u {
|
|
this.delta[length] = index as i32 - code as i32;
|
|
while this.size[index] as u64 == length {
|
|
this.code[index] = code as u16;
|
|
code++;
|
|
index++;
|
|
}
|
|
if (code as u64) > (1u << length) {
|
|
return false;
|
|
}
|
|
this.max_code[length] = code << (16 - length) as u32;
|
|
code <<= 1;
|
|
}
|
|
this.max_code[17] = 0xFFFFFFFF;
|
|
|
|
for slot in 0u..512u {
|
|
this.fast[slot] = 255;
|
|
}
|
|
for at in 0..index {
|
|
val length := this.size[at] as u32;
|
|
if length <= fast_bits {
|
|
val first := (this.code[at] as u64) << (fast_bits - length) as u64;
|
|
for slot in 0..(1u << (fast_bits - length) as u64) {
|
|
this.fast[first + slot] = at as u8;
|
|
}
|
|
}
|
|
}
|
|
for at in 0..values.length {
|
|
this.values[at] = values[at];
|
|
}
|
|
return true;
|
|
}
|
|
|
|
# Decodes a value of a table.
|
|
# @returns The value, or -1 where the bits are no code's.
|
|
def bit_reader::decode(&mut this, table: &huffman): i32 {
|
|
if this.count < 16 {
|
|
this.fill();
|
|
}
|
|
val at := table.fast[(this.buffer >> (32 - fast_bits)) as u64];
|
|
if at != 255 {
|
|
val length := table.size[at as u64] as u32;
|
|
this.buffer <<= length;
|
|
this.count -= length;
|
|
return table.values[at as u64] as i32;
|
|
}
|
|
|
|
# Longer than the table reads: the code's length is the first whose codes end
|
|
# past the bits.
|
|
val top := this.buffer >> 16;
|
|
var length: u64 = (fast_bits + 1) as u64;
|
|
while top >= table.max_code[length] {
|
|
length++;
|
|
}
|
|
if length == 17 {
|
|
return -1;
|
|
}
|
|
val index := ((this.buffer >> (32 - length) as u32) as i32 + table.delta[length]) as u64;
|
|
if index > 255 || table.size[index] as u64 != length {
|
|
return -1;
|
|
}
|
|
this.buffer <<= length as u32;
|
|
this.count -= length as u32;
|
|
return table.values[index] as i32;
|
|
}
|
|
|
|
# Reads bits, and makes them the signed value they stand for: those beginning
|
|
# with 0 are below zero.
|
|
def bit_reader::extend(&mut this, size: u32): i32 {
|
|
val value := this.take(size) as i32;
|
|
if value < (1 << (size - 1)) {
|
|
return value - (1 << size) + 1;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
# Reads a number of bits, up to 16, the first the most significant.
|
|
def bit_reader::take(&mut this, size: u32): u32 {
|
|
if size == 0 {
|
|
return 0;
|
|
}
|
|
if this.count < size {
|
|
this.fill();
|
|
}
|
|
val made := this.buffer >> (32 - size);
|
|
this.buffer <<= size;
|
|
this.count -= size;
|
|
return made;
|
|
}
|
|
|
|
# Fills the buffer to more than 24 bits, with zeros once a marker ends the data.
|
|
def bit_reader::fill(&mut this): void {
|
|
while this.count <= 24 {
|
|
var byte: u32 = 0;
|
|
if this.marker == 0 && this.at < this.data.length {
|
|
byte = this.data[this.at] as u32;
|
|
this.at++;
|
|
if byte == 0xFF {
|
|
while this.at < this.data.length && this.data[this.at] == 0xFF {
|
|
this.at++;
|
|
}
|
|
val next: u8 = this.at < this.data.length ? this.data[this.at] : 0xD9;
|
|
this.at++;
|
|
if next != 0 {
|
|
this.marker = next;
|
|
byte = 0;
|
|
}
|
|
}
|
|
}
|
|
this.buffer |= byte << (24 - this.count);
|
|
this.count += 8;
|
|
}
|
|
}
|