//*======
2、libpng库的example.c文件
======*//
一、压缩流程:结合下面的demo。
1、定义png内部表述结构体和png图片信息结构体以及调色板:
png_structp png_ptr;
png_infop info_ptr;
png_colorp palette;
2、打开输出文件output.png
sprintf(fname, "frame%d.png", iFrame);
FILE *fp = fopen(fname, "wb");
3、创建和初始化png_struct和png_info
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); ----> REQUIRED
/* Allocate/initialize the image information data. */
info_ptr = png_create_info_struct(png_ptr);
----> REQUIRED
4、设置错误跳转:
if (setjmp(png_jmpbuf(png_ptr)))
---->REQUIRED
{
/* If we get here, we had a problem writing the file */
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
return ;
}
5、设置输出控制(标准C流)
png_init_io(png_ptr, fp);
---->REQUIRED
6、设置图片属性:
/* Set the image information here. Width and height are up to 2^31,
* bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
* the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
* PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
* or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
* PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
* currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
*/
png_set_IHDR(png_ptr, info_ptr, width, height, 8,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
---->REQUIRED
注:这里的bit_depth参数为8,依赖于color_type参数:PNG_COLOR_TYPE_RGB。
7、分配和设置调色板空间
/* 分配调色板空间。常数 PNG_MAX_PALETTE_LENGTH 的值是256 */
palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof(png_color));
png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
8、写文件头
/* Write the file header information. REQUIRED */
png_write_info(png_ptr, info_ptr);
---->REQUIRED
9、写入图片信息:
/* The easiest way to write the image */
png_uint_32 k;
png_byte *image;
png_bytep row_pointers[height];
image = pFrame->data[0];
if (height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
png_error (png_ptr, "Image is too tall to process in memory");
for (k = 0; k < height; k++)
row_pointers[k] = image + k*width*3;
/* One of the following output methods is REQUIRED */
png_write_image(png_ptr, row_pointers);
---->REQUIRED
10、文件写入完成
png_write_end(png_ptr, info_ptr);
---->REQUIRED
11、去初始化和销毁结构体
png_free(png_ptr, palette);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
printf("success.\n");
实例:(RGB压缩为png图片:与ffmpeg视频解码结合存储为png图片)
//实现视频帧的png压缩:ffmpeg解码视频为YUV420格式,先转为RGB格式,这部分在ffmpeg里实现,这里不贴出代码。
void MyWritePNG2(AVFrame* pFrame, int width, int height, int iFrame)
{
char fname[128] = { 0 };
png_structp png_ptr;
png_infop info_ptr;
png_colorp palette;
sprintf(fname, "frame%d.png", iFrame);
FILE *fp = fopen(fname, "wb");
if (fp == NULL)
return ;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL)
{
fclose(fp);
return ;
}
/* Allocate/initialize the image information data. REQUIRED */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
fclose(fp);
png_destroy_write_struct(&png_ptr, NULL);
return ;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
/* If we get here, we had a problem writing the file */
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
return ;
}
/* 接下来告诉 libpng 用 fwrite 来写入 PNG 文件,并传给它已按二进制方式打开的 FILE* fp */
png_init_io(png_ptr, fp);
/* 设置png文件的属性 */
png_set_IHDR(png_ptr, info_ptr, width, height, 8,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
/* 分配调色板空间。常数 PNG_MAX_PALETTE_LENGTH 的值是256 */
palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof(png_color));
png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
/* Write the file header information. REQUIRED */
png_write_info(png_ptr, info_ptr);
/* The easiest way to write the image (you may have a different memory
* layout, however, so choose what fits your needs best). You need to
* use the first method if you aren't handling interlacing yourself.
*/
png_uint_32 k;
png_byte *image;
png_bytep row_pointers[height];
image = pFrame->data[0];
if (height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
png_error (png_ptr, "Image is too tall to process in memory");
for (k = 0; k < height; k++)
row_pointers[k] = image + k*width*3;
/* One of the following output methods is REQUIRED */
png_write_image(png_ptr, row_pointers);
//end,进行必要的扫尾工作:
png_write_end(png_ptr, info_ptr);
png_free(png_ptr, palette);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
printf("success.\n");
return ;
}