      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210

    //! Function for reading TIFF tags

    use std::io::{self, Read, Seek};
    use std::collections::{HashMap};

    use super::stream::{ByteOrder, SmartReader, EndianReader};
    use ::{TiffError, TiffFormatError, TiffUnsupportedError, TiffResult};

    use self::Value::{Unsigned, List, Rational, Ascii};

    macro_rules! tags {
        {$(
            $tag:ident
            $val:expr;
        )*} => {

            /// TIFF tag
            #[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)
                    }
                }
            }
        }
    }

    // Note: These tags appear in the order they are mentioned in the TIFF reference
    tags!{
        // Baseline tags:
        Artist 315;
        // grayscale images PhotometricInterpretation 1 or 3
        BitsPerSample 258;
        CellLength 265; // TODO add support
        CellWidth 264; // TODO add support
        // palette-color images (PhotometricInterpretation 3)
        ColorMap 320; // TODO add support
        Compression 259; // TODO add support for 2 and 32773
        Copyright 33_432;
        DateTime 306;
        ExtraSamples 338; // TODO add support
        FillOrder 266; // TODO add support
        FreeByteCounts 289; // TODO add support
        FreeOffsets 288; // TODO add support
        GrayResponseCurve 291; // TODO add support
        GrayResponseUnit 290; // TODO add support
        HostComputer 316;
        ImageDescription 270;
        ImageLength 257;
        ImageWidth 256;
        Make 271;
        MaxSampleValue 281; // TODO add support
        MinSampleValue 280; // TODO add support
        Model 272;
        NewSubfileType 254; // TODO add support
        Orientation 274; // TODO add support
        PhotometricInterpretation 262;
        PlanarConfiguration 284;
        ResolutionUnit 296; // TODO add support
        RowsPerStrip 278;
        SamplesPerPixel 277;
        Software 305;
        StripByteCounts 279;
        StripOffsets 273;
        SubfileType 255; // TODO add support
        Threshholding 263; // TODO add support
        XResolution 282;
        YResolution 283;
        // Advanced tags
        Predictor 317;
    }

    #[derive(Clone, Copy, Debug, FromPrimitive)]
    pub enum Type {
        BYTE = 1,
        ASCII = 2,
        SHORT = 3,
        LONG = 4,
        RATIONAL = 5,
    }

    #[allow(unused_qualifications)]
    #[derive(Debug, Clone, PartialEq, Eq, Hash)]
    pub enum Value {
        //Signed(i32),
        Unsigned(u32),
        List(Vec<Value>),
        Rational(u32, u32),
        Ascii(String)
    }

    impl Value {
        pub fn into_u32(self) -> TiffResult<u32> {
            match self {
                Unsigned(val) => Ok(val),
                val => Err(TiffError::FormatError(TiffFormatError::UnsignedIntegerExpected(val))),
            }
        }
        pub fn into_u32_vec(self) -> TiffResult<Vec<u32>> {
            match self {
                List(vec) => {
                    let mut new_vec = Vec::with_capacity(vec.len());
                    for v in vec {
                        new_vec.push(v.into_u32()?)
                    }
                    Ok(new_vec)
                },
                Unsigned(val) => Ok(vec![val]),
                Rational(numerator, denominator) => Ok(vec![numerator, denominator]),
                Ascii(val) => Ok(val.chars().map(|x| x as u32).collect())
            }
        }
    }

    #[derive(Clone)]
    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_, count, offset }
        }

        /// Returns a mem_reader for the offset/value field
        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::Decoder<R>)
        -> TiffResult<Value> {
            let bo = decoder.byte_order();
            match (self.type_, self.count) {
                // TODO check if this could give wrong results
                // at a different endianess of file/computer.
                (Type::BYTE, 1) => Ok(Unsigned(u32::from(self.offset[0]))),
                (Type::SHORT, 1) => Ok(Unsigned(u32::from(self.r(bo).read_u16()?))),
                (Type::SHORT, 2) => {
                    let mut r = self.r(bo);
                    Ok(List(vec![
                        Unsigned(u32::from(r.read_u16()?)),
                        Unsigned(u32::from(r.read_u16()?))
                    ]))
                },
                (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(u32::from(decoder.read_short()?)))
                    }
                    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))
                }
                (Type::RATIONAL, 1) => {
                    try!(decoder.goto_offset(try!(self.r(bo).read_u32())));
                    let numerator = try!(decoder.read_long());
                    let denominator = try!(decoder.read_long());
                    Ok(Rational(numerator, denominator))
                },
                (Type::RATIONAL, n) => {
                    let mut v = Vec::with_capacity(n as usize);
                    try!(decoder.goto_offset(try!(self.r(bo).read_u32())));
                    for _ in 0 .. n {
                        let numerator = try!(decoder.read_long());
                        let denominator = try!(decoder.read_long());
                        v.push(Rational(numerator, denominator))
                    }
                    Ok(List(v))
                },
                (Type::ASCII, n) => {
                    try!(decoder.goto_offset(try!(self.r(bo).read_u32())));
                    let string = try!(decoder.read_string(n as usize));
                    Ok(Ascii(string))
                }
                _ => Err(TiffError::UnsupportedError(TiffUnsupportedError::UnsupportedDataType))
            }
        }
    }

    /// Type representing an Image File Directory
    pub type Directory = HashMap<Tag, Entry>;

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)