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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
/*
FLAM3 - cosmic recursive fractal flames
Copyright (C) 1992-2009 Spotworks LLC
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <argp.h>
#include "random.h"
#include "private.h"
#include "img.h"
#include "rect.h"
#define streq(a,b) (strcmp (a, b) == 0)
const char *argp_program_version =
"vlam3-pre";
typedef struct {
unsigned int threads, bpc, quality;
float scale;
} render_arguments;
static error_t parse_render_opt (int key, char *arg,
struct argp_state * const state) {
render_arguments * const arguments = state->input;
switch (key) {
case 'b': {
int i = atoi (arg);
if (i == 8 || i == 16) {
arguments->bpc = i;
} else {
argp_error (state, "Bits per channel must be 8 or 16");
}
break;
}
case 'q': {
int i = atoi (arg);
if (i < 1) {
argp_error (state, "Quality must be >= 1");
} else {
arguments->quality = i;
}
break;
}
case 's':
arguments->scale = atof (arg);
if (arguments->scale <= 0.0) {
argp_error (state, "Scale must be > 0");
}
break;
case 't': {
int i = atoi (arg);
if (i <= 0) {
argp_error (state, "Threads must be > 0");
} else {
arguments->threads = i;
}
break;
}
case ARGP_KEY_ARG:
if (state->arg_num > 0) {
return ARGP_ERR_UNKNOWN;
}
break;
case ARGP_KEY_END:
break;
default:
return ARGP_ERR_UNKNOWN;
break;
}
return 0;
}
static void do_render (const render_arguments * const arguments) {
randctx rc;
rand_seed(&rc);
int ncps;
flam3_genome * const cps = flam3_parse_from_file (stdin, NULL,
flam3_defaults_on, &ncps, &rc);
if (cps == NULL) {
fprintf(stderr,"error reading genomes from file\n");
exit(1);
}
assert (ncps == 1);
flam3_genome * const genome = &cps[0];
genome->sample_density = arguments->quality;
genome->height *= arguments->scale;
genome->width *= arguments->scale;
genome->pixels_per_unit *= arguments->scale;
flam3_frame f;
f.genomes = genome;
f.ngenomes = 1;
f.time = 0.0;
f.pixel_aspect_ratio = 1.0;
f.progress = 0;
f.nthreads = arguments->threads;
f.earlyclip = 0;
f.sub_batch_size = 10000;
f.bytes_per_channel = arguments->bpc / 8;
const unsigned int channels = 4;
const size_t this_size = channels * genome->width * genome->height *
f.bytes_per_channel;
void *image = (void *) calloc(this_size, sizeof(char));
stat_struct stats;
if (render_parallel (&f, image, &stats)) {
fprintf(stderr,"error rendering image: aborting.\n");
exit(1);
}
flam3_img_comments fpc;
write_png (stdout, image, genome->width, genome->height, &fpc,
f.bytes_per_channel);
}
static void print_genome (flam3_genome * const genome) {
printf("<pick version=\"FLAM3-%s\">\n", flam3_version());
flam3_print (stdout, genome, NULL, flam3_dont_print_edits);
printf("</pick>\n");
}
typedef struct {
int symmetry;
const char *palette;
unsigned int width, height;
} random_arguments;
static error_t parse_random_opt (int key, char *arg,
struct argp_state * const state) {
random_arguments * const arguments = state->input;
switch (key) {
case 'h': {
int i = atoi (arg);
if (i <= 0) {
argp_error (state, "Height must be > 0");
} else {
arguments->height = i;
}
break;
}
case 'w': {
int i = atoi (arg);
if (i <= 0) {
argp_error (state, "Width must be > 0");
} else {
arguments->width = i;
}
break;
}
case ARGP_KEY_ARG:
if (state->arg_num > 0) {
return ARGP_ERR_UNKNOWN;
}
break;
case ARGP_KEY_END:
break;
default:
return ARGP_ERR_UNKNOWN;
break;
}
return 0;
}
#define GOLDEN_RATIO (1.618033988749894848204586834)
#define GOLDEN_RATIO_M1 (GOLDEN_RATIO-1.0)
#define GOLDEN_RATIO_DIV (GOLDEN_RATIO_M1/GOLDEN_RATIO)
static double golden_bit (randctx * const rc) {
return rand_bool (rc) ? GOLDEN_RATIO_DIV : GOLDEN_RATIO_M1;
}
static void adjust_bounding_box (flam3_genome * const genome, randctx * const rc) {
double bmin[2], bmax[2];
flam3_estimate_bounding_box(genome, 0.01, 100000, bmin, bmax, rc);
if (rand_d01(rc) < 0.3) {
genome->center[0] = (bmin[0] + bmax[0]) / 2.0;
genome->center[1] = (bmin[1] + bmax[1]) / 2.0;
} else {
double mix0, mix1;
if (rand_bool(rc)) {
mix0 = golden_bit(rc) + rand_d11(rc)/5;
mix1 = golden_bit(rc);
} else if (rand_bool(rc)) {
mix0 = golden_bit(rc);
mix1 = golden_bit(rc) + rand_d11(rc)/5;
} else {
mix0 = golden_bit(rc) + rand_d11(rc)/5;
mix1 = golden_bit(rc) + rand_d11(rc)/5;
}
genome->center[0] = mix0 * bmin[0] + (1-mix0)*bmax[0];
genome->center[1] = mix1 * bmin[1] + (1-mix1)*bmax[1];
}
genome->rot_center[0] = genome->center[0];
genome->rot_center[1] = genome->center[1];
genome->pixels_per_unit = genome->width / (bmax[0] - bmin[0]);
}
static void do_random (const random_arguments * const arguments) {
randctx rc;
rand_seed(&rc);
flam3_genome genome = { .edits = NULL };
int ivars = flam3_variation_random;
flam3_random (&genome, &ivars, 1, arguments->symmetry, 0, &rc);
/* random resets genome, adjust before finding appropriate bbox */
genome.width = arguments->width;
genome.height = arguments->height;
adjust_bounding_box (&genome, &rc);
print_genome (&genome);
}
typedef struct {
int method;
unsigned int symmetry;
} mutate_arguments;
static error_t parse_mutate_opt (int key, char *arg,
struct argp_state * const state) {
mutate_arguments * const arguments = state->input;
switch (key) {
case 'm':
if (arg == NULL) {
arguments->method = MUTATE_NOT_SPECIFIED;
} else if (streq (arg, "all-vars")) {
arguments->method = MUTATE_ALL_VARIATIONS;
} else if (streq(arg,"one-xform")) {
arguments->method = MUTATE_ONE_XFORM_COEFS;
} else if (streq(arg,"add-symmetry")) {
arguments->method = MUTATE_ADD_SYMMETRY;
} else if (streq(arg,"post-xforms")) {
arguments->method = MUTATE_POST_XFORMS;
} else if (streq(arg,"color-palette")) {
arguments->method = MUTATE_COLOR_PALETTE;
} else if (streq(arg,"delete-xform")) {
arguments->method = MUTATE_DELETE_XFORM;
} else if (streq(arg,"all-coefs")) {
arguments->method = MUTATE_ALL_COEFS;
} else {
argp_error (state, "Unknown method %s", arg);
}
break;
case ARGP_KEY_ARG:
if (state->arg_num > 0) {
return ARGP_ERR_UNKNOWN;
}
break;
case ARGP_KEY_END:
break;
default:
return ARGP_ERR_UNKNOWN;
break;
}
return 0;
}
static void do_mutate (const mutate_arguments * const arguments) {
randctx rc;
rand_seed(&rc);
int ncps;
flam3_genome * const cps = flam3_parse_from_file (stdin, NULL,
flam3_defaults_on, &ncps, &rc);
if (cps == NULL) {
fprintf(stderr,"error reading genomes from file\n");
exit(1);
}
assert (ncps == 1);
flam3_genome * const genome = &cps[0];
int ivars = flam3_variation_random;
const double speed = 1.0;
flam3_mutate (genome, arguments->method, &ivars, 1, arguments->symmetry,
speed, &rc);
print_genome (genome);
}
typedef struct {
int method;
} cross_arguments;
static error_t parse_cross_opt (int key, char *arg,
struct argp_state * const state) {
mutate_arguments * const arguments = state->input;
switch (key) {
case 'm':
if (arg == NULL) {
arguments->method = CROSS_NOT_SPECIFIED;
} else if (streq(arg,"union")) {
arguments->method = CROSS_UNION;
} else if (streq(arg,"interpolate")) {
arguments->method = CROSS_INTERPOLATE;
} else if (streq(arg,"alternate")) {
arguments->method = CROSS_ALTERNATE;
} else {
argp_error (state, "Unknown method %s", arg);
}
break;
case ARGP_KEY_ARG:
if (state->arg_num > 0) {
return ARGP_ERR_UNKNOWN;
}
break;
case ARGP_KEY_END:
break;
default:
return ARGP_ERR_UNKNOWN;
break;
}
return 0;
}
static void do_cross (const cross_arguments * const arguments) {
randctx rc;
rand_seed(&rc);
int ncps;
flam3_genome * const cps = flam3_parse_from_file (stdin, NULL,
flam3_defaults_on, &ncps, &rc);
if (cps == NULL) {
fprintf(stderr,"error reading genomes from file\n");
exit(1);
}
assert (ncps == 2);
flam3_genome * const genome_a = &cps[0], * const genome_b = &cps[1];
flam3_genome genome_out;
flam3_cross (genome_a, genome_b, &genome_out, arguments->method, &rc);
print_genome (&genome_out);
}
#if 0
static void do_improvecolors () {
flam3_improve_colors(&cp_orig, 100, 0, 10, &rc);
}
static void do_interpolate () {
for (ftime = first_frame; ftime <= last_frame; ftime += 1) {
iscp=0;
for (i=0;i<ncp;i++) {
if ( ftime==cp[i].time ) {
flam3_copy(&interpolated, &(cp[i]) );
iscp=1;
}
}
if (iscp==0) {
flam3_interpolate(cp, ncp, (double)ftime, stagger, &interpolated);
for (i=0;i<ncp;i++) {
if ( ftime==cp[i].time-1 ) {
iscp=1;
}
}
if (iscp==0)
interpolated.interpolation_type = flam3_inttype_linear;
}
if (templ) flam3_apply_template(&interpolated, templ);
gprint(&interpolated, 1);
}
}
#endif
static void show_help (const char * const argv0) {
const char *progname = strrchr (argv0, (int) '/');
if (progname == NULL) {
progname = argv0;
} else {
++progname;
}
fprintf (stderr,
"Usage: %s cross [OPTION...]\n"
" Or: %s mutate [OPTION...]\n"
" Or: %s random [OPTION...]\n"
" Or: %s render [OPTION...]\n",
progname, progname, progname, progname);
}
int main (int argc, char **argv) {
if (argc < 2) {
show_help (argv[0]);
return EXIT_FAILURE;
}
const char * const command = argv[1];
if (streq (command, "cross")) {
const struct argp_option options[] = {
{"method", 'm', "XXX", OPTION_ARG_OPTIONAL, "Cross method" },
{ 0 },
};
const char doc[] = "vlame3-cross -- a fractal flame renderer";
const struct argp argp = {
.options = options, .parser = parse_cross_opt,
.args_doc = NULL, .doc = doc, .children = NULL
};
cross_arguments arguments = {
.method = CROSS_NOT_SPECIFIED,
};
argp_parse (&argp, argc, argv, 0, NULL, &arguments);
do_cross (&arguments);
} else if (streq (command, "mutate")) {
const struct argp_option options[] = {
{"method", 'm', "XXX", OPTION_ARG_OPTIONAL, "Mutation method" },
{ 0 },
};
const char doc[] = "vlame3-mutate -- a fractal flame renderer";
const struct argp argp = {
.options = options, .parser = parse_mutate_opt,
.args_doc = NULL, .doc = doc, .children = NULL
};
mutate_arguments arguments = {
.method = MUTATE_NOT_SPECIFIED,
.symmetry = 0,
};
argp_parse (&argp, argc, argv, 0, NULL, &arguments);
do_mutate (&arguments);
} else if (streq (command, "random")) {
/* generate random genome */
const struct argp_option options[] = {
{"height", 'h', "pixels", 0, "Output flame height" },
{"width", 'w', "pixels", 0, "Output flame width" },
{ 0 },
};
const char doc[] = "vlame3-random -- a fractal flame generator";
const struct argp argp = {
.options = options, .parser = parse_random_opt,
.args_doc = NULL, .doc = doc, .children = NULL
};
random_arguments arguments = {
.symmetry = 0,
.palette = "flam3-palettes.xml",
.width = 1000,
.height = 1000,
};
argp_parse (&argp, argc, argv, 0, NULL, &arguments);
do_random (&arguments);
} else if (streq (command, "render")) {
/* render flame to image file */
const struct argp_option options[] = {
{"threads", 't', "num", 0, "Number of threads (auto)" },
{"scale", 's', "factor", 0, "Scale image dimensions by factor (1.0)" },
{"bpc", 'b', "8|16", 0, "Bits per channel of output image (8)" },
{"quality", 'q', "num", 0, "Average samples per pixel (100)" },
{"width", 'w', "pixels", 0, "Output image width" },
{"height", 'h', "pixels", 0, "Output image height" },
{ 0 },
};
const char doc[] = "vlame3-render -- a fractal flame renderer";
const struct argp argp = {
.options = options, .parser = parse_render_opt,
.args_doc = NULL, .doc = doc, .children = NULL,
};
render_arguments arguments = {
.threads = flam3_count_nthreads(),
.bpc = 8,
.scale = 1.0,
.quality = 100,
};
argp_parse (&argp, argc, argv, 0, NULL, &arguments);
do_render (&arguments);
} else {
show_help (argv[0]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|