dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Create grayscale bitmap from byte array of pixel intensity

  1. #1
    Join Date
    Jun 2010
    Posts
    1

    Create grayscale bitmap from byte array of pixel intensity

    Hi,

    I am reading some image data out of a camera pixels and have extracted the raw pixel intensity. Each "pixel" on the Camera sensor has either a R,G, or B filter that gives the intensity of the 'R', 'G', or 'B' 'pixel'. I read this data and then extract and store the individual R, G, or B pixel intensities into 3 separate 2d unsigned char arrays called m_ptrRed2DPostDecimationArray. m_ptrGreen2DPostDecimationArray, and m_ptrBlue2DPostDecimationArray. Now I can use SetPixel to display the extracted R, G, and B in 3 separate 'R', 'G', or 'B' window areas. I can display them either in color or grayscale through:
    For grayscale: dc.SetPixel(j, i, RGB(theApp.m_ptrRed2DPostDecimationArray[i][j], theApp.m_ptrRed2DPostDecimationArray[i][j], theApp.m_ptrRed2DPostDecimationArray[i][j]));
    For color: dc.SetPixel(j, i, RGB(theApp.m_ptrRed2DPostDecimationArray[i][j], 0, 0));

    However, I realize SetPixel is very ineffecient so looking at other ways to display. I only need the 8 bit grayscale image so was hoping that I could achieve it using CreateBitmap. m_ptrRed2DPostDecimationArray is a BYTE array (actually unsigned char) that holds the 8 bit raw pixel intensities from the camera pixels that have a red filter on them. Here is my current code (that does not work):
    void CVC_ExampleDlg::OnPaint()
    {
    CDC *pDC = GetDC();

    BOOL retval = m_redBitmap.CreateBitmap(600, 200, 1, 8, theApp.m_ptrRed2DPostDecimationArray);

    CDC memDC;
    memDC.CreateCompatibleDC(pDC);
    memDC.SelectObject(&m_redBitmap);
    retVal = pDC->BitBlt(0, 0, 600, 200, &memDC, 0, 0, SRCCOPY);
    memDC.DeleteDC();
    ReleaseDC(pDC);
    CDialog::OnPaint();
    }

    Now I know I can create a grayscale palette and use the raw data as an index to that palette but it seems to me that CreateBitmap has that built in functionality. I mean it seems like that when I give it 1 color plane and 8bpp, it should automatically create a grayscale 8bit bitmap, no?

    Can anyone see what I'm doing wrong. I mean all I want is that I already have the pixel intensity from 0-255 (8 bit) in a byte array. Since I just only want a grayscale bitmap, creating a grayscale palette and indexing it seems overkill especially when CreateBitmap seems to (theoretically) encapsulate that functionality.

    I guess, if my question is clear enough, what is the easiet and/or most efficiect way to create a 8bit grayscale image from the raw pixel intensity value from 0-255?

    Thank you,

    -Vivek Kinra

  2. #2
    Join Date
    Nov 2007
    Posts
    613

    Re: Create grayscale bitmap from byte array of pixel intensity

    Quote Originally Posted by vkinra View Post
    I mean it seems like that when I give it 1 color plane and 8bpp, it should automatically create a grayscale 8bit bitmap, no?
    No. If you request a 256 color bitmap, well, you'll receive a 256 color bitmap.

    For 1 plane and 8 bits/pixel bitmap the color table is mandatory.

    In order to have full control over the creation of the color table, you should use a device independent bitmap. You can create one using the CreateDIBSection function.

    Subsequently you can use SetDIBits and GetDIBits to have direct access to the bits of the bitmap. For direct access to the color table you can use SetDIBColorTable and GetDIBColorTable functions.

    How to do it:

    1. Create an 1 plane 8 bits/pixel device independent bitmap using CreateDIBSection.
    2. Set its color table to greyscale colors.
    3. Use BitBlt to transfer your color bitmap into this one. Windows will make the necessary conversions automatically and the bitmap will appear in greyscale.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)