use std::io::{self, Read, Seek};
use std::collections::{HashMap};
use super::stream::{ByteOrder, SmartReader, EndianReader};
use self::Value::{Unsigned, List};
macro_rules! tags {
{$(
$tag:ident
$val:expr;
)*} => {
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum Tag {
$($tag,)*
Unknown(u16)
}
impl Tag {
pub fn from_u16(n: u16) -> Tag {
$(if n == $val { Tag::$tag } else)* {
Tag::Unknown(n)
}
}
}
}
}
tags!{
Artist 315; BitsPerSample 258;
CellLength 265; CellWidth 264; ColorMap 320; Compression 259; Copyright 33432; DateTime 306; ExtraSamples 338; FillOrder 266; FreeByteCounts 289; FreeOffsets 288; GrayResponseCurve 291; GrayResponseUnit 290; HostComputer 316; ImageDescription 270; ImageLength 257;
ImageWidth 256;
Make 271; MaxSampleValue 281; MinSampleValue 280; Model 272; NewSubfileType 254; Orientation 274; PhotometricInterpretation 262;
PlanarConfiguration 284;
ResolutionUnit 296; RowsPerStrip 278;
SamplesPerPixel 277;
Software 305;
StripByteCounts 279;
StripOffsets 273;
SubfileType 255; Threshholding 263; XResolution 282;
YResolution 283;
Predictor 317;
}
enum_from_primitive! {
#[derive(Clone, Copy, Debug)]
pub enum Type {
BYTE = 1,
ASCII = 2,
SHORT = 3,
LONG = 4,
RATIONAL = 5,
}
}
#[allow(unused_qualifications)]
#[derive(Debug)]
pub enum Value {
Unsigned(u32),
List(Vec<Value>)
}
impl Value {
pub fn as_u32(self) -> ::image::ImageResult<u32> {
match self {
Unsigned(val) => Ok(val),
val => Err(::image::ImageError::FormatError(format!(
"Expected unsigned integer, {:?} found.", val
)))
}
}
pub fn as_u32_vec(self) -> ::image::ImageResult<Vec<u32>> {
match self {
List(vec) => {
let mut new_vec = Vec::with_capacity(vec.len());
for v in vec.into_iter() {
new_vec.push(try!(v.as_u32()))
}
Ok(new_vec)
},
Unsigned(val) => Ok(vec![val]),
}
}
}
pub struct Entry {
type_: Type,
count: u32,
offset: [u8; 4],
}
impl ::std::fmt::Debug for Entry {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
fmt.write_str(&format!("Entry {{ type_: {:?}, count: {:?}, offset: {:?} }}",
self.type_,
self.count,
&self.offset
))
}
}
impl Entry {
pub fn new(type_: Type, count: u32, offset: [u8; 4]) -> Entry {
Entry {
type_: type_,
count: count,
offset: offset
}
}
fn r(&self, byte_order: ByteOrder) -> SmartReader<io::Cursor<Vec<u8>>> {
SmartReader::wrap(
io::Cursor::new(self.offset.to_vec()),
byte_order
)
}
pub fn val<R: Read + Seek>(&self, decoder: &mut super::TIFFDecoder<R>)
-> ::image::ImageResult<Value> {
let bo = decoder.byte_order();
match (self.type_, self.count) {
(Type::BYTE, 1) => Ok(Unsigned(self.offset[0] as u32)),
(Type::SHORT, 1) => Ok(Unsigned(try!(self.r(bo).read_u16()) as u32)),
(Type::SHORT, 2) => {
let mut r = self.r(bo);
Ok(List(vec![
Unsigned(try!(r.read_u16()) as u32),
Unsigned(try!(r.read_u16()) as u32)
]))
},
(Type::SHORT, n) => {
let mut v = Vec::with_capacity(n as usize);
try!(decoder.goto_offset(try!(self.r(bo).read_u32())));
for _ in 0 .. n {
v.push(Unsigned(try!(decoder.read_short()) as u32))
}
Ok(List(v))
},
(Type::LONG, 1) => Ok(Unsigned(try!(self.r(bo).read_u32()))),
(Type::LONG, n) => {
let mut v = Vec::with_capacity(n as usize);
try!(decoder.goto_offset(try!(self.r(bo).read_u32())));
for _ in 0 .. n {
v.push(Unsigned(try!(decoder.read_long())))
}
Ok(List(v))
}
_ => Err(::image::ImageError::UnsupportedError("Unsupported data type.".to_string()))
}
}
}
pub type Directory = HashMap<Tag, Entry>;