Implement missing tag types and signed 64-bit support

This commit is contained in:
Kal Conley committed 2021-03-17 01:24:32 +01:00
1 parent 2cba04dfe7
commit d086e289dd
8 files changed
+334 -14

No files matched your search

+98
View File
@@ -13,6 +13,14 @@ mod writer;
use self::colortype::*;
use self::writer::*;
/// Type to represent tiff values of type `IFD`
#[derive(Clone)]
pub struct Ifd(pub u32);
/// Type to represent tiff values of type `IFD8`
#[derive(Clone)]
pub struct Ifd8(pub u64);
/// Type to represent tiff values of type `RATIONAL`
#[derive(Clone)]
pub struct Rational {
@@ -21,6 +29,7 @@ pub struct Rational {
}
/// Type to represent tiff values of type `SRATIONAL`
#[derive(Clone)]
pub struct SRational {
pub n: i32,
pub d: i32,
@@ -141,6 +150,21 @@ impl TiffValue for [u64] {
}
}
impl TiffValue for [i64] {
const BYTE_LEN: u32 = 8;
const FIELD_TYPE: Type = Type::SLONG8;
fn count(&self) -> u32 {
self.len() as u32
}
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
let slice = bytecast::i64_as_ne_bytes(self);
writer.write_bytes(slice)?;
Ok(())
}
}
impl TiffValue for [f32] {
const BYTE_LEN: u32 = 4;
const FIELD_TYPE: Type = Type::FLOAT;
@@ -173,6 +197,38 @@ impl TiffValue for [f64] {
}
}
impl TiffValue for [Ifd] {
const BYTE_LEN: u32 = 4;
const FIELD_TYPE: Type = Type::IFD;
fn count(&self) -> u32 {
self.len() as u32
}
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
for x in self {
x.write(writer)?;
}
Ok(())
}
}
impl TiffValue for [Ifd8] {
const BYTE_LEN: u32 = 8;
const FIELD_TYPE: Type = Type::IFD8;
fn count(&self) -> u32 {
self.len() as u32
}
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
for x in self {
x.write(writer)?;
}
Ok(())
}
}
impl TiffValue for [Rational] {
const BYTE_LEN: u32 = 8;
const FIELD_TYPE: Type = Type::RATIONAL;
@@ -303,6 +359,20 @@ impl TiffValue for u64 {
}
}
impl TiffValue for i64 {
const BYTE_LEN: u32 = 8;
const FIELD_TYPE: Type = Type::SLONG8;
fn count(&self) -> u32 {
1
}
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
writer.write_i64(*self)?;
Ok(())
}
}
impl TiffValue for f32 {
const BYTE_LEN: u32 = 4;
const FIELD_TYPE: Type = Type::FLOAT;
@@ -331,6 +401,34 @@ impl TiffValue for f64 {
}
}
impl TiffValue for Ifd {
const BYTE_LEN: u32 = 4;
const FIELD_TYPE: Type = Type::IFD;
fn count(&self) -> u32 {
1
}
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
writer.write_u32(self.0)?;
Ok(())
}
}
impl TiffValue for Ifd8 {
const BYTE_LEN: u32 = 8;
const FIELD_TYPE: Type = Type::IFD8;
fn count(&self) -> u32 {
1
}
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
writer.write_u64(self.0)?;
Ok(())
}
}
impl TiffValue for Rational {
const BYTE_LEN: u32 = 8;
const FIELD_TYPE: Type = Type::RATIONAL;
+7
View File
@@ -81,6 +81,13 @@ impl<W: Write> TiffWriter<W> {
Ok(())
}
pub fn write_i64(&mut self, n: i64) -> Result<(), io::Error> {
self.writer.write_all(&n.to_ne_bytes())?;
self.offset += 8;
Ok(())
}
pub fn write_f32(&mut self, n: f32) -> Result<(), io::Error> {
self.writer.write_all(&u32::to_ne_bytes(n.to_bits()))?;
self.offset += 4;