fast_image_resize/alpha/u8x4/
native.rs1use crate::alpha::common::{div_and_clip, mul_div_255, RECIP_ALPHA};
2use crate::pixels::U8x4;
3use crate::{ImageView, ImageViewMut};
4
5pub(crate) fn multiply_alpha(
6 src_view: &impl ImageView<Pixel = U8x4>,
7 dst_view: &mut impl ImageViewMut<Pixel = U8x4>,
8) {
9 let src_rows = src_view.iter_rows(0);
10 let dst_rows = dst_view.iter_rows_mut(0);
11 let rows = src_rows.zip(dst_rows);
12
13 for (src_row, dst_row) in rows {
14 for (src_pixel, dst_pixel) in src_row.iter().zip(dst_row.iter_mut()) {
15 *dst_pixel = multiply_alpha_pixel(*src_pixel);
16 }
17 }
18}
19
20pub(crate) fn multiply_alpha_inplace(image_view: &mut impl ImageViewMut<Pixel = U8x4>) {
21 let rows = image_view.iter_rows_mut(0);
22 for row in rows {
23 multiply_alpha_row_inplace(row);
24 }
25}
26
27#[inline(always)]
28pub(crate) fn multiply_alpha_row(src_row: &[U8x4], dst_row: &mut [U8x4]) {
29 for (src_pixel, dst_pixel) in src_row.iter().zip(dst_row) {
30 *dst_pixel = multiply_alpha_pixel(*src_pixel);
31 }
32}
33
34#[inline(always)]
35pub(crate) fn multiply_alpha_row_inplace(row: &mut [U8x4]) {
36 for pixel in row.iter_mut() {
37 *pixel = multiply_alpha_pixel(*pixel);
38 }
39}
40
41#[inline(always)]
42fn multiply_alpha_pixel(mut pixel: U8x4) -> U8x4 {
43 let alpha = pixel.0[3];
44 pixel.0 = [
45 mul_div_255(pixel.0[0], alpha),
46 mul_div_255(pixel.0[1], alpha),
47 mul_div_255(pixel.0[2], alpha),
48 alpha,
49 ];
50 pixel
51}
52
53#[inline]
56pub(crate) fn divide_alpha(
57 src_view: &impl ImageView<Pixel = U8x4>,
58 dst_view: &mut impl ImageViewMut<Pixel = U8x4>,
59) {
60 let src_rows = src_view.iter_rows(0);
61 let dst_rows = dst_view.iter_rows_mut(0);
62 let rows = src_rows.zip(dst_rows);
63 for (src_row, dst_row) in rows {
64 divide_alpha_row(src_row, dst_row);
65 }
66}
67
68#[inline]
69pub(crate) fn divide_alpha_inplace(image_view: &mut impl ImageViewMut<Pixel = U8x4>) {
70 let rows = image_view.iter_rows_mut(0);
71 for row in rows {
72 row.iter_mut().for_each(|pixel| {
73 *pixel = divide_alpha_pixel(*pixel);
74 });
75 }
76}
77
78#[inline(always)]
79pub(crate) fn divide_alpha_row(src_row: &[U8x4], dst_row: &mut [U8x4]) {
80 for (src_pixel, dst_pixel) in src_row.iter().zip(dst_row) {
81 *dst_pixel = divide_alpha_pixel(*src_pixel);
82 }
83}
84
85#[inline(always)]
86fn divide_alpha_pixel(mut pixel: U8x4) -> U8x4 {
87 let alpha = pixel.0[3];
88 let recip_alpha = RECIP_ALPHA[alpha as usize];
89 pixel.0 = [
90 div_and_clip(pixel.0[0], recip_alpha),
91 div_and_clip(pixel.0[1], recip_alpha),
92 div_and_clip(pixel.0[2], recip_alpha),
93 alpha,
94 ];
95 pixel
96}