Debian Codegraph @

.cargo/vendor/image/src/codecs/avif/ycgco.rs

raw
1 use crate::codecs::avif::yuv::{
2 check_rgb_preconditions, check_yuv_plane_preconditions, qrshr, yuv420_to_rgbx_invoker,
3 yuv422_to_rgbx_invoker, CbCrInverseTransform, HalvedRowHandler, PlaneDefinition,
4 YuvChromaRange, YuvIntensityRange, YuvPlanarImage, YuvStandardMatrix,
5 };
6 use crate::ImageError;
7 use num_traits::AsPrimitive;
8
9 /// Computes YCgCo inverse in limited range
10 /// # Arguments
11 /// - `dst` - dest buffer
12 /// - `y_value` - Y value with subtracted bias
13 /// - `cb` - Cb value with subtracted bias
14 /// - `cr` - Cr value with subtracted bias
15 #[inline(always)]
16 fn ycgco_execute_limited<
17 V: Copy + AsPrimitive<i32> + 'static + Sized,
18 const PRECISION: i32,
19 const CHANNELS: usize,
20 const BIT_DEPTH: usize,
21 >(
22 dst: &mut [V; CHANNELS],
23 y_value: i32,
24 cg: i32,
25 co: i32,
26 scale: i32,
27 ) where
28 i32: AsPrimitive<V>,
29 {
30 let t0 = y_value - cg;
31
32 let r = qrshr::<PRECISION, BIT_DEPTH>((t0 + co) * scale);
33 let b = qrshr::<PRECISION, BIT_DEPTH>((t0 - co) * scale);
34 let g = qrshr::<PRECISION, BIT_DEPTH>((y_value + cg) * scale);
35
36 if CHANNELS == 4 {
37 dst[0] = r.as_();
38 dst[1] = g.as_();
39 dst[2] = b.as_();
40 dst[3] = ((1i32 << BIT_DEPTH) - 1).as_();
41 } else if CHANNELS == 3 {
42 dst[0] = r.as_();
43 dst[1] = g.as_();
44 dst[2] = b.as_();
45 } else {
46 unreachable!();
47 }
48 }
49
50 /// Computes YCgCo inverse in full range
51 /// # Arguments
52 /// - `dst` - dest buffer
53 /// - `y_value` - Y value with subtracted bias
54 /// - `cb` - Cb value with subtracted bias
55 /// - `cr` - Cr value with subtracted bias
56 #[inline(always)]
57 fn ycgco_execute_full<
58 V: Copy + AsPrimitive<i32> + 'static + Sized,
59 const PRECISION: i32,
60 const CHANNELS: usize,
61 const BIT_DEPTH: usize,
62 >(
63 dst: &mut [V; CHANNELS],
64 y_value: i32,
65 cg: i32,
66 co: i32,
67 ) where
68 i32: AsPrimitive<V>,
69 {
70 let t0 = y_value - cg;
71
72 let max_value = (1i32 << BIT_DEPTH) - 1;
73
74 let r = (t0 + co).clamp(0, max_value);
75 let b = (t0 - co).clamp(0, max_value);
76 let g = (y_value + cg).clamp(0, max_value);
77
78 if CHANNELS == 4 {
79 dst[0] = r.as_();
80 dst[1] = g.as_();
81 dst[2] = b.as_();
82 dst[3] = max_value.as_();
83 } else if CHANNELS == 3 {
84 dst[0] = r.as_();
85 dst[1] = g.as_();
86 dst[2] = b.as_();
87 } else {
88 unreachable!();
89 }
90 }
91
92 #[inline(always)]
93 fn process_halved_chroma_row_cgco<
94 V: Copy + AsPrimitive<i32> + 'static + Sized,
95 const PRECISION: i32,
96 const CHANNELS: usize,
97 const BIT_DEPTH: usize,
98 >(
99 image: YuvPlanarImage<V>,
100 rgba: &mut [V],
101 _: &CbCrInverseTransform<i32>,
102 range: &YuvChromaRange,
103 ) where
104 i32: AsPrimitive<V>,
105 {
106 let max_value = (1i32 << BIT_DEPTH) - 1;
107
108 // If the stride is larger than the plane size,
109 // it might contain junk data beyond the actual valid region.
110 // To avoid processing artifacts when working with odd-sized images,
111 // the buffer is reshaped to its actual size,
112 // preventing accidental use of invalid values from the trailing region.
113
114 let y_plane = &image.y_plane[..image.width];
115 let chroma_size = image.width.div_ceil(2);
116 let u_plane = &image.u_plane[..chroma_size];
117 let v_plane = &image.v_plane[..chroma_size];
118 let rgba = &mut rgba[..image.width * CHANNELS];
119
120 let bias_y = range.bias_y as i32;
121 let bias_uv = range.bias_uv as i32;
122 let y_iter = y_plane.chunks_exact(2);
123 let rgb_chunks = rgba.chunks_exact_mut(CHANNELS * 2);
124
125 let scale_coef = ((max_value as f32 / range.range_y as f32) * (1 << PRECISION) as f32) as i32;
126
127 for (((y_src, &u_src), &v_src), rgb_dst) in y_iter.zip(u_plane).zip(v_plane).zip(rgb_chunks) {
128 let y_value0: i32 = y_src[0].as_() - bias_y;
129 let cg_value: i32 = u_src.as_() - bias_uv;
130 let co_value: i32 = v_src.as_() - bias_uv;
131
132 let dst0 = &mut rgb_dst[..CHANNELS];
133
134 ycgco_execute_limited::<V, PRECISION, CHANNELS, BIT_DEPTH>(
135 dst0.try_into().unwrap(),
136 y_value0,
137 cg_value,
138 co_value,
139 scale_coef,
140 );
141
142 let y_value1 = y_src[1].as_() - bias_y;
143
144 let dst1 = &mut rgb_dst[CHANNELS..2 * CHANNELS];
145
146 ycgco_execute_limited::<V, PRECISION, CHANNELS, BIT_DEPTH>(
147 dst1.try_into().unwrap(),
148 y_value1,
149 cg_value,
150 co_value,
151 scale_coef,
152 );
153 }
154
155 // Process remainder if width is odd.
156 if image.width & 1 != 0 {
157 let y_left = y_plane.chunks_exact(2).remainder();
158 let rgb_chunks = rgba
159 .chunks_exact_mut(CHANNELS * 2)
160 .into_remainder()
161 .chunks_exact_mut(CHANNELS);
162 let u_iter = u_plane.iter().rev();
163 let v_iter = v_plane.iter().rev();
164
165 for (((y_src, u_src), v_src), rgb_dst) in
166 y_left.iter().zip(u_iter).zip(v_iter).zip(rgb_chunks)
167 {
168 let y_value = y_src.as_() - bias_y;
169 let cg_value = u_src.as_() - bias_uv;
170 let co_value = v_src.as_() - bias_uv;
171
172 ycgco_execute_limited::<V, PRECISION, CHANNELS, BIT_DEPTH>(
173 rgb_dst.try_into().unwrap(),
174 y_value,
175 cg_value,
176 co_value,
177 scale_coef,
178 );
179 }
180 }
181 }
182
183 /// Converts YCgCo 444 planar format to Rgba
184 ///
185 /// # Arguments
186 ///
187 /// * `image`: see [YuvPlanarImage]
188 /// * `rgba`: RGB image layout
189 /// * `range`: see [YuvIntensityRange]
190 ///
191 fn ycgco444_to_rgbx_impl<
192 V: Copy + AsPrimitive<i32> + 'static + Sized,
193 const CHANNELS: usize,
194 const BIT_DEPTH: usize,
195 >(
196 image: YuvPlanarImage<V>,
197 rgba: &mut [V],
198 yuv_range: YuvIntensityRange,
199 ) -> Result<(), ImageError>
200 where
201 i32: AsPrimitive<V>,
202 {
203 assert!(
204 CHANNELS == 3 || CHANNELS == 4,
205 "YUV 4:4:4 -> RGB is implemented only on 3 and 4 channels"
206 );
207 assert!(
208 (8..=16).contains(&BIT_DEPTH),
209 "Invalid bit depth is provided"
210 );
211 assert!(
212 if BIT_DEPTH > 8 {
213 size_of::<V>() == 2
214 } else {
215 size_of::<V>() == 1
216 },
217 "Unsupported bit depth and data type combination"
218 );
219
220 let y_plane = image.y_plane;
221 let u_plane = image.u_plane;
222 let v_plane = image.v_plane;
223 let y_stride = image.y_stride;
224 let u_stride = image.u_stride;
225 let v_stride = image.v_stride;
226 let height = image.height;
227 let width = image.width;
228
229 check_yuv_plane_preconditions(y_plane, PlaneDefinition::Y, y_stride, height)?;
230 check_yuv_plane_preconditions(u_plane, PlaneDefinition::U, u_stride, height)?;
231 check_yuv_plane_preconditions(v_plane, PlaneDefinition::V, v_stride, height)?;
232
233 check_rgb_preconditions(rgba, image.width * CHANNELS, height)?;
234
235 let range = yuv_range.get_yuv_range(BIT_DEPTH as u32);
236 const PRECISION: i32 = 13;
237
238 let bias_y = range.bias_y as i32;
239 let bias_uv = range.bias_uv as i32;
240
241 let rgb_stride = width * CHANNELS;
242
243 let y_iter = y_plane.chunks_exact(y_stride);
244 let rgb_iter = rgba.chunks_exact_mut(rgb_stride);
245 let u_iter = u_plane.chunks_exact(u_stride);
246 let v_iter = v_plane.chunks_exact(v_stride);
247
248 let max_value: i32 = (1 << BIT_DEPTH) - 1;
249
250 // All branches on generic const will be optimized out.
251 for (((y_src, u_src), v_src), rgb) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
252 let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
253 match yuv_range {
254 YuvIntensityRange::Tv => {
255 let y_coef =
256 ((max_value as f32 / range.range_y as f32) * (1 << PRECISION) as f32) as i32;
257 for (((y_src, u_src), v_src), rgb_dst) in
258 y_src.iter().zip(u_src).zip(v_src).zip(rgb_chunks)
259 {
260 let y_value = y_src.as_() - bias_y;
261 let cg_value = u_src.as_() - bias_uv;
262 let co_value = v_src.as_() - bias_uv;
263
264 ycgco_execute_limited::<V, PRECISION, CHANNELS, BIT_DEPTH>(
265 rgb_dst.try_into().unwrap(),
266 y_value,
267 cg_value,
268 co_value,
269 y_coef,
270 );
271 }
272 }
273 YuvIntensityRange::Pc => {
274 for (((y_src, u_src), v_src), rgb_dst) in
275 y_src.iter().zip(u_src).zip(v_src).zip(rgb_chunks)
276 {
277 let y_value = y_src.as_() - bias_y;
278 let cg_value = u_src.as_() - bias_uv;
279 let co_value = v_src.as_() - bias_uv;
280
281 ycgco_execute_full::<V, PRECISION, CHANNELS, BIT_DEPTH>(
282 rgb_dst.try_into().unwrap(),
283 y_value,
284 cg_value,
285 co_value,
286 );
287 }
288 }
289 }
290 }
291
292 Ok(())
293 }
294
295 macro_rules! define_ycgco_half_chroma {
296 ($name: ident, $invoker: ident, $storage: ident, $cn: expr, $bp: expr, $description: expr) => {
297 #[doc = concat!($description, "
298
299 # Arguments
300
301 * `image`: see [YuvPlanarImage]
302 * `rgb`: RGB image layout
303 * `range`: see [YuvIntensityRange]
304 * `matrix`: see [YuvStandardMatrix]")]
305 pub(crate) fn $name(
306 image: YuvPlanarImage<$storage>,
307 rgb: &mut [$storage],
308 range: YuvIntensityRange,
309 ) -> Result<(), ImageError> {
310 const P: i32 = 13;
311 $invoker::<$storage, HalvedRowHandler<$storage>, P, $cn, $bp>(
312 image,
313 rgb,
314 range,
315 YuvStandardMatrix::Bt709,
316 process_halved_chroma_row_cgco::<$storage, P, $cn, $bp>,
317 )
318 }
319 };
320 }
321
322 const RGBA_CN: usize = 4;
323
324 define_ycgco_half_chroma!(
325 ycgco420_to_rgba8,
326 yuv420_to_rgbx_invoker,
327 u8,
328 RGBA_CN,
329 8,
330 "Converts YCgCo 420 8-bit planar format to Rgba 8-bit"
331 );
332
333 define_ycgco_half_chroma!(
334 ycgco422_to_rgba8,
335 yuv422_to_rgbx_invoker,
336 u8,
337 RGBA_CN,
338 8,
339 "Converts YCgCo 420 8-bit planar format to Rgba 8-bit"
340 );
341
342 define_ycgco_half_chroma!(
343 ycgco420_to_rgba10,
344 yuv420_to_rgbx_invoker,
345 u16,
346 RGBA_CN,
347 10,
348 "Converts YCgCo 420 10-bit planar format to Rgba 10-bit"
349 );
350
351 define_ycgco_half_chroma!(
352 ycgco422_to_rgba10,
353 yuv422_to_rgbx_invoker,
354 u16,
355 RGBA_CN,
356 10,
357 "Converts YCgCo 422 10-bit planar format to Rgba 10-bit"
358 );
359
360 define_ycgco_half_chroma!(
361 ycgco420_to_rgba12,
362 yuv420_to_rgbx_invoker,
363 u16,
364 RGBA_CN,
365 12,
366 "Converts YCgCo 420 12-bit planar format to Rgba 12-bit"
367 );
368
369 define_ycgco_half_chroma!(
370 ycgco422_to_rgba12,
371 yuv422_to_rgbx_invoker,
372 u16,
373 RGBA_CN,
374 12,
375 "Converts YCgCo 422 12-bit planar format to Rgba 12-bit"
376 );
377
378 macro_rules! define_ycgcg_full_chroma {
379 ($name: ident, $storage: ident, $cn: expr, $bp: expr, $description: expr) => {
380 #[doc = concat!($description, "
381
382 # Arguments
383
384 * `image`: see [YuvPlanarImage]
385 * `rgba`: RGB image layout
386 * `range`: see [YuvIntensityRange]
387 * `matrix`: see [YuvStandardMatrix]
388 ")]
389 pub(crate) fn $name(
390 image: YuvPlanarImage<$storage>,
391 rgba: &mut [$storage],
392 range: YuvIntensityRange,
393 ) -> Result<(), ImageError> {
394 ycgco444_to_rgbx_impl::<$storage, $cn, $bp>(image, rgba, range)
395 }
396 };
397 }
398
399 define_ycgcg_full_chroma!(
400 ycgco444_to_rgba8,
401 u8,
402 RGBA_CN,
403 8,
404 "Converts YCgCo 444 planar format 8 bit-depth to Rgba 8 bit"
405 );
406 define_ycgcg_full_chroma!(
407 ycgco444_to_rgba10,
408 u16,
409 RGBA_CN,
410 10,
411 "Converts YCgCo 444 planar format 10 bit-depth to Rgba 10 bit"
412 );
413 define_ycgcg_full_chroma!(
414 ycgco444_to_rgba12,
415 u16,
416 RGBA_CN,
417 12,
418 "Converts YCgCo 444 planar format 12 bit-depth to Rgba 12 bit"
419 );