Skip to main content

webp/dsp/
alpha_processing.rs

1// libwebp v1.6.0, src/dsp/alpha_processing.c:
2//
3// Copyright 2013 Google Inc. All Rights Reserved.
4//
5// Use of this source code is governed by a BSD-style license
6// that can be found in the COPYING file in the root of the source
7// tree. An additional intellectual property rights grant can be found
8// in the file PATENTS. All contributing project authors may
9// be found in the AUTHORS file in the root of the source tree.
10// -----------------------------------------------------------------------------
11//
12// Utilities for processing transparent channel.
13//
14// Author: Skal (pascal.massimino@gmail.com)
15
16//! Translation of `src/dsp/alpha_processing.c` from libwebp v1.6.0.
17//!
18//! Work package: WP4 (pixel ops).
19//!
20//! # Deviations from upstream
21//!
22//! Changes of representation that the ADRs allow, or behavior that the Rust
23//! side cannot reproduce exactly. Each records the reason.
24//!
25//! 1. **`WebPMultARGBRows`** casts a byte pointer to `uint32_t*` in C. Rust has
26//!    no safe reinterpretation, so the function takes `&mut [u32]` and a stride
27//!    in `u32` elements. The probe calls the C function with the stride in
28//!    bytes, which reaches the same pixels.
29//!
30//! # Checks not applicable
31//!
32//! These checks do not apply, or cannot be made, with the reason:
33//!
34//! 1. **Big-endian sources.** `PackARGB_C` and the `WebPPackARGB` pointer
35//!    (`WORDS_BIGENDIAN`) are not compiled on little-endian hosts, which the
36//!    reference configuration is.
37
38// upstream: src/dsp/alpha_processing.c MFIX
39/// Fixed-point precision of the alpha multiplication.
40const MFIX: u32 = 24;
41// upstream: src/dsp/alpha_processing.c HALF
42/// The rounding term of the alpha multiplication.
43const HALF: u32 = (1 << MFIX) >> 1;
44// upstream: src/dsp/alpha_processing.c KINV_255
45/// The inverse of 255 in the fixed-point precision.
46const KINV_255: u32 = (1 << MFIX) / 255;
47
48// upstream: src/dsp/alpha_processing.c MULTIPLIER (premultiplied modes)
49// upstream: src/dsp/alpha_processing.c PREMULTIPLY (premultiplied modes)
50// The `#if 1` branch of the source: `(int)(x * a / 255.)`.
51const MULTIPLIER_SCALE: u32 = 32897;
52
53/// The low byte of a value, as a C conversion to `uint8_t` keeps it.
54#[expect(
55    clippy::cast_possible_truncation,
56    reason = "a C conversion to uint8_t keeps the low eight bits"
57)]
58#[must_use]
59fn low_byte(value: u32) -> u8 {
60    value as u8
61}
62
63// upstream: src/dsp/alpha_processing.c Mult
64/// Multiplies one channel by a fixed-point scale. The product wraps in 32 bits
65/// as the C `uint32_t` arithmetic does.
66fn mult(x: u8, scale: u32) -> u32 {
67    let v = (u32::from(x).wrapping_mul(scale).wrapping_add(HALF)) >> MFIX;
68    debug_assert!(v <= 255); // <- 24bit precision is enough to ensure that.
69    v
70}
71
72// upstream: src/dsp/alpha_processing.c GetScale
73/// The scale of one alpha value, inverse or direct. The inverse scale is
74/// undefined for alpha 0, and the callers never ask for it.
75#[must_use]
76fn get_scale(a: u32, inverse: bool) -> u32 {
77    if inverse {
78        (255 << MFIX) / a
79    } else {
80        a * KINV_255
81    }
82}
83
84// upstream: src/dsp/alpha_processing.c WebPMultARGBRow_C
85/// Premultiplies (or, with `inverse`, un-premultiplies) `width` ARGB pixels.
86/// Opaque pixels are unchanged and fully transparent ones become zero.
87pub fn webp_mult_argb_row_c(ptr: &mut [u32], width: i32, inverse: bool) {
88    let width = usize::try_from(width).unwrap_or(0);
89    for pixel in &mut ptr[..width] {
90        let argb = *pixel;
91        if argb < 0xff00_0000 {
92            // alpha < 255
93            if argb <= 0x00ff_ffff {
94                // alpha == 0
95                *pixel = 0;
96            } else {
97                let alpha = (argb >> 24) & 0xff;
98                let scale = get_scale(alpha, inverse);
99                let mut out = argb & 0xff00_0000;
100                out |= mult(low_byte(argb), scale);
101                out |= mult(low_byte(argb >> 8), scale) << 8;
102                out |= mult(low_byte(argb >> 16), scale) << 16;
103                *pixel = out;
104            }
105        }
106    }
107}
108
109// upstream: src/dsp/alpha_processing.c WebPMultRow_C
110/// Premultiplies (or un-premultiplies) one row of single values with the
111/// alpha values of the same width.
112pub fn webp_mult_row_c(ptr: &mut [u8], alpha: &[u8], width: i32, inverse: bool) {
113    let width = usize::try_from(width).unwrap_or(0);
114    for x in 0..width {
115        let a = u32::from(alpha[x]);
116        if a != 255 {
117            if a == 0 {
118                ptr[x] = 0;
119            } else {
120                let scale = get_scale(a, inverse);
121                ptr[x] = low_byte(mult(ptr[x], scale));
122            }
123        }
124    }
125}
126
127// upstream: src/dsp/alpha_processing.c WebPMultARGBRow
128/// Premultiplies `width` ARGB pixels. The C dispatch pointer is `WebPMultARGBRow_C`.
129pub fn webp_mult_argb_row(ptr: &mut [u32], width: i32, inverse: bool) {
130    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
131    {
132        let features = crate::dsp::cpu::features();
133        if features.sse2 {
134            return crate::dsp::alpha_processing_sse2::mult_argb_row(ptr, width, inverse);
135        }
136    }
137    #[allow(
138        unreachable_code,
139        reason = "the C path is reached only without a SIMD variant"
140    )]
141    webp_mult_argb_row_c(ptr, width, inverse);
142}
143
144// upstream: src/dsp/alpha_processing.c WebPMultRow
145/// Premultiplies one row of single values. The C dispatch pointer is `WebPMultRow_C`.
146pub fn webp_mult_row(ptr: &mut [u8], alpha: &[u8], width: i32, inverse: bool) {
147    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
148    {
149        let features = crate::dsp::cpu::features();
150        if features.sse2 {
151            return crate::dsp::alpha_processing_sse2::mult_row(ptr, alpha, width, inverse);
152        }
153    }
154    #[allow(
155        unreachable_code,
156        reason = "the C path is reached only without a SIMD variant"
157    )]
158    webp_mult_row_c(ptr, alpha, width, inverse);
159}
160
161// upstream: src/dsp/alpha_processing.c WebPMultARGBRows
162/// Premultiplies `num_rows` rows of ARGB pixels. `stride` counts `u32`
163/// elements, where C counts bytes of the same buffer (ADR 0003).
164///
165/// # Panics
166///
167/// Panics when `ptr` holds fewer than `num_rows` rows of `stride` elements
168/// with `width` pixels each.
169pub fn webp_mult_argb_rows(ptr: &mut [u32], stride: i32, width: i32, num_rows: i32, inverse: bool) {
170    let stride = usize::try_from(stride).expect("the stride is not negative");
171    for n in 0..usize::try_from(num_rows).unwrap_or(0) {
172        webp_mult_argb_row(&mut ptr[n * stride..], width, inverse);
173    }
174}
175
176// upstream: src/dsp/alpha_processing.c WebPMultRows
177/// Premultiplies `num_rows` rows of single values, with the alpha values of
178/// each row in `alpha`.
179///
180/// # Panics
181///
182/// Panics when a slice is shorter than the rows that `num_rows` addresses.
183pub fn webp_mult_rows(
184    ptr: &mut [u8],
185    stride: i32,
186    alpha: &[u8],
187    alpha_stride: i32,
188    width: i32,
189    num_rows: i32,
190    inverse: bool,
191) {
192    let stride = usize::try_from(stride).expect("the stride is not negative");
193    let alpha_stride = usize::try_from(alpha_stride).expect("the stride is not negative");
194    for n in 0..usize::try_from(num_rows).unwrap_or(0) {
195        webp_mult_row(
196            &mut ptr[n * stride..],
197            &alpha[n * alpha_stride..],
198            width,
199            inverse,
200        );
201    }
202}
203
204// upstream: src/dsp/alpha_processing.c MULTIPLIER and PREMULTIPLY
205/// `PREMULTIPLY(x, m)` for the premultiplied modes, with the scale of
206/// `MULTIPLIER(a)`.
207#[must_use]
208fn premultiply(x: u8, m: u32) -> u8 {
209    low_byte((u32::from(x) * m) >> 23)
210}
211
212// upstream: src/dsp/alpha_processing.c ApplyAlphaMultiply_C
213/// Premultiplies the color channels of an RGBA or BGRA plane, or of an ARGB
214/// plane when `alpha_first` is set, in place.
215///
216/// # Panics
217///
218/// Panics when a width or stride is negative or a slice is shorter than
219/// the elements the C function addresses.
220pub fn apply_alpha_multiply_c(rgba: &mut [u8], alpha_first: bool, w: i32, h: i32, stride: i32) {
221    let w = usize::try_from(w).unwrap_or(0);
222    let stride = usize::try_from(stride).expect("the stride is not negative");
223    let rgb_offset = usize::from(alpha_first);
224    let alpha_offset = if alpha_first { 0 } else { 3 };
225    for row in 0..usize::try_from(h).unwrap_or(0) {
226        let base = row * stride;
227        for i in 0..w {
228            let a = u32::from(rgba[base + alpha_offset + 4 * i]);
229            if a != 0xff {
230                let mult = a * MULTIPLIER_SCALE;
231                let rgb = base + rgb_offset + 4 * i;
232                rgba[rgb] = premultiply(rgba[rgb], mult);
233                rgba[rgb + 1] = premultiply(rgba[rgb + 1], mult);
234                rgba[rgb + 2] = premultiply(rgba[rgb + 2], mult);
235            }
236        }
237    }
238}
239
240// upstream: src/dsp/alpha_processing.c WebPApplyAlphaMultiply
241/// Premultiplies an RGBA, BGRA or ARGB plane of `w` by `h` pixels in place.
242/// `alpha_first` is set for ARGB, where alpha comes first.
243pub fn webp_apply_alpha_multiply(rgba: &mut [u8], alpha_first: bool, w: i32, h: i32, stride: i32) {
244    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
245    {
246        let features = crate::dsp::cpu::features();
247        if features.sse2 {
248            return crate::dsp::alpha_processing_sse2::apply_alpha_multiply(
249                rgba,
250                alpha_first,
251                w,
252                h,
253                stride,
254            );
255        }
256    }
257    #[cfg(all(feature = "simd", target_arch = "aarch64"))]
258    {
259        return crate::dsp::alpha_processing_neon::apply_alpha_multiply(
260            rgba,
261            alpha_first,
262            w,
263            h,
264            stride,
265        );
266    }
267    #[allow(
268        unreachable_code,
269        reason = "the C path is reached only without a SIMD variant"
270    )]
271    apply_alpha_multiply_c(rgba, alpha_first, w, h, stride);
272}
273
274// upstream: src/dsp/alpha_processing.c dither_hi
275/// Keeps the high nibble and repeats it in the low one.
276#[must_use]
277fn dither_hi(x: u8) -> u8 {
278    (x & 0xf0) | (x >> 4)
279}
280
281// upstream: src/dsp/alpha_processing.c dither_lo
282/// Keeps the low nibble and repeats it in the high one. The shift drops the
283/// high bits, as the `uint8_t` return of the C function does.
284#[must_use]
285fn dither_lo(x: u8) -> u8 {
286    (x & 0x0f) | (x << 4)
287}
288
289// upstream: src/dsp/alpha_processing.c multiply
290/// Multiplies a nibble-expanded channel by the RGBA4444 scale.
291#[must_use]
292fn multiply(x: u8, m: u32) -> u8 {
293    low_byte((u32::from(x) * m) >> 16)
294}
295
296// upstream: src/dsp/alpha_processing.c ApplyAlphaMultiply4444_C
297/// Premultiplies an RGBA4444 or rgbA4444 plane in place. `rg_byte_pos` is the
298/// byte of the pixel that holds red and green, 0 or 1.
299#[expect(
300    clippy::many_single_char_names,
301    reason = "the C names w, h, r, g, b and a of the pixel channels"
302)]
303fn apply_alpha_multiply4444_c(
304    rgba4444: &mut [u8],
305    w: i32,
306    h: i32,
307    stride: i32,
308    rg_byte_pos: usize,
309) {
310    let w = usize::try_from(w).unwrap_or(0);
311    let stride = usize::try_from(stride).expect("the stride is not negative");
312    for row in 0..usize::try_from(h).unwrap_or(0) {
313        let base = row * stride;
314        for i in 0..w {
315            let rg_index = base + 2 * i + rg_byte_pos;
316            let ba_index = base + 2 * i + (rg_byte_pos ^ 1);
317            let rg = rgba4444[rg_index];
318            let ba = rgba4444[ba_index];
319            let a = ba & 0x0f;
320            let mult = u32::from(a) * 0x1111; // MULTIPLIER(a)
321            let r = multiply(dither_hi(rg), mult);
322            let g = multiply(dither_lo(rg), mult);
323            let b = multiply(dither_hi(ba), mult);
324            rgba4444[rg_index] = (r & 0xf0) | ((g >> 4) & 0x0f);
325            rgba4444[ba_index] = (b & 0xf0) | a;
326        }
327    }
328}
329
330// upstream: src/dsp/alpha_processing.c ApplyAlphaMultiply_16b_C
331/// The RGBA4444 variant of `WebPApplyAlphaMultiply4444`, with red and green in
332/// the first byte, as `WEBP_SWAP_16BIT_CSP` is 0 in the reference configuration.
333fn apply_alpha_multiply_16b_c(rgba4444: &mut [u8], w: i32, h: i32, stride: i32) {
334    apply_alpha_multiply4444_c(rgba4444, w, h, stride, 0);
335}
336
337// upstream: src/dsp/alpha_processing.c WebPApplyAlphaMultiply4444
338/// Premultiplies an RGBA4444 plane of `w` by `h` pixels in place.
339pub fn webp_apply_alpha_multiply4444(rgba4444: &mut [u8], w: i32, h: i32, stride: i32) {
340    apply_alpha_multiply_16b_c(rgba4444, w, h, stride);
341}
342
343// upstream: src/dsp/alpha_processing.c DispatchAlpha_C
344/// Copies the alpha plane into the A byte of each ARGB pixel of `dst`, which
345/// has `dst_stride` bytes per row. Returns whether an alpha value is not 0xff.
346///
347/// # Panics
348///
349/// Panics when a width or stride is negative or a slice is shorter than
350/// the elements the C function addresses.
351pub fn dispatch_alpha_c(
352    alpha: &[u8],
353    alpha_stride: i32,
354    width: i32,
355    height: i32,
356    dst: &mut [u8],
357    dst_stride: i32,
358) -> bool {
359    let width = usize::try_from(width).unwrap_or(0);
360    let alpha_stride = usize::try_from(alpha_stride).expect("the stride is not negative");
361    let dst_stride = usize::try_from(dst_stride).expect("the stride is not negative");
362    let mut alpha_mask = 0xff_u8;
363    for j in 0..usize::try_from(height).unwrap_or(0) {
364        for i in 0..width {
365            let alpha_value = alpha[j * alpha_stride + i];
366            dst[j * dst_stride + 4 * i] = alpha_value;
367            alpha_mask &= alpha_value;
368        }
369    }
370    alpha_mask != 0xff
371}
372
373// upstream: src/dsp/alpha_processing.c WebPDispatchAlpha
374/// Dispatches the alpha values of `alpha` to the A bytes of `dst`. Returns
375/// whether an alpha value is not 0xff. The C dispatch pointer is `DispatchAlpha_C`.
376pub fn webp_dispatch_alpha(
377    alpha: &[u8],
378    alpha_stride: i32,
379    width: i32,
380    height: i32,
381    dst: &mut [u8],
382    dst_stride: i32,
383) -> bool {
384    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
385    {
386        let features = crate::dsp::cpu::features();
387        if features.sse2 {
388            return crate::dsp::alpha_processing_sse2::dispatch_alpha(
389                alpha,
390                alpha_stride,
391                width,
392                height,
393                dst,
394                dst_stride,
395            );
396        }
397    }
398    #[cfg(all(feature = "simd", target_arch = "aarch64"))]
399    {
400        return crate::dsp::alpha_processing_neon::dispatch_alpha(
401            alpha,
402            alpha_stride,
403            width,
404            height,
405            dst,
406            dst_stride,
407        );
408    }
409    #[allow(
410        unreachable_code,
411        reason = "the C path is reached only without a SIMD variant"
412    )]
413    dispatch_alpha_c(alpha, alpha_stride, width, height, dst, dst_stride)
414}
415
416// upstream: src/dsp/alpha_processing.c DispatchAlphaToGreen_C
417/// Stores the alpha values in the green channel of each `u32` pixel of `dst`,
418/// with the other channels zeroed. `dst_stride` counts `u32` elements.
419///
420/// # Panics
421///
422/// Panics when a width or stride is negative or a slice is shorter than
423/// the elements the C function addresses.
424pub fn dispatch_alpha_to_green_c(
425    alpha: &[u8],
426    alpha_stride: i32,
427    width: i32,
428    height: i32,
429    dst: &mut [u32],
430    dst_stride: i32,
431) {
432    let width = usize::try_from(width).unwrap_or(0);
433    let alpha_stride = usize::try_from(alpha_stride).expect("the stride is not negative");
434    let dst_stride = usize::try_from(dst_stride).expect("the stride is not negative");
435    for j in 0..usize::try_from(height).unwrap_or(0) {
436        for i in 0..width {
437            dst[j * dst_stride + i] = u32::from(alpha[j * alpha_stride + i]) << 8; // leave A/R/B channels zero'd.
438        }
439    }
440}
441
442// upstream: src/dsp/alpha_processing.c WebPDispatchAlphaToGreen
443/// Stores the alpha values in the green channel of `dst`. The C dispatch
444/// pointer is `DispatchAlphaToGreen_C`.
445pub fn webp_dispatch_alpha_to_green(
446    alpha: &[u8],
447    alpha_stride: i32,
448    width: i32,
449    height: i32,
450    dst: &mut [u32],
451    dst_stride: i32,
452) {
453    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
454    {
455        let features = crate::dsp::cpu::features();
456        if features.sse2 {
457            return crate::dsp::alpha_processing_sse2::dispatch_alpha_to_green(
458                alpha,
459                alpha_stride,
460                width,
461                height,
462                dst,
463                dst_stride,
464            );
465        }
466    }
467    #[cfg(all(feature = "simd", target_arch = "aarch64"))]
468    {
469        return crate::dsp::alpha_processing_neon::dispatch_alpha_to_green(
470            alpha,
471            alpha_stride,
472            width,
473            height,
474            dst,
475            dst_stride,
476        );
477    }
478    #[allow(
479        unreachable_code,
480        reason = "the C path is reached only without a SIMD variant"
481    )]
482    dispatch_alpha_to_green_c(alpha, alpha_stride, width, height, dst, dst_stride);
483}
484
485// upstream: src/dsp/alpha_processing.c ExtractAlpha_C
486/// Copies the A bytes of an ARGB plane to `alpha`. Returns whether every alpha
487/// value is 0xff.
488///
489/// # Panics
490///
491/// Panics when a width or stride is negative or a slice is shorter than
492/// the elements the C function addresses.
493pub fn extract_alpha_c(
494    argb: &[u8],
495    argb_stride: i32,
496    width: i32,
497    height: i32,
498    alpha: &mut [u8],
499    alpha_stride: i32,
500) -> bool {
501    let width = usize::try_from(width).unwrap_or(0);
502    let argb_stride = usize::try_from(argb_stride).expect("the stride is not negative");
503    let alpha_stride = usize::try_from(alpha_stride).expect("the stride is not negative");
504    let mut alpha_mask = 0xff_u8;
505    for j in 0..usize::try_from(height).unwrap_or(0) {
506        for i in 0..width {
507            let alpha_value = argb[j * argb_stride + 4 * i];
508            alpha[j * alpha_stride + i] = alpha_value;
509            alpha_mask &= alpha_value;
510        }
511    }
512    alpha_mask == 0xff
513}
514
515// upstream: src/dsp/alpha_processing.c WebPExtractAlpha
516/// Extracts the alpha values of an ARGB byte plane. Returns whether every
517/// alpha value is 0xff. The C dispatch pointer is `ExtractAlpha_C`.
518pub fn webp_extract_alpha(
519    argb: &[u8],
520    argb_stride: i32,
521    width: i32,
522    height: i32,
523    alpha: &mut [u8],
524    alpha_stride: i32,
525) -> bool {
526    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
527    {
528        let features = crate::dsp::cpu::features();
529        if features.sse2 && features.sse4_1 {
530            return crate::dsp::alpha_processing_sse41::extract_alpha(
531                argb,
532                argb_stride,
533                width,
534                height,
535                alpha,
536                alpha_stride,
537            );
538        }
539        if features.sse2 {
540            return crate::dsp::alpha_processing_sse2::extract_alpha(
541                argb,
542                argb_stride,
543                width,
544                height,
545                alpha,
546                alpha_stride,
547            );
548        }
549    }
550    #[cfg(all(feature = "simd", target_arch = "aarch64"))]
551    {
552        return crate::dsp::alpha_processing_neon::extract_alpha(
553            argb,
554            argb_stride,
555            width,
556            height,
557            alpha,
558            alpha_stride,
559        );
560    }
561    #[allow(
562        unreachable_code,
563        reason = "the C path is reached only without a SIMD variant"
564    )]
565    extract_alpha_c(argb, argb_stride, width, height, alpha, alpha_stride)
566}
567
568// upstream: src/dsp/alpha_processing.c ExtractGreen_C
569/// Copies the green byte of each `u32` pixel to `alpha`.
570pub fn extract_green_c(argb: &[u32], alpha: &mut [u8], size: i32) {
571    let size = usize::try_from(size).unwrap_or(0);
572    for i in 0..size {
573        alpha[i] = low_byte(argb[i] >> 8);
574    }
575}
576
577// upstream: src/dsp/alpha_processing.c WebPExtractGreen
578/// Copies the green byte of each `u32` pixel to `alpha`. The C dispatch
579/// pointer is `ExtractGreen_C`.
580pub fn webp_extract_green(argb: &[u32], alpha: &mut [u8], size: i32) {
581    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
582    {
583        let features = crate::dsp::cpu::features();
584        if features.sse2 {
585            return crate::dsp::alpha_processing_sse2::extract_green(argb, alpha, size);
586        }
587    }
588    #[cfg(all(feature = "simd", target_arch = "aarch64"))]
589    {
590        return crate::dsp::alpha_processing_neon::extract_green(argb, alpha, size);
591    }
592    #[allow(
593        unreachable_code,
594        reason = "the C path is reached only without a SIMD variant"
595    )]
596    extract_green_c(argb, alpha, size);
597}
598
599// upstream: src/dsp/alpha_processing.c MakeARGB32
600/// Packs four channels into one ARGB pixel.
601#[must_use]
602fn make_argb32(a: u32, r: u32, g: u32, b: u32) -> u32 {
603    (a << 24) | (r << 16) | (g << 8) | b
604}
605
606// upstream: src/dsp/alpha_processing.c PackRGB_C
607/// Packs `len` RGB or BGR pixels into opaque ARGB pixels. `step` is the number
608/// of bytes per pixel of the planes, 3 or 4, with the pixels of each plane at
609/// that stride.
610///
611/// # Panics
612///
613/// Panics when a plane or `out` is shorter than the pixels that `len` needs.
614pub fn pack_rgb_c(r: &[u8], g: &[u8], b: &[u8], len: i32, step: i32, out: &mut [u32]) {
615    let len = usize::try_from(len).unwrap_or(0);
616    let step = usize::try_from(step).expect("the step is not negative");
617    let mut offset = 0_usize;
618    for pixel in &mut out[..len] {
619        *pixel = make_argb32(
620            0xff,
621            u32::from(r[offset]),
622            u32::from(g[offset]),
623            u32::from(b[offset]),
624        );
625        offset += step;
626    }
627}
628
629// upstream: src/dsp/alpha_processing.c WebPPackRGB
630/// Packs RGB or BGR planes into opaque ARGB pixels. The C dispatch pointer is `PackRGB_C`.
631pub fn webp_pack_rgb(r: &[u8], g: &[u8], b: &[u8], len: i32, step: i32, out: &mut [u32]) {
632    pack_rgb_c(r, g, b, len, step, out);
633}
634
635// upstream: src/dsp/alpha_processing.c HasAlpha8b_C
636/// Whether any of the first `length` bytes of `src` is not 0xff.
637fn has_alpha8b_c(src: &[u8], length: i32) -> bool {
638    let length = usize::try_from(length).unwrap_or(0);
639    src[..length].iter().any(|&value| value != 0xff)
640}
641
642// upstream: src/dsp/alpha_processing.c WebPHasAlpha8b
643/// Whether any of the first `length` bytes of `src` is not 0xff. The C dispatch
644/// pointer is `HasAlpha8b_C`.
645#[must_use]
646pub fn webp_has_alpha8b(src: &[u8], length: i32) -> bool {
647    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
648    {
649        let features = crate::dsp::cpu::features();
650        if features.sse2 {
651            return crate::dsp::alpha_processing_sse2::has_alpha8b(src, length);
652        }
653    }
654    #[allow(
655        unreachable_code,
656        reason = "the C path is reached only without a SIMD variant"
657    )]
658    has_alpha8b_c(src, length)
659}
660
661// upstream: src/dsp/alpha_processing.c HasAlpha32b_C
662/// Whether the alpha byte of any of the first `length` ARGB pixels in the
663/// byte plane `src` is not 0xff.
664fn has_alpha32b_c(src: &[u8], length: i32) -> bool {
665    let length = usize::try_from(length).unwrap_or(0);
666    (0..length).any(|x| src[4 * x] != 0xff)
667}
668
669// upstream: src/dsp/alpha_processing.c WebPHasAlpha32b
670/// Whether the alpha byte of any of the first `length` pixels of the RGBA byte
671/// plane `src` is not 0xff. The C dispatch pointer is `HasAlpha32b_C`.
672#[must_use]
673pub fn webp_has_alpha32b(src: &[u8], length: i32) -> bool {
674    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
675    {
676        let features = crate::dsp::cpu::features();
677        if features.sse2 {
678            return crate::dsp::alpha_processing_sse2::has_alpha32b(src, length);
679        }
680    }
681    #[allow(
682        unreachable_code,
683        reason = "the C path is reached only without a SIMD variant"
684    )]
685    has_alpha32b_c(src, length)
686}
687
688// upstream: src/dsp/alpha_processing.c AlphaReplace_C
689/// Replaces every pixel of `src` whose alpha is zero by `color`.
690fn alpha_replace_c(src: &mut [u32], length: i32, color: u32) {
691    let length = usize::try_from(length).unwrap_or(0);
692    for pixel in &mut src[..length] {
693        if *pixel >> 24 == 0 {
694            *pixel = color;
695        }
696    }
697}
698
699// upstream: src/dsp/alpha_processing.c WebPAlphaReplace
700/// Replaces every fully transparent pixel of `src` by `color`. The C dispatch
701/// pointer is `AlphaReplace_C`.
702pub fn webp_alpha_replace(src: &mut [u32], length: i32, color: u32) {
703    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
704    {
705        let features = crate::dsp::cpu::features();
706        if features.sse2 {
707            return crate::dsp::alpha_processing_sse2::alpha_replace(src, length, color);
708        }
709    }
710    #[allow(
711        unreachable_code,
712        reason = "the C path is reached only without a SIMD variant"
713    )]
714    alpha_replace_c(src, length, color);
715}