Coverage Report

Created: 2026-12-01 08:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/image/src/codecs/tga/header.rs
Line
Count
Source
1
use crate::error::{UnsupportedError, UnsupportedErrorKind};
2
use crate::{ExtendedColorType, ImageError, ImageFormat, ImageResult};
3
use byteorder_lite::{LittleEndian, ReadBytesExt, WriteBytesExt};
4
use std::io::{Read, Write};
5
6
pub(crate) const ALPHA_BIT_MASK: u8 = 0b1111;
7
pub(crate) const SCREEN_ORIGIN_BIT_MASK: u8 = 0b10_0000;
8
9
pub(crate) enum ImageType {
10
    NoImageData = 0,
11
    /// Uncompressed images.
12
    RawColorMap = 1,
13
    RawTrueColor = 2,
14
    RawGrayScale = 3,
15
    /// Run length encoded images.
16
    RunColorMap = 9,
17
    RunTrueColor = 10,
18
    RunGrayScale = 11,
19
    Unknown,
20
}
21
22
impl ImageType {
23
    /// Create a new image type from a u8.
24
1.05k
    pub(crate) fn new(img_type: u8) -> ImageType {
25
1.05k
        match img_type {
26
32
            0 => ImageType::NoImageData,
27
28
225
            1 => ImageType::RawColorMap,
29
11
            2 => ImageType::RawTrueColor,
30
5
            3 => ImageType::RawGrayScale,
31
32
331
            9 => ImageType::RunColorMap,
33
245
            10 => ImageType::RunTrueColor,
34
113
            11 => ImageType::RunGrayScale,
35
36
89
            _ => ImageType::Unknown,
37
        }
38
1.05k
    }
39
40
    /// Check if the image format uses colors as opposed to gray scale.
41
1.02k
    pub(crate) fn is_color(&self) -> bool {
42
234
        matches!(
43
1.02k
            *self,
44
            ImageType::RawColorMap
45
                | ImageType::RawTrueColor
46
                | ImageType::RunTrueColor
47
                | ImageType::RunColorMap
48
        )
49
1.02k
    }
50
51
    /// Does the image use a color map.
52
2.57k
    pub(crate) fn is_color_mapped(&self) -> bool {
53
2.57k
        matches!(*self, Self::RawColorMap | Self::RunColorMap)
54
2.57k
    }
55
56
    /// Is the image run length encoded.
57
838
    pub(crate) fn is_encoded(&self) -> bool {
58
236
        matches!(
59
838
            *self,
60
            Self::RunColorMap | Self::RunTrueColor | Self::RunGrayScale
61
        )
62
838
    }
63
}
64
65
/// Header used by TGA image files.
66
#[derive(Debug, Default)]
67
pub(crate) struct Header {
68
    pub(crate) id_length: u8,      // length of ID string
69
    pub(crate) map_type: u8,       // color map type
70
    pub(crate) image_type: u8,     // image type code
71
    pub(crate) map_origin: u16,    // starting index of map
72
    pub(crate) map_length: u16,    // length of map
73
    pub(crate) map_entry_size: u8, // size of map entries in bits
74
    pub(crate) x_origin: u16,      // x-origin of image
75
    pub(crate) y_origin: u16,      // y-origin of image
76
    pub(crate) image_width: u16,   // width of image
77
    pub(crate) image_height: u16,  // height of image
78
    pub(crate) pixel_depth: u8,    // bits per pixel
79
    pub(crate) image_desc: u8,     // image descriptor
80
}
81
82
impl Header {
83
    /// Load the header with values from pixel information.
84
0
    pub(crate) fn from_pixel_info(
85
0
        color_type: ExtendedColorType,
86
0
        width: u16,
87
0
        height: u16,
88
0
        use_rle: bool,
89
0
    ) -> ImageResult<Self> {
90
0
        let mut header = Self::default();
91
92
0
        if width > 0 && height > 0 {
93
0
            let (num_alpha_bits, other_channel_bits, image_type) = match (color_type, use_rle) {
94
0
                (ExtendedColorType::Rgba8, true) => (8, 24, ImageType::RunTrueColor),
95
0
                (ExtendedColorType::Rgb8, true) => (0, 24, ImageType::RunTrueColor),
96
0
                (ExtendedColorType::La8, true) => (8, 8, ImageType::RunGrayScale),
97
0
                (ExtendedColorType::L8, true) => (0, 8, ImageType::RunGrayScale),
98
0
                (ExtendedColorType::Rgba8, false) => (8, 24, ImageType::RawTrueColor),
99
0
                (ExtendedColorType::Rgb8, false) => (0, 24, ImageType::RawTrueColor),
100
0
                (ExtendedColorType::La8, false) => (8, 8, ImageType::RawGrayScale),
101
0
                (ExtendedColorType::L8, false) => (0, 8, ImageType::RawGrayScale),
102
                _ => {
103
0
                    return Err(ImageError::Unsupported(
104
0
                        UnsupportedError::from_format_and_kind(
105
0
                            ImageFormat::Tga.into(),
106
0
                            UnsupportedErrorKind::Color(color_type),
107
0
                        ),
108
0
                    ))
109
                }
110
            };
111
112
0
            header.image_type = image_type as u8;
113
0
            header.image_width = width;
114
0
            header.image_height = height;
115
0
            header.pixel_depth = num_alpha_bits + other_channel_bits;
116
0
            header.image_desc = num_alpha_bits & ALPHA_BIT_MASK;
117
0
            header.image_desc |= SCREEN_ORIGIN_BIT_MASK; // Upper left origin.
118
0
        }
119
120
0
        Ok(header)
121
0
    }
122
123
    const HEADER_BYTES: usize = 18;
124
125
    /// Load the header with values from the reader.
126
1.07k
    pub(crate) fn from_reader(reader: &mut dyn Read) -> ImageResult<Self> {
127
1.07k
        let mut buf = [0_u8; Self::HEADER_BYTES];
128
1.07k
        reader.read_exact(&mut buf)?;
129
1.05k
        let mut r = buf.as_slice();
130
131
1.05k
        let header = Self {
132
1.05k
            id_length: r.read_u8()?,
133
1.05k
            map_type: r.read_u8()?,
134
1.05k
            image_type: r.read_u8()?,
135
1.05k
            map_origin: r.read_u16::<LittleEndian>()?,
136
1.05k
            map_length: r.read_u16::<LittleEndian>()?,
137
1.05k
            map_entry_size: r.read_u8()?,
138
1.05k
            x_origin: r.read_u16::<LittleEndian>()?,
139
1.05k
            y_origin: r.read_u16::<LittleEndian>()?,
140
1.05k
            image_width: r.read_u16::<LittleEndian>()?,
141
1.05k
            image_height: r.read_u16::<LittleEndian>()?,
142
1.05k
            pixel_depth: r.read_u8()?,
143
1.05k
            image_desc: r.read_u8()?,
144
        };
145
146
1.05k
        debug_assert!(r.is_empty());
147
148
1.05k
        Ok(header)
149
1.07k
    }
150
151
    /// Write out the header values.
152
0
    pub(crate) fn write_to(&self, writer: &mut dyn Write) -> ImageResult<()> {
153
0
        let mut buf = [0_u8; Self::HEADER_BYTES];
154
0
        let mut w = buf.as_mut_slice();
155
156
0
        w.write_u8(self.id_length)?;
157
0
        w.write_u8(self.map_type)?;
158
0
        w.write_u8(self.image_type)?;
159
0
        w.write_u16::<LittleEndian>(self.map_origin)?;
160
0
        w.write_u16::<LittleEndian>(self.map_length)?;
161
0
        w.write_u8(self.map_entry_size)?;
162
0
        w.write_u16::<LittleEndian>(self.x_origin)?;
163
0
        w.write_u16::<LittleEndian>(self.y_origin)?;
164
0
        w.write_u16::<LittleEndian>(self.image_width)?;
165
0
        w.write_u16::<LittleEndian>(self.image_height)?;
166
0
        w.write_u8(self.pixel_depth)?;
167
0
        w.write_u8(self.image_desc)?;
168
169
0
        debug_assert!(w.is_empty());
170
171
0
        writer.write_all(&buf)?;
172
173
0
        Ok(())
174
0
    }
175
}