Add more colortypes
This commit is contained in:
1 parent
503c4d29ed
commit
fa540dbad6
3 files changed
+130
-25
No files matched your search
@@ -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
@@ -6,7 +6,10 @@ use crate::decoder::ifd;
|
|||||||
use crate::error::{TiffResult, TiffFormatError, TiffError};
|
use crate::error::{TiffResult, TiffFormatError, TiffError};
|
||||||
|
|
||||||
mod writer;
|
mod writer;
|
||||||
|
pub mod colortype;
|
||||||
|
|
||||||
use self::writer::*;
|
use self::writer::*;
|
||||||
|
use self::colortype::*;
|
||||||
|
|
||||||
pub struct Rational {
|
pub struct Rational {
|
||||||
pub n: u32,
|
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> {
|
pub struct TiffEncoder<W> {
|
||||||
|
|||||||
+59
-2
@@ -3,9 +3,10 @@ extern crate tempfile;
|
|||||||
|
|
||||||
use tiff::ColorType;
|
use tiff::ColorType;
|
||||||
use tiff::decoder::{Decoder, DecodingResult};
|
use tiff::decoder::{Decoder, DecodingResult};
|
||||||
use tiff::encoder::{TiffEncoder, RGB8};
|
use tiff::encoder::{colortype, TiffEncoder};
|
||||||
|
|
||||||
use std::io::{Seek, SeekFrom};
|
use std::io::{Seek, SeekFrom};
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn encode_decode() {
|
fn encode_decode() {
|
||||||
@@ -22,7 +23,7 @@ fn encode_decode() {
|
|||||||
{
|
{
|
||||||
let mut tiff = TiffEncoder::new(&mut file).unwrap();
|
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();
|
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());
|
||||||
|
}
|
||||||
Reference in new issue
Block a user