-1

I don't understand, when we writing a tga header, why are we doing something like that to put the dimensions of the picture :

    header[12] =(width & 255);
    header[13] =(width>>8 );

Thanks in advance

Pépito
  • 57
  • 4
  • 11
  • It's not clear what you're asking here. – OMGtechy Mar 16 '14 at 21:33
  • You can view my struggles here doing it: http://stackoverflow.com/questions/14095604/load-24-bit-tga and then you can view how to actually do it here: http://stackoverflow.com/questions/20595340/loading-a-tga-bmp-file-in-c-opengl – Brandon Mar 16 '14 at 22:45

1 Answers1

1

TGA width and height is written as two bytes, I assume that header is of type:

unsigned char header[HEADER_SIZE];

then to properly write width which I suppose of type int, you have to put at index 12 low byte (least significant byte) value of width and at index 13 hi byte (most significant byte) part of width.

so if width is 1023 then in binary it is : 1111111111, so at header[12] you need to put 1023 & 255 which is 11111111 in binary, and in header[13] you put 11:

1023 = 11 11111111
          ^^^^^^^^-- header[12] =(width & 255); // 255 is 11111111, 
       ^^----------- header[13] =(width>>8 );

you could actually rewrite above as:

header[12] =(width % 256);
header[13] =(width / 256);

The fact that you dont write directly integer to you header, is because tga file can be read on systems with different byte ordering, in its specification you can read:

3 Byte Ordering TGA files are stored using the Intel byte ordering convention (least significant byte first, most significant byte last). For this reason, applications running on Motorola-based systems will need to invert the ordering of bytes for short and long values after a file has been read

so this way of writing width makes your code platform independent.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • +1 Good answer. Small nit: `so this way of writing width makes this format platform independent.` It makes the code platform independent, the format already is ;). – Till Mar 16 '14 at 22:24