use crate::crpix::Header;
use crate::{NineSlice, SampleMode};
use core::fmt;
#[derive(Clone, Copy, Debug)]
pub struct Image<'a> {
pub name: &'a str,
pub width: u32,
pub height: u32,
pub rgba: &'a [u8],
}
pub const ALPHABET: &[u8] =
b"kwrgbycmoptvzafhijlnqsuxde0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ+-*/=<>~^&%$@!?:;,'\"()[]{}|`_";
pub const MAX_COLOURS: usize = 512;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TraceError {
NoImages,
WrongBufferLength {
name: String,
expected: usize,
found: usize,
},
EmptyImage { name: String },
SizeMismatch {
first: String,
first_size: (u32, u32),
offender: String,
offender_size: (u32, u32),
},
TooManyColours { found: usize },
DuplicateName { name: String },
}
impl fmt::Display for TraceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoImages => write!(f, "no images to trace"),
Self::WrongBufferLength {
name,
expected,
found,
} => write!(
f,
"`{name}` is {found} bytes and its size needs {expected} — RGBA8, four \
bytes a pixel"
),
Self::EmptyImage { name } => write!(f, "`{name}` has no pixels"),
Self::SizeMismatch {
first,
first_size,
offender,
offender_size,
} => write!(
f,
"`{offender}` is {}x{} and `{first}` is {}x{}; every frame of a sheet is \
the same size, and padding one silently would move the art on that frame \
alone",
offender_size.0, offender_size.1, first_size.0, first_size.1
),
Self::TooManyColours { found } => write!(
f,
"{found} distinct colours, and the limit is {MAX_COLOURS}. This looks like \
a photograph rather than pixel art; reduce its colours in the tool that \
drew it"
),
Self::DuplicateName { name } => {
write!(f, "two frames are both called `{name}`")
}
}
}
}
impl core::error::Error for TraceError {}
#[derive(Clone, Copy, Debug, Default)]
pub struct Options {
pub nine: Option<NineSlice>,
pub sample: SampleMode,
pub clip: Option<&'static str>,
pub hold: u32,
}
pub fn trace(images: &[Image<'_>], options: &Options) -> Result<String, TraceError> {
let first = images.first().ok_or(TraceError::NoImages)?;
if first.width == 0 || first.height == 0 {
return Err(TraceError::EmptyImage {
name: first.name.to_string(),
});
}
for (index, image) in images.iter().enumerate() {
if (image.width, image.height) != (first.width, first.height) {
return Err(TraceError::SizeMismatch {
first: first.name.to_string(),
first_size: (first.width, first.height),
offender: image.name.to_string(),
offender_size: (image.width, image.height),
});
}
let expected = (image.width as usize) * (image.height as usize) * 4;
if image.rgba.len() != expected {
return Err(TraceError::WrongBufferLength {
name: image.name.to_string(),
expected,
found: image.rgba.len(),
});
}
if images[..index].iter().any(|other| other.name == image.name) {
return Err(TraceError::DuplicateName {
name: image.name.to_string(),
});
}
}
let mut palette: Vec<[u8; 4]> = Vec::new();
for image in images {
for pixel in image.rgba.as_chunks::<4>().0 {
let colour = normalise([pixel[0], pixel[1], pixel[2], pixel[3]]);
if !palette.contains(&colour) {
if palette.len() == MAX_COLOURS {
return Err(TraceError::TooManyColours {
found: count_colours(images),
});
}
palette.push(colour);
}
}
}
let transparent = palette.iter().position(|c| c[3] == 0);
let opaque = palette.len() - usize::from(transparent.is_some());
let chars = if opaque <= ALPHABET.len() { 1 } else { 2 };
if opaque > ALPHABET.len() * ALPHABET.len() {
return Err(TraceError::TooManyColours {
found: palette.len(),
});
}
let keys = assign_keys(&palette, transparent, chars);
let header = Header {
width: first.width,
height: first.height,
frames: images.len() as u32,
colours: palette.len() as u32,
chars: chars as u32,
};
Ok(render(images, &palette, &keys, header, options))
}
fn normalise(colour: [u8; 4]) -> [u8; 4] {
if colour[3] == 0 { [0, 0, 0, 0] } else { colour }
}
fn count_colours(images: &[Image<'_>]) -> usize {
let mut seen: Vec<[u8; 4]> = Vec::new();
for image in images {
for pixel in image.rgba.as_chunks::<4>().0 {
let colour = normalise([pixel[0], pixel[1], pixel[2], pixel[3]]);
if !seen.contains(&colour) {
seen.push(colour);
}
}
}
seen.len()
}
fn assign_keys(palette: &[[u8; 4]], transparent: Option<usize>, chars: usize) -> Vec<String> {
let mut keys = Vec::with_capacity(palette.len());
let mut next = 0usize;
for index in 0..palette.len() {
if Some(index) == transparent {
keys.push(".".repeat(chars));
continue;
}
keys.push(key_at(next, chars));
next += 1;
}
keys
}
fn key_at(n: usize, chars: usize) -> String {
let base = ALPHABET.len();
if chars == 1 {
return (ALPHABET[n] as char).to_string();
}
let high = ALPHABET[n / base] as char;
let low = ALPHABET[n % base] as char;
[high, low].iter().collect()
}
fn render(
images: &[Image<'_>],
palette: &[[u8; 4]],
keys: &[String],
header: Header,
options: &Options,
) -> String {
let mut out = String::with_capacity(1024);
out.push_str("# generated by `crcbl crpix`; edit freely\n");
out.push_str(&format!(
"crpix: {} {} {} {} {} # w h frames colours chars-per-pixel\n\npalette:\n",
header.width, header.height, header.frames, header.colours, header.chars
));
for (colour, key) in palette.iter().zip(keys) {
if colour[3] == 0 {
out.push_str(&format!(" {key} c None\n"));
} else if colour[3] == 255 {
out.push_str(&format!(
" {key} c #{:02x}{:02x}{:02x}\n",
colour[0], colour[1], colour[2]
));
} else {
out.push_str(&format!(
" {key} c #{:02x}{:02x}{:02x}{:02x}\n",
colour[0], colour[1], colour[2], colour[3]
));
}
}
for image in images {
out.push_str(&format!("\nframe {}:\n", image.name));
for y in 0..image.height {
out.push_str(" ");
for x in 0..image.width {
let at = ((y * image.width + x) * 4) as usize;
let colour = normalise([
image.rgba[at],
image.rgba[at + 1],
image.rgba[at + 2],
image.rgba[at + 3],
]);
let index = palette
.iter()
.position(|candidate| *candidate == colour)
.expect("every colour was collected in the pass above");
out.push_str(&keys[index]);
}
out.push('\n');
}
}
if let Some(clip) = options.clip {
let names: Vec<&str> = images.iter().map(|image| image.name).collect();
out.push_str(&format!("\nclip {clip}: {}", names.join(" ")));
if options.hold > 1 {
out.push_str(&format!(" @ {}", options.hold));
}
out.push_str(" loop\n");
}
if let Some(nine) = options.nine {
out.push_str(&format!(
"\nnine: {} {} {} {}\n",
nine.left, nine.right, nine.top, nine.bottom
));
}
if options.sample == SampleMode::Smooth {
out.push_str("\nsample: smooth\n");
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::crpix;
const FOUR: [u8; 16] = [
0, 0, 0, 0, 255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 128,
];
fn image<'a>(name: &'a str, rgba: &'a [u8]) -> Image<'a> {
Image {
name,
width: 2,
height: 2,
rgba,
}
}
#[test]
fn a_traced_image_round_trips_through_the_parser() {
let text = trace(&[image("a", &FOUR)], &Options::default()).expect("this traces");
let art = match crpix::parse(&text) {
Ok(art) => art,
Err(error) => panic!("generated text does not parse: {error}\n{text}"),
};
assert_eq!(art.frames.len(), 1);
assert_eq!(art.frames[0].name, "a");
assert_eq!(art.frames[0].pixels, FOUR, "the pixels changed:\n{text}");
}
#[test]
fn several_images_become_one_sheet_of_frames() {
let second: Vec<u8> = FOUR
.as_chunks::<4>()
.0
.iter()
.rev()
.flatten()
.copied()
.collect();
let text = trace(
&[image("up", &FOUR), image("down", &second)],
&Options {
clip: Some("flap"),
hold: 6,
..Options::default()
},
)
.expect("this traces");
let art = crpix::parse(&text).unwrap_or_else(|e| panic!("{e}\n{text}"));
assert_eq!(art.header.frames, 2);
assert_eq!(art.frames[0].name, "up");
assert_eq!(art.frames[1].name, "down");
assert_eq!(art.frames[0].pixels, FOUR);
assert_eq!(art.frames[1].pixels, second);
assert_eq!(art.clips.len(), 1);
assert_eq!(art.clips[0].name, "flap");
assert_eq!(art.frames[0].hold, 6);
assert!(art.clips[0].looping);
}
#[test]
fn images_of_different_sizes_are_refused() {
let tall = [0u8; 2 * 3 * 4];
let error = trace(
&[
image("a", &FOUR),
Image {
name: "b",
width: 2,
height: 3,
rgba: &tall,
},
],
&Options::default(),
)
.expect_err("2x3 is not 2x2");
assert_eq!(
error,
TraceError::SizeMismatch {
first: "a".into(),
first_size: (2, 2),
offender: "b".into(),
offender_size: (2, 3),
}
);
assert!(error.to_string().contains("same size"), "{error}");
}
#[test]
fn the_generated_header_matches_what_was_generated() {
let text = trace(&[image("a", &FOUR)], &Options::default()).expect("traces");
let art = crpix::parse(&text).expect("parses");
assert_eq!(art.header.width, 2);
assert_eq!(art.header.height, 2);
assert_eq!(art.header.frames, 1);
assert_eq!(art.header.colours, 4);
assert_eq!(art.header.chars, 1);
}
#[test]
fn transparency_is_one_colour_and_takes_the_conventional_key() {
let messy = [
0, 0, 0, 0, 200, 30, 90, 0, 17, 34, 51, 255, 17, 34, 51, 255,
];
let text = trace(&[image("a", &messy)], &Options::default()).expect("traces");
assert!(text.contains(" . c None"), "{text}");
let art = crpix::parse(&text).expect("parses");
assert_eq!(art.header.colours, 2, "two colours, not three:\n{text}");
assert_eq!(&art.frames[0].pixels[0..4], [0, 0, 0, 0]);
assert_eq!(
&art.frames[0].pixels[4..8],
[0, 0, 0, 0],
"a transparent pixel keeps no colour, or it haloes"
);
}
#[test]
fn a_palette_larger_than_the_alphabet_widens_the_keys() {
let count = ALPHABET.len() + 5;
let mut rgba = Vec::new();
for i in 0..count {
rgba.extend_from_slice(&[(i % 251) as u8, (i / 251) as u8, 7, 255]);
}
let wide = Image {
name: "a",
width: count as u32,
height: 1,
rgba: &rgba,
};
let text = trace(&[wide], &Options::default()).expect("traces");
let art = crpix::parse(&text).unwrap_or_else(|e| panic!("{e}\n{text}"));
assert_eq!(art.header.chars, 2, "{text}");
assert_eq!(art.header.colours as usize, count);
assert_eq!(art.frames[0].pixels, rgba, "the pixels changed");
}
#[test]
fn art_with_too_many_colours_is_refused() {
let count = MAX_COLOURS + 10;
let mut rgba = Vec::new();
for i in 0..count {
rgba.extend_from_slice(&[(i % 256) as u8, (i / 256) as u8, 11, 255]);
}
let photo = Image {
name: "a",
width: count as u32,
height: 1,
rgba: &rgba,
};
let error = trace(&[photo], &Options::default()).expect_err("too many colours");
assert!(matches!(error, TraceError::TooManyColours { .. }));
assert!(error.to_string().contains("photograph"), "{error}");
}
#[test]
fn the_remaining_refusals_each_say_what_is_wrong() {
assert_eq!(
trace(&[], &Options::default()).expect_err("nothing to do"),
TraceError::NoImages
);
let short = [0u8; 4];
assert!(matches!(
trace(&[image("a", &short)], &Options::default()).expect_err("short buffer"),
TraceError::WrongBufferLength { .. }
));
assert!(matches!(
trace(
&[Image {
name: "a",
width: 0,
height: 2,
rgba: &[]
}],
&Options::default()
)
.expect_err("no pixels"),
TraceError::EmptyImage { .. }
));
assert!(matches!(
trace(&[image("a", &FOUR), image("a", &FOUR)], &Options::default())
.expect_err("two frames called `a`"),
TraceError::DuplicateName { .. }
));
}
#[test]
fn the_options_reach_the_generated_file() {
let text = trace(
&[image("a", &FOUR)],
&Options {
nine: Some(NineSlice::new(1, 1, 1, 1)),
sample: SampleMode::Smooth,
..Options::default()
},
)
.expect("traces");
let art = crpix::parse(&text).unwrap_or_else(|e| panic!("{e}\n{text}"));
assert_eq!(art.nine, Some(NineSlice::new(1, 1, 1, 1)));
assert_eq!(art.sample, SampleMode::Smooth);
}
#[test]
fn tracing_the_same_image_twice_gives_a_byte_identical_file() {
let once = trace(&[image("a", &FOUR)], &Options::default()).expect("traces");
let twice = trace(&[image("a", &FOUR)], &Options::default()).expect("traces");
assert_eq!(once, twice);
}
}