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
//! Graphic segment subheader definition
use std::fmt::Display;
use std::fs::File;
use std::str::FromStr;

use crate::headers::NitfSegmentHeader;
use crate::types::{ExtendedSubheader, NitfField, Security};
use crate::{NitfError, NitfResult};
/// Header fields for Graphic Segment
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GraphicHeader {
    /// File Part Type
    pub sy: NitfField<SY>,
    /// Graphic Identifier
    pub sid: NitfField<String>,
    /// Graphic Name
    pub sname: NitfField<String>,
    /// Security information
    pub security: Security,
    /// Encryption
    pub encryp: NitfField<String>,
    /// Graphic Type
    pub sfmt: NitfField<Format>,
    /// Reserved for Future Use
    pub sstruct: NitfField<u64>,
    /// Graphic Display Level
    pub sdlvl: NitfField<u16>,
    /// Graphic Attachment Level
    pub salvl: NitfField<u16>,
    /// Graphic Location
    pub sloc: NitfField<BoundLocation>, // TODO: Same image image ILOC type thing
    /// First Graphic Bound Location
    pub sbnd1: NitfField<BoundLocation>,
    /// Graphic Color
    pub scolor: NitfField<Color>,
    /// Second Graphic Bound Location
    pub sbnd2: NitfField<BoundLocation>,
    /// Reserved for Future Use
    pub sres2: NitfField<u8>,
    /// Graphic Extended Subheader Data Length
    pub sxshdl: NitfField<u16>,
    /// Graphic Extended Subheader Overflow
    pub sxsofl: NitfField<u16>,
    /// Graphic Extended Subheader Data
    pub sxshd: ExtendedSubheader,
}
impl Default for GraphicHeader {
    fn default() -> Self {
        Self {
            sy: NitfField::init(2u8, "SY"),
            sid: NitfField::init(10u8, "SID"),
            sname: NitfField::init(20u8, "SNAME"),
            security: Security::default(),
            encryp: NitfField::init(1u8, "ENCRYP"),
            sfmt: NitfField::init(1u8, "SFMT"),
            sstruct: NitfField::init(13u8, "SSTRUCT"),
            sdlvl: NitfField::init(3u8, "SDLVL"),
            salvl: NitfField::init(3u8, "SALVL"),
            sloc: NitfField::init(10u8, "SLOC"),
            sbnd1: NitfField::init(10u8, "SBND1"),
            scolor: NitfField::init(1u8, "SCOLOR"),
            sbnd2: NitfField::init(10u8, "SBND2"),
            sres2: NitfField::init(2u8, "SRES2"),
            sxshdl: NitfField::init(5u8, "SXSHDL"),
            sxsofl: NitfField::init(3u8, "SXSOFL"),
            sxshd: ExtendedSubheader::init("SXSHD"),
        }
    }
}
#[derive(Default, Clone, Debug, Eq, PartialEq)]
pub enum SY {
    #[default]
    SY,
}
impl FromStr for SY {
    type Err = NitfError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "SY" => Ok(Self::default()),
            _ => Err(NitfError::ParseError("SY".to_string())),
        }
    }
}
impl Display for SY {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "SY")
    }
}

