Files
image/image/png/huffman.sakura
T
2026-10-30 15:04:14 -05:00

232 lines
6.5 KiB
Plaintext

#! Contains DEFLATE's Huffman codes: the tables inflate decodes symbols by, read
#! as stb_image reads them, by a table of codes up to 9 bits long and longer ones
#! by where each length's codes end; and the codes deflate gives symbols, by how
#! often each is used, no longer than a limit, as miniz makes them.
module malunal::image::png;
# How many bits the fast table reads at once.
const fast_bits: u64 = 9;
# A Huffman code: each symbol's code, as the lengths of all of them give it.
struct huffman {
var fast: u16[512]; #! `length << 9 | symbol` by a code's first 9 bits, read reversed, or 0 for a longer one.
var first_code: u32[16];
var first_symbol: u32[16];
var max_code: u32[17]; #! Just past the last code of each length, shifted to be 16 bits long.
var size: u8[288];
var value: u16[288];
}
# Builds a code from each symbol's length, which may leave codes unused, but not
# give more than there are.
def huffman::build(&mut this, lengths: span<u8>): bool {
var counts: u32[17];
for length in lengths {
counts[length as u64]++;
}
counts[0] = 0;
for bits in 1u..16u {
if counts[bits] as u64 > (1u << bits) {
return false;
}
}
var next: u32[16];
var code: u32 = 0;
var symbols: u32 = 0;
for bits in 1u..16u {
next[bits] = code;
this.first_code[bits] = code;
this.first_symbol[bits] = symbols;
code += counts[bits];
if counts[bits] != 0 && (code - 1) as u64 >= (1u << bits) {
return false;
}
this.max_code[bits] = code << (16 - bits) as u32;
code <<= 1;
symbols += counts[bits];
}
this.max_code[16] = 0x10000;
for symbol, length in lengths {
if length == 0 {
continue;
}
val bits := length as u64;
val slot := (next[bits] - this.first_code[bits] + this.first_symbol[bits]) as u64;
this.size[slot] = length;
this.value[slot] = symbol as u16;
if bits <= fast_bits {
var at := reverse(next[bits], bits) as u64;
while at < 512 {
this.fast[at] = ((bits << 9) | symbol) as u16;
at += 1u << bits;
}
}
next[bits]++;
}
return true;
}
# A symbol deflate gives a code, and how often it's used, which becomes the
# length of its code.
struct leaf {
var symbol: u64;
var weight: u64;
}
# The lengths of a code for symbols, by how often each is used, none longer than
# a limit: Huffman's, found by Moffat and Katajainen's method in place, then
# those too long shortened and others lengthened to make room, as miniz does. A
# symbol never used has none, but two at least have one, so the code is
# complete and each symbol of it a bit long at least.
def code_lengths(counts: span<u32>, limit: u64, lengths: slice<u8>): void {
var leaves := vector::<leaf>::new();
for symbol in 0..counts.length {
lengths[symbol] = 0;
if counts[symbol] != 0 {
leaves.append(leaf { symbol = symbol, weight = counts[symbol] as u64 });
}
}
var extra: u64 = 0;
while leaves.length < 2 {
if counts[extra] == 0 {
leaves.append(leaf { symbol = extra, weight = 1 });
}
extra++;
}
leaves.sort_by(def(a: &leaf, b: &leaf): bool => a.weight < b.weight || (a.weight == b.weight && a.symbol < b.symbol));
# Each leaf's weight, then each node's parent, then each leaf's depth.
val n := leaves.length;
var a := vector::<u64>::new();
for held in leaves {
a.append(held.weight);
}
a[0] += a[1];
var root: u64 = 0;
var next_leaf: u64 = 2;
for next in 1..n - 1 {
if next_leaf >= n || a[root] < a[next_leaf] {
a[next] = a[root];
a[root] = next;
root++;
} else {
a[next] = a[next_leaf];
next_leaf++;
}
if next_leaf >= n || (root < next && a[root] < a[next_leaf]) {
a[next] += a[root];
a[root] = next;
root++;
} else {
a[next] += a[next_leaf];
next_leaf++;
}
}
a[n - 2] = 0;
var node := n as i64 - 3;
while node >= 0 {
a[node as u64] = a[a[node as u64]] + 1;
node--;
}
var available: u64 = 1;
var used: u64 = 0;
var depth: u64 = 0;
var parent := n as i64 - 2;
var filling := n as i64 - 1;
while available > 0 {
while parent >= 0 && a[parent as u64] == depth {
used++;
parent--;
}
while available > used {
a[filling as u64] = depth;
filling--;
available--;
}
available = 2 * used;
depth++;
used = 0;
}
# How many codes of each length, those longer than the limit at it, then
# moved until the code fits: a code taken from the longest, and one shorter
# split into two a bit longer, until there are no more than room for.
var of_length: u64[17];
for depth_of in a {
of_length[depth_of < limit ? depth_of : limit]++;
}
var total: u64 = 0;
for bits in 1..=limit {
total += of_length[bits] << (limit - bits);
}
while total > (1u << limit) {
of_length[limit]--;
var bits := limit - 1;
while bits > 0 {
if of_length[bits] != 0 {
of_length[bits]--;
of_length[bits + 1] += 2;
break;
}
bits--;
}
total--;
}
# The shortest codes to those used most, which sort last.
var taken := n;
for bits in 1..=limit {
for _ in 0..of_length[bits] {
taken--;
lengths[leaves[taken].symbol] = bits as u8;
}
}
}
# The codes of each symbol's length, as DEFLATE gives them: those of each length
# in the order of their symbols, after all those shorter, each reversed, as
# DEFLATE writes a code's first bit as the least significant.
def codes_of(lengths: span<u8>, codes: slice<u16>): void {
var counts: u32[16];
for length in lengths {
counts[length as u64]++;
}
counts[0] = 0;
var next: u32[16];
var code: u32 = 0;
for bits in 1u..16u {
code = (code + counts[bits - 1]) << 1;
next[bits] = code;
}
for symbol, length in lengths {
if length != 0 {
codes[symbol] = reverse(next[length as u64], length as u64) as u16;
next[length as u64]++;
}
}
}
# The lowest bits of a number, in the opposite order.
def reverse(code: u32, bits: u64): u32 {
var from := code;
var made: u32 = 0;
for _ in 0..bits {
made = (made << 1) | (from & 1);
from >>= 1;
}
return made;
}
# The 16 lowest bits of a number, in the opposite order.
def reverse16(code: u32): u32 {
var made := code;
made = ((made & 0xAAAA) >> 1) | ((made & 0x5555) << 1);
made = ((made & 0xCCCC) >> 2) | ((made & 0x3333) << 2);
made = ((made & 0xF0F0) >> 4) | ((made & 0x0F0F) << 4);
return ((made & 0xFF00) >> 8) | ((made & 0x00FF) << 8);
}