0

我现在正在寻找几个图像处理库。昨天我发现了一些名为“ stb_image.h ”的库并尝试进行一些测试,但我无法读取 png 文件格式。当然,jpg 可以正常工作。只有png文件出现了问题。有没有人遇到过同样的问题?

  1. 我下载了链接下面的 stb_image.h 文件。

  2. 我做了一个如下所示的测试代码。

    #include <iostream>
    
    #define STB_IMAGE_IMPLEMENTATION
    #include "stb_image.h"
    
    int main(int argc, const char * argv[]) {
        
        int width, height, channel;
        
        // try to load the jpg file format.
        unsigned char *data = stbi_load("container.jpg", &width, &height, &channel, 0);
        if (data) {
            std::cout << "success load image : width=" << width << ", height=" << height << std::endl;
            stbi_image_free(data);
        }
        else {
            std::cout << "failed load image!" << std::endl;
        }
        
        // try to load the png file format.
        data = stbi_load("aswesome.png", &width, &height, &channel, 0);
        if (data) {
            std::cout << "success load image : width=" << width << ", height=" << height << std::endl;
            stbi_image_free(data);
        }
        else {
            std::cout << "failed load image!" << std::endl;
        }
        
        return 0;
    }
  1. 试验结果

    • 所有 jpg 格式文件均已成功读取数据。
    • 所有 png 格式文件都无法读取数据。但是width,height的值,却成功获取了。但是,数据为空。
  2. 这是我加载 png 文件时返回的内存内容。

  3. 测试环境

    • Xcode 版本 12.5
  4. 测试png文件。

4

0 回答 0