如何修复“图像(由stb_image加载)占用太多内存”

时间:2019-04-01 11:39:35

标签: c++ image loading stb-image

我目前正在学习cg,并且必须加载一些纹理。我尝试使用stb_images,但效果很好,但是当我将图像加载到内存中时会占用太多空间。

我正在使用一个小的jpg(512 * 512,154kb),但是如果我加载10次,它将占用大约12 MB的内存。 (我使用Visual Studio诊断工具检查过程内存)

如何消除浪费的内存量。

struct Image {
    int width, height, bpp;
    unsigned char* rgba;

    Image(const std::string& filePath) {
        rgba = stbi_load(filePath.c_str(), &width, &height, &bpp, 4);
    }

    ~Image() {
        stbi_image_free(rgba);
    }
};

std::vector<Image> images;
images.reserve(10);
for (int i = 0; i < 10; ++i)
    images.emplace_back("res/textures/image.jpg");


0 个答案:

没有答案