1const MFIX: u32 = 24;
41const HALF: u32 = (1 << MFIX) >> 1;
44const KINV_255: u32 = (1 << MFIX) / 255;
47
48const MULTIPLIER_SCALE: u32 = 32897;
52
53#[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
63fn mult(x: u8, scale: u32) -> u32 {
67 let v = (u32::from(x).wrapping_mul(scale).wrapping_add(HALF)) >> MFIX;
68 debug_assert!(v <= 255); v
70}
71
72#[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
84pub 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 if argb <= 0x00ff_ffff {
94 *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
109pub 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
127pub 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
144pub 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
161pub 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
176pub 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#[must_use]
208fn premultiply(x: u8, m: u32) -> u8 {
209 low_byte((u32::from(x) * m) >> 23)
210}
211
212pub 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
240pub 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#[must_use]
277fn dither_hi(x: u8) -> u8 {
278 (x & 0xf0) | (x >> 4)
279}
280
281#[must_use]
285fn dither_lo(x: u8) -> u8 {
286 (x & 0x0f) | (x << 4)
287}
288
289#[must_use]
292fn multiply(x: u8, m: u32) -> u8 {
293 low_byte((u32::from(x) * m) >> 16)
294}
295
296#[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; 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
330fn 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
337pub 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
343pub 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
373pub 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
416pub 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; }
439 }
440}
441
442pub 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
485pub 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
515pub 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
568pub 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
577pub 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#[must_use]
602fn make_argb32(a: u32, r: u32, g: u32, b: u32) -> u32 {
603 (a << 24) | (r << 16) | (g << 8) | b
604}
605
606pub 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
629pub 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
635fn 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#[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
661fn 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#[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
688fn 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
699pub 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}