Tuesday, July 12, 2011

CREATE NEGATIVE FROM PNG IMAGE

0 comments
HI,here we will discuss about how we can create a negative image from png image by using C program.The source code of C Program is given below.Just try it.

SOURCE CODE
/*CREATE NEGATIVE FROM PNG IMAGE*/
#include <stdio.h>
#include <error.h>
#include <gd.h>

int main(int argc, char *argv[]) {
 FILE *fp = {0};
 gdImagePtr img;
 char *iname = NULL;
 char *oname = NULL;
 int color, x, y, w, h;
 int red, green, blue;

 color = x = y = w = h = 0;
 red = green = blue = 0;

 if(argc != 3)
  error(1, 0, "Usage: gdnegat input.png output.png");
 else {
  iname = argv[1];
  oname = argv[2];
 }

 if((fp = fopen(iname, "r")) == NULL)
  error(1, 0, "Error - fopen(): %s", iname);
 else
  img = gdImageCreateFromPng(fp);

 w = gdImageSX(img);
 h = gdImageSY(img);

 for(x = 0; x < w; x++) {
  for(y = 0; y < h; y++) {
   color = gdImageGetPixel(img, x, y);

   red   = 255 - gdImageRed(img, color);
   green = 255 - gdImageGreen(img, color);
   blue  = 255 - gdImageBlue(img, color);

   color = gdImageColorAllocate(img, red, green, blue);
   gdImageSetPixel(img, x, y, color);
  }
 }

 if((fp = fopen(oname, "w")) == NULL)
  error(1, 0, "Error - fopen(): %s", oname);
 else {
  gdImagePng(img, fp);
  fclose(fp);
 }

 gdImageDestroy(img);
 return 0;
}


Leave a Reply