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
use crate::encoder::compression::*;
use crate::error::TiffResult;
use std::io::{self, Seek, SeekFrom, Write};

pub fn write_tiff_header<W: Write>(writer: &mut TiffWriter<W>) -> TiffResult<()> {
    #[cfg(target_endian = "little")]
    let boi: u8 = 0x49;
    #[cfg(not(target_endian = "little"))]
    let boi: u8 = 0x4d;

    writer.writer.write_all(&[boi, boi])?;
    writer.writer.write_all(&42u16.to_ne_bytes())?;
    writer.offset += 4;

    Ok(())
}

/// Writes a BigTiff header, excluding the IFD offset field.
///
/// Writes the byte order, version number, offset byte size, and zero constant fields. Does
// _not_ write the offset to the first IFD, this should be done by the caller.
pub fn write_bigtiff_header<W: Write>(writer: &mut TiffWriter<W>) -> TiffResult<()> {
    #[cfg(target_endian = "little")]
    let boi: u8 = 0x49;
    #[cfg(not(target_endian = "little"))]
    let boi: u8 = 0x4d;

    // byte order indication
    writer.writer.write_all(&[boi, boi])?;
    // version number
    writer.writer.write_all(&43u16.to_ne_bytes())?;
    // bytesize of offsets (pointer size)
    writer.writer.write_all(&8u16.to_ne_bytes())?;
    // always 0
    writer.writer.write_all(&0u16.to_ne_bytes())?;

    // we wrote 8 bytes, so set the internal offset accordingly
    writer.offset += 8;

    Ok(())
}

pub struct TiffWriter<W> {
    writer: W,
    offset: u64,
    byte_count: u64,
    compressor: Compressor,
}

impl<W: Write> TiffWriter<W> {
    pub fn new(writer: W) -> Self {
        Self {
            writer,
            offset: 0,
            byte_count: 0,
            compressor: Compressor::default(),
        }
    }

    pub fn set_compression(&mut self, compressor: Compressor) {
        self.compressor = compressor;
    }

    pub fn reset_compression(&mut self) {
        self.compressor = Compressor::default();
    }

    pub fn offset(&self) -> u64 {
        self.offset
    }

    pub fn last_written(&self) -> u64 {
        self.byte_count
    }

    pub fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), io::Error> {
        self.byte_count = self.compressor.write_to(&mut self.writer, bytes)?;
        self.offset += self.byte_count;
        Ok(())
    }

    pub fn write_u8(&mut self, n: u8) -> Result<(), io::Error> {
        self.byte_count = self
            .compressor
            .write_to(&mut self.writer, &n.to_ne_bytes())?;
        self.offset += self.byte_count;
        Ok(())
    }

    pub fn write_i8(&mut self, n: i8) -> Result<(), io::Error> {
        self.byte_count = self
            .compressor
            .write_to(&mut self.writer, &n.to_ne_bytes())?;
        self.offset += self.byte_count;
        Ok(())
    }

    pub fn write_u16(&mut self, n: u16) -> Result<(), io::Error> {
        self.byte_count = self
            .compressor
            .write_to(&mut self.writer, &n.to_ne_bytes())?;
        self.offset += self.byte_count;

        Ok(())
    }

    pub fn write_i16(&mut self, n: i16) -> Result<(), io::Error> {
        self.byte_count = self
            .compressor
            .write_to(&mut self.writer, &n.to_ne_bytes())?;
        self.offset += self.byte_count;

        Ok(())
    }

    pub fn write_u32(&mut self, n: u32) -> Result<(), io::Error> {
        self.byte_count = self
            .compressor
            .write_to(&mut self.writer, &n.to_ne_bytes())?;
        self.offset += self.byte_count;

        Ok(())
    }

    pub fn write_i32(&mut self, n: i32) -> Result<(), io::Error> {
        self.byte_count = self
            .compressor
            .write_to(&mut self.writer, &n.to_ne_bytes())?;
        self.offset += self.byte_count;

        Ok(())
    }

    pub fn write_u64(&mut self, n: u64) -> Result<(), io::Error> {
        self.byte_count = self
            .compressor
            .write_to(&mut self.writer, &n.to_ne_bytes())?;
        self.offset += self.byte_count;

        Ok(())
    }

    pub fn write_i64(&mut self, n: i64) -> Result<(), io::Error> {
        self.byte_count = self
            .compressor
            .write_to(&mut self.writer, &n.to_ne_bytes())?;
        self.offset += self.byte_count;

        Ok(())
    }

    pub fn write_f32(&mut self, n: f32) -> Result<(), io::Error> {
        self.byte_count = self
            .compressor
            .write_to(&mut self.writer, &u32::to_ne_bytes(n.to_bits()))?;
        self.offset += self.byte_count;

        Ok(())
    }

    pub fn write_f64(&mut self, n: f64) -> Result<(), io::Error> {
        self.byte_count = self
            .compressor
            .write_to(&mut self.writer, &u64::to_ne_bytes(n.to_bits()))?;
        self.offset += self.byte_count;

        Ok(())
    }

    pub fn pad_word_boundary(&mut self) -> Result<(), io::Error> {
        if self.offset % 4 != 0 {
            let padding = [0, 0, 0];
            let padd_len = 4 - (self.offset % 4);
            self.writer.write_all(&padding[..padd_len as usize])?;
            self.offset += padd_len;
        }

        Ok(())
    }
}

impl<W: Seek> TiffWriter<W> {
    pub fn goto_offset(&mut self, offset: u64) -> Result<(), io::Error> {
        self.offset = offset;
        self.writer.seek(SeekFrom::Start(offset))?;
        Ok(())
    }
}