| ... | @@ -21,6 +21,7 @@ impl Engine { | ... | @@ -21,6 +21,7 @@ impl Engine { |
| 21 | "Maximum" => 5.0, | 21 | "Maximum" => 5.0, |
| 22 | "Invert" => 6.0, | 22 | "Invert" => 6.0, |
| 23 | "Replace" => 7.0, | 23 | "Replace" => 7.0, |
| | 24 | "Average" => 8.0, |
| 24 | _ => 0.0, // Merge | 25 | _ => 0.0, // Merge |
| 25 | } | 26 | } |
| 26 | } | 27 | } |
| ... | @@ -580,3 +581,61 @@ impl Engine { | ... | @@ -580,3 +581,61 @@ impl Engine { |
| 580 | } | 581 | } |
| 581 | | 582 | |
| 582 | } | 583 | } |
| | 584 | |
| | 585 | #[cfg(test)] |
| | 586 | mod tests { |
| | 587 | use super::*; |
| | 588 | |
| | 589 | /// Runs `MaskCombine` over 4 pixels with alpha bytes `cur` against `prev`. |
| | 590 | fn combine(paint_mode: &str, invert: bool, level: f32, prev: [u8; 4], cur: [u8; 4]) -> Vec<u8> { |
| | 591 | let eng = Engine::new_headless().expect("gpu"); |
| | 592 | let comp = Comp::from_fusion_str(&format!( |
| | 593 | "Composition {{ Tools = ordered() {{ M = EllipseMask {{ Inputs = {{ PaintMode = Input {{ Value = FuID {{ \"{paint_mode}\" }}, }}, }}, }}, }}, }}" |
| | 594 | )) |
| | 595 | .expect("parse"); |
| | 596 | let mode = Engine::paint_mode_id(comp.node(comp.find_node("M").unwrap())); |
| | 597 | let upload = |a: [u8; 4]| { |
| | 598 | let tex = create_texture(&eng.device, [4, 1], 0); |
| | 599 | let px: Vec<u8> = a.iter().flat_map(|&v| [v, v, v, v]).collect(); |
| | 600 | eng.queue.write_texture( |
| | 601 | tex.texture.as_image_copy(), |
| | 602 | &px, |
| | 603 | wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(16), rows_per_image: Some(1) }, |
| | 604 | wgpu::Extent3d { width: 4, height: 1, depth_or_array_layers: 1 }, |
| | 605 | ); |
| | 606 | tex |
| | 607 | }; |
| | 608 | let (prev, cur, dst) = (upload(prev), upload(cur), create_texture(&eng.device, [4, 1], 0)); |
| | 609 | let mut u = UniformParams::default(); |
| | 610 | u.p[0] = [mode, f32::from(u8::from(invert)), level, 1.0]; |
| | 611 | let mut enc = eng.device.create_command_encoder(&Default::default()); |
| | 612 | eng.dispatch_op(&mut enc, ShaderOp::MaskCombine, &cur.view, &eng.dummy.view, &prev.view, &dst, &u); |
| | 613 | eng.queue.submit(Some(enc.finish())); |
| | 614 | eng.readback_rgba(&dst).expect("readback").chunks(4).map(|p| p[3]).collect() |
| | 615 | } |
| | 616 | |
| | 617 | fn assert_close(got: &[u8], want: [u8; 4]) { |
| | 618 | assert!(got.iter().zip(want).all(|(&g, w)| g.abs_diff(w) <= 1), "got {got:?}, want {want:?}"); |
| | 619 | } |
| | 620 | |
| | 621 | #[test] |
| | 622 | fn average_is_arithmetic_mean_of_prev_and_cur() { |
| | 623 | assert_close(&combine("Average", false, 1.0, [255, 0, 255, 128], [0, 204, 204, 255]), [128, 102, 230, 192]); |
| | 624 | } |
| | 625 | |
| | 626 | #[test] |
| | 627 | fn average_applies_level_before_averaging() { |
| | 628 | assert_close(&combine("Average", false, 0.8, [255, 0, 0, 0], [255, 255, 0, 0]), [230, 102, 0, 0]); |
| | 629 | } |
| | 630 | |
| | 631 | #[test] |
| | 632 | fn replace_overwrites_prev_only_where_cur_has_extent() { |
| | 633 | assert_close(&combine("Replace", false, 1.0, [255, 0, 255, 128], [0, 204, 204, 0]), [255, 204, 204, 128]); |
| | 634 | } |
| | 635 | |
| | 636 | #[test] |
| | 637 | fn replace_uses_level_and_invert_for_extent() { |
| | 638 | assert_close(&combine("Replace", false, 0.8, [100; 4], [255, 0, 128, 0]), [204, 100, 102, 100]); |
| | 639 | assert_close(&combine("Replace", true, 1.0, [100; 4], [255, 0, 0, 255]), [100, 255, 255, 100]); |
| | 640 | } |
| | 641 | } |