在C++中将彩色位图转换为灰度

Converting Color Bitmap to Grayscale in C++

本文关键字:转换 灰度 位图 彩色 C++      更新时间:2023-10-16

需要将彩色位图转换为灰度。方法是:

        HDC             hdcWindow = GetDC(hWnd);
        HBITMAP hDIBBitmap;
        {
            // Create a compatible DC which is used in a BitBlt from the window DC
            HDC hdcMemDC = CreateCompatibleDC(hdcWindow);
            SelectObject(hdcMemDC, bmHatch);
            BITMAPINFO bmi;
            memset(&bmi, 0, sizeof(BITMAPINFO));
            bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            bmi.bmiHeader.biWidth = bm.bmWidth;
            bmi.bmiHeader.biHeight = bm.bmHeight; // top-down
            bmi.bmiHeader.biPlanes = bm.bmPlanes;
            bmi.bmiHeader.biBitCount = bm.bmBitsPixel;
            UINT* pBits;
            HBITMAP hDIBBitmap = CreateDIBSection(hdcMemDC, &bmi, DIB_RGB_COLORS, (void**)&pBits, NULL, NULL);
            for (int i = 0; i < bm.bmWidth; i++) {
                for (int j = 0; j < bm.bmHeight; j++) {
                    UINT val = pBits[i + j * bm.bmWidth];
                    if (val != 0)
                    {
                        COLORREF clr = val;
                        UINT newVal = (GetRValue(clr) + GetBValue(clr) + GetGValue(clr)) / 3;
                        pBits[i + j * bm.bmWidth] = newVal;
                    }
                }
            }
            SelectObject(hdcMemDC, hDIBBitmap);
            // draw content of memory bitmap in the window
            BitBlt(hdcWindow, 0, 0, bm.bmWidth, bm.bmHeight, hdcMemDC, 0, 0, SRCCOPY);
            DeleteObject(hDIBBitmap);
            DeleteDC(hdcMemDC);
        }
        ReleaseDC(hWnd, hdcWindow);

在上面的代码示例中,输入位图由 bm 给出,bm 是一个位图实例。

创建了兼容的 DC。使用 selectObject 语句加载位图。然后想要通过创建用于遍历位图值的 DIB 部分来更改位。更改值后,选择 hDIBBitmap,然后最后使用 BitBlt 函数绘制。

当我注释掉以下行时,我可以看到正确呈现的原始位图: SelectObject(hdcMemDC, hDIBBitmap);

我观察到的是pBits始终为0,因此转换后的灰度图像没有被渲染,而是得到黑色图片。

请告知这种方法有什么问题。

CreateDIBSection正在用这种用法创建一个空白位图。不使用原始位图中的任何位。如果你画它,你会得到一个黑色的位图。

如果您注释掉SelectObject(hdcMemDC, hDIBBitmap)则此新hDIBBitmap也会被忽略。打印之前在设备上下文中选择的原始位图:SelectObject(hdcMemDC, bmHatch)

若要将HBITMAP转换为灰度,请使用以下代码。请注意,这不适用于调色板位图。

将 24 位或 32 位HBITMAP转换为灰度:

void grayscale(HBITMAP hbitmap)
{
    BITMAP bm;
    GetObject(hbitmap, sizeof(bm), &bm);
    if(bm.bmBitsPixel < 24)
    {
        DebugBreak();
        return;
    }
    HDC hdc = GetDC(HWND_DESKTOP);
    DWORD size = ((bm.bmWidth * bm.bmBitsPixel + 31) / 32) * 4 * bm.bmHeight;
    BITMAPINFO bmi
    {sizeof(BITMAPINFOHEADER),bm.bmWidth,bm.bmHeight,1,bm.bmBitsPixel,BI_RGB,size};
    int stride = bm.bmWidth + (bm.bmWidth * bm.bmBitsPixel / 8) % 4;
    BYTE *bits = new BYTE[size];
    GetDIBits(hdc, hbitmap, 0, bm.bmHeight, bits, &bmi, DIB_RGB_COLORS);
    for(int y = 0; y < bm.bmHeight; y++) {
        for(int x = 0; x < stride; x++) {
            int i = (x + y * stride) * bm.bmBitsPixel / 8;
            BYTE gray = BYTE(0.1 * bits[i+0] + 0.6 * bits[i+1] + 0.3 * bits[i+2]);
            bits[i+0] = bits[i+1] = bits[i+2] = gray;
        }
    }
    SetDIBits(hdc, hbitmap, 0, bm.bmHeight, bits, &bmi, DIB_RGB_COLORS);
    ReleaseDC(HWND_DESKTOP, hdc);
    delete[]bits;
}

用法

//convert to grayscale
//this should be called once
grayscale(hbitmap);
//paint image
HDC memdc = CreateCompatibleDC(hdc);
HBITMAP oldbmp = SelectObject(memdc, hbitmap);
BITMAP bm;
GetObject(hbitmap, sizeof(bm), &bm);
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, memdc, 0, 0, SRCCOPY);
...