A moved placement clips instead of squeezing the source
Browse files at this commitef5832fea151ca30db92b142c8de131c5f43fc49Parent: 6b8d7dd- fix
- a moved placement clips instead of squeezing the source
2 changed files +257 -65
- crates/app/src/ui/overlays.rs +8 -1
- crates/engine/src/scale.rs +249 -64
crates/app/src/ui/overlays.rsmodified+8 -1
| @@ -559,7 +559,13 @@ impl Player { | ||
| 559 | 559 | }; |
| 560 | 560 | rows.push( |
| 561 | 561 | row(rows.len()) |
| 562 | - .child(label) | |
| 562 | + // Truncated and shrinkable: a long label ("Transform — | |
| 563 | + // position, scale, rotation, crop") used to run the | |
| 564 | + // hint column clean off the fixed-width menu instead | |
| 565 | + // of sharing the row with it, which is why Transform | |
| 566 | + // showed no stroke while a short label like Colour's | |
| 567 | + // did. | |
| 568 | + .child(div().min_w(px(0.)).flex_shrink().truncate().child(label)) | |
| 563 | 569 | .child(match refusal.why() { |
| 564 | 570 | // One truncated line, like the details side: a |
| 565 | 571 | // reason that wrapped would make the card taller |
| @@ -571,6 +577,7 @@ impl Player { | ||
| 571 | 577 | .text_color(rgb(FG_SECONDARY())) |
| 572 | 578 | .child(why), |
| 573 | 579 | None => div() |
| 580 | + .flex_shrink_0() | |
| 574 | 581 | .text_color(rgb(FG_SECONDARY())) |
| 575 | 582 | .child(self.keymap.display(action)), |
| 576 | 583 | }) |
crates/engine/src/scale.rsmodified+249 -64
| @@ -352,35 +352,85 @@ pub fn crop_rect(src_w: u32, src_h: u32, t: &TransformParams) -> Rect { | ||
| 352 | 352 | Rect { x, y, w, h } |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | -/// `dst` -- a placement [`fit_rect`] already worked out -- moved by | |
| 355 | +/// `dst` -- a placement [`fit_rect`] already worked out, paired with the `src` | |
| 356 | +/// rect (inside a `src_w` x `src_h` picture) it is sourced from -- moved by | |
| 356 | 357 | /// [`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. | |
| 358 | +/// centre) and resized by `scale` (about that same centre). | |
| 359 | 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; | |
| 360 | +/// Translation and scale never distort the picture: the placed box keeps the | |
| 361 | +/// aspect its size and `scale` give it, full stop. What moving it off-canvas | |
| 362 | +/// does is clip -- the part of the box that falls outside `0..canvas_w` / | |
| 363 | +/// `0..canvas_h` is dropped, and the matching fraction of `src` is trimmed | |
| 364 | +/// off the same sides, so what remains on screen is an unscaled slice of the | |
| 365 | +/// source, not the whole source squeezed into whatever box survived | |
| 366 | +/// clamping. Both rects come back chroma-grid aligned like every other rect | |
| 367 | +/// here; a placement pushed entirely off the canvas comes back as two empty | |
| 368 | +/// rects, which is what [`compose_i420`] already treats as nothing to draw. | |
| 369 | +pub fn transformed_dst_rect( | |
| 370 | + dst: Rect, | |
| 371 | + src: Rect, | |
| 372 | + src_w: u32, | |
| 373 | + src_h: u32, | |
| 374 | + t: &TransformParams, | |
| 375 | + canvas_w: u32, | |
| 376 | + canvas_h: u32, | |
| 377 | +) -> (Rect, Rect) { | |
| 378 | + let empty = Rect { x: 0, y: 0, w: 0, h: 0 }; | |
| 379 | + if dst.is_empty() || src.is_empty() || canvas_w == 0 || canvas_h == 0 { | |
| 380 | + return (dst, src); | |
| 367 | 381 | } |
| 368 | 382 | let scale = if t.scale.is_finite() && t.scale > 0.0 { |
| 369 | 383 | f64::from(t.scale) |
| 370 | 384 | } else { |
| 371 | 385 | 1.0 |
| 372 | 386 | }; |
| 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)); | |
| 387 | + // Bounded well above any real canvas so the src-trim ratios below cannot | |
| 388 | + // overflow i64, however extreme a stored scale value is. | |
| 389 | + let cap_w = i64::from(canvas_w.max(1)) * 1024; | |
| 390 | + let cap_h = i64::from(canvas_h.max(1)) * 1024; | |
| 391 | + let new_w = ((f64::from(dst.w) * scale).round() as i64).clamp(1, cap_w); | |
| 392 | + let new_h = ((f64::from(dst.h) * scale).round() as i64).clamp(1, cap_h); | |
| 375 | 393 | let pos_x = if t.pos_x.is_finite() { f64::from(t.pos_x) } else { 0.0 }; |
| 376 | 394 | let pos_y = if t.pos_y.is_finite() { f64::from(t.pos_y) } else { 0.0 }; |
| 377 | 395 | let cx = i64::from(dst.x) + i64::from(dst.w) / 2 + (f64::from(canvas_w) * pos_x).round() as i64; |
| 378 | 396 | 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 } | |
| 397 | + // The full box the transform describes, unclamped -- it may start | |
| 398 | + // negative or run past either edge, which is exactly what "moved off the | |
| 399 | + // canvas" means. | |
| 400 | + let bx = cx - new_w / 2; | |
| 401 | + let by = cy - new_h / 2; | |
| 402 | + | |
| 403 | + // How much of that box, in the box's own pixels, falls outside the | |
| 404 | + // canvas on each side. | |
| 405 | + let left = (-bx).clamp(0, new_w); | |
| 406 | + let top = (-by).clamp(0, new_h); | |
| 407 | + let right = (bx + new_w - i64::from(canvas_w)).clamp(0, new_w); | |
| 408 | + let bottom = (by + new_h - i64::from(canvas_h)).clamp(0, new_h); | |
| 409 | + if left + right >= new_w || top + bottom >= new_h { | |
| 410 | + return (empty, empty); | |
| 411 | + } | |
| 412 | + | |
| 413 | + let dst_x = (bx + left).clamp(0, i64::from(canvas_w)) as u32; | |
| 414 | + let dst_y = (by + top).clamp(0, i64::from(canvas_h)) as u32; | |
| 415 | + let (dst_x, dst_w) = align(dst_x, (new_w - left - right) as u32, canvas_w); | |
| 416 | + let (dst_y, dst_h) = align(dst_y, (new_h - top - bottom) as u32, canvas_h); | |
| 417 | + | |
| 418 | + // The same fraction trimmed off `src`, on the matching side: uniform | |
| 419 | + // scale means the ratio of trimmed-box-pixels to source-pixels is the | |
| 420 | + // same on both axes as the ratio of the box's own size to `src`'s. | |
| 421 | + let src_left = i64::from(src.w) * left / new_w; | |
| 422 | + let src_right = i64::from(src.w) * right / new_w; | |
| 423 | + let src_top = i64::from(src.h) * top / new_h; | |
| 424 | + let src_bottom = i64::from(src.h) * bottom / new_h; | |
| 425 | + let src_x = src.x + src_left as u32; | |
| 426 | + let src_y = src.y + src_top as u32; | |
| 427 | + let (src_x, src_w_) = align(src_x, src.w.saturating_sub((src_left + src_right) as u32), src_w); | |
| 428 | + let (src_y, src_h_) = align(src_y, src.h.saturating_sub((src_top + src_bottom) as u32), src_h); | |
| 429 | + | |
| 430 | + ( | |
| 431 | + Rect { x: dst_x, y: dst_y, w: dst_w, h: dst_h }, | |
| 432 | + Rect { x: src_x, y: src_y, w: src_w_, h: src_h_ }, | |
| 433 | + ) | |
| 384 | 434 | } |
| 385 | 435 | |
| 386 | 436 | /// Nearest 90-degree step `degrees` renders at: `0` (0deg), `1` (90deg |
| @@ -616,7 +666,9 @@ impl Composer { | ||
| 616 | 666 | /// top of the fit policy: the source is cropped to [`crop_rect`], rotated |
| 617 | 667 | /// by [`rotate_i420_90s`] to [`nearest_90_steps`] of `t.rotate`, fit to the |
| 618 | 668 | /// canvas exactly as an untransformed picture is, and the fitted |
| 619 | - /// placement is then moved/resized by [`transformed_dst_rect`]. | |
| 669 | + /// placement is then moved/resized/clipped by [`transformed_dst_rect`], | |
| 670 | + /// which also trims the matching slice off the source rect so an | |
| 671 | + /// off-canvas move clips instead of squeezing. | |
| 620 | 672 | /// `t.is_identity()` takes the exact [`place`](Self::place) path. |
| 621 | 673 | /// |
| 622 | 674 | /// corner-cut: unlike `place`, this always allocates its crop and rotate |
| @@ -651,27 +703,41 @@ impl Composer { | ||
| 651 | 703 | }; |
| 652 | 704 | |
| 653 | 705 | let crop = crop_rect(src_w, src_h, t); |
| 654 | - let mut buf = (Vec::new(), Vec::new(), Vec::new()); | |
| 706 | + let mut buf1 = (Vec::new(), Vec::new(), Vec::new()); | |
| 655 | 707 | let (cy, cu, cv) = if (crop.w, crop.h) == (src_w, src_h) { |
| 656 | 708 | (y, u, v) |
| 657 | 709 | } 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[..]) | |
| 710 | + crop_i420(&mut buf1, y, u, v, src_w as usize, src_h as usize, crop); | |
| 711 | + (&buf1.0[..], &buf1.1[..], &buf1.2[..]) | |
| 660 | 712 | }; |
| 661 | 713 | |
| 714 | + // No rotation is the overwhelmingly common case (a move or a scale, | |
| 715 | + // never a quarter turn); `rotate_i420_90s` always copies its whole | |
| 716 | + // picture even at zero steps, so that copy -- three full-resolution | |
| 717 | + // planes, every decoded frame -- is skipped here rather than paid on | |
| 718 | + // a transform that never rotates. This is the settle-lag fix: it is | |
| 719 | + // what made a plain position/scale edit visibly slower to catch up | |
| 720 | + // to than a colour grade, which never copies the picture at all. | |
| 662 | 721 | 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); | |
| 722 | + let rot_buf; | |
| 723 | + let (ry, ru, rv, rw, rh) = if steps == 0 { | |
| 724 | + (cy, cu, cv, crop.w, crop.h) | |
| 725 | + } else { | |
| 726 | + rot_buf = rotate_i420_90s(cy, cu, cv, crop.w, crop.h, steps); | |
| 727 | + (&rot_buf.0[..], &rot_buf.1[..], &rot_buf.2[..], rot_buf.3, rot_buf.4) | |
| 728 | + }; | |
| 664 | 729 | |
| 665 | 730 | let (dst, src) = fit_rect(rw, rh, canvas_w, canvas_h, self.policy); |
| 731 | + let (dst, src) = transformed_dst_rect(dst, src, rw, rh, t, canvas_w, canvas_h); | |
| 666 | 732 | let (rw_u, rh_u) = (rw as usize, rh as usize); |
| 733 | + let mut buf2 = (Vec::new(), Vec::new(), Vec::new()); | |
| 667 | 734 | let (fy, fu, fv) = if (src.w, src.h) == (rw, rh) { |
| 668 | - (&ry[..], &ru[..], &rv[..]) | |
| 735 | + (ry, ru, rv) | |
| 669 | 736 | } else { |
| 670 | - crop_i420(&mut buf, &ry, &ru, &rv, rw_u, rh_u, src); | |
| 671 | - (&buf.0[..], &buf.1[..], &buf.2[..]) | |
| 737 | + crop_i420(&mut buf2, ry, ru, rv, rw_u, rh_u, src); | |
| 738 | + (&buf2.0[..], &buf2.1[..], &buf2.2[..]) | |
| 672 | 739 | }; |
| 673 | 740 | |
| 674 | - let dst = transformed_dst_rect(dst, t, canvas_w, canvas_h); | |
| 675 | 741 | let (sw, sh) = (src.w as usize, src.h as usize); |
| 676 | 742 | let (dw, dh) = (dst.w as usize, dst.h as usize); |
| 677 | 743 | let mut scaled = (Vec::new(), Vec::new(), Vec::new()); |
| @@ -1382,32 +1448,68 @@ mod tests { | ||
| 1382 | 1448 | } |
| 1383 | 1449 | |
| 1384 | 1450 | /// A placement moved by `pos_x`/`pos_y` (canvas fractions about its own |
| 1385 | - /// centre) and resized by `scale`, both about that same centre. | |
| 1451 | + /// centre) and resized by `scale`, both about that same centre. Neither | |
| 1452 | + /// case here clips, so the source rect comes back untouched. | |
| 1386 | 1453 | #[test] |
| 1387 | 1454 | fn transformed_dst_rect_at_pos_and_scale() { |
| 1388 | 1455 | let dst = Rect { x: 0, y: 0, w: 100, h: 100 }; |
| 1456 | + let src = Rect { x: 0, y: 0, w: 100, h: 100 }; | |
| 1389 | 1457 | // Moved a quarter of the canvas to the right, untouched vertically. |
| 1390 | 1458 | let moved = TransformParams { |
| 1391 | 1459 | pos_x: 0.25, |
| 1392 | 1460 | ..Default::default() |
| 1393 | 1461 | }; |
| 1394 | 1462 | assert_eq!( |
| 1395 | - transformed_dst_rect(dst, &moved, 200, 200), | |
| 1396 | - Rect { x: 50, y: 0, w: 100, h: 100 }, | |
| 1463 | + transformed_dst_rect(dst, src, 100, 100, &moved, 200, 200), | |
| 1464 | + (Rect { x: 50, y: 0, w: 100, h: 100 }, src), | |
| 1397 | 1465 | "25% of a 200-wide canvas is 50 pixels, about the placement's own centre" |
| 1398 | 1466 | ); |
| 1399 | - // Doubled in place: the centre stays, the box fills the canvas. | |
| 1467 | + // Doubled in place, box already centred on the canvas: the centre | |
| 1468 | + // stays, the box fills the canvas, nothing clips. | |
| 1469 | + let centred_dst = Rect { x: 50, y: 50, w: 100, h: 100 }; | |
| 1400 | 1470 | let grown = TransformParams { |
| 1401 | 1471 | scale: 2.0, |
| 1402 | 1472 | ..Default::default() |
| 1403 | 1473 | }; |
| 1404 | 1474 | assert_eq!( |
| 1405 | - transformed_dst_rect(dst, &grown, 200, 200), | |
| 1406 | - Rect { x: 0, y: 0, w: 200, h: 200 }, | |
| 1475 | + transformed_dst_rect(centred_dst, src, 100, 100, &grown, 200, 200), | |
| 1476 | + (Rect { x: 0, y: 0, w: 200, h: 200 }, src), | |
| 1407 | 1477 | "a 100x100 box doubled about its own centre on a 200x200 canvas fills it" |
| 1408 | 1478 | ); |
| 1409 | 1479 | } |
| 1410 | 1480 | |
| 1481 | + /// The defect this replaces: a positive offset used to SHRINK the dst | |
| 1482 | + /// rect (squeezing the whole source into what remained) and a negative | |
| 1483 | + /// offset used to render pixel-identical to the identity (the clamp at 0 | |
| 1484 | + /// killed it). Real translation keeps the box's size and clips instead: | |
| 1485 | + /// the src rect trims by the same fraction that fell off-canvas, on the | |
| 1486 | + /// side that fell off, and the surviving box size never shrinks below | |
| 1487 | + /// what `scale` alone gives it. | |
| 1488 | + #[test] | |
| 1489 | + fn transformed_dst_rect_clips_instead_of_squeezing() { | |
| 1490 | + let dst = Rect { x: 0, y: 0, w: 100, h: 100 }; | |
| 1491 | + let src = Rect { x: 0, y: 0, w: 100, h: 100 }; | |
| 1492 | + // +30% of a 100-wide canvas: the box moves 30px right, half of it | |
| 1493 | + // (50px) falls off the right edge -- half the source is trimmed off | |
| 1494 | + // its right, not the whole source squeezed into 70px. | |
| 1495 | + let pos = TransformParams { pos_x: 0.30, ..Default::default() }; | |
| 1496 | + let (d, s) = transformed_dst_rect(dst, src, 100, 100, &pos, 100, 100); | |
| 1497 | + assert_eq!(d, Rect { x: 30, y: 0, w: 70, h: 100 }, "dst size must not shrink below what clipping removes"); | |
| 1498 | + assert_eq!(s, Rect { x: 0, y: 0, w: 70, h: 100 }, "src trims off its right, does not squeeze"); | |
| 1499 | + | |
| 1500 | + // -30%: symmetric on the left, and no longer identical to identity. | |
| 1501 | + let neg = TransformParams { pos_x: -0.30, ..Default::default() }; | |
| 1502 | + let (d, s) = transformed_dst_rect(dst, src, 100, 100, &neg, 100, 100); | |
| 1503 | + assert_eq!(d, Rect { x: 0, y: 0, w: 70, h: 100 }); | |
| 1504 | + assert_eq!(s, Rect { x: 30, y: 0, w: 70, h: 100 }, "src trims off its left"); | |
| 1505 | + assert_ne!((d, s), (dst, src), "a negative offset must move the picture, not no-op"); | |
| 1506 | + | |
| 1507 | + // Fully off canvas: both rects empty. | |
| 1508 | + let gone = TransformParams { pos_x: 2.0, ..Default::default() }; | |
| 1509 | + let (d, s) = transformed_dst_rect(dst, src, 100, 100, &gone, 100, 100); | |
| 1510 | + assert!(d.is_empty() && s.is_empty(), "wholly off-canvas has nothing to draw"); | |
| 1511 | + } | |
| 1512 | + | |
| 1411 | 1513 | /// [`nearest_90_steps`] rounds to the closest quarter turn and folds a |
| 1412 | 1514 | /// non-finite value to the identity. |
| 1413 | 1515 | #[test] |
| @@ -1461,42 +1563,125 @@ mod tests { | ||
| 1461 | 1563 | assert_eq!((ry, rw, rh), (y, w as u32, h as u32), "four quarter turns is the identity"); |
| 1462 | 1564 | } |
| 1463 | 1565 | |
| 1464 | - /// [`Composer::place_transformed`] at compose level: a picture placed with | |
| 1465 | - /// a `pos_x` offset lands its marked pixel shifted by exactly that many | |
| 1466 | - /// canvas pixels, with black everywhere else. | |
| 1566 | + /// A 64x64 Y plane split down the middle: columns 0..32 are 200, 32..64 | |
| 1567 | + /// are 40. A vertical edge to track through a horizontal move. | |
| 1568 | + fn vsplit_64() -> (Vec<u8>, Vec<u8>, Vec<u8>) { | |
| 1569 | + let y = (0..64 * 64).map(|i| if i % 64 < 32 { 200u8 } else { 40u8 }).collect(); | |
| 1570 | + (y, vec![NEUTRAL_C; 32 * 32], vec![NEUTRAL_C; 32 * 32]) | |
| 1571 | + } | |
| 1572 | + | |
| 1573 | + /// Same split, but across rows: 0..32 are 200, 32..64 are 40 -- an edge | |
| 1574 | + /// to track through a vertical move. | |
| 1575 | + fn hsplit_64() -> (Vec<u8>, Vec<u8>, Vec<u8>) { | |
| 1576 | + let y = (0..64 * 64).map(|i| if i / 64 < 32 { 200u8 } else { 40u8 }).collect(); | |
| 1577 | + (y, vec![NEUTRAL_C; 32 * 32], vec![NEUTRAL_C; 32 * 32]) | |
| 1578 | + } | |
| 1579 | + | |
| 1580 | + /// [`Composer::place_transformed`] at compose level, with the exact | |
| 1581 | + /// defect the geometry used to have: a positive `pos_x` squeezed the | |
| 1582 | + /// whole source into a shrunk box (content narrower, not just displaced) | |
| 1583 | + /// and a negative one rendered pixel-identical to the identity. These | |
| 1584 | + /// assert on the DISPLACED CONTENT (where the source's own edge lands), | |
| 1585 | + /// not on any rect field, so a squeeze that happens to leave rect sizes | |
| 1586 | + /// looking plausible cannot pass silently. | |
| 1467 | 1587 | #[test] |
| 1468 | - fn place_transformed_shifts_a_marked_pixel() { | |
| 1588 | + fn place_transformed_displaces_content_not_just_a_rect() { | |
| 1469 | 1589 | let (src_w, src_h) = (64u32, 64u32); |
| 1470 | - let (y, u, v) = ( | |
| 1471 | - vec![200u8; (src_w * src_h) as usize], | |
| 1472 | - vec![NEUTRAL_C; ((src_w / 2) * (src_h / 2)) as usize], | |
| 1473 | - vec![NEUTRAL_C; ((src_w / 2) * (src_h / 2)) as usize], | |
| 1474 | - ); | |
| 1590 | + let (y, u, v) = vsplit_64(); | |
| 1475 | 1591 | let mut canvas = Composer::new(src_w, src_h, FitPolicy::Fit); |
| 1476 | - // Identity: a pass-through-shaped placement, fills the whole canvas. | |
| 1477 | - let (iy, _, _, iw, ih) = canvas.place_transformed(&y, &u, &v, src_w, src_h, &TransformParams::default()); | |
| 1478 | - assert_eq!((iw, ih), (src_w, src_h)); | |
| 1479 | - assert!(iy.iter().all(|&p| p == 200), "identity fills the canvas"); | |
| 1480 | 1592 | |
| 1481 | - // Moved a quarter of the canvas to the right: the left half is black, | |
| 1482 | - // the right half (minus the strip pushed off the edge) is the picture. | |
| 1483 | - let moved = TransformParams { | |
| 1484 | - pos_x: 0.25, | |
| 1485 | - ..Default::default() | |
| 1486 | - }; | |
| 1487 | - let (my, _, _, mw, mh) = canvas.place_transformed(&y, &u, &v, src_w, src_h, &moved); | |
| 1488 | - assert_eq!((mw, mh), (src_w, src_h), "canvas size never changes"); | |
| 1489 | - let shift = (f64::from(src_w) * 0.25).round() as usize; | |
| 1490 | - for row in 0..mh as usize { | |
| 1491 | - let r = &my[row * mw as usize..][..mw as usize]; | |
| 1492 | - assert!( | |
| 1493 | - r[..shift].iter().all(|&p| p == BLACK_Y), | |
| 1494 | - "row {row}: black ahead of the shifted picture" | |
| 1495 | - ); | |
| 1496 | - assert!( | |
| 1497 | - r[shift..].iter().all(|&p| p == 200), | |
| 1498 | - "row {row}: the picture fills the rest, shifted right" | |
| 1499 | - ); | |
| 1593 | + // Identity: the source's own edge stays at column 32, everywhere. | |
| 1594 | + let (iy, ..) = canvas.place_transformed(&y, &u, &v, src_w, src_h, &TransformParams::default()); | |
| 1595 | + for row in 0..64 { | |
| 1596 | + let r = &iy[row * 64..][..64]; | |
| 1597 | + assert!(r[..32].iter().all(|&p| p == 200) && r[32..].iter().all(|&p| p == 40), "row {row}: {r:?}"); | |
| 1598 | + } | |
| 1599 | + | |
| 1600 | + // +25%: the box moves right by 16 and does NOT shrink -- content | |
| 1601 | + // width stays 64 (the source's own 32/32 split), clipped to what | |
| 1602 | + // still fits. The edge (originally at box-column 32) now sits at | |
| 1603 | + // canvas column 16 + 32 = 48, not squeezed inward. | |
| 1604 | + let plus = TransformParams { pos_x: 0.25, ..Default::default() }; | |
| 1605 | + let (py, ..) = canvas.place_transformed(&y, &u, &v, src_w, src_h, &plus); | |
| 1606 | + for row in 0..64 { | |
| 1607 | + let r = &py[row * 64..][..64]; | |
| 1608 | + assert!(r[..16].iter().all(|&p| p == BLACK_Y), "row {row} left of box: {r:?}"); | |
| 1609 | + assert!(r[16..48].iter().all(|&p| p == 200), "row {row} squeezed inward: {r:?}"); | |
| 1610 | + assert!(r[48..].iter().all(|&p| p == 40), "row {row} edge moved: {r:?}"); | |
| 1611 | + } | |
| 1612 | + | |
| 1613 | + // -25%: mirror image -- this used to be pixel-identical to identity. | |
| 1614 | + let minus = TransformParams { pos_x: -0.25, ..Default::default() }; | |
| 1615 | + let (ny, ..) = canvas.place_transformed(&y, &u, &v, src_w, src_h, &minus); | |
| 1616 | + assert_ne!(ny, iy, "a negative offset must move the picture, not no-op"); | |
| 1617 | + for row in 0..64 { | |
| 1618 | + let r = &ny[row * 64..][..64]; | |
| 1619 | + assert!(r[..16].iter().all(|&p| p == 200), "row {row}: {r:?}"); | |
| 1620 | + assert!(r[16..48].iter().all(|&p| p == 40), "row {row}: {r:?}"); | |
| 1621 | + assert!(r[48..].iter().all(|&p| p == BLACK_Y), "row {row} right of box: {r:?}"); | |
| 1622 | + } | |
| 1623 | + | |
| 1624 | + // Wholly off-canvas: nothing to draw, all black. | |
| 1625 | + let gone = TransformParams { pos_x: 2.0, ..Default::default() }; | |
| 1626 | + let (gy, ..) = canvas.place_transformed(&y, &u, &v, src_w, src_h, &gone); | |
| 1627 | + assert!(gy.iter().all(|&p| p == BLACK_Y), "off-canvas must render background"); | |
| 1628 | + } | |
| 1629 | + | |
| 1630 | + /// The same defect on the vertical axis: `pos_y` moves content up/down, | |
| 1631 | + /// clipped, never squeezed. | |
| 1632 | + #[test] | |
| 1633 | + fn place_transformed_displaces_content_vertically() { | |
| 1634 | + let (src_w, src_h) = (64u32, 64u32); | |
| 1635 | + let (y, u, v) = hsplit_64(); | |
| 1636 | + let mut canvas = Composer::new(src_w, src_h, FitPolicy::Fit); | |
| 1637 | + | |
| 1638 | + let plus = TransformParams { pos_y: 0.25, ..Default::default() }; | |
| 1639 | + let (py, ..) = canvas.place_transformed(&y, &u, &v, src_w, src_h, &plus); | |
| 1640 | + for row in 0..16 { | |
| 1641 | + assert!(py[row * 64..][..64].iter().all(|&p| p == BLACK_Y), "row {row} above the box"); | |
| 1642 | + } | |
| 1643 | + for row in 16..48 { | |
| 1644 | + assert!(py[row * 64..][..64].iter().all(|&p| p == 200), "row {row} squeezed: {:?}", &py[row * 64..][..4]); | |
| 1645 | + } | |
| 1646 | + for row in 48..64 { | |
| 1647 | + assert!(py[row * 64..][..64].iter().all(|&p| p == 40), "row {row} edge moved"); | |
| 1648 | + } | |
| 1649 | + | |
| 1650 | + let minus = TransformParams { pos_y: -0.25, ..Default::default() }; | |
| 1651 | + let (ny, ..) = canvas.place_transformed(&y, &u, &v, src_w, src_h, &minus); | |
| 1652 | + for row in 0..16 { | |
| 1653 | + assert!(ny[row * 64..][..64].iter().all(|&p| p == 200), "row {row}"); | |
| 1654 | + } | |
| 1655 | + for row in 16..48 { | |
| 1656 | + assert!(ny[row * 64..][..64].iter().all(|&p| p == 40), "row {row}"); | |
| 1657 | + } | |
| 1658 | + for row in 48..64 { | |
| 1659 | + assert!(ny[row * 64..][..64].iter().all(|&p| p == BLACK_Y), "row {row} below the box"); | |
| 1660 | + } | |
| 1661 | + } | |
| 1662 | + | |
| 1663 | + /// Position and scale together: a halved box moved right must place its | |
| 1664 | + /// (also halved) content at the moved-and-scaled location, not squeeze | |
| 1665 | + /// the full source into whatever survived clamping. | |
| 1666 | + #[test] | |
| 1667 | + fn place_transformed_displaces_content_with_scale() { | |
| 1668 | + let (src_w, src_h) = (64u32, 64u32); | |
| 1669 | + let (y, u, v) = vsplit_64(); | |
| 1670 | + let mut canvas = Composer::new(src_w, src_h, FitPolicy::Fit); | |
| 1671 | + // scale 0.5, pos_x +0.25: box becomes 32x32 at canvas (32, 16), | |
| 1672 | + // fully on-canvas. The source's column-32 edge, halved, sits at | |
| 1673 | + // box-local column 16, i.e. canvas column 32 + 16 = 48. | |
| 1674 | + let t = TransformParams { scale: 0.5, pos_x: 0.25, ..Default::default() }; | |
| 1675 | + let (sy, ..) = canvas.place_transformed(&y, &u, &v, src_w, src_h, &t); | |
| 1676 | + for row in 0..64 { | |
| 1677 | + let r = &sy[row * 64..][..64]; | |
| 1678 | + let inside = (16..48).contains(&row); | |
| 1679 | + let want_bg = if inside { r[0] } else { BLACK_Y }; | |
| 1680 | + assert_eq!(want_bg, BLACK_Y, "row {row} col 0 outside the box: {r:?}"); | |
| 1681 | + if inside { | |
| 1682 | + assert!(r[40].abs_diff(200) <= 1, "row {row} col 40 should be the left half: {r:?}"); | |
| 1683 | + assert!(r[56].abs_diff(40) <= 1, "row {row} col 56 should be the right half: {r:?}"); | |
| 1684 | + } | |
| 1500 | 1685 | } |
| 1501 | 1686 | } |
| 1502 | 1687 | } |