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

    Creating 8bit bmp from display

    I am trying to generate an 8-bit, grayscale image from a window. The hardware on the machine I am using will allow a 24 bit image, and it seems to be defaulting to that when the bitmap is created. ie, the bitsperpixel field in the bitmap info structure equals 24. How can I force the program to generate an 8-bitperpixel bmp file?


  2. #2
    Join Date
    May 1999
    Posts
    8

    Re: Creating 8bit bmp from display

    In your code that sets all the BITMAPINFOHEADER member var's just set the bitsperpixel to 8. And with 256 color bmp's you must use a RGBQUAD struct for the color table. Here is an example:

    unsigned short bitspersample = 8;
    unsigned long bmpimagesize;
    DWORD offset;
    RGBQUAD* prgbq;

    bmpwidth = ((imagewidth + 31) / 32) * 32;
    colors=256;
    bmpimagesize = imagewidth*imagelength;

    // THIS IS FOR A GRAYSCALE COLOR TABLE

    prgbq = new RGBQUAD[colors];

    for (loop = 0; loop < colors; loop++)
    {
    j = (BYTE)((loop * 255 + colors - 2) / (colors - 1));
    prgbq[loop].rgbBlue = j;
    prgbq[loop].rgbGreen = j;
    prgbq[loop].rgbRed = j;
    prgbq[loop].rgbReserved = 0;
    }

    DWORD size = sizeof(BITMAPFILEHEADER) +
    sizeof(BITMAPINFOHEADER) +
    sizeof(RGBQUAD)*colors + bmpimagesize;

    //--------------------------------------------------------------------------------
    // Set the address of the BITMAPFILEHEADER and BITMAPINFOHEADER.
    //--------------------------------------------------------------------------------
    m_pBmfh = (BITMAPFILEHEADER*) m_pData;
    m_pBmih = (BITMAPINFOHEADER*) &m_pData[sizeof(BITMAPFILEHEADER)];
    //---------------------------------
    // Set the BITMAPFILEHEADER fields.
    //---------------------------------
    m_pBmfh->bfType = * (WORD *) "BM";
    m_pBmfh->bfSize = (DWORD) size;
    m_pBmfh->bfReserved1 = 0;
    m_pBmfh->bfReserved2 = 0;
    m_pBmfh->bfOffBits = (DWORD) (sizeof(BITMAPFILEHEADER) +
    sizeof(BITMAPINFOHEADER) +
    sizeof(RGBQUAD)*colors);

    m_pBmih->biSize = sizeof(BITMAPINFOHEADER);
    m_pBmih->biWidth = bmpwidth ;
    m_pBmih->biHeight = imagelength;
    m_pBmih->biPlanes = 1;
    m_pBmih->biBitCount = bitspersample;
    m_pBmih->biCompression = BI_RGB;
    m_pBmih->biSizeImage = 0;
    m_pBmih->biClrUsed = 0;
    m_pBmih->biClrImportant = 0;

    //---------------------------------
    // Copy the color map into m_pData.
    //---------------------------------
    CopyMemory(&m_pData[sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)],
    prgbq, sizeof(RGBQUAD)*colors);

    delete [] prgbq;
    offset = m_pBmfh->bfOffBits;

    // THIS IS A USER BUFFER THAT I USED TO ALTER PIXELS
    // AND IS COPIED INTO THE BMP

    CopyMemory(&m_pData[offset], pBinBuf, bmpimagesize);

    Hope this helps, I use this in a CDib Class that i wrote to do image processing in grayscale.

    Regards,
    Abadon


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured