This commit is contained in:
olcxja 2026-10-28 09:56:33 +01:00
commit 75df901d2d
1852 changed files with 11873 additions and 0 deletions

View file

@ -0,0 +1,15 @@
[package]
name = "convert_frames"
version = "0.1.0"
edition = "2021"
description = "Converts frames/ JPEG clips into transparent lossless WebP assets + manifest.json"
license = "MIT"
[[bin]]
name = "convert_frames"
path = "src/main.rs"
[dependencies]
hamster-pet = { path = "../.." }
image = { version = "0.25", default-features = false, features = ["jpeg", "webp"] }
serde_json = "1"

View file

@ -0,0 +1,146 @@
use std::path::{Path, PathBuf};
use hamster_pet::DEFAULT_FPS;
/// One output clip: a contiguous run of source frames.
///
/// Ranges are spelled out instead of "everything in the folder" because the source folders
/// contain a stray neutral frame and several clips whose frame numbering restarts.
pub struct ClipSpec {
pub name: &'static str,
pub source: &'static str,
pub first: u32,
pub last: u32,
pub looping: bool,
pub fps: u32,
}
const fn clip(name: &'static str, source: &'static str, first: u32, last: u32) -> ClipSpec {
ClipSpec {
name,
source,
first,
last,
looping: false,
fps: DEFAULT_FPS,
}
}
const fn looping(name: &'static str, source: &'static str, first: u32, last: u32) -> ClipSpec {
ClipSpec {
name,
source,
first,
last,
looping: true,
fps: DEFAULT_FPS,
}
}
pub const CLIPS: &[ClipSpec] = &[
looping("idle_main", "pacmainidle", 60, 60),
clip("idle_blink", "pacidle0", 60, 79),
clip("idle_look_around", "pacidle1", 1560, 1639),
clip("idle_nose_rub", "pacidle2", 0, 59),
clip("idle_yawn", "pacidle3", 220, 279),
clip("idle_rock", "pacidle4", 120, 149),
clip("idle_peek", "pacidle5", 80, 119),
clip("idle_yawn_second", "pacidle6", 150, 219),
clip("idle_dig", "pacidle7", 1640, 1779),
clip("idle_squint", "pacidle8", 420, 489),
clip("idle_paper_plane", "pacidle9", 1780, 1999),
clip("idle_activity_newspaper_start", "pacidlestart1", 1380, 1419),
looping("idle_activity_newspaper_loop", "pacidlestart1", 1420, 1519),
clip(
"idle_activity_newspaper_finish",
"pacidlestart1",
1520,
1559,
),
clip("idle_activity_book_start", "pacidlestart2", 1190, 1229),
looping("idle_activity_book_loop", "pacidlestart2", 1230, 1329),
clip("idle_activity_book_finish", "pacidlestart2", 1330, 1379),
clip("idle_activity_sleep_start", "pacidlestart3", 490, 559),
looping("idle_activity_sleep_loop", "pacidlestart3", 560, 599),
clip("idle_activity_sleep_finish", "pacidlestart3", 600, 679),
clip("typing_start", "pactyping", 680, 719),
looping("typing", "pactyping", 720, 779),
clip("typing_stop", "pactyping", 780, 819),
clip("screenshot", "pacscreenshot", 280, 419),
// The source music animation is one 130 frame take: it puts the headphones on, listens and
// takes them off again. Split into the three phases the engine plays so listening loops in the
// middle (1044..1057 is one bob, verified seamless) and the take off only happens when the
// music stops.
clip("music_start", "pacmusic", 990, 1043),
looping("music", "pacmusic", 1044, 1057),
clip("music_stop", "pacmusic", 1058, 1119),
clip("drag_start", "paccharacter", 1120, 1124),
looping("drag_loop", "paccharacter", 1145, 1174),
clip("drag_end", "paccharacter", 1175, 1189),
];
pub fn source_frame_path(frames_root: &Path, source: &str, number: u32) -> PathBuf {
frames_root
.join(source)
.join("jpeg")
.join(format!("hamster_{number:04}.p.jpg"))
}
pub fn frame_count(clip: &ClipSpec) -> usize {
(clip.last - clip.first + 1) as usize
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeSet;
#[test]
fn clip_names_are_unique() {
let mut names: Vec<&str> = CLIPS.iter().map(|clip| clip.name).collect();
let count = names.len();
names.sort_unstable();
names.dedup();
assert_eq!(names.len(), count);
}
#[test]
fn clip_names_exist_in_the_engine_vocabulary() {
let vocabulary: BTreeSet<&str> = hamster_pet::core::chains::required_clips()
.into_iter()
.collect();
for clip in CLIPS {
assert!(
vocabulary.contains(clip.name),
"{} is not used by the engine",
clip.name
);
}
for name in &vocabulary {
assert!(
CLIPS.iter().any(|clip| clip.name == *name),
"{name} has no source clip"
);
}
}
#[test]
fn ranges_are_ordered_and_non_empty() {
for clip in CLIPS {
assert!(clip.first <= clip.last, "{}", clip.name);
assert!(clip.fps > 0, "{}", clip.name);
}
}
#[test]
fn frame_paths_follow_the_source_naming() {
let path = source_frame_path(Path::new("frames"), "pacidle0", 60);
assert_eq!(path, Path::new("frames/pacidle0/jpeg/hamster_0060.p.jpg"));
}
#[test]
fn total_frames_match_the_source_set_minus_the_unused_neutral_frame() {
let total: usize = CLIPS.iter().map(frame_count).sum();
assert_eq!(total, 1811);
}
}

