{"id":544,"date":"2011-10-30T17:06:48","date_gmt":"2011-10-30T17:06:48","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=544"},"modified":"2011-10-30T19:05:24","modified_gmt":"2011-10-30T19:05:24","slug":"simple-libtiff-example","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2011\/10\/simple-libtiff-example\/","title":{"rendered":"Simple libtiff Example"},"content":{"rendered":"<p>First, you have to install libtiff. On Linux it&#8217;s almost certainly in your repository you should install a package with an name similar to libtiff-dev.<\/p>\n<p>On MacOS X I did the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncurl ftp:\/\/ftp.remotesensing.org\/pub\/libtiff\/tiff-3.9.5.zip &gt; tiff-3.9.5.zip\r\ntar xvzf tiff-3.9.5.zip\r\ncd tiff-3.9.5\r\n.\/configure\r\nmake\r\nsudo make install\r\n<\/pre>\n<p>Then create the following cpp file:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &quot;tiffio.h&quot;\r\n#include &lt;iostream&gt;\r\n\r\nusing namespace std;\r\n\r\nmain(int argc, char* argv&#x5B;])\r\n{\r\n\r\n  TIFF* tif = TIFFOpen(argv&#x5B;1], &quot;r&quot;);\r\n  if (tif) {\r\n    uint32 w, h;\r\n    size_t npixels;\r\n    uint32* raster;\r\n\r\n    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &amp;w);\r\n    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &amp;h);\r\n    npixels = w * h;\r\n\r\n    raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));\r\n    if (raster != NULL) {\r\n      if (TIFFReadRGBAImage(tif, w, h, raster, 0)) {\r\n\r\n\r\n        cout &lt;&lt; &quot;P2&quot; &lt;&lt; endl;\r\n        cout &lt;&lt; w &lt;&lt; &quot; &quot; &lt;&lt; h &lt;&lt; endl;\r\n        cout &lt;&lt; 255 &lt;&lt; endl;\r\n\r\n        for(size_t c_h=0;c_h&lt;h;c_h++) {\r\n          for(size_t c_w=0;c_w&lt;w;c_w++) {\r\n            cout &lt;&lt; raster&amp;#91;(w*h)-((c_h*w)+(w-c_w))&amp;#93;%256 &lt;&lt; &quot; &quot;;\r\n          }\r\n          cout &lt;&lt; endl;\r\n        }\r\n\r\n      }\r\n      _TIFFfree(raster);\r\n    }\r\n    TIFFClose(tif);\r\n  }\r\n}\r\n&amp;#91;\/sourcecode&amp;#93;\r\n\r\nThis program converts a tiff file to a PGM (portable grey map) file. PGM is a simple text based format, this makes it very easy to debug. You can read more about PGM on &lt;a href=&quot;http:\/\/en.wikipedia.org\/wiki\/Portable_graymap&quot;&gt;wikipedia&lt;\/a&gt;.\r\n\r\nNote: It&#039;s not converting the RGB value to grey very well it&#039;s simply modding the value for the example.\r\n\r\nCompile the program as follow (on MacOS you&#039;ll need to have install XCode or another gcc version):\r\n\r\n&#x5B;sourcecode language=&quot;bash&quot;]\r\ng++ tiffsimple.cpp -ltiff\r\n<\/pre>\n<p>Then run it as:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n.\/a.out inputfile.tif\r\n<\/pre>\n<p>You can view the resulting PGM file with ImageMagick.<\/p>\n<h2>Notes<\/h2>\n<p>You can also transform the TIFF coordinates rather than performing the transformation yourself when converting to PGM as follows:<\/p>\n<p>#include &#8220;tiffio.h&#8221;<br \/>\n#include <iostream><\/p>\n<p>using namespace std;<\/p>\n<p>main(int argc, char* argv[])<br \/>\n{<\/p>\n<p>  TIFF* tif = TIFFOpen(argv[1], &#8220;r&#8221;);<br \/>\n  if (tif) {<br \/>\n    uint32 w, h;<br \/>\n    size_t npixels;<br \/>\n    uint32* raster;<\/p>\n<p>    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &#038;w);<br \/>\n    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &#038;h);<br \/>\n    npixels = w * h;<\/p>\n<p>    raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));<br \/>\n    if (raster != NULL) {<br \/>\n      if (TIFFReadRGBAImageOriented(tif, w, h, raster,ORIENTATION_TOPLEFT, 0)) {<\/p>\n<p>        cout << \"P2\" << endl;\n        cout << w << \" \" << h << endl;\n        cout << 255 << endl;\n\n        for(size_t c_h=0;c_h<h;c_h++) {\n          for(size_t c_w=0;c_w<w;c_w++) {\n     \/\/       cout << raster[(w*h)-((c_h*w)+(w-c_w))]%256 << \" \";\n            cout << raster[(c_h*w)+c_w]%256 << \" \";\n          }\n          cout << endl;\n        }\n\n      }\n      _TIFFfree(raster);\n    }\n    TIFFClose(tif);\n  }\n}\n[\/sourcecode]\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First, you have to install libtiff. On Linux it&#8217;s almost certainly in your repository you should install a package with an name similar to libtiff-dev. On MacOS X I did the following: curl ftp:\/\/ftp.remotesensing.org\/pub\/libtiff\/tiff-3.9.5.zip &gt; tiff-3.9.5.zip tar xvzf tiff-3.9.5.zip cd tiff-3.9.5 .\/configure make sudo make install Then create the following cpp file: #include &quot;tiffio.h&quot; #include [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_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},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[16,15],"class_list":["post-544","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-libtiff"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-8M","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/544","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=544"}],"version-history":[{"count":4,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/544\/revisions"}],"predecessor-version":[{"id":547,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/544\/revisions\/547"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}