lighten.c 5.79 KB
Newer Older
1

2
/* !!!! AUTOGENERATED FILE generated by svg-12-blend.rb !!!!!
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with GEGL; if not, see <https://www.gnu.org/licenses/>.
18 19 20
 *
 *  Copyright 2006, 2007 Øyvind Kolås <pippin@gimp.org>
 *            2007 John Marshall
21
 *            2013 Daniel Sabo
22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * SVG rendering modes; see:
 *     http://www.w3.org/TR/SVG12/rendering.html
 *     http://www.w3.org/TR/2004/WD-SVG12-20041027/rendering.html#comp-op-prop
 *
 *     aA = aux(src) alpha      aB = in(dst) alpha      aD = out alpha
 *     cA = aux(src) colour     cB = in(dst) colour     cD = out colour
 *
 * !!!! AUTOGENERATED FILE !!!!!
 */
#include "config.h"
#include <glib/gi18n-lib.h>


36
#ifdef GEGL_PROPERTIES
37

38 39
property_boolean (srgb, _("sRGB"), FALSE)
    description (_("Use sRGB gamma instead of linear"))
40 41 42

#else

43
#define GEGL_OP_POINT_COMPOSER
44 45
#define GEGL_OP_NAME         lighten
#define GEGL_OP_C_FILE       "lighten.c"
46

47
#include "gegl-op.h"
48 49 50

static void prepare (GeglOperation *operation)
{
51 52 53
  const Babl *format = gegl_operation_get_source_format (operation, "input");
  if (!format)
    format = gegl_operation_get_source_format (operation, "aux");
54

55 56
  if(GEGL_PROPERTIES (operation)->srgb)
    format = gegl_babl_variant (format, GEGL_BABL_VARIANT_PERCEPTUAL_PREMULTIPLIED);
57
  else
58
    format = gegl_babl_variant (format, GEGL_BABL_VARIANT_LINEAR_PREMULTIPLIED);
59

60 61
  gegl_operation_set_format (operation, "input",  format);
  gegl_operation_set_format (operation, "aux",    format);
62 63 64
  gegl_operation_set_format (operation, "output", format);
}

65 66 67 68 69 70 71 72 73
/* Fast paths */
static gboolean operation_process (GeglOperation        *operation,
                                   GeglOperationContext *context,
                                   const gchar          *output_prop,
                                   const GeglRectangle  *result,
                                   gint                  level)
{
  GeglOperationClass  *operation_class;
  gpointer input, aux;
74
  operation_class = GEGL_OPERATION_CLASS (gegl_op_parent_class);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

  /* get the raw values this does not increase the reference count */
  input = gegl_operation_context_get_object (context, "input");
  aux = gegl_operation_context_get_object (context, "aux");

  /* pass the input/aux buffers directly through if they are alone*/
  {
    const GeglRectangle *in_extent = NULL;
    const GeglRectangle *aux_extent = NULL;

    if (input)
      in_extent = gegl_buffer_get_abyss (input);

    if ((!input ||
        (aux && !gegl_rectangle_intersect (NULL, in_extent, result))))
      {
         gegl_operation_context_take_object (context, "output",
                                             g_object_ref (aux));
         return TRUE;
      }
    if (aux)
      aux_extent = gegl_buffer_get_abyss (aux);

    if (!aux ||
        (input && !gegl_rectangle_intersect (NULL, aux_extent, result)))
      {
        gegl_operation_context_take_object (context, "output",
                                            g_object_ref (input));
        return TRUE;
      }
  }
  /* chain up, which will create the needed buffers for our actual
   * process function
   */
  return operation_class->process (operation, context, output_prop, result, level);
}

112 113 114 115 116 117
static gboolean
process (GeglOperation       *op,
         void                *in_buf,
         void                *aux_buf,
         void                *out_buf,
         glong                n_pixels,
118 119
         const GeglRectangle *roi,
         gint                 level)
120
{
121 122 123
  const Babl *format = gegl_operation_get_format (op, "output");
  gint    components = babl_format_get_n_components (format);
  gint    alpha      = babl_format_has_alpha (format);
124 125 126 127 128
  gfloat * GEGL_ALIGNED in = in_buf;
  gfloat * GEGL_ALIGNED aux = aux_buf;
  gfloat * GEGL_ALIGNED out = out_buf;
  gint    i;

129 130
  if(aux == NULL)
     return TRUE;
131 132 133 134 135 136

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

137 138 139 140 141 142 143 144 145
      if (alpha)
      {
        aB = in[components-1];
        aA = aux[components-1];
      }
      else
      {
        aB = aA = 1.0f;
      }
146 147
      aD = aA + aB - aA * aB;

148
      for (j = 0; j < components-alpha; j++)
149 150 151 152 153 154 155
        {
          gfloat cA, cB;

          cB = in[j];
          cA = aux[j];
          out[j] = CLAMP (MAX (cA * aB, cB * aA) + cA * (1 - aB) + cB * (1 - aA), 0, aD);
        }
156 157 158 159 160
      if (alpha)
        out[components-1] = aD;
      in  += components;
      aux += components;
      out += components;
161 162 163 164 165 166
    }

  return TRUE;
}

static void
167
gegl_op_class_init (GeglOpClass *klass)
168 169 170 171 172 173 174 175
{
  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;
176 177
  operation_class->process      = operation_process;
  operation_class->prepare      = prepare;
178

179 180
  gegl_operation_class_set_keys (operation_class,
  "name"        , "svg:lighten",
181
  "compat-name" , "gegl:lighten",
182
  "reference-hash" , "81d848688367b57294d91ea1e49880c7",
183
  "description" ,
184
        _("SVG blend operation lighten (<code>d = MAX (cA * aB, cB * aA) + cA * (1 - aB) + cB * (1 - aA)</code>)"),
185 186
        NULL);
  gegl_operation_class_set_key (operation_class, "categories", "compositors:svgfilter");
187 188 189
}

#endif