diff --git a/src/encoder/colortype.rs b/src/encoder/colortype.rs new file mode 100644 index 0000000..92beaaf --- /dev/null +++ b/src/encoder/colortype.rs @@ -0,0 +1,68 @@ +pub trait ColorType { + type Inner: super::TiffValue; + const TIFF_VALUE: u8; + fn bits_per_sample() -> Vec; +} + +pub struct Grey8; +impl ColorType for Grey8 { + type Inner = u8; + const TIFF_VALUE: u8 = 1; + fn bits_per_sample() -> Vec { + vec![8] + } +} + +pub struct Grey16; +impl ColorType for Grey16 { + type Inner = u16; + const TIFF_VALUE: u8 = 1; + fn bits_per_sample() -> Vec { + vec![16] + } +} + +pub struct RGB8; +impl ColorType for RGB8 { + type Inner = u8; + const TIFF_VALUE: u8 = 2; + fn bits_per_sample() -> Vec { + vec![8,8,8] + } +} + +pub struct RGB16; +impl ColorType for RGB16 { + type Inner = u16; + const TIFF_VALUE: u8 = 2; + fn bits_per_sample() -> Vec { + vec![16,16,16] + } +} + +pub struct RGBA8; +impl ColorType for RGBA8 { + type Inner = u8; + const TIFF_VALUE: u8 = 2; + fn bits_per_sample() -> Vec { + vec![8,8,8,8] + } +} + +pub struct RGBA16; +impl ColorType for RGBA16 { + type Inner = u16; + const TIFF_VALUE: u8 = 2; + fn bits_per_sample() -> Vec { + vec![16,16,16,16] + } +} + +pub struct CMYK8; +impl ColorType for CMYK8 { + type Inner = u8; + const TIFF_VALUE: u8 = 5; + fn bits_per_sample() -> Vec { + vec![8,8,8,8] + } +} diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index 95d8a37..a30c81c 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -6,7 +6,10 @@ use crate::decoder::ifd; use crate::error::{TiffResult, TiffFormatError, TiffError}; mod writer; +pub mod colortype; + use self::writer::*; +use self::colortype::*; pub struct Rational { pub n: u32, @@ -117,29 +120,6 @@ impl TiffValue for str { } -pub trait ColorType { - type Inner: TiffValue; - const TIFF_VALUE: u8; - fn bits_per_sample() -> Vec; -} - -pub struct RGB8; -impl ColorType for RGB8 { - type Inner = u8; - const TIFF_VALUE: u8 = 2; - fn bits_per_sample() -> Vec { - vec![8,8,8] - } -} - -pub struct RGBA8; -impl ColorType for RGBA8 { - type Inner = u8; - const TIFF_VALUE: u8 = 2; - fn bits_per_sample() -> Vec { - vec![8,8,8,8] - } -} pub struct TiffEncoder { diff --git a/tests/encode_images.rs b/tests/encode_images.rs index 8a6a098..22965b3 100644 --- a/tests/encode_images.rs +++ b/tests/encode_images.rs @@ -3,9 +3,10 @@ extern crate tempfile; use tiff::ColorType; use tiff::decoder::{Decoder, DecodingResult}; -use tiff::encoder::{TiffEncoder, RGB8}; +use tiff::encoder::{colortype, TiffEncoder}; use std::io::{Seek, SeekFrom}; +use std::fs::File; #[test] fn encode_decode() { @@ -22,7 +23,7 @@ fn encode_decode() { { let mut tiff = TiffEncoder::new(&mut file).unwrap(); - tiff.write_image::(100, 100, &image_data).unwrap(); + tiff.write_image::(100, 100, &image_data).unwrap(); } { file.seek(SeekFrom::Start(0)).unwrap(); @@ -37,3 +38,59 @@ fn encode_decode() { } } } + + +#[test] +fn test_gray_u8_roundtrip() +{ + let img_file = File::open("./tests/images/minisblack-1c-8b.tiff").expect("Cannot find test image!"); + let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); + assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(8)); + let img_res = decoder.read_image(); + assert!(img_res.is_ok()); +} + +#[test] +fn test_rgb_u8() +{ + let img_file = File::open("./tests/images/rgb-3c-8b.tiff").expect("Cannot find test image!"); + let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); + assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8)); + let img_res = decoder.read_image(); + + let mut file = tempfile::tempfile().unwrap(); + let mut tiff = TiffEncoder::new(&mut file).unwrap(); + + tiff.write_image::(100, 100, &image_data).unwrap(); + file.seek(SeekFrom::Start(0)).unwrap(); + + let mut decoder = Decoder::new(&mut file).unwrap(); + assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8)); + assert_eq!(decoder.dimensions().unwrap(), (100, 100)); + if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() { + assert_eq!(image_data, img_res); + } + else { + panic!("Wrong data type"); + } +} + +#[test] +fn test_gray_u16() +{ + let img_file = File::open("./tests/images/minisblack-1c-16b.tiff").expect("Cannot find test image!"); + let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); + assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(16)); + let img_res = decoder.read_image(); + assert!(img_res.is_ok()); +} + +#[test] +fn test_rgb_u16() +{ + let img_file = File::open("./tests/images/rgb-3c-16b.tiff").expect("Cannot find test image!"); + let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); + assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(16)); + let img_res = decoder.read_image(); + assert!(img_res.is_ok()); +}