1//! ```rust
2//! use ravif::*;
3//! # fn doit(pixels: &[RGBA8], width: usize, height: usize) -> Result<(), Error> {
4//! let res = Encoder::new()
5//! .with_quality(70.)
6//! .with_speed(4)
7//! .encode_rgba(Img::new(pixels, width, height))?;
8//! std::fs::write("hello.avif", res.avif_file);
9//! # Ok(()) }
10
11mod av1encoder;
12
13mod error;
14pub use av1encoder::ColorModel;
15pub use error::Error;
16
17#[doc(hidden)]
18#[deprecated = "Renamed to `ColorModel`"]
19pub use ColorModel as ColorSpace;
20
21pub use av1encoder::{AlphaColorMode, BitDepth, EncodedImage, Encoder};
22#[doc(inline)]
23pub use rav1e::prelude::MatrixCoefficients;
24
25mod dirtyalpha;
26
27#[doc(no_inline)]
28pub use imgref::Img;
29#[doc(no_inline)]
30pub use rgb::{RGB8, RGBA8};
31
32#[cfg(not(feature = "threading"))]
33mod rayoff {
34 pub fn current_num_threads() -> usize {
35 std::thread::available_parallelism().map(|v| v.get()).unwrap_or(1)
36 }
37
38 pub fn join<A, B>(a: impl FnOnce() -> A, b: impl FnOnce() -> B) -> (A, B) {
39 (a(), b())
40 }
41}
42
43#[test]
44fn encode8_with_alpha_auto() {
45 let img = imgref::ImgVec::new((0..200).flat_map(|y| (0..256).map(move |x| {
46 RGBA8::new(x as u8, y as u8, 255, (x + y) as u8)
47 })).collect(), 256, 200);
48
49 let enc = Encoder::new()
50 .with_quality(22.0)
51 .with_speed(1)
52 .with_alpha_quality(22.0)
53 .with_alpha_color_mode(AlphaColorMode::UnassociatedDirty)
54 .with_num_threads(Some(2));
55 let EncodedImage { avif_file, color_byte_size, alpha_byte_size , .. } = enc.encode_rgba(img.as_ref()).unwrap();
56 assert!(color_byte_size > 50 && color_byte_size < 1000);
57 assert!(alpha_byte_size > 50 && alpha_byte_size < 1000); // the image must have alpha
58
59 let parsed = avif_parse::read_avif(&mut avif_file.as_slice()).unwrap();
60 assert!(parsed.alpha_item.is_some());
61 assert!(parsed.primary_item.len() > 100);
62 assert!(parsed.primary_item.len() < 1000);
63
64 let md = parsed.primary_item_metadata().unwrap();
65 assert_eq!(md.max_frame_width.get(), 256);
66 assert_eq!(md.max_frame_height.get(), 200);
67 assert_eq!(md.bit_depth, 8);
68}
69
70#[test]
71fn encode8_opaque() {
72 let img = imgref::ImgVec::new((0..101).flat_map(|y| (0..129).map(move |x| {
73 RGBA8::new(255, 100 + x as u8, y as u8, 255)
74 })).collect(), 129, 101);
75
76 let enc = Encoder::new()
77 .with_quality(33.0)
78 .with_speed(10)
79 .with_alpha_quality(33.0)
80 .with_bit_depth(BitDepth::Auto)
81 .with_alpha_color_mode(AlphaColorMode::UnassociatedDirty)
82 .with_num_threads(Some(1));
83 let EncodedImage { avif_file, color_byte_size, alpha_byte_size , .. } = enc.encode_rgba(img.as_ref()).unwrap();
84 assert_eq!(0, alpha_byte_size); // the image must not have alpha
85 assert!(color_byte_size > 50 && color_byte_size < 1000);
86
87 let parsed1 = avif_parse::read_avif(&mut avif_file.as_slice()).unwrap();
88 assert_eq!(None, parsed1.alpha_item);
89
90 let md = parsed1.primary_item_metadata().unwrap();
91 assert_eq!(md.max_frame_width.get(), 129);
92 assert_eq!(md.max_frame_height.get(), 101);
93 assert!(md.still_picture);
94 assert_eq!(md.bit_depth, 10);
95
96 let img = img.map_buf(|b| b.into_iter().map(|px| px.rgb()).collect::<Vec<_>>());
97
98 let enc = Encoder::new()
99 .with_quality(33.0)
100 .with_speed(10)
101 .with_bit_depth(BitDepth::Ten)
102 .with_alpha_quality(33.0)
103 .with_alpha_color_mode(AlphaColorMode::UnassociatedDirty)
104 .with_num_threads(Some(1));
105
106 let EncodedImage { avif_file, color_byte_size, alpha_byte_size , .. } = enc.encode_rgb(img.as_ref()).unwrap();
107 assert_eq!(0, alpha_byte_size); // the image must not have alpha
108 assert!(color_byte_size > 50 && color_byte_size < 1000);
109
110 let parsed2 = avif_parse::read_avif(&mut avif_file.as_slice()).unwrap();
111
112 assert_eq!(parsed1.alpha_item, parsed2.alpha_item);
113 assert_eq!(parsed1.primary_item, parsed2.primary_item); // both are the same pixels
114}
115
116#[test]
117fn encode8_cleans_alpha() {
118 let img = imgref::ImgVec::new((0..200).flat_map(|y| (0..256).map(move |x| {
119 RGBA8::new((((x/ 5 + y ) & 0xF) << 4) as u8, (7 * x + y / 2) as u8, ((x * y) & 0x3) as u8, ((x + y) as u8 & 0x7F).saturating_sub(100))
120 })).collect(), 256, 200);
121
122 let enc = Encoder::new()
123 .with_quality(66.0)
124 .with_speed(6)
125 .with_alpha_quality(88.0)
126 .with_alpha_color_mode(AlphaColorMode::UnassociatedDirty)
127 .with_num_threads(Some(1));
128
129 let dirty = enc
130 .encode_rgba(img.as_ref())
131 .unwrap();
132
133 let clean = enc
134 .with_alpha_color_mode(AlphaColorMode::UnassociatedClean)
135 .encode_rgba(img.as_ref())
136 .unwrap();
137
138 assert_eq!(clean.alpha_byte_size, dirty.alpha_byte_size); // same alpha on both
139 assert!(clean.alpha_byte_size > 200 && clean.alpha_byte_size < 1000);
140 assert!(clean.color_byte_size > 2000 && clean.color_byte_size < 6000);
141 assert!(clean.color_byte_size < dirty.color_byte_size / 2); // significant reduction in color data
142}
143