irox_imagery/
tiff.rs

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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// SPDX-License-Identifier: MIT
// Copyright 2024 IROX Contributors
//

use crate::tags::{get_geokey_directory_tags, GEO_KEY_DIRECTORY, KNOWN_TAG_TYPES};
use crate::tiff::geo::GeoKeyDirectory;
use crate::{ImageError, ImageErrorType};
use core::cmp::Ordering;
use core::fmt::Debug;
use irox_bits::{Bits, BitsError, ByteOrder, Seek, SeekFrom};
use irox_log::log::{debug, warn};
use std::collections::BTreeMap;

pub mod geo;
pub mod tags;

pub struct TiffImage {
    ifd: BTreeMap<u16, TiffTag>,
}
impl TiffImage {
    pub fn ifd(&self) -> &BTreeMap<u16, TiffTag> {
        &self.ifd
    }
}
impl Debug for TiffImage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut str = f.debug_struct("TiffImage");
        for v in self.ifd.values() {
            let name = if let Some(tag) = &v.identified_tag {
                format!("{}({})", tag.name, tag.tag_id)
            } else {
                format!("UNK({})", v.tag)
            };
            if let TiffTagValue::ParsedAscii(val) = &v.value {
                str.field(&name, val);
            } else {
                str.field(&name, &format!("{:?}", v.value));
            }
        }
        str.finish()
    }
}

#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum TiffTagFormat {
    /// u8
    Byte = 1,
    /// i8 >= 0 (null terminated count)
    Ascii = 2,
    /// u16
    Short = 3,
    /// u32
    Long = 4,
    /// u32/u32 = u64
    Rational = 5,
    /// i8
    SByte = 6,
    /// u8?
    Undefined = 7,
    /// i16,
    SShort = 8,
    /// i32,
    SLong = 9,
    /// i32/i32 = u64
    SRational = 10,
    /// f32
    Float = 11,
    /// f64
    Double = 12,
}
impl TiffTagFormat {
    pub fn get_size(&self, count: u32) -> u32 {
        #[allow(clippy::match_same_arms)]
        let size = match self {
            TiffTagFormat::Byte => 1,
            TiffTagFormat::Ascii => {
                return count + 1;
            }
            TiffTagFormat::Short => 2,
            TiffTagFormat::Long => 4,
            TiffTagFormat::Rational => 8,
            TiffTagFormat::SByte => 1,
            TiffTagFormat::Undefined => 1,
            TiffTagFormat::SShort => 2,
            TiffTagFormat::SLong => 4,
            TiffTagFormat::SRational => 8,
            TiffTagFormat::Float => 4,
            TiffTagFormat::Double => 8,
        };
        size * count
    }
}
impl TryFrom<u16> for TiffTagFormat {
    type Error = ImageError;
    fn try_from(value: u16) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(TiffTagFormat::Byte),
            2 => Ok(TiffTagFormat::Ascii),
            3 => Ok(TiffTagFormat::Short),
            4 => Ok(TiffTagFormat::Long),
            5 => Ok(TiffTagFormat::Rational),
            6 => Ok(TiffTagFormat::SByte),
            7 => Ok(TiffTagFormat::Undefined),
            8 => Ok(TiffTagFormat::SShort),
            9 => Ok(TiffTagFormat::SLong),
            10 => Ok(TiffTagFormat::SRational),
            11 => Ok(TiffTagFormat::Float),
            12 => Ok(TiffTagFormat::Double),
            ty => Err(ImageError::bad_type(ty)),
        }
    }
}
#[derive(Debug, Clone)]
pub enum TiffTagValue {
    Offset(u32),
    ParsedByte(Vec<u8>),
    ParsedAscii(String),
    ParsedShort(u16),
    ParsedShorts(Vec<u16>),
    ParsedLong(u32),
    ParsedLongs(Vec<u32>),
    ParsedRational(Vec<(u32, u32)>),
    ParsedSByte(Vec<i8>),
    ParsedSShort(Vec<i16>),
    ParsedSLong(Vec<i32>),
    ParsedSRational(Vec<(i32, i32)>),
    ParsedFloat(f32),
    ParsedFloats(Vec<f32>),
    ParsedDouble(f64),
    ParsedDoubles(Vec<f64>),
}

