0

I'm writing a tga parser in C++ for fun and now it can read files of imagetypes 2, 3, 10, but I'm stuck at type 1 where they have color map. I don't know how to convert color mapped colors to rgb or rgba. It seems like for an type 1 image (unencoded), if I have a char* color_map, I should turn it to uint_8* and if the color_map_entry_depth is 24 and pixel_depth is 8 and we have a uint8_t pixel_data[3] taken from the file buffer, the first pixel color will be

RGB(color_map[pixel_data[2]],color_map[pixel_data[1],color_map[pixel_data[0]])

But it gives me wrong color. Could anybody help?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
Ron
  • 21
  • 2
  • 2
    You might take a look through this TGA reader for help. https://github.com/nothings/stb/blob/master/stb_image.h and search for `stbi__tga_load`. You need to read 1 byte and use that to index into the 3 entries for RGB in the palette. It looks like you're reading 3 bytes of data and trying to use that to index the palette for one pixel. – Retired Ninja Jun 23 '21 at 17:44
  • but how to access to 3 colors using color_map[pixel_data] if pixel_data is one uint_8? do i need to mask the result? – Ron Jun 24 '21 at 00:24
  • Each entry in the palette is 3 bytes, so R = colormap[pixel_data * 3 + 0], G = colormap[pixel_data * 3 + 1], B = colormap[pixel_data * 3 + 2]. At least I assume the palette entries are RGB, haven't manually read a TGA in a while. If they are in a different order adjust the offset as necessary. Looking at the stb source I linked before it's possible they are BGR. *shrug* Use the right test image and it'll be pretty obvious. – Retired Ninja Jun 24 '21 at 00:40

0 Answers0