Debian Codegraph @

src/filter/softglow/softglow.c

raw symbols SCIP
1 /*
2 * This file is a modified port of Softglow plug-in from Gimp.
3 * It contains code from plug-ins/common/softglow.c, see that for copyrights.
4 *
5 * softglow.c
6 * Copyright 2012 Janne Liljeblad
7 *
8 * This file is a Frei0r plugin.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <math.h>
29 #include "frei0r.h"
30 #include "frei0r/blur.h"
31 #include "frei0r/math.h"
32
33 #define SIGMOIDAL_BASE 2
34 #define SIGMOIDAL_RANGE 20
35 #define NBYTES 4
36 #define ALPHA 3
37
38 double PI=3.14159265358979;
39
40 typedef struct softglow_instance
41 {
42 unsigned int width;
43 unsigned int height;
44 double blur;
45 double brightness;
46 double sharpness;
47 double blendtype;
48 f0r_instance_t* blur_instance;
49 uint32_t* sigm_frame;
50 uint32_t* blurred;
51 } softglow_instance_t;
52
53 void overlay(const uint32_t* source1, const uint32_t* source2, uint32_t* out, unsigned int len)
54 {
55 unsigned char* src1 = (unsigned char*)source1;
56 unsigned char* src2 = (unsigned char*)source2;
57 unsigned char* dst = (unsigned char*)out;
58
59 uint32_t b, tmp, tmpM;
60
61 while (len--)
62 {
63 for (b = 0; b < ALPHA; b++)
64 {
65 dst[b] = INT_MULT(src1[b], src1[b] + INT_MULT(2 * src2[b], 255 - src1[b], tmpM), tmp);
66 }
67
68 dst[ALPHA] = MIN(src1[ALPHA], src2[ALPHA]);
69
70 src1 += NBYTES;
71 src2 += NBYTES;
72 dst += NBYTES;
73 }
74 }
75
76 void screen(const uint32_t* source1, const uint32_t* source2, uint32_t* out, unsigned int len)
77 {
78 unsigned char* src1 = (unsigned char*)source1;
79 unsigned char* src2 = (unsigned char*)source2;
80 unsigned char* dst = (unsigned char*)out;
81
82 uint32_t b, tmp;
83
84 while (len--)
85 {
86 for (b = 0; b < ALPHA; b++)
87 dst[b] = 255 - INT_MULT((255 - src1[b]), (255 - src2[b]), tmp);
88
89 dst[ALPHA] = MIN(src1[ALPHA], src2[ALPHA]);
90
91 src1 += NBYTES;
92 src2 += NBYTES;
93 dst += NBYTES;
94 }
95 }
96
97 void add(const uint32_t* source1, const uint32_t* source2, uint32_t* out, unsigned int len)
98 {
99 unsigned char* src1 = (unsigned char*)source1;
100 unsigned char* src2 = (unsigned char*)source2;
101 unsigned char* dst = (unsigned char*)out;
102
103 uint32_t b, val;
104
105 while (len--)
106 {
107 for (b = 0; b < ALPHA; b++)
108 {
109 val = src1[b] + src2[b];
110 dst[b] = CLAMP(val, 0, 255);
111 }
112
113 dst[ALPHA] = MIN(src1[ALPHA], src2[ALPHA]);
114
115 src1 += NBYTES;
116 src2 += NBYTES;
117 dst += NBYTES;
118 }
119 }
120
121 int
122 gimp_rgb_to_l_int (int red,
123 int green,
124 int blue)
125 {
126 int min, max;
127
128 if (red > green)
129 {
130 max = MAX (red, blue);
131 min = MIN (green, blue);
132 }
133 else
134 {
135 max = MAX (green, blue);
136 min = MIN (red, blue);
137 }
138
139 return ROUND ((max + min) / 2.0);
140 }
141
142 int f0r_init()
143 {
144 return 1;
145 }
146
147 void f0r_deinit()
148 { /* no initialization required */ }
149
150 void f0r_get_plugin_info(f0r_plugin_info_t* softglowInfo)
151 {
152 softglowInfo->name = "softglow";
153 softglowInfo->author = "Janne Liljeblad";
154 softglowInfo->plugin_type = F0R_PLUGIN_TYPE_FILTER;
155 softglowInfo->color_model = F0R_COLOR_MODEL_RGBA8888;
156 softglowInfo->frei0r_version = FREI0R_MAJOR_VERSION;
157 softglowInfo->major_version = 0;
158 softglowInfo->minor_version = 9;
159 softglowInfo->num_params = 4;
160 softglowInfo->explanation = "Does softglow effect on highlights";
161 }
162
163 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
164 {
165 switch ( param_index ) {
166 case 0:
167 info->name = "blur";
168 info->type = F0R_PARAM_DOUBLE;
169 info->explanation = "Blur of the glow";
170 break;
171 case 1:
172 info->name = "brightness";
173 info->type = F0R_PARAM_DOUBLE;
174 info->explanation = "Brightness of highlight areas";
175 break;
176 case 2:
177 info->name = "sharpness";
178 info->type = F0R_PARAM_DOUBLE;
179 info->explanation = "Sharpness of highlight areas";
180 break;
181 case 3: // 0 - 0.33 screen, 0.33 - 0.66 overla7, 0.66 - 1.0 add
182 info->name = "blurblend";
183 info->type = F0R_PARAM_DOUBLE;
184 info->explanation = "Blend mode used to blend highlight blur with input image";
185 break;
186 }
187 }
188
189 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
190 {
191 softglow_instance_t* inst = (softglow_instance_t*)calloc(1, sizeof(*inst));
192 inst->width = width;
193 inst->height = height;
194 inst->blur = 0.5;
195 inst->brightness = 0.75;
196 inst->sharpness = 0.85;
197 inst->blendtype = 0.0;
198 inst->blur_instance = (f0r_instance_t *)blur_construct(width, height);
199 inst->sigm_frame = (uint32_t*)malloc(width * height * sizeof(uint32_t));
200 inst->blurred = (uint32_t*)malloc(width * height * sizeof(uint32_t));
201 return (f0r_instance_t)inst;
202 }
203
204 void f0r_destruct(f0r_instance_t instance)
205 {
206 softglow_instance_t* inst = (softglow_instance_t*)instance;
207 blur_destruct(inst->blur_instance);
208 free(inst->sigm_frame);
209 free(inst->blurred);
210 free(instance);
211 }
212
213 void f0r_set_param_value(f0r_instance_t instance,
214 f0r_param_t param, int param_index)
215 {
216 softglow_instance_t* inst = (softglow_instance_t*)instance;
217 switch (param_index)
218 {
219 case 0:
220 inst->blur = *((double*)param);
221 blur_set_param_value(inst->blur_instance, &inst->blur, 0 );
222 break;
223 case 1:
224 inst->brightness = *((double*)param);
225 break;
226 case 2:
227 inst->sharpness = *((double*)param);
228 break;
229 case 3:
230 inst->blendtype = *((double*)param);
231 break;
232 }
233 }
234
235 void f0r_get_param_value(f0r_instance_t instance,
236 f0r_param_t param, int param_index)
237 {
238 softglow_instance_t* inst = (softglow_instance_t*)instance;
239 switch (param_index)
240 {
241 case 0:
242 *((double*)param) = inst->blur;
243 break;
244 case 1:
245 *((double*)param) = inst->brightness;
246 break;
247 case 2:
248 *((double*)param) = inst->sharpness;
249 break;
250 case 3:
251 *((double*)param) = inst->blendtype;
252 break;
253 }
254 }
255
256 void f0r_update(f0r_instance_t instance, double time,
257 const uint32_t* inframe, uint32_t* outframe)
258 {
259 // Check and cast instance
260 assert(instance);
261 softglow_instance_t* inst = (softglow_instance_t*)instance;
262 unsigned int len = inst->width * inst->height;
263
264 double brightness = inst->brightness;
265 double sharpness = inst->sharpness;
266
267 const unsigned char* src = (unsigned char*)inframe;
268
269 memcpy(inst->sigm_frame, inframe, len*sizeof(uint32_t));
270 unsigned char* dst = (unsigned char*)inst->sigm_frame;
271
272 unsigned char luma, r, g, b;
273 double val;
274 while (len--)
275 {
276 r = *src++;
277 g = *src++;
278 b = *src++;
279
280 //desaturate
281 luma = (unsigned char) gimp_rgb_to_l_int (r, g, b);
282
283 //compute sigmoidal transfer
284 val = luma / 255.0;
285 val = 255.0 / (1 + exp (-(SIGMOIDAL_BASE + (sharpness * SIGMOIDAL_RANGE)) * (val - 0.5)));
286 val = val * brightness;
287 luma = (unsigned char) CLAMP (val, 0, 255);
288
289 *dst++ = luma;
290 *dst++ = luma;
291 *dst++ = luma;
292 *dst++ = *src++;
293 }
294
295 blur_update(inst->blur_instance, 0.0, inst->sigm_frame, inst->blurred);
296
297 if (inst->blendtype <= 0.33)
298 screen(inst->blurred, inframe, outframe, inst->width * inst->height);
299 else if(inst->blendtype <= 0.66)
300 overlay(inst->blurred, inframe, outframe, inst->width * inst->height);
301 else
302 add(inst->blurred, inframe, outframe, inst->width * inst->height);
303 }
304
305
306
307