#include <gtk/gtk.h>

int main (int argc, char *argv[])
{
	GdkPixbuf *pbuf;
	GtkWidget *window;
	GtkWidget *image;
	GError    *error = NULL;
	GdkPixbufLoader *loader;
	FILE      *infile;
	guchar    *buffer;
	size_t	   readb;

	gtk_init (&argc, &argv);

	if (argc < 2)
		return 1;

	loader = gdk_pixbuf_loader_new ();
	infile = fopen (argv[1], "rb");
	buffer = g_new0 (guchar, 60000);
	while (!feof (infile)) {
		g_message("Read");
		readb = fread (buffer, sizeof (guchar), 60000, infile);
		gdk_pixbuf_loader_write (loader, buffer, readb, &error);
	}
	if (error)
	{
		g_critical ("Error: %s", error->message);
		g_error_free (error);
		return -1;
	}
	gdk_pixbuf_loader_close (loader, &error);
	fclose (infile);
	if (error == NULL) {
		pbuf = gdk_pixbuf_loader_get_pixbuf (loader);
		g_assert (pbuf != NULL);
		image = gtk_image_new_from_pixbuf (pbuf);
		g_object_unref (pbuf);
	} else {
		g_critical ("Error while loading image: %s", error->message);
		g_error_free (error);
	}
	g_message ("Image loaded");
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

	gtk_container_add (GTK_CONTAINER (window), image);
	gtk_widget_show_all (window);

	g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);

	gtk_main ();
	g_free (buffer);
	return 0;
}
