【问题标题】:Does stbi_load() have a limit on the number of pixels of the read picturestbi_load() 对读取图片的像素数有限制吗
【发布时间】:2021-11-03 07:50:35
【问题描述】:

我使用stbi_load()加载图片失败,但是没有报错。

unsigned char* data = stbi_load("world_test.jpg", &width, &height, &nrChannel, 0); 
if (data) 
{
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);
}
else 
{
    cout << "load image failed" << endl;
}

此“world_test.jpg”是 43K * 21k JPG 图片,函数 stbi_load() 无法读取图片。但是之前的 21k * 10K "world.jpg" 可以读取。两张图片的其他属性相同。是不是因为图片像素太大?

【问题讨论】:

  • 你在stbi里面调试了吗?根据我的计算,与您的 jpg 对应的位图大小约为 3.6GB,溢出了 32 位 int。你的进程是 64 位的吗?

标签: c++ opengl


【解决方案1】:

头文件本身有something to say这个:

// Note that stb_image pervasively uses ints in its public API for sizes,
// including sizes of memory buffers. This is now part of the API and thus
// hard to change without causing breakage. As a result, the various image
// loaders all have certain limits on image size; these differ somewhat
// by format but generally boil down to either just under 2GB or just under
// 1GB. When the decoded image would be larger than this, stb_image decoding
// will fail.
//
// Additionally, stb_image will reject image files that have any of their
// dimensions set to a larger value than the configurable STBI_MAX_DIMENSIONS,
// which defaults to 2**24 = 16777216 pixels. Due to the above memory limit,
// the only way to have an image with such dimensions load correctly
// is for it to have a rather extreme aspect ratio. Either way, the
// assumption here is that such larger images are likely to be malformed
// or malicious. If you do need to load an image with individual dimensions
// larger than that, and it still fits in the overall size limit, you can
// #define STBI_MAX_DIMENSIONS on your own to be something larger.

【讨论】:

  • 感谢您的提醒。但我也有一点关于这里的像素限制是如何计算的问题。比如我的21K x 10K图片的像素是21600 x 10800 x 3 = 699840000,比16777216要大很多,但是这张图还是可以读取的。
  • 您误读了评论。 2**24 限制是每个维度。图像的总大小为 700 MB,即
  • 好的,非常感谢。好像不能使用更高分辨率的地图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
  • 2017-01-31
  • 2011-02-13
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
相关资源
最近更新 更多