
Creating a bitmap from an array of color data
I need to translate a non-standard graphics format to a bitmap. I can grab
the array of color data from the original file, and want to shove it into a
standard bitmap. The original data is 8-bit (BYTE) greyscale, and I am in
16-bit mode on my computer, so I am trying to convert the original data to
that. The closest I can come to getting the proper image is by using the
following code (this actually has an image appear, but it's "redscale". Any
other way just leaves me with a black screen):
m_bitmap = new CBitmap;
BYTE *p = new BYTE[2*n]; // n is width * height; basically sets up short
array for 16 bit
if (p)
{
ZeroMemory(p, 2*n);
int j = 1; // i'm gonna set every other bit at the moment. I need
to convert what I assume is 8-bit grey to 16-bit grey
for (int i = 0; i < n; i++)
{
p[j] = pImage[i]; // pImage is original BYTE data (pImage =
BYTE[n])
j+=2;
}
m_bitmap->CreateCompatibleBitmap(&dc, nWidth, nHeight);
m_bitmap->SetBitmapBits(2*n, p);
delete p;
}
If I set int j = 0 to set first byte of each short, I get a black screen.
If I allocate p as a short array, and set each element to (short)pImage[i],
I get a black screen. When I create a DIB from this data (I swap the array
from top to bottom so it looks right), I get a picture of orange, green and
red points that looks vaguely like the source image, but isn't anywhere near
as clear as the bitmap (I need a DIB to save the bitmap to a file).
I'd like to this to show up in greyscale. If I convert my bitmap back to
the original format, it appears perfectly in a viewer I have for that
format. It's just me setting up the Windows display that is incorrect. I
found some example data at http://www.*-*-*.com/
converting to greyscale, but it just leaves me with a black screen again (I
did use the 8,16,24-bit example).
Can anyone help me? I must be getting the bits and bytes wrong somewhere,
but I can't figure it out.
Thanks...
Dave