gimpoperationsubtractmode.c 4.68 KB
Newer Older
1 2 3 4 5
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpoperationsubtractmode.c
 * Copyright (C) 2008 Michael Natterer <mitch@gimp.org>
6
 *               2012 Ville Sokk <ville.sokk@gmail.com>
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11 12 13 14 15 16 17 18
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 21 22 23 24 25
 */

#include "config.h"

#include <gegl-plugin.h>

26
#include "operations-types.h"
27 28 29 30 31 32 33

#include "gimpoperationsubtractmode.h"


static gboolean gimp_operation_subtract_mode_process (GeglOperation       *operation,
                                                      void                *in_buf,
                                                      void                *aux_buf,
34
                                                      void                *aux2_buf,
35 36
                                                      void                *out_buf,
                                                      glong                samples,
37 38
                                                      const GeglRectangle *roi,
                                                      gint                 level);
39 40 41 42 43 44 45 46 47


G_DEFINE_TYPE (GimpOperationSubtractMode, gimp_operation_subtract_mode,
               GIMP_TYPE_OPERATION_POINT_LAYER_MODE)


static void
gimp_operation_subtract_mode_class_init (GimpOperationSubtractModeClass *klass)
{
48 49
  GeglOperationClass               *operation_class;
  GeglOperationPointComposer3Class *point_class;
50 51

  operation_class = GEGL_OPERATION_CLASS (klass);
52
  point_class     = GEGL_OPERATION_POINT_COMPOSER3_CLASS (klass);
53

54
  gegl_operation_class_set_keys (operation_class,
55 56 57
                                 "name",        "gimp:subtract-mode",
                                 "description", "GIMP subtract mode operation",
                                 NULL);
58

59
  point_class->process = gimp_operation_subtract_mode_process;
60 61 62 63 64 65 66 67 68 69 70
}

static void
gimp_operation_subtract_mode_init (GimpOperationSubtractMode *self)
{
}

static gboolean
gimp_operation_subtract_mode_process (GeglOperation       *operation,
                                      void                *in_buf,
                                      void                *aux_buf,
71
                                      void                *aux2_buf,
72 73
                                      void                *out_buf,
                                      glong                samples,
74 75
                                      const GeglRectangle *roi,
                                      gint                 level)
76
{
77
  gfloat opacity = GIMP_OPERATION_POINT_LAYER_MODE (operation)->opacity;
78 79 80 81 82 83 84 85 86

  return gimp_operation_subtract_mode_process_pixels (in_buf, aux_buf, aux2_buf, out_buf, opacity, samples, roi, level);
}

gboolean
gimp_operation_subtract_mode_process_pixels (gfloat              *in,
                                             gfloat              *layer,
                                             gfloat              *mask,
                                             gfloat              *out,
87
                                             gfloat               opacity,
88 89 90 91
                                             glong                samples,
                                             const GeglRectangle *roi,
                                             gint                 level)
{
92
  const gboolean has_mask = mask != NULL;
93 94 95

  while (samples--)
    {
96
      gfloat comp_alpha, new_alpha;
97

98
      comp_alpha = MIN (in[ALPHA], layer[ALPHA]) * opacity;
99 100
      if (has_mask)
        comp_alpha *= *mask;
101

102
      new_alpha = in[ALPHA] + (1.0 - in[ALPHA]) * comp_alpha;
103 104

      if (comp_alpha && new_alpha)
105
        {
106 107
          gint   b;
          gfloat ratio = comp_alpha / new_alpha;
108

109 110 111
          for (b = RED; b < ALPHA; b++)
            {
              gfloat comp = in[b] - layer[b];
112
              out[b] = comp * ratio + in[b] * (1.0 - ratio);
113
              out[b] = CLAMP(out[b], 0.0, 1.0);
114 115 116 117
            }
        }
      else
        {
118 119 120
          gint b;

          for (b = RED; b < ALPHA; b++)
121 122 123 124
            {
              out[b] = in[b];
            }
        }
125

126 127
      out[ALPHA] = in[ALPHA];

128 129 130
      in    += 4;
      layer += 4;
      out   += 4;
131

132 133
      if (has_mask)
        mask++;
134 135 136 137
    }

  return TRUE;
}