summaryrefslogtreecommitdiff
path: root/main.c
blob: ec6205d08c44f94eb58c94fc78f4eff82b34f8fc (plain)
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
    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 <unistd.h>
#include <argp.h>

#include "random.h"
#include "img.h"
#include "rect.h"
#include "math.h"
#include "genome.h"
#include "palettes_builtin.h"

#define streq(a,b) (strcmp (a, b) == 0)

const char *argp_program_version = PACKAGE "-" VERSION;

typedef struct {
	unsigned int bpc;
	float scale, time;
	char *cache;
} 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 't': {
			float i = atof (arg);
			if (i <= 0) {
				argp_error (state, "Time must be > 0");
			} else {
				arguments->time = i;
			}
			break;
		}

		case 's':
			arguments->scale = atof (arg);
			if (arguments->scale <= 0.0) {
				argp_error (state, "Scale must be > 0");
			}
			break;

		case 'c':
			arguments->cache = strdup (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_render (const render_arguments * const arguments) {
	randctx rc;

	rand_seed(&rc);

	int ncps;
	flam3_genome * const cps = flam3_parse_xml2 (STDIN_FILENO,
			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->height *= arguments->scale;
	genome->width *= arguments->scale;
	genome->pixels_per_unit *= arguments->scale;

	const unsigned int bytes_per_channel = arguments->bpc/8;
	const unsigned int channels = 4;
	const size_t this_size = channels * genome->width * genome->height *
			bytes_per_channel;
	void *image = (void *) calloc(this_size, sizeof(char));

	bucket bucket;
	bucket_init (&bucket, (uint2) { genome->width, genome->height });
	if (arguments->cache != NULL) {
		bucket_deserialize (&bucket, arguments->cache);
	}

	render_bucket (genome, &bucket, arguments->time);

	if (arguments->cache != NULL) {
		bucket_serialize (&bucket, arguments->cache);
	}

	fprintf (stderr, "%lu samples, %lu bad\n",
			bucket.samples, bucket.badvals);
	render_image (genome, &bucket, image, bytes_per_channel);

	write_png (stdout, image, genome->width, genome->height,
			bytes_per_channel);
}

static void print_genome (flam3_genome * const genome) {
	printf("<pick version=\"" PACKAGE "-" VERSION "\">\n");
	flam3_print (stdout, genome, NULL);
	printf("</pick>\n");
}

typedef struct {
	const char *palette;
	unsigned int width, height, max_xforms, max_var;
	double post_likelihood, final_likelihood, symmetry_likelihood;
} 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 'x': {
			int i = atoi (arg);
			if (i <= 0) {
				argp_error (state, "Max xforms must be > 0");
			} else {
				arguments->max_xforms = i;
			}
			break;
		}

		case 'v': {
			int i = atoi (arg);
			if (i <= 0) {
				argp_error (state, "Max variations must be > 0");
			} else {
				arguments->max_var = 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->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;
	clear_cp (&genome,flam3_defaults_on);

	genome.hue_rotation = rand_mod(&rc, 8) ? 0.0 : rand_d01(&rc);
	const palette * const p = palette_random (&builtin_palettes, &rc);
	assert (p != NULL);
	palette_copy (p, &genome.palette);
	palette_rotate_hue (&genome.palette, genome.hue_rotation);
	genome.interpolation = flam3_interpolation_linear;
	genome.rotate = rand_d01 (&rc) * 360.0;

	unsigned int nxforms = rand_mod (&rc, arguments->max_xforms) + 1;
	flam3_add_xforms(&genome,nxforms,0,0);
	/* Add a final xform 15% of the time */
	const bool add_final = rand_d01(&rc) < arguments->final_likelihood;
	if (add_final) {
		flam3_add_xforms(&genome,1,0,1);
		++nxforms;
	}

	/* Loop over xforms */
	assert (nxforms > 0);
	for (unsigned int i = 0; i < nxforms; i++) {
		flam3_xform * const xform = &genome.xform[i];
		const bool add_post = rand_d01 (&rc) < arguments->post_likelihood;
		xform_rand (xform, add_post, arguments->max_var, &rc);
		xform->density = 1.0 / nxforms;
		xform->color_speed = 0.5;
		xform->animate = 1.0;
	}

	/* Randomly add symmetry (but not if we've already added a final xform) */
	if (rand_d01(&rc) < arguments->symmetry_likelihood && !add_final) {
		flam3_add_symmetry(&genome, 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_xml2 (STDIN_FILENO,
			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 = 0;
	const double speed = 1.0;
	flam3_mutate (genome, arguments->method, &ivars, 1, arguments->symmetry,
			speed, &builtin_palettes, &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_xml2 (STDIN_FILENO,
			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);
}

typedef struct {
	unsigned int tries, resolution;
	bool change_palette;
	double time;
} improvecolors_arguments;

static error_t parse_improvecolors_opt (int key, char *arg,
		struct argp_state * const state) {
	improvecolors_arguments * const arguments = state->input;

	return 0;
}

static void do_improvecolors (const improvecolors_arguments * const arguments) {
	randctx rc;

	rand_seed(&rc);

	int ncps;
	flam3_genome * const cps = flam3_parse_xml2 (STDIN_FILENO,
			flam3_defaults_on, &ncps, &rc);
	if (cps == NULL) {
		fprintf(stderr,"error reading genomes from file\n");
		exit(1);
	}
	assert (ncps == 1);

	flam3_improve_colors (&cps[0], arguments->tries, arguments->change_palette,
			arguments->resolution, arguments->time, &builtin_palettes, &rc);

	print_genome (&cps[0]);
}

typedef struct {
	float time;
} interpolate_arguments;

static error_t parse_interpolate_opt (int key, char *arg,
		struct argp_state * const state) {
	interpolate_arguments * const arguments = state->input;
	switch (key) {
		case 't': {
			assert (arg != NULL);
			float i = atof (arg);
			if (i >= 0.0 && i <= 1.0) {
				arguments->time = i;
			} else {
				argp_error (state, "Time must be between 0 and 1");
			}	
			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_interpolate (const interpolate_arguments * const arguments) {
	randctx rc;

	rand_seed(&rc);

	int ncps;
	flam3_genome * const cps = flam3_parse_xml2 (STDIN_FILENO,
			flam3_defaults_on, &ncps, &rc);
	if (cps == NULL) {
		fprintf(stderr,"error reading genomes from file\n");
		exit(1);
	}
	assert (ncps == 2);
	cps[0].time = 0.0;
	cps[1].time = 1.0;

	flam3_genome genome_out;
	flam3_interpolate (cps, 2, arguments->time, 0, &genome_out);

	print_genome (&genome_out);
}

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 improvecolors [OPTION...]\n"
			"   Or: %s interpolate [OPTION...]\n"
			"   Or: %s mutate [OPTION...]\n"
			"   Or: %s random [OPTION...]\n"
			"   Or: %s render [OPTION...]\n",
			progname, 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[] = PACKAGE "-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, "improvecolors")) {
		const struct argp_option options[] = {
				{ 0 },
				};
		const char doc[] = PACKAGE "-improvecolors -- improve flame colors";
		const struct argp argp = {
				.options = options, .parser = parse_improvecolors_opt,
				.args_doc = NULL, .doc = doc, .children = NULL
				};

		improvecolors_arguments arguments = {
				.tries = 100,
				.resolution = 10,
				.change_palette = true,
				.time = 1.0,
				};

		argp_parse (&argp, argc, argv, 0, NULL, &arguments);
		do_improvecolors (&arguments);
	} else if (streq (command, "interpolate")) {
		const struct argp_option options[] = {
				{"time", 't', "float", OPTION_ARG_OPTIONAL, "Time step (0.5)" },
				{ 0 },
				};
		const char doc[] = PACKAGE "-interpolate -- fractal flame interpolation";
		const struct argp argp = {
				.options = options, .parser = parse_interpolate_opt,
				.args_doc = NULL, .doc = doc, .children = NULL
				};

		interpolate_arguments arguments = {
				.time = 0.5,
				};

		argp_parse (&argp, argc, argv, 0, NULL, &arguments);
		do_interpolate (&arguments);
#if 0
	} else if (streq (command, "mutate")) {
		const struct argp_option options[] = {
				{"method", 'm', "XXX", OPTION_ARG_OPTIONAL, "Mutation method" },
				{ 0 },
				};
		const char doc[] = PACKAGE "-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);
#endif
	} else if (streq (command, "random")) {
		/* generate random genome */
		const struct argp_option options[] = {
				{"height", 'h', "pixels", 0, "Output flame height (1000)" },
				{"width", 'w', "pixels", 0, "Output flame width (1000)" },
				{"max-xforms", 'x', "number", 0, "Max number of xforms (6)" },
				{"max-var", 'v', "number", 0, "Max number of variations per xform (unlimited)" },
				{ 0 },
				};
		const char doc[] = PACKAGE "-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 = {
				.palette = "flam3-palettes.xml",
				.width = 1000,
				.height = 1000,
				.max_xforms = 6,
				.max_var = flam3_nvariations,
				.post_likelihood = 0.4,
				.final_likelihood = 0.15,
				.symmetry_likelihood = 0.25,
				};

		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[] = {
				{"scale", 's', "factor", 0, "Scale image dimensions by factor (1.0)" },
				{"bpc", 'b', "8|16", 0, "Bits per channel of output image (8)" },
				{"time", 't', "seconds", 0, "Rendering time" },
				{"cache", 'c', "path", 0, "Cache file" },
				{ 0 },
				};
		const char doc[] = PACKAGE "-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 = {
				.bpc = 8,
				.scale = 1.0,
				.time = 1.0,
				.cache = NULL,
				};

		argp_parse (&argp, argc, argv, 0, NULL, &arguments);
		do_render (&arguments);
	} else {
		show_help (argv[0]);
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}