272 lines
8.8 KiB
Plaintext
272 lines
8.8 KiB
Plaintext
#! Contains how deflate writes its blocks: bits after one another, the first the
|
|
#! least significant, and each block of bytes and runs in whichever of
|
|
#! DEFLATE's three ways is shortest: stored as it is, by the fixed codes, or by
|
|
#! codes of its own, which it gives first.
|
|
|
|
module image::png;
|
|
|
|
|
|
# Writes bits after one another, the first the least significant, as DEFLATE
|
|
# packs them into bytes.
|
|
struct bit_writer {
|
|
var out: vector<u8>;
|
|
var buffer: u64;
|
|
var count: u64; #! How many bits the buffer holds, fewer than 8 between writes.
|
|
}
|
|
|
|
# Writes the lowest bits of a number.
|
|
def bit_writer::put(&mut this, bits: u32, size: u64): void {
|
|
this.buffer |= (bits as u64) << this.count;
|
|
this.count += size;
|
|
while this.count >= 8 {
|
|
this.out.append(this.buffer as u8);
|
|
this.buffer >>= 8;
|
|
this.count -= 8;
|
|
}
|
|
}
|
|
|
|
# Writes zeros up to the next whole byte.
|
|
def bit_writer::align(&mut this): void {
|
|
if this.count > 0 {
|
|
this.put(0, 8 - this.count);
|
|
}
|
|
}
|
|
|
|
# A block's bytes and runs, as deflate finds them: each a byte, by a length of
|
|
# 0, or a run of a length back a distance, and how often each symbol is used.
|
|
struct block {
|
|
var lengths: vector<u16>;
|
|
var values: vector<u16>; #! Each byte, or each run's distance.
|
|
var literals: u32[288]; #! How often each literal and length symbol is used.
|
|
var distances: u32[30];
|
|
var start: u64; #! Where the block's bytes begin in the data.
|
|
}
|
|
|
|
def block::new(): block {
|
|
return block { lengths = vector::<u16>::new(), values = vector::<u16>::new() };
|
|
}
|
|
|
|
# Empties the block, to hold what's found from a place on.
|
|
def block::reset(&mut this, start: u64): void {
|
|
this.lengths.clear();
|
|
this.values.clear();
|
|
for symbol in 0u..288u {
|
|
this.literals[symbol] = 0;
|
|
}
|
|
for symbol in 0u..30u {
|
|
this.distances[symbol] = 0;
|
|
}
|
|
this.start = start;
|
|
}
|
|
|
|
def block::byte(&mut this, value: u8): void {
|
|
this.lengths.append(0);
|
|
this.values.append(value as u16);
|
|
this.literals[value as u64]++;
|
|
}
|
|
|
|
def block::run(&mut this, length: u64, distance: u64): void {
|
|
this.lengths.append(length as u16);
|
|
this.values.append(distance as u16);
|
|
this.literals[257 + length_symbol[length] as u64]++;
|
|
this.distances[distance_symbol(distance)]++;
|
|
}
|
|
|
|
# The codes a block is written by, and their lengths.
|
|
struct codes {
|
|
var literal_lengths: u8[288];
|
|
var literal_codes: u16[288];
|
|
var distance_lengths: u8[30];
|
|
var distance_codes: u16[30];
|
|
}
|
|
|
|
# DEFLATE's fixed codes.
|
|
def codes::fixed(): codes {
|
|
var made := codes { literal_lengths = fixed_lengths() };
|
|
for symbol in 0u..30u {
|
|
made.distance_lengths[symbol] = 5;
|
|
}
|
|
codes_of(made.literal_lengths[:], made.literal_codes[:]);
|
|
codes_of(made.distance_lengths[:], made.distance_codes[:]);
|
|
return made;
|
|
}
|
|
|
|
# How many bits a block's symbols take in codes, without its header.
|
|
def codes::cost(&this, held: &block): u64 {
|
|
var bits: u64 = 0;
|
|
for symbol in 0u..286u {
|
|
bits += held.literals[symbol] as u64 * this.literal_lengths[symbol] as u64;
|
|
}
|
|
for symbol in 0u..30u {
|
|
bits += held.distances[symbol] as u64 * this.distance_lengths[symbol] as u64;
|
|
}
|
|
return bits;
|
|
}
|
|
|
|
# Writes a block of data, the last or not, in whichever way is shortest.
|
|
def bit_writer::block(&mut this, held: &mut block, data: span<u8>, end: u64, last: bool, fixed: &codes): void {
|
|
held.literals[256] = 1;
|
|
val bytes := data[held.start:end - held.start];
|
|
|
|
# The bits more of each run's length and distance, which any codes write.
|
|
var extra: u64 = 0;
|
|
for symbol in 0u..29u {
|
|
extra += held.literals[257 + symbol] as u64 * length_extra[symbol] as u64;
|
|
}
|
|
for symbol in 0u..30u {
|
|
extra += held.distances[symbol] as u64 * distance_extra[symbol] as u64;
|
|
}
|
|
|
|
# Codes of its own, and the lengths of them as the code of lengths gives them.
|
|
var own := codes {};
|
|
code_lengths(held.literals[0:286], 15, own.literal_lengths[0:286]);
|
|
code_lengths(held.distances[:], 15, own.distance_lengths[:]);
|
|
codes_of(own.literal_lengths[:], own.literal_codes[:]);
|
|
codes_of(own.distance_lengths[:], own.distance_codes[:]);
|
|
var literals: u64 = 286;
|
|
while literals > 257 && own.literal_lengths[literals - 1] == 0 {
|
|
literals--;
|
|
}
|
|
var distances: u64 = 30;
|
|
while distances > 1 && own.distance_lengths[distances - 1] == 0 {
|
|
distances--;
|
|
}
|
|
var all_lengths: u8[316];
|
|
for symbol in 0..literals {
|
|
all_lengths[symbol] = own.literal_lengths[symbol];
|
|
}
|
|
for symbol in 0..distances {
|
|
all_lengths[literals + symbol] = own.distance_lengths[symbol];
|
|
}
|
|
var runs := vector::<u8>::new();
|
|
var repeats := vector::<u8>::new();
|
|
var run_counts: u32[19];
|
|
run_lengths(all_lengths[0:literals + distances], &mut runs, &mut repeats, run_counts[:]);
|
|
var length_lengths: u8[19];
|
|
var length_codes: u16[19];
|
|
code_lengths(run_counts[:], 7, length_lengths[:]);
|
|
codes_of(length_lengths[:], length_codes[:]);
|
|
var told: u64 = 19;
|
|
while told > 4 && length_lengths[length_order[told - 1]] == 0 {
|
|
told--;
|
|
}
|
|
|
|
var own_bits: u64 = 3 + 14 + told * 3 + own.cost(held) + extra;
|
|
for symbol in 0u..19u {
|
|
own_bits += run_counts[symbol] as u64 * length_lengths[symbol] as u64;
|
|
}
|
|
own_bits += (run_counts[16] as u64) * 2 + (run_counts[17] as u64) * 3 + (run_counts[18] as u64) * 7;
|
|
val fixed_bits := 3 + fixed.cost(held) + extra;
|
|
val stored_bits := (bytes.length + 5 * ((bytes.length + 65534) / 65535 + 1)) * 8;
|
|
|
|
if stored_bits <= own_bits && stored_bits <= fixed_bits {
|
|
this.stored(bytes, last);
|
|
return;
|
|
}
|
|
this.put(last ? 1 : 0, 1);
|
|
if fixed_bits <= own_bits {
|
|
this.put(1, 2);
|
|
this.symbols(held, fixed);
|
|
return;
|
|
}
|
|
this.put(2, 2);
|
|
this.put((literals - 257) as u32, 5);
|
|
this.put((distances - 1) as u32, 5);
|
|
this.put((told - 4) as u32, 4);
|
|
for index in 0..told {
|
|
this.put(length_lengths[length_order[index]] as u32, 3);
|
|
}
|
|
for index in 0..runs.length {
|
|
val symbol := runs[index] as u64;
|
|
this.put(length_codes[symbol] as u32, length_lengths[symbol] as u64);
|
|
if symbol >= 16 {
|
|
this.put(repeats[index] as u32, symbol == 16 ? 2 : (symbol == 17 ? 3 : 7));
|
|
}
|
|
}
|
|
this.symbols(held, &own);
|
|
}
|
|
|
|
# Writes a block's bytes and runs by codes, then its end.
|
|
def bit_writer::symbols(&mut this, held: &block, by: &codes): void {
|
|
for index in 0..held.lengths.length {
|
|
val length := held.lengths[index] as u64;
|
|
val value := held.values[index] as u64;
|
|
if length == 0 {
|
|
this.put(by.literal_codes[value] as u32, by.literal_lengths[value] as u64);
|
|
continue;
|
|
}
|
|
val symbol := length_symbol[length] as u64;
|
|
this.put(by.literal_codes[257 + symbol] as u32, by.literal_lengths[257 + symbol] as u64);
|
|
this.put((length - length_base[symbol] as u64) as u32, length_extra[symbol] as u64);
|
|
val which := distance_symbol(value);
|
|
this.put(by.distance_codes[which] as u32, by.distance_lengths[which] as u64);
|
|
this.put((value - distance_base[which] as u64) as u32, distance_extra[which] as u64);
|
|
}
|
|
this.put(by.literal_codes[256] as u32, by.literal_lengths[256] as u64);
|
|
}
|
|
|
|
# Writes bytes as stored blocks, as many as hold them, the last of them the
|
|
# data's last block or not.
|
|
def bit_writer::stored(&mut this, bytes: span<u8>, last: bool): void {
|
|
var at: u64 = 0;
|
|
while true {
|
|
val size := bytes.length - at < 65535 ? bytes.length - at : 65535;
|
|
val final := last && at + size == bytes.length;
|
|
this.put(final ? 1 : 0, 1);
|
|
this.put(0, 2);
|
|
this.align();
|
|
this.put(size as u32, 16);
|
|
this.put((size ^ 0xFFFF) as u32, 16);
|
|
this.out.extend(bytes[at:size]);
|
|
at += size;
|
|
if at >= bytes.length {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
# The lengths of a dynamic block's codes as the code of lengths gives them: each
|
|
# length itself, or 16 repeating the last 3 to 6 times, or 17 and 18 giving 3 to
|
|
# 10 zeros, and 11 to 138, each with the count it adds bits for, counting how
|
|
# often each is used.
|
|
def run_lengths(lengths: span<u8>, runs: &mut vector<u8>, repeats: &mut vector<u8>, counts: slice<u32>): void {
|
|
var at: u64 = 0;
|
|
while at < lengths.length {
|
|
val length := lengths[at];
|
|
var run: u64 = 1;
|
|
while at + run < lengths.length && lengths[at + run] == length {
|
|
run++;
|
|
}
|
|
at += run;
|
|
if length == 0 {
|
|
while run >= 11 {
|
|
val taken := run < 138 ? run : 138;
|
|
add_run(runs, repeats, counts, 18, taken - 11);
|
|
run -= taken;
|
|
}
|
|
if run >= 3 {
|
|
add_run(runs, repeats, counts, 17, run - 3);
|
|
run = 0;
|
|
}
|
|
} else {
|
|
add_run(runs, repeats, counts, length, 0);
|
|
run--;
|
|
while run >= 3 {
|
|
val taken := run < 6 ? run : 6;
|
|
add_run(runs, repeats, counts, 16, taken - 3);
|
|
run -= taken;
|
|
}
|
|
}
|
|
while run > 0 {
|
|
add_run(runs, repeats, counts, length, 0);
|
|
run--;
|
|
}
|
|
}
|
|
}
|
|
|
|
def add_run(runs: &mut vector<u8>, repeats: &mut vector<u8>, counts: slice<u32>, symbol: u8, repeat: u64): void {
|
|
runs.append(symbol);
|
|
repeats.append(repeat as u8);
|
|
counts[symbol as u64]++;
|
|
}
|