Skip to content

Commit e5fab92

Browse files
committed
Additional fixes: longstanding bug misunderstanding palette start offset.
We were misunderstanding the role of the palette "start" index. Pixel values are absolute, not relative to the start index. Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent f911099 commit e5fab92

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

src/targa.imageio/targa_pvt.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ typedef struct {
3434
uint8_t idlen; ///< image comment length
3535
uint8_t cmap_type; ///< palette type
3636
uint8_t type; ///< image type (see tga_image_type)
37-
uint16_t cmap_first; ///< offset to first entry
38-
uint16_t cmap_length; ///<
39-
uint8_t cmap_size; ///< palette size
37+
uint16_t cmap_first; ///< offset to first stored color entry
38+
uint16_t cmap_length; ///< number of stored color map entries
39+
uint8_t cmap_size; ///< bits per palette entry: 15, 16, 24, or 32
4040
uint16_t x_origin; ///<
4141
uint16_t y_origin; ///<
4242
uint16_t width; ///< image width

src/targa.imageio/targainput.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,13 @@ TGAInput::decode_pixel(unsigned char* in, unsigned char* out,
584584
case TYPE_PALETTED_RLE:
585585
for (int i = 0; i < bytespp; ++i)
586586
k |= in[i] << (8 * i); // Assemble it in little endian order
587-
k = (m_tga.cmap_first + k) * uint64_t(palbytespp);
588-
if (k + palbytespp > palette_alloc_size) {
587+
if (k < m_tga.cmap_first || k >= m_tga.cmap_first + m_tga.cmap_length) {
589588
errorfmt("Corrupt palette index");
590589
return false;
591590
}
591+
// The palette indices stored in the file's pixels are absolute, so
592+
// subtract the start offset to correctly index from the palette.
593+
k = (k - m_tga.cmap_first) * uint64_t(palbytespp);
592594
switch (palbytespp) {
593595
case 2:
594596
// see the comment for 16bpp RGB below for an explanation of this

0 commit comments

Comments
 (0)