summaryrefslogtreecommitdiff
path: root/operations/common/opacity.c
blob: 5c50cbb8b72b2b97b3d3286cccfe1ab366a26832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* 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 Øyvind Kolås <pippin@gimp.org>
 */

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

#ifdef GEGL_CHANT_PROPERTIES

gegl_chant_double (value, _("Opacity"), -10.0, 10.0, 1.0,
         _("Global opacity value that is always used on top of the optional auxiliary input buffer."))

#else

#define GEGL_CHANT_TYPE_POINT_COMPOSER
#define GEGL_CHANT_C_FILE       "opacity.c"

#include "gegl-chant.h"
#include "graph/gegl-node.h"

#include <stdio.h>

static void prepare (GeglOperation *self)
{
  GeglNode *src_node = gegl_operation_get_source_node (self, "input");
  GeglChantO *o = GEGL_CHANT_PROPERTIES (self);

  if (src_node)
    {
      GeglOperation *op = src_node->operation;
      if (op)
        {
          const Babl *fmt = gegl_operation_get_format (op, "output"); /* XXX: could
                                                         be a different pad */

          if (fmt == babl_format ("RGBA float"))
            {
              /* ugly way of communicating that we want the RGBA version */
              o->chant_data = (void*)0xabc;
              gegl_operation_set_format (self, "input", babl_format ("RGBA float"));
              gegl_operation_set_format (self, "output", babl_format ("RGBA float"));
              gegl_operation_set_format (self, "aux", babl_format ("Y float"));
              return;
            }
        }
    }
  o->chant_data = NULL;
  gegl_operation_set_format (self, "input", babl_format ("RaGaBaA float"));
  gegl_operation_set_format (self, "output", babl_format ("RaGaBaA float"));
  gegl_operation_set_format (self, "aux", babl_format ("Y float"));
}

static gboolean
process (GeglOperation       *op,
         void                *in_buf,
         void                *aux_buf,
         void                *out_buf,
         glong                samples,
         const GeglRectangle *roi,
         gint                 level)
{
  gfloat *in = in_buf;
  gfloat *out = out_buf;
  gfloat *aux = aux_buf;
  gfloat value = GEGL_CHANT_PROPERTIES (op)->value;

  if (GEGL_CHANT_PROPERTIES (op)->chant_data) /* RGBA version indicator */
    {
    if (aux == NULL)
      {
        g_assert (value != 1.0); /* buffer should have been passed through */
        while (samples--)
          {
            gint j;
            for (j=0; j<3; j++)
              out[j] = in[j];
            out[3] = in[3] * value;
            in  += 4;
            out += 4;
          }
      }
    else if (value == 1.0)
      while (samples--)
        {
          gint j;
          for (j=0; j<4; j++)
            out[j] = in[j] * (*aux);
          in  += 4;
          out += 4;
          aux += 1;
        }
    else
      while (samples--)
        {
          gfloat v = (*aux) * value;
          gint j;
          for (j=0; j<3; j++)
            out[j] = in[j];
          out[3] = in[3] * v;
          in  += 4;
          out += 4;
          aux += 1;
        }
    }
  else
    {
    if (aux == NULL)
      {
        g_assert (value != 1.0); /* buffer should have been passed through */
        while (samples--)
          {
            gint j;
            for (j=0; j<4; j++)
              out[j] = in[j] * value;
            in  += 4;
            out += 4;
          }
      }
    else if (value == 1.0)
      while (samples--)
        {
          gint j;
          for (j=0; j<4; j++)
            out[j] = in[j] * (*aux);
          in  += 4;
          out += 4;
          aux += 1;
        }
    else
      while (samples--)
        {
          gfloat v = (*aux) * value;
          gint j;
          for (j=0; j<4; j++)
            out[j] = in[j] * v;
          in  += 4;
          out += 4;
          aux += 1;
        }
    }
  return TRUE;
}

static const char* kernel_source =
"__kernel void gegl_opacity (__global const float4     *in,     \n"
"                            __global const float      *aux,    \n"
"                            __global       float4     *out,    \n"
"                            float value)                       \n"
"{                                                              \n"
"  int gid = get_global_id(0);                                  \n"
"  float4 in_v  = in [gid];                                     \n"
"  float  aux_v = (aux)? aux[gid] : 1.0f;                       \n"
"  float4 out_v;                                                \n"
"  out_v = in_v * aux_v * value;                                \n"
"  out[gid]  =  out_v;                                          \n"
"}                                                              \n";

/* Fast path when opacity is a no-op
 */
static gboolean operation_process (GeglOperation        *operation,
                                   GeglOperationContext *context,
                                   const gchar          *output_prop,
                                   const GeglRectangle  *result,
                                   gint                  level)
{
  GeglOperationClass  *operation_class;
  gpointer in, aux;
  operation_class = GEGL_OPERATION_CLASS (gegl_chant_parent_class);

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

  if (in && !aux && GEGL_CHANT_PROPERTIES (operation)->value == 1.0)
    {
      gegl_operation_context_take_object (context, "output",
                                          g_object_ref (G_OBJECT (in)));
      return TRUE;
    }
  /* chain up, which will create the needed buffers for our actual
   * process function
   */
  return operation_class->process (operation, context, output_prop, result, 
                                  gegl_operation_context_get_level (context));
}


static void
gegl_chant_class_init (GeglChantClass *klass)
{
  GeglOperationClass              *operation_class;
  GeglOperationPointComposerClass *point_composer_class;

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

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

  gegl_operation_class_set_keys (operation_class,
    "name"       , "gegl:opacity",
    "categories" , "transparency",
    "description",
          _("Weights the opacity of the input both the value of the aux"
            " input and the global value property."),
    "cl-source"  , kernel_source,
    NULL);
}

#endif