| 1 | extern crate tiff; |
| 2 | |
| 3 | use std::io::{Cursor, Seek, Write}; |
| 4 | use tiff::{ |
| 5 | decoder::{Decoder, DecodingResult}, |
| 6 | encoder::{ |
| 7 | colortype::{self, ColorType}, |
| 8 | compression::*, |
| 9 | Compression, TiffEncoder, TiffValue, |
| 10 | }, |
| 11 | }; |
| 12 | |
| 13 | trait TestImage<const NUM_CHANNELS: usize>: From<Vec<<Self::Color as ColorType>::Inner>> { |
| 14 | const WIDTH: u32; |
| 15 | const HEIGHT: u32; |
| 16 | type Color: ColorType; |
| 17 | |
| 18 | fn reference_data(&self) -> &[<Self::Color as ColorType>::Inner]; |
| 19 | fn generate_pixel(x: u32, y: u32) -> [<Self::Color as ColorType>::Inner; NUM_CHANNELS]; |
| 20 | |
| 21 | fn compress<W: Write + Seek>(&self, encoder: &mut TiffEncoder<W>) |
| 22 | where |
| 23 | [<Self::Color as ColorType>::Inner]: TiffValue, |
| 24 | { |
| 25 | let image = encoder |
| 26 | .new_image::<Self::Color>(Self::WIDTH, Self::HEIGHT) |
| 27 | .unwrap(); |
| 28 | image.write_data(self.reference_data()).unwrap(); |
| 29 | } |
| 30 | |
| 31 | fn generate() -> Self { |
| 32 | assert_eq!( |
| 33 | Self::Color::BITS_PER_SAMPLE.len(), |
| 34 | NUM_CHANNELS, |
| 35 | "Incompatible color type" |
| 36 | ); |
| 37 | |
| 38 | let mut data = Vec::with_capacity((Self::WIDTH * Self::HEIGHT) as usize * NUM_CHANNELS); |
| 39 | for x in 0..Self::WIDTH { |
| 40 | for y in 0..Self::HEIGHT { |
| 41 | data.extend(IntoIterator::into_iter(Self::generate_pixel(x, y))); |
| 42 | } |
| 43 | } |
| 44 | Self::from(data) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | struct TestImageColor(Vec<u16>); |
| 49 | |
| 50 | impl From<Vec<u16>> for TestImageColor { |
| 51 | fn from(value: Vec<u16>) -> Self { |
| 52 | Self(value) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl TestImage<3> for TestImageColor { |
| 57 | const WIDTH: u32 = 1; |
| 58 | const HEIGHT: u32 = 7; |
| 59 | type Color = colortype::RGB16; |
| 60 | |
| 61 | fn reference_data(&self) -> &[u16] { |
| 62 | &self.0 |
| 63 | } |
| 64 | |
| 65 | fn generate_pixel(x: u32, y: u32) -> [<Self::Color as ColorType>::Inner; 3] { |
| 66 | let val = (x + y) % <Self::Color as ColorType>::Inner::MAX as u32; |
| 67 | [val as <Self::Color as ColorType>::Inner; 3] |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | struct TestImageGrayscale(Vec<u8>); |
| 72 | |
| 73 | impl From<Vec<u8>> for TestImageGrayscale { |
| 74 | fn from(value: Vec<u8>) -> Self { |
| 75 | Self(value) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl TestImage<1> for TestImageGrayscale { |
| 80 | const WIDTH: u32 = 21; |
| 81 | const HEIGHT: u32 = 10; |
| 82 | type Color = colortype::Gray8; |
| 83 | |
| 84 | fn reference_data(&self) -> &[u8] { |
| 85 | &self.0 |
| 86 | } |
| 87 | |
| 88 | fn generate_pixel(x: u32, y: u32) -> [<Self::Color as ColorType>::Inner; 1] { |
| 89 | let val = (x + y) % <Self::Color as ColorType>::Inner::MAX as u32; |
| 90 | [val as <Self::Color as ColorType>::Inner] |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | fn encode_decode_with_compression(compression: Compression) { |
| 95 | let mut data = Cursor::new(Vec::new()); |
| 96 | |
| 97 | let image_rgb = TestImageColor::generate(); |
| 98 | let image_grayscale = TestImageGrayscale::generate(); |
| 99 | |
| 100 | // Encode tiff with compression |
| 101 | { |
| 102 | // Create a multipage image with 2 images |
| 103 | let mut encoder = TiffEncoder::new(&mut data) |
| 104 | .unwrap() |
| 105 | .with_compression(compression); |
| 106 | image_rgb.compress(&mut encoder); |
| 107 | image_grayscale.compress(&mut encoder); |
| 108 | } |
| 109 | |
| 110 | // Decode tiff |
| 111 | data.set_position(0); |
| 112 | { |
| 113 | let mut decoder = Decoder::new(data).unwrap(); |
| 114 | |
| 115 | // Check the RGB image |
| 116 | assert_eq!( |
| 117 | match decoder.read_image() { |
| 118 | Ok(DecodingResult::U16(image_data)) => image_data, |
| 119 | unexpected => panic!("Descoding RGB failed: {unexpected:?}"), |
| 120 | }, |
| 121 | image_rgb.reference_data() |
| 122 | ); |
| 123 | |
| 124 | // Check the grayscale image |
| 125 | decoder.next_image().unwrap(); |
| 126 | assert_eq!( |
| 127 | match decoder.read_image() { |
| 128 | Ok(DecodingResult::U8(image_data)) => image_data, |
| 129 | unexpected => panic!("Decoding grayscale failed: {unexpected:?}"), |
| 130 | }, |
| 131 | image_grayscale.reference_data() |
| 132 | ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn encode_decode_without_compression() { |
| 138 | encode_decode_with_compression(Compression::Uncompressed); |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | #[cfg(feature = "lzw")] |
| 143 | fn encode_decode_with_lzw() { |
| 144 | encode_decode_with_compression(Compression::Lzw); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | #[cfg(feature = "deflate")] |
| 149 | fn encode_decode_with_deflate() { |
| 150 | encode_decode_with_compression(Compression::Deflate(DeflateLevel::Fast)); |
| 151 | encode_decode_with_compression(Compression::Deflate(DeflateLevel::Balanced)); |
| 152 | encode_decode_with_compression(Compression::Deflate(DeflateLevel::Best)); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn encode_decode_with_packbits() { |
| 157 | encode_decode_with_compression(Compression::Packbits); |
| 158 | } |