/* gcc gegl-crop-passthrough.c $(pkg-config --cflags --libs gegl-0.3
   babl) */

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

static const gchar *FILE_PATH = "/home/rishi/Pictures/orientation/gnome-bug-734844.jpg";

static void
invalidated_cb (GeglNode *node)
{
  GeglRectangle bbox;
  bbox = gegl_node_get_bounding_box (node);
}

int
main (void)
{
  GeglNode *crop;
  GeglNode *graph;
  GeglNode *input;
  GeglNode *load;
  GeglNode *output;
  GeglNode *pipeline;
  GeglProcessor *processor;
  GeglRectangle bbox;

  gegl_init (NULL, NULL);

  graph = gegl_node_new ();

  load = gegl_node_new_child (graph,
                              "operation", "gegl:load",
                              "path", FILE_PATH,
                              NULL);

  pipeline = gegl_node_new ();
  gegl_node_add_child (graph, pipeline);
  input = gegl_node_get_input_proxy (pipeline, "input");
  output = gegl_node_get_output_proxy (pipeline, "output");

  crop = gegl_node_new_child (pipeline,
                              "operation", "gegl:crop",
                              "x", 10.0,
                              "y", 10.0,
                              "width", 20.0,
                              "height", 20.0,
                              NULL);

  gegl_node_link_many (input, crop, output, NULL);
  gegl_node_link_many (load, pipeline, NULL);
  gegl_node_process (pipeline);

  g_signal_connect (pipeline, "invalidated", G_CALLBACK (invalidated_cb), NULL);
  gegl_node_set_passthrough (crop, TRUE);

  bbox = gegl_node_get_bounding_box (pipeline);
  g_message ("pipeline (passthrough, wrong): %d, %d, %dx%d", bbox.x, bbox.y, bbox.width, bbox.height);

  bbox = gegl_node_get_bounding_box (output);
  g_message ("output (passthrough, wrong): %d, %d, %dx%d", bbox.x, bbox.y, bbox.width, bbox.height);

  bbox = gegl_node_get_bounding_box (crop);
  g_message ("crop (passthrough, wrong): %d, %d, %dx%d", bbox.x, bbox.y, bbox.width, bbox.height);

  gegl_node_process (pipeline);

  bbox = gegl_node_get_bounding_box (pipeline);
  g_message ("pipeline (passthrough, processed, wrong): %d, %d, %dx%d", bbox.x, bbox.y, bbox.width, bbox.height);

  bbox = gegl_node_get_bounding_box (output);
  g_message ("output (passthrough, processed, correct): %d, %d, %dx%d", bbox.x, bbox.y, bbox.width, bbox.height);

  bbox = gegl_node_get_bounding_box (crop);
  g_message ("crop (passthrough, processed, correct): %d, %d, %dx%d", bbox.x, bbox.y, bbox.width, bbox.height);

  g_object_unref (graph);
  g_object_unref (pipeline);
  gegl_exit ();

  return 0;
}
