Simplify tests
This commit is contained in:
1 parent
99b2aa9b3a
commit
bc7be68d0f
1 file changed
+78
-63
@@ -1,88 +1,111 @@
|
||||
extern crate tiff;
|
||||
|
||||
use std::io::Cursor;
|
||||
use std::io::{Cursor, Seek, Write};
|
||||
use tiff::{
|
||||
decoder::{Decoder, DecodingResult},
|
||||
encoder::{colortype, compression::*, TiffEncoder},
|
||||
tags::Tag,
|
||||
encoder::{
|
||||
colortype::{self, ColorType},
|
||||
compression::*,
|
||||
TiffEncoder, TiffValue,
|
||||
},
|
||||
};
|
||||
|
||||
trait TestImage {
|
||||
trait TestImage<const NUM_CHANNELS: usize>: From<Vec<<Self::Color as ColorType>::Inner>> {
|
||||
const WIDTH: u32;
|
||||
const HEIGHT: u32;
|
||||
type PixelType;
|
||||
type Color: ColorType;
|
||||
|
||||
fn generate() -> Vec<Self::PixelType>;
|
||||
}
|
||||
fn reference_data(&self) -> &[<Self::Color as ColorType>::Inner];
|
||||
fn generate_pixel(x: u32, y: u32) -> [<Self::Color as ColorType>::Inner; NUM_CHANNELS];
|
||||
|
||||
struct TestImageColor;
|
||||
impl TestImage for TestImageColor {
|
||||
const WIDTH: u32 = 1;
|
||||
const HEIGHT: u32 = 7;
|
||||
type PixelType = u16;
|
||||
fn compress<C: Compression, W: Write + Seek>(
|
||||
&self,
|
||||
encoder: &mut TiffEncoder<W>,
|
||||
compression: C,
|
||||
) where
|
||||
[<Self::Color as ColorType>::Inner]: TiffValue,
|
||||
{
|
||||
let image = encoder
|
||||
.new_image_with_compression::<Self::Color, C>(Self::WIDTH, Self::HEIGHT, compression)
|
||||
.unwrap();
|
||||
image.write_data(self.reference_data()).unwrap();
|
||||
}
|
||||
|
||||
fn generate() -> Vec<Self::PixelType> {
|
||||
let mut data: Vec<u16> = Vec::with_capacity((Self::WIDTH * Self::HEIGHT) as usize * 3);
|
||||
fn generate() -> Self {
|
||||
assert_eq!(
|
||||
Self::Color::BITS_PER_SAMPLE.len(),
|
||||
NUM_CHANNELS,
|
||||
"Incompatible color type"
|
||||
);
|
||||
|
||||
let mut data = Vec::with_capacity((Self::WIDTH * Self::HEIGHT) as usize * NUM_CHANNELS);
|
||||
for x in 0..Self::WIDTH {
|
||||
for y in 0..Self::HEIGHT {
|
||||
let val = (x + y) % Self::PixelType::MAX as u32;
|
||||
data.extend(std::iter::repeat(val as Self::PixelType).take(3));
|
||||
data.extend(IntoIterator::into_iter(Self::generate_pixel(x, y)));
|
||||
}
|
||||
}
|
||||
assert_eq!(data.len() as u32, Self::WIDTH * Self::HEIGHT * 3);
|
||||
data
|
||||
Self::from(data)
|
||||
}
|
||||
}
|
||||
|
||||
struct TestImageGrayscale;
|
||||
impl TestImage for TestImageGrayscale {
|
||||
struct TestImageColor(Vec<u16>);
|
||||
|
||||
impl From<Vec<u16>> for TestImageColor {
|
||||
fn from(value: Vec<u16>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl TestImage<3> for TestImageColor {
|
||||
const WIDTH: u32 = 1;
|
||||
const HEIGHT: u32 = 7;
|
||||
type Color = colortype::RGB16;
|
||||
|
||||
fn reference_data(&self) -> &[u16] {
|
||||
&self.0
|
||||
}
|
||||
|
||||
fn generate_pixel(x: u32, y: u32) -> [<Self::Color as ColorType>::Inner; 3] {
|
||||
let val = (x + y) % <Self::Color as ColorType>::Inner::MAX as u32;
|
||||
[val as <Self::Color as ColorType>::Inner; 3]
|
||||
}
|
||||
}
|
||||
|
||||
struct TestImageGrayscale(Vec<u8>);
|
||||
|
||||
impl From<Vec<u8>> for TestImageGrayscale {
|
||||
fn from(value: Vec<u8>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl TestImage<1> for TestImageGrayscale {
|
||||
const WIDTH: u32 = 21;
|
||||
const HEIGHT: u32 = 10;
|
||||
type PixelType = u8;
|
||||
type Color = colortype::Gray8;
|
||||
|
||||
fn generate() -> Vec<Self::PixelType> {
|
||||
let mut data: Vec<u8> = Vec::with_capacity((Self::WIDTH * Self::HEIGHT) as usize);
|
||||
for x in 0..Self::WIDTH {
|
||||
for y in 0..Self::HEIGHT {
|
||||
let val = (x + y) % Self::PixelType::MAX as u32;
|
||||
data.push(val as Self::PixelType);
|
||||
}
|
||||
}
|
||||
assert_eq!(data.len() as u32, Self::WIDTH * Self::HEIGHT);
|
||||
data
|
||||
fn reference_data(&self) -> &[u8] {
|
||||
&self.0
|
||||
}
|
||||
|
||||
fn generate_pixel(x: u32, y: u32) -> [<Self::Color as ColorType>::Inner; 1] {
|
||||
let val = (x + y) % <Self::Color as ColorType>::Inner::MAX as u32;
|
||||
[val as <Self::Color as ColorType>::Inner]
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_decode_with_compression<C: Compression + Clone>(compression: C) {
|
||||
let mut data = Cursor::new(Vec::new());
|
||||
|
||||
let image_data_rgb = TestImageColor::generate();
|
||||
let image_data_gray = TestImageGrayscale::generate();
|
||||
let image_rgb = TestImageColor::generate();
|
||||
let image_grayscale = TestImageGrayscale::generate();
|
||||
|
||||
// Encode tiff with compression
|
||||
{
|
||||
// Create a multipage image with 2 images
|
||||
let mut encoder = TiffEncoder::new(&mut data).unwrap();
|
||||
|
||||
// Write first colored image
|
||||
let image_rgb = encoder
|
||||
.new_image_with_compression::<colortype::RGB16, C>(
|
||||
TestImageColor::WIDTH,
|
||||
TestImageColor::HEIGHT,
|
||||
compression.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
image_rgb.write_data(&image_data_rgb).unwrap();
|
||||
|
||||
// Write second grayscale image
|
||||
let image_gray = encoder
|
||||
.new_image_with_compression::<colortype::Gray8, C>(
|
||||
TestImageGrayscale::WIDTH,
|
||||
TestImageGrayscale::HEIGHT,
|
||||
compression,
|
||||
)
|
||||
.unwrap();
|
||||
image_gray.write_data(&image_data_gray).unwrap();
|
||||
image_rgb.compress(&mut encoder, compression.clone());
|
||||
image_grayscale.compress(&mut encoder, compression);
|
||||
}
|
||||
|
||||
// Decode tiff
|
||||
@@ -91,20 +114,12 @@ fn encode_decode_with_compression<C: Compression + Clone>(compression: C) {
|
||||
let mut decoder = Decoder::new(data).unwrap();
|
||||
|
||||
// Check the RGB image
|
||||
assert_eq!(
|
||||
decoder
|
||||
.get_tag(Tag::ImageWidth)
|
||||
.unwrap()
|
||||
.into_u32()
|
||||
.unwrap(),
|
||||
TestImageColor::WIDTH
|
||||
);
|
||||
assert_eq!(
|
||||
match decoder.read_image() {
|
||||
Ok(DecodingResult::U16(image_data)) => image_data,
|
||||
unexpected => panic!("Descoding RGB failed: {:?}", unexpected),
|
||||
},
|
||||
image_data_rgb
|
||||
image_rgb.reference_data()
|
||||
);
|
||||
|
||||
// Check the grayscale image
|
||||
@@ -114,7 +129,7 @@ fn encode_decode_with_compression<C: Compression + Clone>(compression: C) {
|
||||
Ok(DecodingResult::U8(image_data)) => image_data,
|
||||
unexpected => panic!("Decoding grayscale failed: {:?}", unexpected),
|
||||
},
|
||||
image_data_gray
|
||||
image_grayscale.reference_data()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user