STBI_LOAD无法加载图像

stbi_load cannot load image

本文关键字:加载 图像 LOAD STBI      更新时间:2023-10-16

我正在尝试使用stbi_load()加载纹理。这是我的代码:

int width, height, numComponents;
unsigned char* imgData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4);
if (imgData == NULL)
    cout << "Cannot load texture" << endl;
//some code
//free
stbi_image_free(imgData);

,当我运行程序时,它说Cannot load texture。我不知道怎么了。我确定文件名是有效的路径,因为当我编写时:

std::ifstream infile(fileName);
if (infile.good())
{
    cout << "Good" << endl;
}
else
{
    cout << "Not good" << endl;
}

它产生Good。有人可以告诉我,该代码是否出了问题,该代码导致imgDataNULL(如果有人想知道,图像是*.jpg文件)。任何帮助将不胜感激!

编辑:我运行了stbi_failure_reason(),然后返回了progressive jpeg。这是什么意思?

,因此答案确实很容易。如果您从此网站阅读:https://gist.github.com/roxlu/3077861

您会看到stb_image不支持progressive JPEG图像格式(我什至不知道存在)

如stbi_image源代码(https://gist.github.com/1271/970F5FE47CF721C6CE5D8B75D28B75D2840C46#FILE-STB_IMAGE-C>)

有这样的限制:

- no jpeg progressive support
- non-HDR formats support 8-bit samples only (jpeg, png)
- no delayed line count (jpeg) -- IJG doesn't support either
- no 1-bit BMP
- GIF always returns *comp=4

至于渐进式jpeg:

与标准JPEG相比,主要区别在于图像的加载方式。

  • 标准jpeg格式一次从上到下加载一行一行,每行都是像素完美的。
  • 对于渐进的JPEG而言,图像全部出现在整体上,但它会模糊和像素化。逐渐地,您会看到一个清晰且满载的图像。

如何检查JPEG文件是否渐进:

您可以遵循此答案的指示:https://superuser.com/a/1617350/1319167