1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * gimpoperationgrow.c
5 * Copyright (C) 2012 Michael Natterer <mitch@gimp.org>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include <cairo.h>
24 #include <gegl.h>
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26
27 #include "libgimpcolor/gimpcolor.h"
28 #include "libgimpmath/gimpmath.h"
29
30 #include "operations-types.h"
31
32 #include "gimpoperationgrow.h"
33
34
35 enum
36 {
37 PROP_0,
38 PROP_RADIUS_X,
39 PROP_RADIUS_Y
40 };
41
42
43 static void gimp_operation_grow_get_property (GObject *object,
44 guint property_id,
45 GValue *value,
46 GParamSpec *pspec);
47 static void gimp_operation_grow_set_property (GObject *object,
48 guint property_id,
49 const GValue *value,
50 GParamSpec *pspec);
51
52 static void gimp_operation_grow_prepare (GeglOperation *operation);
53 static GeglRectangle
54 gimp_operation_grow_get_required_for_output (GeglOperation *self,
55 const gchar *input_pad,
56 const GeglRectangle *roi);
57 static GeglRectangle
58 gimp_operation_grow_get_cached_region (GeglOperation *self,
59 const GeglRectangle *roi);
60
61 static gboolean gimp_operation_grow_process (GeglOperation *operation,
62 GeglBuffer *input,
63 GeglBuffer *output,
64 const GeglRectangle *roi,
65 gint level);
66
67
G_DEFINE_TYPE(GimpOperationGrow,gimp_operation_grow,GEGL_TYPE_OPERATION_FILTER)68 G_DEFINE_TYPE (GimpOperationGrow, gimp_operation_grow,
69 GEGL_TYPE_OPERATION_FILTER)
70
71 #define parent_class gimp_operation_grow_parent_class
72
73
74 static void
75 gimp_operation_grow_class_init (GimpOperationGrowClass *klass)
76 {
77 GObjectClass *object_class = G_OBJECT_CLASS (klass);
78 GeglOperationClass *operation_class = GEGL_OPERATION_CLASS (klass);
79 GeglOperationFilterClass *filter_class = GEGL_OPERATION_FILTER_CLASS (klass);
80
81 object_class->set_property = gimp_operation_grow_set_property;
82 object_class->get_property = gimp_operation_grow_get_property;
83
84 gegl_operation_class_set_keys (operation_class,
85 "name", "gimp:grow",
86 "categories", "gimp",
87 "description", "GIMP Grow operation",
88 NULL);
89
90 operation_class->prepare = gimp_operation_grow_prepare;
91 operation_class->get_required_for_output = gimp_operation_grow_get_required_for_output;
92 operation_class->get_cached_region = gimp_operation_grow_get_cached_region;
93 operation_class->threaded = FALSE;
94
95 filter_class->process = gimp_operation_grow_process;
96
97 g_object_class_install_property (object_class, PROP_RADIUS_X,
98 g_param_spec_int ("radius-x",
99 "Radius X",
100 "Grow radius in X direction",
101 1, 2342, 1,
102 G_PARAM_READWRITE |
103 G_PARAM_CONSTRUCT));
104
105 g_object_class_install_property (object_class, PROP_RADIUS_Y,
106 g_param_spec_int ("radius-y",
107 "Radius Y",
108 "Grow radius in Y direction",
109 1, 2342, 1,
110 G_PARAM_READWRITE |
111 G_PARAM_CONSTRUCT));
112 }
113
114 static void
gimp_operation_grow_init(GimpOperationGrow * self)115 gimp_operation_grow_init (GimpOperationGrow *self)
116 {
117 }
118
119 static void
gimp_operation_grow_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)120 gimp_operation_grow_get_property (GObject *object,
121 guint property_id,
122 GValue *value,
123 GParamSpec *pspec)
124 {
125 GimpOperationGrow *self = GIMP_OPERATION_GROW (object);
126
127 switch (property_id)
128 {
129 case PROP_RADIUS_X:
130 g_value_set_int (value, self->radius_x);
131 break;
132
133 case PROP_RADIUS_Y:
134 g_value_set_int (value, self->radius_y);
135 break;
136
137 default:
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
139 break;
140 }
141 }
142
143 static void
gimp_operation_grow_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)144 gimp_operation_grow_set_property (GObject *object,
145 guint property_id,
146 const GValue *value,
147 GParamSpec *pspec)
148 {
149 GimpOperationGrow *self = GIMP_OPERATION_GROW (object);
150
151 switch (property_id)
152 {
153 case PROP_RADIUS_X:
154 self->radius_x = g_value_get_int (value);
155 break;
156
157 case PROP_RADIUS_Y:
158 self->radius_y = g_value_get_int (value);
159 break;
160
161 default:
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
163 break;
164 }
165 }
166
167 static void
gimp_operation_grow_prepare(GeglOperation * operation)168 gimp_operation_grow_prepare (GeglOperation *operation)
169 {
170 const Babl *space = gegl_operation_get_source_space (operation, "input");
171 gegl_operation_set_format (operation, "input", babl_format_with_space ("Y float", space));
172 gegl_operation_set_format (operation, "output", babl_format_with_space ("Y float", space));
173 }
174
175 static GeglRectangle
gimp_operation_grow_get_required_for_output(GeglOperation * self,const gchar * input_pad,const GeglRectangle * roi)176 gimp_operation_grow_get_required_for_output (GeglOperation *self,
177 const gchar *input_pad,
178 const GeglRectangle *roi)
179 {
180 return *gegl_operation_source_get_bounding_box (self, "input");
181 }
182
183 static GeglRectangle
gimp_operation_grow_get_cached_region(GeglOperation * self,const GeglRectangle * roi)184 gimp_operation_grow_get_cached_region (GeglOperation *self,
185 const GeglRectangle *roi)
186 {
187 return *gegl_operation_source_get_bounding_box (self, "input");
188 }
189
190 static void
compute_border(gint16 * circ,guint16 xradius,guint16 yradius)191 compute_border (gint16 *circ,
192 guint16 xradius,
193 guint16 yradius)
194 {
195 gint32 i;
196 gint32 diameter = xradius * 2 + 1;
197 gdouble tmp;
198
199 for (i = 0; i < diameter; i++)
200 {
201 if (i > xradius)
202 tmp = (i - xradius) - 0.5;
203 else if (i < xradius)
204 tmp = (xradius - i) - 0.5;
205 else
206 tmp = 0.0;
207
208 circ[i] = RINT (yradius /
209 (gdouble) xradius * sqrt (SQR (xradius) - SQR (tmp)));
210 }
211 }
212
213 static inline void
rotate_pointers(gfloat ** p,guint32 n)214 rotate_pointers (gfloat **p,
215 guint32 n)
216 {
217 guint32 i;
218 gfloat *tmp;
219
220 tmp = p[0];
221
222 for (i = 0; i < n - 1; i++)
223 p[i] = p[i + 1];
224
225 p[i] = tmp;
226 }
227
228 static gboolean
gimp_operation_grow_process(GeglOperation * operation,GeglBuffer * input,GeglBuffer * output,const GeglRectangle * roi,gint level)229 gimp_operation_grow_process (GeglOperation *operation,
230 GeglBuffer *input,
231 GeglBuffer *output,
232 const GeglRectangle *roi,
233 gint level)
234 {
235 /* Any bugs in this function are probably also in thin_region.
236 * Blame all bugs in this function on jaycox@gimp.org
237 */
238 GimpOperationGrow *self = GIMP_OPERATION_GROW (operation);
239 const Babl *input_format = gegl_operation_get_format (operation, "input");
240 const Babl *output_format = gegl_operation_get_format (operation, "output");
241 gint32 i, j, x, y;
242 gfloat **buf; /* caches the region's pixel data */
243 gfloat *out; /* holds the new scan line we are computing */
244 gfloat **max; /* caches the largest values for each column */
245 gint16 *circ; /* holds the y coords of the filter's mask */
246 gfloat last_max;
247 gint16 last_index;
248 gfloat *buffer;
249
250 max = g_new (gfloat *, roi->width + 2 * self->radius_x);
251 buf = g_new (gfloat *, self->radius_y + 1);
252
253 for (i = 0; i < self->radius_y + 1; i++)
254 buf[i] = g_new (gfloat, roi->width);
255
256 buffer = g_new (gfloat,
257 (roi->width + 2 * self->radius_x) * (self->radius_y + 1));
258
259 for (i = 0; i < roi->width + 2 * self->radius_x; i++)
260 {
261 if (i < self->radius_x)
262 max[i] = buffer;
263 else if (i < roi->width + self->radius_x)
264 max[i] = &buffer[(self->radius_y + 1) * (i - self->radius_x)];
265 else
266 max[i] = &buffer[(self->radius_y + 1) * (roi->width + self->radius_x - 1)];
267
268 for (j = 0; j < self->radius_y + 1; j++)
269 max[i][j] = 0.0;
270 }
271
272 /* offset the max pointer by self->radius_x so the range of the
273 * array is [-self->radius_x] to [roi->width + self->radius_x]
274 */
275 max += self->radius_x;
276
277 out = g_new (gfloat, roi->width);
278
279 circ = g_new (gint16, 2 * self->radius_x + 1);
280 compute_border (circ, self->radius_x, self->radius_y);
281
282 /* offset the circ pointer by self->radius_x so the range of the
283 * array is [-self->radius_x] to [self->radius_x]
284 */
285 circ += self->radius_x;
286
287 memset (buf[0], 0, roi->width * sizeof (gfloat));
288
289 for (i = 0; i < self->radius_y && i < roi->height; i++) /* load top of image */
290 gegl_buffer_get (input,
291 GEGL_RECTANGLE (roi->x, roi->y + i,
292 roi->width, 1),
293 1.0, input_format, buf[i + 1],
294 GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
295
296 for (x = 0; x < roi->width; x++) /* set up max for top of image */
297 {
298 max[x][0] = 0.0; /* buf[0][x] is always 0 */
299 max[x][1] = buf[1][x]; /* MAX (buf[1][x], max[x][0]) always = buf[1][x]*/
300
301 for (j = 2; j < self->radius_y + 1; j++)
302 max[x][j] = MAX (buf[j][x], max[x][j - 1]);
303 }
304
305 for (y = 0; y < roi->height; y++)
306 {
307 rotate_pointers (buf, self->radius_y + 1);
308
309 if (y < roi->height - (self->radius_y))
310 gegl_buffer_get (input,
311 GEGL_RECTANGLE (roi->x, roi->y + y + self->radius_y,
312 roi->width, 1),
313 1.0, input_format, buf[self->radius_y],
314 GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
315 else
316 memset (buf[self->radius_y], 0, roi->width * sizeof (gfloat));
317
318 for (x = 0; x < roi->width; x++) /* update max array */
319 {
320 for (i = self->radius_y; i > 0; i--)
321 max[x][i] = MAX (MAX (max[x][i - 1], buf[i - 1][x]), buf[i][x]);
322
323 max[x][0] = buf[0][x];
324 }
325
326 last_max = max[0][circ[-1]];
327 last_index = 1;
328
329 for (x = 0; x < roi->width; x++) /* render scan line */
330 {
331 last_index--;
332
333 if (last_index >= 0)
334 {
335 if (last_max >= 1.0)
336 {
337 out[x] = 1.0;
338 }
339 else
340 {
341 last_max = 0.0;
342
343 for (i = self->radius_x; i >= 0; i--)
344 if (last_max < max[x + i][circ[i]])
345 {
346 last_max = max[x + i][circ[i]];
347 last_index = i;
348 }
349
350 out[x] = last_max;
351 }
352 }
353 else
354 {
355 last_index = self->radius_x;
356 last_max = max[x + self->radius_x][circ[self->radius_x]];
357
358 for (i = self->radius_x - 1; i >= -self->radius_x; i--)
359 if (last_max < max[x + i][circ[i]])
360 {
361 last_max = max[x + i][circ[i]];
362 last_index = i;
363 }
364
365 out[x] = last_max;
366 }
367 }
368
369 gegl_buffer_set (output,
370 GEGL_RECTANGLE (roi->x, roi->y + y,
371 roi->width, 1),
372 0, output_format, out,
373 GEGL_AUTO_ROWSTRIDE);
374 }
375
376 /* undo the offsets to the pointers so we can free the malloced memory */
377 circ -= self->radius_x;
378 max -= self->radius_x;
379
380 g_free (circ);
381 g_free (buffer);
382 g_free (max);
383
384 for (i = 0; i < self->radius_y + 1; i++)
385 g_free (buf[i]);
386
387 g_free (buf);
388 g_free (out);
389
390 return TRUE;
391 }
392