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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么QDBMP无法写入128 * 128图像?相关的知识,希望对你有一定的参考价值。

我正在开发一个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);
}

坦克是你的帮助

UPDATE1源图像是:enter image description here

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

UPDATE2位Depth的值为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;
}

以上是关于为什么QDBMP无法写入128 * 128图像?的主要内容,如果未能解决你的问题,请参考以下文章

使用 qdbmp 读取.bmp 图像

为啥在 Skylake-Xeon 上写入 2 个缓存行的一部分时,`_mm_stream_si128` 比 `_mm_storeu_si128` 慢得多?但对 Haswell 影响较小

无法将 128 分配给字节,为啥它会出错而不是溢出?

如何将自定义图像输入此模型?

将矩阵转换为128x128图片

Spark - 写入 128 MB 大小的 parquet 文件