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
use std::io;

/// The kind of encoding used to store sample values
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SampleEncoding {
    /// Samples are unsigned binary integers in big endian
    Binary,

    /// Samples are encoded as decimal ascii strings separated by whitespace
    Ascii,
}

/// Denotes the category of the magic number
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum PNMSubtype {
    /// Magic numbers P1 and P4
    Bitmap(SampleEncoding),

    /// Magic numbers P2 and P5
    Graymap(SampleEncoding),

    /// Magic numbers P3 and P6
    Pixmap(SampleEncoding),

    /// Magic number P7
    ArbitraryMap,
}

/// Stores the complete header data of a file.
///
/// Internally, provides mechanisms for lossless reencoding. After reading a file with the decoder
/// it is possible to recover the header and construct an encoder. Using the encoder on the just
/// loaded image should result in a byte copy of the original file (for single image pnms without
/// additional trailing data).
pub struct PNMHeader {
    pub(crate) decoded: HeaderRecord,
    pub(crate) encoded: Option<Vec<u8>>,
}

pub(crate) enum HeaderRecord {
    Bitmap(BitmapHeader),
    Graymap(GraymapHeader),
    Pixmap(PixmapHeader),
    Arbitrary(ArbitraryHeader),
}

/// Header produced by a `pbm` file ("Portable Bit Map")
#[derive(Clone, Copy, Debug)]
pub struct BitmapHeader {
    /// Binary or Ascii encoded file
    pub encoding: SampleEncoding,

    /// Height of the image file
    pub height: u32,

    /// Width of the image file
    pub width: u32,
}

/// Header produced by a `pgm` file ("Portable Gray Map")
#[derive(Clone, Copy, Debug)]
pub struct GraymapHeader {
    /// Binary or Ascii encoded file
    pub encoding: SampleEncoding,

    /// Height of the image file
    pub height: u32,

    /// Width of the image file
    pub width: u32,

    /// Maximum sample value within the image
    pub maxwhite: u32,
}

/// Header produced by a `ppm` file ("Portable Pixel Map")
#[derive(Clone, Copy, Debug)]
pub struct PixmapHeader{
    /// Binary or Ascii encoded file
    pub encoding: SampleEncoding,

    /// Height of the image file
    pub height: u32,

    /// Width of the image file
    pub width: u32,

    /// Maximum sample value within the image
    pub maxval: u32,
}

/// Header produced by a `pam` file ("Portable Arbitrary Map")
#[derive(Clone, Debug)]
pub struct ArbitraryHeader {
    /// Height of the image file
    pub height: u32,

    /// Width of the image file
    pub width: u32,

    /// Number of color channels
    pub depth: u32,

    /// Maximum sample value within the image
    pub maxval: u32,

    /// Color interpretation of image pixels
    pub tupltype: Option<ArbitraryTuplType>,
}

/// Standardized tuple type specifiers in the header of a `pam`.
#[derive(Clone, Debug)]
pub enum ArbitraryTuplType {
    /// Pixels are either black (0) or white (1)
    BlackAndWhite,

    /// Pixels are either black (0) or white (1) and a second alpha channel
    BlackAndWhiteAlpha,

    /// Pixels represent the amount of white
    Grayscale,

    /// Grayscale with an additional alpha channel
    GrayscaleAlpha,

    /// Three channels: Red, Green, Blue
    RGB,

    /// Four channels: Red, Green, Blue, Alpha
    RGBAlpha,

    /// An image format which is not standardized
    Custom(String),
}

impl PNMSubtype {
    /// Get the two magic constant bytes corresponding to this format subtype.
    pub fn magic_constant(self) -> &'static [u8; 2] {
        match self {
            PNMSubtype::Bitmap(SampleEncoding::Ascii) => b"P1",
            PNMSubtype::Graymap(SampleEncoding::Ascii) => b"P2",
            PNMSubtype::Pixmap(SampleEncoding::Ascii) => b"P3",
            PNMSubtype::Bitmap(SampleEncoding::Binary) => b"P4",
            PNMSubtype::Graymap(SampleEncoding::Binary) => b"P5",
            PNMSubtype::Pixmap(SampleEncoding::Binary) => b"P6",
            PNMSubtype::ArbitraryMap => b"P7",
        }
    }

    /// Whether samples are stored as binary or as decimal ascii
    pub fn sample_encoding(self) -> SampleEncoding {
        match self {
            PNMSubtype::ArbitraryMap => SampleEncoding::Binary,
            PNMSubtype::Bitmap(enc) => enc,
            PNMSubtype::Graymap(enc) => enc,
            PNMSubtype::Pixmap(enc) => enc,
        }
    }
}

impl PNMHeader {
    /// Retrieve the format subtype from which the header was created.
    pub fn subtype(&self) -> PNMSubtype {
        match &self.decoded {
            &HeaderRecord::Bitmap(
                BitmapHeader { encoding, .. }) => PNMSubtype::Bitmap(encoding),
            &HeaderRecord::Graymap(
                GraymapHeader { encoding, .. }) => PNMSubtype::Graymap(encoding),
            &HeaderRecord::Pixmap(
                PixmapHeader { encoding, .. }) => PNMSubtype::Pixmap(encoding),
            &HeaderRecord::Arbitrary(
                ArbitraryHeader { .. }) => PNMSubtype::ArbitraryMap,
        }
    }

    /// The width of the image this header is for.
    pub fn width(&self) -> u32 {
        match &self.decoded {
            &HeaderRecord::Bitmap(BitmapHeader { width, .. })       => width,
            &HeaderRecord::Graymap(GraymapHeader { width, .. })     => width,
            &HeaderRecord::Pixmap(PixmapHeader { width, .. })       => width,
            &HeaderRecord::Arbitrary(ArbitraryHeader { width, .. }) => width,
        }
    }

