我正在使用libpng将BITMAP转换为PNG(全部在内存中),它确实创建了图像,但有一些视觉错误。
背景有一条垂直彩色线,看起来是青色,品红色和黄色,图像水平展开,因此右侧缺少1/4。 BITMAP使用CDC创建并绘制到CBitmap
。绘图是在RGB中完成的,所以当BITMAP传递给PNG函数时,我使用BITMAPINFOHEADER并将biCompression
设置为BI_RGB
。在libpng设置中,我使用8作为位深度和PNG_COLOR_TYPE_RGB
。我原本以为BITMAP是在CMYK中导致问题的原因,但它似乎是在RGB中。可能存在转换问题吗?当我将相同的位图转换为JPEG时,一切都很完美。
BITMAPINFOHEADER:
BITMAPINFOHEADER oBitmapInfoHeader;
memset(&oBitmapInfoHeader, 0, sizeof(BITMAPINFOHEADER));
oBitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
oBitmapInfoHeader.biWidth = width;
oBitmapInfoHeader.biHeight = height * -1;
oBitmapInfoHeader.biPlanes = 1;
oBitmapInfoHeader.biBitCount = bitmap.bmPlanes * bitmap.bmBitsPixel; //initializes at 32
oBitmapInfoHeader.biCompression = BI_RGB;
oBitmapInfoHeader.biSizeImage = 0;
oBitmapInfoHeader.biXPelsPerMeter = 0;
oBitmapInfoHeader.biYPelsPerMeter = 0;
oBitmapInfoHeader.biClrUsed = 0;
oBitmapInfoHeader.biClrImportant = 0;
我正在使用BYTE数组:
const uint32_t stride = width * 4;
png_byte *row = (png_byte *)poBitmapInfoHeader + (oBitmapInfoHeader.biSize + iColors * sizeof(RGBQUAD));
for (uint32_t y = 0; y < height; y++) {
png_write_row(png_ptr, row);
row += stride;
}
我想到了:
biBitCount确实是24
oBitmapInfoHeader.biBitCount = 24;// bitmap.bmPlanes * bitmap.bmBitsPixel;
GetDIBits
失败了因为它开始有点太远了
(LPBYTE)poBitmapInfoHeader + (oBitmapInfoHeader.biSize + iColors * 3),//sizeof(RGBQUAD)), // address for bitmap bits
然后用3bpp的步幅起作用
const uint32_t stride = (width * 3);