1use super::lowlevel::*;
2use super::types::*;
3use std::cell::RefCell;
4use std::fmt;
5use std::fmt::Display;
6
7pub(crate) struct NumArray<I>(RefCell<Option<I>>);
8
9impl<I> NumArray<I> {
10 pub fn new(i: I) -> Self {
11 Self(RefCell::new(Some(i)))
12 }
13}
14
15impl<T: Display, I: IntoIterator<Item = T>> Display for NumArray<I> {
16 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17 let mut first = true;
18 let iter = self.0.borrow_mut().take().unwrap();
19 for number in iter {
20 if !first {
21 write!(f, ", {number}")?;
22 first = false;
23 } else {
24 write!(f, "{number}")?;
25 }
26 }
27 Ok(())
28 }
29}
30
31pub(crate) fn tag_value_new(f: &IfdEntry) -> Option<TagValue> {
33 Some(match f.format {
34 IfdFormat::Ascii => {
35 let mut data = &f.data[..];
37 while let Some((&val, rest)) = data.split_last() {
38 if val != 0 {
39 break;
40 }
41 data = rest;
42 }
43 let s = String::from_utf8_lossy(data);
45 let s = s.into_owned();
46 TagValue::Ascii(s)
47 },
48 IfdFormat::U16 => {
49 let a = read_u16_array(f.le, f.count, &f.data)?;
50 TagValue::U16(a)
51 },
52 IfdFormat::I16 => {
53 let a = read_i16_array(f.le, f.count, &f.data)?;
54 TagValue::I16(a)
55 },
56 IfdFormat::U8 => {
57 if f.data.len() < f.count as usize {
58 return None;
59 }
60 TagValue::U8(f.data.clone())
61 },
62 IfdFormat::I8 => {
63 let a = read_i8_array(f.count, &f.data)?;
64 TagValue::I8(a)
65 },
66 IfdFormat::U32 => {
67 let a = read_u32_array(f.le, f.count, &f.data)?;
68 TagValue::U32(a)
69 },
70 IfdFormat::I32 => {
71 let a = read_i32_array(f.le, f.count, &f.data)?;
72 TagValue::I32(a)
73 },
74 IfdFormat::F32 => {
75 let a = read_f32_array(f.count, &f.data)?;
76 TagValue::F32(a)
77 },
78 IfdFormat::F64 => {
79 let a = read_f64_array(f.count, &f.data)?;
80 TagValue::F64(a)
81 },
82 IfdFormat::URational => {
83 let a = read_urational_array(f.le, f.count, &f.data)?;
84 TagValue::URational(a)
85 },
86 IfdFormat::IRational => {
87 let a = read_irational_array(f.le, f.count, &f.data)?;
88 TagValue::IRational(a)
89 },
90
91 IfdFormat::Undefined => {
92 let a = f.data.clone();
93 TagValue::Undefined(a, f.le)
94 },
95 _ => TagValue::Unknown(f.data.clone(), f.le),
96 })
97}
98
99fn vec_cmp_f32(va: &[f32], vb: &[f32]) -> bool {
101 (va.len() == vb.len()) && va.iter().zip(vb)
103 .all(|(a, b)| (a == b) || (a.is_nan() && b.is_nan()))
104}
105
106fn vec_cmp_f64(va: &[f64], vb: &[f64]) -> bool {
107 (va.len() == vb.len()) && va.iter().zip(vb)
109 .all(|(a, b)| (a == b) || (a.is_nan() && b.is_nan()))
110}
111
112pub(crate) fn tag_value_eq(left: &TagValue, right: &TagValue) -> bool {
116 match (left, right) {
117 (TagValue::F32(x), TagValue::F32(y)) => vec_cmp_f32(x, y),
118 (TagValue::F64(x), TagValue::F64(y)) => vec_cmp_f64(x, y),
119 (x, y) => x == y,
120 }
121}