How to correctly display a TGA image in a framebuffer?

119 views Asked by At

I've been working on my own hobby OS. I would like to display icons. For its simplicity, I chose the tga format. But it is impossible to have a correct image: it is completely distorted. Here is my code:

struct tga_header {
      uint8_t magic1;             // must be zero
      uint8_t colormap;           // must be zero
      uint8_t encoding;           // must be 2
      uint16_t cmaporig, cmaplen; // must be zero
      uint8_t cmapent;            // must be zero
      uint16_t x;                 // must be zero
      uint16_t y;                 // image's height
      uint16_t h;                 // image's height
      uint16_t w;                 // image's width
      uint8_t bpp;                // must be 32
      uint8_t pixeltype;          // must be 40
} __attribute__((packed));
            
void display_tga(struct tga_header *base)
{
   if (base->magic1 != 0 || base->colormap != 0 || base->encoding != 2
       || base->cmaporig != 0 || base->cmapent != 0 || base->x != 0
       || base->bpp != 32 || base->pixeltype != 40)
       return;
   uint32_t *img = (u32*)(sizeof(struct tga_header)+(uint64_t)base);
   draw_icon(700, 50, base->w, base->h, img);
}

And the draw_icon function:

static void draw_icon(int x, int y, int w, int h, u32 *img) {
   int j, l, i;

   for (l = j = 0; l < h; l++) {
      for (i = 0; i < w; i++, j++) {
         putpixel(x + i, y + l, img[j]);
      }
   }
}

The image appears like this

1

There are 1 answers

0
ricky On

As long as I searched, you should exchange h and w of tga_header ;)