    /// The height of the image this header is for.
    pub fn height(&self) -> u32 {
        match &self.decoded {
            &HeaderRecord::Bitmap(BitmapHeader { height, .. })       => height,
            &HeaderRecord::Graymap(GraymapHeader { height, .. })     => height,
            &HeaderRecord::Pixmap(PixmapHeader { height, .. })       => height,
            &HeaderRecord::Arbitrary(ArbitraryHeader { height, .. }) => height,
        }
    }

    /// The biggest value a sample can have. In other words, the colour resolution.
    pub fn maximal_sample(&self) -> u32 {
        match &self.decoded {
            &HeaderRecord::Bitmap(BitmapHeader { .. })               => 1,
            &HeaderRecord::Graymap(GraymapHeader { maxwhite, .. })   => maxwhite,
            &HeaderRecord::Pixmap(PixmapHeader { maxval, .. })       => maxval,
            &HeaderRecord::Arbitrary(ArbitraryHeader { maxval, .. }) => maxval,
        }
    }

    /// Retrieve the underlying bitmap header if any
    pub fn as_bitmap(&self) -> Option<&BitmapHeader> {
        match &self.decoded {
            &HeaderRecord::Bitmap(ref bitmap) => Some(bitmap),
            _ => None
        }
    }

    /// Retrieve the underlying graymap header if any
    pub fn as_graymap(&self) -> Option<&GraymapHeader> {
        match &self.decoded {
            &HeaderRecord::Graymap(ref graymap) => Some(graymap),
            _ => None
        }
    }

    /// Retrieve the underlying pixmap header if any
    pub fn as_pixmap(&self) -> Option<&PixmapHeader> {
        match &self.decoded {
            &HeaderRecord::Pixmap(ref pixmap) => Some(pixmap),
            _ => None
        }
    }

    /// Retrieve the underlying arbitrary header if any
    pub fn as_arbitrary(&self) -> Option<&ArbitraryHeader> {
        match &self.decoded {
            &HeaderRecord::Arbitrary(ref arbitrary) => Some(arbitrary),
            _ => None
        }
    }

    /// Write the header back into a binary stream
    pub fn write(&self, writer: &mut io::Write) -> io::Result<()> {
        writer.write_all(self.subtype().magic_constant())?;
        match self {
            &PNMHeader { encoded: Some(ref content), .. }
                => writer.write_all(content),
            &PNMHeader { decoded: HeaderRecord::Bitmap(
                    BitmapHeader{ encoding: _encoding, width, height }), .. } => {
                write!(writer, "\n{} {}\n", width, height)
            },
            &PNMHeader { decoded: HeaderRecord::Graymap(
                    GraymapHeader{ encoding: _encoding, width, height, maxwhite }), .. } => {
                write!(writer, "\n{} {} {}\n", width, height, maxwhite)
            },
            &PNMHeader { decoded: HeaderRecord::Pixmap(
                    PixmapHeader{ encoding: _encoding, width, height, maxval}), .. } => {
                write!(writer, "\n{} {} {}\n", width, height, maxval)
            }
            &PNMHeader { decoded: HeaderRecord::Arbitrary(
                    ArbitraryHeader{ width, height, depth, maxval, ref tupltype}), .. } => {
                #[allow(unused_assignments)]
                // Declared here so its lifetime exceeds the matching. This is a trivial
                // constructor, no allocation takes place and in the custom case we must allocate
                // regardless due to borrow. Still, the warnings checker does pick this up :/
                // Could use std::borrow::Cow instead but that really doesn't achieve anything but
                // increasing type complexity.
                let mut custom_fallback = String::new();

                let tupltype = match tupltype {
                    &None => "",
                    &Some(ArbitraryTuplType::BlackAndWhite) => "TUPLTYPE BLACKANDWHITE\n",
                    &Some(ArbitraryTuplType::BlackAndWhiteAlpha) => "TUPLTYPE BLACKANDWHITE_ALPHA\n",
                    &Some(ArbitraryTuplType::Grayscale) => "TUPLTYPE GRAYSCALE\n",
                    &Some(ArbitraryTuplType::GrayscaleAlpha) => "TUPLTYPE GRAYSCALE_ALPHA\n",
                    &Some(ArbitraryTuplType::RGB) => "TUPLTYPE RGB\n",
                    &Some(ArbitraryTuplType::RGBAlpha) => "TUPLTYPE RGB_ALPHA\n",
                    &Some(ArbitraryTuplType::Custom(ref custom)) => {
                        custom_fallback = format!("TUPLTYPE {}", custom);
                        &custom_fallback
                    }
                };

                write!(writer, "\nWIDTH {}\nHEIGHT {}\nDEPTH {}\nMAXVAL {}\n{}ENDHDR\n",
                    width, height, depth, maxval, tupltype)
            }
        }
    }
}

impl From<BitmapHeader> for PNMHeader {
    fn from(header: BitmapHeader) -> Self {
        PNMHeader {
            decoded: HeaderRecord::Bitmap(header),
            encoded: None,
        }
    }
}

impl From<GraymapHeader> for PNMHeader {
    fn from(header: GraymapHeader) -> Self {
        PNMHeader {
            decoded: HeaderRecord::Graymap(header),
            encoded: None,
        }
    }
}

impl From<PixmapHeader> for PNMHeader {
    fn from(header: PixmapHeader) -> Self {
        PNMHeader {
            decoded: HeaderRecord::Pixmap(header),
            encoded: None,
        }
    }
}

impl From<ArbitraryHeader> for PNMHeader {
    fn from(header: ArbitraryHeader) -> Self {
        PNMHeader {
            decoded: HeaderRecord::Arbitrary(header),
            encoded: None,
        }
    }
}