Jai*_*tes 11
如果你决定采用最小的方法,没有libpng/libjpeg依赖,我建议使用stb_image和stb_image_write,在这里找到.
这是因为它得到一样简单,你只需要放置头文件stb_image.h和stb_image_write.h你的文件夹中.
这是您阅读图像所需的代码:
#include <stdint.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int main() {
int width, height, bpp;
uint8_t* rgb_image = stbi_load("image.png", &width, &height, &bpp, 3);
stbi_image_free(rgb_image);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是编写图像的代码:
#include <stdint.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define CHANNEL_NUM 3
int main() {
int width = 800;
int height = 800;
uint8_t* rgb_image;
rgb_image = malloc(width*height*CHANNEL_NUM);
// Write your code to populate rgb_image here
stbi_write_png("image.png", width, height, CHANNEL_NUM, rgb_image, width*CHANNEL_NUM);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您可以编译没有标志或依赖项:
g++ main.cpp
Run Code Online (Sandbox Code Playgroud)
其他轻量级选择包括: