| 1 |
#![feature(slice_split_once)] |
| 2 |
|
| 3 |
use fax::{encoder, slice_bits, slice_reader, BitReader, ByteReader}; |
| 4 |
use fax::{VecWriter, decoder, decoder::pels, BitWriter, Bits, Color}; |
| 5 |
use std::fmt::Debug; |
| 6 |
use std::io::Write; |
| 7 |
use std::fs::{self, File}; |
| 8 |
use std::path::Path; |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn main() { |
| 12 |
let data_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../fax-test"); |
| 13 |
|
| 14 |
let mut fails = vec![]; |
| 15 |
|
| 16 |
for r in data_path.read_dir().unwrap() { |
| 17 |
let e = r.unwrap(); |
| 18 |
let p = e.path(); |
| 19 |
|
| 20 |
let base = data_path.join(p.file_stem().unwrap()); |
| 21 |
let pbm = base.with_extension("pbm"); |
| 22 |
let r = if p.extension().is_some_and(|e| e == "fax") { |
| 23 |
read_pbm(&pbm).test_fax(&p) |
| 24 |
} else if p.extension().is_some_and(|e| e == "tiff") { |
| 25 |
read_pbm(&pbm).test_tiff(&p) |
| 26 |
} else { |
| 27 |
continue; |
| 28 |
}; |
| 29 |
println!("{base:?} {r:?}"); |
| 30 |
if r.is_err() { |
| 31 |
fails.push(p); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
if fails.len() > 0 { |
| 36 |
println!("failures: {fails:?}"); |
| 37 |
//panic!(""); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
struct TestImage { |
| 42 |
width: u16, |
| 43 |
height: u16, |
| 44 |
data: Vec<u8>, |
| 45 |
} |
| 46 |
fn read_pbm(path: &Path) -> TestImage { |
| 47 |
let ref_data = std::fs::read(path).unwrap(); |
| 48 |
let (header1, data) = ref_data.split_once(|&b| b == b'\n').unwrap(); |
| 49 |
assert_eq!(header1, b"P4"); |
| 50 |
let (header2, ref_image) = data.split_once(|&b| b == b'\n').unwrap(); |
| 51 |
let header2 = std::str::from_utf8(header2).unwrap(); |
| 52 |
let (w, h) = header2.split_once(" ").unwrap(); |
| 53 |
let width: u16 = w.parse().unwrap(); |
| 54 |
let h: u16 = h.parse().unwrap(); |
| 55 |
|
| 56 |
TestImage { width, height: h, data: ref_image.to_vec() } |
| 57 |
} |
| 58 |
impl TestImage { |
| 59 |
fn test_fax(&self, fax_path: &Path) -> Result<(), ()> { |
| 60 |
let data = fs::read(fax_path).unwrap(); |
| 61 |
self.test_stream(&data, false) |
| 62 |
} |
| 63 |
|
| 64 |
fn test_tiff(&self, path: &Path) -> Result<(), ()> { |
| 65 |
use tiff::{decoder::Decoder, tags::Tag}; |
| 66 |
let data = std::fs::read(path).unwrap(); |
| 67 |
let reader = std::io::Cursor::new(data.as_slice()); |
| 68 |
let mut decoder = Decoder::new(reader).unwrap(); |
| 69 |
let strip_offset = decoder.get_tag(Tag::StripOffsets).unwrap().into_u32().unwrap() as usize; |
| 70 |
let strip_bytes = decoder.get_tag(Tag::StripByteCounts).unwrap().into_u32().unwrap() as usize; |
| 71 |
decoder.goto_offset_u64(strip_offset as _).unwrap(); |
| 72 |
|
| 73 |
let white_is_1 = decoder.get_tag(Tag::PhotometricInterpretation).unwrap().into_u16().unwrap() != 0; |
| 74 |
|
| 75 |
let data = &data[strip_offset .. strip_offset + strip_bytes]; |
| 76 |
self.test_stream(&data, white_is_1) |
| 77 |
} |
| 78 |
|
| 79 |
fn test_stream(&self, data: &[u8], white_is_1: bool) -> Result<(), ()> { |
| 80 |
let mut ref_lines = self.data.chunks_exact((self.width as usize + 7) / 8).take(self.height as _); |
| 81 |
|
| 82 |
let (black, white) = match white_is_1 { |
| 83 |
false => (Bits { data: 1, len: 1 }, Bits { data: 0, len: 1 }), |
| 84 |
true => (Bits { data: 0, len: 1 }, Bits { data: 1, len: 1 }) |
| 85 |
}; |
| 86 |
|
| 87 |
let mut height = 0; |
| 88 |
let mut errors = 0; |
| 89 |
let ok = decoder::decode_g4(data.iter().cloned(), self.width, None, |transitions| { |
| 90 |
//println!("{}", transitions.len()); |
| 91 |
let mut writer = VecWriter::new(); |
| 92 |
for c in pels(transitions, self.width) { |
| 93 |
let bit = match c { |
| 94 |
Color::Black => black, |
| 95 |
Color::White => white |
| 96 |
}; |
| 97 |
writer.write(bit).unwrap(); |
| 98 |
} |
| 99 |
writer.pad(); |
| 100 |
let data = writer.finish(); |
| 101 |
let ref_line = ref_lines.next().unwrap(); |
| 102 |
if ref_line != data { |
| 103 |
println!("line {height} mismatch"); |
| 104 |
errors += 1; |
| 105 |
} |
| 106 |
height += 1; |
| 107 |
}).is_some(); |
| 108 |
|
| 109 |
if errors > 0 { |
| 110 |
println!("{} errors", errors); |
| 111 |
if height == self.height { |
| 112 |
return Ok(()); |
| 113 |
} |
| 114 |
return Err(()); |
| 115 |
} |
| 116 |
if !ok { |
| 117 |
println!("not ok"); |
| 118 |
return Err(()); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
fn pixels(line: &[u8], white_is_1: bool) -> impl Iterator<Item=Color> + '_ { |
| 123 |
slice_bits(line).map(move |b| if b ^ white_is_1 { Color::Black } else { Color::White }) |
| 124 |
} |
| 125 |
let mut expected = slice_reader(data); |
| 126 |
let mut encoder = encoder::Encoder::new(TestWriter { expected: &mut expected, offset: 0 }); |
| 127 |
let ref_lines = self.data.chunks_exact((self.width as usize + 7) / 8).take(self.height as _); |
| 128 |
let mut fail = false; |
| 129 |
for (i, line) in ref_lines.enumerate() { |
| 130 |
println!("line {i}"); |
| 131 |
if encoder.encode_line(pixels(line, white_is_1), self.width).is_err() { |
| 132 |
println!("fail at line {i} of {}", self.height); |
| 133 |
fail = true; |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
dbg!(fax::maps::mode::decode(&mut expected)); |
| 139 |
|
| 140 |
if fail { |
| 141 |
return Err(()); |
| 142 |
} |
| 143 |
Ok(()) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
struct TestWriter<'a, R> { |
| 148 |
offset: usize, |
| 149 |
expected: &'a mut ByteReader<R>, |
| 150 |
} |
| 151 |
impl<'a, E: Debug, R: Iterator<Item=Result<u8, E>>> BitWriter for TestWriter<'a, R> { |
| 152 |
type Error = (usize, u8); |
| 153 |
fn write(&mut self, bits: Bits) -> Result<(), Self::Error> { |
| 154 |
match self.expected.expect(bits) { |
| 155 |
Ok(()) => { |
| 156 |
self.expected.consume(bits.len).unwrap(); |
| 157 |
} |
| 158 |
Err(_) => { |
| 159 |
self.expected.print_peek(); |
| 160 |
println!(" @{}+{} found {}", self.offset/8, self.offset%8, bits); |
| 161 |
return Err((self.offset / 8, (self.offset % 8) as u8)); |
| 162 |
}, |
| 163 |
} |
| 164 |
self.offset += bits.len as usize; |
| 165 |
Ok(()) |
| 166 |
} |
| 167 |
} |