#!/usr/bin/env ruby

copyright = '
/* !!!! AUTOGENERATED FILE generated by blend.rb !!!!!
 *
 * This file is an image processing operation for GEGL
 *
 * GEGL is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * GEGL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright 2006 Oyvind Kolas <pippin@gimp.org>
 *
 * The formulas used for the blend modes are from:
 *     http://www.pegtop.net/delphi/articles/blendmodes/
 *
 * !!!! AUTOGENERATED FILE !!!!!
 */'

a = [
      ['average',       '(cA + aB)/2', 'average'],
      #['screen',        '1.0 - ((1.0-cA) * (1.0-cB))'],
      #['darken',        'cA < cB ? cA : cB'],
      #['lighten',       'cA > cB ? cA : cB'],
      #['difference',    'fabs(cA-cB)'],
      ['negation',      '1.0 - fabs(1.0-cA-cB)', 'negation'],
      #['exclusion',     'cA + cB - 2*cA*cB'],
      #['overlay',       'cA<0.5?2*(cA*cB):1.0-2*(1.0-cA)*(1.0-cB)'],
      #['hard-light',    'cB<0.5?2*(cA*cB):1.0-2*(1.0-cA)*(1.0-cB)'],
      #['soft-light',    '2*cA*cB+cA*cA-2*cA*cA*cB'],
      #['color_dodge',   'cA / (1.0 - cB)'],
      ['soft-dodge',    '(cA+cB<1.0)?0.5*cA / (1.0 - cB):1.0-0.5*(1.0 - cB)/cA', 'soft_dodge'],
      #['color_burn',    'cB<=0.0?0.0:1.0-(1.0-cA)/cB'],
      ['soft-burn',     '(cA+cB<1.0)?0.5*cB / (1.0 - cA):1.0-0.5*(1.0 - cA) / cB', 'soft_burn'],
      ['blend-reflect', 'cB>=1.0?1.0:cA*cA / (1.0-cB)', 'blend_reflect'],
      ['subtractive',   'cA+cB-1.0', 'subtractive']
    ]

a.each do
    |item|

    name     = item[0] + ''
    filename = name + '.c'

    puts "generating #{filename}"
    file = File.open(filename, 'w')

    name        = item[0]
    capitalized = name.capitalize
    swapcased   = name.swapcase
    formula     = item[1]

    file.write copyright
    file.write "
#include \"config.h\"
#include <glib/gi18n-lib.h>


#ifdef GEGL_PROPERTIES

/* no properties */

#else

#define GEGL_OP_POINT_COMPOSER
#define GEGL_OP_NAME  #{item[2]}
#define GEGL_OP_C_FILE          \"#{filename}\"

#include \"gegl-op.h\"
#include <math.h>

static void prepare (GeglOperation *self)
{
  const Babl *format = babl_format (\"RaGaBaA float\");

  gegl_operation_set_format (self, \"input\", format);
  gegl_operation_set_format (self, \"aux\", format);
  gegl_operation_set_format (self, \"output\", format);
}

static gboolean
process (GeglOperation       *op,
         void                *in_buf,
         void                *aux_buf,
         void                *out_buf,
         glong                n_pixels,
         const GeglRectangle *roi,
         gint                 level)
{
  gfloat *in  = in_buf;
  gfloat *aux = aux_buf;
  gfloat *out = out_buf;
  gint    i;

  if (aux == NULL)
    return TRUE;

  for (i = 0; i < n_pixels; i++)
    {
      gfloat aA, aB;
      gint   j;

      aA = in[3];
      aB = aux[3];
      for (j = 0; j < 3; j++)
        {
          gfloat cA, cB;

          cA = in[j];
          cB = aux[j];
          out[j] = cA + (#{formula}-cA) * cB;
        }
      out[j] = aA;
      in  += 4;
      aux += 4;
      out += 4;
    }

  return TRUE;
}


static void
gegl_op_class_init (GeglOpClass *klass)
{
  GeglOperationClass              *operation_class;
  GeglOperationPointComposerClass *point_composer_class;

  operation_class      = GEGL_OPERATION_CLASS (klass);
  point_composer_class = GEGL_OPERATION_POINT_COMPOSER_CLASS (klass);

  point_composer_class->process = process;
  operation_class->prepare = prepare;

  gegl_operation_class_set_keys (operation_class,
  \"name\"        , \"gegl:#{name}\",
  \"title\"       , \"#{name}\",
  \"categories\"  , \"compositors:blend\",
  \"description\" ,
        _(\"Image blending operation '#{name}' (<tt>c = #{formula}</tt>)\"),
        NULL);
}

#endif
"

  file.close
end
