edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | //! Generated clips (solid, gradient, countdown, bars), the adjustment layer, clip colour labels |
| 2 | //! and switched-off clips, through the session: the pictures the render shows, the grade that |
| 3 | //! reaches only the frames under an adjustment layer, a disabled clip left out of picture and |
| 4 | //! sound, the file (v48) and undo. |
| 5 | //! |
| 6 | //! ```text |
| 7 | //! cargo test -p engine --release --test generators |
| 8 | //! ``` |
| 9 | |
| 10 | use std::path::PathBuf; |
| 11 | |
| 12 | use engine::PlaybackSession; |
| 13 | use engine::color::ColorParams; |
| 14 | use engine::generate::{Gen, Kind}; |
| 15 | use engine::project::{Lane, LaneKind}; |
| 16 | use engine::scratch::Scratch; |
| 17 | |
| 18 | fn asset(name: &str) -> PathBuf { |
| 19 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets").join(name) |
| 20 | } |
| 21 | |
| 22 | /// `test_av.mp4` (1280x720, 30 fps, 150 frames) with a second video lane. |
| 23 | fn rig() -> PlaybackSession { |
| 24 | engine::ao::set_silent_output(); |
| 25 | let mut s = PlaybackSession::open(asset("test_av.mp4")).expect("open"); |
| 26 | s.add_lane(LaneKind::Video); |
| 27 | s |
| 28 | } |
| 29 | |
| 30 | const W: u32 = 320; |
| 31 | const H: u32 = 180; |
| 32 | |
| 33 | /// The BGRA pixel at (`x`, `y`) of timeline frame `frame`, as RGB. |
| 34 | fn px(s: &PlaybackSession, frame: u32, x: usize, y: usize) -> [i32; 3] { |
| 35 | let f = s.compose_at(frame, W, H).expect("frame"); |
| 36 | let i = (y * f.width as usize + x) * 4; |
| 37 | [i32::from(f.bgra[i + 2]), i32::from(f.bgra[i + 1]), i32::from(f.bgra[i])] |
| 38 | } |
| 39 | |
| 40 | /// Row `y` of timeline frame `frame` as RGB: one composition for the whole row. `px` composes the |
| 41 | /// frame on every call, so a row read pixel by pixel costs a composition per pixel. |
| 42 | fn row_at(s: &PlaybackSession, frame: u32, y: usize) -> Vec<[i32; 3]> { |
| 43 | let f = s.compose_at(frame, W, H).expect("frame"); |
| 44 | (0..W as usize) |
| 45 | .map(|x| { |
| 46 | let i = (y * f.width as usize + x) * 4; |
| 47 | [i32::from(f.bgra[i + 2]), i32::from(f.bgra[i + 1]), i32::from(f.bgra[i])] |
| 48 | }) |
| 49 | .collect() |
| 50 | } |
| 51 | |
| 52 | fn near(a: [i32; 3], b: [i32; 3], tol: i32) -> bool { |
| 53 | a.iter().zip(b).all(|(a, b)| (a - b).abs() <= tol) |
| 54 | } |
| 55 | |
| 56 | #[allow(clippy::chunks_exact_to_as_chunks)] |
| 57 | fn mean_luma(s: &PlaybackSession, frame: u32) -> f64 { |
| 58 | let f = s.compose_at(frame, W, H).expect("frame"); |
| 59 | f.bgra.chunks_exact(4).map(|p| 0.299 * f64::from(p[2]) + 0.587 * f64::from(p[1]) + 0.114 * f64::from(p[0])).sum::<f64>() / (f.bgra.len() / 4) as f64 |
| 60 | } |
| 61 | |
| 62 | fn solid(s: &PlaybackSession, rgb: u32) -> Gen { |
| 63 | Gen::new(Kind::Solid { rgb }, s.meta().width, s.meta().height) |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn a_generator_renders_its_pixels() { |
| 68 | let mut s = rig(); |
| 69 | let v2 = Lane::new(LaneKind::Video, 1); |
| 70 | assert!(s.place_generated(solid(&s, 0x00c800), 1.0, Some(2.0), Some(v2)).expect("placed")); |
| 71 | assert!(near(px(&s, 45, 160, 90), [0, 200, 0], 6), "{:?}", px(&s, 45, 160, 90)); |
| 72 | assert!(!near(px(&s, 10, 160, 90), [0, 200, 0], 30), "before it the footage shows"); |
| 73 | assert!(!near(px(&s, 95, 160, 90), [0, 200, 0], 30), "after its two seconds the footage shows"); |
| 74 | // The clip is 2 s of the project's rate and says what it is. |
| 75 | assert_eq!(s.lane_clips(v2)[0].frames(), 60); |
| 76 | assert_eq!(s.generator_of(v2, 0).map(|g| g.kind), Some(Kind::Solid { rgb: 0x00c800 })); |
| 77 | assert_eq!(s.generator_of(Lane::V1, 0), None); |
| 78 | |
| 79 | // A gradient runs between its two colours across the frame. |
| 80 | let grad = Gen::new(Kind::Gradient { a: 0xff0000, b: 0x0000ff, angle: 0 }, s.meta().width, s.meta().height); |
| 81 | assert!(s.set_generator(v2, 0, grad)); |
| 82 | let (left, right) = (px(&s, 45, 2, 90), px(&s, 45, W as usize - 3, 90)); |
| 83 | assert!(left[0] > 220 && left[2] < 40, "{left:?}"); |
| 84 | assert!(right[2] > 220 && right[0] < 40, "{right:?}"); |
| 85 | assert!(s.undo(), "the change of generator is one undo step"); |
| 86 | assert!(near(px(&s, 45, 160, 90), [0, 200, 0], 6)); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn bars_and_a_countdown_are_pictures() { |
| 91 | let mut s = rig(); |
| 92 | let v2 = Lane::new(LaneKind::Video, 1); |
| 93 | let bars = s.generator_named("bars").expect("bars"); |
| 94 | assert!(s.place_generated(bars, 0.0, None, Some(v2)).expect("placed")); |
| 95 | assert_eq!(s.lane_clips(v2)[0].frames(), 150, "five seconds at 30 fps"); |
| 96 | assert!(near(px(&s, 3, 5, 90), [191, 191, 191], 8), "white first"); |
| 97 | assert!(near(px(&s, 3, W as usize - 5, 90), [0, 0, 191], 8), "blue last"); |
| 98 | |
| 99 | let cd = s.generator_named("countdown").expect("countdown"); |
| 100 | assert!(s.place_generated(cd, 6.0, None, Some(v2)).expect("placed")); |
| 101 | let second = s.lane_clips(v2)[1].clone(); |
| 102 | assert_eq!(second.frames(), 150, "a countdown from 5 runs 5 s"); |
| 103 | let at = |sec: f64| s.compose_at(second.start + (sec * 30.0) as u32 + 2, W, H).expect("frame").bgra; |
| 104 | assert_ne!(at(0.0), at(1.0)); |
| 105 | assert_ne!(at(1.0), at(2.0)); |
| 106 | assert_eq!(at(1.0), at(1.0)); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn an_adjustment_layer_grades_only_what_is_under_it_and_only_while_it_lasts() { |
| 111 | let mut s = rig(); |
| 112 | let v2 = Lane::new(LaneKind::Video, 1); |
| 113 | let before: Vec<f64> = [10, 45, 80].iter().map(|&f| mean_luma(&s, f)).collect(); |
| 114 | let adjust = s.generator_named("adjust").expect("adjust"); |
| 115 | assert!(s.place_generated(adjust, 1.0, Some(1.0), Some(v2)).expect("placed")); |
| 116 | assert!(s.set_color(v2, 0, Some(ColorParams { brightness: -0.9, ..Default::default() }))); |
| 117 | assert_eq!(mean_luma(&s, 10), before[0], "before the layer nothing changed"); |
| 118 | assert_eq!(mean_luma(&s, 80), before[2], "after the layer nothing changed"); |
| 119 | let inside = mean_luma(&s, 45); |
| 120 | assert!(inside < before[1] - 20.0, "the grade reaches the footage under it: {inside} vs {}", before[1]); |
| 121 | // A layer above the adjustment is not under it. |
| 122 | let v3 = s.add_lane(LaneKind::Video); |
| 123 | assert!(s.place_generated(solid(&s, 0xc8c8c8), 1.0, Some(1.0), Some(v3)).expect("placed")); |
| 124 | let top = px(&s, 45, 160, 90); |
| 125 | assert!(near(top, [200, 200, 200], 6), "a layer above the adjustment is untouched: {top:?}"); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn a_disabled_clip_is_skipped_in_picture_and_sound_and_undo_brings_it_back() { |
| 130 | let mut s = rig(); |
| 131 | let v2 = Lane::new(LaneKind::Video, 1); |
| 132 | let plain = mean_luma(&s, 45); |
| 133 | assert!(s.place_generated(solid(&s, 0xffffff), 1.0, Some(2.0), Some(v2)).expect("placed")); |
| 134 | assert!(mean_luma(&s, 45) > 200.0, "the white fills the frame"); |
| 135 | assert!(s.set_enabled(v2, 0, false)); |
| 136 | assert_eq!(mean_luma(&s, 45), plain, "off, the clip is not drawn"); |
| 137 | assert_eq!(s.lane_clips(v2).len(), 1, "but it is still on the timeline"); |
| 138 | assert!(s.mark_of(v2, 0).off); |
| 139 | // The sound too: the render view has no clip on the audio lane where it is switched off. |
| 140 | let a1 = Lane::new(LaneKind::Audio, 0); |
| 141 | assert!(!s.export_snapshot().0.lane(a1).is_empty()); |
| 142 | assert!(s.set_enabled(a1, 0, false)); |
| 143 | assert!(s.export_snapshot().0.lane(a1).is_empty(), "a disabled audio clip is not mixed"); |
| 144 | assert!(s.set_enabled(a1, 0, true)); |
| 145 | assert!(s.undo() && s.mark_of(a1, 0).off, "undo takes the last switch back"); |
| 146 | assert!(s.undo() && !s.mark_of(a1, 0).off, "and the one before"); |
| 147 | assert!(s.undo() && !s.mark_of(v2, 0).off, "and the first"); |
| 148 | assert!(mean_luma(&s, 45) > 200.0, "the white is drawn again"); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn labels_generators_and_switches_round_trip_in_a_v48_file() { |
| 153 | let dir = Scratch::dir("gens_roundtrip"); |
| 154 | let mut s = rig(); |
| 155 | let v2 = Lane::new(LaneKind::Video, 1); |
| 156 | let grad = Gen::new(Kind::Gradient { a: 0x112233, b: 0xaabbcc, angle: 30 }, s.meta().width, s.meta().height); |
| 157 | assert!(s.place_generated(grad, 0.0, Some(3.0), Some(v2)).expect("placed")); |
| 158 | assert!(s.set_label(Lane::V1, 0, 7)); |
| 159 | assert!(!s.set_label(Lane::V1, 0, 7), "no change, no step"); |
| 160 | assert!(!s.set_label(Lane::V1, 0, 9), "past the palette"); |
| 161 | assert!(s.set_enabled(v2, 0, false)); |
| 162 | let path = dir.join("g.edith"); |
| 163 | s.save_project(&path).expect("save"); |
| 164 | let text = std::fs::read_to_string(&path).expect("read"); |
| 165 | assert!(text.starts_with("edith 48\n"), "{}", text.lines().next().unwrap_or("")); |
| 166 | assert!(text.contains("\nmark video 1 0 7 on\n") && text.contains("\nmark video 2 0 0 off\n"), "{text}"); |
| 167 | let back = PlaybackSession::open_project(&path).expect("reopen"); |
| 168 | assert_eq!(back.mark_of(Lane::V1, 0).label, 7); |
| 169 | assert!(back.mark_of(v2, 0).off); |
| 170 | assert_eq!(back.generator_of(v2, 0).map(|g| g.kind), Some(Kind::Gradient { a: 0x112233, b: 0xaabbcc, angle: 30 })); |
| 171 | assert!(back.offline_sources().is_empty(), "a generator has no file to be missing"); |
| 172 | // A project that uses none of it stays on its old version, and a v47 file refuses a mark line. |
| 173 | let plain = dir.join("p.edith"); |
| 174 | PlaybackSession::open(asset("test_av.mp4")).expect("open").save_project(&plain).expect("save"); |
| 175 | assert!(!std::fs::read_to_string(&plain).unwrap().starts_with("edith 48")); |
| 176 | let old = text.replacen("edith 48", "edith 47", 1); |
| 177 | let bad = dir.join("old.edith"); |
| 178 | std::fs::write(&bad, old).unwrap(); |
| 179 | assert!(PlaybackSession::open_project(&bad).is_err(), "a v47 file has no mark lines"); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn an_adjustment_layer_alone_is_black_not_an_error() { |
| 184 | let mut s = rig(); |
| 185 | let v2 = Lane::new(LaneKind::Video, 1); |
| 186 | // V1 holds the footage for 5 s; the layer sits past its end, with nothing beneath it. |
| 187 | let adjust = s.generator_named("adjust").expect("adjust"); |
| 188 | assert!(s.place_generated(adjust, 6.0, Some(1.0), Some(v2)).expect("placed")); |
| 189 | let alone = px(&s, 195, 160, 90); |
| 190 | assert!(near(alone, [0, 0, 0], 4), "an adjustment layer alone is black: {alone:?}"); |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn an_adjustment_layer_on_the_lowest_lane_is_ignored() { |
| 195 | let mut s = rig(); |
| 196 | let v1 = Lane::new(LaneKind::Video, 0); |
| 197 | let v2 = Lane::new(LaneKind::Video, 1); |
| 198 | let adjust = s.generator_named("adjust").expect("adjust"); |
| 199 | assert!(s.place_generated(adjust, 6.0, Some(1.0), Some(v1)).expect("placed")); |
| 200 | assert!(s.set_color(v1, 1, Some(ColorParams { brightness: -0.9, ..Default::default() }))); |
| 201 | assert!(s.place_generated(solid(&s, 0x00c800), 6.0, Some(1.0), Some(v2)).expect("placed")); |
| 202 | let p = px(&s, 195, 160, 90); |
| 203 | assert!(near(p, [0, 200, 0], 6), "the layer beneath everything changes nothing: {p:?}"); |
| 204 | } |
| 205 | |
| 206 | #[test] |
| 207 | fn a_cropped_adjustment_layer_leaves_the_cropped_away_area_untouched() { |
| 208 | let mut s = rig(); |
| 209 | let v2 = Lane::new(LaneKind::Video, 1); |
| 210 | let before = px(&s, 45, 20, 90); |
| 211 | let mean = mean_luma(&s, 45); |
| 212 | let adjust = s.generator_named("adjust").expect("adjust"); |
| 213 | assert!(s.place_generated(adjust, 1.0, Some(2.0), Some(v2)).expect("placed")); |
| 214 | assert!(s.set_color(v2, 0, Some(ColorParams { brightness: -0.9, ..Default::default() }))); |
| 215 | let t = engine::transform::TransformParams { crop_l: 0.5, ..Default::default() }; |
| 216 | assert!(s.set_transform(v2, 0, Some(t))); |
| 217 | assert_eq!(px(&s, 45, 20, 90), before, "the cropped-away area keeps the base"); |
| 218 | let after = mean_luma(&s, 45); |
| 219 | assert!(after < mean - 5.0, "the kept part is graded: {after} vs {mean}"); |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | fn a_scaled_adjustment_layer_moves_the_composite_and_leaves_black_around_it() { |
| 224 | let mut s = rig(); |
| 225 | let v2 = Lane::new(LaneKind::Video, 1); |
| 226 | let centre_before = px(&s, 45, 160, 90); |
| 227 | let adjust = s.generator_named("adjust").expect("adjust"); |
| 228 | assert!(s.place_generated(adjust, 1.0, Some(2.0), Some(v2)).expect("placed")); |
| 229 | assert!(s.set_color(v2, 0, Some(ColorParams { brightness: -0.5, ..Default::default() }))); |
| 230 | let t = engine::transform::TransformParams { scale: 0.5, ..Default::default() }; |
| 231 | assert!(s.set_transform(v2, 0, Some(t))); |
| 232 | let corner = px(&s, 45, 3, 3); |
| 233 | assert!(near(corner, [0, 0, 0], 4), "outside the shrunk composite is black: {corner:?}"); |
| 234 | let centre = px(&s, 45, 160, 90); |
| 235 | assert!(centre != centre_before && !near(centre, [0, 0, 0], 4), "the centre is graded: {centre:?} vs {centre_before:?}"); |
| 236 | } |
| 237 | |
| 238 | #[test] |
| 239 | fn a_lone_adjustment_layer_is_the_same_black_as_a_gap() { |
| 240 | let mut s = rig(); |
| 241 | let v2 = Lane::new(LaneKind::Video, 1); |
| 242 | let adjust = s.generator_named("adjust").expect("adjust"); |
| 243 | assert!(s.place_generated(adjust, 6.0, Some(1.0), Some(v2)).expect("placed")); |
| 244 | let lone = s.compose_at(195, W, H).expect("frame").bgra; |
| 245 | let gap = s.compose_at(165, W, H).expect("frame").bgra; |
| 246 | assert_eq!(lone, gap, "a lone adjustment layer composes the gap's black"); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn a_shape_composites_over_the_lane_below_and_alone_over_black() { |
| 251 | use engine::generate::{Form, Shape}; |
| 252 | let mut s = rig(); |
| 253 | let v2 = Lane::new(LaneKind::Video, 1); |
| 254 | let bare = px(&s, 45, 10, 10); |
| 255 | let circle = Gen::new(Kind::Shape(Shape { form: Form::Ellipse, fill: Some(0xff0000), stroke: None, width: 0, radius: 0 }), s.meta().width, s.meta().height); |
| 256 | assert!(s.place_generated(circle, 1.0, Some(2.0), Some(v2)).expect("placed")); |
| 257 | // The ellipse is red at the centre and the footage shows through the clear corners. |
| 258 | assert!(near(px(&s, 45, 160, 90), [255, 0, 0], 12), "{:?}", px(&s, 45, 160, 90)); |
| 259 | assert!(near(px(&s, 45, 10, 10), bare, 6), "corner {:?} vs footage {bare:?}", px(&s, 45, 10, 10)); |
| 260 | // Its edge is anti-aliased: some pixel on the row is neither red nor the footage. |
| 261 | let row = row_at(&s, 45, 40); |
| 262 | let corner = row[0]; |
| 263 | assert!(row.iter().any(|p| p[0] > 200) && row.iter().any(|p| near(*p, corner, 6))); |
| 264 | // A line clip alone on the timeline sits on black, not on its own colour. |
| 265 | let mut alone = PlaybackSession::empty(W, H, 30.0).expect("empty"); |
| 266 | let line = Gen::new(Kind::Shape(Shape::of(Form::Line)), W, H); |
| 267 | assert!(alone.place_generated(line, 0.0, Some(1.0), Some(Lane::V1)).expect("placed")); |
| 268 | assert!(near(px(&alone, 5, 5, 5), [16, 16, 16], 24) || near(px(&alone, 5, 5, 5), [0, 0, 0], 24), "{:?}", px(&alone, 5, 5, 5)); |
| 269 | assert!(px(&alone, 5, 160, 90)[0] > 200, "the stroke is white"); |
| 270 | // Changing the shape is one undo step. |
| 271 | let rect = Gen::new(Kind::Shape(Shape::of(Form::Rectangle)), s.meta().width, s.meta().height); |
| 272 | assert!(s.set_generator(v2, 0, rect)); |
| 273 | assert!(near(px(&s, 45, 10, 10), [0x2a, 0x6d, 0xf4], 14), "{:?}", px(&s, 45, 10, 10)); |
| 274 | assert!(s.undo()); |
| 275 | assert!(near(px(&s, 45, 10, 10), bare, 6)); |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn a_shape_keeps_its_alpha_under_a_grade_and_on_a_canvas_of_another_size() { |
| 280 | use engine::generate::{Form, Shape}; |
| 281 | let ellipse = |w, h| Gen::new(Kind::Shape(Shape { form: Form::Ellipse, fill: Some(0xff0000), stroke: None, width: 0, radius: 0 }), w, h); |
| 282 | let mut s = rig(); |
| 283 | let v2 = Lane::new(LaneKind::Video, 1); |
| 284 | let bare = row_at(&s, 45, 40); |
| 285 | assert!(s.place_generated(ellipse(s.meta().width, s.meta().height), 1.0, Some(2.0), Some(v2)).expect("placed")); |
| 286 | let covered = |s: &PlaybackSession| row_at(s, 45, 40).iter().zip(&bare).filter(|(p, b)| p.iter().zip(b.iter()).any(|(a, b)| (a - b).abs() > 8)).count(); |
| 287 | let plain = covered(&s); |
| 288 | assert!(plain > 20 && plain < W as usize, "the ellipse covers part of the row: {plain}"); |
| 289 | // A grade changes the shape's colour and not where it is: the alpha takes no part in it. |
| 290 | assert!(s.set_color(v2, 0, Some(ColorParams { brightness: 0.05, ..Default::default() }))); |
| 291 | let graded = covered(&s); |
| 292 | assert!(plain.abs_diff(graded) <= 3, "plain {plain} graded {graded}"); |
| 293 | // A generator drawn for another size than the canvas is still a shape, not the full frame. |
| 294 | let mut small = PlaybackSession::empty(W, H, 30.0).expect("empty"); |
| 295 | assert!(small.place_generated(ellipse(1920, 1080), 0.0, Some(1.0), Some(Lane::V1)).expect("placed")); |
| 296 | assert!(px(&small, 5, 160, 90)[0] > 200, "centre is the fill"); |
| 297 | assert!(px(&small, 5, 2, 2)[0] < 40, "the corner is clear: {:?}", px(&small, 5, 2, 2)); |
| 298 | } |