stbi_load无法加载图像。

8

我正在尝试使用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);

当我运行程序时,它显示“无法加载纹理”。我不知道出了什么问题。我确定文件名是有效路径,因为当我写下以下代码时:
std::ifstream infile(fileName);

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

它生成了Good。有人能告诉我这段代码有什么问题导致imgData为空(如果有人想知道,这个图像是一个*.jpg文件)。任何帮助都将不胜感激!

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


你尝试在调试器中逐步执行它以查看它失败的位置/原因了吗? - tkausl
1
@tkausl 不是,但就在刚才,我运行了 stbi_failure_reason(),它返回了“渐进式 JPEG”。这是什么意思? - Hung Truong
2个回答

5
所以答案其实很简单。如果您阅读此网站:https://gist.github.com/roxlu/3077861,您会发现stb_image不支持渐进式JPEG图像格式(我甚至不知道它的存在)。

1
如stbi_image源代码中所述(https://gist.github.com/1271/970f5fe47cf721c6ce5d8b75d2840c46#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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接