| 1 |
use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 2 |
use image::{GenericImage, ImageBuffer, Rgba}; |
| 3 |
|
| 4 |
pub fn bench_copy_from(c: &mut Criterion) { |
| 5 |
let at = rect_from_xy_ranges(256..1280, 256..1280); |
| 6 |
|
| 7 |
let mut target = ImageBuffer::from_pixel(2048, 2048, Rgba([0u8, 0, 0, 255])); |
| 8 |
let src = ImageBuffer::from_pixel(2048, 2048, Rgba([255u8, 0, 0, 255])); |
| 9 |
let part = ImageBuffer::from_pixel(at.width, at.height, Rgba([255u8, 0, 0, 255])); |
| 10 |
|
| 11 |
let view = image::GenericImageView::view(&src, at.x, at.y, at.width, at.height); |
| 12 |
|
| 13 |
const BG: Rgba<u8> = Rgba([0u8, 0, 0, 255]); |
| 14 |
let samples = image::flat::FlatSamples::with_monocolor(&BG, at.width, at.height); |
| 15 |
let singular = samples.as_view().unwrap(); |
| 16 |
|
| 17 |
let mut samples = src.as_flat_samples(); |
| 18 |
samples.layout.width = 1024; |
| 19 |
samples.layout.width_stride *= 2; |
| 20 |
samples.layout.height = 1024; |
| 21 |
samples.layout.height_stride *= 2; |
| 22 |
let skip = samples.as_view().unwrap(); |
| 23 |
|
| 24 |
c.bench_function("copy_from", |b| { |
| 25 |
b.iter(|| target.copy_from(black_box(&src), 0, 0)); |
| 26 |
}); |
| 27 |
|
| 28 |
c.bench_function("copy_at", |b| { |
| 29 |
b.iter(|| target.copy_from(black_box(&part), at.x, at.y)); |
| 30 |
}); |
| 31 |
|
| 32 |
c.bench_function("copy_view", |b| { |
| 33 |
b.iter(|| target.copy_from(black_box(&*view), at.x, at.y)); |
| 34 |
}); |
| 35 |
|
| 36 |
c.bench_function("copy_fill", |b| { |
| 37 |
b.iter(|| target.copy_from(black_box(&singular), at.x, at.y)); |
| 38 |
}); |
| 39 |
|
| 40 |
c.bench_function("copy_strides", |b| { |
| 41 |
b.iter(|| target.copy_from(black_box(&skip), at.x, at.y)); |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
pub fn bench_copy_subimage_from(c: &mut Criterion) { |
| 46 |
let vp = rect_from_xy_ranges(256..1280, 256..1280); |
| 47 |
let at = rect_from_xy_ranges(128..512, 128..512); |
| 48 |
|
| 49 |
let mut target = ImageBuffer::from_pixel(2048, 2048, Rgba([0u8, 0, 0, 255])); |
| 50 |
let mut target = target.sub_image(vp.x, vp.y, vp.width, vp.height); |
| 51 |
|
| 52 |
let src = ImageBuffer::from_pixel(vp.width, vp.height, Rgba([255u8, 0, 0, 255])); |
| 53 |
let part = ImageBuffer::from_pixel(at.width, at.height, Rgba([255u8, 0, 0, 255])); |
| 54 |
let view = image::GenericImageView::view(&src, at.x, at.y, at.width, at.height); |
| 55 |
|
| 56 |
const BG: Rgba<u8> = Rgba([0u8, 0, 0, 255]); |
| 57 |
let samples = image::flat::FlatSamples::with_monocolor(&BG, at.width, at.height); |
| 58 |
let singular = samples.as_view().unwrap(); |
| 59 |
|
| 60 |
let mut samples = src.as_flat_samples(); |
| 61 |
samples.layout.width = at.width / 2; |
| 62 |
samples.layout.width_stride *= 2; |
| 63 |
samples.layout.height = at.height / 2; |
| 64 |
samples.layout.height_stride *= 2; |
| 65 |
let skip = samples.as_view().unwrap(); |
| 66 |
|
| 67 |
c.bench_function("copy_subimage_from", |b| { |
| 68 |
b.iter(|| target.copy_from(black_box(&src), 0, 0)); |
| 69 |
}); |
| 70 |
|
| 71 |
c.bench_function("copy_subimage_at", |b| { |
| 72 |
b.iter(|| target.copy_from(black_box(&part), at.x, at.y)); |
| 73 |
}); |
| 74 |
|
| 75 |
c.bench_function("copy_subimage_view", |b| { |
| 76 |
b.iter(|| target.copy_from(black_box(&*view), at.x, at.y)); |
| 77 |
}); |
| 78 |
|
| 79 |
c.bench_function("copy_subimage_fill", |b| { |
| 80 |
b.iter(|| target.copy_from(black_box(&singular), at.x, at.y)); |
| 81 |
}); |
| 82 |
|
| 83 |
c.bench_function("copy_subimage_strides", |b| { |
| 84 |
b.iter(|| target.copy_from(black_box(&skip), at.x, at.y)); |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
criterion_group!(benches, bench_copy_from, bench_copy_subimage_from); |
| 89 |
criterion_main!(benches); |
| 90 |
|
| 91 |
// Backport of the constructor. |
| 92 |
fn rect_from_xy_ranges(x: std::ops::Range<u32>, y: std::ops::Range<u32>) -> image::math::Rect { |
| 93 |
image::math::Rect { |
| 94 |
x: x.start, |
| 95 |
y: y.start, |
| 96 |
width: x.end - x.start, |
| 97 |
height: y.end - y.start, |
| 98 |
} |
| 99 |
} |