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
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
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.