Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding benchmark #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ bytemuck = "1.12"

[dev-dependencies]
image = "0.24"
criterion = "0.3"
tiff = "0.7.3"

[[bench]]
name = "decoding"
harness = false
57 changes: 57 additions & 0 deletions benches/decoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use criterion::{criterion_group, criterion_main, Criterion};
use dtm::DTM;
use image::GenericImageView;
use std::fs::File;
use tiff::decoder::{Decoder, DecodingResult};
use tiff::ColorType;

fn decode_full_png() {
let data_png = image::open("data/N265E425.png").unwrap();
assert_eq!(data_png.bounds(), (0, 0, 5000, 5000));
}

fn decode_full_dtm() {
let (descriptor, _data_dtm) = DTM::decode_file("data/N265E425.dtm").unwrap();
assert_eq!(descriptor.width, 5000);
assert_eq!(descriptor.height, 5000);
}

fn decode_full_tif() {
let data_tif = image::open("data/N265E425.tif").unwrap();
assert_eq!(data_tif.bounds(), (0, 0, 5000, 5000));
}

fn decode_tif_tiles() {
let img_file = File::open("data/N265E425.tif").expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(16));

let tiles = decoder.tile_count().unwrap();
assert_eq!(tiles as usize, 100);

for tile in 0..tiles {
match decoder.read_chunk(tile).unwrap() {
DecodingResult::U16(res) => {
let sum: u64 = res.into_iter().map(<u64>::from).sum();
if tile == 0 {
assert_eq!(sum, 173214606);
}
}
_ => panic!("Wrong bit depth"),
}
}
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("png full", |b| b.iter(|| decode_full_png()));
c.bench_function("dtm full", |b| b.iter(|| decode_full_dtm()));
c.bench_function("tif full", |b| b.iter(|| decode_full_tif()));
c.bench_function("tif tiles", |b| b.iter(|| decode_tif_tiles()));
}

criterion_group! {
name = benches;
config = Criterion::default().sample_size(10);
targets = criterion_benchmark
}
criterion_main!(benches);
1 change: 1 addition & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
N265E425*
45 changes: 45 additions & 0 deletions data/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
all: N265E425.tif N265E425.dtm

# EU-DEM tile for testing

N265E425.zip:
wget http://www.openmaps.online/eudem_download/N265E425.zip

N265E425/N265E425.tif: N265E425.zip
unzip $<
touch $@

N265E425.tif: N265E425/N265E425.tif
gdal_translate $< $@ -of COG -co COMPRESS=Deflate -ot UInt16
# gdalinfo N265E425.tif
#
# Driver: GTiff/GeoTIFF
# Files: N265E425.tif
# Size is 5000, 5000
# Coordinate System is:
# PROJCRS["ETRS89_ETRS_LAEA",
# ...
# Data axis to CRS axis mapping: 1,2
# Origin = (4250000.000000000000000,2700000.000000000000000)
# Pixel Size = (10.000000000000000,-10.000000000000000)
# Metadata:
# AREA_OR_POINT=Area
# Image Structure Metadata:
# COMPRESSION=DEFLATE
# INTERLEAVE=BAND
# LAYOUT=COG
# Corner Coordinates:
# Upper Left ( 4250000.000, 2700000.000) ( 9d 3'35.67"E, 47d24'34.38"N)
# Lower Left ( 4250000.000, 2650000.000) ( 9d 4' 4.68"E, 46d57'34.17"N)
# Upper Right ( 4300000.000, 2700000.000) ( 9d43'18.96"E, 47d24'47.70"N)
# Lower Right ( 4300000.000, 2650000.000) ( 9d43'27.54"E, 46d57'47.36"N)
# Center ( 4275000.000, 2675000.000) ( 9d23'36.73"E, 47d11'12.75"N)
# Band 1 Block=512x512 Type=UInt16, ColorInterp=Gray
# NoData Value=0
# Overviews: 2500x2500, 1250x1250, 625x625, 312x312

N265E425.png: N265E425.tif
gdal_translate $< $@ -co ZLEVEL=1

N265E425.dtm: N265E425.png
cargo run --release --example convert $<
29 changes: 29 additions & 0 deletions examples/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use dtm::DTM;
use std::{env, path::Path, process, time::Instant};

fn main() {
let args: Vec<_> = env::args().collect();
if args.len() != 2 {
println!("Usage: convert <in.png>");
process::exit(2);
}

let filein = &args[1];
let source = image::open(filein).unwrap();

let descriptor1 = DTM {
pixel_size: 2, // TODO: depends on input file
channel_count: 1,
width: source.width() as usize,
height: source.height() as usize,
};
let data1 = source.as_luma_alpha16().unwrap().to_vec();

let fileout = Path::new(filein).with_extension("dtm");

let start = Instant::now();
descriptor1.encode_file(&fileout, &data1).unwrap();
let encode = start.elapsed();

println!("Encoded in: {:?}", encode);
}