{"id":43,"date":"2007-03-10T00:53:18","date_gmt":"2007-03-09T22:53:18","guid":{"rendered":"http:\/\/www.zagura.ro\/index.php\/2007\/03\/10\/using-libgd-to-manipulate-jpeg-images-in-c\/"},"modified":"2007-03-11T16:32:25","modified_gmt":"2007-03-11T14:32:25","slug":"using-libgd-to-manipulate-jpeg-images-in-c","status":"publish","type":"post","link":"https:\/\/www.zagura.com\/blog\/index.php\/2007\/03\/10\/using-libgd-to-manipulate-jpeg-images-in-c\/","title":{"rendered":"Using libGD to manipulate jpeg images in C"},"content":{"rendered":"<p>Yesterday, for a small project of mine, I needed to implement a small utility to deform jpeg images from command line. The idea was to use texture mapping for deformations, so I needed a way to manipulate the jpeg images at the pixel level.<\/p>\n<p>The first thing I tried was the  <a href=\"http:\/\/www.imagemagick.org\/\">imagemagik<\/a> utility. I&#8217;ve browsed their documentation but I couldn&#8217;t find anything close to what I needed. I wanted to be able to draw textured mapped triangles into a picture. I knew that the <a href=\"http:\/\/www.allegro.cc\/\">Allegro<\/a> library that I&#8217;ve used back in the old DOS programming days could do the texturing part. The bad thing that I found out is that it does not know how to handle jpeg files \ud83d\ude41<\/p>\n<p>Then I googled the Internet for a free and easy to use graphic library and I found <a href=\"http:\/\/www.libgd.org\">libGD<\/a>. The good thing is that it can handle jpeg files. The bad thing is that it cannot do the texturing part. The next thing I had in mind was to use both libraries but this would mean to add lots of dependencies to my small application for a simple texture mapping function. So I decided to write my own texture mapping function to remember the good old days. I will not describe how texture mapping works as there are plenty of tutorials on the internet for this.<br \/>\n<!--more--><br \/>\nHere is how you can use the gd library to process jpeg images:<br \/>\n[c]<br \/>\n#include <gd.h><\/p>\n<p>int main (int argc, char *argv[])<br \/>\n{<br \/>\n    gdImagePtr image;<br \/>\n    FILE *in,*out;\/*open the existing jpeg file *\/<\/p>\n<p>    in = fopen (argv[1], &#8220;rb&#8221;);<br \/>\n    image = gdImageCreateFromJpeg (in);<br \/>\n    fclose (in);<\/p>\n<p>    \/* do the actual image processing<br \/>\n    image->sx and image->sy gives the image resolution<br \/>\n    image->tpixels[y][x] to access the truecolor pixels directly (int RGB)<br \/>\n    *\/<\/p>\n<p>    \/*saving the new jpeg file *\/<br \/>\n    out = fopen (argv[2], &#8220;wb&#8221;);<br \/>\n    gdImageJpeg(image,out,95\/*compression level*\/);<br \/>\n    fclose(out);<br \/>\n}<br \/>\n[\/c]<\/p>\n<p>For more information see the <a href=\"http:\/\/www.libgd.org\/Documentation\">libGD Documentation<\/a>.<br \/>\nHave fun!<br \/>\n<a href='http:\/\/www.zagura.ro\/wp-content\/uploads\/2007\/03\/basescu_distorted.jpg' title='basescu_distorted.jpg'><img src='http:\/\/www.zagura.ro\/wp-content\/uploads\/2007\/03\/basescu_distorted.jpg' alt='basescu_distorted.jpg' \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, for a small project of mine, I needed to implement a small utility to deform jpeg images from command line. The idea was to use texture mapping for deformations, so I needed a way to manipulate the jpeg images at the pixel level. The first thing I tried was the imagemagik utility. I&#8217;ve browsed&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,11,2,10],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/43"}],"collection":[{"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":0,"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}