weighted-blend.c 5.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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
14
 * License along with GEGL; if not, see <https://www.gnu.org/licenses/>.
15 16 17 18 19 20 21 22
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
 *           2008 James Legg
 */
#include "config.h"
#include <glib/gi18n-lib.h>


23
#ifdef GEGL_PROPERTIES
24 25 26

#else

27
#define GEGL_OP_POINT_COMPOSER
28
#define GEGL_OP_C_SOURCE weighted-blend.c
29
#define GEGL_OP_NAME     weighted_blend
30

31
#include "gegl-op.h"
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
#include "opencl/gegl-cl.h"
#include "opencl/weighted-blend.cl.h"

static GeglClRunData *cl_data = NULL;

static gboolean
cl_process (GeglOperation       *self,
            cl_mem               in_tex,
            cl_mem               aux_tex,
            cl_mem               out_tex,
            size_t               global_worksize,
            const GeglRectangle *roi,
            gint                 level)
{
  gint cl_err = 0;

  if (!cl_data)
    {
      const char *kernel_name[] = {"cl_copy_weigthed_blend",
                                   "cl_weighted_blend",
                                   NULL};
      cl_data = gegl_cl_compile_and_build (weighted_blend_cl_source,
                                           kernel_name);
    }
  if (!cl_data) return TRUE;


  if (!aux_tex)
    {
      cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 0, sizeof(cl_mem), (void*)&in_tex);
      CL_CHECK;
      cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 1, sizeof(cl_mem), (void*)&out_tex);
      CL_CHECK;

      cl_err = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue (),
                                           cl_data->kernel[0], 1,
                                           NULL, &global_worksize, NULL,
                                           0, NULL, NULL);
      CL_CHECK;
    }
  else
    {
      cl_err = gegl_clSetKernelArg(cl_data->kernel[1], 0, sizeof(cl_mem), (void*)&in_tex);
      CL_CHECK;
      cl_err = gegl_clSetKernelArg(cl_data->kernel[1], 1, sizeof(cl_mem), (void*)&aux_tex);
      CL_CHECK;
      cl_err = gegl_clSetKernelArg(cl_data->kernel[1], 2, sizeof(cl_mem), (void*)&out_tex);
      CL_CHECK;

      cl_err = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue (),
                                           cl_data->kernel[1], 1,
                                           NULL, &global_worksize, NULL,
                                           0, NULL, NULL);
      CL_CHECK;
    }

  return FALSE;

error:
  return TRUE;
}

95 96 97 98 99 100
static gboolean
process (GeglOperation       *op,
         void                *in_buf,
         void                *aux_buf,
         void                *out_buf,
         glong                n_pixels,
101 102
         const GeglRectangle *roi,
         gint                 level)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
{
  gfloat *in  = in_buf;
  gfloat *out = out_buf;
  gfloat *aux = aux_buf;
  gint    i;

  if (aux == NULL)
    {
      /* there is no auxilary buffer.
       * output the input buffer.
       */
      for (i = 0; i < n_pixels; i++)
        {
          gint j;
          for (j = 0; j < 4; j++)
            {
              out[j] = in[j];
            }
121 122
          in  += 4;
          out += 4;
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
        }
    }
  else
    {
      for (i=0; i<n_pixels; i++)
        {
          gint   j;
          gfloat total_alpha;
          /* find the proportion between alpha values */
          total_alpha = in[3] + aux[3];
          if (!total_alpha)
            {
              /* no coverage from any source pixel */
              for (j = 0; j < 4; j++)
                {
                  out[j] = 0.0;
                }
            }
          else
            {
              /* the total alpha is non-zero, therefore we may find a colour from a weighted blend */
              gfloat in_weight = in[3] / total_alpha;
              gfloat aux_weight = 1.0 - in_weight;
              for (j = 0; j < 3; j++)
                {
                  out[j] = in_weight * in[j] + aux_weight * aux[j];
                }
              out[3] = total_alpha;
            }
          in  += 4;
          aux += 4;
          out += 4;
        }
    }

  return TRUE;
}

static void
162
gegl_op_class_init (GeglOpClass *klass)
163 164 165 166 167 168 169
{
  GeglOperationClass              *operation_class;
  GeglOperationPointComposerClass *point_composer_class;

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

170 171 172
  point_composer_class->process    = process;
  point_composer_class->cl_process = cl_process;
  operation_class->opencl_support  = TRUE;
173

174
  gegl_operation_class_set_keys (operation_class,
175 176
    "name" ,       "gegl:weighted-blend",
    "title",       _("Weighted Blend"),
177
    "categories" , "compositors:blend",
178
    "reference-hash", "8013d8c837dd6b38579b4437703ed512",
179 180 181
    "description",
      _("blend two images using alpha values as weights"),
    NULL);
182 183
}
#endif