/* gcc gegl-grey.c $(pkg-config --cflags --libs gegl-0.3 babl glib-2.0) */

#include <babl/babl.h>
#include <gegl.h>
#include <glib.h>

static const gchar *FILE_PATH = "checks.png";

gint
main (void)
{
  const Babl *format;
  GeglBuffer *buffer;
  GeglConfig *config;
  GeglNode *graph;
  GeglNode *src, *sink;
  GeglProcessor *processor;
  gboolean use_opencl;
  const gchar *format_name;
  gint threads;
  gint64 end;
  gint64 start;
  guchar *buf;
  guint processors;

  gegl_init (NULL, NULL);

  config = gegl_config ();
  g_object_get (config, "threads", &threads, NULL);
  g_object_get (config, "use-opencl", &use_opencl, NULL);

  processors = g_get_num_processors ();

  g_message ("processors: %u", processors);
  g_message ("threads: %d", threads);
  g_message ("use-opencl: %d", use_opencl);

  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",
                              "buffer", &buffer,
                              NULL);

  gegl_node_link_many (src, sink, 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",
                             "buffer", buffer,
                             NULL);
  sink = gegl_node_new_child (graph,
                              "operation", "gegl:grey",
                              NULL);

  gegl_node_link_many (src, sink, NULL);

  processor = gegl_node_new_processor (sink, NULL);

  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 (buffer);
  gegl_exit ();

  return 0;
}