#[derive(Debug, Copy, Clone)]
pub struct TiffTagType {
    name: &'static str,
    tag_id: u16,
    format: TiffTagFormat,
    usual_value_count: u32,
}
impl PartialEq for TiffTagType {
    fn eq(&self, other: &Self) -> bool {
        self.tag_id == other.tag_id
    }
}
impl Eq for TiffTagType {}
impl PartialOrd for TiffTagType {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.tag_id.cmp(&other.tag_id))
    }
}
impl Ord for TiffTagType {
    fn cmp(&self, other: &Self) -> Ordering {
        self.tag_id.cmp(&other.tag_id)
    }
}
impl TiffTagType {
    pub const fn new(
        name: &'static str,
        tag_id: u16,
        format: TiffTagFormat,
        usual_value_count: u32,
    ) -> Self {
        Self {
            name,
            tag_id,
            format,
            usual_value_count,
        }
    }
    pub fn name(&self) -> &'static str {
        self.name
    }
    pub fn tag_id(&self) -> u16 {
        self.tag_id
    }
    pub fn format(&self) -> TiffTagFormat {
        self.format
    }
    pub fn usual_value_count(&self) -> u32 {
        self.usual_value_count
    }
}

#[derive(Debug, Clone)]
pub struct TiffTag {
    tag: u16,
    identified_tag: Option<TiffTagType>,
    field_type: TiffTagFormat,
    value_count: u32,
    value: TiffTagValue,
}

impl TiffTag {
    pub fn tag(&self) -> u16 {
        self.tag
    }
    pub fn identified_tag(&self) -> &Option<TiffTagType> {
        &self.identified_tag
    }
    pub fn field_type(&self) -> &TiffTagFormat {
        &self.field_type
    }
    pub fn value_count(&self) -> u32 {
        self.value_count
    }
    pub fn value(&self) -> &TiffTagValue {
        &self.value
    }
    pub fn read<T: Bits>(source: &mut T, order: ByteOrder) -> Result<TiffTag, ImageError> {
        let tag = source.read_u16(order)?;
        let field_type = source.read_u16(order)?.try_into()?;
        let value_count = source.read_u32(order)?;
        let value = TiffTagValue::Offset(source.read_u32(order)?);

        let identified_tag = KNOWN_TAG_TYPES.iter().find(|v| v.tag_id == tag).copied();

        Ok(TiffTag {
            tag,
            identified_tag,
            field_type,
            value_count,
            value,
        })
    }
    pub fn try_resolve_value<T: Bits + Seek>(
        &mut self,
        source: &mut T,
        order: ByteOrder,
    ) -> Result<(), BitsError> {
        let size = self.field_type.get_size(self.value_count);
        let TiffTagValue::Offset(offset) = self.value else {
            return Ok(());
        };
        if size <= 4 {
            #[allow(clippy::match_same_arms)]
            match self.field_type {
                TiffTagFormat::Byte => {
                    todo!()
                }
                TiffTagFormat::Ascii => {
                    todo!()
                }
                TiffTagFormat::Short => {
                    self.value = TiffTagValue::ParsedShort(offset as u16);
                }
                TiffTagFormat::Long => {
                    self.value = TiffTagValue::ParsedLong(offset);
                }
                TiffTagFormat::Rational => {
                    todo!()
                }
                TiffTagFormat::SByte => {
                    todo!()
                }
                TiffTagFormat::Undefined => {
                    todo!()
                }
                TiffTagFormat::SShort => {
                    todo!()
                }
                TiffTagFormat::SLong => {
                    todo!()
                }
                TiffTagFormat::SRational => {
                    todo!()
                }
                TiffTagFormat::Float => {
                    todo!()
                }
                TiffTagFormat::Double => {
                    todo!()
                }
            }
        } else {
            source.seek(SeekFrom::Start(offset as u64))?;
            if let TiffTagFormat::Ascii = self.field_type {
                let size = (self.value_count as usize).saturating_sub(1);
                let s = source.read_str_sized_lossy(size)?;
                self.value = TiffTagValue::ParsedAscii(s);
                return Ok(());
            }
            match self.field_type {
                TiffTagFormat::Short => {
                    let mut out = Vec::new();
                    for _ in 0..self.value_count {
                        out.push(source.read_u16(order)?);
                    }
                    self.value = TiffTagValue::ParsedShorts(out);
                }
                TiffTagFormat::Long => {
                    let mut out = Vec::new();
                    for _ in 0..self.value_count {
                        out.push(source.read_u32(order)?);
                    }
                    self.value = TiffTagValue::ParsedLongs(out);
                }
                TiffTagFormat::Float => {
                    let mut out = Vec::new();
                    for _ in 0..self.value_count {
                        out.push(source.read_f32(order)?);
                    }
                    self.value = TiffTagValue::ParsedFloats(out);
                }
                TiffTagFormat::Double => {
                    let mut out = Vec::new();
                    for _ in 0..self.value_count {
                        out.push(source.read_f64(order)?);
                    }
                    self.value = TiffTagValue::ParsedDoubles(out);
                }
                TiffTagFormat::Rational => {
                    let mut out = Vec::new();
                    for _ in 0..self.value_count {
                        let v = (source.read_u32(order)?, source.read_u32(order)?);
                        out.push(v);
                    }
                    if out.len() == 1 {
                        let v = out.pop().unwrap_or_default();
                        if v.1 == 1 {
                            self.value = TiffTagValue::ParsedLong(v.0);
                            return Ok(());
                        }
                    }
                    self.value = TiffTagValue::ParsedRational(out);
                }
                _ => {
                    for _ in 0..self.value_count {
                        todo!("{:?}", self.field_type)
                    }
                }
            }
        };

        Ok(())
    }
}

