| 1 |
#![allow(non_upper_case_globals)] |
| 2 |
|
| 3 |
use std::fs::File; |
| 4 |
use std::io; |
| 5 |
use std::io::prelude::*; |
| 6 |
use std::path::{Path, PathBuf}; |
| 7 |
|
| 8 |
use clap::Parser; |
| 9 |
|
| 10 |
use png::chunk; |
| 11 |
|
| 12 |
#[derive(Parser)] |
| 13 |
#[command(about, version)] |
| 14 |
struct Config { |
| 15 |
/// test quietly (output only errors) |
| 16 |
#[arg(short, long)] |
| 17 |
quiet: bool, |
| 18 |
/// test verbosely (print most chunk data) |
| 19 |
#[arg(short, long)] |
| 20 |
verbose: bool, |
| 21 |
/// print contents of tEXt/zTXt/iTXt chunks (can be used with -q) |
| 22 |
#[arg(short, long)] |
| 23 |
text: bool, |
| 24 |
paths: Vec<PathBuf>, |
| 25 |
} |
| 26 |
|
| 27 |
fn display_interlaced(i: bool) -> &'static str { |
| 28 |
if i { |
| 29 |
"interlaced" |
| 30 |
} else { |
| 31 |
"non-interlaced" |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
fn display_image_type(bits: u8, color: png::ColorType) -> String { |
| 36 |
use png::ColorType::*; |
| 37 |
format!( |
| 38 |
"{}-bit {}", |
| 39 |
bits, |
| 40 |
match color { |
| 41 |
Grayscale => "grayscale", |
| 42 |
Rgb => "RGB", |
| 43 |
Indexed => "palette", |
| 44 |
GrayscaleAlpha => "grayscale+alpha", |
| 45 |
Rgba => "RGB+alpha", |
| 46 |
} |
| 47 |
) |
| 48 |
} |
| 49 |
// channels after expansion of tRNS |
| 50 |
fn final_channels(c: png::ColorType, trns: bool) -> u8 { |
| 51 |
use png::ColorType::*; |
| 52 |
match c { |
| 53 |
Grayscale => 1 + u8::from(trns), |
| 54 |
Rgb => 3, |
| 55 |
Indexed => 3 + u8::from(trns), |
| 56 |
GrayscaleAlpha => 2, |
| 57 |
Rgba => 4, |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
fn check_image<P: AsRef<Path>>(c: &Config, fname: P) -> io::Result<()> { |
| 62 |
// TODO improve performance by reusing allocations from decoder |
| 63 |
use png::Decoded::*; |
| 64 |
let data = &mut vec![0; 10 * 1024][..]; |
| 65 |
let mut reader = io::BufReader::new(File::open(&fname)?); |
| 66 |
let fname = fname.as_ref().to_string_lossy(); |
| 67 |
let n = reader.read(data)?; |
| 68 |
let mut buf = &data[..n]; |
| 69 |
let mut pos = 0; |
| 70 |
let mut decoder = png::StreamingDecoder::new(); |
| 71 |
// Image data |
| 72 |
let mut width = 0; |
| 73 |
let mut height = 0; |
| 74 |
let mut color = png::ColorType::Grayscale; |
| 75 |
let mut bits = 0; |
| 76 |
let mut trns = false; |
| 77 |
let mut interlaced = false; |
| 78 |
let mut compressed_size = 0; |
| 79 |
let mut n_chunks = 0; |
| 80 |
let mut have_idat = false; |
| 81 |
macro_rules! c_ratio( |
| 82 |
// TODO add palette entries to compressed_size |
| 83 |
() => ({ |
| 84 |
compressed_size as f32/( |
| 85 |
height as u64 * |
| 86 |
(width as u64 * final_channels(color, trns) as u64 * bits as u64 + 7)>>3 |
| 87 |
) as f32 |
| 88 |
}); |
| 89 |
); |
| 90 |
let display_error = |err| -> Result<_, io::Error> { |
| 91 |
if c.verbose { |
| 92 |
println!(": {}", err); |
| 93 |
print!("ERRORS DETECTED"); |
| 94 |
println!(" in {}", fname); |
| 95 |
} else { |
| 96 |
if !c.quiet { |
| 97 |
println!("ERROR: {}", fname) |
| 98 |
} |
| 99 |
print!("{}: ", fname); |
| 100 |
println!("{}", err); |
| 101 |
} |
| 102 |
Ok(()) |
| 103 |
}; |
| 104 |
|
| 105 |
if c.verbose { |
| 106 |
print!("File: "); |
| 107 |
print!("{}", fname); |
| 108 |
print!(" ({}) bytes", data.len()) |
| 109 |
} |
| 110 |
loop { |
| 111 |
if buf.is_empty() { |
| 112 |
// circumvent borrow checker |
| 113 |
assert!(!data.is_empty()); |
| 114 |
let n = reader.read(data)?; |
| 115 |
|
| 116 |
// EOF |
| 117 |
if n == 0 { |
| 118 |
println!("ERROR: premature end of file {}", fname); |
| 119 |
break; |
| 120 |
} |
| 121 |
buf = &data[..n]; |
| 122 |
} |
| 123 |
match decoder.update(buf, None) { |
| 124 |
Ok((_, ChunkComplete(chunk::IEND))) => { |
| 125 |
if !have_idat { |
| 126 |
// This isn't beautiful. But it works. |
| 127 |
display_error(png::DecodingError::IoError(io::Error::new( |
| 128 |
io::ErrorKind::InvalidData, |
| 129 |
"IDAT chunk missing", |
| 130 |
)))?; |
| 131 |
break; |
| 132 |
} |
| 133 |
if !c.verbose && !c.quiet { |
| 134 |
print!("OK: {}", fname); |
| 135 |
println!( |
| 136 |
" ({}x{}, {}{}, {}, {:.1}%)", |
| 137 |
width, |
| 138 |
height, |
| 139 |
display_image_type(bits, color), |
| 140 |
(if trns { "+trns" } else { "" }), |
| 141 |
display_interlaced(interlaced), |
| 142 |
100.0 * (1.0 - c_ratio!()) |
| 143 |
) |
| 144 |
} else if !c.quiet { |
| 145 |
println!(); |
| 146 |
print!("No errors detected "); |
| 147 |
println!( |
| 148 |
"in {} ({} chunks, {:.1}% compression)", |
| 149 |
fname, |
| 150 |
n_chunks, |
| 151 |
100.0 * (1.0 - c_ratio!()), |
| 152 |
) |
| 153 |
} |
| 154 |
break; |
| 155 |
} |
| 156 |
Ok((n, res)) => { |
| 157 |
buf = &buf[n..]; |
| 158 |
pos += n; |
| 159 |
match res { |
| 160 |
ChunkBegin(len, type_str) => { |
| 161 |
n_chunks += 1; |
| 162 |
if c.verbose { |
| 163 |
let chunk = type_str; |
| 164 |
println!(); |
| 165 |
print!(" chunk "); |
| 166 |
print!("{:?}", chunk); |
| 167 |
print!( |
| 168 |
" at offset {:#07x}, length {}", |
| 169 |
pos - 4, // subtract chunk name length |
| 170 |
len |
| 171 |
) |
| 172 |
} |
| 173 |
match type_str { |
| 174 |
chunk::IDAT => { |
| 175 |
have_idat = true; |
| 176 |
compressed_size += len |
| 177 |
} |
| 178 |
chunk::tRNS => { |
| 179 |
trns = true; |
| 180 |
} |
| 181 |
_ => (), |
| 182 |
} |
| 183 |
} |
| 184 |
ChunkComplete(chunk::IHDR) => { |
| 185 |
width = decoder.info().unwrap().width; |
| 186 |
height = decoder.info().unwrap().height; |
| 187 |
bits = decoder.info().unwrap().bit_depth as u8; |
| 188 |
color = decoder.info().unwrap().color_type; |
| 189 |
interlaced = decoder.info().unwrap().interlaced; |
| 190 |
|
| 191 |
if c.verbose { |
| 192 |
println!(); |
| 193 |
print!( |
| 194 |
" {} x {} image, {}{}, {}", |
| 195 |
width, |
| 196 |
height, |
| 197 |
display_image_type(bits, color), |
| 198 |
(if trns { "+trns" } else { "" }), |
| 199 |
display_interlaced(interlaced), |
| 200 |
); |
| 201 |
} |
| 202 |
} |
| 203 |
ChunkComplete(chunk::acTL) => { |
| 204 |
let actl = decoder.info().unwrap().animation_control.unwrap(); |
| 205 |
println!(); |
| 206 |
print!(" {} frames, {} plays", actl.num_frames, actl.num_plays,); |
| 207 |
} |
| 208 |
ChunkComplete(chunk::fdAT) => { |
| 209 |
let fctl = decoder.info().unwrap().frame_control.unwrap(); |
| 210 |
println!(); |
| 211 |
println!( |
| 212 |
" sequence #{}, {} x {} pixels @ ({}, {})", |
| 213 |
fctl.sequence_number, |
| 214 |
fctl.width, |
| 215 |
fctl.height, |
| 216 |
fctl.x_offset, |
| 217 |
fctl.y_offset, |
| 218 |
/*fctl.delay_num, |
| 219 |
fctl.delay_den, |
| 220 |
fctl.dispose_op, |
| 221 |
fctl.blend_op,*/ |
| 222 |
); |
| 223 |
print!( |
| 224 |
" {}/{} s delay, dispose: {}, blend: {}", |
| 225 |
fctl.delay_num, |
| 226 |
if fctl.delay_den == 0 { |
| 227 |
100 |
| 228 |
} else { |
| 229 |
fctl.delay_den |
| 230 |
}, |
| 231 |
fctl.dispose_op, |
| 232 |
fctl.blend_op, |
| 233 |
); |
| 234 |
} |
| 235 |
ImageData => { |
| 236 |
//println!("got {} bytes of image data", data.len()) |
| 237 |
} |
| 238 |
_ => (), |
| 239 |
} |
| 240 |
//println!("{} {:?}", n, res) |
| 241 |
} |
| 242 |
Err(err) => { |
| 243 |
let _ = display_error(err); |
| 244 |
break; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
if c.text { |
| 249 |
println!("Parsed tEXt chunks:"); |
| 250 |
for text_chunk in &decoder.info().unwrap().uncompressed_latin1_text { |
| 251 |
println!("{:#?}", text_chunk); |
| 252 |
} |
| 253 |
|
| 254 |
println!("Parsed zTXt chunks:"); |
| 255 |
for text_chunk in &decoder.info().unwrap().compressed_latin1_text { |
| 256 |
let mut cloned_text_chunk = text_chunk.clone(); |
| 257 |
cloned_text_chunk.decompress_text()?; |
| 258 |
println!("{:#?}", cloned_text_chunk); |
| 259 |
} |
| 260 |
|
| 261 |
println!("Parsed iTXt chunks:"); |
| 262 |
for text_chunk in &decoder.info().unwrap().utf8_text { |
| 263 |
let mut cloned_text_chunk = text_chunk.clone(); |
| 264 |
cloned_text_chunk.decompress_text()?; |
| 265 |
println!("{:#?}", cloned_text_chunk); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
Ok(()) |
| 270 |
} |
| 271 |
|
| 272 |
fn main() { |
| 273 |
let config = Config::parse(); |
| 274 |
|
| 275 |
for file in &config.paths { |
| 276 |
let result = if let Some(glob) = file.to_str().filter(|n| n.contains('*')) { |
| 277 |
glob::glob(glob) |
| 278 |
.map_err(|err| io::Error::new(io::ErrorKind::Other, err)) |
| 279 |
.and_then(|mut glob| { |
| 280 |
glob.try_for_each(|entry| { |
| 281 |
entry |
| 282 |
.map_err(|err| io::Error::new(io::ErrorKind::Other, err)) |
| 283 |
.and_then(|file| check_image(&config, file)) |
| 284 |
}) |
| 285 |
}) |
| 286 |
} else { |
| 287 |
check_image(&config, &file) |
| 288 |
}; |
| 289 |
|
| 290 |
result.unwrap_or_else(|err| { |
| 291 |
println!("{}: {}", file.display(), err); |
| 292 |
std::process::exit(1) |
| 293 |
}); |
| 294 |
} |
| 295 |
} |