Add more colortypes

This commit is contained in:
Birk Tjelmeland committed 2019-02-09 20:53:11 +01:00
1 parent 503c4d29ed
commit fa540dbad6
3 files changed
+130 -25

No files matched your search

+68
View File
@@ -0,0 +1,68 @@
pub trait ColorType {
type Inner: super::TiffValue;
const TIFF_VALUE: u8;
fn bits_per_sample() -> Vec<u16>;
}
pub struct Grey8;
impl ColorType for Grey8 {
type Inner = u8;
const TIFF_VALUE: u8 = 1;
fn bits_per_sample() -> Vec<u16> {
vec![8]
}
}
pub struct Grey16;
impl ColorType for Grey16 {
type Inner = u16;
const TIFF_VALUE: u8 = 1;
fn bits_per_sample() -> Vec<u16> {
vec![16]
}
}
pub struct RGB8;
impl ColorType for RGB8 {
type Inner = u8;
const TIFF_VALUE: u8 = 2;
fn bits_per_sample() -> Vec<u16> {
vec![8,8,8]
}
}
pub struct RGB16;
impl ColorType for RGB16 {
type Inner = u16;
const TIFF_VALUE: u8 = 2;
fn bits_per_sample() -> Vec<u16> {
vec![16,16,16]
}
}
pub struct RGBA8;
impl ColorType for RGBA8 {
type Inner = u8;
const TIFF_VALUE: u8 = 2;
fn bits_per_sample() -> Vec<u16> {
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<u16> {
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<u16> {
vec![8,8,8,8]
}
}
+3 -23
View File
@@ -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<u16>;
}
pub struct RGB8;
impl ColorType for RGB8 {
type Inner = u8;
const TIFF_VALUE: u8 = 2;
fn bits_per_sample() -> Vec<u16> {
vec![8,8,8]
}
}
pub struct RGBA8;
impl ColorType for RGBA8 {
type Inner = u8;
const TIFF_VALUE: u8 = 2;
fn bits_per_sample() -> Vec<u16> {
vec![8,8,8,8]
}
}
pub struct TiffEncoder<W> {
+59 -2
View File
@@ -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::<RGB8>(100, 100, &image_data).unwrap();
tiff.write_image::<colortype::RGB8>(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::<colortype::RGB8>(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());
}