| 1 |
use std::{fs, io::Cursor, path::PathBuf}; |
| 2 |
|
| 3 |
use clap::Parser; |
| 4 |
use png::Decoder; |
| 5 |
|
| 6 |
#[derive(clap::ValueEnum, Clone)] |
| 7 |
enum Speed { |
| 8 |
Fast, |
| 9 |
Default, |
| 10 |
Best, |
| 11 |
} |
| 12 |
|
| 13 |
#[derive(clap::ValueEnum, Clone)] |
| 14 |
enum Filter { |
| 15 |
None, |
| 16 |
Sub, |
| 17 |
Up, |
| 18 |
Average, |
| 19 |
Paeth, |
| 20 |
Adaptive, |
| 21 |
} |
| 22 |
|
| 23 |
#[derive(clap::Parser)] |
| 24 |
struct Args { |
| 25 |
directory: Option<PathBuf>, |
| 26 |
#[clap(short, long, value_enum, default_value_t = Speed::Fast)] |
| 27 |
speed: Speed, |
| 28 |
#[clap(short, long, value_enum, default_value_t = Filter::Adaptive)] |
| 29 |
filter: Filter, |
| 30 |
} |
| 31 |
|
| 32 |
#[inline(never)] |
| 33 |
fn run_encode( |
| 34 |
args: &Args, |
| 35 |
dimensions: (u32, u32), |
| 36 |
color_type: png::ColorType, |
| 37 |
bit_depth: png::BitDepth, |
| 38 |
image: &[u8], |
| 39 |
) -> Vec<u8> { |
| 40 |
let mut reencoded = Vec::new(); |
| 41 |
let mut encoder = png::Encoder::new(&mut reencoded, dimensions.0, dimensions.1); |
| 42 |
encoder.set_color(color_type); |
| 43 |
encoder.set_depth(bit_depth); |
| 44 |
encoder.set_compression(match args.speed { |
| 45 |
Speed::Fast => png::Compression::Fast, |
| 46 |
Speed::Default => png::Compression::Balanced, |
| 47 |
Speed::Best => png::Compression::High, |
| 48 |
}); |
| 49 |
encoder.set_filter(match args.filter { |
| 50 |
Filter::None => png::Filter::NoFilter, |
| 51 |
Filter::Sub => png::Filter::Sub, |
| 52 |
Filter::Up => png::Filter::Up, |
| 53 |
Filter::Average => png::Filter::Avg, |
| 54 |
Filter::Paeth => png::Filter::Paeth, |
| 55 |
Filter::Adaptive => png::Filter::Adaptive, |
| 56 |
}); |
| 57 |
let mut encoder = encoder.write_header().unwrap(); |
| 58 |
encoder.write_image_data(image).unwrap(); |
| 59 |
encoder.finish().unwrap(); |
| 60 |
reencoded |
| 61 |
} |
| 62 |
|
| 63 |
#[inline(never)] |
| 64 |
fn run_decode(image: &[u8], output: &mut [u8]) { |
| 65 |
let mut reader = Decoder::new(Cursor::new(image)).read_info().unwrap(); |
| 66 |
reader.next_frame(output).unwrap(); |
| 67 |
} |
| 68 |
|
| 69 |
fn main() { |
| 70 |
let mut total_uncompressed = 0; |
| 71 |
let mut total_compressed = 0; |
| 72 |
let mut total_pixels = 0; |
| 73 |
let mut total_encode_time = 0; |
| 74 |
let mut total_decode_time = 0; |
| 75 |
|
| 76 |
let args = Args::parse(); |
| 77 |
|
| 78 |
println!( |
| 79 |
"{:45} Ratio Encode Decode", |
| 80 |
"Directory" |
| 81 |
); |
| 82 |
println!( |
| 83 |
"{:45}------- -------------------- --------------------", |
| 84 |
"---------" |
| 85 |
); |
| 86 |
|
| 87 |
let mut image2 = Vec::new(); |
| 88 |
|
| 89 |
let mut pending = vec![args.directory.clone().unwrap_or(PathBuf::from("."))]; |
| 90 |
while let Some(directory) = pending.pop() { |
| 91 |
let mut dir_uncompressed = 0; |
| 92 |
let mut dir_compressed = 0; |
| 93 |
let mut dir_pixels = 0; |
| 94 |
let mut dir_encode_time = 0; |
| 95 |
let mut dir_decode_time = 0; |
| 96 |
|
| 97 |
for entry in fs::read_dir(&directory).unwrap().flatten() { |
| 98 |
if entry.file_type().unwrap().is_dir() { |
| 99 |
pending.push(entry.path()); |
| 100 |
continue; |
| 101 |
} |
| 102 |
|
| 103 |
match entry.path().extension() { |
| 104 |
Some(st) if st == "png" => {} |
| 105 |
_ => continue, |
| 106 |
} |
| 107 |
|
| 108 |
// Parse |
| 109 |
let data = fs::read(entry.path()).unwrap(); |
| 110 |
let mut decoder = Decoder::new(Cursor::new(&*data)); |
| 111 |
if decoder.read_header_info().ok().map(|h| h.color_type) |
| 112 |
== Some(png::ColorType::Indexed) |
| 113 |
{ |
| 114 |
decoder.set_transformations( |
| 115 |
png::Transformations::EXPAND | png::Transformations::STRIP_16, |
| 116 |
); |
| 117 |
} |
| 118 |
let mut reader = match decoder.read_info() { |
| 119 |
Ok(reader) => reader, |
| 120 |
Err(_) => continue, |
| 121 |
}; |
| 122 |
let mut image = vec![0; reader.output_buffer_size().unwrap()]; |
| 123 |
let info = match reader.next_frame(&mut image) { |
| 124 |
Ok(info) => info, |
| 125 |
Err(_) => continue, |
| 126 |
}; |
| 127 |
let (width, height) = (info.width, info.height); |
| 128 |
let bit_depth = info.bit_depth; |
| 129 |
let mut color_type = info.color_type; |
| 130 |
|
| 131 |
// qoibench expands grayscale to RGB, so we do the same. |
| 132 |
if bit_depth == png::BitDepth::Eight { |
| 133 |
if color_type == png::ColorType::Grayscale { |
| 134 |
image = image.into_iter().flat_map(|v| [v, v, v, 255]).collect(); |
| 135 |
color_type = png::ColorType::Rgba; |
| 136 |
} else if color_type == png::ColorType::GrayscaleAlpha { |
| 137 |
image = image |
| 138 |
.chunks_exact(2) |
| 139 |
.flat_map(|v| [v[0], v[0], v[0], v[1]]) |
| 140 |
.collect(); |
| 141 |
color_type = png::ColorType::Rgba; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Re-encode |
| 146 |
let start = std::time::Instant::now(); |
| 147 |
let reencoded = run_encode(&args, (width, height), color_type, bit_depth, &image); |
| 148 |
let elapsed = start.elapsed().as_nanos() as u64; |
| 149 |
|
| 150 |
// And decode again |
| 151 |
image2.resize(image.len(), 0); |
| 152 |
let start2 = std::time::Instant::now(); |
| 153 |
run_decode(&reencoded, &mut image2); |
| 154 |
let elapsed2 = start2.elapsed().as_nanos() as u64; |
| 155 |
|
| 156 |
assert_eq!(image, image2); |
| 157 |
|
| 158 |
// Stats |
| 159 |
dir_uncompressed += image.len(); |
| 160 |
dir_compressed += reencoded.len(); |
| 161 |
dir_pixels += (width * height) as u64; |
| 162 |
dir_encode_time += elapsed; |
| 163 |
dir_decode_time += elapsed2; |
| 164 |
} |
| 165 |
if dir_uncompressed > 0 { |
| 166 |
println!( |
| 167 |
"{:45}{:6.2}%{:8} mps {:6.2} GiB/s {:8} mps {:6.2} GiB/s", |
| 168 |
directory.display(), |
| 169 |
100.0 * dir_compressed as f64 / dir_uncompressed as f64, |
| 170 |
dir_pixels * 1000 / dir_encode_time, |
| 171 |
dir_uncompressed as f64 / (dir_encode_time as f64 * 1e-9 * (1 << 30) as f64), |
| 172 |
dir_pixels * 1000 / dir_decode_time, |
| 173 |
dir_uncompressed as f64 / (dir_decode_time as f64 * 1e-9 * (1 << 30) as f64) |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
total_uncompressed += dir_uncompressed; |
| 178 |
total_compressed += dir_compressed; |
| 179 |
total_pixels += dir_pixels; |
| 180 |
total_encode_time += dir_encode_time; |
| 181 |
total_decode_time += dir_decode_time; |
| 182 |
} |
| 183 |
|
| 184 |
println!(); |
| 185 |
println!( |
| 186 |
"{:44}{:7.3}%{:8} mps {:6.3} GiB/s {:8} mps {:6.3} GiB/s", |
| 187 |
"Total", |
| 188 |
100.0 * total_compressed as f64 / total_uncompressed as f64, |
| 189 |
total_pixels * 1000 / total_encode_time, |
| 190 |
total_uncompressed as f64 / (total_encode_time as f64 * 1e-9 * (1 << 30) as f64), |
| 191 |
total_pixels * 1000 / total_decode_time, |
| 192 |
total_uncompressed as f64 / (total_decode_time as f64 * 1e-9 * (1 << 30) as f64) |
| 193 |
); |
| 194 |
} |