/* gcc gegl-buffer-1.c $(pkg-config --cflags --libs gegl-0.3
   gdk-pixbuf-2.0 babl glib-2.0) */

#include <babl/babl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include <glib.h>

static const gchar *FILE_PATH = "/home/rishi/Pictures/ride-to-kromeriz/darktable_exported/IMG_0631.jpg";

gint
main (void)
{
  const Babl *format;
  GeglNode *graph;
  GeglNode *translate;
  GeglNode *src, *sink;
  GeglBuffer *buffer = NULL;
  GeglProcessor *processor;
  GdkPixbuf *pixbuf = NULL;
  const gchar *format_name;
  gint64 end;
  gint64 start;

  g_setenv ("GEGL_MIPMAP_RENDERING", "1", TRUE);

  gegl_init (NULL, NULL);

  graph = gegl_node_new ();
  src = gegl_node_new_child (graph,
                             "operation", "gegl:load",
                             "path", FILE_PATH,
                             NULL);
  sink = gegl_node_new_child (graph,
                              "operation", "gegl:buffer-sink",
                              NULL);

  gegl_node_link_many (src, sink, NULL);
  gegl_node_set (sink, "buffer", &buffer, NULL);

  gegl_node_process (sink);
  g_object_unref (graph);

  format = gegl_buffer_get_format (buffer);
  format_name = babl_get_name (format);
  g_message ("%s", format_name);

  graph = gegl_node_new ();
  src = gegl_node_new_child (graph,
                             "operation", "gegl:buffer-source",
                             NULL);
  translate = gegl_node_new_child (graph,
                                   "operation", "gegl:translate",
                                   "sampler", GEGL_SAMPLER_NEAREST,
                                   "x", 0.5,
                                   "y", 0.5,
                                   NULL);
  sink = gegl_node_new_child (graph,
                              "operation", "gegl:save-pixbuf",
                              NULL);

  gegl_node_link_many (src, translate, sink, NULL);
  gegl_node_set (src, "buffer", buffer, NULL);
  gegl_node_set (sink, "pixbuf", &pixbuf, NULL);

  processor = gegl_node_new_processor (sink, NULL);
  gegl_processor_set_scale (processor, 1.0);

  start = g_get_monotonic_time ();
  while (gegl_processor_work (processor, NULL));
  end = g_get_monotonic_time ();
  g_message ("%" G_GINT64_FORMAT, end - start);

  g_object_unref (processor);
  g_object_unref (graph);
  g_object_unref (pixbuf);
  g_object_unref (buffer);
  gegl_exit ();

  return 0;
}
