How to convert 2 bytes from string to integer?

Question:
How to convert 2 bytes from string to integer or how to convert them directly to R,G,B without performance lost? Maybe the question should be: how to read the string to get R,G,B from 2 byte substring...

I have 16bit bitmap, which I am reading by two bytes and need to extract the RGB values. No problem here, I know how to do it. But before I can do it I need to convert the two byte number to integer. I have read the image buffer so it is kept under pBitmap pointer and it is processed with p pointer and p+=2 increment. But now I don't know how to convert the two bytes to number. It seems to me odd convert to be able convert (two conversions? This looks like waste of time / ineffective way of reading string/buffer). The image is 4096x4096 so you can imaginate I can do it as fast as possible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (bmpfile->dib.depth==16) {
   unsigned char *p ,* end;
   unsigned char c[2];
   uint16_t b;
   p = *pBitmap;
   end = p+(bmpfile->dib.raster_size-1);

   size_t a; uint8_t R,G,B;
        // consideration: 1 byte==8bit
while( *p<end )
          {
          c[0]=p[0]; // THREE LINES OF CODE
          c[1]=p[1]; // JUST A TRY
          b=(uint16_t)c; // THIS DOES NOT WORK
          R = (b & 0x1f );
          G = ((b >> 6) & 0x3f );
          B = ((b >> 11) & 0x1f );
          p+=2;
            }
}


My try is not successful. Is there better (&faster)way how to do it? The code should read 16 bit image to 2D pixel array.
The 16-bit bitmap uses these bits to save RGB values:
1
2
3
4
5
6
R-G-B
5-6-5 binary digits
so maximum values are:
R        G     B
11111-111111-11111 (16x binary 1)
31   -   63  - 31

which is maximum range of:
32,64, and 32 colors. Not usint 256 values. But this is possible to convert to 256:
R*8,G*4,B*8
Last edited on
Topic archived. No new replies allowed.