libpng创建及编辑图片

使用libpng读写PNG图片

https://blog.csdn.net/wang93IT/article/details/85003730

如何用libpng输出一个编辑后的png图片
https://blog.csdn.net/Rong_Toa/article/details/80685228

 

png格式详解

https://blog.csdn.net/rony2012/article/details/74749455

实例

保存一个width*height的黑白图

int make_wite_png(const char *outfile, int width, int height)
{
	FILE *fp; 
	png_structp png_ptr;
	png_infop info_ptr;
	unsigned char *row;
	int y;
	int row_width;
	int bit_depth = 1;
	int color_type = PNG_COLOR_TYPE_GRAY;

	if(color_type == PNG_COLOR_TYPE_GRAY){
		bit_depth = 1;
		row_width = width;
	} else if(color_type == PNG_COLOR_TYPE_RGB) {
		bit_depth = 8;
		row_width = width*3;
	} else if(color_type == PNG_COLOR_TYPE_RGBA) {
		bit_depth = 8;
		row_width = width*4;
	} else {
		log_printf("not support color\n");
		return -1;
	}

	row = (unsigned char *)malloc(row_width);
	if(row == NULL) {
		log_printf("Failed to allocate memory.\n");
		return -1;
	}

	fp = fopen(outfile, "wb");
	if(fp == NULL) {
		log_printf("fail to create %s\n", outfile);
		free(row);
		return -1;
	}

	png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
	if(png_ptr == NULL) {
		log_printf("Failed to png_create_write_struct\n");
		fclose(fp);
		free(row);
		return -1;
	}

	info_ptr = png_create_info_struct(png_ptr);
	if(info_ptr == NULL) {
		log_printf("Failed to png_create_info_struct\n");
		fclose(fp);
		free(row);
		return -1;
	}
	if(setjmp(png_jmpbuf(png_ptr)))
	{
		log_printf("call png_jmpbuf fail!\n");
		fclose(fp);
		free(row);
		return -1;
	}

	png_init_io(png_ptr, fp);

	png_set_IHDR(png_ptr, info_ptr,
			width, height,
			bit_depth,//bit_depth
			color_type,//color_type
			PNG_INTERLACE_NONE,//interlace or not
			PNG_COMPRESSION_TYPE_DEFAULT,//compression
			PNG_FILTER_TYPE_DEFAULT);//filter
	png_set_packing(png_ptr);
	png_write_info(png_ptr, info_ptr);
	/* data */
	for(y = 0; y < height; y++) {
		memset(row, 0xff, row_width);//white
		//memset(row, 0x0, width);//black
		png_write_row(png_ptr, row);
	}

	png_write_end(png_ptr, info_ptr);
	png_destroy_write_struct(&png_ptr, &info_ptr);
	fclose(fp);
	free(row);
	
	return 0;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值