pub struct TiffImageReader;

impl TiffImageReader {
    pub fn read<T: Seek + Bits>(mut source: T) -> Result<TiffImage, ImageError> {
        let order = source.read_be_u16()?;
        let order = match order {
            0x4949 => ByteOrder::LittleEndian,
            0x4D4D => ByteOrder::BigEndian,
            _ => return ImageErrorType::BadByteOrder.into(),
        };
        let magic = source.read_u16(order)?;
        if magic != 42 {
            return ImageErrorType::BadMagic.into();
        }
        let ifd_offset = source.read_u32(order)?;
        source.seek(SeekFrom::Start(ifd_offset as u64))?;

        let ifd_count = source.read_u16(order)?;
        let mut ifd = BTreeMap::new();
        for _ in 0..ifd_count {
            let tag = TiffTag::read(&mut source, order)?;
            ifd.insert(tag.tag, tag);
        }

        for ifd in &mut ifd.values_mut() {
            ifd.try_resolve_value(&mut source, order)?;
        }
        if let Some(gkd) = ifd.get(&GEO_KEY_DIRECTORY.tag_id) {
            if let TiffTagValue::ParsedShorts(shorts) = &gkd.value {
                let dir = GeoKeyDirectory::parse_from(shorts)?;
                for key in &dir.keys {
                    let Some(ent) = get_geokey_directory_tags().get(&key.id) else {
                        warn!("Cannot find ID {} in known geokey tags", key.id);
                        continue;
                    };
                    if key.location == 0 {
                        // directly in value
                        let value = TiffTagValue::ParsedShort(key.value_offset);
                        let tag = key.id;
                        let field_type = ent.format;
                        let value_count = key.count as u32;
                        let identified_tag = Some(*ent);
                        ifd.insert(
                            key.id,
                            TiffTag {
                                value,
                                tag,
                                field_type,
                                value_count,
                                identified_tag,
                            },
                        );
                    } else {
                        // find some other tag.
                        let Some(deref) = ifd.get(&key.location) else {
                            warn!("Cannot find location {} in known ifd tags", key.location);
                            continue;
                        };
                        match deref.field_type {
                            TiffTagFormat::Ascii => {
                                let TiffTagValue::ParsedAscii(val) = &deref.value else {
                                    warn!(
                                        "Expected a parsed ascii value, but was: {:#?}",
                                        &deref.value
                                    );
                                    continue;
                                };
                                let start = key.value_offset as usize;
                                let end = start + key.count as usize - 1;
                                let val = val.get(start..end).unwrap_or_default();
                                ifd.insert(
                                    key.id,
                                    TiffTag {
                                        field_type: TiffTagFormat::Ascii,
                                        tag: key.id,
                                        value_count: 1,
                                        identified_tag: Some(*ent),
                                        value: TiffTagValue::ParsedAscii(val.to_string()),
                                    },
                                );
                            }
                            TiffTagFormat::Double => {
                                debug!("{:#?} {:#?}", key, ent);
                                let TiffTagValue::ParsedDoubles(val) = &deref.value else {
                                    warn!(
                                        "Expected a parsed double values, but was: {:#?}",
                                        &deref.value
                                    );
                                    continue;
                                };
                                let start = key.value_offset as usize;
                                if key.count == 1 {
                                    let val = val.get(start).copied().unwrap_or_default();
                                    ifd.insert(
                                        key.id,
                                        TiffTag {
                                            field_type: TiffTagFormat::Double,
                                            tag: key.id,
                                            value_count: 1,
                                            identified_tag: Some(*ent),
                                            value: TiffTagValue::ParsedDouble(val),
                                        },
                                    );
                                    continue;
                                }

                                todo!()
                            }
                            _ => {
                                warn!("Unsupported GKD field type: {:?}", deref.field_type);
                            }
                        }
                    }
                }
            };
        }
        Ok(TiffImage { ifd })
    }
}

#[cfg(test)]
mod test {
    use crate::tiff::TiffImageReader;
    use crate::ImageError;
    use irox_log::log::Level;
    use std::fs::OpenOptions;

    #[test]
    pub fn test() -> Result<(), ImageError> {
        irox_log::init_console_level(Level::Debug);
        let path = "E:/charts/FAA_Charts/New_York/New York SEC.tif";
        let file = OpenOptions::new()
            .read(true)
            .create(false)
            .open(path)
            .unwrap();
        let img = TiffImageReader::read(file)?;
        println!("{:#?}", img);

        Ok(())
    }
}