Click here to Skip to main content
15,559,275 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Load png image file or data, use libpng library function to decode png image, and convert it into jpg and bmp format image data, you can use libjpeg library function encoding jpg image data. Hope to be able to provide the relevant code. Thank you. Visual Studio C ++

What I have tried:

C++
int ReadPNGfile(const char* filename, CImageInfo* img)
{
	//用于解码png图片	
	FILE *fp;//文件指针
	fp = fopen(filename, "rb");
	if(fp == NULL) /* 文件打开失败 */
	{
		perror("fopen");
		return -1;
	}
	char        buf[PNG_BYTES_TO_CHECK];
	int        temp;
	//用户可以自定义错误处理函数,如果不需要指定自定义错误处理函数,则传NULL即可。
	/* 初始化各种结构 */
	png_structp png_ptr;
	png_infop  info_ptr;
	png_ptr= png_create_read_struct(PNG_LIBPNG_VER_STRING, 0,0,0);
	if (png_ptr == NULL)
	{
		fclose(fp);
		return -1;
	}
	//设置图像信息
	info_ptr = png_create_info_struct(png_ptr);
	if (info_ptr == NULL)
	{
		fclose(fp);
		png_destroy_read_struct(&png_ptr, NULL, NULL);
		return -1;
	}

	//设置错误返回点
	if (setjmp(png_jmpbuf(png_ptr)))
	{
		/* 释放分配png图像信息的所有内存*/
		png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
		fclose(fp);
		return -1;
	}
	temp = fread(buf,1,PNG_BYTES_TO_CHECK,fp);
	temp = png_sig_cmp((png_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK);

	/*检测是否为png文件*/
	if (temp!=0)
	{
		fclose(fp);
		return -1;
	}
	
	//复位文件指针
	rewind(fp);
	//开始读文件
	png_init_io(png_ptr,fp);//读取PNG图片信息和像素数据
	png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND, NULL);//读取PNG图片信息和像素数据
	int channels_ = png_get_channels(png_ptr, info_ptr); /*获取通道数*/
	int color_type= png_get_color_type(png_ptr, info_ptr); /*颜色类型*/
	int bit_depth = png_get_bit_depth(png_ptr, info_ptr); /* 获取位深 */

	/* row_pointers里边就是rgba数据 */
	png_bytep* row_pointers;
	row_pointers = png_get_rows(png_ptr, info_ptr);
	int width = png_get_image_width(png_ptr, info_ptr);
	int height = png_get_image_height(png_ptr, info_ptr);

	int size = width * channels_;

	size+=size%4;//4字节对齐
	size *= height;
	unsigned char* data = new unsigned char[size];
	int pos = 0;
	int stride=channels_;
	for(int i = height-1; i >= 0; i--)//png上下颠倒
	{
		for(int j = 0; j <stride*width; j += stride)
		{/* 一个字节一个字节的赋值 */
			data[pos++] = row_pointers[i][j]; // red
			data[pos++] = row_pointers[i][j+1]; // green
			data[pos++] = row_pointers[i][j+2];  // blue
			if(stride==4)
			{
				data[pos++] = row_pointers[i][j+3]; // alpha
			}
		}
		pos+=(stride*width)%4;
	}
	CImageInfo newImg(width,height,channels_,size,data);
	*img=newImg;
	/* 撤销数据占用的内存 */
	png_destroy_read_struct(&png_ptr, &info_ptr, 0);
	fclose(fp);
	return 0;
};
Posted
Updated 12-Jul-17 7:39am
v2

1 solution

Converting an image is done by decoding to old format, to access the raw pixel data and than recode the new format.

Take a look at the outstanding article CXImage. For the bmp somebody has done the job.

PS: please also comments in english.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900