Files
image/image/png/colors.sakura
T
2026-10-28 08:26:26 -05:00

133 lines
4.1 KiB
Plaintext

#! Contains what colors an image holds, which choose the color type and depth
#! it's written in: whether every pixel is gray, whether every one is opaque,
#! the least depth its grays are exact at, and its colors, where there are no
#! more than a palette holds.
module image::png;
# How many places the table of colors has: four for each entry a palette may.
const color_slots: u64 = 1024;
# The colors of an image, up to a palette's 256, each an entry in the order
# they're added, found by its RGBA in a table.
struct colors {
var keys: u32[1024];
var slots: u16[1024]; #! Each place's entry, plus 1, or 0 where it's empty.
var entries: u32[256]; #! Each entry's RGBA, red the most significant.
var count: u32;
var full: bool; #! Whether more colors were seen than a palette holds.
}
# The RGBA of the pixel at a byte, red the most significant.
def key_of(pixels: span<u8>, at: u64): u32 {
return (pixels[at] as u32 << 24) | (pixels[at + 1] as u32 << 16) | (pixels[at + 2] as u32 << 8) | pixels[at + 3] as u32;
}
def slot_of(key: u32): u64 {
return ((key * 2654435761) >> 22) as u64;
}
# Adds a color, where it's new, and marks the colors full where there's no room.
def colors::add(&mut this, key: u32): void {
var slot := slot_of(key);
while this.slots[slot] != 0 {
if this.keys[slot] == key {
return;
}
slot = (slot + 1) % color_slots;
}
if this.count >= 256 {
this.full = true;
return;
}
this.keys[slot] = key;
this.slots[slot] = (this.count + 1) as u16;
this.entries[this.count as u64] = key;
this.count++;
}
# The entry of a color added.
def colors::entry_of(&this, key: u32): u32 {
var slot := slot_of(key);
while this.keys[slot] != key || this.slots[slot] == 0 {
slot = (slot + 1) % color_slots;
}
return this.slots[slot] as u32 - 1;
}
# The same colors, those with any alpha first, so `tRNS` needs name no others.
def colors::alpha_first(&this): colors {
var made := colors {};
for pass in 0u..2u {
for entry in 0..this.count as u64 {
val opaque := (this.entries[entry] & 0xFF) == 0xFF;
if opaque == (pass == 1) {
made.add(this.entries[entry]);
}
}
}
return made;
}
# The header an image is written with: where it's reduced, the least color type
# and depth that hold its pixels exactly, and its palette, where it has one,
# made of its colors; and otherwise RGBA of 8 bits.
def header_for(picture: &image, reduced: bool, found: &mut colors): header {
var head := header { width = picture.width, height = picture.height, depth = 8, color = rgba, channels = 4 };
if !reduced {
return head;
}
var is_gray := true;
var opaque := true;
var gray_depth: u32 = 1;
var seen := colors {};
val pixels := picture.pixels.view();
var at: u64 = 0;
while at < pixels.length {
val r := pixels[at];
if is_gray && (r != pixels[at + 1] || r != pixels[at + 2]) {
is_gray = false;
}
if is_gray && gray_depth < 8 {
val needs: u32 = r % 255 == 0 ? 1 : (r % 85 == 0 ? 2 : (r % 17 == 0 ? 4 : 8));
gray_depth = needs > gray_depth ? needs : gray_depth;
}
if pixels[at + 3] != 255 {
opaque = false;
}
if !seen.full {
seen.add(key_of(pixels, at));
}
at += 4;
}
# A palette's indices are as few bits as its entries need, which is fewer than
# any but gray's samples take.
val entries := seen.count;
val palette_depth: u32 = entries <= 2 ? 1 : (entries <= 4 ? 2 : (entries <= 16 ? 4 : 8));
if is_gray && opaque && (seen.full || palette_depth >= gray_depth) {
head.color = gray;
head.depth = gray_depth;
} else if !seen.full {
head.color = indexed;
head.depth = palette_depth;
found = seen.alpha_first();
head.entries = found.count;
for entry in 0..found.count as u64 {
val key := found.entries[entry];
head.palette[entry * 4] = (key >> 24) as u8;
head.palette[entry * 4 + 1] = (key >> 16) as u8;
head.palette[entry * 4 + 2] = (key >> 8) as u8;
head.palette[entry * 4 + 3] = key as u8;
}
} else if is_gray {
head.color = gray_alpha;
} else if opaque {
head.color = truecolor;
}
head.channels = channels_of(head.color);
return head;
}