#include <gegl.h>

static void
test_gegl_iter_incompatible (void)
{
  GeglBufferIterator *it;
  GeglRectangle extent = { 0, 0, 1024, 1024 };
  GeglRectangle roi = { 0, 0, 1024 - 32, 1024 - 32};
  GeglBuffer *one = gegl_buffer_new (&extent, babl_format ("RGB u8"));
  GeglBuffer *two = gegl_buffer_new (&extent, babl_format ("RGB u8"));
  gint j, k;

  it = gegl_buffer_iterator_new (one, &roi, 0, NULL, GEGL_BUFFER_WRITE, GEGL_ABYSS_NONE);

  roi.x = roi.y = 32; 
  j = gegl_buffer_iterator_add (it, two, &roi, 0, NULL, GEGL_BUFFER_WRITE, GEGL_ABYSS_NONE);

  while (gegl_buffer_iterator_next (it))
    {
      guchar *b1 = it->data[0];
      guchar *b2 = it->data[j];

      for (k = 0; k != it->length; ++k)
        {
          b1[3 * k + 0] = b1[3 * k + 1] = b1[3 * k + 2] = 0;
          b2[3 * k + 0] = b2[3 * k + 1] = b2[3 * k + 2] = 0;
        }
    }

  g_object_unref (one);
  g_object_unref (two);
}

static void
test_gegl_iter_compatible (void)
{
  GeglBufferIterator *it;
  GeglRectangle extent = { 0, 0, 1024, 1024 };
  GeglRectangle roi = { 0, 0, 1024 - 32, 1024 - 32};
  GeglBuffer *one = gegl_buffer_new (&extent, babl_format ("RGB u8"));
  GeglBuffer *two = g_object_new (GEGL_TYPE_BUFFER,
                                  "x", 32,
                                  "y", 32,
                                  "width", extent.width,
                                  "height", extent.height,
                                  "format", babl_format ("RGB u8"),
                                  "shift-x", -32,
                                  "shift-y", -32,
                                  NULL);
  gint j, k;

  it = gegl_buffer_iterator_new (one, &roi, 0, NULL, GEGL_BUFFER_WRITE, GEGL_ABYSS_NONE);

  roi.x = roi.y = 32; 
  j = gegl_buffer_iterator_add (it, two, &roi, 0, NULL, GEGL_BUFFER_WRITE, GEGL_ABYSS_NONE);

  while (gegl_buffer_iterator_next (it))
    {
      guchar *b1 = it->data[0];
      guchar *b2 = it->data[j];

      for (k = 0; k != it->length; ++k)
        {
          b1[3 * k + 0] = b1[3 * k + 1] = b1[3 * k + 2] = 0;
          b2[3 * k + 0] = b2[3 * k + 1] = b2[3 * k + 2] = 0;
        }
    }

  g_object_unref (one);
  g_object_unref (two);
}

int
main (int   argc,
      char *argv[])
{
  gegl_init (&argc, &argv);

  test_gegl_iter_incompatible ();
  test_gegl_iter_compatible ();

  gegl_exit ();
  return 0;
}
