Debian Codegraph @

flam3.c

raw ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 /*
2 FLAM3 - cosmic recursive fractal flames
3 Copyright (C) 1992-2009 Spotworks LLC
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef _MSC_VER /* VC++ */
20 #define _GNU_SOURCE
21 #endif
22
23 #include "private.h"
24 #include "img.h"
25 #include "config.h"
26 #include "variations.h"
27 #include "interpolation.h"
28 #include "parser.h"
29 #include "filters.h"
30 #include "palettes.h"
31 #include <limits.h>
32 #include <locale.h>
33 #include <math.h>
34 #ifdef HAVE_STDINT_H
35 #include <stdint.h>
36 #endif
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 #include <errno.h>
41
42 #ifdef HAVE_LIBPTHREAD
43 #include <pthread.h>
44 #endif
45
46 #ifdef __APPLE__
47 #include <mach/mach.h>
48 #include <mach/mach_error.h>
49 #define flam3_os "OSX"
50 #else
51 #ifdef _WIN32
52 #define WINVER 0x0500
53 #include <windows.h>
54 #define flam3_os "WIN"
55 #else
56 #define flam3_os "LNX"
57 #endif
58 #endif
59
60
61
62 char *flam3_version() {
63 return flam3_os "-" VERSION;
64 }
65
66
67 #define CHOOSE_XFORM_GRAIN 16384
68 #define CHOOSE_XFORM_GRAIN_M1 16383
69
70 #define random_distrib(v) ((v)[random()%vlen(v)])
71
72
73 unsigned short *flam3_create_xform_distrib(flam3_genome *cp) {
74
75 /* Xform distrib is created in this function */
76 int numrows;
77 int dist_row,i;
78 unsigned short *xform_distrib;
79
80 numrows = cp->num_xforms - (cp->final_xform_index>=0) + 1;
81 xform_distrib = calloc(numrows*CHOOSE_XFORM_GRAIN,sizeof(unsigned short));
82
83 /* First, set up the first row of the xform_distrib (raw weights) */
84 flam3_create_chaos_distrib(cp, -1, xform_distrib);
85
86 /* Check for non-unity chaos */
87 cp->chaos_enable = 1 - flam3_check_unity_chaos(cp);
88
89 if (cp->chaos_enable) {
90
91 /* Now set up a row for each of the xforms */
92 dist_row = 0;
93 for (i=0;i<cp->num_xforms;i++) {
94
95 if (cp->final_xform_index == i)
96 continue;
97 else
98 dist_row++;
99
100 if (flam3_create_chaos_distrib(cp, i, &(xform_distrib[CHOOSE_XFORM_GRAIN*(dist_row)]))) {
101 free(xform_distrib);
102 return(NULL);
103 }
104 }
105 }
106
107 return(xform_distrib);
108 }
109
110 /* Run this on a copy of a genome to get a strip of the output */
111 int flam3_make_strip(flam3_genome *cp, int nstrips, int stripnum) {
112
113 double old_center[2];
114 int j;
115
116 /* Strip out motion elements */
117 for (j=0;j<cp->num_xforms;j++)
118 flam3_delete_motion_elements(&cp->xform[j]);
119
120 old_center[0] = cp->center[0];
121 old_center[1] = cp->center[1];
122 cp->height /= nstrips;
123 cp->center[1] = cp->center[1] - ((nstrips - 1) * cp->height) / (2 * cp->pixels_per_unit * pow(2.0, cp->zoom));
124 cp->center[1] += cp->height * stripnum / ( cp->pixels_per_unit * pow(2.0,cp->zoom) );
125 rotate_by(cp->center, old_center, cp->rotate);
126
127 return(0);
128 }
129
130
131 void rotate_by(double *p, double *center, double by) {
132 double r[2];
133 double th = by * 2 * M_PI / 360.0;
134 double c = cos(th);
135 double s = -sin(th);
136 p[0] -= center[0];
137 p[1] -= center[1];
138 r[0] = c * p[0] - s * p[1];
139 r[1] = s * p[0] + c * p[1];
140 p[0] = r[0] + center[0];
141 p[1] = r[1] + center[1];
142 }
143
144
145 int flam3_check_unity_chaos(flam3_genome *cp) {
146
147 int i,j;
148 int num_std;
149 int unity=1;
150 num_std = cp->num_xforms - (cp->final_xform_index >= 0);
151
152 for (i=0;i<num_std;i++) {
153 for (j=0;j<num_std;j++) {
154 if ( fabs(cp->chaos[i][j]-1.0) > EPS)
155 unity=0;
156 }
157 }
158
159 return(unity);
160 }
161
162 int flam3_create_chaos_distrib(flam3_genome *cp, int xi, unsigned short *xform_distrib) {
163
164 /* Xform distrib is a preallocated array of CHOOSE_XFORM_GRAIN chars */
165 /* address of array is passed in, contents are modified */
166 double t,r,dr;
167 int i,j;
168 int num_std;
169
170 //fprintf(stdout,"storing at %ld\n",xform_distrib);
171
172 num_std = cp->num_xforms - (cp->final_xform_index >= 0);
173
174 dr = 0.0;
175 for (i = 0; i < num_std; i++) {
176 double d = cp->xform[i].density;
177
178 if (xi>=0)
179 d *= cp->chaos[xi][i];
180
181 //fprintf(stdout,"%f ",d);
182 if (d < 0.0) {
183 fprintf(stderr, "xform weight must be non-negative, not %g.\n", d);
184 return(1);
185 }
186
187 dr += d;
188 }
189
190 //fprintf(stdout,"dr=%f\n",dr);
191
192 if (dr == 0.0) {
193 fprintf(stderr, "cannot iterate empty flame.\n");
194 return(1);
195 }
196
197 dr = dr / CHOOSE_XFORM_GRAIN;
198
199 j = 0;
200 t = cp->xform[0].density;
201 if (xi>=0)
202 t *= cp->chaos[xi][0];
203 r = 0.0;
204 for (i = 0; i < CHOOSE_XFORM_GRAIN; i++) {
205 while (r >= t) {
206 j++;
207
208 if (xi>=0)
209 t += cp->xform[j].density*cp->chaos[xi][j];
210 else
211 t += cp->xform[j].density;
212
213 }
214 //fprintf(stdout,"%d ",j);
215 xform_distrib[i] = j;
216 r += dr;
217 }
218 //fprintf(stdout,"\n---\n");
219
220 return(0);
221 }
222 /*
223 * run the function system described by CP forward N generations. store
224 * the N resulting 4-vectors in SAMPLES. the initial point is passed in
225 * SAMPLES[0..3]. ignore the first FUSE iterations.
226 */
227
228
229 int flam3_iterate(flam3_genome *cp, int n, int fuse, double *samples, unsigned short *xform_distrib, randctx *rc) {
230 int i;
231 double p[4], q[4];
232 int consec = 0;
233 int badvals = 0;
234 int lastxf=0;
235 int fn;
236
237 p[0] = samples[0];
238 p[1] = samples[1];
239 p[2] = samples[2];
240 p[3] = samples[3];
241
242 /* Perform precalculations */
243 for (i=0;i<cp->num_xforms;i++)
244 xform_precalc(cp,i);
245
246 for (i = -4*fuse; i < 4*n; i+=4) {
247
248 // fn = xform_distrib[ lastxf*CHOOSE_XFORM_GRAIN + (((unsigned)irand(rc)) % CHOOSE_XFORM_GRAIN)];
249 if (cp->chaos_enable)
250 fn = xform_distrib[ lastxf*CHOOSE_XFORM_GRAIN + (((unsigned)irand(rc)) & CHOOSE_XFORM_GRAIN_M1)];
251 else
252 fn = xform_distrib[ ((unsigned)irand(rc)) & CHOOSE_XFORM_GRAIN_M1 ];
253
254 if (apply_xform(cp, fn, p, q, rc)>0) {
255 consec ++;
256 badvals ++;
257 if (consec<5) {
258 p[0] = q[0];
259 p[1] = q[1];
260 p[2] = q[2];
261 p[3] = q[3];
262 i -= 4;
263 continue;
264 } else
265 consec = 0;
266 } else
267 consec = 0;
268
269 /* Store the last used transform */
270 lastxf = fn+1;
271
272 p[0] = q[0];
273 p[1] = q[1];
274 p[2] = q[2];
275 p[3] = q[3];
276
277 if (cp->final_xform_enable == 1) {
278 if (cp->xform[cp->final_xform_index].opacity==1 ||
279 flam3_random_isaac_01(rc)<cp->xform[cp->final_xform_index].opacity) {
280 apply_xform(cp, cp->final_xform_index, p, q, rc);
281 /* Keep the opacity from the original xform */
282 q[3] = p[3];
283 }
284 }
285
286 /* if fuse over, store it */
287 if (i >= 0) {
288 samples[i] = q[0];
289 samples[i+1] = q[1];
290 samples[i+2] = q[2];
291 samples[i+3] = q[3];
292 }
293 }
294
295 return(badvals);
296 }
297
298 int flam3_xform_preview(flam3_genome *cp, int xi, double range, int numvals, int depth, double *result, randctx *rc) {
299
300 /* We will evaluate the 'xi'th xform 'depth' times, over the following values: */
301 /* x in [-range : range], y in [-range : range], with 2* (2*numvals+1)^2 values returned */
302 double p[4];
303 double incr;
304 int outi;
305 int xx,yy,dd;
306 double oldweight;
307
308 outi=0;
309
310 oldweight = cp->xform[xi].density;
311 cp->xform[xi].density = 1.0;
312
313 /* Prepare the function pointers */
314 if (prepare_precalc_flags(cp)) {
315 cp->xform[xi].density = oldweight;
316 return(1);
317 }
318
319 /* Calculate increment */
320 incr = range / (double)numvals;
321
322 /* Perform precalculations */
323 xform_precalc(cp,xi);
324
325 /* Loop over the grid */
326 for (xx=-numvals;xx<=numvals;xx++) {
327 for (yy=-numvals;yy<=numvals;yy++) {
328
329 /* Calculate the input coordinates */
330 p[0] = (double)xx * incr;
331 p[1] = (double)yy * incr;
332
333 /* Loop over the depth */
334 for (dd=0;dd<depth;dd++)
335 apply_xform(cp, xi, p, p, rc);
336
337 result[outi] = p[0];
338 result[outi+1] = p[1];
339
340 outi += 2;
341 }
342 }
343 cp->xform[xi].density = oldweight;
344
345 return(0);
346 }
347
348 int flam3_colorhist(flam3_genome *cp, int num_batches, randctx *rc, double *hist) {
349
350 int lp,plp;
351 int mycolor;
352 unsigned short *xform_distrib;
353 int sbs = 10000;
354 double sub_batch[4*10000];
355
356 memset(hist,0,256*sizeof(double));
357
358 // get into the attractor
359 if (prepare_precalc_flags(cp))
360 return(1);
361
362 xform_distrib = flam3_create_xform_distrib(cp);
363
364 for (lp=0;lp<num_batches;lp++) {
365
366 sub_batch[0] = flam3_random_isaac_11(rc);
367 sub_batch[1] = flam3_random_isaac_11(rc);
368 sub_batch[2] = 0;
369 sub_batch[3] = 0;
370
371
372 if (xform_distrib==NULL)
373 return(1);
374 flam3_iterate(cp, sbs, 20, sub_batch, xform_distrib, rc);
375
376 // histogram the colors in the sub_batch array
377 for (plp=0;plp<4*sbs;plp+=4) {
378 mycolor = (int)(sub_batch[plp+2]*CMAP_SIZE);
379 if (mycolor<0) mycolor=0;
380 if (mycolor>CMAP_SIZE_M1) mycolor=CMAP_SIZE_M1;
381
382 hist[mycolor] += 1;
383 }
384 }
385
386 free(xform_distrib);
387 for (plp=0;plp<256;plp++)
388 hist[plp] /= (float)(num_batches*sbs);
389
390 return(0);
391 }
392
393 flam3_genome *sheep_loop(flam3_genome *cp, double blend) {
394
395 flam3_genome *result;
396 int i;
397
398 /* Allocate the genome - this must be freed by calling function */
399 result = calloc(1,sizeof(flam3_genome));
400
401 /* Clear it */
402 clear_cp(result,flam3_defaults_on);
403
404 /* Copy the original */
405 flam3_copy(result,cp);
406
407 /*
408 * Insert motion magic here :
409 * if there are motion elements, we will modify the contents of
410 * the result genome before flam3_rotate is called.
411 */
412 for (i=0;i<cp->num_xforms;i++) {
413 if (cp->xform[i].num_motion>0) {
414 /* Apply motion parameters to result.xform[i] using blend parameter */
415 apply_motion_parameters(&cp->xform[i], &result->xform[i], blend);
416 }
417
418 /* Delete the motion parameters from the result */
419 flam3_delete_motion_elements(&result->xform[i]);
420 }
421
422 /* Rotate the affines */
423 flam3_rotate(result, blend*360.0,result->interpolation_type);
424
425 return(result);
426 }
427
428
429
430
431 flam3_genome *sheep_edge(flam3_genome *cp, double blend, int seqflag, double stagger) {
432
433 flam3_genome spun[2];
434 flam3_genome prealign[2];
435 flam3_genome *result;
436 int i,si;
437 char *ai;
438
439 memset(spun, 0, 2*sizeof(flam3_genome));
440 memset(prealign, 0, 2*sizeof(flam3_genome));
441
442 /* Allocate the memory for the result */
443 result = calloc(1,sizeof(flam3_genome));
444
445 /*
446 * Insert motion magic here :
447 * if there are motion elements, we will modify the contents of
448 * the prealign genomes before we rotate and interpolate.
449 */
450
451 for (si=0;si<2;si++) {
452 flam3_copy(&prealign[si], &cp[si]);
453 for (i=0;i<cp[si].num_xforms;i++) {
454 if (cp[si].xform[i].num_motion>0) {
455 /* Apply motion parameters to result.xform[i] using blend parameter */
456 apply_motion_parameters(&cp[si].xform[i], &prealign[si].xform[i], blend);
457 }
458 }
459 }
460
461 /* Use the un-padded original for blend=0 when creating a sequence */
462 /* This keeps the original interpolation type intact */
463 if (seqflag && 0.0 == blend) {
464 flam3_copy(result, &prealign[0]);
465 } else {
466
467 /* Align what we're going to interpolate */
468 flam3_align(spun, prealign, 2);
469
470 spun[0].time = 0.0;
471 spun[1].time = 1.0;
472
473 /* Call this first to establish the asymmetric reference angles */
474 establish_asymmetric_refangles(spun,2);
475
476 /* Rotate the aligned xforms */
477 flam3_rotate(&spun[0], blend*360.0, spun[0].interpolation_type);
478 flam3_rotate(&spun[1], blend*360.0, spun[0].interpolation_type);
479
480 //fprintf(stderr, "xyzzy %d %d\n", spun[0].palette_interpolation, spun[1].palette_interpolation);
481
482 /* Now call the interpolation */
483 if (argi("unsmoother",0) == 0)
484 flam3_interpolate(spun, 2, smoother(blend), stagger, result);
485 else
486 flam3_interpolate(spun, 2, blend, stagger, result);
487
488
489 /* Interpolation type no longer needs to be forced to linear mode */
490 // if (!seqflag)
491 // result.interpolation_type = flam3_inttype_linear;
492 }
493
494 /* Clear the genomes we used */
495 clear_cp(&spun[0],flam3_defaults_on);
496 clear_cp(&spun[1],flam3_defaults_on);
497 clear_cp(&prealign[0],flam3_defaults_on);
498 clear_cp(&prealign[1],flam3_defaults_on);
499
500 /* Make sure there are no motion elements in the result */
501 for (i=0;i<result->num_xforms;i++)
502 flam3_delete_motion_elements(&result->xform[i]);
503
504 return(result);
505 }
506
507
508 /* BY is angle in degrees */
509 void flam3_rotate(flam3_genome *cp, double by, int interpolation_type) {
510 int i;
511 for (i = 0; i < cp->num_xforms; i++) {
512 double r[2][2];
513 double T[2][2];
514 double U[2][2];
515 double dtheta = by * 2.0 * M_PI / 360.0;
516
517 /* Don't rotate xforms with > 0 animate values */
518 if (cp->xform[i].animate == 0.0)
519 continue;
520
521 if (cp->xform[i].padding == 1) {
522 if (interpolation_type == flam3_inttype_compat) {
523 /* gen 202 era flam3 did not rotate padded xforms */
524 continue;
525 } else if (interpolation_type == flam3_inttype_older) {
526 /* not sure if 198 era flam3 rotated padded xforms */
527 continue;
528 } else if (interpolation_type == flam3_inttype_linear) {
529 /* don't rotate for prettier symsings */
530 continue;
531 } else if (interpolation_type == flam3_inttype_log) {
532 /* Current flam3: what do we prefer? */
533 //continue;
534 }
535 }
536
537 /* Do NOT rotate final xforms */
538 if (cp->final_xform_enable==1 && cp->final_xform_index==i)
539 continue;
540
541 r[1][1] = r[0][0] = cos(dtheta);
542 r[0][1] = sin(dtheta);
543 r[1][0] = -r[0][1];
544 T[0][0] = cp->xform[i].c[0][0];
545 T[1][0] = cp->xform[i].c[1][0];
546 T[0][1] = cp->xform[i].c[0][1];
547 T[1][1] = cp->xform[i].c[1][1];
548 mult_matrix(r, T, U);
549 cp->xform[i].c[0][0] = U[0][0];
550 cp->xform[i].c[1][0] = U[1][0];
551 cp->xform[i].c[0][1] = U[0][1];
552 cp->xform[i].c[1][1] = U[1][1];
553 }
554 }
555
556 #define APPMOT(x) do { addto->x += mot[i].x * motion_funcs(func,freq*blend); } while (0);
557
558 void apply_motion_parameters(flam3_xform *xf, flam3_xform *addto, double blend) {
559
560 int i,j,k;
561 int freq;
562 int func;
563 flam3_xform* mot;
564
565 mot = xf->motion;
566
567 /* Loop over the motion elements and add their contribution to the original vals */
568 for (i=0; i<xf->num_motion; i++) {
569
570 freq = mot[i].motion_freq;
571 func = mot[i].motion_func;
572
573 APPMOT(density); /* Must ensure > 0 after all is applied */
574 APPMOT(color); /* Must ensure [0,1] after all is applied */
575
576 APPMOT(opacity);
577 APPMOT(color_speed);
578 APPMOT(animate);
579 APPMOT(blob_low);
580 APPMOT(blob_high);
581 APPMOT(blob_waves);
582 APPMOT(pdj_a);
583 APPMOT(pdj_b);
584 APPMOT(pdj_c);
585 APPMOT(pdj_d);
586 APPMOT(fan2_x);
587 APPMOT(fan2_y);
588 APPMOT(rings2_val);
589 APPMOT(perspective_angle);
590 APPMOT(perspective_dist);
591 APPMOT(julian_power);
592 APPMOT(julian_dist);
593 APPMOT(juliascope_power);
594 APPMOT(juliascope_dist);
595 APPMOT(radial_blur_angle);
596 APPMOT(pie_slices);
597 APPMOT(pie_rotation);
598 APPMOT(pie_thickness);
599 APPMOT(ngon_sides);
600 APPMOT(ngon_power);
601 APPMOT(ngon_circle);
602 APPMOT(ngon_corners);
603 APPMOT(curl_c1);
604 APPMOT(curl_c2);
605 APPMOT(rectangles_x);
606 APPMOT(rectangles_y);
607 APPMOT(amw_amp);
608 APPMOT(disc2_rot);
609 APPMOT(disc2_twist);
610 APPMOT(super_shape_rnd);
611 APPMOT(super_shape_m);
612 APPMOT(super_shape_n1);
613 APPMOT(super_shape_n2);
614 APPMOT(super_shape_n3);
615 APPMOT(super_shape_holes);
616 APPMOT(flower_petals);
617 APPMOT(flower_holes);
618 APPMOT(conic_eccentricity);
619 APPMOT(conic_holes);
620 APPMOT(parabola_height);
621 APPMOT(parabola_width);
622 APPMOT(bent2_x);
623 APPMOT(bent2_y);
624 APPMOT(bipolar_shift);
625 APPMOT(cell_size);
626 APPMOT(cpow_r);
627 APPMOT(cpow_i);
628 APPMOT(cpow_power);
629 APPMOT(curve_xamp);
630 APPMOT(curve_yamp);
631 APPMOT(curve_xlength);
632 APPMOT(curve_ylength);
633 APPMOT(escher_beta);
634 APPMOT(lazysusan_x);
635 APPMOT(lazysusan_y);
636 APPMOT(lazysusan_twist);
637 APPMOT(lazysusan_space);
638 APPMOT(lazysusan_spin);
639 APPMOT(modulus_x);
640 APPMOT(modulus_y);
641 APPMOT(oscope_separation);
642 APPMOT(oscope_frequency);
643 APPMOT(oscope_amplitude);
644 APPMOT(oscope_damping);
645 APPMOT(popcorn2_x);
646 APPMOT(popcorn2_y);
647 APPMOT(popcorn2_c);
648 APPMOT(separation_x);
649 APPMOT(separation_xinside);
650 APPMOT(separation_y);
651 APPMOT(separation_yinside);
652 APPMOT(split_xsize);
653 APPMOT(split_ysize);
654 APPMOT(splits_x);
655 APPMOT(splits_y);
656 APPMOT(stripes_space);
657 APPMOT(stripes_warp);
658 APPMOT(wedge_angle);
659 APPMOT(wedge_hole);
660 APPMOT(wedge_count);
661 APPMOT(wedge_swirl);
662 APPMOT(wedge_julia_angle);
663 APPMOT(wedge_julia_count);
664 APPMOT(wedge_julia_power);
665 APPMOT(wedge_julia_dist);
666 APPMOT(wedge_sph_angle);
667 APPMOT(wedge_sph_hole);
668 APPMOT(wedge_sph_count);
669 APPMOT(wedge_sph_swirl);
670 APPMOT(whorl_inside);
671 APPMOT(whorl_outside);
672 APPMOT(waves2_scalex);
673 APPMOT(waves2_scaley);
674 APPMOT(waves2_freqx);
675 APPMOT(waves2_freqy);
676 APPMOT(auger_sym);
677 APPMOT(auger_weight);
678 APPMOT(auger_freq);
679 APPMOT(auger_scale);
680 APPMOT(flux_spread);
681 APPMOT(mobius_re_a);
682 APPMOT(mobius_re_b);
683 APPMOT(mobius_re_c);
684 APPMOT(mobius_re_d);
685 APPMOT(mobius_im_a);
686 APPMOT(mobius_im_b);
687 APPMOT(mobius_im_c);
688 APPMOT(mobius_im_d);
689
690 for (j = 0; j < flam3_nvariations; j++)
691 APPMOT(var[j]);
692
693 for (j=0; j<3; j++) {
694 for (k=0; k<2; k++) {
695 APPMOT(c[j][k]);
696 APPMOT(post[j][k]);
697 }
698 }
699
700 }
701
702 /* Make sure certain params are within reasonable bounds */
703 if (addto->color<0) addto->color=0;
704 if (addto->color>1) addto->color=1;
705 if (addto->density<0) addto->density=0;
706
707 }
708
709
710 /*
711 * create a control point that interpolates between the control points
712 * passed in CPS. CPS must be sorted by time.
713 */
714 void flam3_interpolate(flam3_genome cps[], int ncps,
715 double time, double stagger, flam3_genome *result) {
716 int i1, i2;
717 double c[2];
718 flam3_genome cpi[4];
719 int smoothflag = 0;
720
721 if (1 == ncps) {
722 flam3_copy(result, &(cps[0]));
723 return;
724 }
725
726 if (cps[0].time >= time) {
727 i1 = 0;
728 i2 = 1;
729 } else if (cps[ncps - 1].time <= time) {
730 i1 = ncps - 2;
731 i2 = ncps - 1;
732 } else {
733 i1 = 0;
734 while (cps[i1].time < time)
735 i1++;
736
737 i1--;
738 i2 = i1 + 1;
739
740 }
741
742 c[0] = (cps[i2].time - time) / (cps[i2].time - cps[i1].time);
743 c[1] = 1.0 - c[0];
744
745 memset(cpi, 0, 4*sizeof(flam3_genome));
746
747 /* To interpolate the xforms, we will make copies of the source cps */
748 /* and ensure that they both have the same number before progressing */
749 if (flam3_interpolation_linear == cps[i1].interpolation) {
750 flam3_align(&cpi[0], &cps[i1], 2);
751 smoothflag = 0;
752
753 } else {
754 if (0 == i1) {
755 fprintf(stderr, "error: cannot use smooth interpolation on first segment.\n");
756 fprintf(stderr, "reverting to linear interpolation.\n");
757 flam3_align(&cpi[0], &cps[i1], 2);
758 smoothflag = 0;
759 }
760
761 if (ncps-1 == i2) {
762 fprintf(stderr, "error: cannot use smooth interpolation on last segment.\n");
763 fprintf(stderr, "reverting to linear interpolation.\n");
764 flam3_align(&cpi[0], &cps[i1], 2);
765 smoothflag = 0;
766 }
767
768 flam3_align(&cpi[0], &cps[i1-1], 4);
769 smoothflag = 1;
770 }
771
772 /* Clear the destination cp */
773 clear_cp(result, 1);
774
775 if (cpi[0].final_xform_index >= 0) {
776 flam3_add_xforms(result, cpi[0].num_xforms-1, 0, 0);
777 flam3_add_xforms(result, 1, 0, 1);
778 } else
779 flam3_add_xforms(result, cpi[0].num_xforms, 0, 0);
780
781
782 result->time = time;
783 result->interpolation = flam3_interpolation_linear;
784 result->interpolation_type = cpi[0].interpolation_type;
785 result->palette_interpolation = flam3_palette_interpolation_hsv_circular;
786 result->hsv_rgb_palette_blend = 0.0;
787
788 if (!smoothflag) {
789 flam3_interpolate_n(result, 2, cpi, c, stagger);
790 } else {
791 interpolate_catmull_rom(cpi, c[1], result);
792 clear_cp(&(cpi[2]),0);
793 clear_cp(&(cpi[3]),0);
794 }
795
796 clear_cp(&(cpi[0]),0);
797 clear_cp(&(cpi[1]),0);
798
799 }
800
801 void flam3_copy_params(flam3_xform *dest, flam3_xform *src, int varn) {
802
803 /* We only want to copy param var coefs for this one */
804 if (varn==VAR_BLOB) {
805 /* Blob */
806 dest->blob_low = src->blob_low;
807 dest->blob_high = src->blob_high;
808 dest->blob_waves = src->blob_waves;
809 } else if (varn==VAR_PDJ) {
810 /* PDJ */
811 dest->pdj_a = src->pdj_a;
812 dest->pdj_b = src->pdj_b;
813 dest->pdj_c = src->pdj_c;
814 dest->pdj_d = src->pdj_d;
815 } else if (varn==VAR_FAN2) {
816 /* Fan2 */
817 dest->fan2_x = src->fan2_x;
818 dest->fan2_y = src->fan2_y;
819 } else if (varn==VAR_RINGS2) {
820 /* Rings2 */
821 dest->rings2_val = src->rings2_val;
822 } else if (varn==VAR_PERSPECTIVE) {
823 /* Perspective */
824 dest->perspective_angle = src->perspective_angle;
825 dest->perspective_dist = src->perspective_dist;
826 dest->persp_vsin = src->persp_vsin;
827 dest->persp_vfcos = src->persp_vfcos;
828 } else if (varn==VAR_JULIAN) {
829 /* Julia_N */
830 dest->julian_power = src->julian_power;
831 dest->julian_dist = src->julian_dist;
832 dest->julian_rN = src->julian_rN;
833 dest->julian_cn = src->julian_cn;
834 } else if (varn==VAR_JULIASCOPE) {
835 /* Julia_Scope */
836 dest->juliascope_power = src->juliascope_power;
837 dest->juliascope_dist = src->juliascope_dist;
838 dest->juliascope_rN = src->juliascope_rN;
839 dest->juliascope_cn = src->juliascope_cn;
840 } else if (varn==VAR_RADIAL_BLUR) {
841 /* Radial Blur */
842 dest->radial_blur_angle = src->radial_blur_angle;
843 } else if (varn==VAR_PIE) {
844 /* Pie */
845 dest->pie_slices = src->pie_slices;
846 dest->pie_rotation = src->pie_rotation;
847 dest->pie_thickness = src->pie_thickness;
848 } else if (varn==VAR_NGON) {
849 /* Ngon */
850 dest->ngon_sides = src->ngon_sides;
851 dest->ngon_power = src->ngon_power;
852 dest->ngon_corners = src->ngon_corners;
853 dest->ngon_circle = src->ngon_circle;
854 } else if (varn==VAR_CURL) {
855 /* Curl */
856 dest->curl_c1 = src->curl_c1;
857 dest->curl_c2 = src->curl_c2;
858 } else if (varn==VAR_RECTANGLES) {
859 /* Rect */
860 dest->rectangles_x = src->rectangles_x;
861 dest->rectangles_y = src->rectangles_y;
862 } else if (varn==VAR_DISC2) {
863 /* Disc2 */
864 dest->disc2_rot = src->disc2_rot;
865 dest->disc2_twist = src->disc2_twist;
866 } else if (varn==VAR_SUPER_SHAPE) {
867 /* Supershape */
868 dest->super_shape_rnd = src->super_shape_rnd;
869 dest->super_shape_m = src->super_shape_m;
870 dest->super_shape_n1 = src->super_shape_n1;
871 dest->super_shape_n2 = src->super_shape_n2;
872 dest->super_shape_n3 = src->super_shape_n3;
873 dest->super_shape_holes = src->super_shape_holes;
874 } else if (varn==VAR_FLOWER) {
875 /* Flower */
876 dest->flower_petals = src->flower_petals;
877 dest->flower_petals = src->flower_petals;
878 } else if (varn==VAR_CONIC) {
879 /* Conic */
880 dest->conic_eccentricity = src->conic_eccentricity;
881 dest->conic_holes = src->conic_holes;
882 } else if (varn==VAR_PARABOLA) {
883 /* Parabola */
884 dest->parabola_height = src->parabola_height;
885 dest->parabola_width = src->parabola_width;
886 } else if (varn==VAR_BENT2) {
887 /* Bent2 */
888 dest->bent2_x = src->bent2_x;
889 dest->bent2_y = src->bent2_y;
890 } else if (varn==VAR_BIPOLAR) {
891 /* Bipolar */
892 dest->bipolar_shift = src->bipolar_shift;
893 } else if (varn==VAR_CELL) {
894 /* Cell */
895 dest->cell_size = src->cell_size;
896 } else if (varn==VAR_CPOW) {
897 /* Cpow */
898 dest->cpow_i = src->cpow_i;
899 dest->cpow_r = src->cpow_r;
900 dest->cpow_power = src->cpow_power;
901 } else if (varn==VAR_CURVE) {
902 /* Curve */
903 dest->curve_xamp = src->curve_xamp;
904 dest->curve_yamp = src->curve_yamp;
905 dest->curve_xlength = src->curve_xlength;
906 dest->curve_ylength = src->curve_ylength;
907 } else if (varn==VAR_ESCHER) {
908 /* Escher */
909 dest->escher_beta = src->escher_beta;
910 } else if (varn==VAR_LAZYSUSAN) {
911 /* Lazysusan */
912 dest->lazysusan_x = src->lazysusan_x;
913 dest->lazysusan_y = src->lazysusan_y;
914 dest->lazysusan_spin = src->lazysusan_spin;
915 dest->lazysusan_space = src->lazysusan_space;
916 dest->lazysusan_twist = src->lazysusan_twist;
917 } else if (varn==VAR_MODULUS) {
918 /* Modulus */
919 dest->modulus_x = src->modulus_x;
920 dest->modulus_y = src->modulus_y;
921 } else if (varn==VAR_OSCILLOSCOPE) {
922 /* Oscope */
923 dest->oscope_separation = src->oscope_separation;
924 dest->oscope_frequency = src->oscope_frequency;
925 dest->oscope_amplitude = src->oscope_amplitude;
926 dest->oscope_damping = src->oscope_damping;
927 } else if (varn==VAR_POPCORN2) {
928 /* Popcorn2 */
929 dest->popcorn2_x = src->popcorn2_x;
930 dest->popcorn2_y = src->popcorn2_y;
931 dest->popcorn2_c = src->popcorn2_c;
932 } else if (varn==VAR_SEPARATION) {
933 /* Separation */
934 dest->separation_x = src->separation_x;
935 dest->separation_y = src->separation_y;
936 dest->separation_xinside = src->separation_xinside;
937 dest->separation_yinside = src->separation_yinside;
938 } else if (varn==VAR_SPLIT) {
939 /* Split */
940 dest->split_xsize = src->split_xsize;
941 dest->split_ysize = src->split_ysize;
942 } else if (varn==VAR_SPLITS) {
943 /* Splits */
944 dest->splits_x = src->splits_x;
945 dest->splits_y = src->splits_y;
946 } else if (varn==VAR_STRIPES) {
947 /* Stripes */
948 dest->stripes_space = src->stripes_space;
949 dest->stripes_warp = src->stripes_warp;
950 } else if (varn==VAR_WEDGE) {
951 /* Wedge */
952 dest->wedge_angle = src->wedge_angle;
953 dest->wedge_hole = src->wedge_hole;
954 dest->wedge_count = src->wedge_count;
955 dest->wedge_swirl = src->wedge_swirl;
956 } else if (varn==VAR_WEDGE_JULIA) {
957 /* Wedge_Julia */
958 dest->wedge_julia_angle = src->wedge_julia_angle;
959 dest->wedge_julia_count = src->wedge_julia_count;
960 dest->wedge_julia_power = src->wedge_julia_power;
961 dest->wedge_julia_dist = src->wedge_julia_dist;
962 dest->wedgeJulia_cf = src->wedgeJulia_cf;
963 dest->wedgeJulia_cn = src->wedgeJulia_cn;
964 dest->wedgeJulia_rN = src->wedgeJulia_rN;
965 } else if (varn==VAR_WEDGE_SPH) {
966 /* Wedge_sph */
967 dest->wedge_sph_angle = src->wedge_sph_angle;
968 dest->wedge_sph_hole = src->wedge_sph_hole;
969 dest->wedge_sph_count = src->wedge_sph_count;
970 dest->wedge_sph_swirl = src->wedge_sph_swirl;
971 } else if (varn==VAR_WHORL) {
972 /* whorl */
973 dest->whorl_inside = src->whorl_inside;
974 dest->whorl_outside = src->whorl_outside;
975 } else if (varn==VAR_WAVES2) {
976 /* waves2 */
977 dest->waves2_scalex = src->waves2_scalex;
978 dest->waves2_scaley = src->waves2_scaley;
979 dest->waves2_freqx = src->waves2_freqx;
980 dest->waves2_freqy = src->waves2_freqy;
981 } else if (varn==VAR_AUGER) {
982 /* auger */
983 dest->auger_sym = src->auger_sym;
984 dest->auger_weight = src->auger_weight;
985 dest->auger_freq = src->auger_freq;
986 dest->auger_scale = src->auger_scale;
987 } else if (varn==VAR_FLUX) {
988 /* flux */
989 dest->flux_spread = src->flux_spread;
990 } else if (varn==VAR_MOBIUS) {
991 /* mobius */
992 dest->mobius_re_a = src->mobius_re_a;
993 dest->mobius_re_b = src->mobius_re_b;
994 dest->mobius_re_c = src->mobius_re_c;
995 dest->mobius_re_d = src->mobius_re_d;
996 dest->mobius_im_a = src->mobius_im_a;
997 dest->mobius_im_b = src->mobius_im_b;
998 dest->mobius_im_c = src->mobius_im_c;
999 dest->mobius_im_d = src->mobius_im_d;
1000 }
1001 }
1002
1003 /* Motion support functions */
1004 void flam3_add_motion_element(flam3_xform *xf) {
1005
1006 /* Add one to the xform's count of motion elements */
1007 xf->num_motion++;
1008
1009 /* Reallocate the motion storage to include the empty space */
1010 xf->motion = (struct xform *)realloc(xf->motion, xf->num_motion * sizeof(struct xform));
1011
1012 /* Initialize the motion element */
1013 /* In this case, all elements should be set to 0 */
1014 memset( &(xf->motion[xf->num_motion-1]), 0, sizeof(struct xform));
1015
1016 }
1017
1018 /* Motion support functions */
1019 void flam3_delete_motion_elements(flam3_xform *xf) {
1020
1021 /* Free the motion elements */
1022 if (xf->num_motion>0) {
1023 free(xf->motion);
1024 xf->num_motion = 0;
1025 }
1026
1027 }
1028
1029 /* Xform support functions */
1030 void flam3_add_xforms(flam3_genome *thiscp, int num_to_add, int interp_padding, int final_flag) {
1031
1032 int i,j;
1033 int old_num = thiscp->num_xforms;
1034 int oldstd,numstd;
1035 flam3_xform tmp;
1036
1037 oldstd = thiscp->num_xforms - (thiscp->final_xform_index >= 0);
1038
1039 /* !!! must make sure that if final_flag is specified, we don't already have a final xform! !!! */
1040
1041 // if (thiscp->num_xforms > 0)
1042 thiscp->xform = (flam3_xform *)realloc(thiscp->xform, (thiscp->num_xforms + num_to_add) * sizeof(flam3_xform));
1043 // else
1044 // thiscp->xform = (flam3_xform *)malloc(num_to_add * sizeof(flam3_xform));
1045
1046 thiscp->num_xforms += num_to_add;
1047
1048 /* Initialize all the new xforms */
1049 initialize_xforms(thiscp, old_num);
1050
1051 /* Set the padding flag for the new xforms */
1052 if (interp_padding) {
1053 for (i = old_num ; i < thiscp->num_xforms ; i++)
1054 thiscp->xform[i].padding=1;
1055 }
1056
1057 /* If the final xform is not the last xform in the list, make it so */
1058 if (thiscp->final_xform_index >= 0 && thiscp->final_xform_index != thiscp->num_xforms-1) {
1059 tmp = thiscp->xform[thiscp->final_xform_index];
1060 for (i=thiscp->final_xform_index; i < thiscp->num_xforms-1; i++)
1061 thiscp->xform[i] = thiscp->xform[i+1];
1062
1063 thiscp->final_xform_index = thiscp->num_xforms-1;
1064 thiscp->xform[thiscp->final_xform_index] = tmp;
1065 }
1066
1067 if (final_flag) {
1068 /* Set the final xform index */
1069 thiscp->final_xform_enable = 1;
1070 thiscp->final_xform_index = thiscp->num_xforms-1;
1071 } else {
1072 /* Handle the chaos array */
1073 numstd = thiscp->num_xforms - (thiscp->final_xform_index>=0);
1074
1075 /* Pad existing rows */
1076 for (i=0;i<oldstd;i++) {
1077 thiscp->chaos[i] = realloc(thiscp->chaos[i], numstd * sizeof(double));
1078 for (j=oldstd; j<numstd; j++)
1079 thiscp->chaos[i][j] = 1.0;
1080 }
1081
1082 /* Add new rows */
1083 thiscp->chaos = realloc(thiscp->chaos,numstd * sizeof(double *));
1084 for (i=oldstd; i<numstd; i++) {
1085 thiscp->chaos[i] = malloc(numstd * sizeof(double));
1086 for (j=0;j<numstd;j++)
1087 thiscp->chaos[i][j] = 1.0;
1088 }
1089 }
1090 }
1091
1092 void flam3_delete_xform(flam3_genome *thiscp, int idx_to_delete) {
1093
1094 int i,j;
1095 int num_std = thiscp->num_xforms - (thiscp->final_xform_index >= 0);
1096
1097 if (thiscp->final_xform_index != idx_to_delete) {
1098 /* We're going to delete the nth std xform. */
1099
1100 /* Delete the nth_std row of the chaos array */
1101 free(thiscp->chaos[idx_to_delete]);
1102
1103 /* Shift the pointers down one */
1104 for (i=idx_to_delete+1;i<num_std;i++)
1105 thiscp->chaos[i-1] = thiscp->chaos[i];
1106
1107 /* Realloc the pointer array */
1108 thiscp->chaos = realloc(thiscp->chaos,(num_std-1)*sizeof(double *));
1109 num_std--;
1110
1111 /* Loop over all of the rows and remove the nth_std element from them */
1112 for (i=0;i<num_std;i++) {
1113 for (j=idx_to_delete+1;j<num_std+1;j++) {
1114 thiscp->chaos[i][j-1] = thiscp->chaos[i][j];
1115 }
1116 /* Realloc the vector to have one less element */
1117 thiscp->chaos[i] = realloc(thiscp->chaos[i],num_std*sizeof(double));
1118
1119 }
1120 }
1121
1122 /* Handle the final xform index */
1123 if (thiscp->final_xform_index == idx_to_delete) {
1124 thiscp->final_xform_index = -1;
1125 thiscp->final_xform_enable = 0;
1126 } else if (thiscp->final_xform_index > idx_to_delete) {
1127 thiscp->final_xform_index--;
1128 }
1129
1130 /* Delete the motion elements of the banished xform */
1131 flam3_delete_motion_elements(&(thiscp->xform[idx_to_delete]));
1132
1133 /* Move all of the xforms down one - this does not require manual motion xform adjustment */
1134 for (i=idx_to_delete; i<thiscp->num_xforms-1; i++)
1135 thiscp->xform[i] = thiscp->xform[i+1];
1136
1137 thiscp->num_xforms--;
1138
1139 /* Reduce the memory storage by one xform */
1140 thiscp->xform = (flam3_xform *)realloc(thiscp->xform, sizeof(flam3_xform) * thiscp->num_xforms);
1141
1142 }
1143
1144 void flam3_copy_xform(flam3_xform *dest, flam3_xform *src) {
1145
1146 int j;
1147
1148 /* Make sure the dest doesn't have motion already */
1149 if (dest->num_motion>0)
1150 flam3_delete_motion_elements(dest);
1151
1152 /* Copy everything */
1153 *dest = *src;
1154
1155 /* Reset motion in dest and copy it */
1156 dest->num_motion=0;
1157 dest->motion=NULL;
1158
1159 if (src->num_motion>0) {
1160 for (j=0;j<src->num_motion;j++)
1161 flam3_add_motion_element(dest);
1162
1163 memcpy(dest->motion,src->motion,src->num_motion*sizeof(flam3_xform));
1164 }
1165 }
1166
1167 /* Copy one control point to another */
1168 void flam3_copy(flam3_genome *dest, flam3_genome *src) {
1169
1170 int i,ii;
1171 int numstd;
1172
1173 /* If there are any xforms in dest before the copy, clean them up */
1174 clear_cp(dest, 1);
1175
1176 /* Copy main contents of genome */
1177 memcpy(dest, src, sizeof(flam3_genome));
1178
1179 /* Only the pointer to the xform was copied, not the actual xforms. */
1180 /* We need to create new xform memory storage for this new cp */
1181 /* This goes for chaos, too. */
1182 dest->num_xforms = 0;
1183 dest->final_xform_index = -1;
1184 dest->xform = NULL;
1185 dest->chaos = NULL;
1186
1187 /* Add the standard xforms first */
1188 numstd = src->num_xforms-(src->final_xform_index>=0);
1189 flam3_add_xforms(dest, numstd, 0, 0);
1190 for (i=0;i<numstd;i++)
1191 flam3_copy_xform(&dest->xform[i], &src->xform[i]);
1192
1193 /* Add the final x if it's present */
1194 if (src->final_xform_index>=0) {
1195 i = src->final_xform_index;
1196 flam3_add_xforms(dest, 1, 0, 1);
1197 ii = dest->final_xform_index;
1198 flam3_copy_xform(&dest->xform[ii],&src->xform[i]);
1199 }
1200
1201 /* Also, only the pointer to the chaos array was copied.
1202 * We have to take care of that as well. */
1203 for (i=0;i<numstd;i++)
1204 memcpy(dest->chaos[i],src->chaos[i], numstd * sizeof(double));
1205
1206 }
1207
1208 void flam3_copyx(flam3_genome *dest, flam3_genome *src, int dest_std_xforms, int dest_final_xform) {
1209
1210 int i,numsrcstd;
1211
1212 /* If there are any xforms in dest before the copy, clean them up */
1213 clear_cp(dest, 1);
1214
1215 /* Copy main contents of genome */
1216 memcpy(dest, src, sizeof(flam3_genome));
1217
1218 /* Only the pointer to the xform was copied, not the actual xforms. */
1219 /* We need to create new xform memory storage for this new cp */
1220 /* This goes for chaos, too. */
1221 dest->num_xforms = 0;
1222 dest->xform = NULL;
1223 dest->chaos = NULL;
1224 dest->final_xform_index = -1;
1225
1226 /* Add the padded standard xform list */
1227 /* Set the pad to 1 for these */
1228 flam3_add_xforms(dest, dest_std_xforms, 1, 0);
1229
1230 numsrcstd = src->num_xforms - (src->final_xform_index >= 0);
1231
1232 for(i=0;i<numsrcstd;i++) {
1233
1234 /* When we copy the old xform, the pad is set to 0 */
1235 flam3_copy_xform(&dest->xform[i],&src->xform[i]);
1236
1237 /* Copy the initial chaos from the src - the rest are already 1 */
1238 memcpy(dest->chaos[i], src->chaos[i], numsrcstd*sizeof(double));
1239
1240 }
1241
1242 /* Add the final xform if necessary */
1243 if (dest_final_xform > 0) {
1244 flam3_add_xforms(dest, dest_final_xform, 1, 1);
1245
1246 if (src->final_xform_enable > 0) {
1247
1248 i = src->final_xform_index;
1249
1250 flam3_copy_xform(&dest->xform[dest->num_xforms-1],&src->xform[i]);
1251
1252 } else {
1253 /* Interpolated-against final xforms need animate & color_speed set to 0.0 */
1254 dest->xform[dest->num_xforms-1].num_motion = 0;
1255 dest->xform[dest->num_xforms-1].motion=NULL;
1256 dest->xform[dest->num_xforms-1].animate=0.0;
1257 dest->xform[dest->num_xforms-1].color_speed=0.0;
1258 }
1259
1260 } else {
1261 dest->final_xform_index = -1;
1262 dest->final_xform_enable = 0;
1263 }
1264
1265 }
1266
1267 void clear_cp(flam3_genome *cp, int default_flag) {
1268 cp->palette_index = flam3_palette_random;
1269 cp->center[0] = 0.0;
1270 cp->center[1] = 0.0;
1271 cp->rot_center[0] = 0.0;
1272 cp->rot_center[1] = 0.0;
1273 cp->gamma = 4.0;
1274 cp->vibrancy = 1.0;
1275 cp->contrast = 1.0;
1276 cp->brightness = 4.0;
1277 cp->symmetry = 0;
1278 cp->hue_rotation = 0.0;
1279 cp->rotate = 0.0;
1280 cp->pixels_per_unit = 50;
1281 cp->interpolation = flam3_interpolation_linear;
1282 cp->palette_interpolation = flam3_palette_interpolation_hsv_circular;
1283 cp->hsv_rgb_palette_blend = 0.0;
1284
1285 cp->genome_index = 0;
1286 memset(cp->parent_fname,0,flam3_parent_fn_len);
1287
1288 if (default_flag==flam3_defaults_on) {
1289 /* If defaults are on, set to reasonable values */
1290 cp->highlight_power = -1.0;
1291 cp->background[0] = 0.0;
1292 cp->background[1] = 0.0;
1293 cp->background[2] = 0.0;
1294 cp->width = 100;
1295 cp->height = 100;
1296 cp->spatial_oversample = 1;
1297 cp->spatial_filter_radius = 0.5;
1298 cp->zoom = 0.0;
1299 cp->sample_density = 1;
1300 /* Density estimation stuff defaulting to ON */
1301 cp->estimator = 9.0;
1302 cp->estimator_minimum = 0.0;
1303 cp->estimator_curve = 0.4;
1304 cp->gam_lin_thresh = 0.01;
1305 // cp->motion_exp = 0.0;
1306 cp->nbatches = 1;
1307 cp->ntemporal_samples = 1000;
1308 cp->spatial_filter_select = flam3_gaussian_kernel;
1309 cp->interpolation_type = flam3_inttype_log;
1310 cp->temporal_filter_type = flam3_temporal_box;
1311 cp->temporal_filter_width = 1.0;
1312 cp->temporal_filter_exp = 0.0;
1313 cp->palette_mode = flam3_palette_mode_step;
1314
1315 } else {
1316 /* Defaults are off, so set to UN-reasonable values. */
1317 cp->highlight_power = -1.0;
1318 cp->background[0] = -1.0;
1319 cp->background[1] = -1.0;
1320 cp->background[2] = -1.0;
1321 cp->zoom = 999999999;
1322 cp->spatial_oversample = -1;
1323 cp->spatial_filter_radius = -1;
1324 cp->nbatches = -1;
1325 cp->ntemporal_samples = -1;
1326 cp->width = -1;
1327 cp->height = -1;
1328 cp->sample_density = -1;
1329 cp->estimator = -1;
1330 cp->estimator_minimum = -1;
1331 cp->estimator_curve = -1;
1332 cp->gam_lin_thresh = -1;
1333 // cp->motion_exp = -999;
1334 cp->nbatches = 0;
1335 cp->ntemporal_samples = 0;
1336 cp->spatial_filter_select = -1;
1337 cp->interpolation_type = -1;
1338 cp->temporal_filter_type = -1;
1339 cp->temporal_filter_width = -1;
1340 cp->temporal_filter_exp = -999;
1341 cp->palette_mode = -1;
1342 }
1343
1344 if (cp->xform != NULL && cp->num_xforms > 0) {
1345 int i;
1346 int ns = cp->num_xforms - (cp->final_xform_index>=0);
1347
1348 for (i=0;i<ns;i++) {
1349 free(cp->chaos[i]);
1350 }
1351 free(cp->chaos);
1352 cp->chaos=NULL;
1353
1354 for (i=0;i<cp->num_xforms;i++)
1355 flam3_delete_motion_elements(&cp->xform[i]);
1356
1357 free(cp->xform);
1358 cp->xform=NULL;
1359
1360 cp->num_xforms = 0;
1361 }
1362
1363 cp->final_xform_enable = 0;
1364 cp->final_xform_index = -1;
1365
1366 }
1367
1368
1369 int flam3_count_nthreads(void) {
1370 int nthreads;
1371 #ifndef HAVE_LIBPTHREAD
1372 return(1);
1373 #endif
1374
1375 #ifdef _WIN32
1376 SYSTEM_INFO sysInfo;
1377 GetSystemInfo(&sysInfo);
1378 nthreads = sysInfo.dwNumberOfProcessors;
1379 #else
1380 #ifdef __APPLE__
1381 kern_return_t kr;
1382 host_name_port_t host;
1383 unsigned int size;
1384 struct host_basic_info hi;
1385
1386 host = mach_host_self();
1387 size = sizeof(hi)/sizeof(int);
1388 kr = host_info(host, HOST_BASIC_INFO, (host_info_t)&hi, &size);
1389 if (kr != KERN_SUCCESS) {
1390 mach_error("host_info():", kr);
1391 /* set threads to 1 on error */
1392 nthreads = 1;
1393 } else
1394 nthreads = hi.avail_cpus;
1395 #else
1396 #ifndef _SC_NPROCESSORS_ONLN
1397 char line[MAXBUF];
1398 FILE *f = fopen("/proc/cpuinfo", "r");
1399 if (NULL == f) goto def;
1400 nthreads = 0;
1401 while (fgets(line, MAXBUF, f)) {
1402 if (!strncmp("processor\t:", line, 11))
1403 nthreads++;
1404 }
1405 fclose(f);
1406 if (nthreads < 1) goto def;
1407 return (nthreads);
1408 def:
1409 fprintf(stderr, "could not read /proc/cpuinfo, using one render thread.\n");
1410 nthreads = 1;
1411 #else
1412 nthreads = sysconf(_SC_NPROCESSORS_ONLN);
1413 if (nthreads < 1) nthreads = 1;
1414 #endif
1415 #endif
1416 #endif
1417 return (nthreads);
1418 }
1419
1420 flam3_genome *flam3_parse_xml2(char *xmldata, char *xmlfilename, int default_flag, int *ncps) {
1421
1422 xmlDocPtr doc; /* Parsed XML document tree */
1423 xmlNode *rootnode;
1424 char *bn;
1425 int i;
1426 int loc_all_ncps=0;
1427 flam3_genome *loc_all_cp=NULL;
1428 char* locale = NULL;
1429 char* lorig = setlocale(LC_NUMERIC, NULL);
1430
1431 /* Parse XML string into internal document */
1432 /* Forbid network access during read */
1433 doc = xmlReadMemory(xmldata, (int)strlen(xmldata), xmlfilename, NULL, XML_PARSE_NONET);
1434
1435 /* Check for errors */
1436 if (doc==NULL) {
1437 fprintf(stderr, "Failed to parse %s\n", xmlfilename);
1438 return NULL;
1439 }
1440
1441 /* What is the root node of the document? */
1442 rootnode = xmlDocGetRootElement(doc);
1443
1444 // force use of "C" locale when writing reals.
1445 // first save away the current settings.
1446 if (lorig == NULL)
1447 fprintf(stderr, "error: couldn't get current locale\n");
1448 else {
1449 int slen = strlen(lorig) + 1;
1450 locale = (char*)malloc(slen);
1451 if (locale != NULL)
1452 memcpy(locale, lorig, slen);
1453 }
1454 if (setlocale(LC_NUMERIC, "C") == NULL)
1455 fprintf(stderr, "error: couldn't set C locale\n");
1456
1457 /* Scan for <flame> nodes, starting with this node */
1458 bn = basename(xmlfilename);
1459
1460 /* Have to use &loc_all_cp since the memory gets allocated in scan_for_flame_nodes */
1461 scan_for_flame_nodes(rootnode, bn, default_flag,&loc_all_cp,&loc_all_ncps);
1462
1463 // restore locale
1464 if (locale != NULL) {
1465 if (setlocale(LC_NUMERIC, locale) == NULL)
1466 fprintf(stderr, "error: couldn't replace locale settings\n");
1467 free(locale);
1468 }
1469
1470 xmlFreeDoc(doc);
1471
1472 *ncps = loc_all_ncps;
1473
1474 /* Check to see if the first control point or the second-to-last */
1475 /* control point has interpolation="smooth". This is invalid */
1476 /* and should be reset to linear (with a warning). */
1477 if (loc_all_ncps>=1) {
1478 if (loc_all_cp[0].interpolation == flam3_interpolation_smooth) {
1479 fprintf(stderr,"Warning: smooth interpolation cannot be used for first segment.\n"
1480 " switching to linear.\n");
1481 loc_all_cp[0].interpolation = flam3_interpolation_linear;
1482 }
1483 }
1484
1485 if (loc_all_ncps>=2) {
1486 if (loc_all_cp[(loc_all_ncps)-2].interpolation == flam3_interpolation_smooth) {
1487 fprintf(stderr,"Warning: smooth interpolation cannot be used for last segment.\n"
1488 " switching to linear.\n");
1489 loc_all_cp[loc_all_ncps-2].interpolation = flam3_interpolation_linear;
1490 }
1491 }
1492
1493 /* Finally, ensure that consecutive 'rotate' parameters never exceed */
1494 /* a difference of more than 180 degrees (+/-) for interpolation. */
1495 /* An adjustment of +/- 360 degrees is made until this is true. */
1496 if (*ncps>1) {
1497
1498 for (i=1;i<*ncps;i++) {
1499
1500 /* Only do this adjustment if we're not in compat mode */
1501 if (flam3_inttype_compat != loc_all_cp[i-1].interpolation_type
1502 && flam3_inttype_older != loc_all_cp[i-1].interpolation_type) {
1503
1504 while (loc_all_cp[i].rotate < loc_all_cp[i-1].rotate-180)
1505 loc_all_cp[i].rotate += 360;
1506
1507 while (loc_all_cp[i].rotate > loc_all_cp[i-1].rotate+180)
1508 loc_all_cp[i].rotate -= 360;
1509 }
1510 }
1511 }
1512
1513 //Note that concurrent calls to flam3, if in parallel, potentially segfault
1514 //if this function is called. technically it's required but it doesn't
1515 //leak memory continuously.
1516 //xmlCleanupParser();
1517
1518 return loc_all_cp;
1519 }
1520
1521 flam3_genome * flam3_parse_from_file(FILE *f, char *fname, int default_flag, int *ncps) {
1522 int i, c, slen = 5000;
1523 char *s, *snew;
1524 flam3_genome *ret;
1525
1526 /* Incrementally read XML file into a string */
1527 s = malloc(slen);
1528 i = 0;
1529 do {
1530 c = getc(f);
1531 if (EOF == c)
1532 break;
1533 s[i++] = c;
1534 if (i == slen-1) {
1535 slen *= 2;
1536 snew = realloc(s, slen);
1537 if (snew==NULL) {
1538 fprintf(stderr,"XML file too large to be read. continuing with partial file.\n");
1539 break;
1540 } else
1541 s = snew;
1542 }
1543 } while (1);
1544
1545 /* Null-terminate the read XML data */
1546 s[i] = 0;
1547
1548 /* Parse the XML string */
1549 if (fname)
1550 ret = flam3_parse_xml2(s, fname, default_flag, ncps);
1551 else
1552 ret = flam3_parse_xml2(s, "stdin", default_flag, ncps);
1553
1554 free(s);
1555
1556 return(ret);
1557
1558 }
1559
1560
1561 void flam3_apply_template(flam3_genome *cp, flam3_genome *templ) {
1562
1563 /* Check for invalid values - only replace those with valid ones */
1564 if (templ->background[0] >= 0)
1565 cp->background[0] = templ->background[0];
1566 if (templ->background[1] >= 0)
1567 cp->background[1] = templ->background[1];
1568 if (templ->background[1] >= 0)
1569 cp->background[2] = templ->background[2];
1570 if (templ->zoom < 999999998)
1571 cp->zoom = templ->zoom;
1572 if (templ->spatial_oversample > 0)
1573 cp->spatial_oversample = templ->spatial_oversample;
1574 if (templ->spatial_filter_radius >= 0)
1575 cp->spatial_filter_radius = templ->spatial_filter_radius;
1576 if (templ->sample_density > 0)
1577 cp->sample_density = templ->sample_density;
1578 if (templ->nbatches > 0)
1579 cp->nbatches = templ->nbatches;
1580 if (templ->ntemporal_samples > 0)
1581 cp->ntemporal_samples = templ->ntemporal_samples;
1582 if (templ->width > 0) {
1583 /* preserving scale should be an option */
1584 // cp->pixels_per_unit = cp->pixels_per_unit * templ->width / cp->width;
1585 cp->pixels_per_unit = cp->pixels_per_unit * templ->height / cp->height;
1586 cp->width = templ->width;
1587 }
1588 if (templ->height > 0)
1589 cp->height = templ->height;
1590 if (templ->estimator >= 0)
1591 cp->estimator = templ->estimator;
1592 if (templ->estimator_minimum >= 0)
1593 cp->estimator_minimum = templ->estimator_minimum;
1594 if (templ->estimator_curve >= 0)
1595 cp->estimator_curve = templ->estimator_curve;
1596 if (templ->gam_lin_thresh >= 0)
1597 cp->gam_lin_thresh = templ->gam_lin_thresh;
1598 if (templ->nbatches>0)
1599 cp->nbatches = templ->nbatches;
1600 if (templ->ntemporal_samples>0)
1601 cp->ntemporal_samples = templ->ntemporal_samples;
1602 if (templ->spatial_filter_select>0)
1603 cp->spatial_filter_select = templ->spatial_filter_select;
1604 if (templ->interpolation >= 0)
1605 cp->interpolation = templ->interpolation;
1606 if (templ->interpolation_type >= 0)
1607 cp->interpolation_type = templ->interpolation_type;
1608 if (templ->temporal_filter_type >= 0)
1609 cp->temporal_filter_type = templ->temporal_filter_type;
1610 if (templ->temporal_filter_width > 0)
1611 cp->temporal_filter_width = templ->temporal_filter_width;
1612 if (templ->temporal_filter_exp > -900)
1613 cp->temporal_filter_exp = templ->temporal_filter_exp;
1614 if (templ->highlight_power >=0)
1615 cp->highlight_power = templ->highlight_power;
1616 if (templ->palette_mode >= 0)
1617 cp->palette_mode = templ->palette_mode;
1618 if (templ->palette_interpolation >= 0)
1619 cp->palette_interpolation = templ->palette_interpolation;
1620 if (templ->hsv_rgb_palette_blend >= 0)
1621 cp->hsv_rgb_palette_blend = templ->hsv_rgb_palette_blend;
1622
1623 }
1624
1625 char *flam3_print_to_string(flam3_genome *cp) {
1626
1627 FILE *tmpflame;
1628 long stringbytes;
1629 char *genome_string;
1630
1631 int using_tmpdir = 0;
1632 char tmpnam[256];
1633
1634 tmpflame = tmpfile();
1635 if (NULL==tmpflame) {
1636 #ifdef _WIN32
1637 // This might be a permissions problem, so let's try to open a
1638 // tempfile in the env var TEMP's area instead
1639 char* tmp_path = getenv("TEMP");
1640
1641 if (tmp_path != NULL) {
1642 strcpy(tmpnam, tmp_path);
1643 strcat(tmpnam, "\\fr0st.tmp");
1644 tmpflame = fopen(tmpnam, "w+");
1645 if (tmpflame != NULL) {
1646 using_tmpdir = 1;
1647 }
1648 }
1649 #endif
1650 if (using_tmpdir == 0) {
1651 perror("FLAM3: opening temporary file");
1652 return (NULL);
1653 }
1654 }
1655 flam3_print(tmpflame,cp,NULL,flam3_dont_print_edits);
1656 stringbytes = ftell(tmpflame);
1657 fseek(tmpflame,0L, SEEK_SET);
1658 genome_string = (char *)calloc(stringbytes+1,1);
1659 if (stringbytes != fread(genome_string, 1, stringbytes, tmpflame)) {
1660 perror("FLAM3: reading string from temp file");
1661 }
1662 fclose(tmpflame);
1663
1664 if (using_tmpdir)
1665 unlink(tmpnam);
1666
1667 return(genome_string);
1668 }
1669
1670
1671 void flam3_print(FILE *f, flam3_genome *cp, char *extra_attributes, int print_edits) {
1672 int i,numstd;
1673 int flam27_flag;
1674 char *ai;
1675
1676 // force use of "C" locale when writing reals.
1677 // first save away the current settings.
1678 char* locale = NULL;
1679 char* lorig = setlocale(LC_NUMERIC, NULL);
1680 if (lorig == NULL)
1681 fprintf(stderr, "error: couldn't get current locale\n");
1682 else {
1683 int slen = strlen(lorig) + 1;
1684 locale = (char*)malloc(slen);
1685 if (locale != NULL)
1686 memcpy(locale, lorig, slen);
1687 }
1688 if (setlocale(LC_NUMERIC, "C") == NULL)
1689 fprintf(stderr, "error: couldn't set C locale\n");
1690
1691
1692 flam27_flag = argi("flam27",0);
1693
1694 fprintf(f, "<flame version=\"FLAM3-%s\" time=\"%g\"", flam3_version(),cp->time);
1695
1696 if (cp->flame_name[0]!=0)
1697 fprintf(f, " name=\"%s\"",cp->flame_name);
1698
1699 fprintf(f, " size=\"%d %d\"", cp->width, cp->height);
1700 fprintf(f, " center=\"%g %g\"", cp->center[0], cp->center[1]);
1701 fprintf(f, " scale=\"%g\"", cp->pixels_per_unit);
1702
1703 if (cp->zoom != 0.0)
1704 fprintf(f, " zoom=\"%g\"", cp->zoom);
1705
1706 fprintf(f, " rotate=\"%g\"", cp->rotate);
1707 fprintf(f, " supersample=\"%d\"", cp->spatial_oversample);
1708 fprintf(f, " filter=\"%g\"", cp->spatial_filter_radius);
1709
1710 /* Need to print the correct kernel to use */
1711 if (cp->spatial_filter_select == flam3_gaussian_kernel)
1712 fprintf(f, " filter_shape=\"gaussian\"");
1713 else if (cp->spatial_filter_select == flam3_hermite_kernel)
1714 fprintf(f, " filter_shape=\"hermite\"");
1715 else if (cp->spatial_filter_select == flam3_box_kernel)
1716 fprintf(f, " filter_shape=\"box\"");
1717 else if (cp->spatial_filter_select == flam3_triangle_kernel)
1718 fprintf(f, " filter_shape=\"triangle\"");
1719 else if (cp->spatial_filter_select == flam3_bell_kernel)
1720 fprintf(f, " filter_shape=\"bell\"");
1721 else if (cp->spatial_filter_select == flam3_b_spline_kernel)
1722 fprintf(f, " filter_shape=\"bspline\"");
1723 else if (cp->spatial_filter_select == flam3_mitchell_kernel)
1724 fprintf(f, " filter_shape=\"mitchell\"");
1725 else if (cp->spatial_filter_select == flam3_blackman_kernel)
1726 fprintf(f, " filter_shape=\"blackman\"");
1727 else if (cp->spatial_filter_select == flam3_catrom_kernel)
1728 fprintf(f, " filter_shape=\"catrom\"");
1729 else if (cp->spatial_filter_select == flam3_hanning_kernel)
1730 fprintf(f, " filter_shape=\"hanning\"");
1731 else if (cp->spatial_filter_select == flam3_hamming_kernel)
1732 fprintf(f, " filter_shape=\"hamming\"");
1733 else if (cp->spatial_filter_select == flam3_lanczos3_kernel)
1734 fprintf(f, " filter_shape=\"lanczos3\"");
1735 else if (cp->spatial_filter_select == flam3_lanczos2_kernel)
1736 fprintf(f, " filter_shape=\"lanczos2\"");
1737 else if (cp->spatial_filter_select == flam3_quadratic_kernel)
1738 fprintf(f, " filter_shape=\"quadratic\"");
1739
1740 if (cp->temporal_filter_type == flam3_temporal_box)
1741 fprintf(f, " temporal_filter_type=\"box\"");
1742 else if (cp->temporal_filter_type == flam3_temporal_gaussian)
1743 fprintf(f, " temporal_filter_type=\"gaussian\"");
1744 else if (cp->temporal_filter_type == flam3_temporal_exp)
1745 fprintf(f, " temporal_filter_type=\"exp\" temporal_filter_exp=\"%g\"",cp->temporal_filter_exp);
1746
1747 fprintf(f, " temporal_filter_width=\"%g\"",cp->temporal_filter_width);
1748
1749
1750
1751 fprintf(f, " quality=\"%g\"", cp->sample_density);
1752 fprintf(f, " passes=\"%d\"", cp->nbatches);
1753 fprintf(f, " temporal_samples=\"%d\"", cp->ntemporal_samples);
1754 fprintf(f, " background=\"%g %g %g\"",
1755 cp->background[0], cp->background[1], cp->background[2]);
1756 fprintf(f, " brightness=\"%g\"", cp->brightness);
1757 fprintf(f, " gamma=\"%g\"", cp->gamma);
1758
1759 if (!flam27_flag)
1760 fprintf(f, " highlight_power=\"%g\"", cp->highlight_power);
1761
1762 fprintf(f, " vibrancy=\"%g\"", cp->vibrancy);
1763 fprintf(f, " estimator_radius=\"%g\" estimator_minimum=\"%g\" estimator_curve=\"%g\"",
1764 cp->estimator, cp->estimator_minimum, cp->estimator_curve);
1765 fprintf(f, " gamma_threshold=\"%g\"", cp->gam_lin_thresh);
1766
1767 if (flam3_palette_mode_step == cp->palette_mode)
1768 fprintf(f, " palette_mode=\"step\"");
1769 else if (flam3_palette_mode_linear == cp->palette_mode)
1770 fprintf(f, " palette_mode=\"linear\"");
1771
1772 if (flam3_interpolation_linear != cp->interpolation)
1773 fprintf(f, " interpolation=\"smooth\"");
1774
1775 if (flam3_inttype_linear == cp->interpolation_type)
1776 fprintf(f, " interpolation_type=\"linear\"");
1777 else if (flam3_inttype_log == cp->interpolation_type)
1778 fprintf(f, " interpolation_type=\"log\"");
1779 else if (flam3_inttype_compat == cp->interpolation_type)
1780 fprintf(f, " interpolation_type=\"old\"");
1781 else if (flam3_inttype_older == cp->interpolation_type)
1782 fprintf(f, " interpolation_type=\"older\"");
1783
1784
1785 if (flam3_palette_interpolation_sweep == cp->palette_interpolation)
1786 fprintf(f, " palette_interpolation=\"sweep\"");
1787 else if (flam3_palette_interpolation_rgb == cp->palette_interpolation)
1788 fprintf(f, " palette_interpolation=\"rgb\"");
1789 else if (flam3_palette_interpolation_hsv == cp->palette_interpolation)
1790 fprintf(f, " palette_interpolation=\"hsv\"");
1791 else if (flam3_palette_interpolation_hsv_circular == cp->palette_interpolation) {
1792 fprintf(f, " palette_interpolation=\"hsv_circular\"");
1793 if (cp->hsv_rgb_palette_blend > 0.0)
1794 fprintf(f, " hsv_rgb_palette_blend=\"%g\"", cp->hsv_rgb_palette_blend);
1795 }
1796
1797 if (extra_attributes)
1798 fprintf(f, " %s", extra_attributes);
1799
1800 fprintf(f, ">\n");
1801
1802 if (cp->symmetry)
1803 fprintf(f, " <symmetry kind=\"%d\"/>\n", cp->symmetry);
1804
1805 numstd = cp->num_xforms - (cp->final_xform_index>=0);
1806
1807 for (i = 0; i < cp->num_xforms; i++) {
1808
1809 if (i==cp->final_xform_index)
1810 flam3_print_xform(f, &cp->xform[i], 1, numstd, NULL, 0);
1811 else
1812 flam3_print_xform(f, &cp->xform[i], 0, numstd, cp->chaos[i], 0);
1813
1814 }
1815
1816 int hexpalette = argi("hexpalette",0);
1817
1818 if (hexpalette) {
1819
1820 fprintf(f," <palette count=\"256\" format=\"RGB\">");
1821
1822 for (i=0; i < 256; i++) {
1823
1824 int r, g, b;
1825 r = rint(cp->palette[i].color[0] * 255.0);
1826 g = rint(cp->palette[i].color[1] * 255.0);
1827 b = rint(cp->palette[i].color[2] * 255.0);
1828
1829 if (i % 8 == 0) {
1830 fprintf(f,"\n");
1831 fprintf(f," ");
1832 }
1833
1834 fprintf(f,"%2x%2x%2x",r,g,b);
1835
1836 }
1837
1838 fprintf(f,"\n");
1839 fprintf(f," </palette>\n");
1840
1841 } else {
1842 for (i = 0; i < 256; i++) {
1843 double r, g, b, a;
1844 r = (cp->palette[i].color[0] * 255.0);
1845 g = (cp->palette[i].color[1] * 255.0);
1846 b = (cp->palette[i].color[2] * 255.0);
1847 a = (cp->palette[i].color[3] * 255.0);
1848
1849 fprintf(f, " ");
1850
1851 if (flam27_flag || a==255.0) {
1852
1853 if (flam27_flag && a!=255.0)
1854 fprintf(stderr,"alpha channel in palette cannot be stored in 2.7 compatibility mode; truncating\n");
1855
1856 if (getenv("intpalette"))
1857 fprintf(f, "<color index=\"%d\" rgb=\"%d %d %d\"/>", i, (int)rint(r), (int)rint(g), (int)rint(b));
1858 else {
1859 #ifdef USE_FLOAT_INDICES
1860 fprintf(f, "<color index=\"%.10g\" rgb=\"%.6g %.6g %.6g\"/>", cp->palette[i].index, r, g, b);
1861 #else
1862 fprintf(f, "<color index=\"%d\" rgb=\"%.6g %.6g %.6g\"/>", i, r, g, b);
1863 #endif
1864 }
1865 } else {
1866 if (getenv("intpalette"))
1867 fprintf(f, " <color index=\"%d\" rgba=\"%d %d %d %d\"/>", i, (int)rint(r), (int)rint(g), (int)rint(b), (int)rint(a));
1868 else
1869 fprintf(f, " <color index=\"%d\" rgba=\"%.6g %.6g %.6g %.6g\"/>", i, r, g, b, a);
1870 }
1871 // if (i%4 == 3)
1872 fprintf(f, "\n");
1873
1874 }
1875 }
1876
1877 if (cp->edits != NULL && print_edits==flam3_print_edits) {
1878
1879 /* We need a custom script for printing these */
1880 /* and it needs to be recursive */
1881 xmlNodePtr elem_node = xmlDocGetRootElement(cp->edits);
1882 flam3_edit_print(f,elem_node, 1, 1);
1883 }
1884 fprintf(f, "</flame>\n");
1885
1886 if (locale != NULL) {
1887 if (setlocale(LC_NUMERIC, locale) == NULL)
1888 fprintf(stderr, "error: couldn't restore locale settings\n");
1889 free(locale);
1890 }
1891 }
1892
1893 #define PRINTNON(p) do { if (x->p != 0.0) fprintf(f, #p "=\"%f\" ",x->p); } while(0)
1894
1895
1896 void flam3_print_xform(FILE *f, flam3_xform *x, int final_flag, int numstd, double *chaos_row, int motion_flag) {
1897
1898 int blob_var=0,pdj_var=0,fan2_var=0,rings2_var=0,perspective_var=0;
1899 int juliaN_var=0,juliaScope_var=0,radialBlur_var=0,pie_var=0,disc2_var=0;
1900 int ngon_var=0,curl_var=0,rectangles_var=0,supershape_var=0;
1901 int flower_var=0,conic_var=0,parabola_var=0,bent2_var=0,bipolar_var=0;
1902 int cell_var=0,cpow_var=0,curve_var=0,escher_var=0,lazys_var=0;
1903 int modulus_var=0,oscope_var=0,popcorn2_var=0,separation_var=0;
1904 int split_var=0,splits_var=0,stripes_var=0,wedge_var=0,wedgeJ_var=0;
1905 int wedgeS_var=0,whorl_var=0,waves2_var=0,auger_var=0,flux_var=0;
1906 int mobius_var=0;
1907
1908 int j;
1909 int lnv;
1910
1911 int flam27_flag;
1912 char *ai;
1913
1914 flam27_flag = argi("flam27",0);
1915
1916 /* Motion flag will not be set if flam27_flag is set */
1917 if (motion_flag) {
1918 fprintf(f, " <motion motion_frequency=\"%d\" ",x->motion_freq);
1919 if (x->motion_func == MOTION_SIN)
1920 fprintf(f, "motion_function=\"sin\" ");
1921 else if (x->motion_func == MOTION_TRIANGLE)
1922 fprintf(f, "motion_function=\"triangle\" ");
1923 else if (x->motion_func == MOTION_HILL)
1924 fprintf(f, "motion_function=\"hill\" ");
1925 } else {
1926 if (final_flag)
1927 fprintf(f, " <finalxform ");
1928 else
1929 fprintf(f, " <xform weight=\"%g\" ", x->density);
1930 }
1931
1932 if (!motion_flag || x->color != 0.0)
1933 fprintf(f, "color=\"%g\" ", x->color);
1934
1935 if (flam27_flag)
1936 fprintf(f, "symmetry=\"%g\" ", 1.0-2.0*x->color_speed);
1937 else if (!motion_flag || x->color_speed != 0.0)
1938 fprintf(f, "color_speed=\"%g\" ", x->color_speed);
1939
1940 if (!final_flag && !motion_flag && !flam27_flag)
1941 fprintf(f, "animate=\"%g\" ", x->animate);
1942
1943 lnv = flam27_flag ? 54:flam3_nvariations;
1944
1945 for (j = 0; j < lnv; j++) {
1946 double v = x->var[j];
1947 if (j == VAR_TWINTRIAN) continue;
1948 if (0.0 != v) {
1949 fprintf(f, "%s=\"%g\" ", flam3_variation_names[j], v);
1950 if (j==VAR_BLOB)
1951 blob_var=1;
1952 else if (j==VAR_PDJ)
1953 pdj_var=1;
1954 else if (j==VAR_FAN2)
1955 fan2_var=1;
1956 else if (j==VAR_RINGS2)
1957 rings2_var=1;
1958 else if (j==VAR_PERSPECTIVE)
1959 perspective_var=1;
1960 else if (j==VAR_JULIAN)
1961 juliaN_var=1;
1962 else if (j==VAR_JULIASCOPE)
1963 juliaScope_var=1;
1964 else if (j==VAR_RADIAL_BLUR)
1965 radialBlur_var=1;
1966 else if (j==VAR_PIE)
1967 pie_var=1;
1968 else if (j==VAR_NGON)
1969 ngon_var=1;
1970 else if (j==VAR_CURL)
1971 curl_var=1;
1972 else if (j==VAR_RECTANGLES)
1973 rectangles_var=1;
1974 else if (j==VAR_DISC2)
1975 disc2_var=1;
1976 else if (j==VAR_SUPER_SHAPE)
1977 supershape_var=1;
1978 else if (j==VAR_FLOWER)
1979 flower_var=1;
1980 else if (j==VAR_CONIC)
1981 conic_var=1;
1982 else if (j==VAR_PARABOLA)
1983 parabola_var=1;
1984 else if (j==VAR_BENT2)
1985 bent2_var=1;
1986 else if (j==VAR_BIPOLAR)
1987 bipolar_var=1;
1988 else if (j==VAR_CELL)
1989 cell_var=1;
1990 else if (j==VAR_CPOW)
1991 cpow_var=1;
1992 else if (j==VAR_CURVE)
1993 curve_var=1;
1994 else if (j==VAR_ESCHER)
1995 escher_var=1;
1996 else if (j==VAR_LAZYSUSAN)
1997 lazys_var=1;
1998 else if (j==VAR_MODULUS)
1999 modulus_var=1;
2000 else if (j==VAR_OSCILLOSCOPE)
2001 oscope_var=1;
2002 else if (j==VAR_POPCORN2)
2003 popcorn2_var=1;
2004 else if (j==VAR_SPLIT)
2005 split_var=1;
2006 else if (j==VAR_SPLITS)
2007 splits_var=1;
2008 else if (j==VAR_STRIPES)
2009 stripes_var=1;
2010 else if (j==VAR_WEDGE)
2011 wedge_var=1;
2012 else if (j==VAR_WEDGE_JULIA)
2013 wedgeJ_var=1;
2014 else if (j==VAR_WEDGE_SPH)
2015 wedgeS_var=1;
2016 else if (j==VAR_WHORL)
2017 whorl_var=1;
2018 else if (j==VAR_WAVES2)
2019 waves2_var=1;
2020 else if (j==VAR_AUGER)
2021 auger_var=1;
2022 else if (j==VAR_FLUX)
2023 flux_var=1;
2024 else if (j==VAR_MOBIUS)
2025 mobius_var=1;
2026 }
2027 }
2028
2029 if (!motion_flag) {
2030 if (blob_var==1) {
2031 fprintf(f, "blob_low=\"%g\" ", x->blob_low);
2032 fprintf(f, "blob_high=\"%g\" ", x->blob_high);
2033 fprintf(f, "blob_waves=\"%g\" ", x->blob_waves);
2034 }
2035
2036 if (pdj_var==1) {
2037 fprintf(f, "pdj_a=\"%g\" ", x->pdj_a);
2038 fprintf(f, "pdj_b=\"%g\" ", x->pdj_b);
2039 fprintf(f, "pdj_c=\"%g\" ", x->pdj_c);
2040 fprintf(f, "pdj_d=\"%g\" ", x->pdj_d);
2041 }
2042
2043 if (fan2_var==1) {
2044 fprintf(f, "fan2_x=\"%g\" ", x->fan2_x);
2045 fprintf(f, "fan2_y=\"%g\" ", x->fan2_y);
2046 }
2047
2048 if (rings2_var==1) {
2049 fprintf(f, "rings2_val=\"%g\" ", x->rings2_val);
2050 }
2051
2052 if (perspective_var==1) {
2053 fprintf(f, "perspective_angle=\"%g\" ", x->perspective_angle);
2054 fprintf(f, "perspective_dist=\"%g\" ", x->perspective_dist);
2055 }
2056
2057 if (juliaN_var==1) {
2058 fprintf(f, "julian_power=\"%g\" ", x->julian_power);
2059 fprintf(f, "julian_dist=\"%g\" ", x->julian_dist);
2060 }
2061
2062 if (juliaScope_var==1) {
2063 fprintf(f, "juliascope_power=\"%g\" ", x->juliascope_power);
2064 fprintf(f, "juliascope_dist=\"%g\" ", x->juliascope_dist);
2065 }
2066
2067 if (radialBlur_var==1) {
2068 fprintf(f, "radial_blur_angle=\"%g\" ", x->radial_blur_angle);
2069 }
2070
2071 if (pie_var==1) {
2072 fprintf(f, "pie_slices=\"%g\" ", x->pie_slices);
2073 fprintf(f, "pie_rotation=\"%g\" ", x->pie_rotation);
2074 fprintf(f, "pie_thickness=\"%g\" ", x->pie_thickness);
2075 }
2076
2077 if (ngon_var==1) {
2078 fprintf(f, "ngon_sides=\"%g\" ", x->ngon_sides);
2079 fprintf(f, "ngon_power=\"%g\" ", x->ngon_power);
2080 fprintf(f, "ngon_corners=\"%g\" ", x->ngon_corners);
2081 fprintf(f, "ngon_circle=\"%g\" ", x->ngon_circle);
2082 }
2083
2084 if (curl_var==1) {
2085 fprintf(f, "curl_c1=\"%g\" ", x->curl_c1);
2086 fprintf(f, "curl_c2=\"%g\" ", x->curl_c2);
2087 }
2088
2089 if (rectangles_var==1) {
2090 fprintf(f, "rectangles_x=\"%g\" ", x->rectangles_x);
2091 fprintf(f, "rectangles_y=\"%g\" ", x->rectangles_y);
2092 }
2093
2094 if (disc2_var==1) {
2095 fprintf(f, "disc2_rot=\"%g\" ", x->disc2_rot);
2096 fprintf(f, "disc2_twist=\"%g\" ", x->disc2_twist);
2097 }
2098
2099 if (supershape_var==1) {
2100 fprintf(f, "super_shape_rnd=\"%g\" ", x->super_shape_rnd);
2101 fprintf(f, "super_shape_m=\"%g\" ", x->super_shape_m);
2102 fprintf(f, "super_shape_n1=\"%g\" ", x->super_shape_n1);
2103 fprintf(f, "super_shape_n2=\"%g\" ", x->super_shape_n2);
2104 fprintf(f, "super_shape_n3=\"%g\" ", x->super_shape_n3);
2105 fprintf(f, "super_shape_holes=\"%g\" ", x->super_shape_holes);
2106 }
2107
2108 if (flower_var==1) {
2109 fprintf(f, "flower_petals=\"%g\" ", x->flower_petals);
2110 fprintf(f, "flower_holes=\"%g\" ", x->flower_holes);
2111 }
2112
2113 if (conic_var==1) {
2114 fprintf(f, "conic_eccentricity=\"%g\" ", x->conic_eccentricity);
2115 fprintf(f, "conic_holes=\"%g\" ", x->conic_holes);
2116 }
2117
2118 if (parabola_var==1) {
2119 fprintf(f, "parabola_height=\"%g\" ", x->parabola_height);
2120 fprintf(f, "parabola_width=\"%g\" ", x->parabola_width);
2121 }
2122
2123 if (bent2_var==1) {
2124 fprintf(f, "bent2_x=\"%g\" ", x->bent2_x);
2125 fprintf(f, "bent2_y=\"%g\" ", x->bent2_y);
2126 }
2127
2128 if (bipolar_var==1) {
2129 fprintf(f, "bipolar_shift=\"%g\" ", x->bipolar_shift);
2130 }
2131
2132 if (cell_var==1) {
2133 fprintf(f, "cell_size=\"%g\" ", x->cell_size);
2134 }
2135
2136 if (cpow_var==1) {
2137 fprintf(f, "cpow_i=\"%g\" ", x->cpow_i);
2138 fprintf(f, "cpow_r=\"%g\" ", x->cpow_r);
2139 fprintf(f, "cpow_power=\"%g\" ", x->cpow_power);
2140 }
2141
2142 if (curve_var==1) {
2143 fprintf(f, "curve_xamp=\"%g\" ", x->curve_xamp);
2144 fprintf(f, "curve_yamp=\"%g\" ", x->curve_yamp);
2145 fprintf(f, "curve_xlength=\"%g\" ", x->curve_xlength);
2146 fprintf(f, "curve_ylength=\"%g\" ", x->curve_ylength);
2147 }
2148
2149 if (escher_var==1) {
2150 fprintf(f, "escher_beta=\"%g\" ", x->escher_beta);
2151 }
2152
2153 if (lazys_var==1) {
2154 fprintf(f, "lazysusan_x=\"%g\" ", x->lazysusan_x);
2155 fprintf(f, "lazysusan_y=\"%g\" ", x->lazysusan_y);
2156 fprintf(f, "lazysusan_spin=\"%g\" ", x->lazysusan_spin);
2157 fprintf(f, "lazysusan_space=\"%g\" ", x->lazysusan_space);
2158 fprintf(f, "lazysusan_twist=\"%g\" ", x->lazysusan_twist);
2159 }
2160
2161 if (modulus_var==1) {
2162 fprintf(f, "modulus_x=\"%g\" ", x->modulus_x);
2163 fprintf(f, "modulus_y=\"%g\" ", x->modulus_y);
2164 }
2165
2166 if (oscope_var==1) {
2167 fprintf(f, "oscilloscope_separation=\"%g\" ", x->oscope_separation);
2168 fprintf(f, "oscilloscope_frequency=\"%g\" ", x->oscope_frequency);
2169 fprintf(f, "oscilloscope_amplitude=\"%g\" ", x->oscope_amplitude);
2170 fprintf(f, "oscilloscope_damping=\"%g\" ", x->oscope_damping);
2171 }
2172
2173 if (popcorn2_var==1) {
2174 fprintf(f, "popcorn2_x=\"%g\" ", x->popcorn2_x);
2175 fprintf(f, "popcorn2_y=\"%g\" ", x->popcorn2_y);
2176 fprintf(f, "popcorn2_c=\"%g\" ", x->popcorn2_c);
2177 }
2178
2179 if (separation_var==1) {
2180 fprintf(f, "separation_x=\"%g\" ", x->separation_x);
2181 fprintf(f, "separation_y=\"%g\" ", x->separation_y);
2182 fprintf(f, "separation_xinside=\"%g\" ", x->separation_xinside);
2183 fprintf(f, "separation_yinside=\"%g\" ", x->separation_yinside);
2184 }
2185
2186 if (split_var==1) {
2187 fprintf(f, "split_xsize=\"%g\" ", x->split_xsize);
2188 fprintf(f, "split_ysize=\"%g\" ", x->split_ysize);
2189 }
2190
2191 if (splits_var==1) {
2192 fprintf(f, "splits_x=\"%g\" ", x->splits_x);
2193 fprintf(f, "splits_y=\"%g\" ", x->splits_y);
2194 }
2195
2196 if (stripes_var==1) {
2197 fprintf(f, "stripes_space=\"%g\" ", x->stripes_space);
2198 fprintf(f, "stripes_warp=\"%g\" ", x->stripes_warp);
2199 }
2200
2201 if (wedge_var==1) {
2202 fprintf(f, "wedge_angle=\"%g\" ", x->wedge_angle);
2203 fprintf(f, "wedge_hole=\"%g\" ", x->wedge_hole);
2204 fprintf(f, "wedge_count=\"%g\" ", x->wedge_count);
2205 fprintf(f, "wedge_swirl=\"%g\" ", x->wedge_swirl);
2206 }
2207
2208 if (wedgeJ_var==1) {
2209 fprintf(f, "wedge_julia_angle=\"%g\" ", x->wedge_julia_angle);
2210 fprintf(f, "wedge_julia_count=\"%g\" ", x->wedge_julia_count);
2211 fprintf(f, "wedge_julia_power=\"%g\" ", x->wedge_julia_power);
2212 fprintf(f, "wedge_julia_dist=\"%g\" ", x->wedge_julia_dist);
2213 }
2214
2215 if (wedgeS_var==1) {
2216 fprintf(f, "wedge_sph_angle=\"%g\" ", x->wedge_sph_angle);
2217 fprintf(f, "wedge_sph_hole=\"%g\" ", x->wedge_sph_hole);
2218 fprintf(f, "wedge_sph_count=\"%g\" ", x->wedge_sph_count);
2219 fprintf(f, "wedge_sph_swirl=\"%g\" ", x->wedge_sph_swirl);
2220 }
2221
2222 if (whorl_var==1) {
2223 fprintf(f, "whorl_inside=\"%g\" ", x->whorl_inside);
2224 fprintf(f, "whorl_outside=\"%g\" ", x->whorl_outside);
2225 }
2226
2227 if (waves2_var==1) {
2228 fprintf(f, "waves2_scalex=\"%g\" ", x->waves2_scalex);
2229 fprintf(f, "waves2_scaley=\"%g\" ", x->waves2_scaley);
2230 fprintf(f, "waves2_freqx=\"%g\" ", x->waves2_freqx);
2231 fprintf(f, "waves2_freqy=\"%g\" ", x->waves2_freqy);
2232 }
2233
2234 if (auger_var==1) {
2235 fprintf(f, "auger_sym=\"%g\" ", x->auger_sym);
2236 fprintf(f, "auger_weight=\"%g\" ", x->auger_weight);
2237 fprintf(f, "auger_freq=\"%g\" ", x->auger_freq);
2238 fprintf(f, "auger_scale=\"%g\" ", x->auger_scale);
2239 }
2240
2241 if (flux_var==1)
2242 fprintf(f, "flux_spread=\"%g\" ", x->flux_spread);
2243
2244 if (mobius_var==1) {
2245 fprintf(f, "mobius_re_a=\"%g\" ", x->mobius_re_a);
2246 fprintf(f, "mobius_im_a=\"%g\" ", x->mobius_im_a);
2247 fprintf(f, "mobius_re_b=\"%g\" ", x->mobius_re_b);
2248 fprintf(f, "mobius_im_b=\"%g\" ", x->mobius_im_b);
2249 fprintf(f, "mobius_re_c=\"%g\" ", x->mobius_re_c);
2250 fprintf(f, "mobius_im_c=\"%g\" ", x->mobius_im_c);
2251 fprintf(f, "mobius_re_d=\"%g\" ", x->mobius_re_d);
2252 fprintf(f, "mobius_im_d=\"%g\" ", x->mobius_im_d);
2253 }
2254
2255 fprintf(f, "coefs=\"");
2256 for (j = 0; j < 3; j++) {
2257 if (j) fprintf(f, " ");
2258 fprintf(f, "%g %g", x->c[j][0], x->c[j][1]);
2259 }
2260 fprintf(f, "\"");
2261
2262 if (!id_matrix(x->post)) {
2263 fprintf(f, " post=\"");
2264 for (j = 0; j < 3; j++) {
2265 if (j) fprintf(f, " ");
2266 fprintf(f, "%g %g", x->post[j][0], x->post[j][1]);
2267 }
2268 fprintf(f, "\"");
2269 }
2270
2271
2272 } else {
2273 /* For motion, print any parameter if it's nonzero */
2274 PRINTNON(blob_low);
2275 PRINTNON(blob_high);
2276 PRINTNON(blob_waves);
2277
2278 PRINTNON(pdj_a);
2279 PRINTNON(pdj_b);
2280 PRINTNON(pdj_c);
2281 PRINTNON(pdj_d);
2282
2283 PRINTNON(fan2_x);
2284 PRINTNON(fan2_y);
2285
2286 PRINTNON(rings2_val);
2287
2288 PRINTNON(perspective_angle);
2289 PRINTNON(perspective_dist);
2290
2291 PRINTNON(julian_power);
2292 PRINTNON(julian_dist);
2293
2294 PRINTNON(juliascope_power);
2295 PRINTNON(juliascope_dist);
2296
2297 PRINTNON(radial_blur_angle);
2298
2299 PRINTNON(pie_slices);
2300 PRINTNON(pie_rotation);
2301 PRINTNON(pie_thickness);
2302
2303 PRINTNON(ngon_sides);
2304 PRINTNON(ngon_power);
2305 PRINTNON(ngon_corners);
2306 PRINTNON(ngon_circle);
2307
2308 PRINTNON(curl_c1);
2309 PRINTNON(curl_c2);
2310
2311 PRINTNON(rectangles_x);
2312 PRINTNON(rectangles_y);
2313
2314 PRINTNON(disc2_rot);
2315 PRINTNON(disc2_twist);
2316
2317 PRINTNON(super_shape_rnd);
2318 PRINTNON(super_shape_m);
2319 PRINTNON(super_shape_n1);
2320 PRINTNON(super_shape_n2);
2321 PRINTNON(super_shape_n3);
2322 PRINTNON(super_shape_holes);
2323
2324 PRINTNON(flower_petals);
2325 PRINTNON(flower_holes);
2326
2327 PRINTNON(conic_eccentricity);
2328 PRINTNON(conic_holes);
2329
2330 PRINTNON(parabola_height);
2331 PRINTNON(parabola_width);
2332
2333 PRINTNON(bent2_x);
2334 PRINTNON(bent2_y);
2335
2336 PRINTNON(bipolar_shift);
2337
2338 PRINTNON(cell_size);
2339
2340 PRINTNON(cpow_i);
2341 PRINTNON(cpow_r);
2342 PRINTNON(cpow_power);
2343
2344 PRINTNON(curve_xamp);
2345 PRINTNON(curve_yamp);
2346 PRINTNON(curve_xlength);
2347 PRINTNON(curve_ylength);
2348
2349 PRINTNON(escher_beta);
2350
2351 PRINTNON(lazysusan_x);
2352 PRINTNON(lazysusan_y);
2353 PRINTNON(lazysusan_spin);
2354 PRINTNON(lazysusan_space);
2355 PRINTNON(lazysusan_twist);
2356
2357 PRINTNON(modulus_x);
2358 PRINTNON(modulus_y);
2359
2360 PRINTNON(oscope_separation);
2361 PRINTNON(oscope_frequency);
2362 PRINTNON(oscope_amplitude);
2363 PRINTNON(oscope_damping);
2364
2365 PRINTNON(popcorn2_x);
2366 PRINTNON(popcorn2_y);
2367 PRINTNON(popcorn2_c);
2368
2369 PRINTNON(separation_x);
2370 PRINTNON(separation_y);
2371 PRINTNON(separation_xinside);
2372 PRINTNON(separation_yinside);
2373
2374 PRINTNON(split_xsize);
2375 PRINTNON(split_ysize);
2376
2377 PRINTNON(splits_x);
2378 PRINTNON(splits_y);
2379
2380 PRINTNON(stripes_space);
2381 PRINTNON(stripes_warp);
2382
2383 PRINTNON(wedge_angle);
2384 PRINTNON(wedge_hole);
2385 PRINTNON(wedge_count);
2386 PRINTNON(wedge_swirl);
2387
2388 PRINTNON(wedge_julia_angle);
2389 PRINTNON(wedge_julia_count);
2390 PRINTNON(wedge_julia_power);
2391 PRINTNON(wedge_julia_dist);
2392
2393 PRINTNON(wedge_sph_angle);
2394 PRINTNON(wedge_sph_hole);
2395 PRINTNON(wedge_sph_count);
2396 PRINTNON(wedge_sph_swirl);
2397
2398 PRINTNON(whorl_inside);
2399 PRINTNON(whorl_outside);
2400
2401 PRINTNON(waves2_scalex);
2402 PRINTNON(waves2_scaley);
2403 PRINTNON(waves2_freqx);
2404 PRINTNON(waves2_freqy);
2405
2406 PRINTNON(auger_sym);
2407 PRINTNON(auger_weight);
2408 PRINTNON(auger_freq);
2409 PRINTNON(auger_scale);
2410
2411 PRINTNON(flux_spread);
2412
2413 PRINTNON(mobius_re_a);
2414 PRINTNON(mobius_im_a);
2415 PRINTNON(mobius_re_b);
2416 PRINTNON(mobius_im_b);
2417 PRINTNON(mobius_re_c);
2418 PRINTNON(mobius_im_c);
2419 PRINTNON(mobius_re_d);
2420 PRINTNON(mobius_im_d);
2421
2422 if (!zero_matrix(x->c)) {
2423 fprintf(f, "coefs=\"");
2424 for (j = 0; j < 3; j++) {
2425 if (j) fprintf(f, " ");
2426 fprintf(f, "%g %g", x->c[j][0], x->c[j][1]);
2427 }
2428 fprintf(f, "\"");
2429 }
2430
2431 if (!zero_matrix(x->post)) {
2432 fprintf(f, " post=\"");
2433 for (j = 0; j < 3; j++) {
2434 if (j) fprintf(f, " ");
2435 fprintf(f, "%g %g", x->post[j][0], x->post[j][1]);
2436 }
2437 fprintf(f, "\"");
2438 }
2439 }
2440
2441 if (!final_flag && !motion_flag && !flam27_flag) {
2442
2443 /* Print out the chaos row for this xform */
2444 int numcols = numstd;
2445
2446 while (numcols > 0 && chaos_row[numcols-1]==1.0)
2447 numcols--;
2448
2449 if (numcols>0) {
2450 fprintf(f, " chaos=\"");
2451 for (j=0;j<numcols;j++)
2452 fprintf(f, "%g ",chaos_row[j]);
2453 fprintf(f, "\"");
2454 }
2455
2456
2457 }
2458
2459 if (!flam27_flag && !motion_flag) {
2460 fprintf(f, " opacity=\"%g\"",x->opacity);
2461 }
2462
2463 if (!motion_flag && x->num_motion>0 && !flam27_flag) {
2464 int nm;
2465
2466 fprintf(f,">\n");
2467
2468 for (nm=0; nm<x->num_motion; nm++)
2469 flam3_print_xform(f, &(x->motion[nm]), 0, 0, NULL, 1);
2470
2471 if (final_flag)
2472 fprintf(f," </finalxform>\n");
2473 else
2474 fprintf(f," </xform>\n");
2475
2476 } else
2477 fprintf(f, "/>\n");
2478 }
2479
2480
2481 /* returns a uniform variable from 0 to 1 */
2482 double flam3_random01() {
2483 return (random() & 0xfffffff) / (double) 0xfffffff;
2484 }
2485
2486 double flam3_random11() {
2487 return ((random() & 0xfffffff) - 0x7ffffff) / (double) 0x7ffffff;
2488 }
2489
2490 /* This function must be called prior to rendering a frame */
2491 void flam3_init_frame(flam3_frame *f) {
2492
2493 char *ai;
2494 char *isaac_seed = args("isaac_seed",NULL);
2495 long int default_isaac_seed = (long int)time(0);
2496
2497 /* Clear out the isaac state */
2498 memset(f->rc.randrsl, 0, RANDSIZ*sizeof(ub4));
2499
2500 /* Set the isaac seed */
2501 if (NULL == isaac_seed) {
2502 int lp;
2503 /* No isaac seed specified. Use the system time to initialize. */
2504 for (lp = 0; lp < RANDSIZ; lp++)
2505 f->rc.randrsl[lp] = default_isaac_seed;
2506 } else {
2507 /* Use the specified string */
2508 strncpy((char *)&f->rc.randrsl,(const char *)isaac_seed, RANDSIZ*sizeof(ub4));
2509 }
2510
2511 /* Initialize the random number generator */
2512 irandinit(&f->rc,1);
2513 }
2514
2515 /* returns uniform variable from ISAAC rng */
2516 double flam3_random_isaac_01(randctx *ct) {
2517 return ((int)irand(ct) & 0xfffffff) / (double) 0xfffffff;
2518 }
2519
2520 double flam3_random_isaac_11(randctx *ct) {
2521 return (((int)irand(ct) & 0xfffffff) - 0x7ffffff) / (double) 0x7ffffff;
2522 }
2523
2524 int flam3_random_bit() {
2525 /* might not be threadsafe */
2526 static int n = 0;
2527 static int l;
2528 if (0 == n) {
2529 l = random();
2530 n = 20;
2531 } else {
2532 l = l >> 1;
2533 n--;
2534 }
2535 return l & 1;
2536 }
2537
2538 int flam3_random_isaac_bit(randctx *ct) {
2539 int tmp = irand(ct);
2540 return tmp & 1;
2541 }
2542
2543 static double round6(double x) {
2544 x *= 1e6;
2545 if (x < 0) x -= 1.0;
2546 return 1e-6*(int)(x+0.5);
2547 }
2548
2549 /* sym=2 or more means rotational
2550 sym=1 means identity, ie no symmetry
2551 sym=0 means pick a random symmetry (maybe none)
2552 sym=-1 means bilateral (reflection)
2553 sym=-2 or less means rotational and reflective
2554 */
2555 void flam3_add_symmetry(flam3_genome *cp, int sym) {
2556 int i, j, k;
2557 double a;
2558 int result = 0;
2559
2560 if (0 == sym) {
2561 static int sym_distrib[] = {
2562 -4, -3,
2563 -2, -2, -2,
2564 -1, -1, -1,
2565 2, 2, 2,
2566 3, 3,
2567 4, 4,
2568 };
2569 if (random()&1) {
2570 sym = random_distrib(sym_distrib);
2571 } else if (random()&31) {
2572 sym = (random()%13)-6;
2573 } else {
2574 sym = (random()%51)-25;
2575 }
2576 }
2577
2578 if (1 == sym || 0 == sym) return;
2579
2580 cp->symmetry = sym;
2581
2582 if (sym < 0) {
2583
2584 i = cp->num_xforms;
2585 if (cp->final_xform_enable)
2586 i -= 1;
2587
2588 flam3_add_xforms(cp,1,0,0);
2589
2590 cp->xform[i].density = 1.0;
2591 cp->xform[i].color_speed = 0.0;
2592 cp->xform[i].animate = 0.0;
2593 cp->xform[i].var[0] = 1.0;
2594 for (j = 1; j < flam3_nvariations; j++)
2595 cp->xform[i].var[j] = 0;
2596 cp->xform[i].color = 1.0;
2597 cp->xform[i].c[0][0] = -1.0;
2598 cp->xform[i].c[0][1] = 0.0;
2599 cp->xform[i].c[1][0] = 0.0;
2600 cp->xform[i].c[1][1] = 1.0;
2601 cp->xform[i].c[2][0] = 0.0;
2602 cp->xform[i].c[2][1] = 0.0;
2603
2604 result++;
2605 sym = -sym;
2606 }
2607
2608 a = 2*M_PI/sym;
2609
2610 for (k = 1; k < sym; k++) {
2611
2612 i = cp->num_xforms;
2613 if (cp->final_xform_enable)
2614 i -= 1;
2615
2616 flam3_add_xforms(cp, 1, 0,0);
2617
2618 cp->xform[i].density = 1.0;
2619 cp->xform[i].color_speed = 0.0;
2620 cp->xform[i].animate = 0.0;
2621 cp->xform[i].var[0] = 1.0;
2622 for (j = 1; j < flam3_nvariations; j++)
2623 cp->xform[i].var[j] = 0;
2624 cp->xform[i].color = (sym<3) ? 0.0 : ((k-1.0)/(sym-2.0));
2625 cp->xform[i].c[0][0] = round6(cos(k*a));
2626 cp->xform[i].c[0][1] = round6(sin(k*a));
2627 cp->xform[i].c[1][0] = round6(-cp->xform[i].c[0][1]);
2628 cp->xform[i].c[1][1] = cp->xform[i].c[0][0];
2629 cp->xform[i].c[2][0] = 0.0;
2630 cp->xform[i].c[2][1] = 0.0;
2631
2632 result++;
2633 }
2634
2635 qsort((char *) &cp->xform[cp->num_xforms-result], result,
2636 sizeof(flam3_xform), compare_xforms);
2637
2638 }
2639
2640 void add_to_action(char *action, char *addtoaction) {
2641
2642 /* action is a flam3_max_action_length array */
2643 if (action != NULL) {
2644
2645 int alen = strlen(action);
2646 int addlen = strlen(addtoaction);
2647
2648 if (alen+addlen < flam3_max_action_length)
2649 strcat(action,addtoaction);
2650 else
2651 fprintf(stderr,"action string too long, truncating...\n");
2652 }
2653 }
2654
2655
2656 void flam3_cross(flam3_genome *cp0, flam3_genome *cp1, flam3_genome *out, int cross_mode, randctx *rc, char *action) {
2657
2658 int i,j, rb;
2659 char ministr[10];
2660
2661 if (cross_mode == CROSS_NOT_SPECIFIED) {
2662
2663 double s = flam3_random_isaac_01(rc);
2664
2665 if (s < 0.1)
2666 cross_mode = CROSS_UNION;
2667 else if (s < 0.2)
2668 cross_mode = CROSS_INTERPOLATE;
2669 else
2670 cross_mode = CROSS_ALTERNATE;
2671
2672 }
2673
2674 if (cross_mode == CROSS_UNION) {
2675
2676 flam3_xform mycopy;
2677
2678 /* Make a copy of cp0 */
2679 flam3_copy(out, cp0);
2680
2681 for (j=0;j<cp1->num_xforms;j++) {
2682 /* Skip over the final xform, if it's present. */
2683 /* Default behavior keeps the final from parent0. */
2684 if (cp1->final_xform_index == j)
2685 continue;
2686
2687 i = out->num_xforms;
2688 if (out->final_xform_enable)
2689 i -= 1;
2690
2691 flam3_add_xforms(out, 1, 0, 0);
2692 flam3_copy_xform(&out->xform[i],&cp1->xform[j]);
2693 }
2694
2695 /* Put the final xform last (if there is one) */
2696 /* We do not need to do complicated xform copies here since we're just moving them around */
2697 if (out->final_xform_index >= 0) {
2698 mycopy = out->xform[out->final_xform_index];
2699 out->xform[out->final_xform_index] = out->xform[out->num_xforms-1];
2700 out->xform[out->num_xforms-1] = mycopy;
2701 out->final_xform_index = out->num_xforms-1;
2702 }
2703
2704 add_to_action(action,"cross union");
2705
2706 } else if (cross_mode == CROSS_INTERPOLATE) {
2707
2708 /* linearly interpolate somewhere between the two */
2709 flam3_genome parents[2];
2710 double t = flam3_random_isaac_01(rc);
2711
2712 memset(parents, 0, 2*sizeof(flam3_genome));
2713
2714 flam3_copy(&(parents[0]), cp0);
2715 flam3_copy(&(parents[1]), cp1);
2716
2717 parents[0].time = 0.0;
2718 parents[1].time = 1.0;
2719 flam3_interpolate(parents, 2, t, 0, out);
2720
2721 for (i=0;i<out->num_xforms;i++)
2722 flam3_delete_motion_elements(&out->xform[i]);
2723
2724 clear_cp(&parents[0],flam3_defaults_on);
2725 clear_cp(&parents[1],flam3_defaults_on);
2726
2727 sprintf(ministr,"%7.5g",t);
2728
2729 add_to_action(action,"cross interpolate ");
2730 add_to_action(action,ministr);
2731
2732 } else {
2733
2734 /* alternate mode */
2735 int got0, got1, used_parent;
2736 char *trystr;
2737
2738 trystr = calloc(4 * (cp0->num_xforms + cp1->num_xforms), sizeof(char));
2739
2740 /* each xform comes from a random parent, possible for an entire parent to be excluded */
2741 do {
2742
2743 trystr[0] = 0;
2744 got0 = got1 = 0;
2745 rb = flam3_random_isaac_bit(rc);
2746 sprintf(ministr,"%d:",rb);
2747 strcat(trystr,ministr);
2748
2749 /* Copy the parent, sorting the final xform to the end if it's present. */
2750 if (rb)
2751 flam3_copyx(out, cp1, cp1->num_xforms - (cp1->final_xform_index > 0), cp1->final_xform_enable);
2752 else
2753 flam3_copyx(out, cp0, cp0->num_xforms - (cp0->final_xform_index > 0), cp0->final_xform_enable);
2754
2755 used_parent = rb;
2756
2757 /* Only replace non-final xforms */
2758
2759 for (i = 0; i < out->num_xforms - out->final_xform_enable; i++) {
2760 rb = flam3_random_isaac_bit(rc);
2761
2762 /* Replace xform if bit is 1 */
2763 if (rb==1) {
2764 if (used_parent==0) {
2765 if (i < cp1->num_xforms && cp1->xform[i].density > 0) {
2766 flam3_copy_xform(&out->xform[i],&cp1->xform[i]);
2767 sprintf(ministr," 1");
2768 got1 = 1;
2769 } else {
2770 sprintf(ministr," 0");
2771 got0 = 1;
2772 }
2773 } else {
2774 if (i < cp0->num_xforms && cp0->xform[i].density > 0) {
2775 flam3_copy_xform(&out->xform[i],&cp0->xform[i]);
2776 sprintf(ministr," 0");
2777 got0 = 1;
2778 } else {
2779 sprintf(ministr," 1");
2780 got1 = 1;
2781 }
2782 }
2783 } else {
2784 sprintf(ministr," %d",used_parent);
2785 if (used_parent)
2786 got1 = 1;
2787 else
2788 got0 = 1;
2789 }
2790
2791 strcat(trystr,ministr);
2792 }
2793
2794 if (used_parent==0 && cp0->final_xform_enable)
2795 got0 = 1;
2796 else if (used_parent==1 && cp1->final_xform_enable)
2797 got1 = 1;
2798
2799 } while ((i > 1) && !(got0 && got1));
2800
2801 add_to_action(action,"cross alternate ");
2802 add_to_action(action,trystr);
2803
2804 free(trystr);
2805 }
2806
2807 /* reset color coords */
2808 for (i = 0; i < out->num_xforms; i++) {
2809 out->xform[i].color = i&1;
2810 }
2811
2812 /* Potentially genetically cross the two colormaps together */
2813 if (flam3_random_isaac_01(rc) < 0.4) {
2814
2815 /* Select the starting parent */
2816 int startParent=flam3_random_isaac_bit(rc);
2817 int ci;
2818
2819 add_to_action(action," cmap_cross");
2820 sprintf(ministr," %d:",startParent);
2821 add_to_action(action,ministr);
2822
2823 /* Loop over the entries, switching to the other parent 1% of the time */
2824 for (ci=0;ci<256;ci++) {
2825 if (flam3_random_isaac_01(rc)<.01) {
2826 startParent = 1-startParent;
2827 sprintf(ministr," %d",ci);
2828 add_to_action(action,ministr);
2829 }
2830
2831 out->palette[ci] = startParent ? cp1->palette[ci]: cp0->palette[ci];
2832 }
2833 }
2834
2835 }
2836
2837 void flam3_mutate(flam3_genome *cp, int mutate_mode, int *ivars, int ivars_n, int sym, double speed, randctx *rc, char *action) {
2838
2839 double randselect;
2840 flam3_genome mutation;
2841 int i,j,done;
2842 char ministr[30];
2843
2844 /* If mutate_mode = -1, choose a random mutation mode */
2845 if (mutate_mode == MUTATE_NOT_SPECIFIED) {
2846
2847 randselect = flam3_random_isaac_01(rc);
2848
2849 if (randselect < 0.1)
2850 mutate_mode = MUTATE_ALL_VARIATIONS;
2851 else if (randselect < 0.3)
2852 mutate_mode = MUTATE_ONE_XFORM_COEFS;
2853 else if (randselect < 0.5)
2854 mutate_mode = MUTATE_ADD_SYMMETRY;
2855 else if (randselect < 0.6)
2856 mutate_mode = MUTATE_POST_XFORMS;
2857 else if (randselect < 0.7)
2858 mutate_mode = MUTATE_COLOR_PALETTE;
2859 else if (randselect < 0.8)
2860 mutate_mode = MUTATE_DELETE_XFORM;
2861 else
2862 mutate_mode = MUTATE_ALL_COEFS;
2863
2864 }
2865
2866 memset(&mutation, 0, sizeof(flam3_genome));
2867
2868 if (mutate_mode == MUTATE_ALL_VARIATIONS) {
2869
2870 add_to_action(action,"mutate all variations");
2871
2872 do {
2873 /* Create a random flame, and use the variations */
2874 /* to replace those in the original */
2875 flam3_random(&mutation, ivars, ivars_n, sym, cp->num_xforms);
2876 for (i = 0; i < cp->num_xforms; i++) {
2877 for (j = 0; j < flam3_nvariations; j++) {
2878 if (cp->xform[i].var[j] != mutation.xform[i].var[j]) {
2879
2880 /* Copy the new var weights */
2881 cp->xform[i].var[j] = mutation.xform[i].var[j];
2882
2883 /* Copy parameters for this variation only */
2884 flam3_copy_params(&(cp->xform[i]),&(mutation.xform[i]),j);
2885
2886 done = 1;
2887 }
2888 }
2889 }
2890 } while (!done);
2891
2892 } else if (mutate_mode == MUTATE_ONE_XFORM_COEFS) {
2893
2894 int modxf;
2895
2896 /* Generate a 2-xform random */
2897 flam3_random(&mutation, ivars, ivars_n, sym, 2);
2898
2899 /* Which xform do we mutate? */
2900 modxf = ((unsigned)irand(rc)) % cp->num_xforms;
2901
2902 add_to_action(action,"mutate xform ");
2903 sprintf(ministr,"%d coefs",modxf);
2904 add_to_action(action,ministr);
2905
2906 /* if less than 3 xforms, then change only the translation part */
2907 if (2 >= cp->num_xforms) {
2908 for (j = 0; j < 2; j++)
2909 cp->xform[modxf].c[2][j] = mutation.xform[0].c[2][j];
2910 } else {
2911 for (i = 0; i < 3; i++)
2912 for (j = 0; j < 2; j++)
2913 cp->xform[modxf].c[i][j] = mutation.xform[0].c[i][j];
2914 }
2915
2916 } else if (mutate_mode == MUTATE_ADD_SYMMETRY) {
2917
2918 add_to_action(action,"mutate symmetry");
2919 flam3_add_symmetry(cp, 0);
2920
2921 } else if (mutate_mode == MUTATE_POST_XFORMS) {
2922
2923 int b = 1 + ((unsigned)irand(rc))%6;
2924 int same = ((unsigned)irand(rc))&3; /* 25% chance of using the same post for all of them */
2925
2926 sprintf(ministr,"(%d%s)",b,(same>0) ? " same" : "");
2927 add_to_action(action,"mutate post xforms ");
2928 add_to_action(action,ministr);
2929 for (i = 0; i < cp->num_xforms; i++) {
2930 int copy = (i > 0) && same;
2931
2932 if (copy) { /* Copy the post from the first xform to the rest of them */
2933 for (j = 0; j < 3; j++) {
2934 cp->xform[i].post[j][0] = cp->xform[0].post[j][0];
2935 cp->xform[i].post[j][1] = cp->xform[0].post[j][1];
2936 }
2937
2938 } else {
2939
2940 if (b&1) { /* 50% chance */
2941
2942 double f = M_PI * flam3_random_isaac_11(rc);
2943 double t[2][2];
2944
2945 t[0][0] = (cp->xform[i].c[0][0] * cos(f) + cp->xform[i].c[0][1] * -sin(f));
2946 t[0][1] = (cp->xform[i].c[0][0] * sin(f) + cp->xform[i].c[0][1] * cos(f));
2947 t[1][0] = (cp->xform[i].c[1][0] * cos(f) + cp->xform[i].c[1][1] * -sin(f));
2948 t[1][1] = (cp->xform[i].c[1][0] * sin(f) + cp->xform[i].c[1][1] * cos(f));
2949
2950 cp->xform[i].c[0][0] = t[0][0];
2951 cp->xform[i].c[0][1] = t[0][1];
2952 cp->xform[i].c[1][0] = t[1][0];
2953 cp->xform[i].c[1][1] = t[1][1];
2954
2955 f *= -1.0;
2956
2957 t[0][0] = (cp->xform[i].post[0][0] * cos(f) + cp->xform[i].post[0][1] * -sin(f));
2958 t[0][1] = (cp->xform[i].post[0][0] * sin(f) + cp->xform[i].post[0][1] * cos(f));
2959 t[1][0] = (cp->xform[i].post[1][0] * cos(f) + cp->xform[i].post[1][1] * -sin(f));
2960 t[1][1] = (cp->xform[i].post[1][0] * sin(f) + cp->xform[i].post[1][1] * cos(f));
2961
2962 cp->xform[i].post[0][0] = t[0][0];
2963 cp->xform[i].post[0][1] = t[0][1];
2964 cp->xform[i].post[1][0] = t[1][0];
2965 cp->xform[i].post[1][1] = t[1][1];
2966
2967 }
2968
2969 if (b&2) { /* 33% chance */
2970
2971 double f = 0.2 + flam3_random_isaac_01(rc);
2972 double g = 0.2 + flam3_random_isaac_01(rc);
2973
2974 if (flam3_random_isaac_bit(rc))
2975 f = 1.0 / f;
2976
2977 if (flam3_random_isaac_bit(rc))
2978 g = f;
2979 else {
2980 if (flam3_random_isaac_bit(rc))
2981 g = 1.0 / g;
2982 }
2983
2984 cp->xform[i].c[0][0] /= f;
2985 cp->xform[i].c[0][1] /= f;
2986 cp->xform[i].c[1][1] /= g;
2987 cp->xform[i].c[1][0] /= g;
2988 cp->xform[i].post[0][0] *= f;
2989 cp->xform[i].post[1][0] *= f;
2990 cp->xform[i].post[0][1] *= g;
2991 cp->xform[i].post[1][1] *= g;
2992 }
2993
2994 if (b&4) { /* 16% chance */
2995
2996 double f = flam3_random_isaac_11(rc);
2997 double g = flam3_random_isaac_11(rc);
2998
2999 cp->xform[i].c[2][0] -= f;
3000 cp->xform[i].c[2][1] -= g;
3001 cp->xform[i].post[2][0] += f;
3002 cp->xform[i].post[2][1] += g;
3003 }
3004 }
3005 }
3006 } else if (mutate_mode == MUTATE_COLOR_PALETTE) {
3007
3008 double s = flam3_random_isaac_01(rc);
3009
3010 if (s < 0.4) { /* randomize xform color coords */
3011
3012 flam3_improve_colors(cp, 100, 0, 10);
3013 add_to_action(action,"mutate color coords");
3014
3015 } else if (s < 0.8) { /* randomize xform color coords and palette */
3016
3017 flam3_improve_colors(cp, 25, 1, 10);
3018 add_to_action(action,"mutate color all");
3019
3020 } else { /* randomize palette only */
3021
3022 cp->palette_index = flam3_get_palette(flam3_palette_random, cp->palette, cp->hue_rotation);
3023 /* if our palette retrieval fails, skip the mutation */
3024 if (cp->palette_index >= 0)
3025 add_to_action(action,"mutate color palette");
3026 else
3027 fprintf(stderr,"failure getting random palette, palette set to white\n");
3028
3029 }
3030 } else if (mutate_mode == MUTATE_DELETE_XFORM) {
3031
3032 int nx = ((unsigned)irand(rc))%cp->num_xforms;
3033 sprintf(ministr,"%d",nx);
3034 add_to_action(action,"mutate delete xform ");
3035 add_to_action(action,ministr);
3036
3037 if (cp->num_xforms > 1)
3038 flam3_delete_xform(cp,nx);
3039
3040 } else { /* MUTATE_ALL_COEFS */
3041
3042 int x;
3043 add_to_action(action,"mutate all coefs");
3044 flam3_random(&mutation, ivars, ivars_n, sym, cp->num_xforms);
3045
3046 /* change all the coefs by a fraction of the random */
3047 for (x = 0; x < cp->num_xforms; x++) {
3048 for (i = 0; i < 3; i++) {
3049 for (j = 0; j < 2; j++) {
3050 cp->xform[x].c[i][j] += speed * mutation.xform[x].c[i][j];
3051
3052 }
3053 }
3054 /* Eventually, we can mutate the parametric variation coefs here. */
3055 }
3056 }
3057
3058 clear_cp(&mutation,flam3_defaults_on);
3059
3060 }
3061
3062 static int random_var() {
3063 return random() % flam3_nvariations;
3064 }
3065
3066 static int random_varn(int n) {
3067 return random() % n;
3068 }
3069
3070 void flam3_random(flam3_genome *cp, int *ivars, int ivars_n, int sym, int spec_xforms) {
3071
3072 int i, nxforms, var, samed, multid, samepost, postid, addfinal=0;
3073 int finum = -1;
3074 int n;
3075 char *ai;
3076 int f27 = argi("flam27",0);
3077 int mvar = f27 ? 54 : flam3_nvariations;
3078 double sum;
3079
3080 static int xform_distrib[] = {
3081 2, 2, 2, 2,
3082 3, 3, 3, 3,
3083 4, 4, 4,
3084 5, 5,
3085 6
3086 };
3087
3088 clear_cp(cp,flam3_defaults_on);
3089
3090 cp->hue_rotation = (random()&7) ? 0.0 : flam3_random01();
3091 cp->palette_index = flam3_get_palette(flam3_palette_random, cp->palette, cp->hue_rotation);
3092 if (cp->palette_index < 0)
3093 fprintf(stderr,"error getting palette from xml file, setting to all white\n");
3094 cp->time = 0.0;
3095 cp->interpolation = flam3_interpolation_linear;
3096 cp->palette_interpolation = flam3_palette_interpolation_hsv_circular;
3097 cp->hsv_rgb_palette_blend = 0.0;
3098
3099 /* Choose the number of xforms */
3100 if (spec_xforms>0) {
3101 nxforms = spec_xforms;
3102 flam3_add_xforms(cp,nxforms,0,0);
3103 } else {
3104 nxforms = random_distrib(xform_distrib);
3105 flam3_add_xforms(cp,nxforms,0,0);
3106 /* Add a final xform 15% of the time */
3107 addfinal = flam3_random01() < 0.15;
3108 if (addfinal) {
3109 flam3_add_xforms(cp,1,0,1);
3110 nxforms = nxforms + addfinal;
3111 finum = nxforms-1;
3112 }
3113 }
3114
3115 /* If first input variation is 'flam3_variation_random' */
3116 /* choose one to use or decide to use multiple */
3117 if (flam3_variation_random == ivars[0]) {
3118 if (flam3_random_bit()) {
3119 var = random_varn(mvar);
3120 } else {
3121 var = flam3_variation_random;
3122 }
3123 } else {
3124 var = flam3_variation_random_fromspecified;
3125 }
3126
3127 samed = flam3_random_bit();
3128 multid = flam3_random_bit();
3129 postid = flam3_random01() < 0.6;
3130 samepost = flam3_random_bit();
3131
3132 /* Loop over xforms */
3133 for (i = 0; i < nxforms; i++) {
3134 int j, k;
3135 cp->xform[i].density = 1.0 / nxforms;
3136 cp->xform[i].color = i&1;
3137 cp->xform[i].color_speed = 0.5;
3138 cp->xform[i].animate = 1.0;
3139 for (j = 0; j < 3; j++) {
3140 for (k = 0; k < 2; k++) {
3141 cp->xform[i].c[j][k] = flam3_random11();
3142 cp->xform[i].post[j][k] = (double)(k==j);
3143 }
3144 }
3145
3146 if ( i != finum ) {
3147
3148 if (!postid) {
3149
3150 for (j = 0; j < 3; j++)
3151 for (k = 0; k < 2; k++) {
3152 if (samepost || (i==0))
3153 cp->xform[i].post[j][k] = flam3_random11();
3154 else
3155 cp->xform[i].post[j][k] = cp->xform[0].post[j][k];
3156 }
3157 }
3158
3159 /* Clear all variation coefs */
3160 for (j = 0; j < flam3_nvariations; j++)
3161 cp->xform[i].var[j] = 0.0;
3162
3163 if (flam3_variation_random != var &&
3164 flam3_variation_random_fromspecified != var) {
3165
3166 /* Use only one variation specified for all xforms */
3167 cp->xform[i].var[var] = 1.0;
3168
3169 } else if (multid && flam3_variation_random == var) {
3170
3171 /* Choose a random var for this xform */
3172 cp->xform[i].var[random_varn(mvar)] = 1.0;
3173
3174 } else {
3175
3176 if (samed && i > 0) {
3177
3178 /* Copy the same variations from the previous xform */
3179 for (j = 0; j < flam3_nvariations; j++) {
3180 cp->xform[i].var[j] = cp->xform[i-1].var[j];
3181 flam3_copy_params(&(cp->xform[i]),&(cp->xform[i-1]),j);
3182 }
3183
3184 } else {
3185
3186 /* Choose a random number of vars to use, at least 2 */
3187 /* but less than flam3_nvariations.Probability leans */
3188 /* towards fewer variations. */
3189 n = 2;
3190 while ((flam3_random_bit()) && (n<mvar))
3191 n++;
3192
3193 /* Randomly choose n variations, and change their weights. */
3194 /* A var can be selected more than once, further reducing */
3195 /* the probability that multiple vars are used. */
3196 for (j = 0; j < n; j++) {
3197 if (flam3_variation_random_fromspecified != var)
3198 cp->xform[i].var[random_varn(mvar)] = flam3_random01();
3199 else
3200 cp->xform[i].var[ivars[random_varn(ivars_n)]] = flam3_random01();
3201 }
3202
3203 /* Normalize weights to 1.0 total. */
3204 sum = 0.0;
3205 for (j = 0; j < flam3_nvariations; j++)
3206 sum += cp->xform[i].var[j];
3207 if (sum == 0.0)
3208 cp->xform[i].var[random_var()] = 1.0;
3209 else {
3210 for (j = 0; j < flam3_nvariations; j++)
3211 cp->xform[i].var[j] /= sum;
3212 }
3213 }
3214 }
3215 } else {
3216 /* Handle final xform randomness. */
3217 n = 1;
3218 if (flam3_random_bit()) n++;
3219
3220 /* Randomly choose n variations, and change their weights. */
3221 /* A var can be selected more than once, further reducing */
3222 /* the probability that multiple vars are used. */
3223 for (j = 0; j < n; j++) {
3224 if (flam3_variation_random_fromspecified != var)
3225 cp->xform[i].var[random_varn(mvar)] = flam3_random01();
3226 else
3227 cp->xform[i].var[ivars[random_varn(ivars_n)]] = flam3_random01();
3228 }
3229
3230 /* Normalize weights to 1.0 total. */
3231 sum = 0.0;
3232 for (j = 0; j < flam3_nvariations; j++)
3233 sum += cp->xform[i].var[j];
3234 if (sum == 0.0)
3235 cp->xform[i].var[random_var()] = 1.0;
3236 else {
3237 for (j = 0; j < flam3_nvariations; j++)
3238 cp->xform[i].var[j] /= sum;
3239 }
3240 }
3241
3242 /* Generate random params for parametric variations, if selected. */
3243 if (cp->xform[i].var[VAR_BLOB] > 0) {
3244 /* Create random params for blob */
3245 cp->xform[i].blob_low = 0.2 + 0.5 * flam3_random01();
3246 cp->xform[i].blob_high = 0.8 + 0.4 * flam3_random01();
3247 cp->xform[i].blob_waves = (int)(2 + 5 * flam3_random01());
3248 }
3249
3250 if (cp->xform[i].var[VAR_PDJ] > 0) {
3251 /* Create random params for PDJ */
3252 cp->xform[i].pdj_a = 3.0 * flam3_random11();
3253 cp->xform[i].pdj_b = 3.0 * flam3_random11();
3254 cp->xform[i].pdj_c = 3.0 * flam3_random11();
3255 cp->xform[i].pdj_d = 3.0 * flam3_random11();
3256 }
3257
3258 if (cp->xform[i].var[VAR_FAN2] > 0) {
3259 /* Create random params for fan2 */
3260 cp->xform[i].fan2_x = flam3_random11();
3261 cp->xform[i].fan2_y = flam3_random11();
3262 }
3263
3264 if (cp->xform[i].var[VAR_RINGS2] > 0) {
3265 /* Create random params for rings2 */
3266 cp->xform[i].rings2_val = 2*flam3_random01();
3267 }
3268
3269 if (cp->xform[i].var[VAR_PERSPECTIVE] > 0) {
3270
3271 /* Create random params for perspective */
3272 cp->xform[i].perspective_angle = flam3_random01();
3273 cp->xform[i].perspective_dist = 2*flam3_random01() + 1.0;
3274
3275 }
3276
3277 if (cp->xform[i].var[VAR_JULIAN] > 0) {
3278
3279 /* Create random params for julian */
3280 cp->xform[i].julian_power = (int)(5*flam3_random01() + 2);
3281 cp->xform[i].julian_dist = 1.0;
3282
3283 }
3284
3285 if (cp->xform[i].var[VAR_JULIASCOPE] > 0) {
3286
3287 /* Create random params for juliaScope */
3288 cp->xform[i].juliascope_power = (int)(5*flam3_random01() + 2);
3289 cp->xform[i].juliascope_dist = 1.0;
3290
3291 }
3292
3293 if (cp->xform[i].var[VAR_RADIAL_BLUR] > 0) {
3294
3295 /* Create random params for radialBlur */
3296 cp->xform[i].radial_blur_angle = (2 * flam3_random01() - 1);
3297
3298 }
3299
3300 if (cp->xform[i].var[VAR_PIE] > 0) {
3301 /* Create random params for pie */
3302 cp->xform[i].pie_slices = (int) 10.0*flam3_random01();
3303 cp->xform[i].pie_thickness = flam3_random01();
3304 cp->xform[i].pie_rotation = 2.0 * M_PI * flam3_random11();
3305 }
3306
3307 if (cp->xform[i].var[VAR_NGON] > 0) {
3308 /* Create random params for ngon */
3309 cp->xform[i].ngon_sides = (int) flam3_random01()* 10 + 3;
3310 cp->xform[i].ngon_power = 3*flam3_random01() + 1;
3311 cp->xform[i].ngon_circle = 3*flam3_random01();
3312 cp->xform[i].ngon_corners = 2*flam3_random01()*cp->xform[i].ngon_circle;
3313 }
3314
3315 if (cp->xform[i].var[VAR_CURL] > 0) {
3316 /* Create random params for curl */
3317 cp->xform[i].curl_c1 = flam3_random01();
3318 cp->xform[i].curl_c2 = flam3_random01();
3319 }
3320
3321 if (cp->xform[i].var[VAR_RECTANGLES] > 0) {
3322 /* Create random params for rectangles */
3323 cp->xform[i].rectangles_x = flam3_random01();
3324 cp->xform[i].rectangles_y = flam3_random01();
3325 }
3326
3327 if (cp->xform[i].var[VAR_DISC2] > 0) {
3328 /* Create random params for disc2 */
3329 cp->xform[i].disc2_rot = 0.5 * flam3_random01();
3330 cp->xform[i].disc2_twist = 0.5 * flam3_random01();
3331
3332 }
3333
3334 if (cp->xform[i].var[VAR_SUPER_SHAPE] > 0) {
3335 /* Create random params for supershape */
3336 cp->xform[i].super_shape_rnd = flam3_random01();
3337 cp->xform[i].super_shape_m = (int) flam3_random01()*6;
3338 cp->xform[i].super_shape_n1 = flam3_random01()*40;
3339 cp->xform[i].super_shape_n2 = flam3_random01()*20;
3340 cp->xform[i].super_shape_n3 = cp->xform[i].super_shape_n2;
3341 cp->xform[i].super_shape_holes = 0.0;
3342 }
3343
3344 if (cp->xform[i].var[VAR_FLOWER] > 0) {
3345 /* Create random params for flower */
3346 cp->xform[i].flower_petals = 4 * flam3_random01();
3347 cp->xform[i].flower_holes = flam3_random01();
3348 }
3349
3350 if (cp->xform[i].var[VAR_CONIC] > 0) {
3351 /* Create random params for conic */
3352 cp->xform[i].conic_eccentricity = flam3_random01();
3353 cp->xform[i].conic_holes = flam3_random01();
3354 }
3355
3356 if (cp->xform[i].var[VAR_PARABOLA] > 0) {
3357 /* Create random params for parabola */
3358 cp->xform[i].parabola_height = 0.5 + flam3_random01();
3359 cp->xform[i].parabola_width = 0.5 + flam3_random01();
3360 }
3361
3362 if (cp->xform[i].var[VAR_BENT2] > 0) {
3363 /* Create random params for bent2 */
3364 cp->xform[i].bent2_x = 3*(-0.5 + flam3_random01());
3365 cp->xform[i].bent2_y = 3*(-0.5 + flam3_random01());
3366 }
3367
3368 if (cp->xform[i].var[VAR_BIPOLAR] > 0) {
3369 /* Create random params for bipolar */
3370 cp->xform[i].bipolar_shift = 2.0 * flam3_random01() - 1;
3371 }
3372
3373 if (cp->xform[i].var[VAR_CELL] > 0) {
3374 /* Create random params for cell */
3375 cp->xform[i].cell_size = 2.0 * flam3_random01() + 0.5;
3376 }
3377
3378 if (cp->xform[i].var[VAR_CPOW] > 0) {
3379 /* Create random params for cpow */
3380 cp->xform[i].cpow_r = 3.0 * flam3_random01();
3381 cp->xform[i].cpow_i = flam3_random01() - 0.5;
3382 cp->xform[i].cpow_power = (int)(5.0 * flam3_random01());
3383 }
3384
3385 if (cp->xform[i].var[VAR_CURVE] > 0) {
3386 /* Create random params for curve */
3387 cp->xform[i].curve_xamp = 5 * (flam3_random01()-.5);
3388 cp->xform[i].curve_yamp = 4 * (flam3_random01()-.5);
3389 cp->xform[i].curve_xlength = 2 * (flam3_random01()+.5);
3390 cp->xform[i].curve_ylength = 2 * (flam3_random01()+.5);
3391 }
3392
3393 if (cp->xform[i].var[VAR_ESCHER] > 0) {
3394 /* Create random params for escher */
3395 cp->xform[i].escher_beta = M_PI * flam3_random11();
3396 }
3397
3398 if (cp->xform[i].var[VAR_LAZYSUSAN] > 0) {
3399 /* Create random params for lazysusan */
3400 cp->xform[i].lazysusan_x = 2.0*flam3_random11();
3401 cp->xform[i].lazysusan_y = 2.0*flam3_random11();
3402 cp->xform[i].lazysusan_spin = M_PI*flam3_random11();
3403 cp->xform[i].lazysusan_space = 2.0*flam3_random11();
3404 cp->xform[i].lazysusan_twist = 2.0*flam3_random11();
3405 }
3406
3407 if (cp->xform[i].var[VAR_MODULUS] > 0) {
3408 /* Create random params for modulus */
3409 cp->xform[i].modulus_x = flam3_random11();
3410 cp->xform[i].modulus_y = flam3_random11();
3411 }
3412
3413 if (cp->xform[i].var[VAR_OSCILLOSCOPE] > 0) {
3414 /* Create random params for oscope */
3415 cp->xform[i].oscope_separation = 1.0 + flam3_random11();
3416 cp->xform[i].oscope_frequency = M_PI * flam3_random11();
3417 cp->xform[i].oscope_amplitude = 1.0 + 2 * flam3_random01();
3418 cp->xform[i].oscope_damping = flam3_random01();
3419 }
3420
3421 if (cp->xform[i].var[VAR_POPCORN2] > 0) {
3422 /* Create random params for popcorn2 */
3423 cp->xform[i].popcorn2_x = 0.2 * flam3_random01();
3424 cp->xform[i].popcorn2_y = 0.2 * flam3_random01();
3425 cp->xform[i].popcorn2_c = 5 * flam3_random01();
3426 }
3427
3428 if (cp->xform[i].var[VAR_SEPARATION] > 0) {
3429 /* Create random params for separation */
3430 cp->xform[i].separation_x = 1 + flam3_random11();
3431 cp->xform[i].separation_y = 1 + flam3_random11();
3432 cp->xform[i].separation_xinside = flam3_random11();
3433 cp->xform[i].separation_yinside = flam3_random11();
3434 }
3435
3436 if (cp->xform[i].var[VAR_SPLIT] > 0) {
3437 /* Create random params for split */
3438 cp->xform[i].split_xsize = flam3_random11();
3439 cp->xform[i].split_ysize = flam3_random11();
3440 }
3441
3442 if (cp->xform[i].var[VAR_SPLITS] > 0) {
3443 /* Create random params for splits */
3444 cp->xform[i].splits_x = flam3_random11();
3445 cp->xform[i].splits_y = flam3_random11();
3446 }
3447
3448 if (cp->xform[i].var[VAR_STRIPES] > 0) {
3449 /* Create random params for stripes */
3450 cp->xform[i].stripes_space = flam3_random01();
3451 cp->xform[i].stripes_warp = 5*flam3_random01();
3452 }
3453
3454 if (cp->xform[i].var[VAR_WEDGE] > 0) {
3455 /* Create random params for wedge */
3456 cp->xform[i].wedge_angle = M_PI*flam3_random01();
3457 cp->xform[i].wedge_hole = 0.5*flam3_random11();
3458 cp->xform[i].wedge_count = floor(5*flam3_random01())+1;
3459 cp->xform[i].wedge_swirl = flam3_random01();
3460 }
3461
3462 if (cp->xform[i].var[VAR_WEDGE_JULIA] > 0) {
3463
3464 /* Create random params for wedge_julia */
3465 cp->xform[i].wedge_julia_power = (int)(5*flam3_random01() + 2);
3466 cp->xform[i].wedge_julia_dist = 1.0;
3467 cp->xform[i].wedge_julia_count = (int)(3*flam3_random01() + 1);
3468 cp->xform[i].wedge_julia_angle = M_PI * flam3_random01();
3469
3470 }
3471
3472 if (cp->xform[i].var[VAR_WEDGE_SPH] > 0) {
3473 /* Create random params for wedge_sph */
3474 cp->xform[i].wedge_sph_angle = M_PI*flam3_random01();
3475 cp->xform[i].wedge_sph_hole = 0.5*flam3_random11();
3476 cp->xform[i].wedge_sph_count = floor(5*flam3_random01())+1;
3477 cp->xform[i].wedge_sph_swirl = flam3_random01();
3478 }
3479
3480 if (cp->xform[i].var[VAR_WHORL] > 0) {
3481 /* Create random params for whorl */
3482 cp->xform[i].whorl_inside = flam3_random01();
3483 cp->xform[i].whorl_outside = flam3_random01();
3484 }
3485
3486 if (cp->xform[i].var[VAR_WAVES2] > 0) {
3487 /* Create random params for waves2 */
3488 cp->xform[i].waves2_scalex = 0.5 + flam3_random01();
3489 cp->xform[i].waves2_scaley = 0.5 + flam3_random01();
3490 cp->xform[i].waves2_freqx = 4 * flam3_random01();
3491 cp->xform[i].waves2_freqy = 4 * flam3_random01();
3492 }
3493
3494 if (cp->xform[i].var[VAR_AUGER] > 0) {
3495 /* Create random params for auger */
3496 cp->xform[i].auger_sym = 0;
3497 cp->xform[i].auger_weight = 0.5 + flam3_random01()/2.0;
3498 cp->xform[i].auger_freq = floor(5*flam3_random01())+1;
3499 cp->xform[i].auger_scale = flam3_random01();
3500 }
3501
3502 if (cp->xform[i].var[VAR_FLUX] > 0) {
3503 /* Create random params for flux */
3504 cp->xform[i].flux_spread = 0.5 + flam3_random01()/2.0;
3505 }
3506
3507 if (cp->xform[i].var[VAR_MOBIUS] > 0) {
3508 /* Create random params for mobius */
3509 cp->xform[i].mobius_re_a = flam3_random11();
3510 cp->xform[i].mobius_im_a = flam3_random11();
3511 cp->xform[i].mobius_re_b = flam3_random11();
3512 cp->xform[i].mobius_im_b = flam3_random11();
3513 cp->xform[i].mobius_re_c = flam3_random11();
3514 cp->xform[i].mobius_im_c = flam3_random11();
3515 cp->xform[i].mobius_re_d = flam3_random11();
3516 cp->xform[i].mobius_im_d = flam3_random11();
3517 }
3518
3519 }
3520
3521 /* Randomly add symmetry (but not if we've already added a final xform) */
3522 if (sym || (!(random()%4) && !addfinal))
3523 flam3_add_symmetry(cp, sym);
3524 else
3525 cp->symmetry = 0;
3526
3527 //qsort((char *) cp->xform, (cp->num_xforms-addfinal), sizeof(flam3_xform), compare_xforms);
3528
3529
3530 }
3531
3532
3533 static int sort_by_x(const void *av, const void *bv) {
3534 double *a = (double *) av;
3535 double *b = (double *) bv;
3536 if (a[0] < b[0]) return -1;
3537 if (a[0] > b[0]) return 1;
3538 return 0;
3539 }
3540
3541 static int sort_by_y(const void *av, const void *bv) {
3542 double *a = (double *) av;
3543 double *b = (double *) bv;
3544 if (a[1] < b[1]) return -1;
3545 if (a[1] > b[1]) return 1;
3546 return 0;
3547 }
3548
3549
3550 /* Memory helper functions because
3551
3552 Python on Windows uses the MSVCR71.dll version of the C Runtime and
3553 mingw uses the MSVCRT.dll version. */
3554
3555 void *flam3_malloc(size_t size) {
3556
3557 return (malloc(size));
3558
3559 }
3560
3561 void flam3_free(void *ptr) {
3562
3563 free(ptr);
3564
3565 }
3566
3567 /*
3568 * find a 2d bounding box that does not enclose eps of the fractal density
3569 * in each compass direction.
3570 */
3571 int flam3_estimate_bounding_box(flam3_genome *cp, double eps, int nsamples,
3572 double *bmin, double *bmax, randctx *rc) {
3573 int i;
3574 int low_target, high_target;
3575 double min[2], max[2];
3576 double *points;
3577 int bv;
3578 unsigned short *xform_distrib;
3579
3580 if (nsamples <= 0) nsamples = 10000;
3581
3582 points = (double *) malloc(sizeof(double) * 4 * nsamples);
3583 points[0] = flam3_random_isaac_11(rc);
3584 points[1] = flam3_random_isaac_11(rc);
3585 points[2] = 0.0;
3586 points[3] = 0.0;
3587
3588 if (prepare_precalc_flags(cp))
3589 return(-1);
3590 xform_distrib = flam3_create_xform_distrib(cp);
3591 if (xform_distrib==NULL)
3592 return(-1);
3593 bv=flam3_iterate(cp, nsamples, 20, points, xform_distrib, rc);
3594 free(xform_distrib);
3595
3596 if ( bv/(double)nsamples > eps )
3597 eps = 3*bv/(double)nsamples;
3598
3599 if ( eps > 0.3 )
3600 eps = 0.3;
3601
3602 low_target = (int)(nsamples * eps);
3603 high_target = nsamples - low_target;
3604
3605
3606 min[0] = min[1] = 1e10;
3607 max[0] = max[1] = -1e10;
3608
3609 for (i = 0; i < nsamples; i++) {
3610 double *p = &points[4*i];
3611 if (p[0] < min[0]) min[0] = p[0];
3612 if (p[1] < min[1]) min[1] = p[1];
3613 if (p[0] > max[0]) max[0] = p[0];
3614 if (p[1] > max[1]) max[1] = p[1];
3615 }
3616
3617 if (low_target == 0) {
3618 bmin[0] = min[0];
3619 bmin[1] = min[1];
3620 bmax[0] = max[0];
3621 bmax[1] = max[1];
3622 free(points);
3623 return(bv);
3624 }
3625
3626 qsort(points, nsamples, sizeof(double) * 4, sort_by_x);
3627 bmin[0] = points[4 * low_target];
3628 bmax[0] = points[4 * high_target];
3629
3630 qsort(points, nsamples, sizeof(double) * 4, sort_by_y);
3631 bmin[1] = points[4 * low_target + 1];
3632 bmax[1] = points[4 * high_target + 1];
3633 free(points);
3634
3635 return(bv);
3636 }
3637
3638
3639 typedef double bucket_double[5];
3640 typedef double abucket_double[4];
3641 typedef unsigned int bucket_int[5];
3642 typedef unsigned int abucket_int[4];
3643 typedef float bucket_float[5];
3644 typedef float abucket_float[4];
3645
3646 #ifdef HAVE_GCC_64BIT_ATOMIC_OPS
3647 static inline void
3648 double_atomic_add(double *dest, double delta)
3649 {
3650 uint64_t *int_ptr = (uint64_t *)dest;
3651 union {
3652 double dblval;
3653 uint64_t intval;
3654 } old_val, new_val;
3655 int success;
3656
3657 do {
3658 old_val.dblval = *dest;
3659 new_val.dblval = old_val.dblval + delta;
3660 success = __sync_bool_compare_and_swap(
3661 int_ptr, old_val.intval, new_val.intval);
3662 } while (!success);
3663 }
3664 #endif /* HAVE_GCC_64BIT_ATOMIC_OPS */
3665
3666 #ifdef HAVE_GCC_ATOMIC_OPS
3667 static inline void
3668 float_atomic_add(float *dest, float delta)
3669 {
3670 uint32_t *int_ptr = (uint32_t *)dest;
3671 union {
3672 float fltval;
3673 uint32_t intval;
3674 } old_val, new_val;
3675 int success;
3676
3677 do {
3678 old_val.fltval = *dest;
3679 new_val.fltval = old_val.fltval + delta;
3680 success = __sync_bool_compare_and_swap(
3681 int_ptr, old_val.intval, new_val.intval);
3682 } while (!success);
3683 }
3684
3685 static inline void
3686 uint_atomic_add(unsigned int *dest, unsigned int delta)
3687 {
3688 unsigned int old_val, new_val;
3689 int success;
3690
3691 do {
3692 old_val = *dest;
3693 if (UINT_MAX - old_val > delta)
3694 new_val = old_val + delta;
3695 else
3696 new_val = UINT_MAX;
3697 success = __sync_bool_compare_and_swap(
3698 dest, old_val, new_val);
3699 } while (!success);
3700 }
3701
3702 static inline void
3703 ushort_atomic_add(unsigned short *dest, unsigned short delta)
3704 {
3705 unsigned short old_val, new_val;
3706 int success;
3707
3708 do {
3709 old_val = *dest;
3710 if (USHRT_MAX - old_val > delta)
3711 new_val = old_val + delta;
3712 else
3713 new_val = USHRT_MAX;
3714 success = __sync_bool_compare_and_swap(
3715 dest, old_val, new_val);
3716 } while (!success);
3717 }
3718 #endif /* HAVE_GCC_ATOMIC_OPS */
3719
3720 /* 64-bit datatypes */
3721 #define bucket bucket_double
3722 #define abucket abucket_double
3723 #define abump_no_overflow(dest, delta) do {dest += delta;} while (0)
3724 #define add_c_to_accum(acc,i,ii,j,jj,wid,hgt,c) do { \
3725 if ( (j) + (jj) >=0 && (j) + (jj) < (hgt) && (i) + (ii) >=0 && (i) + (ii) < (wid)) { \
3726 abucket *a = (acc) + ( (i) + (ii) ) + ( (j) + (jj) ) * (wid); \
3727 abump_no_overflow(a[0][0],(c)[0]); \
3728 abump_no_overflow(a[0][1],(c)[1]); \
3729 abump_no_overflow(a[0][2],(c)[2]); \
3730 abump_no_overflow(a[0][3],(c)[3]); \
3731 } \
3732 } while (0)
3733 /* single-threaded */
3734 #define USE_LOCKS
3735 #define bump_no_overflow(dest, delta) do {dest += delta;} while (0)
3736 #define render_rectangle render_rectangle_double
3737 #define iter_thread iter_thread_double
3738 #define de_thread_helper de_thread_helper_64
3739 #define de_thread de_thread_64
3740 #include "rect.c"
3741 #ifdef HAVE_GCC_64BIT_ATOMIC_OPS
3742 /* multi-threaded */
3743 #undef USE_LOCKS
3744 #undef bump_no_overflow
3745 #undef render_rectangle
3746 #undef iter_thread
3747 #undef de_thread_helper
3748 #undef de_thread
3749 #define bump_no_overflow(dest, delta) double_atomic_add(&dest, delta)
3750 #define render_rectangle render_rectangle_double_mt
3751 #define iter_thread iter_thread_double_mt
3752 #define de_thread_helper de_thread_helper_64_mt
3753 #define de_thread de_thread_64_mt
3754 #include "rect.c"
3755 #else /* !HAVE_GCC_64BIT_ATOMIC_OPS */
3756 #define render_rectangle_double_mt render_rectangle_double
3757 #endif /* HAVE_GCC_64BIT_ATOMIC_OPS */
3758 #undef render_rectangle
3759 #undef iter_thread
3760 #undef add_c_to_accum
3761 #undef bucket
3762 #undef abucket
3763 #undef bump_no_overflow
3764 #undef abump_no_overflow
3765 #undef de_thread_helper
3766 #undef de_thread
3767
3768 /* 32-bit datatypes */
3769 #define bucket bucket_int
3770 #define abucket abucket_int
3771 #define abump_no_overflow(dest, delta) do { \
3772 if (UINT_MAX - dest > delta) dest += delta; else dest = UINT_MAX; \
3773 } while (0)
3774 #define add_c_to_accum(acc,i,ii,j,jj,wid,hgt,c) do { \
3775 if ( (j) + (jj) >=0 && (j) + (jj) < (hgt) && (i) + (ii) >=0 && (i) + (ii) < (wid)) { \
3776 abucket *a = (acc) + ( (i) + (ii) ) + ( (j) + (jj) ) * (wid); \
3777 abump_no_overflow(a[0][0],(c)[0]); \
3778 abump_no_overflow(a[0][1],(c)[1]); \
3779 abump_no_overflow(a[0][2],(c)[2]); \
3780 abump_no_overflow(a[0][3],(c)[3]); \
3781 } \
3782 } while (0)
3783 /* single-threaded */
3784 #define USE_LOCKS
3785 #define bump_no_overflow(dest, delta) do { \
3786 if (UINT_MAX - dest > delta) dest += delta; else dest = UINT_MAX; \
3787 } while (0)
3788 #define render_rectangle render_rectangle_int
3789 #define iter_thread iter_thread_int
3790 #define de_thread_helper de_thread_helper_32
3791 #define de_thread de_thread_32
3792 #include "rect.c"
3793 #ifdef HAVE_GCC_ATOMIC_OPS
3794 /* multi-threaded */
3795 #undef USE_LOCKS
3796 #undef bump_no_overflow
3797 #undef render_rectangle
3798 #undef iter_thread
3799 #undef de_thread_helper
3800 #undef de_thread
3801 #define bump_no_overflow(dest, delta) uint_atomic_add(&dest, delta)
3802 #define render_rectangle render_rectangle_int_mt
3803 #define iter_thread iter_thread_int_mt
3804 #define de_thread_helper de_thread_helper_32_mt
3805 #define de_thread de_thread_32_mt
3806 #include "rect.c"
3807 #else /* !HAVE_GCC_ATOMIC_OPS */
3808 #define render_rectangle_int_mt render_rectangle_int
3809 #endif /* HAVE_GCC_ATOMIC_OPS */
3810 #undef iter_thread
3811 #undef render_rectangle
3812 #undef add_c_to_accum
3813 #undef bucket
3814 #undef abucket
3815 #undef bump_no_overflow
3816 #undef abump_no_overflow
3817 #undef de_thread_helper
3818 #undef de_thread
3819
3820 /* experimental 32-bit datatypes (called 33) */
3821 #define bucket bucket_int
3822 #define abucket abucket_float
3823 #define abump_no_overflow(dest, delta) do {dest += delta;} while (0)
3824 #define add_c_to_accum(acc,i,ii,j,jj,wid,hgt,c) do { \
3825 if ( (j) + (jj) >=0 && (j) + (jj) < (hgt) && (i) + (ii) >=0 && (i) + (ii) < (wid)) { \
3826 abucket *a = (acc) + ( (i) + (ii) ) + ( (j) + (jj) ) * (wid); \
3827 abump_no_overflow(a[0][0],(c)[0]); \
3828 abump_no_overflow(a[0][1],(c)[1]); \
3829 abump_no_overflow(a[0][2],(c)[2]); \
3830 abump_no_overflow(a[0][3],(c)[3]); \
3831 } \
3832 } while (0)
3833 /* single-threaded */
3834 #define USE_LOCKS
3835 #define bump_no_overflow(dest, delta) do { \
3836 if (UINT_MAX - dest > delta) dest += delta; else dest = UINT_MAX; \
3837 } while (0)
3838 #define render_rectangle render_rectangle_float
3839 #define iter_thread iter_thread_float
3840 #define de_thread_helper de_thread_helper_33
3841 #define de_thread de_thread_33
3842 #include "rect.c"
3843 #ifdef HAVE_GCC_ATOMIC_OPS
3844 /* multi-threaded */
3845 #undef USE_LOCKS
3846 #undef bump_no_overflow
3847 #undef render_rectangle
3848 #undef iter_thread
3849 #undef de_thread_helper
3850 #undef de_thread
3851 #define bump_no_overflow(dest, delta) uint_atomic_add(&dest, delta)
3852 #define render_rectangle render_rectangle_float_mt
3853 #define iter_thread iter_thread_float_mt
3854 #define de_thread_helper de_thread_helper_33_mt
3855 #define de_thread de_thread_33_mt
3856 #include "rect.c"
3857 #else /* !HAVE_GCC_ATOMIC_OPS */
3858 #define render_rectangle_float_mt render_rectangle_float
3859 #endif /* HAVE_GCC_ATOMIC_OPS */
3860 #undef iter_thread
3861 #undef render_rectangle
3862 #undef add_c_to_accum
3863 #undef bucket
3864 #undef abucket
3865 #undef bump_no_overflow
3866 #undef abump_no_overflow
3867 #undef de_thread_helper
3868 #undef de_thread
3869
3870
3871 double flam3_render_memory_required(flam3_frame *spec)
3872 {
3873 flam3_genome *cps = spec->genomes;
3874 int real_bits = spec->bits;
3875 int real_bytes;
3876
3877 if (33 == real_bits) real_bits = 32;
3878
3879 real_bytes = real_bits / 8;
3880
3881 return
3882 (double) cps[0].spatial_oversample * cps[0].spatial_oversample *
3883 (double) cps[0].width * cps[0].height * real_bytes * 9.0;
3884 }
3885
3886 void bits_error(flam3_frame *spec) {
3887 fprintf(stderr, "flam3: bits must be 32, 33, or 64 not %d.\n",
3888 spec->bits);
3889 }
3890
3891 int flam3_render(flam3_frame *spec, void *out,
3892 int field, int nchan, int trans, stat_struct *stats) {
3893
3894 int retval;
3895
3896 if (spec->nthreads <= 2) {
3897 /* single-threaded or 2 threads without atomic operations */
3898 switch (spec->bits) {
3899 case 32:
3900 retval = render_rectangle_int(spec, out, field, nchan, trans, stats);
3901 return(retval);
3902 case 33:
3903 retval = render_rectangle_float(spec, out, field, nchan, trans, stats);
3904 return(retval);
3905 case 64:
3906 retval = render_rectangle_double(spec, out, field, nchan, trans, stats);
3907 return(retval);
3908 default:
3909 bits_error(spec);
3910 return(1);
3911 }
3912 } else {
3913 /* 3+ threads using atomic ops if available */
3914 switch (spec->bits) {
3915 case 32:
3916 retval = render_rectangle_int_mt(spec, out, field, nchan, trans, stats);
3917 return(retval);
3918 case 33:
3919 retval = render_rectangle_float_mt(spec, out, field, nchan, trans, stats);
3920 return(retval);
3921 case 64:
3922 retval = render_rectangle_double_mt(spec, out, field, nchan, trans, stats);
3923 return(retval);
3924 default:
3925 bits_error(spec);
3926 return(1);
3927 }
3928 }
3929 }
3930
3931
3932 void flam3_srandom() {
3933 unsigned int seed;
3934 char *s = getenv("seed");
3935
3936 if (s)
3937 seed = atoi(s);
3938 else
3939 seed = time(0) + getpid();
3940
3941 srandom(seed);
3942 }
3943
3944
3945 /* correlation dimension, after clint sprott.
3946 computes slope of the correlation sum at a size scale
3947 the order of 2% the size of the attractor or the camera. */
3948 double flam3_dimension(flam3_genome *cp, int ntries, int clip_to_camera) {
3949 double fd;
3950 double *hist;
3951 double bmin[2];
3952 double bmax[2];
3953 double d2max;
3954 int lp;
3955 long int default_isaac_seed = (long int)time(0);
3956 randctx rc;
3957 int SBS = 10000;
3958 int i, n1=0, n2=0, got, nclipped;
3959
3960 /* Set up the isaac rng */
3961 for (lp = 0; lp < RANDSIZ; lp++)
3962 rc.randrsl[lp] = default_isaac_seed;
3963
3964 irandinit(&rc,1);
3965
3966 if (ntries < 2) ntries = 3000*1000;
3967
3968 if (clip_to_camera) {
3969 double scale, ppux, corner0, corner1;
3970 scale = pow(2.0, cp->zoom);
3971 ppux = cp->pixels_per_unit * scale;
3972 corner0 = cp->center[0] - cp->width / ppux / 2.0;
3973 corner1 = cp->center[1] - cp->height / ppux / 2.0;
3974 bmin[0] = corner0;
3975 bmin[1] = corner1;
3976 bmax[0] = corner0 + cp->width / ppux;
3977 bmax[1] = corner1 + cp->height / ppux;
3978 } else {
3979 if (flam3_estimate_bounding_box(cp, 0.0, 0, bmin, bmax, &rc)<0)
3980 return(-1.0);
3981
3982 }
3983
3984 d2max =
3985 (bmax[0] - bmin[0]) * (bmax[0] - bmin[0]) +
3986 (bmax[1] - bmin[1]) * (bmax[1] - bmin[1]);
3987
3988 // fprintf(stderr, "d2max=%g %g %g %g %g\n", d2max,
3989 // bmin[0], bmin[1], bmax[0], bmax[1]);
3990
3991 hist = malloc(2 * ntries * sizeof(double));
3992
3993 got = 0;
3994 nclipped = 0;
3995 while (got < 2*ntries) {
3996 double subb[40000];
3997 int i4, clipped;
3998 unsigned short *xform_distrib;
3999 subb[0] = flam3_random_isaac_11(&rc);
4000 subb[1] = flam3_random_isaac_11(&rc);
4001 subb[2] = 0.0;
4002 subb[3] = 0.0;
4003 if (prepare_precalc_flags(cp))
4004 return(-1.0);
4005 xform_distrib = flam3_create_xform_distrib(cp);
4006 if (xform_distrib==NULL)
4007 return(-1.0);
4008 flam3_iterate(cp, SBS, 20, subb, xform_distrib, &rc);
4009 free(xform_distrib);
4010 i4 = 0;
4011 for (i = 0; i < SBS; i++) {
4012 if (got == 2*ntries) break;
4013 clipped = clip_to_camera &&
4014 ((subb[i4] < bmin[0]) ||
4015 (subb[i4+1] < bmin[1]) ||
4016 (subb[i4] > bmax[0]) ||
4017 (subb[i4+1] > bmax[1]));
4018 if (!clipped) {
4019 hist[got] = subb[i4];
4020 hist[got+1] = subb[i4+1];
4021 got += 2;
4022 } else {
4023 nclipped++;
4024 if (nclipped > 10 * ntries) {
4025 fprintf(stderr, "warning: too much clipping, "
4026 "flam3_dimension giving up.\n");
4027 return sqrt(-1.0);
4028 }
4029 }
4030 i4 += 4;
4031 }
4032 }
4033 if (0)
4034 fprintf(stderr, "cliprate=%g\n", nclipped/(ntries+(double)nclipped));
4035
4036 for (i = 0; i < ntries; i++) {
4037 int ri;
4038 double dx, dy, d2;
4039 double tx, ty;
4040
4041 tx = hist[2*i];
4042 ty = hist[2*i+1];
4043
4044 do {
4045 ri = 2 * (random() % ntries);
4046 } while (ri == i);
4047
4048 dx = hist[ri] - tx;
4049 dy = hist[ri+1] - ty;
4050 d2 = dx*dx + dy*dy;
4051 if (d2 < 0.004 * d2max) n2++;
4052 if (d2 < 0.00004 * d2max) n1++;
4053 }
4054
4055 fd = 0.434294 * log(n2 / (n1 - 0.5));
4056
4057 if (0)
4058 fprintf(stderr, "n1=%d n2=%d\n", n1, n2);
4059
4060 free(hist);
4061 return fd;
4062 }
4063
4064 double flam3_lyapunov(flam3_genome *cp, int ntries) {
4065 double p[4];
4066 double x, y;
4067 double xn, yn;
4068 double xn2, yn2;
4069 double dx, dy, r;
4070 double eps = 1e-5;
4071 int i;
4072 double sum = 0.0;
4073 unsigned short *xform_distrib;
4074
4075 int lp;
4076 long int default_isaac_seed = (long int)time(0);
4077 randctx rc;
4078
4079 /* Set up the isaac rng */
4080 for (lp = 0; lp < RANDSIZ; lp++)
4081 rc.randrsl[lp] = default_isaac_seed;
4082
4083 irandinit(&rc,1);
4084
4085
4086 if (ntries < 1) ntries = 10000;
4087
4088 for (i = 0; i < ntries; i++) {
4089 x = flam3_random_isaac_11(&rc);
4090 y = flam3_random_isaac_11(&rc);
4091
4092 p[0] = x;
4093 p[1] = y;
4094 p[2] = 0.0;
4095 p[3] = 0.0;
4096
4097 // get into the attractor
4098 if (prepare_precalc_flags(cp))
4099 return(-1.0);
4100 xform_distrib = flam3_create_xform_distrib(cp);
4101 if (xform_distrib==NULL)
4102 return(-1.0);
4103
4104 flam3_iterate(cp, 1, 20+(random()%10), p, xform_distrib, &rc);
4105 free(xform_distrib);
4106
4107 x = p[0];
4108 y = p[1];
4109
4110 // take one deterministic step
4111 srandom(i);
4112
4113 if (prepare_precalc_flags(cp))
4114 return(-1.0);
4115 xform_distrib = flam3_create_xform_distrib(cp);
4116 if (xform_distrib==NULL)
4117 return(-1.0);
4118
4119 flam3_iterate(cp, 1, 0, p, xform_distrib, &rc);
4120 free(xform_distrib);
4121
4122 xn = p[0];
4123 yn = p[1];
4124
4125 do {
4126 dx = flam3_random_isaac_11(&rc);
4127 dy = flam3_random_isaac_11(&rc);
4128 r = sqrt(dx * dx + dy * dy);
4129 } while (r == 0.0);
4130 dx /= r;
4131 dy /= r;
4132
4133 dx *= eps;
4134 dy *= eps;
4135
4136 p[0] = x + dx;
4137 p[1] = y + dy;
4138 p[2] = 0.0;
4139
4140 // take the same step but with eps
4141 srandom(i);
4142 if (prepare_precalc_flags(cp))
4143 return(-1.0);
4144 xform_distrib = flam3_create_xform_distrib(cp);
4145 if (xform_distrib==NULL)
4146 return(-1.0);
4147
4148 flam3_iterate(cp, 1, 0, p, xform_distrib, &rc);
4149 free(xform_distrib);
4150
4151 xn2 = p[0];
4152 yn2 = p[1];
4153
4154 r = sqrt((xn-xn2)*(xn-xn2) + (yn-yn2)*(yn-yn2));
4155
4156 sum += log(r/eps);
4157 }
4158 return sum/(log(2.0)*ntries);
4159 }
4160