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
//! No-std compatible TGA parser designed for embedded systems, but usable anywhere

#![no_std]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(missing_copy_implementations)]
#![deny(trivial_casts)]
#![deny(trivial_numeric_casts)]
#![deny(unsafe_code)]
#![deny(unstable_features)]
#![deny(unused_import_braces)]
#![deny(unused_qualifications)]

mod footer;
mod header;
mod packet;
mod parse_error;

use crate::footer::*;
use crate::header::*;
use crate::packet::{next_rle_packet, Packet};
use crate::parse_error::ParseError;

pub use crate::footer::TgaFooter;
pub use crate::header::{ImageType, TgaHeader};

/// TGA image
#[derive(Debug, Copy, Clone)]
pub struct Tga<'a> {
    /// TGA header
    pub header: TgaHeader,

    /// TGA footer (last 26 bytes of file)
    pub footer: TgaFooter,

    /// Image pixel data
    pub pixel_data: &'a [u8],
}

impl<'a> Tga<'a> {
    /// Parse a TGA image from a byte slice
    pub fn from_slice(bytes: &'a [u8]) -> Result<Self, ParseError> {
        let (_remaining, header) = header(bytes).map_err(|_| ParseError::Header)?;

        // Read last 26 bytes as TGA footer
        let (_remaining, footer) =
            footer(&bytes[bytes.len() - FOOTER_LEN..]).map_err(|_| ParseError::Footer)?;

        let header_len = HEADER_LEN + header.id_len as usize;

        // TODO: Support color maps with by color map size with
        // (header.color_map_len * header.color_map_entry_size)
        let image_data_start = header_len;

        let image_data_end = [
            footer.extension_area_offset as usize,
            footer.developer_directory_offset as usize,
        ]
        .iter()
        .filter_map(|v| if *v > 0 { Some(*v) } else { None })
        .min()
        .unwrap_or(bytes.len() - FOOTER_LEN);

        let pixel_data = &bytes[image_data_start..image_data_end];

        Ok(Self {
            header,
            footer,
            pixel_data,
        })
    }

    /// Get the bit depth (BPP) of this image
    pub fn bpp(&self) -> u8 {
        self.header.pixel_depth
    }

    /// Get the image width in pixels
    pub fn width(&self) -> u16 {
        self.header.width
    }

    /// Get the image height in pixels
    pub fn height(&self) -> u16 {
        self.header.height
    }

    /// Get the raw image data contained in this image
    ///
    /// TGA images are encoded as packets, either [`RawPacket`]s or [`RlePacket`]s
    pub fn image_data(&self) -> &[u8] {
        self.pixel_data
    }
}

impl<'a> IntoIterator for &'a Tga<'a> {
    type Item = u32;
    type IntoIter = TgaIterator<'a>;

    fn into_iter(self) -> Self::IntoIter {
        let (bytes_to_consume, current_packet) = match self.header.image_type {
            ImageType::Monochrome | ImageType::Truecolor => {
                let data = Packet::from_slice(self.image_data());

                (Some(self.image_data()), data)
            }
            ImageType::RleMonochrome | ImageType::RleTruecolor => {
                next_rle_packet(self.image_data(), self.bpp() / 8)
                    .map(|(remaining, packet)| (Some(remaining), packet))
                    .expect("Failed to parse first image RLE data packet")
            }
            image_type => panic!("Image type {:?} not supported", image_type),
        };

        // Explicit match to prevent integer division rounding errors
        let stride = match self.bpp() {
            8 => 1,
            16 => 2,
            24 => 3,
            32 => 4,
            depth => panic!("Bit depth {} not supported", depth),
        };

        let current_packet_len = current_packet.len();

        TgaIterator {
            tga: self,
            bytes_to_consume,
            current_packet,
            current_packet_position: 0,
            current_packet_pixel_length: current_packet_len / stride,
            stride,
        }
    }
}

/// Iterator over individual TGA pixels
///
/// This can be used to build a raw image buffer to pass around
#[derive(Debug)]
pub struct TgaIterator<'a> {
    /// Reference to original TGA image
    tga: &'a Tga<'a>,

    /// Remaining bytes (after current packet) to consume
    bytes_to_consume: Option<&'a [u8]>,

    /// Reference to current packet definition (either RLE or raw)
    current_packet: Packet<'a>,

    /// Current position within the current packet's pixel run
    current_packet_position: usize,

    /// Current packet length in pixels
    current_packet_pixel_length: usize,

    /// Number of bytes contained within each pixel
    stride: usize,
}

impl<'a> Iterator for TgaIterator<'a> {
    type Item = u32;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_packet_position >= self.current_packet_pixel_length {
            // Parse next packet from remaining bytes
            match self.tga.header.image_type {
                ImageType::Monochrome | ImageType::Truecolor => {
                    return None;
                }
                ImageType::RleMonochrome | ImageType::RleTruecolor => {
                    if self.bytes_to_consume.is_none() {
                        return None;
                    } else {
                        self.current_packet_position = 0;
                    }

                    let (bytes_to_consume, current_packet) =
                        next_rle_packet(self.bytes_to_consume.unwrap(), self.tga.bpp() / 8)
                            .map(|(remaining, packet)| {
                                (
                                    if remaining.len() > 0 {
                                        Some(remaining)
                                    } else {
                                        None
                                    },
                                    packet,
                                )
                            })
                            .expect("Failed to parse first image RLE data packet");

                    self.bytes_to_consume = bytes_to_consume;
                    self.current_packet_pixel_length = current_packet.len() / self.stride;
                    self.current_packet = current_packet;
                }
                image_type => panic!("Image type {:?} not supported", image_type),
            };
            // }
        }

        let (start, px): (usize, &[u8]) = match self.current_packet {
            // RLE packets use the same 4 bytes for the colour of every pixel in the packet, so
            // there is no start offet like `RawPacket`s have
            Packet::RlePacket(ref p) => (0, p.pixel_data),
            // Raw packets need to look within the byte array to find the correct bytes to
            // convert to a pixel value, hence the calculation of `start = position * stride`
            Packet::RawPacket(ref p) => {
                let px = p.pixel_data;
                let start = self.current_packet_position * self.stride;

                (start, px)
            }
            // Uncompressed data just walks along the byte array in steps of `self.stride`
            Packet::FullContents(px) => {
                let start = self.current_packet_position * self.stride;

                (start, px)
            }
        };

        let pixel_value = {
            let out = match self.stride {
                1 => px[start] as u32,
                2 => u32::from_le_bytes([px[start], px[start + 1], 0, 0]),
                3 => u32::from_le_bytes([px[start], px[start + 1], px[start + 2], 0]),
                4 => u32::from_le_bytes([px[start], px[start + 1], px[start + 2], px[start + 3]]),
                depth => unreachable!("Depth {} is not supported", depth),
            };

            self.current_packet_position += 1;

            out
        };

        Some(pixel_value)
    }
}