-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolar-coordinates.c
404 lines (342 loc) · 10.6 KB
/
polar-coordinates.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* 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
* License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
*
* Polarize plug-in --- maps a rectangle to a circle or vice-versa
* Copyright (C) 1997 Daniel Dunbar
* Email: [email protected]
* WWW: http://millennium.diads.com/gimp/
* Copyright (C) 1997 Federico Mena Quintero
* Copyright (C) 1996 Marc Bless
* E-mail: [email protected]
* WWW: www.ai-lab.fh-furtwangen.de/~bless
*
* Copyright (C) 2011 Robert Sasu <[email protected]>
*/
#include "config.h"
#include <glib/gi18n-lib.h>
#ifdef GEGL_CHANT_PROPERTIES
gegl_chant_double (depth, _("Circle depth in percent"), 0.0, 100.0, 100.0,
_("Circle depth in percent."))
gegl_chant_double (angle, _("Offset angle"), 0.0, 359.9, 0.0,
_("Offset angle."))
gegl_chant_boolean (bw, _("Map backwards"), FALSE, _("Map backwards."))
gegl_chant_boolean (top, _("Map from top"), TRUE, _("Map from top"))
gegl_chant_boolean (polar, _("To polar"), TRUE, _("To polar."))
gegl_chant_int (pole_x, _("X:"), 0, G_MAXINT, 0,
_("Origin point for the polar coordinates"))
gegl_chant_int (pole_y, _("Y:"), 0, G_MAXINT, 0,
_("Origin point for the polar coordinates"))
gegl_chant_boolean (middle, _("Choose middle"), TRUE,
_("Let origin point to be the middle one"))
#else
#define GEGL_CHANT_TYPE_FILTER
#define GEGL_CHANT_C_FILE "polar-coordinates.c"
#include "gegl-chant.h"
#include <stdio.h>
#include <math.h>
#define WITHIN(a, b, c) ((((a) <= (b)) && ((b) <= (c))) ? 1 : 0)
#define SQR(x) (x)*(x)
#define SCALE_WIDTH 200
#define ENTRY_WIDTH 60
static void prepare (GeglOperation *operation)
{
gegl_operation_set_format (operation, "input",
babl_format ("RGBA float"));
gegl_operation_set_format (operation, "output",
babl_format ("RGBA float"));
}
static gboolean
calc_undistorted_coords (gdouble wx,
gdouble wy,
gdouble *x,
gdouble *y,
GeglChantO *o,
GeglRectangle boundary)
{
gboolean inside;
gdouble phi, phi2;
gdouble xx, xm, ym, yy;
gint xdiff, ydiff;
gdouble r;
gdouble m;
gdouble xmax, ymax, rmax;
gdouble x_calc, y_calc;
gdouble xi, yi, cen_x, cen_y;
gdouble circle, angl, t, angle;
gint x1, x2, y1, y2;
/* initialize */
phi = 0.0;
r = 0.0;
x1 = 0;
y1 = 0;
x2 = boundary.width;
y2 = boundary.height;
xdiff = x2 - x1;
ydiff = y2 - y1;
xm = xdiff / 2.0;
ym = ydiff / 2.0;
circle = o->depth;
angle = o->angle;
angl = (gdouble) angle / 180.0 * G_PI;
cen_x = o->pole_x;
cen_y = o->pole_y;
if (o->polar)
{
if (wx >= cen_x)
{
if (wy > cen_y)
{
phi = G_PI - atan (((double)(wx - cen_x))/
((double)(wy - cen_y)));
}
else if (wy < cen_y)
{
phi = atan (((double)(wx - cen_x))/((double)(cen_y - wy)));
}
else
{
phi = G_PI / 2;
}
}
else if (wx < cen_x)
{
if (wy < cen_y)
{
phi = 2 * G_PI - atan (((double)(cen_x -wx)) /
((double)(cen_y - wy)));
}
else if (wy > cen_y)
{
phi = G_PI + atan (((double)(cen_x - wx))/
((double)(wy - cen_y)));
}
else
{
phi = 1.5 * G_PI;
}
}
r = sqrt (SQR (wx - cen_x) + SQR (wy - cen_y));
if (wx != cen_x)
{
m = fabs (((double)(wy - cen_y)) / ((double)(wx - cen_x)));
}
else
{
m = 0;
}
if (m <= ((double)(y2 - y1) / (double)(x2 - x1)))
{
if (wx == cen_x)
{
xmax = 0;
ymax = cen_y - y1;
}
else
{
xmax = cen_x - x1;
ymax = m * xmax;
}
}
else
{
ymax = cen_y - y1;
xmax = ymax / m;
}
rmax = sqrt ( (double)(SQR (xmax) + SQR (ymax)) );
t = ((cen_y - y1) < (cen_x - x1)) ? (cen_y - y1) : (cen_x - x1);
rmax = (rmax - t) / 100 * (100 - circle) + t;
phi = fmod (phi + angl, 2*G_PI);
if (o->bw)
x_calc = x2 - 1 - (x2 - x1 - 1)/(2*G_PI) * phi;
else
x_calc = (x2 - x1 - 1)/(2*G_PI) * phi + x1;
if (o->top)
y_calc = (y2 - y1)/rmax * r + y1;
else
y_calc = y2 - (y2 - y1)/rmax * r;
}
else
{
if (o->bw)
phi = (2 * G_PI) * (x2 - wx) / xdiff;
else
phi = (2 * G_PI) * (wx - x1) / xdiff;
phi = fmod (phi + angl, 2 * G_PI);
if (phi >= 1.5 * G_PI)
phi2 = 2 * G_PI - phi;
else if (phi >= G_PI)
phi2 = phi - G_PI;
else if (phi >= 0.5 * G_PI)
phi2 = G_PI - phi;
else
phi2 = phi;
xx = tan (phi2);
if (xx != 0)
m = (double) 1.0 / xx;
else
m = 0;
if (m <= ((double)(ydiff) / (double)(xdiff)))
{
if (phi2 == 0)
{
xmax = 0;
ymax = ym - y1;
}
else
{
xmax = xm - x1;
ymax = m * xmax;
}
}
else
{
ymax = ym - y1;
xmax = ymax / m;
}
rmax = sqrt ((double)(SQR (xmax) + SQR (ymax)));
t = ((ym - y1) < (xm - x1)) ? (ym - y1) : (xm - x1);
rmax = (rmax - t) / 100.0 * (100 - circle) + t;
if (o->top)
r = rmax * (double)((wy - y1) / (double)(ydiff));
else
r = rmax * (double)((y2 - wy) / (double)(ydiff));
xx = r * sin (phi2);
yy = r * cos (phi2);
if (phi >= 1.5 * G_PI)
{
x_calc = (double)xm - xx;
y_calc = (double)ym - yy;
}
else if (phi >= G_PI)
{
x_calc = (double)xm - xx;
y_calc = (double)ym + yy;
}
else if (phi >= 0.5 * G_PI)
{
x_calc = (double)xm + xx;
y_calc = (double)ym + yy;
}
else
{
x_calc = (double)xm + xx;
y_calc = (double)ym - yy;
}
}
xi = (int) (x_calc + 0.5);
yi = (int) (y_calc + 0.5);
inside = (WITHIN (0, xi, boundary.width - 1) && WITHIN (0, yi, boundary.height - 1));
if (inside)
{
*x = x_calc;
*y = y_calc;
}
return inside;
}
static GeglRectangle
get_effective_area (GeglOperation *operation)
{
GeglRectangle result = {0,0,0,0};
GeglRectangle *in_rect = gegl_operation_source_get_bounding_box (operation, "input");
gegl_rectangle_copy(&result, in_rect);
return result;
}
static gboolean
process (GeglOperation *operation,
GeglBuffer *input,
GeglBuffer *output,
const GeglRectangle *result)
{
GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
GeglRectangle boundary = get_effective_area (operation);
Babl *format = babl_format ("RGBA float");
gint x,y;
gfloat *src_buf, *dst_buf;
gfloat dest[4];
gint i, offset = 0;
gboolean inside;
gdouble px, py;
GeglMatrix2 scale; /* a matrix indicating scaling factors around the
current center pixel.
*/
src_buf = g_new0 (gfloat, result->width * result->height * 4);
dst_buf = g_new0 (gfloat, result->width * result->height * 4);
gegl_buffer_get (input, 1.0, result, format, src_buf, GEGL_AUTO_ROWSTRIDE);
if (o->middle)
{
o->pole_x = boundary.width / 2;
o->pole_y = boundary.height / 2;
}
for (y = result->y; y < result->y + result->height; y++)
for (x = result->x; x < result->x + result->width; x++)
{
#define gegl_unmap(u,v,ud,vd) {\
gdouble rx, ry;\
inside = calc_undistorted_coords ((gdouble)x, (gdouble)y, \
&rx, &ry, o, boundary);\
ud = rx;\
vd = ry;\
}
gegl_sampler_compute_scale (scale, x, y);
gegl_unmap(x,y,px,py);
#undef gegl_unmap
if (inside)
gegl_buffer_sample (input, px, py, &scale, dest, format,
GEGL_INTERPOLATION_LOHALO);
else
for (i=0; i<4; i++)
dest[i] = 0.0;
for (i=0; i<4; i++)
dst_buf[offset++] = dest[i];
}
gegl_buffer_set (output, result, format, dst_buf, GEGL_AUTO_ROWSTRIDE);
g_free (src_buf);
g_free (dst_buf);
return TRUE;
}
static GeglRectangle
get_bounding_box (GeglOperation *operation)
{
GeglRectangle result = {0,0,0,0};
GeglRectangle *in_rect;
in_rect = gegl_operation_source_get_bounding_box (operation, "input");
if (!in_rect)
return result;
return *in_rect;
}
static GeglRectangle
get_required_for_output (GeglOperation *operation,
const gchar *input_pad,
const GeglRectangle *roi)
{
return get_bounding_box (operation);
}
static void
gegl_chant_class_init (GeglChantClass *klass)
{
GeglOperationClass *operation_class;
GeglOperationFilterClass *filter_class;
operation_class = GEGL_OPERATION_CLASS (klass);
filter_class = GEGL_OPERATION_FILTER_CLASS (klass);
filter_class->process = process;
operation_class->prepare = prepare;
operation_class->get_bounding_box = get_bounding_box;
operation_class->get_required_for_output = get_required_for_output;
operation_class->categories = "enhance";
operation_class->name = "gegl:polar-coordinates";
operation_class->description = _("Performs polar-coordinates on the image.");
}
#endif