/* gcc babl-cie-rgb.c $(pkg-config --cflags --libs babl glib-2.0) */

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

gint
main (void)
{
  const Babl *destination_format;
  const Babl *fish;
  const Babl *source_format;
  gfloat *ptr;
  gfloat value;
  gint destination_bpp;
  gint j;
  gint source_bpp;
  gint n_source_components;
  gint64 end;
  gint64 start;
  g_autofree gpointer destination = NULL;
  g_autofree gpointer source = NULL;
  gsize i;
  gsize n_converted;
  gsize n_pixels = 15 * 1024 * 1024;

  babl_init ();

  destination_format = babl_format ("RGBA float");
  destination_bpp = babl_format_get_bytes_per_pixel (destination_format);
  destination = g_malloc_n (n_pixels, destination_bpp);

  source_format = babl_format ("CIE Lab alpha float");
  source_bpp = babl_format_get_bytes_per_pixel (source_format);
  source = g_malloc_n (n_pixels, source_bpp);

  ptr = (gfloat *) source;
  n_source_components = babl_format_get_n_components (source_format);
  value = 0.0f;

  for (i = 0; i < n_pixels; i++)
    {
      for (j = 0; j < n_source_components; j++)
        {
          *ptr = value;
          value += 0.001;

          if (value > 1.0f)
            value = 0.0f;
        }
    }

  fish = babl_fish (source_format, destination_format);

  start = g_get_monotonic_time ();
  n_converted = babl_process (fish, source, destination, (glong) n_pixels);
  end = g_get_monotonic_time ();
  g_message ("time: %" G_GINT64_FORMAT ",  pixels: %ld",
             end - start,
             n_converted);

  babl_exit ();
  return 0;
}
