{"id":716,"date":"2012-01-25T02:46:05","date_gmt":"2012-01-25T02:46:05","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=716"},"modified":"2012-01-25T02:46:05","modified_gmt":"2012-01-25T02:46:05","slug":"working-with-png-files-in-c","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2012\/01\/working-with-png-files-in-c\/","title":{"rendered":"Working with PNG files in C++"},"content":{"rendered":"<p>I&#8217;m working on MacOS X, so first of all I need to compile and install the libraries (libpng and png++):<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#Download here: http:\/\/prdownloads.sourceforge.net\/libpng\/libpng-1.5.7.tar.gz?download\r\ntar xvzf libpng-1.5.7.tar.gz\r\ncd libpng-1.5.7\r\n.\/configure\r\nmake\r\nsudo make install\r\n\r\n#Download png++\r\ncurl http:\/\/download.savannah.nongnu.org\/releases\/pngpp\/png++-0.2.5.tar.gz &gt; .\/png++.tar.gz\r\ntar xzvf png++.tar.gz\r\ncd png++-0.2.5\r\nmake\r\nsudo make install\r\n<\/pre>\n<p>Everything should be installed now, and we can write some code. Create the following C++ program:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;png++\/png.hpp&gt;\r\n\r\nint main() {\r\n\r\n  png::image&lt; png::rgb_pixel &gt; image(&quot;input.png&quot;);\r\n\r\n  for (size_t y = 0; y &lt; image.get_height(); ++y) {\r\n    for (size_t x = 0; x &lt; image.get_width(); ++x) {\r\n      if(((x+y)%2) == 0) image&#x5B;x]&#x5B;y] = png::rgb_pixel(0, 0, 0);\r\n    }\r\n  }\r\n\r\n image.write(&quot;output.png&quot;);\r\n}\r\n<\/pre>\n<p>The program loads a file called input.png, sets every other pixel to black and writes it to output.png. Compile and run as follows:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ng++ test.cpp -lpng\r\n.\/a.out\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m working on MacOS X, so first of all I need to compile and install the libraries (libpng and png++): #Download here: http:\/\/prdownloads.sourceforge.net\/libpng\/libpng-1.5.7.tar.gz?download tar xvzf libpng-1.5.7.tar.gz cd libpng-1.5.7 .\/configure make sudo make install #Download png++ curl http:\/\/download.savannah.nongnu.org\/releases\/pngpp\/png++-0.2.5.tar.gz &gt; .\/png++.tar.gz tar xzvf png++.tar.gz cd png++-0.2.5 make sudo make install Everything should be installed now, and we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-716","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-by","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=716"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/716\/revisions"}],"predecessor-version":[{"id":717,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/716\/revisions\/717"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}