/
fractal-trace.c
323 lines (272 loc) · 9.35 KB
/
fractal-trace.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
/* 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/>.
*
* Copyright (C) 1997 Hirotsuna Mizuno <s1041150@u-aizu.ac.jp>
* Copyright (C) 2011 Robert Sasu <sasu.robert@gmail.com>
*/
#include "config.h"
#include <glib/gi18n-lib.h>
#ifdef GEGL_CHANT_PROPERTIES
gegl_chant_string (fractal, _("Fractal"), "fractal",
_("Type of fractal to use. "
"Choices are julia, mandelbrot. Default is mandelbrot."))
gegl_chant_double (X1, _("X1"), -50.0, 50.0, -1.00,
_("X1 value, position"))
gegl_chant_double (X2, _("X2"), -50.0, 50.0, 0.50,
_("X2 value, position"))
gegl_chant_double (Y1, _("Y1"), -50.0, 50.0, -1.00,
_("X2 value, position"))
gegl_chant_double (Y2, _("Y2"), -50.0, 50.0, 1.00,
_("Y2 value, position"))
gegl_chant_double (JX, _("JX"), -50.0, 50.0, 0.5,
_("Julia seed X value, position"))
gegl_chant_double (JY, _("JY"), -50.0, 50.0, 0.5,
_("Julia seed Y value, position"))
gegl_chant_int (depth, _("Depth"), 1, 65536, 3,
_("Depth value"))
gegl_chant_double (bailout, _("Bailout"), 0.0, G_MAXDOUBLE, G_MAXDOUBLE,
_("Bailout length"))
gegl_chant_string (background, _("Background"), "wrap",
_("Optional parameter to override automatic selection of wrap background. "
"Choices are wrap, black, white and transparent."))
#else
#define GEGL_CHANT_TYPE_FILTER
#define GEGL_CHANT_C_FILE "fractal-trace.c"
#include "gegl-chant.h"
#include <math.h>
typedef enum
{
BACKGROUND_TYPE_WRAP,
BACKGROUND_TYPE_TRANSPARENT,
BACKGROUND_TYPE_BLACK,
BACKGROUND_TYPE_WHITE
} BackgroundType;
typedef enum
{
FRACTAL_TYPE_MANDELBROT,
FRACTAL_TYPE_JULIA
} FractalType;
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 void
julia (gdouble x,
gdouble y,
gdouble jx,
gdouble jy,
gdouble *u,
gdouble *v,
gint depth,
gdouble bailout2)
{
gint i;
gdouble xx = x;
gdouble yy = y;
for (i = 0; i < depth; i++)
{
gdouble x2, y2, tmp;
x2 = xx * xx;
y2 = yy * yy;
tmp = x2 - y2 + jx;
yy = 2 * xx * yy + jy;
xx = tmp;
if ((x2 + y2) > bailout2)
break;
}
*u = xx;
*v = yy;
}
static void
fractaltrace (GeglBuffer *input,
const GeglRectangle *picture,
gfloat *dst_buf,
const GeglRectangle *roi,
GeglChantO *o,
gint y,
FractalType fractal_type,
BackgroundType background_type,
Babl *format)
{
GeglMatrix2 scale; /* a matrix indicating scaling factors around the
current center pixel.
*/
gint x, i, offset;
gdouble scale_x, scale_y;
gdouble bailout2;
gfloat dest[4];
scale_x = (o->X2 - o->X1) / picture->width;
scale_y = (o->Y2 - o->Y1) / picture->height;
bailout2 = o->bailout * o->bailout;
offset = (y - roi->y) * roi->width * 4;
for (x = roi->x; x < roi->x + roi->width; x++)
{
gdouble cx, cy;
gdouble px, py;
dest[1] = dest[2] = dest[3] = dest[0] = 0.0;
switch (fractal_type)
{
case FRACTAL_TYPE_JULIA:
#define gegl_unmap(u,v,ud,vd) {\
gdouble rx, ry;\
cx = o->X1 + ((u) - picture->x) * scale_x; \
cy = o->Y1 + ((v) - picture->y) * scale_y; \
julia (cx, cy, o->JX, o->JY, &rx, &ry, o->depth, bailout2);\
ud = (rx - o->X1) / scale_x + picture->x;\
vd = (ry - o->Y1) / scale_y + picture->y;\
}
//gegl_sampler_compute_scale (scale, x, y);
gegl_unmap(x,y,px,py);
#undef gegl_unmap
break;
case FRACTAL_TYPE_MANDELBROT:
#define gegl_unmap(u,v,ud,vd) {\
gdouble rx, ry;\
cx = o->X1 + ((u) - picture->x) * scale_x; \
cy = o->Y1 + ((v) - picture->y) * scale_y; \
julia (cx, cy, cx, cy, &rx, &ry, o->depth, bailout2);\
ud = (rx - o->X1) / scale_x + picture->x;\
vd = (ry - o->Y1) / scale_y + picture->y;\
}
// gegl_sampler_compute_scale (scale, x, y);
gegl_unmap(x,y,px,py);
#undef gegl_unmap
break;
default:
g_error (_("Unsupported fractal type"));
}
if (0 <= px && px < picture->width && 0 <= py && py < picture->height)
{
gegl_buffer_sample (input, px, py, 1.0, dest, format,
GEGL_INTERPOLATION_LINEAR);
}
else
{
switch (background_type)
{
case BACKGROUND_TYPE_WRAP:
px = fmod (px, picture->width);
py = fmod (py, picture->height);
if (px < 0.0)
px += picture->width;
if (py < 0.0)
py += picture->height;
/* Check for NaN */
if (isnan (px))
{
if (signbit (px))
px = 0.0;
else
px = picture->width - 1.0;
}
if (isnan (py))
{
if (signbit (py))
py = 0.0;
else
py = picture->height - 1.0;
}
gegl_buffer_sample (input, px, py, 1.0, dest, format,
GEGL_INTERPOLATION_LINEAR);
break;
case BACKGROUND_TYPE_TRANSPARENT:
dest[0] = dest[1] = dest[2] = dest[3] = 0.0;
break;
case BACKGROUND_TYPE_BLACK:
dest[0] = dest[1] = dest[2] = 0.0;
dest[3] = 1.0;
break;
case BACKGROUND_TYPE_WHITE:
dest[0] = dest[1] = dest[2] = dest[3] = 1.0;
break;
}
}
for (i = 0; i < 4; i++)
dst_buf[offset++] = dest[i];
}
}
static gboolean
process (GeglOperation *operation,
GeglBuffer *input,
GeglBuffer *output,
const GeglRectangle *result)
{
GeglChantO *o;
GeglRectangle boundary;
Babl *format;
FractalType fractal_type;
BackgroundType background_type;
gfloat *dst_buf;
gint y;
o = GEGL_CHANT_PROPERTIES (operation);
boundary = gegl_operation_get_bounding_box (operation);
fractal_type = FRACTAL_TYPE_MANDELBROT;
if (!strcmp (o->fractal, "mandelbrot"))
fractal_type = FRACTAL_TYPE_MANDELBROT;
else if (!strcmp(o->fractal, "julia"))
fractal_type = FRACTAL_TYPE_JULIA;
background_type = BACKGROUND_TYPE_WRAP;
if (!strcmp (o->background, "wrap"))
background_type = BACKGROUND_TYPE_WRAP;
else if (!strcmp (o->background, "transparent"))
background_type = BACKGROUND_TYPE_TRANSPARENT;
else if (!strcmp (o->background, "black"))
background_type = BACKGROUND_TYPE_BLACK;
else if (!strcmp (o->background, "white"))
background_type = BACKGROUND_TYPE_WHITE;
format = babl_format ("RGBA float");
dst_buf = g_new0 (gfloat, result->width * result->height * 4);
for (y = result->y; y < result->y + result->height; y++)
fractaltrace (input, &boundary, dst_buf, result, o, y,
fractal_type, background_type, format);
gegl_buffer_set (output, result, format, dst_buf, GEGL_AUTO_ROWSTRIDE);
g_free (dst_buf);
gegl_buffer_sample_cleanup (input);
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 = "map";
operation_class->name = "gegl:fractal-trace";
operation_class->description = _("Performs fractal trace on the image");
}
#endif