1 /* This file is an image processing operation for GEGL
3 * GEGL is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 3 of the License, or (at your option) any later version.
8 * GEGL is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
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/>.
16 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
20 #include <glib/gi18n-lib.h>
23 #ifdef GEGL_PROPERTIES
27 #define GEGL_OP_POINT_COMPOSER
28 #define GEGL_OP_C_SOURCE weighted-blend.c
29 #define GEGL_OP_NAME weighted_blend
33 #include "opencl/gegl-cl.h"
34 #include "opencl/weighted-blend.cl.h"
36 static GeglClRunData
*cl_data
= NULL
;
39 cl_process (GeglOperation
*self
,
43 size_t global_worksize
,
44 const GeglRectangle
*roi
,
51 const char *kernel_name
[] = {"cl_copy_weigthed_blend",
54 cl_data
= gegl_cl_compile_and_build (weighted_blend_cl_source
,
57 if (!cl_data
) return TRUE
;
62 cl_err
= gegl_clSetKernelArg(cl_data
->kernel
[0], 0, sizeof(cl_mem
), (void*)&in_tex
);
64 cl_err
= gegl_clSetKernelArg(cl_data
->kernel
[0], 1, sizeof(cl_mem
), (void*)&out_tex
);
67 cl_err
= gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue (),
68 cl_data
->kernel
[0], 1,
69 NULL
, &global_worksize
, NULL
,
75 cl_err
= gegl_clSetKernelArg(cl_data
->kernel
[1], 0, sizeof(cl_mem
), (void*)&in_tex
);
77 cl_err
= gegl_clSetKernelArg(cl_data
->kernel
[1], 1, sizeof(cl_mem
), (void*)&aux_tex
);
79 cl_err
= gegl_clSetKernelArg(cl_data
->kernel
[1], 2, sizeof(cl_mem
), (void*)&out_tex
);
82 cl_err
= gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue (),
83 cl_data
->kernel
[1], 1,
84 NULL
, &global_worksize
, NULL
,
96 process (GeglOperation
*op
,
101 const GeglRectangle
*roi
,
105 gfloat
*out
= out_buf
;
106 gfloat
*aux
= aux_buf
;
111 /* there is no auxilary buffer.
112 * output the input buffer.
114 for (i
= 0; i
< n_pixels
; i
++)
117 for (j
= 0; j
< 4; j
++)
127 for (i
=0; i
<n_pixels
; i
++)
131 /* find the proportion between alpha values */
132 total_alpha
= in
[3] + aux
[3];
135 /* no coverage from any source pixel */
136 for (j
= 0; j
< 4; j
++)
143 /* the total alpha is non-zero, therefore we may find a colour from a weighted blend */
144 gfloat in_weight
= in
[3] / total_alpha
;
145 gfloat aux_weight
= 1.0 - in_weight
;
146 for (j
= 0; j
< 3; j
++)
148 out
[j
] = in_weight
* in
[j
] + aux_weight
* aux
[j
];
150 out
[3] = total_alpha
;
162 gegl_op_class_init (GeglOpClass
*klass
)
164 GeglOperationClass
*operation_class
;
165 GeglOperationPointComposerClass
*point_composer_class
;
167 operation_class
= GEGL_OPERATION_CLASS (klass
);
168 point_composer_class
= GEGL_OPERATION_POINT_COMPOSER_CLASS (klass
);
170 point_composer_class
->process
= process
;
171 point_composer_class
->cl_process
= cl_process
;
172 operation_class
->opencl_support
= TRUE
;
174 gegl_operation_class_set_keys (operation_class
,
175 "name" , "gegl:weighted-blend",
176 "title", _("Weighted Blend"),
177 "categories" , "compositors:blend",
178 "reference-hash", "8013d8c837dd6b38579b4437703ed512",
180 _("blend two images using alpha values as weights"),