From 1b6a04ca5504955c571d1c97504fb45ea0befee4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 8 Jan 2024 01:21:28 +0400 Subject: Initial vendor packages Signed-off-by: Valentin Popov --- vendor/image/benches/README.md | 6 ++ vendor/image/benches/copy_from.rs | 14 ++++ vendor/image/benches/decode.rs | 109 +++++++++++++++++++++++++++++++ vendor/image/benches/encode.rs | 134 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 263 insertions(+) create mode 100644 vendor/image/benches/README.md create mode 100644 vendor/image/benches/copy_from.rs create mode 100644 vendor/image/benches/decode.rs create mode 100644 vendor/image/benches/encode.rs (limited to 'vendor/image/benches') diff --git a/vendor/image/benches/README.md b/vendor/image/benches/README.md new file mode 100644 index 0000000..9516f2c --- /dev/null +++ b/vendor/image/benches/README.md @@ -0,0 +1,6 @@ +# Getting started with benchmarking + +To run the benchmarks you need a nightly rust toolchain. +Then you launch it with + + cargo +nightly bench --features=benchmarks diff --git a/vendor/image/benches/copy_from.rs b/vendor/image/benches/copy_from.rs new file mode 100644 index 0000000..37a4af8 --- /dev/null +++ b/vendor/image/benches/copy_from.rs @@ -0,0 +1,14 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use image::{GenericImage, ImageBuffer, Rgba}; + +pub fn bench_copy_from(c: &mut Criterion) { + 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])); + + c.bench_function("copy_from", |b| { + b.iter(|| dst.copy_from(black_box(&src), 0, 0)) + }); +} + +criterion_group!(benches, bench_copy_from); +criterion_main!(benches); diff --git a/vendor/image/benches/decode.rs b/vendor/image/benches/decode.rs new file mode 100644 index 0000000..3702d69 --- /dev/null +++ b/vendor/image/benches/decode.rs @@ -0,0 +1,109 @@ +use std::{fs, iter, path}; + +use criterion::{criterion_group, criterion_main, Criterion}; +use image::ImageFormat; + +#[derive(Clone, Copy)] +struct BenchDef { + dir: &'static [&'static str], + files: &'static [&'static str], + format: ImageFormat, +} + +fn load_all(c: &mut Criterion) { + const BENCH_DEFS: &'static [BenchDef] = &[ + BenchDef { + dir: &["bmp", "images"], + files: &[ + "Core_1_Bit.bmp", + "Core_4_Bit.bmp", + "Core_8_Bit.bmp", + "rgb16.bmp", + "rgb24.bmp", + "rgb32.bmp", + "pal4rle.bmp", + "pal8rle.bmp", + "rgb16-565.bmp", + "rgb32bf.bmp", + ], + format: ImageFormat::Bmp, + }, + BenchDef { + dir: &["gif", "simple"], + files: &["alpha_gif_a.gif", "sample_1.gif"], + format: ImageFormat::Gif, + }, + BenchDef { + dir: &["hdr", "images"], + files: &["image1.hdr", "rgbr4x4.hdr"], + format: ImageFormat::Hdr, + }, + BenchDef { + dir: &["ico", "images"], + files: &[ + "bmp-24bpp-mask.ico", + "bmp-32bpp-alpha.ico", + "png-32bpp-alpha.ico", + "smile.ico", + ], + format: ImageFormat::Ico, + }, + BenchDef { + dir: &["jpg", "progressive"], + files: &["3.jpg", "cat.jpg", "test.jpg"], + format: ImageFormat::Jpeg, + }, + // TODO: pnm + // TODO: png + BenchDef { + dir: &["tga", "testsuite"], + files: &["cbw8.tga", "ctc24.tga", "ubw8.tga", "utc24.tga"], + format: ImageFormat::Tga, + }, + BenchDef { + dir: &["tiff", "testsuite"], + files: &[ + "hpredict.tiff", + "hpredict_packbits.tiff", + "mandrill.tiff", + "rgb-3c-16b.tiff", + ], + format: ImageFormat::Tiff, + }, + BenchDef { + dir: &["webp", "images"], + files: &[ + "simple-gray.webp", + "simple-rgb.webp", + "vp8x-gray.webp", + "vp8x-rgb.webp", + ], + format: ImageFormat::WebP, + }, + ]; + + for bench in BENCH_DEFS { + bench_load(c, bench); + } +} + +criterion_group!(benches, load_all); +criterion_main!(benches); + +fn bench_load(c: &mut Criterion, def: &BenchDef) { + let group_name = format!("load-{:?}", def.format); + let mut group = c.benchmark_group(&group_name); + let paths = IMAGE_DIR.iter().chain(def.dir); + + for file_name in def.files { + let path: path::PathBuf = paths.clone().chain(iter::once(file_name)).collect(); + let buf = fs::read(path).unwrap(); + group.bench_function(file_name.to_owned(), |b| { + b.iter(|| { + image::load_from_memory_with_format(&buf, def.format).unwrap(); + }) + }); + } +} + +const IMAGE_DIR: [&'static str; 3] = [".", "tests", "images"]; diff --git a/vendor/image/benches/encode.rs b/vendor/image/benches/encode.rs new file mode 100644 index 0000000..0ca4b2a --- /dev/null +++ b/vendor/image/benches/encode.rs @@ -0,0 +1,134 @@ +extern crate criterion; + +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use image::{codecs::bmp::BmpEncoder, codecs::jpeg::JpegEncoder, ColorType}; + +use std::fs::File; +use std::io::{BufWriter, Seek, SeekFrom, Write}; + +trait Encoder { + fn encode_raw(&self, into: &mut Vec, im: &[u8], dims: u32, color: ColorType); + fn encode_bufvec(&self, into: &mut Vec, im: &[u8], dims: u32, color: ColorType); + fn encode_file(&self, file: &File, im: &[u8], dims: u32, color: ColorType); +} + +#[derive(Clone, Copy)] +struct BenchDef { + with: &'static dyn Encoder, + name: &'static str, + sizes: &'static [u32], + colors: &'static [ColorType], +} + +fn encode_all(c: &mut Criterion) { + const BENCH_DEFS: &'static [BenchDef] = &[ + BenchDef { + with: &Bmp, + name: "bmp", + sizes: &[100u32, 200, 400], + colors: &[ColorType::L8, ColorType::Rgb8, ColorType::Rgba8], + }, + BenchDef { + with: &Jpeg, + name: "jpeg", + sizes: &[64u32, 128, 256], + colors: &[ColorType::L8, ColorType::Rgb8, ColorType::Rgba8], + }, + ]; + + for definition in BENCH_DEFS { + encode_definition(c, definition) + } +} + +criterion_group!(benches, encode_all); +criterion_main!(benches); + +type BenchGroup<'a> = criterion::BenchmarkGroup<'a, criterion::measurement::WallTime>; + +/// Benchmarks encoding a zeroed image. +/// +/// For compressed formats this is surely not representative of encoding a normal image but it's a +/// start for benchmarking. +fn encode_zeroed(group: &mut BenchGroup, with: &dyn Encoder, size: u32, color: ColorType) { + let bytes = size as usize * usize::from(color.bytes_per_pixel()); + let im = vec![0; bytes * bytes]; + + group.bench_with_input( + BenchmarkId::new(format!("zero-{:?}-rawvec", color), size), + &im, + |b, image| { + let mut v = vec![]; + with.encode_raw(&mut v, &im, size, color); + b.iter(|| with.encode_raw(&mut v, image, size, color)); + }, + ); + group.bench_with_input( + BenchmarkId::new(format!("zero-{:?}-bufvec", color), size), + &im, + |b, image| { + let mut v = vec![]; + with.encode_raw(&mut v, &im, size, color); + b.iter(|| with.encode_bufvec(&mut v, image, size, color)); + }, + ); + group.bench_with_input( + BenchmarkId::new(format!("zero-{:?}-file", color), size), + &im, + |b, image| { + let file = File::create("temp.bmp").unwrap(); + b.iter(|| with.encode_file(&file, image, size, color)); + }, + ); +} + +fn encode_definition(criterion: &mut Criterion, def: &BenchDef) { + let mut group = criterion.benchmark_group(format!("encode-{}", def.name)); + + for &color in def.colors { + for &size in def.sizes { + encode_zeroed(&mut group, def.with, size, color); + } + } +} + +struct Bmp; + +struct Jpeg; + +trait EncoderBase { + fn encode(&self, into: impl Write, im: &[u8], dims: u32, color: ColorType); +} + +impl Encoder for T { + fn encode_raw(&self, into: &mut Vec, im: &[u8], dims: u32, color: ColorType) { + into.clear(); + self.encode(into, im, dims, color); + } + + fn encode_bufvec(&self, into: &mut Vec, im: &[u8], dims: u32, color: ColorType) { + into.clear(); + let buf = BufWriter::new(into); + self.encode(buf, im, dims, color); + } + + fn encode_file(&self, mut file: &File, im: &[u8], dims: u32, color: ColorType) { + file.seek(SeekFrom::Start(0)).unwrap(); + let buf = BufWriter::new(file); + self.encode(buf, im, dims, color); + } +} + +impl EncoderBase for Bmp { + fn encode(&self, mut into: impl Write, im: &[u8], size: u32, color: ColorType) { + let mut x = BmpEncoder::new(&mut into); + x.encode(im, size, size, color).unwrap(); + } +} + +impl EncoderBase for Jpeg { + fn encode(&self, mut into: impl Write, im: &[u8], size: u32, color: ColorType) { + let mut x = JpegEncoder::new(&mut into); + x.encode(im, size, size, color).unwrap(); + } +} -- cgit v1.2.3