wip: transform engine scaffolding
Browse files at this commit3050ac917ec00e2ba7e4386d0ed3258d6928ed3dParent: 8f0c5004 changed files +412 -0
- crates/engine/src/lib.rs +1 -0
- crates/engine/src/project.rs +112 -0
- crates/engine/src/scale.rs +218 -0
- crates/engine/src/transform.rs +81 -0
crates/engine/src/lib.rsmodified+1 -0
| @@ -29,6 +29,7 @@ pub(crate) mod map; | ||
| 29 | 29 | mod sweep; |
| 30 | 30 | pub(crate) mod stretch; |
| 31 | 31 | pub mod tonemap; |
| 32 | +pub mod transform; | |
| 32 | 33 | pub mod waveform; |
| 33 | 34 | |
| 34 | 35 | pub use audio::{ |
crates/engine/src/project.rsmodified+112 -0
| @@ -77,6 +77,7 @@ use crate::map::TimelineMap; | ||
| 77 | 77 | use std::path::{Path, PathBuf}; |
| 78 | 78 | |
| 79 | 79 | use crate::color::ColorParams; |
| 80 | +use crate::transform::TransformParams; | |
| 80 | 81 | use crate::eq::EqParams; |
| 81 | 82 | use crate::limiter::{Limiter, db_to_linear}; |
| 82 | 83 | use crate::scale::FitPolicy; |
| @@ -413,6 +414,11 @@ pub struct Clip { | ||
| 413 | 414 | /// plays ungraded. An index for [`Clip::eq`]'s reason, and with the same |
| 414 | 415 | /// promises: `Copy`, append-only within a session, never dangling. |
| 415 | 416 | pub color: Option<u16>, |
| 417 | + /// Index into the project's transform table | |
| 418 | + /// ([`Project::transform_of`]/[`Project::set_transform`]), or `None` for a | |
| 419 | + /// clip that plays at its fit policy's own placement. An index for | |
| 420 | + /// [`Clip::color`]'s reason and with the same promises. | |
| 421 | + pub transform: Option<u16>, | |
| 416 | 422 | /// How this clip's picture meets a project canvas of another shape |
| 417 | 423 | /// ([`crate::scale::FitPolicy`]). Inline rather than a table index like the |
| 418 | 424 | /// two above: it is one byte with no parameters, so there is nothing for a |
| @@ -757,6 +763,7 @@ pub type Parts = ( | ||
| 757 | 763 | Vec<(LaneKind, Vec<Clip>)>, |
| 758 | 764 | Vec<EqParams>, |
| 759 | 765 | Vec<ColorParams>, |
| 766 | + Vec<TransformParams>, | |
| 760 | 767 | ); |
| 761 | 768 | |
| 762 | 769 | /// How many undo steps a project keeps. One gesture is one step, so 100 is past |
| @@ -778,6 +785,8 @@ pub struct Project { | ||
| 778 | 785 | eq: Vec<EqParams>, |
| 779 | 786 | /// The same, for [`Clip::color`]. |
| 780 | 787 | color: Vec<ColorParams>, |
| 788 | + /// The same, for [`Clip::transform`]. | |
| 789 | + transform: Vec<TransformParams>, | |
| 781 | 790 | /// Snapshots pushed *before* each successful edit; `undo` pops one. The |
| 782 | 791 | /// whole lane list, so adding a lane undoes as well. Bounded by |
| 783 | 792 | /// [`HISTORY_CAP`]: at the cap the oldest step goes. |
| @@ -817,6 +826,7 @@ impl Project { | ||
| 817 | 826 | link: Some(0), |
| 818 | 827 | eq: None, |
| 819 | 828 | color: None, |
| 829 | + transform: None, | |
| 820 | 830 | fit: FitPolicy::default(), |
| 821 | 831 | speed: Speed::NORMAL, |
| 822 | 832 | }; |
| @@ -827,6 +837,7 @@ impl Project { | ||
| 827 | 837 | lanes: LaneData::two_lanes(vec![clip], vec![clip]), |
| 828 | 838 | eq: Vec::new(), |
| 829 | 839 | color: Vec::new(), |
| 840 | + transform: Vec::new(), | |
| 830 | 841 | history: Vec::new(), |
| 831 | 842 | next_link: 1, |
| 832 | 843 | subtitles: Vec::new(), |
| @@ -857,6 +868,7 @@ impl Project { | ||
| 857 | 868 | lanes: Vec<(LaneKind, Vec<Clip>)>, |
| 858 | 869 | eq: Vec<EqParams>, |
| 859 | 870 | color: Vec<ColorParams>, |
| 871 | + transform: Vec<TransformParams>, | |
| 860 | 872 | ) -> crate::Result<Self> { |
| 861 | 873 | if let Some(bad) = eq.iter().position(|p| !finite(p)) { |
| 862 | 874 | return Err(format!("eq {bad} holds a band that is not a finite number").into()); |
| @@ -864,6 +876,9 @@ impl Project { | ||
| 864 | 876 | if let Some(bad) = color.iter().position(|p| !color_finite(p)) { |
| 865 | 877 | return Err(format!("color {bad} holds a value that is not a finite number").into()); |
| 866 | 878 | } |
| 879 | + if let Some(bad) = transform.iter().position(|p| !transform_finite(p)) { | |
| 880 | + return Err(format!("transform {bad} holds a value that is not a finite number").into()); | |
| 881 | + } | |
| 867 | 882 | if lanes.is_empty() { |
| 868 | 883 | return Err("no lanes at all: that is not a project".into()); |
| 869 | 884 | } |
| @@ -927,6 +942,15 @@ impl Project { | ||
| 927 | 942 | ) |
| 928 | 943 | .into()); |
| 929 | 944 | } |
| 945 | + if c.transform.is_some_and(|i| usize::from(i) >= transform.len()) { | |
| 946 | + return Err(format!( | |
| 947 | + "{name} clip at {} names transform {} of {}", | |
| 948 | + c.start, | |
| 949 | + c.transform.unwrap_or_default(), | |
| 950 | + transform.len() | |
| 951 | + ) | |
| 952 | + .into()); | |
| 953 | + } | |
| 930 | 954 | } |
| 931 | 955 | if !sorted_disjoint(&data.clips) { |
| 932 | 956 | return Err(format!("the {name} lane is out of order or overlaps itself").into()); |
| @@ -956,6 +980,7 @@ impl Project { | ||
| 956 | 980 | lanes, |
| 957 | 981 | eq, |
| 958 | 982 | color, |
| 983 | + transform, | |
| 959 | 984 | history: Vec::new(), |
| 960 | 985 | next_link, |
| 961 | 986 | subtitles: Vec::new(), |
| @@ -1302,6 +1327,8 @@ impl Project { | ||
| 1302 | 1327 | let mut eq = Vec::new(); |
| 1303 | 1328 | let mut moved_color = vec![None; self.color.len()]; |
| 1304 | 1329 | let mut color = Vec::new(); |
| 1330 | + let mut moved_transform = vec![None; self.transform.len()]; | |
| 1331 | + let mut transform = Vec::new(); | |
| 1305 | 1332 | let mut lanes = self.lanes.clone(); |
| 1306 | 1333 | for c in lanes.iter_mut().flat_map(|l| &mut l.clips) { |
| 1307 | 1334 | let old = c.source; |
| @@ -1335,6 +1362,17 @@ impl Project { | ||
| 1335 | 1362 | } |
| 1336 | 1363 | }); |
| 1337 | 1364 | } |
| 1365 | + if let Some(old) = c.transform.map(usize::from) { | |
| 1366 | + c.transform = Some(match moved_transform[old] { | |
| 1367 | + Some(new) => new, | |
| 1368 | + None => { | |
| 1369 | + transform.push(self.transform[old]); | |
| 1370 | + let new = (transform.len() - 1) as u16; | |
| 1371 | + moved_transform[old] = Some(new); | |
| 1372 | + new | |
| 1373 | + } | |
| 1374 | + }); | |
| 1375 | + } | |
| 1338 | 1376 | } |
| 1339 | 1377 | if sources.is_empty() { |
| 1340 | 1378 | sources.extend(self.sources.first().cloned()); |
| @@ -1348,6 +1386,7 @@ impl Project { | ||
| 1348 | 1386 | lanes.into_iter().map(|l| (l.kind, l.clips)).collect(), |
| 1349 | 1387 | eq, |
| 1350 | 1388 | color, |
| 1389 | + transform, | |
| 1351 | 1390 | ) |
| 1352 | 1391 | } |
| 1353 | 1392 | |
| @@ -2192,6 +2231,71 @@ impl Project { | ||
| 2192 | 2231 | true |
| 2193 | 2232 | } |
| 2194 | 2233 | |
| 2234 | + /// How the clip at `idx` of `lane` is placed, or `None` for one at its fit | |
| 2235 | + /// policy's own placement (and for an index that is not there) -- what the | |
| 2236 | + /// renderer and the export ask before they place its picture. | |
| 2237 | + pub fn transform_of(&self, lane: Lane, idx: usize) -> Option<&TransformParams> { | |
| 2238 | + self.transform | |
| 2239 | + .get(usize::from(self.lane(lane).get(idx)?.transform?)) | |
| 2240 | + } | |
| 2241 | + | |
| 2242 | + /// [`set_color`](Self::set_color)'s twin for [`Clip::transform`]: one undo | |
| 2243 | + /// step, `false` (and no history) for an index that is not there or a | |
| 2244 | + /// value that is not finite, and equal settings share a table entry. | |
| 2245 | + pub fn set_transform(&mut self, lane: Lane, idx: usize, params: Option<TransformParams>) -> bool { | |
| 2246 | + self.write_transform(lane, idx, params, true) | |
| 2247 | + } | |
| 2248 | + | |
| 2249 | + /// [`set_transform`](Self::set_transform) without the undo step, for | |
| 2250 | + /// [`set_color_live`](Self::set_color_live)'s reason: the samples inside | |
| 2251 | + /// one drag on a position/scale/rotate/crop control are one undo, not one | |
| 2252 | + /// per pixel. | |
| 2253 | + pub fn set_transform_live( | |
| 2254 | + &mut self, | |
| 2255 | + lane: Lane, | |
| 2256 | + idx: usize, | |
| 2257 | + params: Option<TransformParams>, | |
| 2258 | + ) -> bool { | |
| 2259 | + self.write_transform(lane, idx, params, false) | |
| 2260 | + } | |
| 2261 | + | |
| 2262 | + fn write_transform( | |
| 2263 | + &mut self, | |
| 2264 | + lane: Lane, | |
| 2265 | + idx: usize, | |
| 2266 | + params: Option<TransformParams>, | |
| 2267 | + snapshot: bool, | |
| 2268 | + ) -> bool { | |
| 2269 | + if idx >= self.lane(lane).len() { | |
| 2270 | + return false; | |
| 2271 | + } | |
| 2272 | + let slot = match params { | |
| 2273 | + None => None, | |
| 2274 | + Some(params) => { | |
| 2275 | + if !transform_finite(¶ms) { | |
| 2276 | + return false; | |
| 2277 | + } | |
| 2278 | + Some(match self.transform.iter().position(|p| *p == params) { | |
| 2279 | + Some(at) => at as u16, | |
| 2280 | + // corner-cut: the 65535th *distinct* transform of one | |
| 2281 | + // session is refused rather than silently aliased, | |
| 2282 | + // exactly as `write_color` refuses the 65535th grade; | |
| 2283 | + // same upgrade path. | |
| 2284 | + None if self.transform.len() >= usize::from(u16::MAX) => return false, | |
| 2285 | + None => { | |
| 2286 | + self.transform.push(params); | |
| 2287 | + (self.transform.len() - 1) as u16 | |
| 2288 | + } | |
| 2289 | + }) | |
| 2290 | + } | |
| 2291 | + }; | |
| 2292 | + if snapshot { | |
| 2293 | + self.snapshot(); | |
| 2294 | + } | |
| 2295 | + self.lane_mut(lane).expect("checked above")[idx].transform = slot; | |
| 2296 | + true | |
| 2297 | + } | |
| 2298 | + | |
| 2195 | 2299 | /// How fast the clip at `idx` of `lane` plays. [`Speed::NORMAL`] for an |
| 2196 | 2300 | /// index that is not there, which is what every clip starts at anyway. |
| 2197 | 2301 | pub fn speed_of(&self, lane: Lane, idx: usize) -> Speed { |
| @@ -2658,6 +2762,14 @@ impl Project { | ||
| 2658 | 2762 | self.color_of(lane, idx) |
| 2659 | 2763 | } |
| 2660 | 2764 | |
| 2765 | + /// [`composite_color_at`](Self::composite_color_at)'s twin for | |
| 2766 | + /// [`Clip::transform`]: `None` over a gap and for a clip at its fit | |
| 2767 | + /// policy's own placement. | |
| 2768 | + pub fn composite_transform_at(&self, timeline_frame: u32) -> Option<&TransformParams> { | |
| 2769 | + let (lane, idx) = self.composite_clip_at(timeline_frame)?; | |
| 2770 | + self.transform_of(lane, idx) | |
| 2771 | + } | |
| 2772 | + | |
| 2661 | 2773 | /// How the composite meets the project canvas at `timeline_frame`, so the |
| 2662 | 2774 | /// picture playback composes and the one an export encodes are placed from |
| 2663 | 2775 | /// one answer. `Fit` over a gap, where black is already the canvas. |
crates/engine/src/scale.rsmodified+218 -0
| @@ -26,6 +26,8 @@ | ||
| 26 | 26 | //! chroma-sample bias that no eye finds and that a siting-aware kernel would fix |
| 27 | 27 | //! in the same place the Lanczos upgrade goes. |
| 28 | 28 | |
| 29 | +use crate::transform::TransformParams; | |
| 30 | + | |
| 29 | 31 | /// Limited-range black: what an untouched canvas pixel is. |
| 30 | 32 | pub const BLACK_Y: u8 = 16; |
| 31 | 33 | /// Neutral chroma: 128 in both planes. |
| @@ -330,6 +332,130 @@ pub fn compose_i420( | ||
| 330 | 332 | } |
| 331 | 333 | } |
| 332 | 334 | |
| 335 | +/// The source rect [`TransformParams`]'s crop fractions cut out of a `src_w` x | |
| 336 | +/// `src_h` picture, chroma-grid aligned exactly as [`fit_rect`]'s rects are. | |
| 337 | +/// Fractions are clamped to `0.0..=0.5` a side, so opposing crops can never | |
| 338 | +/// meet or cross -- the smallest surviving rect is one chroma sample, for | |
| 339 | +/// [`align`]'s reason. | |
| 340 | +pub fn crop_rect(src_w: u32, src_h: u32, t: &TransformParams) -> Rect { | |
| 341 | + if src_w == 0 || src_h == 0 { | |
| 342 | + return Rect { x: 0, y: 0, w: 0, h: 0 }; | |
| 343 | + } | |
| 344 | + let (cl, cr) = (t.crop_l.clamp(0.0, 0.5), t.crop_r.clamp(0.0, 0.5)); | |
| 345 | + let (ct, cb) = (t.crop_t.clamp(0.0, 0.5), t.crop_b.clamp(0.0, 0.5)); | |
| 346 | + let x0 = (f64::from(src_w) * f64::from(cl)).round() as u32; | |
| 347 | + let x1 = (f64::from(src_w) * (1.0 - f64::from(cr))).round() as u32; | |
| 348 | + let y0 = (f64::from(src_h) * f64::from(ct)).round() as u32; | |
| 349 | + let y1 = (f64::from(src_h) * (1.0 - f64::from(cb))).round() as u32; | |
| 350 | + let (x, w) = align(x0.min(src_w), x1.saturating_sub(x0), src_w); | |
| 351 | + let (y, h) = align(y0.min(src_h), y1.saturating_sub(y0), src_h); | |
| 352 | + Rect { x, y, w, h } | |
| 353 | +} | |
| 354 | + | |
| 355 | +/// `dst` -- a placement [`fit_rect`] already worked out -- moved by | |
| 356 | +/// [`TransformParams::pos_x`]/`pos_y` (canvas fractions, about `dst`'s own | |
| 357 | +/// centre) and resized by `scale` (about that same centre), clamped onto the | |
| 358 | +/// canvas and chroma-grid aligned like every other rect here. | |
| 359 | +/// | |
| 360 | +/// corner-cut: a placement pushed partway off the canvas is clipped at the | |
| 361 | +/// edge (what [`compose_i420`] already does to any rect), not wrapped or | |
| 362 | +/// reflected -- there is no picture to draw once it is entirely off, and | |
| 363 | +/// [`compose_i420`] already refuses that case by returning without a write. | |
| 364 | +pub fn transformed_dst_rect(dst: Rect, t: &TransformParams, canvas_w: u32, canvas_h: u32) -> Rect { | |
| 365 | + if dst.is_empty() || canvas_w == 0 || canvas_h == 0 { | |
| 366 | + return dst; | |
| 367 | + } | |
| 368 | + let scale = if t.scale.is_finite() && t.scale > 0.0 { | |
| 369 | + f64::from(t.scale) | |
| 370 | + } else { | |
| 371 | + 1.0 | |
| 372 | + }; | |
| 373 | + let new_w = ((f64::from(dst.w) * scale).round() as i64).clamp(1, i64::from(canvas_w)); | |
| 374 | + let new_h = ((f64::from(dst.h) * scale).round() as i64).clamp(1, i64::from(canvas_h)); | |
| 375 | + let pos_x = if t.pos_x.is_finite() { f64::from(t.pos_x) } else { 0.0 }; | |
| 376 | + let pos_y = if t.pos_y.is_finite() { f64::from(t.pos_y) } else { 0.0 }; | |
| 377 | + let cx = i64::from(dst.x) + i64::from(dst.w) / 2 + (f64::from(canvas_w) * pos_x).round() as i64; | |
| 378 | + let cy = i64::from(dst.y) + i64::from(dst.h) / 2 + (f64::from(canvas_h) * pos_y).round() as i64; | |
| 379 | + let x = (cx - new_w / 2).clamp(0, i64::from(canvas_w)) as u32; | |
| 380 | + let y = (cy - new_h / 2).clamp(0, i64::from(canvas_h)) as u32; | |
| 381 | + let (x, w) = align(x, new_w as u32, canvas_w); | |
| 382 | + let (y, h) = align(y, new_h as u32, canvas_h); | |
| 383 | + Rect { x, y, w, h } | |
| 384 | +} | |
| 385 | + | |
| 386 | +/// Nearest 90-degree step `degrees` renders at: `0` (0deg), `1` (90deg | |
| 387 | +/// clockwise), `2` (180deg) or `3` (270deg). Continuous rotation is not | |
| 388 | +/// rendered at all -- see the [`crate::transform`] module docs -- so this is | |
| 389 | +/// the one lossy step between what a project *stores* and what a frame | |
| 390 | +/// *shows*. Non-finite folds to `0`. | |
| 391 | +pub fn nearest_90_steps(degrees: f32) -> u8 { | |
| 392 | + if !degrees.is_finite() { | |
| 393 | + return 0; | |
| 394 | + } | |
| 395 | + let norm = degrees.rem_euclid(360.0); | |
| 396 | + ((norm / 90.0).round() as i64).rem_euclid(4) as u8 | |
| 397 | +} | |
| 398 | + | |
| 399 | +/// Rotates a tightly packed I420 frame by `steps` quarter turns clockwise | |
| 400 | +/// (`steps % 4`; see [`nearest_90_steps`]). `0` and `2` keep the frame's | |
| 401 | +/// shape, `1` and `3` swap it -- the chroma planes rotate the same way at | |
| 402 | +/// their own `(w+1)/2` x `(h+1)/2` size, which stays exact because it is | |
| 403 | +/// exactly the luma size's, swapped the same way. | |
| 404 | +pub fn rotate_i420_90s( | |
| 405 | + y: &[u8], | |
| 406 | + u: &[u8], | |
| 407 | + v: &[u8], | |
| 408 | + w: u32, | |
| 409 | + h: u32, | |
| 410 | + steps: u8, | |
| 411 | +) -> (Vec<u8>, Vec<u8>, Vec<u8>, u32, u32) { | |
| 412 | + let steps = steps % 4; | |
| 413 | + if steps == 0 || w == 0 || h == 0 { | |
| 414 | + return (y.to_vec(), u.to_vec(), v.to_vec(), w, h); | |
| 415 | + } | |
| 416 | + let (cw, ch) = chroma_dims(w as usize, h as usize); | |
| 417 | + let (ry, rw, rh) = rotate_plane(y, w as usize, h as usize, steps); | |
| 418 | + let (ru, ..) = rotate_plane(u, cw, ch, steps); | |
| 419 | + let (rv, ..) = rotate_plane(v, cw, ch, steps); | |
| 420 | + (ry, ru, rv, rw as u32, rh as u32) | |
| 421 | +} | |
| 422 | + | |
| 423 | +/// One tightly packed plane, rotated `steps` (1..=3) quarter turns clockwise. | |
| 424 | +fn rotate_plane(src: &[u8], w: usize, h: usize, steps: u8) -> (Vec<u8>, usize, usize) { | |
| 425 | + match steps { | |
| 426 | + 1 => { | |
| 427 | + let (nw, nh) = (h, w); | |
| 428 | + let mut out = vec![0u8; nw * nh]; | |
| 429 | + for yp in 0..nh { | |
| 430 | + for xp in 0..nw { | |
| 431 | + out[yp * nw + xp] = src[(h - 1 - xp) * w + yp]; | |
| 432 | + } | |
| 433 | + } | |
| 434 | + (out, nw, nh) | |
| 435 | + } | |
| 436 | + 2 => { | |
| 437 | + let mut out = vec![0u8; w * h]; | |
| 438 | + for yp in 0..h { | |
| 439 | + for xp in 0..w { | |
| 440 | + out[yp * w + xp] = src[(h - 1 - yp) * w + (w - 1 - xp)]; | |
| 441 | + } | |
| 442 | + } | |
| 443 | + (out, w, h) | |
| 444 | + } | |
| 445 | + 3 => { | |
| 446 | + let (nw, nh) = (h, w); | |
| 447 | + let mut out = vec![0u8; nw * nh]; | |
| 448 | + for yp in 0..nh { | |
| 449 | + for xp in 0..nw { | |
| 450 | + out[yp * nw + xp] = src[xp * w + (w - 1 - yp)]; | |
| 451 | + } | |
| 452 | + } | |
| 453 | + (out, nw, nh) | |
| 454 | + } | |
| 455 | + _ => unreachable!("steps % 4 is 1, 2 or 3 here"), | |
| 456 | + } | |
| 457 | +} | |
| 458 | + | |
| 333 | 459 | /// One clip's pictures placed on the project canvas: the geometry, plus the |
| 334 | 460 | /// buffers it composes into so a frame costs no allocation. |
| 335 | 461 | /// |
| @@ -485,6 +611,98 @@ impl Composer { | ||
| 485 | 611 | compose_i420(ky, ku, kv, w, h, pic_y, pic_u, pic_v, dst); |
| 486 | 612 | (ky, ku, kv, self.width, self.height) |
| 487 | 613 | } |
| 614 | + | |
| 615 | + /// [`place`](Self::place) with a per-clip [`TransformParams`] applied on | |
| 616 | + /// top of the fit policy: the source is cropped to [`crop_rect`], rotated | |
| 617 | + /// by [`rotate_i420_90s`] to [`nearest_90_steps`] of `t.rotate`, fit to the | |
| 618 | + /// canvas exactly as an untransformed picture is, and the fitted | |
| 619 | + /// placement is then moved/resized by [`transformed_dst_rect`]. | |
| 620 | + /// `t.is_identity()` takes the exact [`place`](Self::place) path. | |
| 621 | + /// | |
| 622 | + /// corner-cut: unlike `place`, this always allocates its crop and rotate | |
| 623 | + /// buffers fresh rather than reusing `self`'s -- the grade path already | |
| 624 | + /// pays the same per-frame cost for a graded clip | |
| 625 | + /// ([`crate::decode::Render::frame`]'s `graded` copy), so a transformed | |
| 626 | + /// clip follows that precedent instead of adding a second steady-state | |
| 627 | + /// buffer budget. Upgrade path if this shows up in a measurement: give | |
| 628 | + /// `Composer` its own crop/rotate scratch fields, sized once like `crop` | |
| 629 | + /// and `scaled` already are. | |
| 630 | + pub fn place_transformed( | |
| 631 | + &mut self, | |
| 632 | + y: &[u8], | |
| 633 | + u: &[u8], | |
| 634 | + v: &[u8], | |
| 635 | + src_w: u32, | |
| 636 | + src_h: u32, | |
| 637 | + t: &TransformParams, | |
| 638 | + ) -> (Vec<u8>, Vec<u8>, Vec<u8>, u32, u32) { | |
| 639 | + if t.is_identity() { | |
| 640 | + let (y, u, v, w, h) = self.place(y, u, v, src_w, src_h); | |
| 641 | + return (y.to_vec(), u.to_vec(), v.to_vec(), w, h); | |
| 642 | + } | |
| 643 | + // A pass-through canvas still has a picture to place a transform on; | |
| 644 | + // treat it as a canvas the exact size of the incoming picture, so a | |
| 645 | + // transformed clip at the project's own resolution still crops, | |
| 646 | + // rotates and moves instead of skipping straight through. | |
| 647 | + let (canvas_w, canvas_h) = if self.width == 0 || self.height == 0 { | |
| 648 | + (src_w, src_h) | |
| 649 | + } else { | |
| 650 | + (self.width, self.height) | |
| 651 | + }; | |
| 652 | + | |
| 653 | + let crop = crop_rect(src_w, src_h, t); | |
| 654 | + let mut buf = (Vec::new(), Vec::new(), Vec::new()); | |
| 655 | + let (cy, cu, cv) = if (crop.w, crop.h) == (src_w, src_h) { | |
| 656 | + (y, u, v) | |
| 657 | + } else { | |
| 658 | + crop_i420(&mut buf, y, u, v, src_w as usize, src_h as usize, crop); | |
| 659 | + (&buf.0[..], &buf.1[..], &buf.2[..]) | |
| 660 | + }; | |
| 661 | + | |
| 662 | + let steps = nearest_90_steps(t.rotate); | |
| 663 | + let (ry, ru, rv, rw, rh) = rotate_i420_90s(cy, cu, cv, crop.w, crop.h, steps); | |
| 664 | + | |
| 665 | + let (dst, src) = fit_rect(rw, rh, canvas_w, canvas_h, self.policy); | |
| 666 | + let (rw_u, rh_u) = (rw as usize, rh as usize); | |
| 667 | + let (fy, fu, fv) = if (src.w, src.h) == (rw, rh) { | |
| 668 | + (&ry[..], &ru[..], &rv[..]) | |
| 669 | + } else { | |
| 670 | + crop_i420(&mut buf, &ry, &ru, &rv, rw_u, rh_u, src); | |
| 671 | + (&buf.0[..], &buf.1[..], &buf.2[..]) | |
| 672 | + }; | |
| 673 | + | |
| 674 | + let dst = transformed_dst_rect(dst, t, canvas_w, canvas_h); | |
| 675 | + let (sw, sh) = (src.w as usize, src.h as usize); | |
| 676 | + let (dw, dh) = (dst.w as usize, dst.h as usize); | |
| 677 | + let mut scaled = (Vec::new(), Vec::new(), Vec::new()); | |
| 678 | + let (py, pu, pv) = if (sw, sh) == (dw, dh) { | |
| 679 | + (fy, fu, fv) | |
| 680 | + } else { | |
| 681 | + let (ccw, cch) = chroma_dims(dw, dh); | |
| 682 | + scaled.0.resize(dw * dh, 0); | |
| 683 | + scaled.1.resize(ccw * cch, 0); | |
| 684 | + scaled.2.resize(ccw * cch, 0); | |
| 685 | + scale_i420( | |
| 686 | + fy, fu, fv, sw, sh, &mut scaled.0, &mut scaled.1, &mut scaled.2, dw, dh, | |
| 687 | + ); | |
| 688 | + (&scaled.0[..], &scaled.1[..], &scaled.2[..]) | |
| 689 | + }; | |
| 690 | + | |
| 691 | + let (w, h) = (canvas_w as usize, canvas_h as usize); | |
| 692 | + let mut canvas = black_i420(w, h); | |
| 693 | + compose_i420( | |
| 694 | + &mut canvas.0, | |
| 695 | + &mut canvas.1, | |
| 696 | + &mut canvas.2, | |
| 697 | + w, | |
| 698 | + h, | |
| 699 | + py, | |
| 700 | + pu, | |
| 701 | + pv, | |
| 702 | + dst, | |
| 703 | + ); | |
| 704 | + (canvas.0, canvas.1, canvas.2, canvas_w, canvas_h) | |
| 705 | + } | |
| 488 | 706 | } |
| 489 | 707 | |
| 490 | 708 | /// Resizes `plane` to `len` and sets every byte to `value` -- a canvas reused |
crates/engine/src/transform.rsadded+81 -0
| @@ -0,0 +1,81 @@ | ||
| 1 | +//! Per-clip position/scale/rotate/crop, applied engine-side on the placed | |
| 2 | +//! picture so that playback and export land the same pixel from the same | |
| 3 | +//! numbers -- [`crate::color::ColorParams`]'s twin in every respect but what | |
| 4 | +//! it moves: a grade changes what a sample *is*, this changes where it *goes*. | |
| 5 | +//! | |
| 6 | +//! Plain data with no invariants of its own: out-of-range or non-finite | |
| 7 | +//! values are clamped where the geometry is built ([`crate::scale::crop_rect`], | |
| 8 | +//! [`crate::scale::transformed_dst_rect`]), so a deserializer can hand this | |
| 9 | +//! over unchecked, exactly as [`ColorParams`](crate::color::ColorParams) can. | |
| 10 | + | |
| 11 | +/// One clip's placement on the project canvas, on top of whatever | |
| 12 | +/// [`crate::scale::FitPolicy`] already does. | |
| 13 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 14 | +pub struct TransformParams { | |
| 15 | + /// Canvas-fraction offset from centred, positive right. `0.0` is | |
| 16 | + /// identity. | |
| 17 | + pub pos_x: f32, | |
| 18 | + /// Canvas-fraction offset from centred, positive down. `0.0` is identity. | |
| 19 | + pub pos_y: f32, | |
| 20 | + /// Multiplier on the fit-policy placement size. `1.0` is identity. | |
| 21 | + pub scale: f32, | |
| 22 | + /// Degrees, clockwise. Rendered at the nearest 90-degree step | |
| 23 | + /// ([`crate::scale::rotate_i420_90s`]); the value itself is kept exactly, | |
| 24 | + /// so a UI that later grows continuous rotation loses nothing already | |
| 25 | + /// saved. `0.0` is identity. | |
| 26 | + pub rotate: f32, | |
| 27 | + /// Fraction of the source cropped off the left, `0.0..0.5`. | |
| 28 | + pub crop_l: f32, | |
| 29 | + /// Fraction of the source cropped off the right, `0.0..0.5`. | |
| 30 | + pub crop_r: f32, | |
| 31 | + /// Fraction of the source cropped off the top, `0.0..0.5`. | |
| 32 | + pub crop_t: f32, | |
| 33 | + /// Fraction of the source cropped off the bottom, `0.0..0.5`. | |
| 34 | + pub crop_b: f32, | |
| 35 | +} | |
| 36 | + | |
| 37 | +impl Default for TransformParams { | |
| 38 | + fn default() -> Self { | |
| 39 | + Self { | |
| 40 | + pos_x: 0.0, | |
| 41 | + pos_y: 0.0, | |
| 42 | + scale: 1.0, | |
| 43 | + rotate: 0.0, | |
| 44 | + crop_l: 0.0, | |
| 45 | + crop_r: 0.0, | |
| 46 | + crop_t: 0.0, | |
| 47 | + crop_b: 0.0, | |
| 48 | + } | |
| 49 | + } | |
| 50 | +} | |
| 51 | + | |
| 52 | +impl TransformParams { | |
| 53 | + /// Whether this is the do-nothing setting. Compared exactly, for | |
| 54 | + /// [`crate::color::ColorParams::is_identity`]'s reason: only a value that | |
| 55 | + /// is precisely identity is allowed to skip the geometry. | |
| 56 | + pub fn is_identity(&self) -> bool { | |
| 57 | + self.pos_x == 0.0 | |
| 58 | + && self.pos_y == 0.0 | |
| 59 | + && self.scale == 1.0 | |
| 60 | + && self.rotate == 0.0 | |
| 61 | + && self.crop_l == 0.0 | |
| 62 | + && self.crop_r == 0.0 | |
| 63 | + && self.crop_t == 0.0 | |
| 64 | + && self.crop_b == 0.0 | |
| 65 | + } | |
| 66 | +} | |
| 67 | + | |
| 68 | +#[cfg(test)] | |
| 69 | +mod tests { | |
| 70 | + use super::*; | |
| 71 | + | |
| 72 | + #[test] | |
| 73 | + fn default_is_identity() { | |
| 74 | + assert!(TransformParams::default().is_identity()); | |
| 75 | + assert!(!TransformParams { | |
| 76 | + pos_x: 0.001, | |
| 77 | + ..TransformParams::default() | |
| 78 | + } | |
| 79 | + .is_identity()); | |
| 80 | + } | |
| 81 | +} |