Click to See Complete Forum and Search --> : How to convert 9 bits per pixel DIB to 24 bits per pixel DIB?


saradhi
May 12th, 1999, 05:35 PM
Hi,

Is there any way to convert a 9 bits per pixel DIB to 24 bits per pixel DIB?

Thanks,
Vijay

May 13th, 1999, 09:25 PM
I take it by 9 bpp you mean 3 bits for red, 3 for green and 3 for blue, with the 0 to 7 range of each 3 bit segment being mapped to the 0 to 255 range of each color. Assuming you are familiar with DIBs, I would:

1. Read each pixel of the 9 bpp image into an unsigned int (which we will call pixel9bpp). Assuming a 9 bpp color value is stored as follows, (in base 2) 0000000bbbgggrrr (in 24 bpp COLORREFs, because of byte ordering an RGB is actually ordered as 0x00bbggrr), you could do the following:

unsigned int red = (pixel9bpp<<13)>>13;
unsigned int green = ((pixel9bpp<<10)>>13;
unsigned int blue = pixel9bpp>>6;

Of course, if your 9 bpp format is different, you would have to do something else.

2. Scale each component up to eight bits by multiplying by 255/7 (~= 36.43).

red *=(float)(255/7);
green*=(float)(255/7);
blue *=(float)(255/7);

Note that left-shifting by 5 (<<5) or, equivalently, multiplying by 32, will not work as expected, because you will have no values in the range 225-255.

3. Construct a COLORREF using RBG macro:

COLORREF pixel24bpp = RGB(red, green, blue);

4. Insert into memory you allocated to hold the data for your 24 bpp DIB.

*pData = pixel24bpp;
pData++;

I hope this rough sketch of a solution is helpful.

bubu
October 21st, 2002, 11:07 PM
What would be the algorythm for retriving RGB values from a specified pixel from a DIB of 8, 15, 16, 24 and 32 bpp.

I already have:
int x // the x position of the pixel
int y // the y position of the pixel
LPBITMAPINFO lpBmi // the DIB info such as width, height...
void* pBits // the DIB pixels in a single-dimension array

I'm new to C++ and tht lacks me from understanding whats going on in some examples. Today I got a PutPixel() function that may help you to understand what I want. But instead of puting the pixel in a DIB, I want to get it from the DIB bit array. Here is the function:




void PutPixel(int x, int y, BYTE r, BYTE g, BYTE b, LPBITMAPINFO lpBmi, void* pBits)
{
int iOffset = lpBmi->bmiHeader.biWidth * y + x;

switch(lpBmi->bmiHeader.biBitCount)
{
case 8 :
{
//-- Cast void* to a BYTE* and write pixel to surface
BYTE* p = (BYTE*)pBits;
p[iOffset] = (BYTE)r;
}
break;

case 15 :
{
//-- Cast void* to a WORD* and write pixel to surface
WORD* p = (WORD*)pBits;
p[iOffset] = (WORD)(((r & 0xF8) << 7) | ((g & 0xF8) << 2) | b >> 3);
}
break;

case 16 :
{
//-- Cast void* to a WORD* and write pixel to surface
WORD* p = (WORD*)pBits;
p[iOffset] = (WORD)(((r & 0xF8) << 8) | ((g & 0xFC) << 3) | b >> 3);
}
break;

case 24 :
{
//-- Cast void* to a BYTE* and write pixel to surface
BYTE* p = (BYTE*)pBits;
p[iOffset * 3 + 0] = r;
p[iOffset * 3 + 1] = g;
p[iOffset * 3 + 2] = b;
}
break;

case 32 :
{
//-- Cast void* to a DWORD* and write pixel to surface
DWORD* p = (DWORD*)pBits;
p[iOffset] = (DWORD)((r << 16) | (g << 8) | b);
}
break;
}
}


That's all.