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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
//! Representations of TIFF field data types.
//!
//! Each type comes with convenience functions in order
//! to facilitate its use.
//!
//! Every TIFF data type has to implement [`TiffType`] in order to be
//! usable in the crate.
//!
//! [`TiffType`]: trait.TiffType.html
use super::*;
/// A type of data for TIFF fields.
///
/// Other types that might come to exist (and aren't supported by
/// this crate yet) can be easily implemented by implementing this
/// trait.
pub trait TiffType {
/// The TIFF 16-bit code that identifies the type.
fn id() -> u16;
/// The number of bytes occupied by a single value of this type.
fn size() -> u32;
/// The function that writes this type to a given [`EndianFile`].
///
/// # Panics
///
/// Will `panic` if the number of bytes written to the file is
/// different than the number of bytes specified in [`size()`].
///
/// [`EndianFile`]: ../struct.EndianFile.html
/// [`size()`]: #tymethod.size
fn write_to(self, file: &mut EndianFile) -> io::Result<()>;
}
/// 8-bit unsigned integer.
pub struct BYTE(pub u8);
impl BYTE {
/// Constructs a [`TiffTypeValues`] of `BYTE`s from a vector of
/// bytes.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(values: Vec<u8>) -> TiffTypeValues<BYTE> {
TiffTypeValues::new(values.into_iter().map(|value| BYTE(value)).collect())
}
/// Constructs a [`TiffTypeValues`] consisting of a single `BYTE`.
///
/// In other words, marks this `BYTE` as the single value of its
/// field.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn single(value: u8) -> TiffTypeValues<BYTE> {
TiffTypeValues::new(vec![BYTE(value)])
}
}
impl TiffType for BYTE {
fn id() -> u16 {1}
fn size() -> u32 {1}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_u8(self.0)
}
}
/// 8-bit byte that contains a 7-bit ASCII code.
///
/// According the TIFF specification, the last byte
/// of a field of `ASCII`s must be `NUL` (binary zero, '\0').
pub struct ASCII(u8);
impl ASCII {
/// Constructs a [`TiffTypeValues`] of `ASCII`s from a `&str`.
///
/// If the string doesn't already end with a `NUL` value, it will
/// be added automatically.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn from_str(s: &str) -> TiffTypeValues<ASCII> {
let mut values = Vec::with_capacity(s.chars().count());
for c in s.chars() {
if c >= (128 as char) {
panic!("String contains non-ASCII character {}.", c)
}
values.push(c as u8);
}
Self::values(values)
}
/// Constructs a [`TiffTypeValues`] of `ASCII`s from a vector of
/// bytes.
///
/// If last value isn't already a `NUL` value, a `NUL` value will
/// be added automatically after the last value.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(mut values: Vec<u8>) -> TiffTypeValues<ASCII> {
if values.len() == 0 {
panic!("Cannot create an empty instance of TiffTypeValues.")
}
if *values.last().unwrap() != 0 {
// TIFF ASCIIs must end with a NUL character.
// If the user doesn't add it, add it automatically.
values.push(0);
}
TiffTypeValues::new(values.into_iter().map(|value| ASCII::new(value)).collect())
}
/// Creates an `ASCII`s value from a byte.
///
/// # Panics
///
/// An ASCII value only uses 7 bytes. Trying to create an
/// `ASCII` from values bigger than 127 will `panic`.
pub fn new(value: u8) -> ASCII {
if value >= 128 {
panic!("Tried to create an ASCII encoded by the value {}.\n An ASCII value can only range from 0 to 127.", value);
}
ASCII(value)
}
}
impl TiffType for ASCII {
fn id() -> u16 {2}
fn size() -> u32 {1}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_u8(self.0)
}
}
/// 16-bit (2-byte) unsigned integer.
pub struct SHORT(pub u16);
impl SHORT {
/// Constructs a [`TiffTypeValues`] of `SHORTS`s from a vector of
/// `u16`.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(values: Vec<u16>) -> TiffTypeValues<SHORT> {
TiffTypeValues::new(values.into_iter().map(|value| SHORT(value)).collect())
}
/// Constructs a [`TiffTypeValues`] consisting of a single `SHORT`.
///
/// In other words, marks this `SHORT` as the single value of its
/// field.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn single(value: u16) -> TiffTypeValues<SHORT> {
TiffTypeValues::new(vec![SHORT(value)])
}
}
impl TiffType for SHORT {
fn id() -> u16 {3}
fn size() -> u32 {2}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_u16(self.0)
}
}
/// 32-bit (4-byte) unsigned integer.
pub struct LONG(pub u32);
impl LONG {
/// Constructs a [`TiffTypeValues`] of `LONG`s from a vector of
/// `u32`.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(values: Vec<u32>) -> TiffTypeValues<LONG> {
TiffTypeValues::new(values.into_iter().map(|value| LONG(value)).collect())
}
/// Constructs a [`TiffTypeValues`] consisting of a single `LONG`.
///
/// In other words, marks this `LONG` as the single value of its
/// field.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn single(value: u32) -> TiffTypeValues<LONG> {
TiffTypeValues::new(vec![LONG(value)])
}
}
impl TiffType for LONG {
fn id() -> u16 {4}
fn size() -> u32 {4}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_u32(self.0)
}
}
/// Two LONGs representing, respectively, the numerator and the denominator of a fraction.
pub struct RATIONAL{
pub numerator: u32,
pub denominator: u32,
}
impl RATIONAL {
/// Constructs a [`TiffTypeValues`] of `RATIONAL`s from a vector of
/// pairs (numerator, denominator). Both must be `u32` values.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(values: Vec<(u32, u32)>) -> TiffTypeValues<RATIONAL> {
TiffTypeValues::new(values.into_iter().map(|(numerator, denominator)| RATIONAL {numerator, denominator}).collect())
}
/// Constructs a [`TiffTypeValues`] consisting of a single `RATIONAL`
/// from a pair (numerator, denominator). Both values must be `u32`.
///
/// In other words, marks this `RATIONAL` as the single value of its
/// field.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn single(numerator: u32, denominator: u32) -> TiffTypeValues<RATIONAL> {
TiffTypeValues::new(vec![RATIONAL {numerator, denominator}])
}
}
impl TiffType for RATIONAL {
fn id() -> u16 {5}
fn size() -> u32 {8}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_u32(self.numerator)?;
file.write_u32(self.denominator)?;
Ok(())
}
}
/// 8-bit signed (twos-complement) integer.
pub struct SBYTE(pub i8);
impl SBYTE {
/// Constructs a [`TiffTypeValues`] of `SBYTE`s from a vector of
/// `i8`.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(values: Vec<i8>) -> TiffTypeValues<SBYTE> {
TiffTypeValues::new(values.into_iter().map(|value| SBYTE(value)).collect())
}
/// Constructs a [`TiffTypeValues`] consisting of a single `SBYTE`.
///
/// In other words, marks this `SBYTE` as the single value of its
/// field.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn single(value: i8) -> TiffTypeValues<SBYTE> {
TiffTypeValues::new(vec![SBYTE(value)])
}
}
impl TiffType for SBYTE {
fn id() -> u16 {6}
fn size() -> u32 {1}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_i8(self.0)
}
}
/// 8-bit byte that may contain anything, depending on the definition of the field.
pub struct UNDEFINED(pub u8);
impl UNDEFINED {
/// Constructs a [`TiffTypeValues`] of `UNDEFINED`s from a vector of
/// bytes.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(values: Vec<u8>) -> TiffTypeValues<UNDEFINED> {
TiffTypeValues::new(values.into_iter().map(|value| UNDEFINED(value)).collect())
}
/// Constructs a [`TiffTypeValues`] consisting of a single `UNDEFINED`.
///
/// In other words, marks this `UNDEFINED` as the single value of its
/// field.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn single(value: u8) -> TiffTypeValues<UNDEFINED> {
TiffTypeValues::new(vec![UNDEFINED(value)])
}
}
impl TiffType for UNDEFINED {
fn id() -> u16 {7}
fn size() -> u32 {1}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_u8(self.0)
}
}
/// 16-bit (2-byte) signed (twos-complement) integer.
pub struct SSHORT(pub i16);
impl SSHORT {
/// Constructs a [`TiffTypeValues`] of `SSHORT`s from a vector of
/// `i16`.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(values: Vec<i16>) -> TiffTypeValues<SSHORT> {
TiffTypeValues::new(values.into_iter().map(|value| SSHORT(value)).collect())
}
/// Constructs a [`TiffTypeValues`] consisting of a single `SSHORT`.
///
/// In other words, marks this `SSHORT` as the single value of its
/// field.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn single(value: i16) -> TiffTypeValues<SSHORT> {
TiffTypeValues::new(vec![SSHORT(value)])
}
}
impl TiffType for SSHORT {
fn id() -> u16 {8}
fn size() -> u32 {2}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_i16(self.0)
}
}
/// 32-bit (4-byte) signed (twos-complement) integer.
pub struct SLONG(pub i32);
impl SLONG {
/// Constructs a [`TiffTypeValues`] of `SLONG`s from a vector of
/// `i32`.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(values: Vec<i32>) -> TiffTypeValues<SLONG> {
TiffTypeValues::new(values.into_iter().map(|value| SLONG(value)).collect())
}
/// Constructs a [`TiffTypeValues`] consisting of a single `SLONG`.
///
/// In other words, marks this `SLONG` as the single value of its
/// field.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn single(value: i32) -> TiffTypeValues<SLONG> {
TiffTypeValues::new(vec![SLONG(value)])
}
}
impl TiffType for SLONG {
fn id() -> u16 {9}
fn size() -> u32 {4}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_i32(self.0)
}
}
/// Two SLONGs representing, respectively, the numerator and the denominator of a fraction.
pub struct SRATIONAL{
pub numerator: i32,
pub denominator: i32,
}
impl SRATIONAL {
/// Constructs a [`TiffTypeValues`] of `SRATIONAL`s from a vector of
/// pairs (numerator, denominator). Both must be `i32` values.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(values: Vec<(i32, i32)>) -> TiffTypeValues<SRATIONAL> {
TiffTypeValues::new(values.into_iter().map(|(numerator, denominator)| SRATIONAL {numerator, denominator}).collect())
}
/// Constructs a [`TiffTypeValues`] consisting of a single `SRATIONAL`
/// from a pair (numerator, denominator). Both values must be `i32`.
///
/// In other words, marks this `SRATIONAL` as the single value of its
/// field.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn single(numerator: i32, denominator: i32) -> TiffTypeValues<SRATIONAL> {
TiffTypeValues::new(vec![SRATIONAL {numerator, denominator}])
}
}
impl TiffType for SRATIONAL {
fn id() -> u16 {10}
fn size() -> u32 {8}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_i32(self.numerator)?;
file.write_i32(self.denominator)?;
Ok(())
}
}
/// Single precision (4-byte) IEEE format.
pub struct FLOAT(pub f32);
impl FLOAT {
/// Constructs a [`TiffTypeValues`] of `FLOAT`s from a vector of
/// `f32`.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(values: Vec<f32>) -> TiffTypeValues<FLOAT> {
TiffTypeValues::new(values.into_iter().map(|value| FLOAT(value)).collect())
}
/// Constructs a [`TiffTypeValues`] consisting of a single `FLOAT`.
///
/// In other words, marks this `FLOAT` as the single value of its
/// field.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn single(value: f32) -> TiffTypeValues<FLOAT> {
TiffTypeValues::new(vec![FLOAT(value)])
}
}
impl TiffType for FLOAT {
fn id() -> u16 {11}
fn size() -> u32 {4}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_f32(self.0)
}
}
/// Double precision (8-byte) IEEE format.
pub struct DOUBLE(pub f64);
impl DOUBLE {
/// Constructs a [`TiffTypeValues`] of `DOUBLE`s from a vector of
/// `f64`.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn values(values: Vec<f64>) -> TiffTypeValues<DOUBLE> {
TiffTypeValues::new(values.into_iter().map(|value| DOUBLE(value)).collect())
}
/// Constructs a [`TiffTypeValues`] consisting of a single `DOUBLE`.
///
/// In other words, marks this `DOUBLE` as the single value of its
/// field.
///
/// [`TiffTypeValues`]: ../struct.TiffTypeValues.html
pub fn single(value: f64) -> TiffTypeValues<DOUBLE> {
TiffTypeValues::new(vec![DOUBLE(value)])
}
}
impl TiffType for DOUBLE {
fn id() -> u16 {12}
fn size() -> u32 {8}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_f64(self.0)
}
}
/// 32-bit (4-byte) unsigned integer used exclusively to point to IFDs.
///
/// See [`OffsetsToIfds`]
///
/// [`OffsetsToIfds`]: ../struct.OffsetsToIfds.html
pub struct IFD(pub u32);
impl TiffType for IFD {
fn id() -> u16 {13}
fn size() -> u32 {4}
fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
file.write_u32(self.0)
}
}