This commit is contained in:
bendn 2023-09-07 10:02:44 +07:00
parent c997bf1990
commit 51c506425c
No known key found for this signature in database
GPG key ID: 0D9D3A2A3B2A93D6
10 changed files with 49 additions and 55 deletions

View file

@ -10,6 +10,17 @@ repository = "https://github.com/bend-n/fimg"
[dependencies] [dependencies]
png = { version = "0.17", features = ["unstable"], optional = true } png = { version = "0.17", features = ["unstable"], optional = true }
[dev-dependencies]
iai = { version = "0.1.1", features = [], git = "https://github.com/bend-n/iai/" }
[[bench]]
name = "overlays"
harness = false
[[bench]]
name = "affine_transformations"
harness = false
[features] [features]
save = ["png"] save = ["png"]
default = ["save"] default = ["save"]

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,20 +1,16 @@
#![feature(test)]
extern crate test;
use fimg::*; use fimg::*;
use test::Bencher;
macro_rules! bench { macro_rules! bench {
(fn $name: ident() { run $fn: ident() }) => { (fn $name: ident() { run $fn: ident() }) => {
#[bench] fn $name() {
fn $name(b: &mut Bencher) {
let mut img: Image<_, 4> = let mut img: Image<_, 4> =
Image::build(64, 64).buf(include_bytes!("4_180x180.imgbuf").to_vec()); Image::build(160, 160).buf(include_bytes!("4_160x160.imgbuf").to_vec());
#[allow(unused_unsafe)] for _ in 0..256 {
b.iter(|| unsafe { #[allow(unused_unsafe)]
for _ in 0..256 { unsafe {
img.$fn(); img.$fn()
} };
}); }
} }
}; };
} }
@ -24,3 +20,4 @@ bench!(fn flip_v() { run flip_v() });
bench!(fn rotate_90() { run rot_90() }); bench!(fn rotate_90() { run rot_90() });
bench!(fn rotate_180() { run rot_180() }); bench!(fn rotate_180() { run rot_180() });
bench!(fn rotate_270() { run rot_270() }); bench!(fn rotate_270() { run rot_270() });
iai::main!(flip_h, flip_v, rotate_90, rotate_180, rotate_270);

View file

@ -1,47 +1,32 @@
#![feature(test)]
extern crate test;
use fimg::*; use fimg::*;
use test::Bencher;
#[bench] fn overlay_3on3at() {
fn overlay_3on3at(bench: &mut Bencher) { let mut a: Image<_, 3> = Image::alloc(128, 128);
let mut v = vec![0u8; 3 * 64 * 64]; let b: Image<&[u8], 3> = Image::build(8, 8).buf(include_bytes!("3_8x8.imgbuf"));
let mut a: Image<_, 3> = Image::build(64, 64).buf(v.as_mut_slice()); for x in 0..16 {
let b: Image<&[u8], 3> = Image::build(4, 4).buf(include_bytes!("3_4x4.imgbuf")); for y in 0..16 {
bench.iter(|| unsafe { unsafe { a.as_mut().overlay_at(&b, x * 8, y * 8) };
for x in 0..16 {
for y in 0..16 {
a.overlay_at(&b, x * 4, y * 4);
}
} }
}); }
assert_eq!(a.as_ref().buffer(), include_bytes!("3x3_at_out.imgbuf"));
} }
#[bench] fn overlay_4on3at() {
fn overlay_4on3at(bench: &mut Bencher) { let mut a: Image<_, 3> = Image::alloc(128, 128);
let mut a: Image<_, 3> = Image::alloc(64, 64); let b: Image<&[u8], 4> = Image::build(8, 8).buf(include_bytes!("4_8x8.imgbuf"));
let b: Image<&[u8], 4> = Image::build(4, 4).buf(include_bytes!("4_4x4.imgbuf")); for x in 0..16 {
bench.iter(|| unsafe { for y in 0..16 {
for x in 0..16 { unsafe { a.as_mut().overlay_at(&b, x * 8, y * 8) };
for y in 0..16 {
a.as_mut().overlay_at(&b, x * 4, y * 4);
}
} }
}); }
assert_eq!(a.as_ref().buffer(), include_bytes!("4x3_at_out.imgbuf"));
} }
#[bench] fn overlay_4on4at() {
fn overlay_4on4at(bench: &mut Bencher) { let mut a: Image<_, 4> = Image::alloc(128, 128);
let mut a: Image<_, 4> = Image::alloc(64, 64); let b: Image<&[u8], 4> = Image::build(8, 8).buf(include_bytes!("4_8x8.imgbuf"));
let b: Image<&[u8], 4> = Image::build(4, 4).buf(include_bytes!("4_4x4.imgbuf")); for x in 0..16 {
bench.iter(|| unsafe { for y in 0..16 {
for x in 0..16 { unsafe { a.as_mut().overlay_at(&b, x * 8, y * 8) };
for y in 0..16 {
a.as_mut().overlay_at(&b, x * 4, y * 4);
}
} }
}); }
assert_eq!(a.as_ref().buffer(), include_bytes!("4x4_at_out.imgbuf"));
} }
iai::main!(overlay_3on3at, overlay_4on3at, overlay_4on4at);

View file

@ -32,10 +32,14 @@ impl<B: buf::Buffer, const C: usize> Builder<B, C> {
} }
/// apply a buffer, and build /// apply a buffer, and build
#[track_caller]
pub fn buf(self, buffer: B) -> Image<B, C> { pub fn buf(self, buffer: B) -> Image<B, C> {
if buffer.len() as u32 != C as u32 * self.width * self.height { let len = C as u32 * self.width * self.height;
panic!("invalid buffer size"); assert!(
} buffer.len() as u32 == len,
"invalid buffer size (expected {len}, got {})",
buffer.len()
);
Image { Image {
buffer, buffer,
width: self.width.try_into().expect("passed zero width to builder"), width: self.width.try_into().expect("passed zero width to builder"),