Skip to content

Commit fd7b408

Browse files
committed
Add ycbcr benchmark
1 parent 521dd2f commit fd7b408

File tree

6 files changed

+118
-3
lines changed

6 files changed

+118
-3
lines changed

criterion/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ harness = false
2828
[[bench]]
2929
name = "fdct"
3030
harness = false
31+
32+
[[bench]]
33+
name = "ycbcr"
34+
harness = false

criterion/benches/ycbcr.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2+
3+
use jpeg_encoder::{ImageBuffer, RgbImage};
4+
use std::time::Duration;
5+
6+
fn create_bench_img() -> (Vec<u8>, u16, u16) {
7+
let width = 1001;
8+
let height = 500;
9+
10+
let mut data = Vec::with_capacity(width * height * 3);
11+
12+
for y in 0..height {
13+
for x in 0..width {
14+
if (x * y) % 13 == 0 {
15+
data.push(0);
16+
data.push(0);
17+
data.push(0);
18+
} else if (x * y) % 17 == 0 {
19+
data.push(255);
20+
data.push(255);
21+
data.push(255);
22+
} else if (x * y) % 19 == 0 {
23+
data.push(255);
24+
data.push(0);
25+
data.push(0);
26+
} else if (x * y) % 21 == 0 {
27+
data.push(0);
28+
data.push(0);
29+
data.push(255);
30+
} else if (x * y) % 23 == 0 {
31+
data.push(0);
32+
data.push(255);
33+
data.push(0);
34+
} else if (x * y) % 25 == 0 {
35+
data.push(96);
36+
data.push(255);
37+
data.push(96);
38+
} else if (x * y) % 27 == 0 {
39+
data.push(255);
40+
data.push(96);
41+
data.push(96);
42+
} else if (x * y) % 29 == 0 {
43+
data.push(96);
44+
data.push(96);
45+
data.push(255);
46+
} else {
47+
data.push((x % 256) as u8);
48+
data.push((x % 256) as u8);
49+
data.push(((x * y) % 256) as u8);
50+
}
51+
}
52+
}
53+
54+
(data, width as u16, height as u16)
55+
}
56+
57+
fn criterion_benchmark(c: &mut Criterion) {
58+
let (data, width, height) = create_bench_img();
59+
let res1 = Vec::with_capacity(usize::from(width));
60+
let res2 = Vec::with_capacity(usize::from(width));
61+
let res3 = Vec::with_capacity(usize::from(width));
62+
let res4 = Vec::with_capacity(usize::from(width));
63+
64+
let mut res = [res1, res2, res3, res4];
65+
66+
let mut group = c.benchmark_group("ycbcr");
67+
group.measurement_time(Duration::from_secs(45));
68+
group.warm_up_time(Duration::from_secs(10));
69+
70+
group.bench_function("default ycbcr", |b| {
71+
let image_buffer = RgbImage(&data, width, height);
72+
73+
b.iter(|| {
74+
for y in 0..height {
75+
res[0].clear();
76+
res[1].clear();
77+
res[2].clear();
78+
image_buffer.fill_buffers(y, black_box(&mut res));
79+
}
80+
black_box(&res);
81+
});
82+
});
83+
84+
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
85+
group.bench_function("ycbcr avx2", |b| {
86+
use jpeg_encoder::RgbImageAVX2;
87+
let image_buffer = RgbImageAVX2(&data, width, height);
88+
89+
b.iter(|| {
90+
for y in 0..height {
91+
res[0].clear();
92+
res[1].clear();
93+
res[2].clear();
94+
image_buffer.fill_buffers(y, black_box(&mut res));
95+
}
96+
black_box(&res);
97+
})
98+
});
99+
100+
group.finish();
101+
}
102+
103+
criterion_group!(benches, criterion_benchmark);
104+
criterion_main!(benches);

src/avx2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod ycbcr;
33

44
use crate::encoder::Operations;
55
pub use fdct::fdct_avx2;
6-
pub(crate) use ycbcr::*;
6+
pub use ycbcr::*;
77

88
pub(crate) struct AVX2Operations;
99

src/avx2/ycbcr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{rgb_to_ycbcr, ImageBuffer, JpegColorType};
1616

1717
macro_rules! ycbcr_image_avx2 {
1818
($name:ident, $num_colors:expr, $o1:expr, $o2:expr, $o3:expr) => {
19-
pub(crate) struct $name<'a>(pub &'a [u8], pub u16, pub u16);
19+
pub struct $name<'a>(pub &'a [u8], pub u16, pub u16);
2020

2121
impl<'a> $name<'a> {
2222
#[target_feature(enable = "avx2")]

src/image_buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn get_line(data: &[u8], y: u16, width:u16, num_colors: usize) -> &[u8] {
134134

135135
macro_rules! ycbcr_image {
136136
($name:ident, $num_colors:expr, $o1:expr, $o2:expr, $o3:expr) => {
137-
pub(crate) struct $name<'a>(pub &'a [u8], pub u16, pub u16);
137+
pub struct $name<'a>(pub &'a [u8], pub u16, pub u16);
138138

139139
impl<'a> ImageBuffer for $name<'a> {
140140
fn get_jpeg_color_type(&self) -> JpegColorType {

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,16 @@ pub use writer::{PixelDensity, PixelDensityUnit, JfifWrite};
5050

5151
#[cfg(feature = "benchmark")]
5252
pub use fdct::fdct;
53+
54+
#[cfg(feature = "benchmark")]
55+
pub use image_buffer::RgbImage;
56+
5357
#[cfg(all(feature = "benchmark", feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
5458
pub use avx2::fdct_avx2;
5559

60+
#[cfg(all(feature = "benchmark", feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
61+
pub use avx2::RgbImageAVX2;
62+
5663
#[cfg(test)]
5764
mod tests {
5865
use crate::image_buffer::rgb_to_ycbcr;

0 commit comments

Comments
 (0)