Creating a bitmap from an array of color data 
Author Message
 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



Sat, 19 May 2001 03:00:00 GMT  
 Creating a bitmap from an array of color data
Thanks to the folks at CodeGuru, I came up with this that works perfectly.
Too easy...

// need to remap colors to greyscale
    short nColors[32];
    for (int i = 0; i < 32; i++)
    {
     nColors[i] = i + (i << 5) + (i << 10);
    }

    short* s = (short*)p;
    for (i = 0; i < nSize; i++)
    {
     s[i] = nColors[pImage[i]/5];
    }

Quote:

>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.codeguru.com/bitmap/index.shtml for
>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



Sat, 19 May 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Copy bitmap colors to array

2. Getting the RGB data as an array out of Bitmap

3. create bitmap from a dialog box with data from .RC file

4. Creating Bitmap with values(from array) and drawing in gray scale

5. Image List: Creating images from a Bitmap Array

6. Creating Bitmap with values(from array) and drawing in gray scale

7. Creating a Bitmap from an Array

8. Change a color bitmap to 8 bit gray scale bitmap

9. Monochrome Bitmap to a multiple plane color Bitmap?

10. Data arrays creating and sorting ?

11. Converting a high-color bitmap to 16 colors

12. Need help to select a 256 color bitmap into 16 bit color DC

 

 
Powered by phpBB® Forum Software