跳到主要内容

png

简介

PNG图片的读取与保存

组件接口

png.gs

函数原型函数作用
map load(buffer png_data)加载/解析PNG图片
bool save(map data, string name)将解析后的PNG图片保存到指定文件

样例

// Build an image and save it to 'copy.png'
int w = 500;
int h = 600;
buffer buf = buffer.allocate(w * h * 4);
for (int i = 0; i < w * h; i++)
{
buf[i] = 255;
}
map data = {
"width": w,
"height": h,
"pixels": buf
};
bool save_result = png.save(data, __DIR__ "copy.png");
gtest.test_equal(save_result, true);
printf("save png success, path:%s\n", file.get_os_path_from_script_path(__DIR__ "copy.png"));

// Using unsafe underlying interfaces
cp_image_t pngx = png.load_png(__DIR__ "Lenna.png");
bool save_result2 = png.save_png(pngx, __DIR__ "copy2.png");
gtest.test_equal(save_result2, true);
printf("save png success, path:%s\n", file.get_os_path_from_script_path(__DIR__ "copy2.png"));
png.free_png(pngx);