将一段图像数据生成为png图像。
使用libpng库,新建png图像文件,将数据按格式写入到文件内。
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
void write_png_file(char* filename, int width, int height, png_bytep* row_pointers) {
FILE* fp = fopen(filename, "wb");
if (!fp) {
printf("Error opening file %s for writing\n", filename);
return;
}
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
printf("Error creating PNG write structure\n");
fclose(fp);
return;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
printf("Error creating PNG info structure\n");
png_destroy_write_struct(&png_ptr, NULL);
fclose(fp);
return;
}
if (setjmp(png_jmpbuf(png_ptr))) {
printf("Error during PNG writing\n");
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return;
}
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
}
int main() {
int width = 256;
int height = 256;
png_bytep* row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
for (int y = 0; y < height; y++) {
row_pointers[y] = (png_byte*)malloc(sizeof(png_byte) * width * 4);
for (int x = 0; x < width; x++) {
png_byte* pixel = &(row_pointers[y][x * 4]);
pixel[0] = x % 256; // Red
pixel[1] = y % 256; // Green
pixel[2] = (x + y) % 256; // Blue
pixel[3] = 255; // Alpha
}
}
write_png_file("output.png", width, height, row_pointers);
for (int y = 0; y < height; y++) {
free(row_pointers[y]);
}
free(row_pointers);
return 0;
}
gcc编译指令:
依据环境不同,指令内容会有小幅变化。指令编译的各参数解析详见下文:
Evenurs@admin-PowerEdge-T630-212:~/f1c100s/dev/f1c100s/tina$ /home/Evenurs/f1c100s/dev/f1c100s/tina/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-arm9-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-gcc -I /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/libpng-1.2.56/ -I /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/zlib-1.2.8/ data2png.c -o data2png -L /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/libpng-1.2.56/.libs/ -L /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/zlib-1.2.8/ -lpng -lz
编译结果:
arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
详细烧录步骤参考下文:
root@TinaLinux:/# chmod -R 777 ./data2png
root@TinaLinux:/# ./data2png
root@TinaLinux:/# ls
bin hello overlay qrencode sbin var
data2png lib proc rdinit sys www
dev mnt pseudo_init rom tmp
etc output.png qrcode.png root usr
root@TinaLinux:/# exit
C:\Users\Evenurs>adb pull /output.png D:\work\temp
/output.png: 1 file pulled. 0.0 MB/s (855 bytes in 0.019s)
C:\Users\Evenurs>
图像生成算法:
for (int y = 0; y < height; y++) {
row_pointers[y] = (png_byte*)malloc(sizeof(png_byte) * width * 4);
for (int x = 0; x < width; x++) {
png_byte* pixel = &(row_pointers[y][x * 4]);
pixel[0] = x % 256; // Red
pixel[1] = y % 256; // Green
pixel[2] = (x + y) % 256; // Blue
pixel[3] = 255; // Alpha
}
}
png图像:
可以通过libpng将数据直接转为png图像文件。