impl Display for GraphicHeader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut out_str = String::default();
        out_str += format!("{}, ", self.sy).as_ref();
        out_str += format!("{}, ", self.sid).as_ref();
        out_str += format!("{}, ", self.sname).as_ref();
        out_str += format!("SECURITY: [{}], ", self.security).as_ref();
        out_str += format!("{}, ", self.encryp).as_ref();
        out_str += format!("{}, ", self.sfmt).as_ref();
        out_str += format!("{}, ", self.sstruct).as_ref();
        out_str += format!("{}, ", self.sdlvl).as_ref();
        out_str += format!("{}, ", self.salvl).as_ref();
        out_str += format!("{}, ", self.sloc).as_ref();
        out_str += format!("{}, ", self.sbnd1).as_ref();
        out_str += format!("{}, ", self.scolor).as_ref();
        out_str += format!("{}, ", self.sbnd2).as_ref();
        out_str += format!("{}, ", self.sres2).as_ref();
        out_str += format!("{}, ", self.sxshdl).as_ref();
        out_str += format!("{}, ", self.sxsofl).as_ref();
        out_str += format!("{}", self.sxshd).as_ref();
        write!(f, "Graphic Header: [{out_str}]")
    }
}
impl NitfSegmentHeader for GraphicHeader {
    fn read(&mut self, reader: &mut File) -> NitfResult<()> {
        self.sy.read(reader)?;
        self.sid.read(reader)?;
        self.sname.read(reader)?;
        self.security.read(reader)?;
        self.encryp.read(reader)?;
        self.sfmt.read(reader)?;
        self.sstruct.read(reader)?;
        self.sdlvl.read(reader)?;
        self.salvl.read(reader)?;
        self.sloc.read(reader)?;
        self.sbnd1.read(reader)?;
        self.scolor.read(reader)?;
        self.sbnd2.read(reader)?;
        self.sres2.read(reader)?;
        self.sxshdl.read(reader)?;
        if self.sxshdl.val != 0 {
            self.sxsofl.read(reader)?;
            self.sxshd.read(reader, (self.sxshdl.val - 3) as usize)?;
        }
        Ok(())
    }
    fn write(&self, writer: &mut File) -> NitfResult<usize> {
        let mut bytes_written = 0;
        bytes_written += self.sy.write(writer)?;
        bytes_written += self.sid.write(writer)?;
        bytes_written += self.sname.write(writer)?;
        bytes_written += self.security.write(writer)?;
        bytes_written += self.encryp.write(writer)?;
        bytes_written += self.sfmt.write(writer)?;
        bytes_written += self.sstruct.write(writer)?;
        bytes_written += self.sdlvl.write(writer)?;
        bytes_written += self.salvl.write(writer)?;
        bytes_written += self.sloc.write(writer)?;
        bytes_written += self.sbnd1.write(writer)?;
        bytes_written += self.scolor.write(writer)?;
        bytes_written += self.sbnd2.write(writer)?;
        bytes_written += self.sres2.write(writer)?;
        bytes_written += self.sxshdl.write(writer)?;
        if self.sxshdl.val != 0 {
            bytes_written += self.sxsofl.write(writer)?;
            bytes_written += self.sxshd.write(writer)?;
        }
        Ok(bytes_written)
    }
    fn length(&self) -> usize {
        let mut length = 0;
        length += self.sy.length;
        length += self.sid.length;
        length += self.sname.length;
        length += self.security.length();
        length += self.encryp.length;
        length += self.sfmt.length;
        length += self.sstruct.length;
        length += self.sdlvl.length;
        length += self.salvl.length;
        length += self.sloc.length;
        length += self.sbnd1.length;
        length += self.scolor.length;
        length += self.sbnd2.length;
        length += self.sres2.length;
        length += self.sxshdl.length;
        if self.sxshdl.val != 0 {
            length += self.sxsofl.length;
            length += self.sxshd.size();
        }
        length
    }
}

/// Graphic type. Right now standard only supports C
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum Format {
    #[default]
    /// Computer graphics metafile
    C,
}

impl FromStr for Format {
    type Err = NitfError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "C" => Ok(Self::C),
            _ => Err(NitfError::ParseError("Format".to_string())),
        }
    }
}
impl Display for Format {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::C => write!(f, "C"),
        }
    }
}

/// Graphic bound position relative to origin of coordinate system
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct BoundLocation {
    pub row: i32,
    pub col: i32,
}

impl FromStr for BoundLocation {
    type Err = NitfError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.len() == 10 {
            let bounds = Self {
                row: s[..5]
                    .parse()
                    .or(Err(NitfError::ParseError("BoundLocation.row".to_string())))?,
                col: s[5..]
                    .parse()
                    .or(Err(NitfError::ParseError("BoundLocation.col".to_string())))?,
            };
            Ok(bounds)
        } else {
            Err(NitfError::ParseError("BoundLocation".to_string()))
        }
    }
}
impl Display for BoundLocation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:0<5}{:0<5}", self.row, self.col)
    }
}
/// Color type of graphics
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum Color {
    #[default]
    /// Color pieces
    C,
    /// Monochrome
    M,
}

impl FromStr for Color {
    type Err = NitfError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "C" => Ok(Self::C),
            "M" => Ok(Self::M),
            _ => Err(NitfError::ParseError("Color".to_string())),
        }
    }
}
impl Display for Color {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::C => write!(f, "C"),
            Self::M => write!(f, "M"),
        }
    }
}