View file

@ -0,0 +1,427 @@
//! Converts the source JPEG clips in `frames/` into transparent lossless WebP clips plus
//! `manifest.json`. Deterministic: the same input always produces byte-identical output.
mod clips;
mod matte;
use std::collections::{BTreeMap, BTreeSet};
use std::error::Error;
use std::fs;
use std::io::BufWriter;
use std::path::{Path, PathBuf};
use hamster_pet::core::manifest::{clip_dir, frame_file_name, MANIFEST_VERSION};
use hamster_pet::{Canvas, Clip, Manifest, Point, Rect};
use image::{ImageEncoder, RgbImage, RgbaImage};
use matte::Shadow;
/// Transparent padding kept around each clip so smooth edges survive scaling.
const MARGIN: u32 = 2;
const USAGE: &str = "\
convert_frames [options]
--in <dir> source frames (default: frames)
--out <dir> generated clip set + manifest (default: assets/animations)
--shadow <mode> keep | drop the baked drop shadow (default: keep)
--force overwrite a non-empty --out directory
--help";
struct Options {
frames: PathBuf,
out: PathBuf,
shadow: Shadow,
force: bool,
}
struct ResolvedClip {
spec: &'static clips::ClipSpec,
paths: Vec<PathBuf>,
}
/// Inclusive pixel rectangle in source coordinates.
#[derive(Clone, Copy, Debug)]
struct Bounds {
x0: u32,
y0: u32,
x1: u32,
y1: u32,
}
impl Bounds {
fn width(self) -> u32 {
self.x1 - self.x0 + 1
}
fn height(self) -> u32 {
self.y1 - self.y0 + 1
}
fn union(self, other: Bounds) -> Bounds {
Bounds {
x0: self.x0.min(other.x0),
y0: self.y0.min(other.y0),
x1: self.x1.max(other.x1),
y1: self.y1.max(other.y1),
}
}
fn padded(self, margin: u32, frame: (u32, u32)) -> Bounds {
Bounds {
x0: self.x0.saturating_sub(margin),
y0: self.y0.saturating_sub(margin),
x1: (self.x1 + margin).min(frame.0 - 1),
y1: (self.y1 + margin).min(frame.1 - 1),
}
}
fn clamp_inside(self, outer: Bounds) -> Bounds {
Bounds {
x0: self.x0.max(outer.x0),
y0: self.y0.max(outer.y0),
x1: self.x1.min(outer.x1),
y1: self.y1.min(outer.y1),
}
}
fn to_rect(self, origin: Bounds) -> Rect {
Rect {
x: (self.x0 - origin.x0) as i32,
y: (self.y0 - origin.y0) as i32,
width: self.width(),
height: self.height(),
}
}
}
fn main() {
if let Err(err) = run() {
eprintln!("error: {err}");
std::process::exit(1);
}
}
fn run() -> Result<(), Box<dyn Error>> {
let options = match parse_args(std::env::args().skip(1)) {
Ok(Some(options)) => options,
Ok(None) => {
println!("{USAGE}");
return Ok(());
}
Err(message) => {
eprintln!("{message}\n\n{USAGE}");
std::process::exit(2);
}
};
let resolved = resolve_sources(&options)?;
prepare_output_dir(&options)?;
let (canvas, clip_bounds) = measure(&resolved, options.shadow)?;
let anchor = anchor_from(clip_bounds["idle_main"], canvas);
let mut manifest_clips = BTreeMap::new();
let mut total_bytes = 0u64;
for clip in &resolved {
let bounds = clip_bounds[clip.spec.name].clamp_inside(canvas);
let rect = bounds.to_rect(canvas);
write_clip(clip, bounds, options.shadow, &options.out, &mut total_bytes)?;
manifest_clips.insert(
clip.spec.name.to_string(),
Clip {
frames: clip.paths.len() as u32,
fps: clip.spec.fps,
looping: clip.spec.looping,
rect,
},
);
println!(
"{:<36} {:>4} frames {:>3}x{:<3} at {:>3},{:<3}",
clip.spec.name,
clip.paths.len(),
rect.width,
rect.height,
rect.x,
rect.y
);
}
let manifest = Manifest {
version: MANIFEST_VERSION,
canvas: Canvas {
width: canvas.width(),
height: canvas.height(),
},
anchor: Point {
x: anchor.0 as i32,
y: anchor.1 as i32,
},
clips: manifest_clips,
};
manifest.validate()?;
let manifest_path = options.out.join("manifest.json");
fs::write(&manifest_path, serde_json::to_string_pretty(&manifest)?)?;
report_unused_sources(&resolved, &options.frames)?;
println!(
"\n{} clips, {} frames, canvas {}x{}, anchor ({}, {}), {:.1} MB on disk",
manifest.clips.len(),
manifest.clips.values().map(|clip| clip.frames).sum::<u32>(),
manifest.canvas.width,
manifest.canvas.height,
manifest.anchor.x,
manifest.anchor.y,
total_bytes as f64 / 1_000_000.0
);
println!("wrote {}", manifest_path.display());
Ok(())
}
fn resolve_sources(options: &Options) -> Result<Vec<ResolvedClip>, Box<dyn Error>> {
let mut resolved = Vec::new();
let mut missing = Vec::new();
for spec in clips::CLIPS {
let mut paths = Vec::with_capacity(clips::frame_count(spec));
for number in spec.first..=spec.last {
let path = clips::source_frame_path(&options.frames, spec.source, number);
if !path.is_file() {
missing.push(path.clone());
}
paths.push(path);
}
resolved.push(ResolvedClip { spec, paths });
}
if !missing.is_empty() {
return Err(format!(
"{} source frames are missing, first: {}",
missing.len(),
missing[0].display()
)
.into());
}
Ok(resolved)
}
fn prepare_output_dir(options: &Options) -> Result<(), Box<dyn Error>> {
if options.out.exists() {
let generated = options.out.join("manifest.json").is_file();
let empty = fs::read_dir(&options.out)?.next().is_none();
if !empty && !generated && !options.force {
return Err(format!(
"{} is not empty and does not look generated; pass --force to overwrite",
options.out.display()
)
.into());
}
fs::remove_dir_all(&options.out)?;
}
fs::create_dir_all(&options.out)?;
Ok(())
}
/// First pass: matting bounds, so crops and the canvas are known before anything is written.
fn measure(
resolved: &[ResolvedClip],
shadow: Shadow,
) -> Result<(Bounds, BTreeMap<&'static str, Bounds>), Box<dyn Error>> {
let mut frame_size: Option<(u32, u32)> = None;
let mut global: Option<Bounds> = None;
let mut clip_bounds: BTreeMap<&'static str, Bounds> = BTreeMap::new();
for clip in resolved {
let mut bounds: Option<Bounds> = None;
for path in &clip.paths {
let rgb = load_rgb(path)?;
check_size(&mut frame_size, &rgb, path)?;
let matted = matte::apply(&rgb, shadow);
let content = matte::content_bbox(&matted).ok_or_else(|| {
format!(
"{} is fully transparent after matting: {}",
clip.spec.name,
path.display()
)
})?;
let content = bounds_of(content);
bounds = Some(bounds.map_or(content, |current| current.union(content)));
global = Some(global.map_or(content, |current| current.union(content)));
}
clip_bounds.insert(
clip.spec.name,
bounds.expect("every clip has at least one frame"),
);
}
let frame_size = frame_size.expect("the clip table is not empty");
let canvas = global
.expect("the clip table is not empty")
.padded(MARGIN, frame_size);
Ok((canvas, clip_bounds))
}
fn bounds_of((x0, y0, x1, y1): (u32, u32, u32, u32)) -> Bounds {
Bounds { x0, y0, x1, y1 }
}
fn write_clip(
clip: &ResolvedClip,
bounds: Bounds,
shadow: Shadow,
out: &Path,
total_bytes: &mut u64,
) -> Result<(), Box<dyn Error>> {
let dir = clip_dir(out, clip.spec.name);
fs::create_dir_all(&dir)?;
for (index, path) in clip.paths.iter().enumerate() {
let rgb = load_rgb(path)?;
let matted = matte::apply(&rgb, shadow);
let cropped = image::imageops::crop_imm(
&matted,
bounds.x0,
bounds.y0,
bounds.width(),
bounds.height(),
)
.to_image();
let target = dir.join(frame_file_name(index as u32));
encode_webp(&cropped, &target)?;
*total_bytes += fs::metadata(&target)?.len();
// Lossless output must survive a decode byte for byte; checked on the clip edges.
if index == 0 || index + 1 == clip.paths.len() {
verify_round_trip(&cropped, &target)?;
}
}
Ok(())
}
fn report_unused_sources(
resolved: &[ResolvedClip],
frames_root: &Path,
) -> Result<(), Box<dyn Error>> {
let mut used: BTreeMap<&'static str, BTreeSet<u32>> = BTreeMap::new();
for clip in resolved {
let entry = used.entry(clip.spec.source).or_default();
for number in clip.spec.first..=clip.spec.last {
entry.insert(number);
}
}
for (source, used_numbers) in used {
let dir = frames_root.join(source).join("jpeg");
let mut unused = Vec::new();
for entry in fs::read_dir(&dir)? {
let name = entry?.file_name().to_string_lossy().into_owned();
if let Some(number) = parse_frame_number(&name) {
if !used_numbers.contains(&number) {
unused.push(number);
}
}
}
unused.sort_unstable();
if !unused.is_empty() {
println!("unused source frames in {source}: {unused:?}");
}
}
Ok(())
}
fn parse_frame_number(name: &str) -> Option<u32> {
name.strip_prefix("hamster_")?
.split('.')
.next()?
.parse()
.ok()
}
fn load_rgb(path: &Path) -> Result<RgbImage, Box<dyn Error>> {
Ok(image::open(path)?.to_rgb8())
}
fn check_size(
size: &mut Option<(u32, u32)>,
image: &RgbImage,
path: &Path,
) -> Result<(), Box<dyn Error>> {
let dimensions = image.dimensions();
match size {
Some(expected) if *expected != dimensions => Err(format!(
"{} is {}x{} but the first frame was {}x{}",
path.display(),
dimensions.0,
dimensions.1,
expected.0,
expected.1
)
.into()),
Some(_) => Ok(()),
None => {
*size = Some(dimensions);
Ok(())
}
}
}
fn encode_webp(image: &RgbaImage, path: &Path) -> Result<(), Box<dyn Error>> {
let file = BufWriter::new(fs::File::create(path)?);
let encoder = image::codecs::webp::WebPEncoder::new_lossless(file);
encoder.write_image(
image.as_raw(),
image.width(),
image.height(),
image::ExtendedColorType::Rgba8,
)?;
Ok(())
}
fn verify_round_trip(expected: &RgbaImage, path: &Path) -> Result<(), Box<dyn Error>> {
let bytes = fs::read(path)?;
let decoded = image::load_from_memory_with_format(&bytes, image::ImageFormat::WebP)?.to_rgba8();
if decoded.as_raw() != expected.as_raw() {
return Err(format!("lossless round trip changed pixels in {}", path.display()).into());
}
Ok(())
}
/// Bottom centre of the resting silhouette: the point that stays put while clips change.
fn anchor_from(idle_main: Bounds, canvas: Bounds) -> (u32, u32) {
let x = (idle_main.x0 + idle_main.x1 + 1) / 2 - canvas.x0;
let y = idle_main.y1 + 1 - canvas.y0;
(x.min(canvas.width() - 1), y.min(canvas.height() - 1))
}
fn parse_args(args: impl Iterator<Item = String>) -> Result<Option<Options>, String> {
let mut options = Options {
frames: PathBuf::from("frames"),
out: PathBuf::from("assets/animations"),
shadow: Shadow::Keep,
force: false,
};
let args: Vec<String> = args.collect();
let mut index = 0;
while index < args.len() {
let arg = args[index].as_str();
index += 1;
match arg {
"--help" | "-h" => return Ok(None),
"--force" => options.force = true,
"--in" => options.frames = PathBuf::from(take_value(&args, &mut index, arg)?),
"--out" => options.out = PathBuf::from(take_value(&args, &mut index, arg)?),
"--shadow" => {
let raw = take_value(&args, &mut index, arg)?;
options.shadow = Shadow::parse(&raw)
.ok_or_else(|| format!("--shadow: expected keep or drop, got '{raw}'"))?;
}
other => return Err(format!("unknown option '{other}'")),
}
}
Ok(Some(options))
}
fn take_value(args: &[String], index: &mut usize, name: &str) -> Result<String, String> {
if *index < args.len() {
let value = args[*index].clone();
*index += 1;
Ok(value)
} else {
Err(format!("{name} needs a value"))
}
}

