   How to load 16-bit targas?
  Hi, I am getting stuck on the 16-bit targa type 2 format.  Here is my code at the moment:  void tgaGet16bitData ( image_t *p, FILE *file ){unsigned short int tmp = 0x0000;for (int i = 0; i < p->info.bytes; i += 3 ){p->data[i]   = 0;// Redp->data[i+1] = 0;// Greenp->data[i+2] = 0;// Bluefread(&tmp, 2, 1, file);// Load Blue bitsif ( tmp&0x0100 == 0x0100 ) p->data[i+2] += 1;if ( tmp&0x0200 == 0x0200 ) p->data[i+2] += 2;if ( tmp&0x0400 == 0x0400 ) p->data[i+2] += 4;if ( tmp&0x0800 == 0x0800 ) p->data[i+2] += 8;if ( tmp&0x1000 == 0x1000 ) p->data[i+2] += 16;// Load Green bitsif ( tmp&0x2000 == 0x2000 ) p->data[i+1] += 1;if ( tmp&0x4000 == 0x4000 ) p->data[i+1] += 2;if ( tmp&0x8000 == 0x8000 ) p->data[i+1] += 4;if ( tmp&0x0001 == 0x0001 ) p->data[i+1] += 8;if ( tmp&0x0002 == 0x0002 ) p->data[i+1] += 16;// Load Red bitsif ( tmp&0x0004 == 0x0004 ) p->data[i] += 1;if ( tmp&0x0008 == 0x0008 ) p->data[i] += 2;if ( tmp&0x0010 == 0x0010 ) p->data[i] += 4;if ( tmp&0x0020 == 0x0020 ) p->data[i] += 8;if ( tmp&0x0040 == 0x0040 ) p->data[i] += 16;// Scale each channel from 32 to 256 coloursp->data[i]   *= 8;// Redp->data[i+1] *= 8;// Greenp->data[i+2] *= 8;// Blue}} But this only gets me a black and white image.  I understand each colour channel is coded in 5 bits but how to read them? I am unfortunately a novice with bit-level operations. Thanks for your help[edited by - Keermalec on January 26, 2004 4:27:47 PM]
  34 views and not one answer.  Is this a very dumb question or a very hard one? ;  I think it''s a hard one, however, i think one thing you''re doing wrong is that you address to a bit and then check if the bit value (true or false) equals the bit address. Try to test them with true. This is just a wild guess, but it just might be the solution, i''d give it a try. ;for(i=0;i<Width*Height;i++){    unsigned short temp=0;    fread(&temp, sizeof(unsigned short), 1, stream);    Dst[3*i+0]=((temp>>0x0)&0x1F)<<5; //Red    Dst[3*i+1]=((temp>>0x5)&0x1F)<<5; //Green    Dst[3*i+2]=((temp>>0xA)&0x1F)<<5; //Blue} ;  16 pixels are stored as 5-6-5 bit structures, 5 bits for red, 6 for green and 5 for blue. Green gets an extra bit because the human eye is more sensitive to it (also, no one wants to waste that extra bit). You''ll have to change your bitwise ANDs to allow for this. ;  TGA images are 1555 (16bit) and 555 (15bit), not 565.  The left-most bit is the "attribute" bit, it isn''t used. ;  After having a look at the filespec for Tga's, the storage of a 16bit entry is 2 bytes as AP said:  ARRRRRGG GGGBBBBB.  Be careful however, as it is in little endian format, it actually comes from disk like this:  1st byte = GGGBBBBB, 2nd byte = ARRRRRGG.Information garnered from an article by David McDuffee on Wotsit.  Try the official filespec from Truevision as well.Hope this helps. [edit] links--Cheers,   [edited by - eSCHEn on January 28, 2004 3:42:00 AM];  Thanks for all the help. Apparently I misuse the & operator so i just reverted to using %s and /s...Here''s the code that (almost) works: void tgaGet16bitData ( image_t *p, FILE *file ){unsigned short int byte_1 = 0, byte_2 = 0;for (int i = 0; i < p->info.width * p->info.height * 3; i += 3 ){p->data[i]   = 0;// Redp->data[i+1] = 0;// Greenp->data[i+2] = 0;// Bluefread(&byte_1, 1, 1, file);fread(&byte_2, 1, 1, file);// Load Blue bitsp->data[i] = ((byte_1%2)+((byte_1%4)/2)*2+((byte_1%8)/4)*4+((byte_1%16)/8)*8+((byte_1%32)/16)*16+1)*8-1;// Load Green bitsp->data[i+1] = (((byte_1%64)/32)+((byte_1%128)/64)*2+(byte_1/128)*4+(byte_2%2)*8+((byte_2%4)/2)*16+1)*8-1;// Load Red bitsp->data[i+2] = (((byte_2%8)/4)+((byte_2%16)/8)*2+((byte_2%32)/16)*4+((byte_2%64)/32)*8+((byte_2%128)/64)*16+1)*8-1;}}I can load 16-bit targas with this but the colours are a bit off when I compare them to the same image loaded in PhotoShop or PaintShop Pro.  The byte order is GGGBBBBB ARRRRRGG like eschen said, but the end colour is not simply a case of multiplying each colour by 8 minus 1, it seems a bit more specific, like, for example: 11111 = 255 but 01001 = 79 and 10000 = 130... Any clues? 