{"id":390,"date":"2012-10-08T14:04:05","date_gmt":"2012-10-08T14:04:05","guid":{"rendered":"http:\/\/blog.zhukunqian.com\/?p=390"},"modified":"2012-10-08T14:18:45","modified_gmt":"2012-10-08T14:18:45","slug":"libpng%e8%af%bb%e5%8f%96png%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/blog.zhukunqian.com\/?p=390","title":{"rendered":"libpng\u8bfb\u53d6png\u56fe\u7247"},"content":{"rendered":"<p>\u8fd9\u662f\u4ece\u7f51\u4e0a\u627e\u7684\u4ee3\u7801\uff0c\u4f46\u6709\u4ee5\u4e0b\u4e24\u4e2a\u7f3a\u9677\uff1a<\/p>\n<p>1\u3001\u4e0d\u652f\u6301\u4ece\u5185\u5b58\u4e2d\u89e3\u6790png\u56fe\u7247\u3002<\/p>\n<p>2\u3001\u751f\u6210\u7684\u56fe\u7247\u6570\u636e\u4e2d\u4e0d\u5305\u542balpha\u6570\u636e\uff0c\u65e2\u4f7fpng\u56fe\u4e2d\u5e26\u6709alpha\u6570\u636e\u3002<\/p>\n<p>\u7ee7\u7eed\u67e5\u627ecximage\u79fb\u690dndk.<\/p>\n<p>unsigned char* readpng(const char* file, png_uint_32* w, png_uint_32* h,<br \/>\nbool* hasAlpha) {<br \/>\nLOGI(&#8220;readPng 1&#8221;);<br \/>\nFILE* f;<br \/>\nunsigned char sig[8];<br \/>\npng_structp png_ptr;<br \/>\npng_infop info_ptr;<br \/>\nunsigned char* image_data;<br \/>\nint bit_depth;<br \/>\nint color_type;<br \/>\nunsigned int rowbytes;<br \/>\npng_uint_32 i;<br \/>\npng_bytepp row_pointers;<br \/>\nLOGI(&#8220;readPng 2&#8221;);<br \/>\nif ((f = fopen(file, &#8220;rb&#8221;)) == NULL)<br \/>\nreturn NULL;<br \/>\nfread(sig, sizeof(*sig), sizeof(sig), f);<br \/>\n\/\/\u00a0\u00a0\u00a0 if (!png_check_sig(sig, sizeof(*sig))) {<br \/>\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fclose(f);<br \/>\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return NULL;<br \/>\n\/\/\u00a0\u00a0\u00a0 }<br \/>\nLOGI(&#8220;readPng 3&#8221;);<br \/>\nif ((png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,<br \/>\nNULL)) == NULL) {<br \/>\nfclose(f);<br \/>\nreturn NULL;<br \/>\n}<br \/>\nif ((info_ptr = png_create_info_struct(png_ptr)) == NULL) {<br \/>\npng_destroy_read_struct(&amp;png_ptr, NULL, NULL);<br \/>\nfclose(f);<br \/>\nreturn NULL;<br \/>\n}<br \/>\nLOGI(&#8220;readPng 4&#8221;);<br \/>\nif (setjmp(png_jmpbuf(png_ptr))) {<br \/>\npng_destroy_read_struct(&amp;png_ptr, &amp;info_ptr, NULL);<br \/>\nfclose(f);<br \/>\nreturn NULL;<br \/>\n}<br \/>\nLOGI(&#8220;readPng 4.1&#8221;);<br \/>\n\/\/\u00a0\u00a0\u00a0 png_ptr-&gt;io_ptr = (png_voidp) f;<br \/>\npng_init_io(png_ptr, f); \/\/ fuck,\u52a0\u4e0a\u8fd9\u4e00\u53e5\u5c31OK\u4e86<br \/>\npng_set_sig_bytes(png_ptr, 8);<br \/>\nLOGI(&#8220;readPng 4.2&#8221;);<br \/>\npng_read_info(png_ptr, info_ptr);<br \/>\nLOGI(&#8220;readPng 4.3&#8221;);<br \/>\npng_get_IHDR(png_ptr, info_ptr, w, h, &amp;bit_depth, &amp;color_type, NULL, NULL,<br \/>\nNULL);<br \/>\nLOGI(&#8220;readPng 5&#8221;);<br \/>\nif (color_type &amp; PNG_COLOR_MASK_ALPHA) {<br \/>\npng_set_strip_alpha(png_ptr);<br \/>\n*hasAlpha = true;<br \/>\n}<br \/>\nif (bit_depth == 16)<br \/>\npng_set_strip_16(png_ptr);<br \/>\nif (color_type == PNG_COLOR_TYPE_GRAY || color_type<br \/>\n== PNG_COLOR_TYPE_GRAY_ALPHA)<br \/>\npng_set_gray_to_rgb(png_ptr);<br \/>\nif (color_type == PNG_COLOR_TYPE_PALETTE)<br \/>\npng_set_palette_to_rgb(png_ptr);<br \/>\npng_read_update_info(png_ptr, info_ptr);<\/p>\n<p>LOGI(&#8220;readPng 6&#8221;);<br \/>\nrowbytes = png_get_rowbytes(png_ptr, info_ptr);<br \/>\nif ((image_data = (unsigned char *) malloc(*h * rowbytes)) == NULL) {<br \/>\npng_destroy_read_struct(&amp;png_ptr, &amp;info_ptr, NULL);<br \/>\nfclose(f);<br \/>\nreturn NULL;<br \/>\n}<br \/>\nif ((row_pointers = (png_bytepp) malloc(*h * sizeof(png_bytep))) == NULL) {<br \/>\npng_destroy_read_struct(&amp;png_ptr, &amp;info_ptr, NULL);<br \/>\nfclose(f);<br \/>\nfree(image_data);<br \/>\nreturn NULL;<br \/>\n}<\/p>\n<p>for (i = 0; i &lt; *h; i++)<br \/>\nrow_pointers[*h &#8211; 1 &#8211; i] = image_data + i * rowbytes;<br \/>\npng_read_image(png_ptr, row_pointers);<br \/>\nfree(row_pointers);<br \/>\npng_destroy_read_struct(&amp;png_ptr, &amp;info_ptr, NULL);<br \/>\nfclose(f);<\/p>\n<p>\/\/\u00a0\u00a0 \u00a0FILE* f2=fopen(&#8220;\/sdcard\/2.png&#8221;, &#8220;w&#8221;);<br \/>\n\/\/\u00a0\u00a0 \u00a0fwrite(image_data,*h * rowbytes,1,f2);<br \/>\n\/\/\u00a0\u00a0 \u00a0fclose(f2);<br \/>\nreturn image_data;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4ece\u7f51\u4e0a\u627e\u7684\u4ee3\u7801\uff0c\u4f46\u6709\u4ee5\u4e0b\u4e24\u4e2a\u7f3a\u9677\uff1a 1\u3001\u4e0d\u652f\u6301\u4ece\u5185\u5b58\u4e2d\u89e3\u6790png\u56fe\u7247\u3002 2\u3001 &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/blog.zhukunqian.com\/?p=390\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/390"}],"collection":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=390"}],"version-history":[{"count":3,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/390\/revisions"}],"predecessor-version":[{"id":392,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/390\/revisions\/392"}],"wp:attachment":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}