我写了一个小函数来将 ARGB 中的位图转换为灰度。转换本身效果很好,但结果是颠倒的。我找不到错误。
代码:#include #include
inline BYTE GrayScaleValue(BYTE* r, BYTE* g, BYTE* b) { return /*ceil*/(0.21f * (*r) + 0.72f * (*g) + 0.07f * (*b)); }
extern "C" __declspec(dllexport) HBITMAP ConvertToMonocrom(HBITMAP bmp) {
INT x = 0, y = 0;
char Gray;
BITMAP bm;
GetObject(bmp, sizeof(BITMAP), (LPSTR)&bm);
BYTE * pImgByte = (BYTE *)bm.bmBits;
INT iWidthBytes = bm.bmWidth * 4;
for (y = 0; y < bm.bmHeight; y++) {
for (x = 0; x < bm.bmWidth; x++) {
Gray = GrayScaleValue(&pImgByte[y * iWidthBytes + x * 4 + 3], &pImgByte[y * iWidthBytes + x * 4 + 2], &pImgByte[y * iWidthBytes + x * 4 + 1]);
pImgByte[y * iWidthBytes + x * 4] = Gray;
pImgByte[y * iWidthBytes + x * 4 + 1] = Gray;
pImgByte[y * iWidthBytes + x * 4 + 2] = Gray;
pImgByte[y * iWidthBytes + x * 4 + 3] = Gray;
}
}
return CreateBitmapIndirect(&bm);
}
这是图片:
嗯,我不知道,为什么他将“透明”设置为黑色......