mirror of
https://github.com/storytold/AniCraft.git
synced 2026-11-21 10:01:55 +01:00
498 lines
22 KiB
Rust
498 lines
22 KiB
Rust
use super::*;
|
||
use crate::Session;
|
||
use anicraft_doc::sample;
|
||
use serde_json::json;
|
||
use std::path::Path;
|
||
|
||
fn run(s: &mut Session, id: &str, p: Value) -> Value {
|
||
s.execute(id, &p).unwrap_or_else(|e| panic!("{id}: {e}"))
|
||
}
|
||
|
||
fn tmp(name: &str) -> PathBuf {
|
||
let dir = std::env::temp_dir().join(format!("anicraft-anim-{}-{name}", std::process::id()));
|
||
std::fs::create_dir_all(&dir).unwrap();
|
||
dir
|
||
}
|
||
|
||
/// What the CPU renderer draws for movie frames `from..=to`, as straight RGBA8.
|
||
fn renders(doc: &anicraft_doc::Document, from: u32, to: u32, scale: f32, background: bool) -> Vec<Vec<u8>> {
|
||
let mut view = View::stage(doc, scale);
|
||
if !background {
|
||
view.background = None;
|
||
}
|
||
(from..=to).map(|f| render(&anicraft_doc::evaluate(doc, f), &view).unwrap().0.to_rgba8()).collect()
|
||
}
|
||
|
||
/// An APNG decoded as a player shows it (the APNG specification's compositing): each frame's
|
||
/// canvas, its delay (num, den), its region and blend op, and the play count.
|
||
struct ApngFrame {
|
||
canvas: Vec<u8>,
|
||
delay: (u16, u16),
|
||
rect: Rect,
|
||
over: bool,
|
||
}
|
||
|
||
struct Apng {
|
||
frames: Vec<ApngFrame>,
|
||
plays: u32,
|
||
size: (u32, u32),
|
||
}
|
||
|
||
fn decode_apng(bytes: &[u8]) -> Apng {
|
||
assert_eq!(&bytes[..8], b"\x89PNG\r\n\x1a\n");
|
||
let mut pos = 8usize;
|
||
let mut ihdr = [0u8; 13];
|
||
let mut num_frames = 0u32;
|
||
let mut plays = 0u32;
|
||
struct RawFc {
|
||
w: u32,
|
||
h: u32,
|
||
x: u32,
|
||
y: u32,
|
||
delay_num: u16,
|
||
delay_den: u16,
|
||
dispose: u8,
|
||
blend: u8,
|
||
idat: Vec<u8>,
|
||
}
|
||
let mut raw_frames: Vec<RawFc> = Vec::new();
|
||
while pos + 12 <= bytes.len() {
|
||
let len = u32::from_be_bytes(bytes[pos..pos + 4].try_into().unwrap()) as usize;
|
||
let kind: [u8; 4] = bytes[pos + 4..pos + 8].try_into().unwrap();
|
||
let data = &bytes[pos + 8..pos + 8 + len];
|
||
pos += 12 + len;
|
||
match &kind {
|
||
b"IHDR" => ihdr.copy_from_slice(data),
|
||
b"acTL" => {
|
||
num_frames = u32::from_be_bytes(data[0..4].try_into().unwrap());
|
||
plays = u32::from_be_bytes(data[4..8].try_into().unwrap());
|
||
}
|
||
b"fcTL" => {
|
||
raw_frames.push(RawFc {
|
||
w: u32::from_be_bytes(data[4..8].try_into().unwrap()),
|
||
h: u32::from_be_bytes(data[8..12].try_into().unwrap()),
|
||
x: u32::from_be_bytes(data[12..16].try_into().unwrap()),
|
||
y: u32::from_be_bytes(data[16..20].try_into().unwrap()),
|
||
delay_num: u16::from_be_bytes(data[20..22].try_into().unwrap()),
|
||
delay_den: u16::from_be_bytes(data[22..24].try_into().unwrap()),
|
||
dispose: data[24],
|
||
blend: data[25],
|
||
idat: Vec::new(),
|
||
});
|
||
}
|
||
b"IDAT" => {
|
||
if let Some(f) = raw_frames.last_mut() {
|
||
f.idat.extend_from_slice(data);
|
||
}
|
||
}
|
||
b"fdAT" => {
|
||
if let Some(f) = raw_frames.last_mut() {
|
||
f.idat.extend_from_slice(&data[4..]);
|
||
}
|
||
}
|
||
b"IEND" => break,
|
||
_ => {}
|
||
}
|
||
}
|
||
assert_eq!(raw_frames.len(), num_frames as usize);
|
||
let w = u32::from_be_bytes(ihdr[0..4].try_into().unwrap());
|
||
let h = u32::from_be_bytes(ihdr[4..8].try_into().unwrap());
|
||
assert_eq!((ihdr[8], ihdr[9]), (8, 6), "8-bit RGBA");
|
||
let mut canvas = vec![0u8; (w * h * 4) as usize];
|
||
let mut frames = Vec::new();
|
||
let push_chunk = |out: &mut Vec<u8>, tag: &[u8; 4], payload: &[u8]| {
|
||
out.extend_from_slice(&(payload.len() as u32).to_be_bytes());
|
||
out.extend_from_slice(tag);
|
||
out.extend_from_slice(payload);
|
||
let mut crc_buf = Vec::with_capacity(4 + payload.len());
|
||
crc_buf.extend_from_slice(tag);
|
||
crc_buf.extend_from_slice(payload);
|
||
out.extend_from_slice(&anicraft_xfl::zip::crc32(&crc_buf).to_be_bytes());
|
||
};
|
||
for fc in raw_frames {
|
||
assert_eq!(fc.dispose, 0, "DisposeOp::None");
|
||
let over = fc.blend == 1;
|
||
let r = Rect { x: fc.x, y: fc.y, w: fc.w, h: fc.h };
|
||
assert!(r.x + r.w <= w && r.y + r.h <= h);
|
||
let mut frame_png = Vec::from(&b"\x89PNG\r\n\x1a\n"[..]);
|
||
let mut f_ihdr = ihdr;
|
||
f_ihdr[0..4].copy_from_slice(&r.w.to_be_bytes());
|
||
f_ihdr[4..8].copy_from_slice(&r.h.to_be_bytes());
|
||
push_chunk(&mut frame_png, b"IHDR", &f_ihdr);
|
||
push_chunk(&mut frame_png, b"IDAT", &fc.idat);
|
||
push_chunk(&mut frame_png, b"IEND", &[]);
|
||
let img = craft_image_codecs::decode_as(craft_image_codecs::Format::Png, &frame_png).unwrap();
|
||
assert_eq!(img.dimensions(), (r.w, r.h));
|
||
let buf = img.to_rgba8();
|
||
for y in 0..r.h {
|
||
for x in 0..r.w {
|
||
let s = ((y * r.w + x) * 4) as usize;
|
||
let d = (((r.y + y) * w + r.x + x) * 4) as usize;
|
||
let src = &buf[s..s + 4];
|
||
if over {
|
||
// Exact for the pixels the exporter blends: transparent or opaque.
|
||
assert!(src[3] == 0 || src[3] == 255, "an over-blended pixel is neither transparent nor opaque");
|
||
if src[3] == 255 {
|
||
canvas[d..d + 4].copy_from_slice(src);
|
||
}
|
||
} else {
|
||
canvas[d..d + 4].copy_from_slice(src);
|
||
}
|
||
}
|
||
}
|
||
frames.push(ApngFrame { canvas: canvas.clone(), delay: (fc.delay_num, fc.delay_den), rect: r, over });
|
||
}
|
||
Apng { frames, plays, size: (w, h) }
|
||
}
|
||
|
||
/// The frames an APNG shows, one per movie frame at `ratio` fps (each delay is a whole number
|
||
/// of movie frames, exactly).
|
||
fn apng_movie(a: &Apng, (p, q): (u64, u64)) -> Vec<Vec<u8>> {
|
||
let mut out = Vec::new();
|
||
for f in &a.frames {
|
||
let (num, den) = (u64::from(f.delay.0), u64::from(f.delay.1));
|
||
// num/den s = k·q/p s
|
||
assert_eq!((num * p) % (den * q), 0, "a delay of {num}/{den} s is not a whole number of frames at {p}/{q} fps");
|
||
for _ in 0..num * p / (den * q) {
|
||
out.push(f.canvas.clone());
|
||
}
|
||
}
|
||
out
|
||
}
|
||
|
||
/// A WebP decoded as a player shows it: each frame's canvas and duration.
|
||
/// An animated WebP's frames (canvas, milliseconds), loop count, size and whether it has alpha.
|
||
struct Webp {
|
||
frames: Vec<(Vec<u8>, u32)>,
|
||
loop_count: u16,
|
||
size: (u32, u32),
|
||
alpha: bool,
|
||
}
|
||
|
||
fn decode_webp(bytes: &[u8]) -> Webp {
|
||
let info = craft_webp::scan(bytes, 1024).unwrap();
|
||
assert!(!info.frames.is_empty());
|
||
let decoded = craft_image_codecs::decode_webp_frames(bytes, &craft_image_codecs::DecodeOptions::default()).unwrap();
|
||
let frames = decoded.into_iter().map(|f| (f.image.to_rgba8(), f.delay_ms)).collect();
|
||
Webp { frames, loop_count: info.loop_count.unwrap_or(0), size: (info.width, info.height), alpha: info.alpha }
|
||
}
|
||
|
||
/// The frames a WebP shows, one per movie frame: the frame showing when each starts.
|
||
fn webp_movie(frames: &[(Vec<u8>, u32)], n: u64, ratio: (u64, u64)) -> Vec<Vec<u8>> {
|
||
let mut starts = Vec::new();
|
||
let mut t = 0u64;
|
||
for (_, ms) in frames {
|
||
starts.push(t);
|
||
t += u64::from(*ms);
|
||
}
|
||
assert_eq!(t, start_ms(n, ratio), "the running time is exact to the millisecond");
|
||
(0..n).map(|f| {
|
||
let at = start_ms(f, ratio);
|
||
let i = starts.iter().rposition(|s| *s <= at).unwrap();
|
||
frames[i].0.clone()
|
||
})
|
||
.collect()
|
||
}
|
||
|
||
fn same(got: &[Vec<u8>], want: &[Vec<u8>], what: &str) {
|
||
assert_eq!(got.len(), want.len(), "{what}: frame count");
|
||
for (k, (g, w)) in got.iter().zip(want).enumerate() {
|
||
assert!(g == w, "{what}: frame {} differs from the CPU renderer", k + 1);
|
||
}
|
||
}
|
||
|
||
/// A ball that holds still for 10 frames, jumps, and holds 6 more: 16 frames, 2 pictures.
|
||
fn holds() -> Session {
|
||
let mut s = Session::default();
|
||
run(&mut s, "draw.ellipse", json!({"x": 40, "y": 40, "w": 60, "h": 60}));
|
||
run(&mut s, "timeline.goto", json!({"frame": 10}));
|
||
run(&mut s, "timeline.insert_keyframe", json!({}));
|
||
run(&mut s, "edit.select_all", json!({}));
|
||
run(&mut s, "element.translate", json!({"dx": 200, "dy": 30}));
|
||
run(&mut s, "timeline.goto", json!({"frame": 15}));
|
||
run(&mut s, "timeline.insert_frame", json!({}));
|
||
assert_eq!(s.frame_count(), 16);
|
||
s
|
||
}
|
||
|
||
#[test]
|
||
fn frame_rates_are_exact_fractions() {
|
||
assert_eq!(fps_ratio(24.0), (24, 1));
|
||
assert_eq!(fps_ratio(29.97), (2997, 100));
|
||
assert_eq!(fps_ratio(23.976), (2997, 125));
|
||
assert_eq!(fps_ratio(12.5), (25, 2));
|
||
assert_eq!(fps_ratio(f32::NAN), (24, 1));
|
||
assert_eq!(fps_ratio(-3.0), (24, 1));
|
||
// Millisecond starts round from the exact time, so the error never adds up.
|
||
assert_eq!(start_ms(24, (24, 1)), 1000);
|
||
assert_eq!(start_ms(1, (30, 1)), 33);
|
||
assert_eq!(start_ms(2, (30, 1)), 67);
|
||
assert_eq!(start_ms(2997, (2997, 100)), 100_000);
|
||
assert_eq!(webp_durations(0, 3, (30, 1)), vec![100]);
|
||
let total: u32 = (0..30).flat_map(|i| webp_durations(i, 1, (30, 1))).sum();
|
||
assert_eq!(total, 1000);
|
||
// APNG delays are fractions of the frame rate, reduced.
|
||
assert_eq!(apng_delays(0, 1, (24, 1)), vec![(1, 24)]);
|
||
assert_eq!(apng_delays(0, 6, (24, 1)), vec![(1, 4)]);
|
||
assert_eq!(apng_delays(5, 2, (2997, 100)), vec![(200, 2997)]);
|
||
// Longer than one fcTL holds: more pieces, adding up exactly.
|
||
let long = apng_delays(0, 200_000, (2997, 100));
|
||
assert!(long.len() > 1);
|
||
let frames: u64 = long.iter().map(|&(n, d)| u64::from(n) * 2997 / (u64::from(d) * 100)).sum();
|
||
assert_eq!(frames, 200_000);
|
||
// A rate with a huge denominator falls back to milliseconds, still adding up.
|
||
let odd = (100_003, 1000);
|
||
let ms: u64 = apng_delays(0, 1000, odd).iter().map(|&(n, d)| {
|
||
assert_eq!(d, 1000);
|
||
u64::from(n)
|
||
}).sum();
|
||
assert_eq!(ms, start_ms(1000, odd));
|
||
let hours = webp_durations(0, 100_000, (1, 1));
|
||
assert_eq!(hours.iter().map(|&m| u64::from(m)).sum::<u64>(), 100_000_000);
|
||
assert!(hours.iter().all(|&m| u64::from(m) <= MAX_WEBP_MS) && hours.len() > 1);
|
||
}
|
||
|
||
#[test]
|
||
fn the_changed_rectangle_is_tight() {
|
||
let (w, h) = (8u32, 6u32);
|
||
let a = vec![0u8; (w * h * 4) as usize];
|
||
assert_eq!(changed(&a, &a, w, h), None);
|
||
let mut b = a.clone();
|
||
b[((2 * w + 3) * 4 + 1) as usize] = 9; // (3, 2)
|
||
b[((4 * w + 6) * 4 + 3) as usize] = 9; // (6, 4)
|
||
assert_eq!(changed(&a, &b, w, h), Some(Rect { x: 3, y: 2, w: 4, h: 3 }));
|
||
// WebP regions start on even pixels; APNG's stay tight.
|
||
let (r, over, px) = region(Kind::Webp, Some(&a), &b, w, h);
|
||
assert_eq!((r, over, px.len()), (Rect { x: 2, y: 2, w: 5, h: 3 }, false, 5 * 3 * 4));
|
||
let (r, over, _) = region(Kind::Apng, Some(&a), &b, w, h);
|
||
assert_eq!((r, over), (Rect { x: 3, y: 2, w: 4, h: 3 }, false), "semi-transparent changes replace");
|
||
let mut c = a.clone();
|
||
c[((2 * w + 3) * 4) as usize..((2 * w + 3) * 4 + 4) as usize].copy_from_slice(&[1, 2, 3, 255]);
|
||
let (r, over, px) = region(Kind::Apng, Some(&a), &c, w, h);
|
||
assert_eq!((r, over, px), (Rect { x: 3, y: 2, w: 1, h: 1 }, true, vec![1, 2, 3, 255]), "opaque changes blend over");
|
||
}
|
||
|
||
#[test]
|
||
fn apng_frames_match_the_renderer_exactly() {
|
||
let dir = tmp("apng");
|
||
let mut s = Session::new(sample::demo());
|
||
for background in [false, true] {
|
||
let path = dir.join(format!("walk-{background}.png"));
|
||
let v = run(&mut s, "file.export_apng", json!({"path": path.display().to_string(), "scale": 0.25, "to": 23, "background": background}));
|
||
assert_eq!(v["frames"], 24);
|
||
let bytes = std::fs::read(&path).unwrap();
|
||
assert_eq!(v["bytes"], bytes.len());
|
||
let a = decode_apng(&bytes);
|
||
assert_eq!(a.plays, 0, "loops forever by default");
|
||
let want = renders(&s.doc, 0, 23, 0.25, background);
|
||
assert_eq!(a.size, (320, 180));
|
||
same(&apng_movie(&a, fps_ratio(s.doc.stage.fps)), &want, "APNG");
|
||
// Frames after the first cover only what changed.
|
||
assert!(a.frames.iter().skip(1).any(|f| f.rect.w * f.rect.h < 320 * 180), "dirty rectangles");
|
||
if background {
|
||
assert!(a.frames.iter().skip(1).all(|f| f.over), "opaque frames blend over");
|
||
}
|
||
// The default image is the first frame, and a plain PNG reader sees it.
|
||
assert_eq!((a.frames[0].rect.w, a.frames[0].rect.h, a.frames[0].rect.x, a.frames[0].rect.y), (320, 180, 0, 0));
|
||
}
|
||
let _ = std::fs::remove_dir_all(&dir);
|
||
}
|
||
|
||
#[test]
|
||
fn webp_frames_match_the_renderer_exactly() {
|
||
let dir = tmp("webp");
|
||
let mut s = Session::new(sample::demo());
|
||
for background in [false, true] {
|
||
let path = dir.join(format!("walk-{background}.webp"));
|
||
let v = run(&mut s, "file.export_webp", json!({"path": path.display().to_string(), "scale": 0.25, "from": 4, "to": 27, "background": background}));
|
||
assert_eq!(v["frames"], 24);
|
||
let bytes = std::fs::read(&path).unwrap();
|
||
assert!(bytes.starts_with(b"RIFF") && &bytes[8..12] == b"WEBP");
|
||
assert_eq!(u32::from_le_bytes(bytes[4..8].try_into().unwrap()) as usize + 8, bytes.len());
|
||
let Webp { frames, loop_count, size, alpha } = decode_webp(&bytes);
|
||
assert_eq!((size, alpha), ((320, 180), !background));
|
||
assert_eq!(loop_count, 0);
|
||
let want = renders(&s.doc, 4, 27, 0.25, background);
|
||
same(&webp_movie(&frames, 24, fps_ratio(s.doc.stage.fps)), &want, "WebP");
|
||
}
|
||
let _ = std::fs::remove_dir_all(&dir);
|
||
}
|
||
|
||
#[test]
|
||
fn identical_frames_merge_into_longer_delays() {
|
||
let dir = tmp("holds");
|
||
let mut s = holds();
|
||
let ratio = fps_ratio(s.doc.stage.fps);
|
||
let want = renders(&s.doc, 0, 15, 1.0, false);
|
||
let png = dir.join("holds.png");
|
||
let v = run(&mut s, "file.export_apng", json!({"path": png.display().to_string(), "loop": 3}));
|
||
assert_eq!((v["frames"].as_u64(), v["stored_frames"].as_u64(), v["loop"].as_u64()), (Some(16), Some(2), Some(3)));
|
||
let a = decode_apng(&std::fs::read(&png).unwrap());
|
||
assert_eq!(a.frames.len(), 2);
|
||
assert_eq!(a.plays, 3);
|
||
let fps = s.doc.stage.fps;
|
||
assert_eq!(a.frames[0].delay, apng_delays(0, 10, ratio)[0], "10 frames at {fps} fps");
|
||
same(&apng_movie(&a, ratio), &want, "held APNG");
|
||
let webp = dir.join("holds.webp");
|
||
let v = run(&mut s, "file.export_webp", json!({"path": webp.display().to_string(), "loop": false}));
|
||
assert_eq!(v["stored_frames"], 2);
|
||
let Webp { frames, loop_count, .. } = decode_webp(&std::fs::read(&webp).unwrap());
|
||
assert_eq!(frames.len(), 2);
|
||
assert_eq!(loop_count, 1);
|
||
same(&webp_movie(&frames, 16, ratio), &want, "held WebP");
|
||
let _ = std::fs::remove_dir_all(&dir);
|
||
}
|
||
|
||
#[test]
|
||
fn bad_params_are_refused_and_overwrites_confirm() {
|
||
let dir = tmp("params");
|
||
let mut s = holds();
|
||
let at = |n: &str| dir.join(n).display().to_string();
|
||
for id in ["file.export_apng", "file.export_webp"] {
|
||
for bad in [json!({"loop": -1}), json!({"loop": 70_000}), json!({"loop": "yes"}), json!({"scale": 0}), json!({"from": 9, "to": 2}), json!({"background": 3})] {
|
||
let mut p = bad.clone();
|
||
p["path"] = json!(at("x.out"));
|
||
assert!(s.execute(id, &p).is_err(), "{id} {bad}");
|
||
}
|
||
assert!(s.execute(id, &json!({"path": at("big.out"), "scale": 8})).is_err(), "{id}: too large");
|
||
assert!(!Path::new(&at("x.out")).exists() && !Path::new(&at("big.out")).exists());
|
||
let path = at(&format!("once-{id}.out"));
|
||
run(&mut s, id, json!({"path": path, "to": 1}));
|
||
assert!(s.execute(id, &json!({"path": path})).is_err(), "{id}: replacing a file asks first");
|
||
run(&mut s, id, json!({"path": path, "confirm": true}));
|
||
}
|
||
let _ = std::fs::remove_dir_all(&dir);
|
||
}
|
||
|
||
#[test]
|
||
fn a_cancelled_publish_stops_the_export() {
|
||
let mut s = Session::new(sample::demo());
|
||
let job = std::sync::Arc::new(crate::publish::Control::default());
|
||
job.cancel();
|
||
s.job = Some(job);
|
||
let dir = tmp("cancel");
|
||
let path = dir.join("walk.webp");
|
||
let e = s.execute("file.export_webp", &json!({"path": path.display().to_string(), "scale": 0.1})).unwrap_err();
|
||
assert!(matches!(e, EngineError::Cancelled), "{e}");
|
||
assert!(!path.exists());
|
||
let _ = std::fs::remove_dir_all(&dir);
|
||
}
|
||
|
||
#[test]
|
||
#[ignore = "a measurement: cargo test --release -p anicraft-engine anim::tests::sizes -- --ignored --nocapture"]
|
||
fn sizes() {
|
||
// docs/performance.md › Animated exports: the sample, every frame, at full size and half.
|
||
let dir = tmp("sizes");
|
||
for scale in [1.0, 0.5] {
|
||
let mut s = Session::new(sample::demo());
|
||
let mut row = format!("scale {scale}:");
|
||
for (id, ext, extra) in [
|
||
("file.export_gif", "gif", json!({})),
|
||
("file.export_apng", "png", json!({"background": true})),
|
||
("file.export_apng", "png", json!({})),
|
||
("file.export_webp", "webp", json!({"background": true})),
|
||
("file.export_webp", "webp", json!({})),
|
||
] {
|
||
let mut p = extra.clone();
|
||
p["path"] = json!(dir.join(format!("walk-{scale}-{}.{ext}", extra.as_object().map_or(0, |o| o.len()))).display().to_string());
|
||
p["scale"] = json!(scale);
|
||
p["confirm"] = json!(true);
|
||
let t = std::time::Instant::now();
|
||
let v = run(&mut s, id, p);
|
||
row.push_str(&format!(" | {id} {extra}: {} KB, {} stored, {} ms", v["bytes"].as_u64().unwrap() / 1024, v["stored_frames"], t.elapsed().as_millis()));
|
||
}
|
||
println!("{row}");
|
||
}
|
||
let _ = std::fs::remove_dir_all(&dir);
|
||
}
|
||
|
||
#[test]
|
||
fn shared_webp_container_preserves_payload_timing_loop_and_alpha() {
|
||
let px = [12, 34, 56, 128];
|
||
let f = encode(Kind::Webp, None, &px, 1, 1).unwrap();
|
||
assert_eq!(f.data.first(), Some(&0x2f), "raw VP8L payload, not a nested chunk");
|
||
let bytes = webp_file(1, 1, 7, true, [1, 2, 3, 4], &[(&f, 123)]).unwrap();
|
||
let anim = riff_chunk(&bytes, b"ANIM").unwrap();
|
||
assert_eq!(anim, &[3, 2, 1, 4, 7, 0]);
|
||
let frame = riff_chunk(&bytes, b"ANMF").unwrap();
|
||
assert_eq!(&frame[12..15], &[123, 0, 0], "single-frame duration is retained on the wire");
|
||
// The pinned image decoder treats one-frame WebP as a still image (delay 0).
|
||
// Check that contract separately, then use two distinct frames to exercise playback timing.
|
||
let decoded = decode_webp(&bytes);
|
||
assert_eq!(decoded.frames, vec![(px.to_vec(), 0)]);
|
||
let second_px = [90, 80, 70, 160];
|
||
let second = encode(Kind::Webp, None, &second_px, 1, 1).unwrap();
|
||
let animation = webp_file(1, 1, 7, true, [1, 2, 3, 4], &[(&f, 123), (&second, 47)]).unwrap();
|
||
let decoded = decode_webp(&animation);
|
||
assert_eq!(decoded.size, (1, 1));
|
||
assert_eq!(decoded.loop_count, 7);
|
||
assert!(decoded.alpha);
|
||
assert_eq!(decoded.frames, vec![(px.to_vec(), 123), (second_px.to_vec(), 47)]);
|
||
assert!(webp_file(1, 1, 0, false, [0; 4], &[]).is_err());
|
||
assert!(webp_file(1, 1, 0, false, [0; 4], &[(&f, 1 << 24)]).is_err());
|
||
}
|
||
|
||
// Frozen pre-cutover assembly oracle (original APNG implementation by Tanish Manku).
|
||
fn png_chunk(out: &mut Vec<u8>, id: &[u8; 4], data: &[u8]) -> Result<()> {
|
||
let len =
|
||
u32::try_from(data.len()).ok().filter(|l| *l < 1 << 31).ok_or_else(|| EngineError::Format("an animated PNG frame is too large".into()))?;
|
||
out.extend_from_slice(&len.to_be_bytes());
|
||
let from = out.len();
|
||
out.extend_from_slice(id);
|
||
out.extend_from_slice(data);
|
||
let crc = anicraft_xfl::zip::crc32(out.get(from..).unwrap_or(&[]));
|
||
out.extend_from_slice(&crc.to_be_bytes());
|
||
Ok(())
|
||
}
|
||
|
||
/// The APNG file: frames with their delays (the first frame is the default image).
|
||
fn legacy_apng_file(w: u32, h: u32, plays: u32, frames: &[(&Encoded, (u16, u16))]) -> Result<Vec<u8>> {
|
||
let mut out = b"\x89PNG\r\n\x1a\n".to_vec();
|
||
let mut ihdr = Vec::with_capacity(13);
|
||
ihdr.extend_from_slice(&w.to_be_bytes());
|
||
ihdr.extend_from_slice(&h.to_be_bytes());
|
||
ihdr.extend_from_slice(&[8, 6, 0, 0, 0]); // 8-bit RGBA, deflate, adaptive filters, no interlace
|
||
png_chunk(&mut out, b"IHDR", &ihdr)?;
|
||
let count = u32::try_from(frames.len()).map_err(|_| EngineError::Format("too many frames".into()))?;
|
||
let mut actl = count.to_be_bytes().to_vec();
|
||
actl.extend_from_slice(&plays.to_be_bytes());
|
||
png_chunk(&mut out, b"acTL", &actl)?;
|
||
let mut seq = 0u32;
|
||
for (k, (f, (num, den))) in frames.iter().enumerate() {
|
||
let mut fctl = Vec::with_capacity(26);
|
||
for v in [seq, f.rect.w, f.rect.h, f.rect.x, f.rect.y] {
|
||
fctl.extend_from_slice(&v.to_be_bytes());
|
||
}
|
||
fctl.extend_from_slice(&num.to_be_bytes());
|
||
fctl.extend_from_slice(&den.to_be_bytes());
|
||
fctl.extend_from_slice(&[0, u8::from(f.over)]); // dispose: none; blend: source or over
|
||
png_chunk(&mut out, b"fcTL", &fctl)?;
|
||
seq += 1;
|
||
if k == 0 {
|
||
png_chunk(&mut out, b"IDAT", &f.data)?;
|
||
} else {
|
||
let mut fdat = Vec::with_capacity(f.data.len() + 4);
|
||
fdat.extend_from_slice(&seq.to_be_bytes());
|
||
fdat.extend_from_slice(&f.data);
|
||
png_chunk(&mut out, b"fdAT", &fdat)?;
|
||
seq += 1;
|
||
}
|
||
}
|
||
png_chunk(&mut out, b"IEND", &[])?;
|
||
Ok(out)
|
||
}
|
||
|
||
#[test]
|
||
fn shared_apng_container_preserves_exact_wire_bytes() {
|
||
let first = Encoded { rect: Rect { x: 0, y: 0, w: 3, h: 2 }, over: false, data: vec![1, 2, 3] };
|
||
let second = Encoded { rect: Rect { x: 2, y: 1, w: 1, h: 1 }, over: true, data: vec![4, 5] };
|
||
for plays in [0, 1, 7] {
|
||
for delay in [(0, 0), (200, 2997), (u16::MAX, u16::MAX)] {
|
||
let frames = [(&first, delay), (&second, (1, 100)), (&second, (0, 1))];
|
||
assert_eq!(apng_file(3, 2, plays, &frames).unwrap(), legacy_apng_file(3, 2, plays, &frames).unwrap());
|
||
}
|
||
}
|
||
assert!(apng_file(3, 2, 0, &[]).is_err());
|
||
assert!(apng_file(3, 2, 0, &[(&second, (1, 1))]).is_err());
|
||
}
|