当前位置:   article > 正文

LIBPNG读写PNG图像_png_get_text

png_get_text
  1. //file:pngtest.c
  2. //changed from the libpng,对照libpng中源码阅读
  3. //myers
  4. #include "png.h"
  5. #include <stdio.h>
  6. #include "zlib.h"
  7. #include "png.h"
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. int main(int argc,char* argv[])
  12. {
  13. static png_FILE_p fpin;
  14. static png_FILE_p fpout;
  15. //输入文件名
  16. char *inname = "/home/mingming/graph/1.png";
  17. char *outname = "/home/mingming/graph/1_like.png";
  18. //读:
  19. png_structp read_ptr;
  20. png_infop read_info_ptr, end_info_ptr;
  21. //写
  22. png_structp write_ptr;
  23. png_infop write_info_ptr,write_end_info_ptr;
  24. //
  25. png_bytep row_buf;
  26. png_uint_32 y;
  27. int num_pass, pass;
  28. png_uint_32 width, height;//宽度,高度
  29. int bit_depth, color_type;//位深,颜色类型
  30. int interlace_type, compression_type, filter_type;//扫描方式,压缩方式,滤波方式
  31. //读
  32. row_buf = NULL;
  33. //打开读文件
  34. if ((fpin = fopen(inname, "rb")) == NULL)
  35. {
  36. fprintf(stderr,"Could not find input file %s\n", inname);
  37. return (1);
  38. }
  39. //打开写文件
  40. if ((fpout = fopen(outname, "wb")) == NULL)
  41. {
  42. printf("Could not open output file %s\n", outname);
  43. fclose(fpin);
  44. return (1);
  45. }
  46. //我们这里不处理未知的块unknown chunk
  47. //初始化1
  48. read_ptr =
  49. png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  50. write_ptr =
  51. png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  52. read_info_ptr = png_create_info_struct(read_ptr);
  53. end_info_ptr = png_create_info_struct(read_ptr);
  54. write_info_ptr = png_create_info_struct(write_ptr);
  55. write_end_info_ptr = png_create_info_struct(write_ptr);
  56. //初始化2
  57. png_init_io(read_ptr, fpin);
  58. png_init_io(write_ptr, fpout);
  59. //读文件有high level(高层)和low level两种,我们选择从底层具体信息中读取。
  60. //这里我们读取所有可选。
  61. png_read_info(read_ptr, read_info_ptr);
  62. //(1)IHDR
  63. //读取图像宽度(width),高度(height),位深(bit_depth),颜色类型(color_type),压缩方法(compression_type)
  64. //滤波器方法(filter_type),隔行扫描方式(interlace_type)
  65. if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
  66. &color_type, &interlace_type, &compression_type, &filter_type))
  67. {
  68. //我们采用默认扫描方式
  69. png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
  70. color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
  71. }
  72. //(2)cHRM
  73. //读取白色度信息 白/红/绿/蓝 点的x,y坐标,这里采用整形,不采用浮点数
  74. png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,blue_y;
  75. if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
  76. &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y))
  77. {
  78. png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
  79. red_y, green_x, green_y, blue_x, blue_y);
  80. }
  81. //(3)gAMA
  82. png_fixed_point gamma;
  83. if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
  84. png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
  85. //(4)iCCP
  86. png_charp name;
  87. png_bytep profile;
  88. png_uint_32 proflen;
  89. if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
  90. &profile, &proflen))
  91. {
  92. png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
  93. profile, proflen);
  94. }
  95. //(5)sRGB
  96. int intent;
  97. if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
  98. png_set_sRGB(write_ptr, write_info_ptr, intent);
  99. //(7)PLTE
  100. png_colorp palette;
  101. int num_palette;
  102. if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
  103. png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
  104. //(8)bKGD
  105. png_color_16p background;
  106. if (png_get_bKGD(read_ptr, read_info_ptr, &background))
  107. {
  108. png_set_bKGD(write_ptr, write_info_ptr, background);
  109. }
  110. //(9)hist
  111. png_uint_16p hist;
  112. if (png_get_hIST(read_ptr, read_info_ptr, &hist))
  113. png_set_hIST(write_ptr, write_info_ptr, hist);
  114. //(10)oFFs
  115. png_int_32 offset_x, offset_y;
  116. int unit_type;
  117. if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
  118. &unit_type))
  119. {
  120. png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
  121. }
  122. //(11)pCAL
  123. png_charp purpose, units;
  124. png_charpp params;
  125. png_int_32 X0, X1;
  126. int type, nparams;
  127. if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
  128. &nparams, &units, ¶ms))
  129. {
  130. png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
  131. nparams, units, params);
  132. }
  133. //(12)pHYs
  134. png_uint_32 res_x, res_y;
  135. if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
  136. png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
  137. //(13)sBIT
  138. png_color_8p sig_bit;
  139. if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
  140. png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
  141. //(14)sCAL
  142. int unit;
  143. png_charp scal_width, scal_height;
  144. if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
  145. &scal_height))
  146. {
  147. png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
  148. scal_height);
  149. }
  150. //(15)iTXt
  151. png_textp text_ptr;
  152. int num_text;
  153. if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
  154. {
  155. png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
  156. }
  157. //(16)tIME,这里我们不支持RFC1123
  158. png_timep mod_time;
  159. if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
  160. {
  161. png_set_tIME(write_ptr, write_info_ptr, mod_time);
  162. }
  163. //(17)tRNS
  164. png_bytep trans_alpha;
  165. int num_trans;
  166. png_color_16p trans_color;
  167. if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
  168. &trans_color))
  169. {
  170. int sample_max = (1 << bit_depth);
  171. /* libpng doesn't reject a tRNS chunk with out-of-range samples */
  172. if (!((color_type == PNG_COLOR_TYPE_GRAY &&
  173. (int)trans_color->gray > sample_max) ||
  174. (color_type == PNG_COLOR_TYPE_RGB &&
  175. ((int)trans_color->red > sample_max ||
  176. (int)trans_color->green > sample_max ||
  177. (int)trans_color->blue > sample_max))))
  178. png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
  179. trans_color);
  180. }
  181. //写进新的png文件中
  182. png_write_info(write_ptr, write_info_ptr);
  183. //读真正的图像数据
  184. num_pass = 1;
  185. for (pass = 0; pass < num_pass; pass++)
  186. {
  187. for (y = 0; y < height; y++)
  188. {
  189. //分配内存
  190. row_buf = (png_bytep)png_malloc(read_ptr,
  191. png_get_rowbytes(read_ptr, read_info_ptr));
  192. png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
  193. png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
  194. png_free(read_ptr, row_buf);
  195. row_buf = NULL;
  196. }
  197. }
  198. //结束
  199. png_read_end(read_ptr, end_info_ptr);
  200. //
  201. //tTXt
  202. if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
  203. {
  204. png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
  205. }
  206. //tIME
  207. if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
  208. {
  209. png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
  210. }
  211. //
  212. png_write_end(write_ptr, write_end_info_ptr);
  213. //回收
  214. png_free(read_ptr, row_buf);
  215. row_buf = NULL;
  216. png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
  217. png_destroy_info_struct(write_ptr, &write_end_info_ptr);
  218. png_destroy_write_struct(&write_ptr, &write_info_ptr);
  219. //
  220. fclose(fpin);
  221. fclose(fpout);
  222. //测试,比较两个PNG文件是否相同
  223. if ((fpin = fopen(inname, "rb")) == NULL)
  224. {
  225. printf("Could not find file %s\n", inname);
  226. return (1);
  227. }
  228. if ((fpout = fopen(outname, "rb")) == NULL)
  229. {
  230. printf("Could not find file %s\n", outname);
  231. fclose(fpin);
  232. return (1);
  233. }
  234. char inbuf[256], outbuf[256];
  235. for (;;)
  236. {
  237. png_size_t num_in, num_out;
  238. num_in = fread(inbuf, 1, 1, fpin);
  239. num_out = fread(outbuf, 1, 1, fpout);
  240. if (num_in != num_out)
  241. {
  242. printf("\nFiles %s and %s 大小不同\n",
  243. inname, outname);
  244. fclose(fpin);
  245. fclose(fpout);
  246. return (0);
  247. }
  248. if (!num_in)
  249. break;
  250. if (memcmp(inbuf, outbuf, num_in))
  251. {
  252. printf("\nFiles %s and %s 内容不同\n", inname, outname);
  253. fclose(fpin);
  254. fclose(fpout);
  255. return (0);
  256. }
  257. }
  258. fclose(fpin);
  259. fclose(fpout);
  260. return (0);
  261. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号