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

时间:2018-09-01 22:02:30

标签: c++ visual-studio-2015 bitmap

我正在开发一个c ++应用程序,该应用程序读取一些位图并使用它们,然后将它们另存为bitmap。我使用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);
}

坦克为您提供帮助

UPDATE1 源图像是:  enter image description here

保存源图像的结果是:  enter image description here

UPDATE2 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));

1 个答案:

答案 0 :(得分:1)

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

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