|
| 1 | +extern crate tiff; |
| 2 | + |
| 3 | +use tiff::decoder::{Decoder, DecodingResult}; |
| 4 | +use tiff::ColorType; |
| 5 | + |
| 6 | +use std::fs::File; |
| 7 | +use std::path::PathBuf; |
| 8 | + |
| 9 | +const TEST_IMAGE_DIR: &str = "./tests/images/"; |
| 10 | + |
| 11 | +/// Test a basic all white image |
| 12 | +#[test] |
| 13 | +fn test_white_ieee_fp16() { |
| 14 | + let filenames = ["white-fp16.tiff"]; |
| 15 | + |
| 16 | + for filename in filenames.iter() { |
| 17 | + let path = PathBuf::from(TEST_IMAGE_DIR).join(filename); |
| 18 | + let img_file = File::open(path).expect("Cannot find test image!"); |
| 19 | + let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 20 | + assert_eq!( |
| 21 | + decoder.dimensions().expect("Cannot get dimensions"), |
| 22 | + (256, 256) |
| 23 | + ); |
| 24 | + assert_eq!( |
| 25 | + decoder.colortype().expect("Cannot get colortype"), |
| 26 | + ColorType::Gray(16) |
| 27 | + ); |
| 28 | + if let DecodingResult::F16(img) = decoder.read_image().unwrap() { |
| 29 | + for p in img { |
| 30 | + assert!(p == half::f16::from_f32_const(1.0)); |
| 31 | + } |
| 32 | + } else { |
| 33 | + panic!("Wrong data type"); |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// Test a single black pixel, to make sure scaling is ok |
| 39 | +#[test] |
| 40 | +fn test_one_black_pixel_ieee_fp16() { |
| 41 | + let filenames = ["single-black-fp16.tiff"]; |
| 42 | + |
| 43 | + for filename in filenames.iter() { |
| 44 | + let path = PathBuf::from(TEST_IMAGE_DIR).join(filename); |
| 45 | + let img_file = File::open(path).expect("Cannot find test image!"); |
| 46 | + let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 47 | + assert_eq!( |
| 48 | + decoder.dimensions().expect("Cannot get dimensions"), |
| 49 | + (256, 256) |
| 50 | + ); |
| 51 | + assert_eq!( |
| 52 | + decoder.colortype().expect("Cannot get colortype"), |
| 53 | + ColorType::Gray(16) |
| 54 | + ); |
| 55 | + if let DecodingResult::F16(img) = decoder.read_image().unwrap() { |
| 56 | + for (i, p) in img.iter().enumerate() { |
| 57 | + if i == 0 { |
| 58 | + assert!(p < &half::f16::from_f32_const(0.001)); |
| 59 | + } else { |
| 60 | + assert!(p == &half::f16::from_f32_const(1.0)); |
| 61 | + } |
| 62 | + } |
| 63 | + } else { |
| 64 | + panic!("Wrong data type"); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Test white with horizontal differencing predictor |
| 70 | +#[test] |
| 71 | +fn test_pattern_horizontal_differencing_ieee_fp16() { |
| 72 | + let filenames = ["white-fp16-pred2.tiff"]; |
| 73 | + |
| 74 | + for filename in filenames.iter() { |
| 75 | + let path = PathBuf::from(TEST_IMAGE_DIR).join(filename); |
| 76 | + let img_file = File::open(path).expect("Cannot find test image!"); |
| 77 | + let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 78 | + assert_eq!( |
| 79 | + decoder.dimensions().expect("Cannot get dimensions"), |
| 80 | + (256, 256) |
| 81 | + ); |
| 82 | + assert_eq!( |
| 83 | + decoder.colortype().expect("Cannot get colortype"), |
| 84 | + ColorType::Gray(16) |
| 85 | + ); |
| 86 | + if let DecodingResult::F16(img) = decoder.read_image().unwrap() { |
| 87 | + // 0, 2, 5, 8, 12, 16, 255 are black |
| 88 | + let black = [0, 2, 5, 8, 12, 16, 255]; |
| 89 | + for (i, p) in img.iter().enumerate() { |
| 90 | + if black.contains(&i) { |
| 91 | + assert!(p < &half::f16::from_f32_const(0.001)); |
| 92 | + } else { |
| 93 | + assert!(p == &half::f16::from_f32_const(1.0)); |
| 94 | + } |
| 95 | + } |
| 96 | + } else { |
| 97 | + panic!("Wrong data type"); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// Test white with floating point predictor |
| 103 | +#[test] |
| 104 | +fn test_pattern_predictor_ieee_fp16() { |
| 105 | + let filenames = ["white-fp16-pred3.tiff"]; |
| 106 | + |
| 107 | + for filename in filenames.iter() { |
| 108 | + let path = PathBuf::from(TEST_IMAGE_DIR).join(filename); |
| 109 | + let img_file = File::open(path).expect("Cannot find test image!"); |
| 110 | + let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 111 | + assert_eq!( |
| 112 | + decoder.dimensions().expect("Cannot get dimensions"), |
| 113 | + (256, 256) |
| 114 | + ); |
| 115 | + assert_eq!( |
| 116 | + decoder.colortype().expect("Cannot get colortype"), |
| 117 | + ColorType::Gray(16) |
| 118 | + ); |
| 119 | + if let DecodingResult::F16(img) = decoder.read_image().unwrap() { |
| 120 | + // 0, 2, 5, 8, 12, 16, 255 are black |
| 121 | + let black = [0, 2, 5, 8, 12, 16, 255]; |
| 122 | + for (i, p) in img.iter().enumerate() { |
| 123 | + if black.contains(&i) { |
| 124 | + assert!(p < &half::f16::from_f32_const(0.001)); |
| 125 | + } else { |
| 126 | + assert!(p == &half::f16::from_f32_const(1.0)); |
| 127 | + } |
| 128 | + } |
| 129 | + } else { |
| 130 | + panic!("Wrong data type"); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/// Test several random images |
| 136 | +/// we'rell compare against a pnm file, that scales from 0 (for 0.0) to 65767 (for 1.0) |
| 137 | +#[test] |
| 138 | +fn test_predictor_ieee_fp16() { |
| 139 | + // first parse pnm, skip the first 4 \n |
| 140 | + let pnm_path = PathBuf::from(TEST_IMAGE_DIR).join("random-fp16.pgm"); |
| 141 | + let pnm_bytes = std::fs::read(pnm_path).expect("Failed to read expected PNM file"); |
| 142 | + |
| 143 | + // PGM looks like this: |
| 144 | + // --- |
| 145 | + // P5 |
| 146 | + // #Created with GIMP |
| 147 | + // 16 16 |
| 148 | + // 65535 |
| 149 | + // ... <big-endian bytes> |
| 150 | + // --- |
| 151 | + // get index of 4th \n |
| 152 | + let byte_start = pnm_bytes |
| 153 | + .iter() |
| 154 | + .enumerate() |
| 155 | + .filter(|(_, &v)| v == b'\n') |
| 156 | + .map(|(i, _)| i) |
| 157 | + .nth(3) |
| 158 | + .expect("Must be 4 \\n's"); |
| 159 | + |
| 160 | + let pnm_values: Vec<f32> = pnm_bytes[(byte_start + 1)..] |
| 161 | + .chunks(2) |
| 162 | + .map(|slice| { |
| 163 | + let bts = [slice[0], slice[1]]; |
| 164 | + (u16::from_be_bytes(bts) as f32) / (u16::MAX as f32) |
| 165 | + }) |
| 166 | + .collect(); |
| 167 | + assert!(pnm_values.len() == 256); |
| 168 | + |
| 169 | + let filenames = [ |
| 170 | + "random-fp16-pred2.tiff", |
| 171 | + "random-fp16-pred3.tiff", |
| 172 | + "random-fp16.tiff", |
| 173 | + ]; |
| 174 | + |
| 175 | + for filename in filenames.iter() { |
| 176 | + let path = PathBuf::from(TEST_IMAGE_DIR).join(filename); |
| 177 | + let img_file = File::open(path).expect("Cannot find test image!"); |
| 178 | + let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 179 | + assert_eq!( |
| 180 | + decoder.dimensions().expect("Cannot get dimensions"), |
| 181 | + (16, 16) |
| 182 | + ); |
| 183 | + assert_eq!( |
| 184 | + decoder.colortype().expect("Cannot get colortype"), |
| 185 | + ColorType::Gray(16) |
| 186 | + ); |
| 187 | + if let DecodingResult::F16(img) = decoder.read_image().unwrap() { |
| 188 | + for (exp, found) in std::iter::zip(pnm_values.iter(), img.iter()) { |
| 189 | + assert!((exp - found.to_f32()).abs() < 0.0001); |
| 190 | + } |
| 191 | + } else { |
| 192 | + panic!("Wrong data type"); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments