Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions benches/copy_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use image::{GenericImage, ImageBuffer, Rgba};
pub fn bench_copy_from(c: &mut Criterion) {
let at = image::math::Rect::from_xy_ranges(256..1280, 256..1280);

let mut target = ImageBuffer::from_pixel(2048, 2048, Rgba([0u8, 0, 0, 255]));
let src = ImageBuffer::from_pixel(2048, 2048, Rgba([255u8, 0, 0, 255]));
let mut dst = ImageBuffer::from_pixel(2048, 2048, Rgba([0u8, 0, 0, 255]));
let part = ImageBuffer::from_pixel(at.width, at.height, Rgba([255u8, 0, 0, 255]));

let view = image::GenericImageView::view(&src, at);
Expand All @@ -22,25 +22,68 @@ pub fn bench_copy_from(c: &mut Criterion) {
let skip = samples.as_view().unwrap();

c.bench_function("copy_from", |b| {
b.iter(|| dst.copy_from(black_box(&src), 0, 0));
b.iter(|| target.copy_from(black_box(&src), 0, 0));
});

c.bench_function("copy_at", |b| {
b.iter(|| dst.copy_from(black_box(&part), at.x, at.y));
b.iter(|| target.copy_from(black_box(&part), at.x, at.y));
});

c.bench_function("copy_view", |b| {
b.iter(|| dst.copy_from(black_box(&*view), at.x, at.y));
b.iter(|| target.copy_from(black_box(&*view), at.x, at.y));
});

c.bench_function("copy_fill", |b| {
b.iter(|| dst.copy_from(black_box(&singular), at.x, at.y));
b.iter(|| target.copy_from(black_box(&singular), at.x, at.y));
});

c.bench_function("copy_strides", |b| {
b.iter(|| dst.copy_from(black_box(&skip), at.x, at.y));
b.iter(|| target.copy_from(black_box(&skip), at.x, at.y));
});
}

criterion_group!(benches, bench_copy_from);
pub fn bench_copy_subimage_from(c: &mut Criterion) {
let viewport = image::math::Rect::from_xy_ranges(256..1280, 256..1280);
let at = image::math::Rect::from_xy_ranges(128..512, 128..512);

let mut target = ImageBuffer::from_pixel(2048, 2048, Rgba([0u8, 0, 0, 255]));
let mut target = target.sub_image(viewport);

let src = ImageBuffer::from_pixel(viewport.width, viewport.height, Rgba([255u8, 0, 0, 255]));
let part = ImageBuffer::from_pixel(at.width, at.height, Rgba([255u8, 0, 0, 255]));
let view = image::GenericImageView::view(&src, at);

const BG: Rgba<u8> = Rgba([0u8, 0, 0, 255]);
let samples = image::flat::FlatSamples::with_monocolor(&BG, at.width, at.height);
let singular = samples.as_view().unwrap();

let mut samples = src.as_flat_samples();
samples.layout.width = at.width / 2;
samples.layout.width_stride *= 2;
samples.layout.height = at.height / 2;
samples.layout.height_stride *= 2;
let skip = samples.as_view().unwrap();

c.bench_function("copy_subimage_from", |b| {
b.iter(|| target.copy_from(black_box(&src), 0, 0));
});

c.bench_function("copy_subimage_at", |b| {
b.iter(|| target.copy_from(black_box(&part), at.x, at.y));
});

c.bench_function("copy_subimage_view", |b| {
b.iter(|| target.copy_from(black_box(&*view), at.x, at.y));
});

c.bench_function("copy_subimage_fill", |b| {
b.iter(|| target.copy_from(black_box(&singular), at.x, at.y));
});

c.bench_function("copy_subimage_strides", |b| {
b.iter(|| target.copy_from(black_box(&skip), at.x, at.y));
});
}

criterion_group!(benches, bench_copy_from, bench_copy_subimage_from);
criterion_main!(benches);
11 changes: 11 additions & 0 deletions src/images/sub_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ where
self.image
.blend_pixel(x + self.xoffset, y + self.yoffset, pixel);
}

fn copy_from<O>(&mut self, other: &O, x: u32, y: u32) -> Result<(), crate::ImageError>
where
O: GenericImageView<Pixel = Self::Pixel>,
{
Rect::from_image_at(other, x, y).test_in_bounds(self)?;
// Dispatch the inner images `copy_from` method with adjusted offsets. this ensures its
// potentially optimized implementation gets used.
self.image
.copy_from(other, x + self.xoffset, y + self.yoffset)
}
}

#[cfg(test)]
Expand Down
Loading