1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
45use super::{ImageRef, ImageRefMut};
6use usvg::filter::{ColorChannel, DisplacementMap};
78/// Applies a displacement map.
9///
10/// - `map` pixels should have a **unpremultiplied alpha**.
11/// - `src` pixels can have any alpha method.
12///
13/// `sx` and `sy` indicate canvas scale.
14///
15/// # Panics
16///
17/// When `src`, `map` and `dest` have different sizes.
18pub fn apply(
19 fe: &DisplacementMap,
20 sx: f32,
21 sy: f32,
22 src: ImageRef,
23 map: ImageRef,
24 dest: ImageRefMut,
25) {
26assert!(src.width == map.width && src.width == dest.width);
27assert!(src.height == map.height && src.height == dest.height);
2829let w = src.width as i32;
30let h = src.height as i32;
3132let mut x: u32 = 0;
33let mut y: u32 = 0;
34for pixel in map.data.iter() {
35let calc_offset = |channel| {
36let c = match channel {
37 ColorChannel::B => pixel.b,
38 ColorChannel::G => pixel.g,
39 ColorChannel::R => pixel.r,
40 ColorChannel::A => pixel.a,
41 };
4243 c as f32 / 255.0 - 0.5
44};
4546let dx = calc_offset(fe.x_channel_selector());
47let dy = calc_offset(fe.y_channel_selector());
48let ox = (x as f32 + dx * sx * fe.scale()).round() as i32;
49let oy = (y as f32 + dy * sy * fe.scale()).round() as i32;
5051// TODO: we should use some kind of anti-aliasing when offset is on a pixel border
5253if x < w as u32 && y < h as u32 && ox >= 0 && ox < w && oy >= 0 && oy < h {
54let idx = (oy * w + ox) as usize;
55let idx1 = (y * w as u32 + x) as usize;
56 dest.data[idx1] = src.data[idx];
57 }
5859 x += 1;
60if x == src.width {
61 x = 0;
62 y += 1;
63 }
64 }
65}