#include<gtk/gtk.h>
#include<glib/gprintf.h>
#include<string.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;
}
GtkWidget* get_image(const gchar *filename)
{
GtkWidget *im;
GdkPixbuf *pb;
gchar *savefile,*file;
pb=gdk_pixbuf_new_from_file(filename,NULL);
colorinvert_picture(pb);
//ignore the extension and save as jpeg.
file=g_strdup(filename);
savefile=strchr(file,'.');
if(savefile!=NULL)
*savefile='\0';
savefile=g_strdup_printf("%s%s",file,"_colorinvert.jpeg");
gdk_pixbuf_save(pb,savefile,"jpeg",NULL,NULL);//save the file
im=gtk_image_new_from_pixbuf(pb);
gdk_pixbuf_unref(pb);
g_free(savefile);
g_free(file);
return im;
}
int main(int argc,char *argv[])
{
GtkWidget *w,*vb,*b;
gchar *file="kathakali.jpeg";
gtk_init(&argc,&argv);
w=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(w),200,200);
b=gtk_button_new_with_mnemonic("_Exit");
vb=gtk_vbox_new(FALSE,FALSE);
gtk_container_add(GTK_CONTAINER(w),vb);
gtk_box_pack_start_defaults(GTK_BOX(vb),get_image(file));
gtk_box_pack_start_defaults(GTK_BOX(vb),gtk_hseparator_new());
gtk_box_pack_start_defaults(GTK_BOX(vb),gtk_image_new_from_file(file));
gtk_box_pack_start_defaults(GTK_BOX(vb),b);//exit button.
gtk_signal_connect(GTK_OBJECT(w),"destroy",GTK_SIGNAL_FUNC(gtk_main_quit),NULL);
gtk_signal_connect(GTK_OBJECT(b),"clicked",GTK_SIGNAL_FUNC(gtk_main_quit),NULL);
gtk_widget_show_all(w);
gtk_main();
return 0;
}
//compilation lines
//gcc -Wall example_colorinvert.c `pkg-config libgnomeui-2.0 --cflags --libs`