/*
    Compile with:
gcc -Wall -o gdkpixbuftest gdkpixbuftest.c $(pkg-config gtk+-2.0 --cflags --libs)
*/

#include <gtk/gtk.h>

GdkPixbuf *
gdk_pixbuf_new_from_buffer (const guchar *buffer, gsize length, GError **error)
{
    GdkPixbufLoader *loader;
    GdkPixbuf *pixbuf;

    g_return_val_if_fail (buffer != NULL, NULL);
    g_return_val_if_fail (length > 0, NULL);

    loader = gdk_pixbuf_loader_new ();
    if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
        g_object_unref (loader);
        return NULL;
    }
    if (!gdk_pixbuf_loader_close (loader, error)) {
        g_object_unref (loader);
        return NULL;
    }
    pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
    g_object_ref (pixbuf);
    g_object_unref (loader);
    return pixbuf;
}


int main (int argc, char *argv[])
{
    GdkPixbuf *pixbuf;
    gchar *filename;
    gchar *contents;
    gsize length;
    GError *error;

    gtk_init (&argc, &argv);
    if (argc != 2) {
        fprintf (stderr, "Please, specify the file to load as parameter!\n");
        return 1;
    }
    filename = argv[1];
    error = NULL;
    if (!g_file_get_contents (filename, &contents, &length, &error)) {
        fprintf (stderr, "Cannot read file: %s.\n", error->message);
        g_error_free (error);
        return 2;
    }
    pixbuf = gdk_pixbuf_new_from_buffer ((guchar*)contents, length, &error);
    if (!pixbuf) {
        fprintf (stderr, "Cannot get pixbuf from memory buffer: %s.\n",
            error->message);
        g_error_free (error);
        return 3;
    }
    g_object_unref (pixbuf);
    fprintf (stdout, "All went ok!\n");
    return 0;
}

