next up previous contents
Next: Colour Masking. Up: Image Processing using GTK. Previous: Digital Image Processing   Contents

Colour Inverting:

This is the simple operation of making every pixel take its 1's complement. This in effect creates the negative of the image given. We can do this by loading each pixel and doing the following transformation.

   	      pixel.red   =255-pixel.red;
	      pixel.green =255-pixel.green;
	      pixel.blue  =255-pixel.blue;

Accessing the image pixels as shown in the above code section [*] we use the transformation inside the loop. This code below in sec [*] yielded this image.

Figure: Kathakali, colour inverted.
Image kathakali_colorinvert

#include<gtk/gtk.h>

void colorinvert_picture(GdkPixbuf *pb)
{ 
  int ht,wt;
  int i=0,j=0;
  int rowstride=0;  
  int bpp=0;
  gchar *pixel;


  if(gdk_pixbuf_get_bits_per_sample(pb)!=8)   //we handle only 24 bit images.
  	return;                               //look at 3 bytes per pixel.

  bpp=3;	         	  //getting attributes of height,
  ht=gdk_pixbuf_get_height(pb);   //width, and bpp.Also get pointer
  wt=gdk_pixbuf_get_width(pb);	  //to pixels.
  pixel=gdk_pixbuf_get_pixels(pb);
  rowstride=wt*bpp;

  for(i=0;i<ht;i++)		//iterate over the height of image.
    for(j=0;j<rowstride;j+=bpp)   //read every pixel in the row.skip
				//bpp bytes 
      {	
      
      //access pixel[i][j] as
      // pixel[i*rowstride + j]

      //access red,green and blue as
	pixel[i*rowstride + j+0]=255-pixel[i*rowstride + j+0];
	pixel[i*rowstride + j+1]=255-pixel[i*rowstride + j+1];
	pixel[i*rowstride + j+2]=255-pixel[i*rowstride + j+2];	      
      }  
  return;
}

% <!-- provide link here-->
% complete working example with glue code in example_colorinvert.c



Muthiah 2004-06-04