color-overlay.c 4.23 KB
Newer Older
1 2
/* This file is an image processing operation for GEGL
 *
Ell's avatar
Ell committed
3 4 5 6
 * 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.
7
 *
Ell's avatar
Ell committed
8
 * GEGL is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Ell's avatar
Ell committed
10 11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
12
 *
Ell's avatar
Ell committed
13
 * You should have received a copy of the GNU Lesser General Public
14
 * License along with GEGL; if not, see <https://www.gnu.org/licenses/>.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * Copyright (C) 2018 Ell
 */

#include "config.h"
#include <glib/gi18n-lib.h>

#define EPSILON 1e-6

#ifdef GEGL_PROPERTIES

property_color (value, _("Color"), "transparent")
    description (_("The color to paint over the input"))
    ui_meta     ("role", "color-primary")

property_boolean (srgb, _("sRGB"), FALSE)
    description (_("Use sRGB gamma instead of linear"))

#else

#define GEGL_OP_POINT_FILTER
#define GEGL_OP_NAME     color_overlay
#define GEGL_OP_C_SOURCE color-overlay.c

#include "gegl-op.h"
Ell's avatar
Ell committed
40
#include <math.h>
41 42 43

static void prepare (GeglOperation *operation)
{
44
  const Babl *space = gegl_operation_get_source_space (operation, "input");
45 46 47 48
  GeglProperties *o = GEGL_PROPERTIES (operation);
  const Babl     *format;

  if (! o->srgb)
49
    format = babl_format_with_space ("RGBA float", space);
50
  else
51
    format = babl_format_with_space ("R~G~B~A float", space);
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

  gegl_operation_set_format (operation, "input", format);
  gegl_operation_set_format (operation, "output", format);
}

static gboolean
process (GeglOperation       *operation,
         void                *in_buf,
         void                *out_buf,
         glong                samples,
         const GeglRectangle *roi,
         gint                 level)
{
  GeglProperties *o   = GEGL_PROPERTIES (operation);
  gfloat         *in  = in_buf;
  gfloat         *out = out_buf;
68
  const Babl     *format = gegl_operation_get_format (operation, "output");
69 70
  gfloat          color[4];
  gfloat          alpha_c;
71
  gint            i;
72 73 74

  gegl_color_get_pixel (o->value, format, &color);

75 76 77
  for (i = 0; i < 3; i++)
    color[i] *= color[3];

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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  alpha_c = 1.0f - color[3];

  if (fabs (alpha_c) <= EPSILON)
    {
      while (samples--)
        {
          gint i;

          for (i = 0; i < 3; i++)
            out[i] = color[i];

          out[3] = in[3];

          in  += 4;
          out += 4;
        }
    }
  else
    {
      while (samples--)
        {
          gint i;

          for (i = 0; i < 3; i++)
            out[i] = in[i] * alpha_c + color[i];

          out[3] = in[3];

          in  += 4;
          out += 4;
        }
    }

  return TRUE;
}

static gboolean
operation_process (GeglOperation        *operation,
                   GeglOperationContext *context,
                   const gchar          *output_prop,
                   const GeglRectangle  *result,
                   gint                  level)
{
  GeglProperties *o = GEGL_PROPERTIES (operation);
  gdouble         alpha;

  gegl_color_get_rgba (o->value, NULL, NULL, NULL, &alpha);

  if (fabs (alpha) <= EPSILON)
    {
      /* pass input directly */
      gegl_operation_context_set_object (
        context,
        "output", gegl_operation_context_get_object (context, "input"));

      return TRUE;
    }

  return GEGL_OPERATION_CLASS (gegl_op_parent_class)->process (
    operation, context, output_prop, result, level);
}

static void
gegl_op_class_init (GeglOpClass *klass)
{
  GeglOperationClass            *operation_class;
  GeglOperationPointFilterClass *point_filter_class;

  operation_class    = GEGL_OPERATION_CLASS (klass);
  point_filter_class = GEGL_OPERATION_POINT_FILTER_CLASS (klass);

  operation_class->prepare    = prepare;
  operation_class->process    = operation_process;

  point_filter_class->process = process;

  gegl_operation_class_set_keys (operation_class,
155 156 157
      "name",        "gegl:color-overlay",
      "categories",  "color",
      "title",       _("Color Overlay"),
158
      "description", _("Paint a color overlay over the input, "
159
                       "preserving its transparency."),
160 161 162 163 164
      NULL);
}

#endif