linux 读取png图片大小,使用libpng读取PNG图片像素数据

这篇博客介绍了如何在Linux环境下利用libpng库读取PNG图片的大小和像素数据。通过示例代码展示了如何打开文件、初始化png_structp和png_infop结构体,以及设置错误处理、读取文件、处理图像信息和进行数据转换的过程。
摘要由CSDN通过智能技术生成

附录  让我们打开pnglib下面的一个example.c

/* Read a PNG file. You may want to return an error code if the read

* fails (depending upon the failure). There are two "prototypes" given

* here - one where we are given the filename, and we need to open the

* file, and the other where we are given an open file (possibly with

* some or all of the magic bytes read - see comments above).

*/

#ifdef open_file /* prototype 1 */

void read_png(char *file_name) /* We need to open the file */

{

定义两个很重要的pngwen文件指针。png_info里面放的是pngchuck和png的数据。

png_structp png_ptr;

png_infop info_ptr;

这些是读出图像需要的参数,图像高。宽、深度、图像类型、交错类型。

unsigned int sig_read = 0;

png_uint_32 width, height;

int bit_depth, color_type, interlace_type;

FILE *fp;

打开文件,把文件指针给png_prt

if ((fp = fopen(file_name, "rb")) == NULL)

return (ERROR);

#else no_open_file /* prototype 2 */

void read_png(FILE *fp, unsigned int sig_read) /* file is already open */

{

png_structp png_ptr;

png_infop info_ptr;

png_uint_32 width, height;

int bit_depth, color_type, interlace_type;

#endif no_open_file /* only use one prototype! */

/* Create and initialize the png_struct with the desired error handler

* functions. If you want to use the default stderr and longjump method,

* you can supply NULL for the last three parameters. We also supply the

* the compiler header file version, so that we know if the application

* was compiled with a compatible version of the library. REQUIRED

*/

png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,

png_voidp user_error_ptr, user_error_fn, user_warning_fn);

if (png_ptr == NULL)

{

fclose(fp);

return (ERROR);

}

/* Allocate/initialize the memory for image information. REQUIRED. */

info_ptr = png_create_info_struct(png_ptr);

if (info_ptr == NULL)

{

fclose(fp);

png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);

return (ERROR);

}

/* Set error handling if you are using the setjmp/longjmp method (this is

* the normal method of doing things with libpng). REQUIRED unless you

* set up your own error handlers in the png_create_read_struct() earlier.

*/

if (setjmp(png_jmpbuf(png_ptr)))

{

/* Free all of the memory assoc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值