Skip to content

Commit

Permalink
Simplify benchmarks and tests
Browse files Browse the repository at this point in the history
Phase out the use of dedicated image buffers.
Instead, use iterators everywhere for pixel-to-pixel usecases.

Signed-off-by: Christopher N. Hesse <[email protected]>
  • Loading branch information
raymanfx committed Oct 28, 2023
1 parent f40e995 commit 97fa0a0
Show file tree
Hide file tree
Showing 21 changed files with 231 additions and 416 deletions.
4 changes: 2 additions & 2 deletions ffimage-yuv/benches/bench_main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use criterion::criterion_main;

mod packed;
mod convert;

criterion_main! {
packed::convert::benches,
convert::benches,
}
45 changes: 45 additions & 0 deletions ffimage-yuv/benches/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use criterion::{black_box, criterion_group, Criterion};

use ffimage::color::Rgb;
use ffimage_yuv::{yuv::Yuv, yuv422::Yuv422};

pub fn rgb_to_yuv(c: &mut Criterion) {
let resolutions = [(640, 480), (1280, 720)];

for res in resolutions {
let rgb = vec![Rgb::<u8>([10, 10, 10]); res.0 * res.1];
let mut yuv = vec![Yuv::<u8>([0, 0, 0]); res.0 * res.1];

c.bench_function(&format!("Rgb[u8] -> Yuv[u8] ({}x{})", res.0, res.1), |b| {
b.iter(|| {
rgb.iter()
.zip(yuv.iter_mut())
.for_each(|(rgb, yuv)| black_box(*yuv = (*rgb).into()))
})
});
}
}

pub fn rgb_to_yuyv(c: &mut Criterion) {
let resolutions = [(640, 480), (1280, 720)];

for res in resolutions {
let rgb = vec![Rgb::<u8>([10, 10, 10]); res.0 * res.1];
let [mut yuv1, mut yuv2] = [Yuv::<u8>([0, 0, 0]); 2];
let mut yuyv = vec![Yuv422::<u8, 0, 2, 1, 3>([0, 0, 0, 0]); (res.0 * res.1) / 2];

c.bench_function(&format!("Rgb[u8] -> Yuyv[u8] ({}x{})", res.0, res.1), |b| {
b.iter(|| {
(rgb.iter().zip(rgb.iter().skip(1)))
.zip(yuyv.iter_mut())
.for_each(|((rgb1, rgb2), yuyv)| {
black_box(yuv1 = (*rgb1).into());
black_box(yuv2 = (*rgb2).into());
black_box(*yuyv = [yuv1, yuv2].into());
})
})
});
}
}

criterion_group!(benches, rgb_to_yuv, rgb_to_yuyv);
38 changes: 0 additions & 38 deletions ffimage-yuv/benches/packed/convert.rs

This file was deleted.

1 change: 0 additions & 1 deletion ffimage-yuv/benches/packed/mod.rs

This file was deleted.

85 changes: 85 additions & 0 deletions ffimage-yuv/tests/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::ops::RangeInclusive;

use ffimage::color::Rgb;

use ffimage_yuv::{
yuv::Yuv,
yuv422::{Yuv422, Yuyv},
};

fn make_range(val: u8, delta: u8) -> RangeInclusive<u8> {
let lower = if val <= delta { 0 } else { val - delta };
let upper = if val >= 255 - delta { 255 } else { val + delta };

lower..=upper
}

#[test]
fn convert_convert_yuy_to_yuyv() {
let yuv = vec![Yuv::<u8>([10, 10, 10]); 10];
let yuyv: Vec<Yuyv<u8>> = yuv
.iter()
.copied()
.zip(yuv.iter().copied().skip(1))
.map(|(yuv1, yuv2)| Yuyv::<u8>::from([yuv1, yuv2]))
.collect();

(yuv.iter().copied().zip(yuv.iter().copied().skip(1)))
.zip(yuyv.into_iter())
.for_each(|((yuv1, yuv2), yuyv)| {
// one macropixel is two image pixels
assert_eq!(yuyv[0], yuv1[0]);
assert_eq!(yuyv[1], yuv1[1]);
assert_eq!(yuyv[2], yuv2[1]);
assert_eq!(yuyv[3], yuv1[2]);
});
}

