c++ - 我无法加载 .BMP 图像

标签 c++ graphics

我是 opengl 的新手。我正在尝试使用 opengl 加载图像。但我无法这样做。它给了我这个错误:

*** Error in `./lena': double free or corruption (top) : 0x0000000001353070 ***

我不知道我做错了什么。我的代码已在下面给出。实际上这不是我的代码。我在 Stack Overflow 的另一篇文章中看到了它,作者是一位名叫 Ollo 的人。

#include <bits/stdc++.h>
#include <GL/glut.h>

using namespace std;

struct BITMAPFILEHEADER
{
    int bfType;  //specifies the file type
    long long bfSize;  //specifies the size in bytes of the bitmap file
    int bfReserved1;  //reserved; must be 0
    int bfReserved2;  //reserved; must be 0
    long long bOffBits;  //species the offset in bytes from the bitmapfileheader to the bitmap bits
};

struct BITMAPINFOHEADER
{
    long long biSize;  //specifies the number of bytes required by the struct
    long long biWidth;  //specifies width in pixels
    long long biHeight;  //species height in pixels
    int biPlanes; //specifies the number of color planes, must be 1
    int biBitCount; //specifies the number of bit per pixel
    long long biCompression;//spcifies the type of compression
    long long biSizeImage;  //size of image in bytes
    long long biXPelsPerMeter;  //number of pixels per meter in x axis
    long long biYPelsPerMeter;  //number of pixels per meter in y axis
    long long biClrUsed;  //number of colors used by th ebitmap
    long long biClrImportant;  //number of colors that are important
};

int main(void){
    FILE *filePtr;
    BITMAPFILEHEADER bitmapFileHeader;
    BITMAPINFOHEADER *bitmapInfoHeader = new BITMAPINFOHEADER;
    unsigned char *bitmapImage;  //store image data
    int imageIdx=0;  //image index counter
    unsigned char tempRGB;  //our swap variable

    filePtr = fopen("lena.bmp","rb");
    if (filePtr == NULL)
        cout << "ERROR!!! 1" << endl;
    fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER),1,filePtr);

    fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER),1,filePtr); // small edit. forgot to add the closing bracket at sizeof

    //move file point to the begging of bitmap data
    fseek(filePtr, bitmapFileHeader.bOffBits, SEEK_SET);

    //allocate enough memory for the bitmap image data
    bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

    //verify memory allocation
    if (!bitmapImage)
    {
        free(bitmapImage);
        fclose(filePtr);
    }

    //read in the bitmap image data
    fread(bitmapImage, bitmapInfoHeader->biSizeImage, 1, filePtr);

    //make sure bitmap image data was read
    if (bitmapImage == NULL)
    {
        fclose(filePtr);
    }

    //swap the r and b values to get RGB (bitmap is BGR)
    for (imageIdx = 0;imageIdx < bitmapInfoHeader->biSizeImage;imageIdx+=3)
    {
        tempRGB = bitmapImage[imageIdx];
        bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
        bitmapImage[imageIdx + 2] = tempRGB;
    }

    return 0;
}

最佳答案

您从 another answer 获得的此代码StackOverflow 上有一些尴尬的问题。

  1. 它检查 bitmapImage 是否为 0,如果是,它立即调用 上的 free (...)位图图片
  2. 它需要在失败时返回以防止崩溃。
  3. fread (...) 不会改变 bitmapImage 是否为 NULL

以下是您需要进行的一些最小更改:

if (filePtr == NULL) {
    cout << "ERROR!!! 1" << endl;
    return -1; // FAILURE, so return!
}

[...]

//allocate enough memory for the bitmap image data
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

//verify memory allocation
if (!bitmapImage)
{
    //free(bitmapImage); // This does not belong here!
    fclose(filePtr);
    return -2; // FAILURE, so return!
}

//read in the bitmap image data
fread(bitmapImage, bitmapInfoHeader->biSizeImage, 1, filePtr);

// THIS IS POINTLESS TOO, fread (...) is not going to change the address of
//                          bitmapImage.
/*
//make sure bitmap image data was read
if (bitmapImage == NULL)
{
    fclose(filePtr);
}
*/

//swap the r and b values to get RGB (bitmap is BGR)
for (imageIdx = 0;imageIdx < bitmapInfoHeader->biSizeImage;imageIdx+=3)
{
    tempRGB = bitmapImage[imageIdx];
    bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
    bitmapImage[imageIdx + 2] = tempRGB;
}

关于c++ - 我无法加载 .BMP 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23598944/

相关文章:

c++ - 在析构函数中删除[]并将类对象传递给函数作为参数

java - 为什么JFrame的update、revalidate、repaint不更新窗口?

c# - 在 .NET 中组合多个 PNG8 图像的最简单方法

c++ - 如果 C++ 枚举的 switch 语句中的枚举值 > const N,如何获取?

c++ - 类成员函数的基于范围的for循环?

c++ - 在 OpenGL 上用 Bresenham 算法画圆

C++ 模板特化语法

c++ - 访问呈现的 WPF 页面/窗口的底层 DirectX 表面/渲染目标

java - 游戏开发资源

graphics - 纹理过滤和纹理采样有什么区别?