Update Chinese (China) translation
[gegl.git] / operations / common / weighted-blend.c
blob349cd5d55abf5f76040fb80917121b6b3d85e704
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>
17 * 2008 James Legg
19 #include "config.h"
20 #include <glib/gi18n-lib.h>
23 #ifdef GEGL_PROPERTIES
25 #else
27 #define GEGL_OP_POINT_COMPOSER
28 #define GEGL_OP_C_SOURCE weighted-blend.c
29 #define GEGL_OP_NAME weighted_blend
31 #include "gegl-op.h"
33 #include "opencl/gegl-cl.h"
34 #include "opencl/weighted-blend.cl.h"
36 static GeglClRunData *cl_data = NULL;
38 static gboolean
39 cl_process (GeglOperation *self,
40 cl_mem in_tex,
41 cl_mem aux_tex,
42 cl_mem out_tex,
43 size_t global_worksize,
44 const GeglRectangle *roi,
45 gint level)
47 gint cl_err = 0;
49 if (!cl_data)
51 const char *kernel_name[] = {"cl_copy_weigthed_blend",
52 "cl_weighted_blend",
53 NULL};
54 cl_data = gegl_cl_compile_and_build (weighted_blend_cl_source,
55 kernel_name);
57 if (!cl_data) return TRUE;
60 if (!aux_tex)
62 cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 0, sizeof(cl_mem), (void*)&in_tex);
63 CL_CHECK;
64 cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 1, sizeof(cl_mem), (void*)&out_tex);
65 CL_CHECK;
67 cl_err = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue (),
68 cl_data->kernel[0], 1,
69 NULL, &global_worksize, NULL,
70 0, NULL, NULL);
71 CL_CHECK;
73 else
75 cl_err = gegl_clSetKernelArg(cl_data->kernel[1], 0, sizeof(cl_mem), (void*)&in_tex);
76 CL_CHECK;
77 cl_err = gegl_clSetKernelArg(cl_data->kernel[1], 1, sizeof(cl_mem), (void*)&aux_tex);
78 CL_CHECK;
79 cl_err = gegl_clSetKernelArg(cl_data->kernel[1], 2, sizeof(cl_mem), (void*)&out_tex);
80 CL_CHECK;
82 cl_err = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue (),
83 cl_data->kernel[1], 1,
84 NULL, &global_worksize, NULL,
85 0, NULL, NULL);
86 CL_CHECK;
89 return FALSE;
91 error:
92 return TRUE;
95 static gboolean
96 process (GeglOperation *op,
97 void *in_buf,
98 void *aux_buf,
99 void *out_buf,
100 glong n_pixels,
101 const GeglRectangle *roi,
102 gint level)
104 gfloat *in = in_buf;
105 gfloat *out = out_buf;
106 gfloat *aux = aux_buf;
107 gint i;
109 if (aux == NULL)
111 /* there is no auxilary buffer.
112 * output the input buffer.
114 for (i = 0; i < n_pixels; i++)
116 gint j;
117 for (j = 0; j < 4; j++)
119 out[j] = in[j];
121 in += 4;
122 out += 4;
125 else
127 for (i=0; i<n_pixels; i++)
129 gint j;
130 gfloat total_alpha;
131 /* find the proportion between alpha values */
132 total_alpha = in[3] + aux[3];
133 if (!total_alpha)
135 /* no coverage from any source pixel */
136 for (j = 0; j < 4; j++)
138 out[j] = 0.0;
141 else
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;
152 in += 4;
153 aux += 4;
154 out += 4;
158 return TRUE;
161 static void
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",
179 "description",
180 _("blend two images using alpha values as weights"),
181 NULL);
183 #endif