图像加载功能不工作

Image loading function not working

本文关键字:工作 功能 加载 图像      更新时间:2023-10-16

好吧,我一直在做一个项目,遇到了这个问题。当我运行我的程序时,我得到这个消息:

Unhandled exception at 0x76fa15de in programmingproj.exe: 0xC0000005: Access violation reading location 0x00000000.

下面是Visual Studio认为是错误的代码:

float **LoadImg(const char *filename)
{
    float **data = { 0 };
    char *buf = new char[32];
    std::string buf2;
    std::ifstream filebuf(filename);
    filebuf.getline(buf, 32);
    // Reiterate over each pixel, very inefficient, needs to be fixed.
    for (int x = 0; x < (IMAGE_SIZE_X - 1); x++)
    {
        for (int y = 0; y < (IMAGE_SIZE_Y - 1); y++)
        {
            filebuf.getline(buf, 32);
            // Only copy the values.
            for (int i = 8; i < 32; i++)
            {
                if (buf[i] != 't' && buf[i] != ' ')
                {
                    buf2 += buf[i];
                }
            }
            // Set the pixel's value.
            data[x][y] = (float)strtodbl(buf2);
        }
    }
    filebuf.close();
    return data;
}

下面是我正在尝试阅读的格式示例:

x   y       Value
1   1           0
1   2           0
1   3           0
1   4           0
1   5      10.159
1   6       5.225
1   7       1.337
1   8           0
1   9           0
1   10          0

我只需要将value字段加载到适当的像素(x, y)。

strtodbl函数只是我写的一个快速的东西,用来替换atof和/或strtod。

EDIT: IMAGE_SIZE_X和IMAGE_SIZE_Y只是图像大小的常量(97x56)。

您已经将data声明为指向指针的指针,并且您正在使用data,但您从未分配空间并设置data指向它。您必须在尝试读取/写入data应该指向的内容之前这样做。