/rust/registry/src/index.crates.io-6f17d22bba15001f/av1-grain-0.2.4/src/util.rs
Line | Count | Source (jump to first uncovered line) |
1 | | #[cfg(feature = "diff")] |
2 | | use std::{borrow::Cow, mem::size_of}; |
3 | | |
4 | | #[cfg(feature = "diff")] |
5 | | use v_frame::{ |
6 | | frame::Frame, |
7 | | prelude::{CastFromPrimitive, ChromaSampling, Pixel}, |
8 | | }; |
9 | | |
10 | | #[cfg(feature = "diff")] |
11 | 0 | pub fn frame_into_u8<T: Pixel>(frame: &Frame<T>, bit_depth: usize) -> Cow<'_, Frame<u8>> { |
12 | 0 | if size_of::<T>() == 1 { |
13 | 0 | assert_eq!(bit_depth, 8); |
14 | | // SAFETY: We know from the size check that this must be a `Frame<u8>` |
15 | 0 | Cow::Borrowed(unsafe { &*(frame as *const Frame<T>).cast::<Frame<u8>>() }) |
16 | 0 | } else if size_of::<T>() == 2 { |
17 | 0 | assert!(bit_depth > 8 && bit_depth <= 16); |
18 | 0 | let mut u8_frame: Frame<u8> = Frame::new_with_padding( |
19 | 0 | frame.planes[0].cfg.width, |
20 | 0 | frame.planes[0].cfg.height, |
21 | 0 | match frame.planes[1].cfg.xdec + frame.planes[1].cfg.ydec { |
22 | 0 | 0 => ChromaSampling::Cs444, |
23 | 0 | 1 => ChromaSampling::Cs422, |
24 | 0 | 2 => ChromaSampling::Cs420, |
25 | 0 | _ => unreachable!(), |
26 | | }, |
27 | 0 | frame.planes[0].cfg.xpad, |
28 | | ); |
29 | 0 | for i in 0..3 { |
30 | 0 | let out_plane = &mut u8_frame.planes[i]; |
31 | 0 | for (i, o) in frame.planes[i] |
32 | 0 | .data_origin() |
33 | 0 | .iter() |
34 | 0 | .zip(out_plane.data_origin_mut().iter_mut()) |
35 | 0 | { |
36 | 0 | *o = (u16::cast_from(*i) >> (bit_depth - 8usize)) as u8; |
37 | 0 | } |
38 | | } |
39 | 0 | Cow::Owned(u8_frame) |
40 | | } else { |
41 | 0 | unimplemented!("Bit depths greater than 16 are not currently supported"); |
42 | | } |
43 | 0 | } |