Debian Codegraph @

.cargo/vendor/gif/benches/rgb_frame.rs

raw
1 use std::{fs, io};
2
3 use criterion::{Criterion, Throughput};
4 use gif::{Encoder, Frame, Repeat};
5
6 const DIR: &str = "benches/samples";
7
8 fn main() {
9 let mut c = Criterion::default().configure_from_args();
10 let mut group = c.benchmark_group("rgb_frame");
11
12 let dir = fs::read_dir(DIR).expect("Cant'r read dir:\n{}");
13
14 for path in dir {
15 let path = path.expect("Can't read path:\n{}").path();
16 if path.extension().unwrap() != "png" {
17 continue;
18 }
19
20 let mut reader = {
21 let input = fs::File::open(&path).unwrap();
22 let decoder = png::Decoder::new(io::BufReader::new(input));
23 decoder.read_info().unwrap()
24 };
25
26 let mut buf = vec![0; reader.output_buffer_size().unwrap()];
27 let info = reader.next_frame(&mut buf).unwrap();
28
29 let (w, h, size) = {
30 // could use try_into().unwrap() but probably no need
31 (info.width as u16, info.height as u16, info.buffer_size())
32 };
33
34 //size might have to be adjusted for large images
35 group
36 .sample_size(50)
37 .throughput(Throughput::Bytes(size as u64))
38 .bench_function(
39 path.file_name().unwrap().to_str().unwrap(),
40 |b| match info.color_type {
41 png::ColorType::Rgb => b.iter(|| Frame::from_rgb_speed(w, h, &buf[..size], 30)),
42 png::ColorType::Rgba => {
43 b.iter(|| Frame::from_rgba_speed(w, h, &mut buf[..size], 30))
44 }
45 c => {
46 println!("Image has wrong color type: {c:?}");
47 }
48 },
49 );
50
51 // actually write the image as a singe frame gif... while MSE can be used
52 // for quality check, it might not be as good as visual inspection
53 let mut encoder = {
54 let output = fs::File::create(path.with_extension("gif")).unwrap();
55 Encoder::new(output, w, h, &[]).unwrap()
56 };
57 encoder.set_repeat(Repeat::Finite(0)).unwrap();
58
59 let frame = match info.color_type {
60 png::ColorType::Rgb => Frame::from_rgb(w, h, &buf[..size]),
61 png::ColorType::Rgba => Frame::from_rgba(w, h, &mut buf[..size]),
62 _ => continue,
63 };
64
65 encoder.write_frame(&frame).unwrap();
66 }
67 group.finish();
68 c.final_summary();
69 }