From eceaa1f14dc7d395e93a9b026a26c989ee25f41e Mon Sep 17 00:00:00 2001 From: "Sergey \"Shnatsel\" Davidoff" Date: Fri, 27 Feb 2026 00:27:40 +0000 Subject: [PATCH] Add encoding benchmark for RGB32Float with float predictor and DEFLATE Co-Authored-By: Claude --- Cargo.toml | 4 ++++ benches/encode.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 benches/encode.rs diff --git a/Cargo.toml b/Cargo.toml index c1a7f0f..d1531f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,10 @@ zstd = ["dep:zstd"] name = "lzw" harness = false +[[bench]] +name = "encode" +harness = false + [[test]] name = "decode_libtiffpic" path = "tests/decode_libtiffpic.rs" diff --git a/benches/encode.rs b/benches/encode.rs new file mode 100644 index 0000000..23b2cf9 --- /dev/null +++ b/benches/encode.rs @@ -0,0 +1,44 @@ +extern crate criterion; +extern crate tiff; + +use criterion::{black_box, Criterion, Throughput}; +use std::io::Cursor; +use tiff::encoder::colortype::RGB32Float; +use tiff::encoder::{Compression, DeflateLevel, Predictor, TiffEncoder}; + +const WIDTH: u32 = 500; +const HEIGHT: u32 = 500; + +fn encode_image_f32_float_predictor_deflate(data: &[f32]) { + let mut buf = Cursor::new(Vec::with_capacity(WIDTH as usize * HEIGHT as usize * 4)); + TiffEncoder::new(&mut buf) + .unwrap() + .with_predictor(Predictor::FloatingPoint) + .with_compression(Compression::Deflate(DeflateLevel::Fast)) + .write_image::(WIDTH, HEIGHT, black_box(data)) + .unwrap(); +} + +fn main() { + // Generate synthetic f32 image data with a gradient pattern + let data: Vec = (0..WIDTH as usize * HEIGHT as usize * 3) + .map(|i| { + let pixel = i / 3; + let x = (pixel % WIDTH as usize) as f32 / WIDTH as f32; + let y = (pixel / WIDTH as usize) as f32 / HEIGHT as f32; + x * 0.5 + y * 0.5 + }) + .collect(); + + let mut c = Criterion::default().configure_from_args(); + let mut group = c.benchmark_group("tiff-encode"); + + group + .sample_size(20) + .throughput(Throughput::Bytes( + (WIDTH as u64) * (HEIGHT as u64) * 3 * std::mem::size_of::() as u64, + )) + .bench_function("f32-float-predictor-deflate-fast", |b| { + b.iter(|| encode_image_f32_float_predictor_deflate(&data)) + }); +}