use crate::{Clip, Direction, Frame, NineSlice, Rect, SampleMode, Sheet, SheetError};
use core::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Rgba8 {
pub width: u32,
pub height: u32,
pub pixels: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Loaded {
pub sheet: Sheet,
pub image: Rgba8,
}
impl Loaded {
#[must_use]
pub fn frame_pixels(&self, index: usize) -> Option<Vec<u8>> {
let rect = self.sheet.frames.get(index)?.rect;
let stride = self.image.width as usize * 4;
Some(
(0..rect.h as usize)
.flat_map(|row| {
let start = (rect.y as usize + row) * stride + rect.x as usize * 4;
self.image.pixels[start..start + rect.w as usize * 4].to_vec()
})
.collect(),
)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LoadError {
Png(String),
UnsupportedPixels { color: String, depth: u8 },
Syntax {
line: usize,
column: usize,
offset: usize,
expected: String,
},
MissingField { path: String, field: &'static str },
WrongType {
path: String,
expected: &'static str,
found: &'static str,
},
NotAnInteger { path: String, found: String },
ZeroDuration { frame: String },
TagOutOfRange {
tag: String,
from: u32,
to: u32,
frames: usize,
},
TagReversed { tag: String, from: u32, to: u32 },
UnknownDirection { tag: String, direction: String },
CenterOutsideFrame { center: Rect, frame: Rect },
SizeMismatch { json: (u32, u32), png: (u32, u32) },
ZeroTickRate,
Sheet(SheetError),
}
impl fmt::Display for LoadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Png(message) => write!(f, "decoding the sheet PNG failed: {message}"),
Self::UnsupportedPixels { color, depth } => {
write!(f, "a {depth}-bit {color} image did not normalise to RGBA8")
}
Self::Syntax {
line,
column,
offset,
expected,
} => write!(
f,
"the sidecar is not JSON: line {line}, column {column} (byte {offset}): \
expected {expected}"
),
Self::MissingField { path, field } => {
write!(f, "`{path}` has no `{field}`")
}
Self::WrongType {
path,
expected,
found,
} => write!(f, "`{path}` should be {expected}, and is {found}"),
Self::NotAnInteger { path, found } => write!(
f,
"`{path}` should be a whole number a sheet can use, and is `{found}`"
),
Self::ZeroDuration { frame } => write!(
f,
"frame `{frame}` has a duration of 0 ms, which is no time at all"
),
Self::TagOutOfRange {
tag,
from,
to,
frames,
} => write!(
f,
"frame tag `{tag}` runs {from}..={to}, but the sidecar has {frames} frames"
),
Self::TagReversed { tag, from, to } => write!(
f,
"frame tag `{tag}` runs {from}..={to}, which is backwards and names no frames"
),
Self::UnknownDirection { tag, direction } => write!(
f,
"frame tag `{tag}` has direction `{direction}`, not `forward`, `reverse` \
or `pingpong`"
),
Self::CenterOutsideFrame { center, frame } => write!(
f,
"a nine-slice centre {}x{}+{}+{} does not fit inside a {}x{} frame",
center.w, center.h, center.x, center.y, frame.w, frame.h
),
Self::SizeMismatch { json, png } => write!(
f,
"the sidecar says the sheet is {}x{} and the PNG is {}x{}",
json.0, json.1, png.0, png.1
),
Self::ZeroTickRate => write!(
f,
"a tick rate of zero: frame holds are `duration * tick_hz / 1000`"
),
Self::Sheet(error) => write!(f, "the sidecar describes an unusable sheet: {error}"),
}
}
}
impl core::error::Error for LoadError {}
impl From<SheetError> for LoadError {
fn from(error: SheetError) -> Self {
Self::Sheet(error)
}
}
pub fn decode_png(bytes: &[u8]) -> Result<Rgba8, LoadError> {
let mut decoder = png::Decoder::new(std::io::Cursor::new(bytes));
decoder.set_transformations(
png::Transformations::EXPAND | png::Transformations::ALPHA | png::Transformations::STRIP_16,
);
let mut reader = decoder
.read_info()
.map_err(|error| LoadError::Png(error.to_string()))?;
const MAX_PIXELS: u64 = 1 << 28;
let (declared_width, declared_height) = reader.info().size();
let rgba_bytes = {
let pixels = u64::from(declared_width) * u64::from(declared_height);
if pixels > MAX_PIXELS {
return Err(LoadError::Png(format!(
"a {declared_width}x{declared_height} PNG declares more than {MAX_PIXELS} pixels"
)));
}
usize::try_from(pixels * 4).map_err(|_| {
LoadError::Png("the image does not fit in this host's address space".to_owned())
})?
};
let capacity = reader.output_buffer_size().ok_or_else(|| {
LoadError::Png("the image does not fit in this host's address space".to_owned())
})?;
if capacity > rgba_bytes {
return Err(LoadError::Png(format!(
"a {declared_width}x{declared_height} PNG needing {capacity} bytes is deeper than 8-bit RGBA"
)));
}
let mut buffer = vec![0u8; capacity];
let info = reader
.next_frame(&mut buffer)
.map_err(|error| LoadError::Png(error.to_string()))?;
let (width, height) = (info.width, info.height);
buffer.truncate(info.buffer_size());
let pixels = match (info.color_type, info.bit_depth) {
(png::ColorType::Rgba, png::BitDepth::Eight) => buffer,
(png::ColorType::GrayscaleAlpha, png::BitDepth::Eight) => {
let mut rgba = Vec::with_capacity(buffer.len() * 2);
for pair in buffer.as_chunks::<2>().0 {
rgba.extend_from_slice(&[pair[0], pair[0], pair[0], pair[1]]);
}
rgba
}
(color, depth) => {
return Err(LoadError::UnsupportedPixels {
color: format!("{color:?}"),
depth: depth as u8,
});
}
};
let expected = (u64::from(width) * u64::from(height) * 4) as usize;
if pixels.len() != expected {
return Err(LoadError::Png(format!(
"a {width}x{height} RGBA8 image is {expected} bytes, and the decode produced {}",
pixels.len()
)));
}
Ok(Rgba8 {
width,
height,
pixels,
})
}
#[must_use]
pub fn hold_ticks(ms: u32, tick_hz: u32) -> u32 {
let ticks = u64::from(ms) * u64::from(tick_hz) / 1000;
u32::try_from(ticks).unwrap_or(u32::MAX).max(1)
}
pub fn read_aseprite_json(json: &str, tick_hz: u32) -> Result<Sheet, LoadError> {
if tick_hz == 0 {
return Err(LoadError::ZeroTickRate);
}
let root = Parser::new(json).parse_document()?;
let root = object(&root, "the sidecar")?;
let frames = read_frames(field_required(root, "the sidecar", "frames")?, tick_hz)?;
let meta = object(field_required(root, "the sidecar", "meta")?, "meta")?;
let (width, height) = read_size(field_required(meta, "meta", "size")?, "meta.size")?;
let clips = match field(meta, "frameTags") {
Some(tags) => read_tags(tags, frames.len())?,
None => Vec::new(),
};
let nine = match field(meta, "slices") {
Some(slices) => read_nine(slices, &frames)?,
None => None,
};
let sheet = Sheet {
width,
height,
frames,
clips,
nine,
sample: SampleMode::default(),
};
sheet.validate()?;
Ok(sheet)
}
pub fn load(png: &[u8], sidecar: Option<&str>, tick_hz: u32) -> Result<Loaded, LoadError> {
let image = decode_png(png)?;
let sheet = match sidecar {
Some(json) => {
let sheet = read_aseprite_json(json, tick_hz)?;
if (sheet.width, sheet.height) != (image.width, image.height) {
return Err(LoadError::SizeMismatch {
json: (sheet.width, sheet.height),
png: (image.width, image.height),
});
}
sheet
}
None => {
let sheet = Sheet {
width: image.width,
height: image.height,
frames: vec![Frame {
name: "default".to_owned(),
rect: Rect::new(0, 0, image.width, image.height),
hold: 1,
}],
clips: Vec::new(),
nine: None,
sample: SampleMode::default(),
};
sheet.validate()?;
sheet
}
};
Ok(Loaded { sheet, image })
}
#[track_caller]
pub fn load_baked(name: &str, png: &[u8], sidecar: Option<&str>, tick_hz: u32) -> Loaded {
load(png, sidecar, tick_hz)
.unwrap_or_else(|error| panic!("the baked {name} sheet did not load: {error}"))
}
fn read_frames(value: &Json, tick_hz: u32) -> Result<Vec<Frame>, LoadError> {
let entries = array(value, "frames")?;
let mut frames = Vec::with_capacity(entries.len());
for (index, entry) in entries.iter().enumerate() {
let path = format!("frames[{index}]");
let fields = object(entry, &path)?;
let name = string(
field_required(fields, &path, "filename")?,
&format!("{path}.filename"),
)?
.to_owned();
let rect = read_rect(
field_required(fields, &path, "frame")?,
&format!("{path}.frame"),
)?;
let ms = integer(
field_required(fields, &path, "duration")?,
&format!("{path}.duration"),
)?;
if ms == 0 {
return Err(LoadError::ZeroDuration { frame: name });
}
frames.push(Frame {
name,
rect,
hold: hold_ticks(ms, tick_hz),
});
}
Ok(frames)
}
fn read_tags(value: &Json, frames: usize) -> Result<Vec<Clip>, LoadError> {
let entries = array(value, "meta.frameTags")?;
let mut clips = Vec::with_capacity(entries.len());
for (index, entry) in entries.iter().enumerate() {
let path = format!("meta.frameTags[{index}]");
let fields = object(entry, &path)?;
let name = string(
field_required(fields, &path, "name")?,
&format!("{path}.name"),
)?
.to_owned();
let from = integer(
field_required(fields, &path, "from")?,
&format!("{path}.from"),
)?;
let to = integer(field_required(fields, &path, "to")?, &format!("{path}.to"))?;
if to < from {
return Err(LoadError::TagReversed {
tag: name,
from,
to,
});
}
if usize::try_from(to).is_err() || to as usize >= frames {
return Err(LoadError::TagOutOfRange {
tag: name,
from,
to,
frames,
});
}
let direction = match string(
field_required(fields, &path, "direction")?,
&format!("{path}.direction"),
)? {
"forward" => Direction::Forward,
"reverse" => Direction::Reverse,
"pingpong" => Direction::PingPong,
other => {
return Err(LoadError::UnknownDirection {
tag: name,
direction: other.to_owned(),
});
}
};
let looping = match field(fields, "repeat") {
Some(repeat) => {
string(repeat, &format!("{path}.repeat"))?;
false
}
None => true,
};
clips.push(Clip {
name,
frames: (from as usize..=to as usize).collect(),
direction,
looping,
});
}
Ok(clips)
}
fn read_nine(value: &Json, frames: &[Frame]) -> Result<Option<NineSlice>, LoadError> {
let slices = array(value, "meta.slices")?;
let Some(slice) = slices.first() else {
return Ok(None);
};
let fields = object(slice, "meta.slices[0]")?;
let keys = array(
field_required(fields, "meta.slices[0]", "keys")?,
"meta.slices[0].keys",
)?;
let Some(key) = keys.first() else {
return Ok(None);
};
let key = object(key, "meta.slices[0].keys[0]")?;
let Some(center) = field(key, "center") else {
return Ok(None);
};
let center = read_rect(center, "meta.slices[0].keys[0].center")?;
let frame = frames.first().map_or(Rect::default(), |frame| frame.rect);
NineSlice::from_center(frame, center)
.map(Some)
.ok_or(LoadError::CenterOutsideFrame { center, frame })
}
fn read_rect(value: &Json, path: &str) -> Result<Rect, LoadError> {
let fields = object(value, path)?;
Ok(Rect {
x: integer(field_required(fields, path, "x")?, &format!("{path}.x"))?,
y: integer(field_required(fields, path, "y")?, &format!("{path}.y"))?,
w: integer(field_required(fields, path, "w")?, &format!("{path}.w"))?,
h: integer(field_required(fields, path, "h")?, &format!("{path}.h"))?,
})
}
fn read_size(value: &Json, path: &str) -> Result<(u32, u32), LoadError> {
let fields = object(value, path)?;
Ok((
integer(field_required(fields, path, "w")?, &format!("{path}.w"))?,
integer(field_required(fields, path, "h")?, &format!("{path}.h"))?,
))
}
fn object<'j>(value: &'j Json, path: &str) -> Result<&'j [(String, Json)], LoadError> {
match value {
Json::Object(fields) => Ok(fields),
other => Err(LoadError::WrongType {
path: path.to_owned(),
expected: "an object",
found: other.kind(),
}),
}
}
fn array<'j>(value: &'j Json, path: &str) -> Result<&'j [Json], LoadError> {
match value {
Json::Array(items) => Ok(items),
other => Err(LoadError::WrongType {
path: path.to_owned(),
expected: "an array",
found: other.kind(),
}),
}
}
fn string<'j>(value: &'j Json, path: &str) -> Result<&'j str, LoadError> {
match value {
Json::Str(text) => Ok(text),
other => Err(LoadError::WrongType {
path: path.to_owned(),
expected: "a string",
found: other.kind(),
}),
}
}
fn integer(value: &Json, path: &str) -> Result<u32, LoadError> {
match value {
Json::Number(text) => text.parse::<u32>().map_err(|_| LoadError::NotAnInteger {
path: path.to_owned(),
found: text.clone(),
}),
other => Err(LoadError::WrongType {
path: path.to_owned(),
expected: "a number",
found: other.kind(),
}),
}
}
fn field<'j>(fields: &'j [(String, Json)], name: &str) -> Option<&'j Json> {
fields
.iter()
.find(|(key, _)| key == name)
.map(|(_, value)| value)
}
fn field_required<'j>(
fields: &'j [(String, Json)],
path: &str,
name: &'static str,
) -> Result<&'j Json, LoadError> {
field(fields, name).ok_or_else(|| LoadError::MissingField {
path: path.to_owned(),
field: name,
})
}
#[derive(Clone, Debug, PartialEq)]
enum Json {
Null,
Bool(bool),
Number(String),
Str(String),
Array(Vec<Json>),
Object(Vec<(String, Json)>),
}
impl Json {
fn kind(&self) -> &'static str {
match self {
Self::Null => "null",
Self::Bool(_) => "a boolean",
Self::Number(_) => "a number",
Self::Str(_) => "a string",
Self::Array(_) => "an array",
Self::Object(_) => "an object",
}
}
}
struct Parser<'a> {
src: &'a [u8],
pos: usize,
}
impl<'a> Parser<'a> {
fn new(text: &'a str) -> Self {
Self {
src: text.as_bytes(),
pos: 0,
}
}
fn error(&self, at: usize, expected: impl Into<String>) -> LoadError {
let (line, column) = line_column(self.src, at);
LoadError::Syntax {
line,
column,
offset: at,
expected: expected.into(),
}
}
fn peek(&self) -> Option<u8> {
self.src.get(self.pos).copied()
}
fn skip_whitespace(&mut self) {
while matches!(self.peek(), Some(b' ' | b'\t' | b'\n' | b'\r')) {
self.pos += 1;
}
}
fn text(&self, start: usize, end: usize) -> Result<&'a str, LoadError> {
core::str::from_utf8(&self.src[start..end])
.map_err(|_| self.error(start, "valid UTF-8 text"))
}
fn parse_document(mut self) -> Result<Json, LoadError> {
self.skip_whitespace();
let value = self.parse_value()?;
self.skip_whitespace();
if self.pos != self.src.len() {
return Err(self.error(self.pos, "the end of the file after the top-level value"));
}
Ok(value)
}
fn parse_value(&mut self) -> Result<Json, LoadError> {
match self.peek() {
Some(b'{') => self.parse_object(),
Some(b'[') => self.parse_array(),
Some(b'"') => self.parse_string().map(Json::Str),
Some(b't') => self.parse_keyword("true", Json::Bool(true)),
Some(b'f') => self.parse_keyword("false", Json::Bool(false)),
Some(b'n') => self.parse_keyword("null", Json::Null),
Some(b'-' | b'0'..=b'9') => self.parse_number(),
_ => Err(self.error(self.pos, "a JSON value")),
}
}
fn parse_keyword(&mut self, word: &str, value: Json) -> Result<Json, LoadError> {
if self.src[self.pos..].starts_with(word.as_bytes()) {
self.pos += word.len();
Ok(value)
} else {
Err(self.error(self.pos, format!("`{word}`")))
}
}
fn parse_object(&mut self) -> Result<Json, LoadError> {
let open = self.pos;
self.pos += 1; let mut fields = Vec::new();
self.skip_whitespace();
if self.peek() == Some(b'}') {
self.pos += 1;
return Ok(Json::Object(fields));
}
loop {
self.skip_whitespace();
let key = self.parse_string()?;
self.skip_whitespace();
if self.peek() != Some(b':') {
return Err(self.error(self.pos, "`:` after a member name"));
}
self.pos += 1;
self.skip_whitespace();
let value = self.parse_value()?;
fields.push((key, value));
self.skip_whitespace();
match self.peek() {
Some(b',') => self.pos += 1,
Some(b'}') => {
self.pos += 1;
return Ok(Json::Object(fields));
}
Some(_) => return Err(self.error(self.pos, "`,` or `}`")),
None => return Err(self.error(open, "a `}` closing this object")),
}
}
}
fn parse_array(&mut self) -> Result<Json, LoadError> {
let open = self.pos;
self.pos += 1; let mut items = Vec::new();
self.skip_whitespace();
if self.peek() == Some(b']') {
self.pos += 1;
return Ok(Json::Array(items));
}
loop {
self.skip_whitespace();
items.push(self.parse_value()?);
self.skip_whitespace();
match self.peek() {
Some(b',') => self.pos += 1,
Some(b']') => {
self.pos += 1;
return Ok(Json::Array(items));
}
Some(_) => return Err(self.error(self.pos, "`,` or `]`")),
None => return Err(self.error(open, "a `]` closing this array")),
}
}
}
fn parse_string(&mut self) -> Result<String, LoadError> {
let open = self.pos;
if self.peek() != Some(b'"') {
return Err(self.error(self.pos, "a string"));
}
self.pos += 1;
let mut out = String::new();
let mut run = self.pos;
loop {
let Some(byte) = self.peek() else {
return Err(self.error(open, "a `\"` closing this string"));
};
match byte {
b'"' => {
out.push_str(self.text(run, self.pos)?);
self.pos += 1;
return Ok(out);
}
b'\\' => {
out.push_str(self.text(run, self.pos)?);
self.pos += 1;
let ch = self.parse_escape()?;
out.push(ch);
run = self.pos;
}
0x00..=0x1f => {
return Err(self.error(
self.pos,
"an escape: a raw control character cannot appear in a JSON string",
));
}
_ => self.pos += 1,
}
}
}
fn parse_escape(&mut self) -> Result<char, LoadError> {
let at = self.pos;
let Some(byte) = self.peek() else {
return Err(self.error(at, "an escape character after `\\`"));
};
self.pos += 1;
Ok(match byte {
b'"' => '"',
b'\\' => '\\',
b'/' => '/',
b'b' => '\u{8}',
b'f' => '\u{c}',
b'n' => '\n',
b'r' => '\r',
b't' => '\t',
b'u' => {
let first = self.parse_hex4()?;
if (0xd800..0xdc00).contains(&first) {
if self.peek() != Some(b'\\') || self.src.get(self.pos + 1) != Some(&b'u') {
return Err(self.error(at, "a `\\u` low surrogate after a high surrogate"));
}
self.pos += 2;
let low = self.parse_hex4()?;
if !(0xdc00..0xe000).contains(&low) {
return Err(self.error(
at,
"a low surrogate in `\\udc00`..`\\udfff` after a high one",
));
}
let code =
0x1_0000 + ((u32::from(first) - 0xd800) << 10) + (u32::from(low) - 0xdc00);
char::from_u32(code).ok_or_else(|| self.error(at, "a Unicode scalar value"))?
} else {
char::from_u32(u32::from(first)).ok_or_else(|| {
self.error(at, "a Unicode scalar value, not a lone low surrogate")
})?
}
}
_ => return Err(self.error(at, "one of `\"`, `\\`, `/`, `b`, `f`, `n`, `r`, `t`, `u`")),
})
}
fn parse_hex4(&mut self) -> Result<u16, LoadError> {
let at = self.pos;
let mut value: u16 = 0;
for _ in 0..4 {
let Some(byte) = self.peek() else {
return Err(self.error(at, "four hex digits after `\\u`"));
};
let digit = match byte {
b'0'..=b'9' => u16::from(byte - b'0'),
b'a'..=b'f' => u16::from(byte - b'a') + 10,
b'A'..=b'F' => u16::from(byte - b'A') + 10,
_ => return Err(self.error(at, "four hex digits after `\\u`")),
};
value = value * 16 + digit;
self.pos += 1;
}
Ok(value)
}
fn parse_number(&mut self) -> Result<Json, LoadError> {
let start = self.pos;
if self.peek() == Some(b'-') {
self.pos += 1;
}
match self.peek() {
Some(b'0') => self.pos += 1,
Some(b'1'..=b'9') => {
while matches!(self.peek(), Some(b'0'..=b'9')) {
self.pos += 1;
}
}
_ => return Err(self.error(self.pos, "a digit")),
}
if self.peek() == Some(b'.') {
self.pos += 1;
if !matches!(self.peek(), Some(b'0'..=b'9')) {
return Err(self.error(self.pos, "a digit after the decimal point"));
}
while matches!(self.peek(), Some(b'0'..=b'9')) {
self.pos += 1;
}
}
if matches!(self.peek(), Some(b'e' | b'E')) {
self.pos += 1;
if matches!(self.peek(), Some(b'+' | b'-')) {
self.pos += 1;
}
if !matches!(self.peek(), Some(b'0'..=b'9')) {
return Err(self.error(self.pos, "a digit in the exponent"));
}
while matches!(self.peek(), Some(b'0'..=b'9')) {
self.pos += 1;
}
}
let text = self.text(start, self.pos)?;
Ok(Json::Number(text.to_owned()))
}
}
fn line_column(src: &[u8], offset: usize) -> (usize, usize) {
let upto = &src[..offset.min(src.len())];
let line = 1 + upto.iter().filter(|&&byte| byte == b'\n').count();
let start = upto
.iter()
.rposition(|&byte| byte == b'\n')
.map_or(0, |index| index + 1);
(line, upto.len() - start + 1)
}
#[cfg(test)]
mod tests {
use super::*;
use crcbl_store::crc32::crc32;
const SIDECAR: &str = r##"{
"frames": [
{"filename": "up", "frame": {"x": 0, "y": 0, "w": 2, "h": 3}, "rotated": false, "trimmed": false, "spriteSourceSize": {"x": 0, "y": 0, "w": 2, "h": 3}, "sourceSize": {"w": 2, "h": 3}, "duration": 100},
{"filename": "down", "frame": {"x": 2, "y": 0, "w": 2, "h": 3}, "rotated": false, "trimmed": false, "spriteSourceSize": {"x": 0, "y": 0, "w": 2, "h": 3}, "sourceSize": {"w": 2, "h": 3}, "duration": 100}
],
"meta": {
"app": "https://github.com/kryptic-sh/crcbl",
"version": "crcbl-sprite bake",
"image": "bird.png",
"format": "RGBA8888",
"size": {"w": 4, "h": 3},
"scale": "1",
"frameTags": [
{"name": "flap", "from": 0, "to": 1, "direction": "forward"}
],
"slices": [
{"name": "nine", "color": "#0000ffff", "keys": [{"frame": 0, "bounds": {"x": 0, "y": 0, "w": 2, "h": 3}, "center": {"x": 1, "y": 1, "w": 1, "h": 1}}]}
]
}
}
"##;
fn sheet() -> Sheet {
read_aseprite_json(SIDECAR, 60).expect("this sidecar is well formed")
}
#[test]
fn a_sidecar_reads_back_into_the_sheet_it_describes() {
let sheet = sheet();
assert_eq!((sheet.width, sheet.height), (4, 3));
assert_eq!(sheet.frames.len(), 2);
assert_eq!(sheet.frames[0].name, "up");
assert_eq!(sheet.frames[0].rect, Rect::new(0, 0, 2, 3));
assert_eq!(sheet.frames[1].name, "down");
assert_eq!(sheet.frames[1].rect, Rect::new(2, 0, 2, 3));
assert_eq!(sheet.frames[0].hold, 6);
assert_eq!(sheet.frames[1].hold, 6);
assert_eq!(sheet.clips.len(), 1);
let clip = &sheet.clips[0];
assert_eq!(clip.name, "flap");
assert_eq!(clip.frames, vec![0, 1]);
assert_eq!(clip.direction, Direction::Forward);
assert!(clip.looping, "no `repeat` field is Aseprite's forever");
assert_eq!(sheet.nine, Some(NineSlice::new(1, 0, 1, 1)));
}
#[test]
fn durations_are_read_at_the_tick_rate_they_are_given() {
assert_eq!(read_aseprite_json(SIDECAR, 30).unwrap().frames[0].hold, 3);
assert_eq!(read_aseprite_json(SIDECAR, 120).unwrap().frames[0].hold, 12);
assert_eq!(read_aseprite_json(SIDECAR, 0), Err(LoadError::ZeroTickRate));
}
#[test]
fn a_hold_is_never_rounded_down_to_nothing() {
assert_eq!(hold_ticks(1, 30), 1, "1 ms at 30 Hz is under a tick");
assert_eq!(hold_ticks(17, 60), 1);
assert_eq!(hold_ticks(100, 60), 6);
assert_eq!(hold_ticks(1000, 30), 30);
assert_eq!(hold_ticks(u32::MAX, u32::MAX), u32::MAX, "and saturates");
}
#[test]
fn a_repeat_field_turns_a_clip_into_a_one_shot() {
let json = SIDECAR.replace(
r#""direction": "forward""#,
r#""direction": "forward", "repeat": "1""#,
);
let sheet = read_aseprite_json(&json, 60).expect("this parses");
assert_eq!(sheet.clips.len(), 1);
assert!(!sheet.clips[0].looping);
}
#[test]
fn every_direction_the_schema_spells_is_understood() {
for (text, expected) in [
("forward", Direction::Forward),
("reverse", Direction::Reverse),
("pingpong", Direction::PingPong),
] {
let json = SIDECAR.replace(
r#""direction": "forward""#,
&format!(r#""direction": "{text}""#),
);
let sheet = read_aseprite_json(&json, 60).expect("this parses");
assert_eq!(sheet.clips.len(), 1);
assert_eq!(sheet.clips[0].direction, expected);
}
}
#[test]
fn a_truncated_sidecar_names_what_was_left_unclosed() {
let cases: [(&str, u8, &str); 4] = [
("", 0, "a JSON value"),
(r#"{"frames": [1"#, b'[', "]"),
(r#"{"frames": [{"filename": "up""#, b'{', "}"),
(r#"{"frames": [{"filename": "up"#, b'"', "\""),
];
for (json, opener, wanted) in cases {
let error = match read_aseprite_json(json, 60) {
Err(error) => error,
Ok(sheet) => panic!("`{json}` is truncated, and parsed to {sheet:?}"),
};
let LoadError::Syntax {
line,
column,
offset,
expected,
} = error
else {
panic!("`{json}` is a syntax error, and gave {error:?}");
};
assert!(
expected.contains(wanted),
"`{json}`: wanted {wanted} in the message, got `{expected}`"
);
assert_eq!((line, column), (1, offset + 1), "all on one line");
if opener != 0 {
assert_eq!(
json.as_bytes()[offset],
opener,
"`{json}`: the offset should point at the unclosed `{}`, and points at byte \
{offset}",
opener as char
);
}
}
let half = &SIDECAR[..SIDECAR.len() / 2];
let error = read_aseprite_json(half, 60).expect_err("this is cut in half");
let LoadError::Syntax { line, .. } = error else {
panic!("a truncated sidecar is a syntax error, got {error:?}");
};
assert!(line > 1, "the cut is past the first line, and says so");
}
#[test]
fn a_frame_missing_a_field_names_the_frame_and_the_field() {
let json = SIDECAR.replace(r#""filename": "down", "#, "");
assert_eq!(
read_aseprite_json(&json, 60),
Err(LoadError::MissingField {
path: "frames[1]".to_owned(),
field: "filename",
})
);
let json = SIDECAR.replace(
r#", "duration": 100}
]"#,
"}\n ]",
);
assert_eq!(
read_aseprite_json(&json, 60),
Err(LoadError::MissingField {
path: "frames[1]".to_owned(),
field: "duration",
})
);
}
#[test]
fn a_tag_that_runs_past_the_frames_is_refused() {
let json = SIDECAR.replace(r#""from": 0, "to": 1"#, r#""from": 7, "to": 9"#);
assert_eq!(
read_aseprite_json(&json, 60),
Err(LoadError::TagOutOfRange {
tag: "flap".to_owned(),
from: 7,
to: 9,
frames: 2,
})
);
let json = SIDECAR.replace(r#""from": 0, "to": 1"#, r#""from": 1, "to": 0"#);
assert_eq!(
read_aseprite_json(&json, 60),
Err(LoadError::TagReversed {
tag: "flap".to_owned(),
from: 1,
to: 0,
})
);
}
#[test]
fn a_zero_duration_is_refused_rather_than_clamped() {
let json = SIDECAR.replacen(r#""duration": 100"#, r#""duration": 0"#, 1);
assert_eq!(
read_aseprite_json(&json, 60),
Err(LoadError::ZeroDuration {
frame: "up".to_owned()
})
);
}
#[test]
fn a_centre_that_does_not_fit_its_frame_is_refused() {
let json = SIDECAR.replace(
r#""center": {"x": 1, "y": 1, "w": 1, "h": 1}"#,
r#""center": {"x": 1, "y": 1, "w": 4, "h": 1}"#,
);
assert_eq!(
read_aseprite_json(&json, 60),
Err(LoadError::CenterOutsideFrame {
center: Rect::new(1, 1, 4, 1),
frame: Rect::new(0, 0, 2, 3),
})
);
}
#[test]
fn a_field_of_the_wrong_kind_says_which_and_what_it_wanted() {
let json = SIDECAR.replace(r#""filename": "up""#, r#""filename": 7"#);
assert_eq!(
read_aseprite_json(&json, 60),
Err(LoadError::WrongType {
path: "frames[0].filename".to_owned(),
expected: "a string",
found: "a number",
})
);
let json = SIDECAR
.replace(r#""frames": ["#, r#""frames": {"0": ["#)
.replace(
r#" ],
"meta""#,
r#" ]},
"meta""#,
);
assert!(matches!(
read_aseprite_json(&json, 60),
Err(LoadError::WrongType {
expected: "an array",
..
})
));
}
#[test]
fn a_number_that_is_not_a_pixel_count_is_refused() {
let json = SIDECAR.replace(r#""w": 4, "h": 3"#, r#""w": 4.5, "h": 3"#);
assert_eq!(
read_aseprite_json(&json, 60),
Err(LoadError::NotAnInteger {
path: "meta.size.w".to_owned(),
found: "4.5".to_owned(),
})
);
}
#[test]
fn a_sheet_the_schema_allows_but_a_sheet_rule_forbids_is_refused() {
let json = SIDECAR.replace(
r#""frame": {"x": 2, "y": 0, "w": 2, "h": 3}"#,
r#""frame": {"x": 3, "y": 0, "w": 2, "h": 3}"#,
);
assert!(matches!(
read_aseprite_json(&json, 60),
Err(LoadError::Sheet(SheetError::FrameOutsideSheet { .. }))
));
}
#[test]
fn the_escapes_the_baker_can_emit_are_all_understood() {
let json = SIDECAR.replace(
r#""filename": "up""#,
r#""filename": "a \"quoted\\name\"\twith\ncontrol\u0001chars\u00e9\ud83d\ude00""#,
);
let sheet = read_aseprite_json(&json, 60).expect("this parses");
assert_eq!(sheet.frames.len(), 2);
assert_eq!(
sheet.frames[0].name,
"a \"quoted\\name\"\twith\ncontrol\u{1}chars\u{e9}\u{1f600}"
);
}
#[test]
fn arbitrary_whitespace_between_tokens_is_accepted() {
let json = " {\n\t\"frames\"\r\n:\n[ { \"filename\" : \"a\" , \"frame\" : \
{ \"x\":0 , \"y\":0 , \"w\":1 , \"h\":1 } , \"duration\" : 100 } ] , \
\"meta\" : { \"size\" : { \"w\" : 1 , \"h\" : 1 } } } \n";
let sheet = read_aseprite_json(json, 60).expect("whitespace is not an error");
assert_eq!(sheet.frames.len(), 1);
assert_eq!(sheet.frames[0].rect, Rect::new(0, 0, 1, 1));
}
#[test]
fn every_png_colour_type_normalises_to_the_same_rgba8() {
const GREYS: [u8; 4] = [0x00, 0x40, 0x80, 0xff];
let expected: Vec<u8> = GREYS.iter().flat_map(|&g| [g, g, g, 0xff]).collect();
let rgba: Vec<u8> = expected.clone();
let rgb: Vec<u8> = GREYS.iter().flat_map(|&g| [g, g, g]).collect();
let gray: Vec<u8> = GREYS.to_vec();
let gray_alpha: Vec<u8> = GREYS.iter().flat_map(|&g| [g, 0xff]).collect();
let palette: Vec<u8> = GREYS.iter().flat_map(|&g| [g, g, g]).collect();
let indices: Vec<u8> = vec![0, 1, 2, 3];
type Case<'a> = (&'a str, png::ColorType, &'a [u8], Option<&'a [u8]>);
let cases: [Case<'_>; 5] = [
("rgba", png::ColorType::Rgba, &rgba, None),
("rgb", png::ColorType::Rgb, &rgb, None),
("grayscale", png::ColorType::Grayscale, &gray, None),
(
"grayscale+alpha",
png::ColorType::GrayscaleAlpha,
&gray_alpha,
None,
),
("palette", png::ColorType::Indexed, &indices, Some(&palette)),
];
for (name, color, data, plte) in cases {
let mut bytes = Vec::new();
{
let mut encoder = png::Encoder::new(&mut bytes, 2, 2);
encoder.set_color(color);
encoder.set_depth(png::BitDepth::Eight);
if let Some(plte) = plte {
encoder.set_palette(plte.to_vec());
}
let mut writer = encoder.write_header().expect("a header");
writer.write_image_data(data).expect("the rows");
writer.finish().expect("the end");
}
let image = decode_png(&bytes).unwrap_or_else(|e| panic!("{name}: {e}"));
assert_eq!((image.width, image.height), (2, 2), "{name}");
assert_eq!(image.pixels, expected, "{name} did not normalise");
}
}
#[test]
fn a_grayscale_alpha_image_keeps_its_alpha_and_greys_all_three_channels() {
let samples: [u8; 8] = [0x00, 0xff, 0x40, 0x00, 0x80, 0x33, 0xff, 0x7f];
let mut bytes = Vec::new();
{
let mut encoder = png::Encoder::new(&mut bytes, 2, 2);
encoder.set_color(png::ColorType::GrayscaleAlpha);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header().expect("a header");
writer.write_image_data(&samples).expect("the rows");
writer.finish().expect("the end");
}
let image = decode_png(&bytes).expect("grayscale+alpha decodes");
assert_eq!(
image.pixels,
vec![
0x00, 0x00, 0x00, 0xff, 0x40, 0x40, 0x40, 0x00, 0x80, 0x80, 0x80, 0x33, 0xff, 0xff, 0xff, 0x7f,
]
);
}
#[test]
fn sixteen_bit_input_is_stripped_to_eight() {
let mut bytes = Vec::new();
{
let mut encoder = png::Encoder::new(&mut bytes, 1, 1);
encoder.set_color(png::ColorType::Rgb);
encoder.set_depth(png::BitDepth::Sixteen);
let mut writer = encoder.write_header().expect("a header");
writer
.write_image_data(&[0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc])
.expect("the row");
writer.finish().expect("the end");
}
let image = decode_png(&bytes).expect("16-bit is stripped, not refused");
assert_eq!(image.pixels, vec![0x12, 0x56, 0x9a, 0xff]);
}
#[test]
fn bytes_that_are_not_a_png_are_refused() {
let error = decode_png(b"not a png at all").expect_err("this is not a PNG");
assert!(matches!(error, LoadError::Png(_)), "{error:?}");
}
fn png_bytes(width: u32, height: u32) -> Vec<u8> {
let mut bytes = Vec::new();
{
let mut encoder = png::Encoder::new(&mut bytes, width, height);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header().expect("a header");
writer
.write_image_data(&vec![0x7f; (width * height * 4) as usize])
.expect("the rows");
writer.finish().expect("the end");
}
bytes
}
fn png_with_declared_size(width: u32, height: u32) -> Vec<u8> {
let mut bytes = png_bytes(1, 1);
bytes[16..20].copy_from_slice(&width.to_be_bytes());
bytes[20..24].copy_from_slice(&height.to_be_bytes());
let crc = crc32(&bytes[12..29]);
bytes[29..33].copy_from_slice(&crc.to_be_bytes());
bytes
}
#[test]
fn a_png_that_declares_a_huge_size_is_refused_before_allocating() {
let hostile = png_with_declared_size(65_536, 65_536);
let error = decode_png(&hostile).expect_err("a multi-gigabyte claim must be refused");
assert!(
matches!(&error, LoadError::Png(message) if message.contains("declares more than")),
"the refusal must name the pixel-count guard: {error:?}"
);
let fine = decode_png(&png_bytes(6, 3)).expect("a sane PNG still decodes");
assert_eq!((fine.width, fine.height), (6, 3));
}
#[test]
fn a_frame_is_cut_out_of_the_strip_row_by_row() {
let loaded = Loaded {
sheet: sheet(),
image: Rgba8 {
width: 4,
height: 3,
pixels: (0..4 * 3)
.flat_map(|texel: u8| [texel, texel, texel, 0xff])
.collect(),
},
};
let red = |pixels: Vec<u8>| pixels.chunks(4).map(|rgba| rgba[0]).collect::<Vec<_>>();
assert_eq!(
red(loaded.frame_pixels(0).expect("frame 0")),
[0, 1, 4, 5, 8, 9]
);
assert_eq!(
red(loaded.frame_pixels(1).expect("frame 1")),
[2, 3, 6, 7, 10, 11]
);
assert_eq!(loaded.frame_pixels(2), None, "there is no third frame");
}
#[test]
fn a_sheet_with_no_sidecar_is_one_frame_covering_the_image() {
let loaded = load(&png_bytes(6, 3), None, 60).expect("a still sprite loads");
assert_eq!(loaded.sheet.frames.len(), 1);
assert_eq!(loaded.sheet.frames[0].rect, Rect::new(0, 0, 6, 3));
assert_eq!(loaded.sheet.frames[0].hold, 1);
assert_eq!(loaded.sheet.frames[0].name, "default");
assert!(loaded.sheet.clips.is_empty());
assert_eq!(loaded.sheet.nine, None);
assert_eq!((loaded.image.width, loaded.image.height), (6, 3));
assert_eq!(loaded.image.pixels.len(), 6 * 3 * 4);
}
#[test]
fn a_sidecar_that_does_not_match_its_png_is_refused() {
assert_eq!(
load(&png_bytes(8, 8), Some(SIDECAR), 60),
Err(LoadError::SizeMismatch {
json: (4, 3),
png: (8, 8),
})
);
let loaded = load(&png_bytes(4, 3), Some(SIDECAR), 60).expect("these agree");
assert_eq!(loaded.sheet.frames.len(), 2);
}
}