View file

@ -0,0 +1,272 @@
use image::{RgbImage, RgbaImage};
/// Alpha ramp applied to the one pixel wide silhouette edge (values measured on the source art).
/// Dim edge pixels fade out: the hairs of a whisker stay hairs instead of a thick dark spike.
const EDGE_LOW: f32 = 12.0;
const EDGE_HIGH: f32 = 62.0;
/// Background handling. The art has a soft baked shadow that is darker than the character
/// outline, so removing it always erodes some dark detail; `Keep` is the default.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Shadow {
Keep,
Drop,
}
impl Shadow {
/// Pixels dimmer than this, connected to the border, are treated as background. Even `Keep`
/// removes the dim halo around thin details such as whiskers, where the art is nearly black.
pub fn background_threshold(self) -> u8 {
match self {
Shadow::Keep => 22,
// High enough to swallow the baked shadow, at the cost of dark edges and whiskers.
Shadow::Drop => 45,
}
}
pub fn parse(text: &str) -> Option<Shadow> {
match text {
"keep" => Some(Shadow::Keep),
"drop" => Some(Shadow::Drop),
_ => None,
}
}
}
/// Turns the black background into transparent pixels.
///
/// Layer transparency is used as alpha only on the silhouette edge; enclosed dark details
/// (eyes, outline, held props) stay fully opaque, which luminance-as-alpha fails to do.
pub fn apply(src: &RgbImage, shadow: Shadow) -> RgbaImage {
let (width, height) = src.dimensions();
let (w, h) = (width as usize, height as usize);
let raw = src.as_raw();
let key: Vec<u8> = (0..w * h)
.map(|i| raw[i * 3].max(raw[i * 3 + 1]).max(raw[i * 3 + 2]))
.collect();
let outside = flood_background(&key, w, h, shadow.background_threshold());
let mut out = vec![0u8; w * h * 4];
for y in 0..h {
for x in 0..w {
let i = y * w + x;
let alpha = if outside[i] {
0.0
} else if touches_outside(&outside, x, y, w, h) {
ramp(key[i])
} else {
255.0
};
let alpha = alpha.clamp(0.0, 255.0).round() as u8;
let (mut r, mut g, mut b) = (raw[i * 3], raw[i * 3 + 1], raw[i * 3 + 2]);
if alpha > 0 && alpha < 255 {
// Edge pixels were rendered over black: undo that blend before storing them.
let scale = 255.0 / f32::from(alpha);
r = (f32::from(r) * scale).min(255.0).round() as u8;
g = (f32::from(g) * scale).min(255.0).round() as u8;
b = (f32::from(b) * scale).min(255.0).round() as u8;
}
out[i * 4] = r;
out[i * 4 + 1] = g;
out[i * 4 + 2] = b;
out[i * 4 + 3] = alpha;
}
}
RgbaImage::from_raw(width, height, out).expect("buffer size matches the image")
}
fn ramp(value: u8) -> f32 {
((f32::from(value) - EDGE_LOW) / (EDGE_HIGH - EDGE_LOW)).clamp(0.0, 1.0) * 255.0
}
fn touches_outside(outside: &[bool], x: usize, y: usize, w: usize, h: usize) -> bool {
let neighbours = [
(x.wrapping_sub(1), y),
(x + 1, y),
(x, y.wrapping_sub(1)),
(x, y + 1),
];
neighbours.iter().any(|&(nx, ny)| {
if nx >= w || ny >= h {
return true;
}
outside[ny * w + nx]
})
}
/// Marks pixels below `threshold` that are reachable from the image border.
/// Anything enclosed by the character stays part of the mask.
fn flood_background(key: &[u8], w: usize, h: usize, threshold: u8) -> Vec<bool> {
let mut outside = vec![false; w * h];
let mut stack: Vec<usize> = Vec::new();
for x in 0..w {
push_if_background(x, key, threshold, &mut outside, &mut stack);
push_if_background((h - 1) * w + x, key, threshold, &mut outside, &mut stack);
}
for y in 0..h {
push_if_background(y * w, key, threshold, &mut outside, &mut stack);
push_if_background(y * w + w - 1, key, threshold, &mut outside, &mut stack);
}
while let Some(index) = stack.pop() {
let x = index % w;
let y = index / w;
if x > 0 {
push_if_background(index - 1, key, threshold, &mut outside, &mut stack);
}
if x + 1 < w {
push_if_background(index + 1, key, threshold, &mut outside, &mut stack);
}
if y > 0 {
push_if_background(index - w, key, threshold, &mut outside, &mut stack);
}
if y + 1 < h {
push_if_background(index + w, key, threshold, &mut outside, &mut stack);
}
}
outside
}
fn push_if_background(
index: usize,
key: &[u8],
threshold: u8,
outside: &mut [bool],
stack: &mut Vec<usize>,
) {
if !outside[index] && key[index] < threshold {
outside[index] = true;
stack.push(index);
}
}
/// Bounding box of pixels with any opacity, inclusive of both corners.
pub fn content_bbox(image: &RgbaImage) -> Option<(u32, u32, u32, u32)> {
let (mut x0, mut y0, mut x1, mut y1) = (u32::MAX, u32::MAX, 0, 0);
for (x, y, pixel) in image.enumerate_pixels() {
if pixel.0[3] > 0 {
x0 = x0.min(x);
y0 = y0.min(y);
x1 = x1.max(x);
y1 = y1.max(y);
}
}
if x0 == u32::MAX {
None
} else {
Some((x0, y0, x1, y1))
}
}
#[cfg(test)]
mod tests {
use super::*;
use image::Rgb;
fn frame() -> RgbImage {
// Black canvas with a bright 3x3 square at (1,1) containing a dark enclosed pixel.
let mut image = RgbImage::from_pixel(6, 6, Rgb([0, 0, 0]));
for y in 1..4 {
for x in 1..4 {
image.put_pixel(x, y, Rgb([200, 180, 90]));
}
}
image.put_pixel(2, 2, Rgb([4, 4, 4]));
image
}
#[test]
fn background_becomes_transparent_and_enclosed_dark_pixels_stay_opaque() {
let matted = apply(&frame(), Shadow::Keep);
assert_eq!(matted.get_pixel(0, 0).0[3], 0, "corner is background");
assert_eq!(matted.get_pixel(3, 3).0[3], 255, "body is opaque");
assert_eq!(
matted.get_pixel(2, 2).0[3],
255,
"enclosed dark pixel is opaque"
);
assert_eq!(
matted.get_pixel(2, 2).0[0],
4,
"enclosed colour is untouched"
);
assert_eq!(matted.get_pixel(3, 3).0[0], 200, "body colour is untouched");
}
#[test]
fn shadow_drop_removes_only_the_detached_dim_blob() {
let mut image = frame();
// Dim blob well below the drop threshold, touching nothing bright.
for y in 4..6 {
for x in 0..2 {
image.put_pixel(x, y, Rgb([40, 38, 36]));
}
}
let kept = apply(&image, Shadow::Keep);
assert!(kept.get_pixel(0, 5).0[3] > 0, "keep mode keeps the shadow");
let dropped = apply(&image, Shadow::Drop);
assert_eq!(
dropped.get_pixel(0, 5).0[3],
0,
"drop mode removes the shadow"
);
assert_eq!(
dropped.get_pixel(3, 3).0[3],
255,
"the character stays opaque"
);
}
#[test]
fn edge_pixels_are_a_ramp_unblended_from_black() {
let matted = apply(&frame(), Shadow::Keep);
// Left neighbour of the body is background, so the body edge keeps full opacity.
assert_eq!(matted.get_pixel(1, 1).0[3], 255);
// A dim single pixel is a soft edge and gets its colour unblended.
let mut image = RgbImage::from_pixel(4, 4, Rgb([0, 0, 0]));
image.put_pixel(1, 1, Rgb([28, 26, 24]));
image.put_pixel(1, 2, Rgb([28, 26, 24]));
let matted = apply(&image, Shadow::Keep);
let edge = matted.get_pixel(1, 1).0;
assert!(edge[3] > 0 && edge[3] < 255, "soft edge alpha {}", edge[3]);
assert!(edge[0] >= 28, "edge colour is not darkened further");
}
#[test]
fn the_dim_halo_around_a_thin_detail_is_dropped() {
// A whisker: a hair over black, with the near black halo that used to read as a thick spike.
let mut image = RgbImage::from_pixel(8, 4, Rgb([0, 0, 0]));
for x in 2..6 {
image.put_pixel(x, 1, Rgb([14, 13, 12]));
image.put_pixel(x, 2, Rgb([70, 66, 60]));
}
let matted = apply(&image, Shadow::Keep);
assert_eq!(matted.get_pixel(2, 1).0[3], 0, "the halo is background");
assert!(matted.get_pixel(2, 2).0[3] > 200, "the hair itself stays");
}
#[test]
fn fully_black_frame_has_no_content() {
let image = RgbImage::from_pixel(4, 4, Rgb([0, 0, 0]));
let matted = apply(&image, Shadow::Keep);
assert_eq!(content_bbox(&matted), None);
}
#[test]
fn bbox_covers_opaque_pixels() {
let matted = apply(&frame(), Shadow::Keep);
assert_eq!(content_bbox(&matted), Some((1, 1, 3, 3)));
}
#[test]
fn shadow_flag_parsing() {
assert_eq!(Shadow::parse("drop"), Some(Shadow::Drop));
assert_eq!(Shadow::parse("keep"), Some(Shadow::Keep));
assert_eq!(Shadow::parse("nope"), None);
}
}