高分求助!!

zw1270644192 2011-11-29 04:07:33
现要使用libpng库做一个png图片的解码 谁有源代码 给发一下 邮箱是1270644192@qq.com 问题解决立马结贴 那位大虾帮助一下吧
...全文
94 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zw1270644192 2011-11-30
  • 打赏
  • 举报
回复
谁给说说libpng怎么用啊
zw1270644192 2011-11-30
  • 打赏
  • 举报
回复
谁给说说图片解码是一个什么样的过程啊
zw1270644192 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qscool1987 的回复:]
正在写一个图像数据传输协议,规格书看的我头昏
[/Quote]
与君共勉
qscool1987 2011-11-29
  • 打赏
  • 举报
回复

正在写一个图像数据传输协议,规格书看的我头昏
zw1270644192 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dahuaixiaohuai 的回复:]
http://zcg9033586.blog.163.com/blog/static/175217310201181410184350/
图像解码之二——使用libpng解码png图片 2011-09-14 22:18:43| 分类: Linux网络编程 | 标签: |字号大

小 订阅
png图片应用也非常广泛,本文将会简单介绍怎样使用开源libpng库解码png图片。
libpn……
[/Quote]

有没有源代码供参考一下 这讲得有点片面 看了还是不知道怎么整啊
一叶之舟 2011-11-29
  • 打赏
  • 举报
回复
http://zcg9033586.blog.163.com/blog/static/175217310201181410184350/
图像解码之二——使用libpng解码png图片 2011-09-14 22:18:43| 分类: Linux网络编程 | 标签: |字号大

小 订阅
png图片应用也非常广泛,本文将会简单介绍怎样使用开源libpng库解码png图片。
libpng的数据结构如下
系列相关:
1 图像解码之一—使用libjpeg解码jpeg图片
2 图像解码之二——使用libpng解码png图片
3 图像解码之三——giflib解码gif图片
png_structp变量是在libpng初始化的时候创建,由libpng库内部使用,代表libpng的是调用上下文,库的使用者不应该对这个变量进行访问。调用libpng的API的时候,需要把这个参数作为第一个参数传入。

png_infop变量,初始化完成libpng之后,可以从libpng中获得该类型变量指针。这个变量保存了png图片数据的信息,库的使用者可以修改和查阅该变量,比如:查阅图片信息,修改图片解码参数。在早期的版本中直接访问该变量的成员,最新的版本建议是通过API来访问这些成员。
libpng的使用
0、判断是否为libpng数据

这步是可选的,在利用libpng继续数据处理之前,可以调用png_sig_cmp函数来检查是否为png数据,请参阅libpng手册了解详细内容。
1、初始化libpng

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 /* 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);

初始化libpng的时候,用户可以指定自定义错误处理函数,如果不需要指定自定义错误处理函数,则传NULL即可。png_create_read_struct函数返回一个png_structp变量,前面已经提到该变量不应该被用户访问,应该在以后调用libpng的函数时传递给libpng库。

如果你需要提供自定义内存管理模块则需要调用png_create_read_struct_2来完成对libpng的初始化:

1 2 3 4 5 6 7 png_structp png_ptr = png_create_read_struct_2 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, user_error_fn, user_warning_fn, (png_voidp) user_mem_ptr, user_malloc_fn, user_free_fn)

2、创建图像信息——png_infop变量

1 2 3 4 5 6 7 8 9 10 11 12 13 /* Allocate/initialize the memory for image information. REQUIRED. */ info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL); return (ERROR); }

如前面所说,用户将会通过png_infop变量来获得图片的信息,设置图片解码参数等。
3、设置错误返回点

上文libjpeg解码jpeg图片中提到用setjmp/longjmp函数来处理异常。libpng库默认集成这种机制来完成异常处理,如下代码初始化错误返回点:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /* 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 associated with the png_ptr and info_ptr */ png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); /* If we get here, we had a problem reading the file */ return (ERROR); }

正如上面注释中提到情况,只有在初始化libpng的时候未指定用户自定义的错误处理函数情况下,才需要设置错误返回点。如果设置了用户自定义的错误处理函数,libpng将会调用用户自定义错误处理函数,而不会返回到这个调用点。

当libpng库出现错误的时候,libpng将会自动调用longjmp函数返回到这个点。在这个点我们可以进行必要的清理工作。
4、设置libpng的数据源

我在上文《图像解码之一——使用libjpeg解码jpeg图片》中提到,一个好的代码库应该能够运行用户输入各式各样的数据,而不能把输入数据定死。libpng在这方面做得非常的好,它提供了默认的文件输入流的支持,并且提供了用户自定义回调函数来完成png数据的输入。

对于文件流数据数据设置代码如下:

1 2 3 4 5 6 7 /* One of the following I/O initialization methods is REQUIRED */ def streams /* PNG file I/O method 1 */ /* Set up the input control if you are using standard C streams */ png_init_io(png_ptr, fp);

用户自定义回调函数设置libpng数据源的代码如下:

1 2 3 4 5 6 7 8 9 /* If you are using replacement read functions, instead of calling * png_init_io() here you would call: */ png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn); /* where user_io_ptr is a structure you want available to the callbacks */

如果你已经使用png_sig_cmp函数来检查了png数据,需要调用png_set_sig_bytes函数来告诉libpng库,这样库处理数据的时候将会跳过相应的数据,具体请参考libpng手册。
5、png图像处理

这步有两种设置方案一种称为高层处理,一种称为底层处理。
高层处理

当用户的内存足够大,可以一次性读入所有的png数据,并且输出数据格式为如下libpng预定义数据类型时,可以用高层函数,下libpng预定义数据类型为:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 PNG_TRANSFORM_IDENTITY No transformation PNG_TRANSFORM_STRIP_16 Strip 16-bit samples to 8 bits PNG_TRANSFORM_STRIP_ALPHA Discard the alpha channel PNG_TRANSFORM_PACKING Expand 1, 2 and 4-bit samples to bytes PNG_TRANSFORM_PACKSWAP Change order of packed pixels to LSB first PNG_TRANSFORM_EXPAND Perform set_expand() PNG_TRANSFORM_INVERT_MONO Invert monochrome images PNG_TRANSFORM_SHIFT Normalize pixels to the sBIT depth PNG_TRANSFORM_BGR Flip RGB to BGR, RGBA to BGRA PNG_TRANSFORM_SWAP_ALPHA Flip RGBA to ARGB or GA to AG PNG_TRANSFORM_INVERT_ALPHA Change alpha from opacity to transparency PNG_TRANSFORM_SWAP_ENDIAN Byte-swap 16-bit samples PNG_TRANSFORM_GRAY_TO_RGB Expand grayscale samples to RGB (or GA to RGBA)

高层读取函数如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /* * If you have enough memory to read in the entire image at once, * and you need to specify only transforms that can be controlled * with one of the PNG_TRANSFORM_* bits (this presently excludes * dithering, filling, setting background, and doing gamma * adjustment), then you can read the entire image (including * pixels) into the info structure with this call: */ png_read_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL);

该函数将会把所有的图片数据解码到info_ptr数据结构中。png_transforms为整型参数,为上面libpng预定义的数据类型进行or操作得到。调用了该函数,就不可以再调用png_set_transform函数来设置输出数据
zw1270644192 2011-11-29
  • 打赏
  • 举报
回复
大牛都去哪了啊 给解决一下啊

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