为什么 QDBMP 无法写入 128*128 图像?

Why QDBMP fail to write 128*128 images?

我正在开发一个 c++ 应用程序,它读取一些位图并使用它们,然后将它们另存为位图。我使用 QDBMP 库来处理位图文件,每样东西都适合 512*512 位图图像。但是在处理 128*128 位图文件时,它只是在输出中写入一些条纹线。这是我读写位图文件的代码:

int readBitmapImage(const char *file_name,UCHAR* r, UCHAR* g, UCHAR* b)
{
BMP* bmp;
UINT width, height;
bmp = BMP_ReadFile(file_name);
BMP_GetDepth(bmp);
BMP_CHECK_ERROR(stderr, -1);
width = BMP_GetWidth(bmp); height = BMP_GetHeight(bmp);
for (int x = 0; x < width; ++x)
{
    for (int y = 0; y < height; ++y)
    {           
        BMP_GetPixelRGB(bmp, x, y, &r[x*width+y], &g[x*width + y], &b[x*width + y]);
    }
}
BMP_CHECK_ERROR(stderr, -2); 

return 0;
}

void writeImageData(const char *file_name, UCHAR* r, UCHAR* g, UCHAR* b,int width,int height,int bitDepth)
{
    BMP* bmp=BMP_Create(width,height,bitDepth);
width = BMP_GetWidth(bmp); height = BMP_GetHeight(bmp);
for (int x = 0; x < width; ++x)
{
    for (int y = 0; y < height; ++y)
    {
        BMP_SetPixelRGB(bmp, x, y, r[x*width + y], g[x*width + y], b[x*width + y]);
    }
}
BMP_WriteFile(bmp, file_name);
}

坦克为你提供帮助

更新1 源图像是:

保存源图像的结果是:

更新2 bitDepth 的值为 24,分配内存的代码块为:

    UCHAR* WimageDataR = (UCHAR*)calloc(128* 128, sizeof(UCHAR));
    UCHAR* WimageDataG = (UCHAR*)calloc(128 * 128, sizeof(UCHAR));
    UCHAR* WimageDataB = (UCHAR*)calloc(128 * 128, sizeof(UCHAR));

过了一会儿我终于发现了问题所在。在 QDBMP 的 BMP_ReadFile() 函数中,当图像大小为 128*128 时,头参数 ImageDataSize 将不会从文件中读取并且大小为 0 。所以我将这段代码添加到其中以防止出现此问题,一切都很好。

if (bmp->Header.ImageDataSize == 0)
{
    bmp->Header.ImageDataSize = bmp->Header.FileSize - bmp->Header.DataOffset;
}