#[test]
fn convert_convert_yuyv_to_yuv() {
let yuyv = vec![Yuv422::<u8, 0, 2, 1, 3>([10, 10, 10, 10]); 10];
let yuv: Vec<Yuv<u8>> = yuyv
.iter()
.copied()
.map(|yuyv| <[Yuv<u8>; 2]>::from(yuyv))
.flatten()
.collect();

yuyv.iter()
.copied()
.zip(yuv.iter().copied().zip(yuv.iter().copied().skip(1)))
.for_each(|(yuyv, (yuv1, yuv2))| {
// one macropixel is two image pixels
assert_eq!(yuv1[0], yuyv[0]);
assert_eq!(yuv1[1], yuyv[1]);
assert_eq!(yuv2[0], yuyv[2]);
assert_eq!(yuv1[2], yuyv[3]);
});
}

#[test]
fn convert_rgb_to_yuv_to_rgb() {
let rgb_in = vec![Rgb::<u8>([10, 10, 10]); 10];
let yuv: Vec<Yuv<u8>> = rgb_in
.iter()
.copied()
.map(|rgb| Yuv::<u8>::from(rgb))
.collect();
let rgb_out: Vec<Rgb<u8>> = yuv
.iter()
.copied()
.map(|yuv| Rgb::<u8>::from(yuv))
.collect();

rgb_in
.into_iter()
.zip(rgb_out.into_iter())
.for_each(|(rgb_in, rgb_out)| {
let r_range = make_range(rgb_in[0], 1);
let g_range = make_range(rgb_in[1], 1);
let b_range = make_range(rgb_in[2], 1);
assert!(r_range.contains(&rgb_out[0]));
assert!(g_range.contains(&rgb_out[1]));
assert!(b_range.contains(&rgb_out[2]));
});
}
44 changes: 0 additions & 44 deletions ffimage-yuv/tests/yuv.rs

This file was deleted.

48 changes: 0 additions & 48 deletions ffimage-yuv/tests/yuy422.rs

This file was deleted.

4 changes: 2 additions & 2 deletions ffimage/benches/bench_main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use criterion::criterion_main;

mod packed;
mod convert;

criterion_main! {
packed::convert::benches,
convert::benches,
}
39 changes: 39 additions & 0 deletions ffimage/benches/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use criterion::{black_box, criterion_group, Criterion};

use ffimage::color::{Gray, Rgb};

pub fn rgb_to_bgr(c: &mut Criterion) {
let resolutions = [(640, 480), (1280, 720)];

for res in resolutions {
let rgb = vec![Rgb::<u8>([10, 10, 10]); res.0 * res.1];
let mut bgr = vec![Rgb::<u8, 2, 1, 0>([0, 0, 0]); res.0 * res.1];

c.bench_function(&format!("Rgb[u8] -> Bgr[u8] ({}x{})", res.0, res.1), |b| {
b.iter(|| {
rgb.iter()
.zip(bgr.iter_mut())
.for_each(|(rgb, bgr)| black_box(*bgr = (*rgb).into()))
})
});
}
}

pub fn rgb_to_gray(c: &mut Criterion) {
let resolutions = [(640, 480), (1280, 720)];

for res in resolutions {
let rgb = vec![Rgb::<u8>([10, 10, 10]); res.0 * res.1];
let mut gray = vec![Gray::<u8>([0]); res.0 * res.1];

c.bench_function(&format!("Rgb[u8] -> Gray[u8] ({}x{})", res.0, res.1), |b| {
b.iter(|| {
rgb.iter()
.zip(gray.iter_mut())
.for_each(|(rgb, gray)| black_box(*gray = (*rgb).into()))
})
});
}
}

criterion_group!(benches, rgb_to_bgr, rgb_to_gray);
31 changes: 0 additions & 31 deletions ffimage/benches/packed/convert.rs

This file was deleted.

1 change: 0 additions & 1 deletion ffimage/benches/packed/mod.rs

This file was deleted.

6 changes: 6 additions & 0 deletions ffimage/src/color/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use crate::traits::Pixel;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Rgb<T, const R: usize = 0, const G: usize = 1, const B: usize = 2>(pub [T; 3]);

/// BGR pixel
pub type Bgr<T> = Rgb<T, 2, 1, 0>;

impl<T, const R: usize, const G: usize, const B: usize> Deref for Rgb<T, R, G, B> {
type Target = [T; 3];

Expand Down Expand Up @@ -89,6 +92,9 @@ pub struct Rgba<T, const R: usize = 0, const G: usize = 1, const B: usize = 2, c
pub [T; 4],
);

/// BGR pixel with alpha channel
pub type Bgra<T> = Rgba<T, 2, 1, 0, 3>;

impl<T, const R: usize, const G: usize, const B: usize, const A: usize> Deref
for Rgba<T, R, G, B, A>
{
Expand Down
16 changes: 0 additions & 16 deletions ffimage/tests/color/gray.rs

This file was deleted.

Loading

0 comments on commit 97fa0a0

Please sign in to comment.