#include<gtk/gtk.h>
#include<glib/gprintf.h>
#include<string.h>
#define swap(x,y) {int temp;temp=x;x=y;y=temp;}
void flip_picture(GdkPixbuf *pb) //L-R swap
{
int ht,wt;
int i=0,j=0;
int rowstride=0;
gchar *pixel;
ht=gdk_pixbuf_get_height(pb);
wt=gdk_pixbuf_get_width(pb);
pixel=gdk_pixbuf_get_pixels(pb);
rowstride=gdk_pixbuf_get_rowstride(pb);
for(i=0;i<ht;i++) //half the width
for(j=0;j<(rowstride)/2;j+=3)
{
swap(pixel[i*rowstride+j+0],pixel[i*rowstride+(rowstride-j)+0]);
swap(pixel[i*rowstride+j+1],pixel[i*rowstride+(rowstride-j)+1]);
swap(pixel[i*rowstride+j+2],pixel[i*rowstride+(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);
flip_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,"_pictureflip.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_imageflip.c `pkg-config libgnomeui-2.0 --cflags --libs`