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 
20 #ifndef flam3_included
21 #define flam3_included
22 
23 #include <stdio.h>
24 #include <libxml/parser.h>
25 #include "isaac.h"
26 
27 #if defined(_MSC_VER) /* VC++ */
28 #include <windows.h>
29 #define EXPORT __declspec (dllexport)
30 #else
31 #define EXPORT
32 #endif
33 
34 EXPORT char *flam3_version();
35 
36 #define flam3_palette_random       (-1)
37 #define flam3_palette_interpolated (-2)
38 
39 #define flam3_defaults_on          (1)
40 #define flam3_defaults_off         (0)
41 
42 #define flam3_name_len    64
43 
44 #define flam3_print_edits  (1)
45 #define flam3_dont_print_edits  (0)
46 
47 //typedef double flam3_palette[256][3];
48 typedef struct {
49 	double index;
50 	double color[4];
51 } flam3_palette_entry;
52 
53 typedef flam3_palette_entry flam3_palette[256];
54 
55 int flam3_get_palette(int palette_index, flam3_palette p, double hue_rotation);
56 
57 #define flam3_variation_random (-1)
58 #define flam3_variation_random_fromspecified (-2)
59 
60 extern char *flam3_variation_names[];
61 
62 #define flam3_nvariations 99
63 #define flam3_nxforms     12
64 
65 #define flam3_parent_fn_len     30
66 
67 #define flam3_interpolation_linear 0
68 #define flam3_interpolation_smooth 1
69 
70 #define flam3_inttype_linear 0
71 #define flam3_inttype_log    1
72 #define flam3_inttype_compat 2 /* Linear and old behaviour */
73 #define flam3_inttype_older  3 /* rotate padded xforms     */
74 
75 #define flam3_palette_interpolation_hsv   0
76 #define flam3_palette_interpolation_sweep 1
77 #define flam3_palette_interpolation_rgb   2
78 #define flam3_palette_interpolation_hsv_circular  3
79 
80 #define flam3_max_action_length 10000
81 
82 #define flam3_palette_mode_step   0
83 #define flam3_palette_mode_linear 1
84 
85 #define VAR_LINEAR   0
86 #define VAR_SINUSOIDAL  1
87 #define VAR_SPHERICAL  2
88 #define VAR_SWIRL 3
89 #define VAR_HORSESHOE  4
90 #define VAR_POLAR 5
91 #define VAR_HANDKERCHIEF 6
92 #define VAR_HEART 7
93 #define VAR_DISC 8
94 #define VAR_SPIRAL 9
95 #define VAR_HYPERBOLIC 10
96 #define VAR_DIAMOND 11
97 #define VAR_EX 12
98 #define VAR_JULIA 13
99 #define VAR_BENT 14
100 #define VAR_WAVES 15
101 #define VAR_FISHEYE 16
102 #define VAR_POPCORN 17
103 #define VAR_EXPONENTIAL 18
104 #define VAR_POWER 19
105 #define VAR_COSINE 20
106 #define VAR_RINGS 21
107 #define VAR_FAN 22
108 #define VAR_BLOB 23
109 #define VAR_PDJ 24
110 #define VAR_FAN2 25
111 #define VAR_RINGS2 26
112 #define VAR_EYEFISH 27
113 #define VAR_BUBBLE 28
114 #define VAR_CYLINDER 29
115 #define VAR_PERSPECTIVE 30
116 #define VAR_NOISE 31
117 #define VAR_JULIAN 32
118 #define VAR_JULIASCOPE 33
119 #define VAR_BLUR 34
120 #define VAR_GAUSSIAN_BLUR 35
121 #define VAR_RADIAL_BLUR 36
122 #define VAR_PIE 37
123 #define VAR_NGON 38
124 #define VAR_CURL 39
125 #define VAR_RECTANGLES 40
126 #define VAR_ARCH 41
127 #define VAR_TANGENT 42
128 #define VAR_SQUARE 43
129 #define VAR_RAYS 44
130 #define VAR_BLADE 45
131 #define VAR_SECANT2 46
132 #define VAR_TWINTRIAN 47
133 #define VAR_CROSS 48
134 #define VAR_DISC2 49
135 #define VAR_SUPER_SHAPE 50
136 #define VAR_FLOWER 51
137 #define VAR_CONIC 52
138 #define VAR_PARABOLA 53
139 #define VAR_BENT2 54
140 #define VAR_BIPOLAR 55
141 #define VAR_BOARDERS 56
142 #define VAR_BUTTERFLY 57
143 #define VAR_CELL 58
144 #define VAR_CPOW 59
145 #define VAR_CURVE 60
146 #define VAR_EDISC 61
147 #define VAR_ELLIPTIC 62
148 #define VAR_ESCHER 63
149 #define VAR_FOCI 64
150 #define VAR_LAZYSUSAN 65
151 #define VAR_LOONIE 66
152 #define VAR_PRE_BLUR 67
153 #define VAR_MODULUS 68
154 #define VAR_OSCILLOSCOPE 69
155 #define VAR_POLAR2 70
156 #define VAR_POPCORN2 71
157 #define VAR_SCRY 72
158 #define VAR_SEPARATION 73
159 #define VAR_SPLIT 74
160 #define VAR_SPLITS 75
161 #define VAR_STRIPES 76
162 #define VAR_WEDGE 77
163 #define VAR_WEDGE_JULIA 78
164 #define VAR_WEDGE_SPH 79
165 #define VAR_WHORL 80
166 #define VAR_WAVES2 81
167 #define VAR_EXP 82
168 #define VAR_LOG 83
169 #define VAR_SIN 84
170 #define VAR_COS 85
171 #define VAR_TAN 86
172 #define VAR_SEC 87
173 #define VAR_CSC 88
174 #define VAR_COT 89
175 #define VAR_SINH 90
176 #define VAR_COSH 91
177 #define VAR_TANH 92
178 #define VAR_SECH 93
179 #define VAR_CSCH 94
180 #define VAR_COTH 95
181 #define VAR_AUGER 96
182 #define VAR_FLUX 97
183 #define VAR_MOBIUS 98
184 
185 typedef struct {
186 
187    double badvals;
188    long int num_iters;
189    int render_seconds;
190 
191 } stat_struct;
192 
193 typedef struct {
194 
195    unsigned int width, height;
196    int version;
197    int id;
198 
199    /* There are 256 levels of gray to work with */
200    double intensity_weight[256];
201    unsigned int bin_size[256];
202    unsigned int bin_offset[256];
203 
204    /* Pointer to newly allocated memory; we will be allocating */
205    /* 2*w*h ushorts for this storage.  The bin offset will     */
206    /* provide the starting point for a random selection from   */
207    /* (bin size) ordered pairs                                 */
208    unsigned short *rowcols;
209 
210 } flam3_image_store;
211 
212 
213 typedef struct xform {
214    double var[flam3_nvariations];   /* interp coefs between variations */
215    double c[3][2];      /* the coefs to the affine part of the function */
216    double post[3][2];   /* the post transform */
217    double density;      /* probability that this function is chosen. 0 - 1 */
218    double color;     /* color coords for this function. 0 - 1 */
219    double color_speed;  /* scaling factor on color added to current iteration */
220    double animate;      /* whether or not this xform rotates (in sheep) >0 means stationary */
221    double opacity;   /* 0=invisible, 1=totally visible */
222    double vis_adjusted; /* adjusted visibility for better transitions */
223 
224    int padding;/* Set to 1 for padding xforms */
225    double wind[2]; /* winding numbers */
226 
227    int precalc_angles_flag;
228    int precalc_atan_xy_flag;
229    int precalc_atan_yx_flag;
230    double has_preblur;
231    int has_post;
232 
233    /* Params for new parameterized variations */
234    /* Blob */
235    double blob_low;
236    double blob_high;
237    double blob_waves;
238 
239    /* PDJ */
240    double pdj_a;
241    double pdj_b;
242    double pdj_c;
243    double pdj_d;
244 
245    /* Fan2 */
246    double fan2_x;
247    double fan2_y;
248 
249    /* Rings2 */
250    double rings2_val;
251 
252    /* Perspective */
253    double perspective_angle;
254    double perspective_dist;
255 
256    /* Julia_N */
257    double julian_power;
258    double julian_dist;
259 
260    /* Julia_Scope */
261    double juliascope_power;
262    double juliascope_dist;
263 
264    /* Radial_Blur */
265    double radial_blur_angle;
266 
267    /* Pie */
268    double pie_slices;
269    double pie_rotation;
270    double pie_thickness;
271 
272    /* Ngon */
273    double ngon_sides;
274    double ngon_power;
275    double ngon_circle;
276    double ngon_corners;
277 
278    /* Curl */
279    double curl_c1;
280    double curl_c2;
281 
282    /* Rectangles */
283    double rectangles_x;
284    double rectangles_y;
285 
286    /* AMW */
287    double amw_amp;
288 
289    /* Disc 2 */
290    double disc2_rot;
291    double disc2_twist;
292 
293    /* Supershape */
294    double super_shape_rnd;
295    double super_shape_m;
296    double super_shape_n1;
297    double super_shape_n2;
298    double super_shape_n3;
299    double super_shape_holes;
300 
301    /* Flower */
302    double flower_petals;
303    double flower_holes;
304 
305    /* Conic */
306    double conic_eccentricity;
307    double conic_holes;
308 
309    /* Parabola */
310    double parabola_height;
311    double parabola_width;
312 
313    /* Bent2 */
314    double bent2_x;
315    double bent2_y;
316 
317    /* Bipolar */
318    double bipolar_shift;
319 
320    /* Cell */
321    double cell_size;
322 
323    /* Cpow */
324    double cpow_r;
325    double cpow_i;
326    double cpow_power; /* int in apo */
327 
328    /* Curve */
329    double curve_xamp,curve_yamp;
330    double curve_xlength,curve_ylength;
331 
332    /* Escher */
333    double escher_beta;
334 
335    /* Lazysusan */
336    double lazysusan_spin;
337    double lazysusan_space;
338    double lazysusan_twist;
339    double lazysusan_x, lazysusan_y;
340 
341    /* Modulus */
342    double modulus_x, modulus_y;
343 
344    /* Oscope */
345    double oscope_separation;
346    double oscope_frequency;
347    double oscope_amplitude;
348    double oscope_damping;
349 
350    /* Popcorn2 */
351    double popcorn2_x, popcorn2_y, popcorn2_c;
352 
353    /* Separation */
354    double separation_x, separation_xinside;
355    double separation_y, separation_yinside;
356 
357    /* Split */
358    double split_xsize;
359    double split_ysize;
360 
361    /* Splits */
362    double splits_x,splits_y;
363 
364    /* Stripes */
365    double stripes_space;
366    double stripes_warp;
367 
368    /* Wedge */
369    double wedge_angle, wedge_hole;
370    double wedge_count, wedge_swirl;
371 
372    /* Wedge_Julia */
373    double wedge_julia_angle;
374    double wedge_julia_count;
375    double wedge_julia_power;
376    double wedge_julia_dist;
377 
378    /* Wedge_Sph */
379    double wedge_sph_angle, wedge_sph_count;
380    double wedge_sph_hole, wedge_sph_swirl;
381 
382    /* Whorl */
383    double whorl_inside, whorl_outside;
384 
385    /* Waves2 */
386    double waves2_freqx, waves2_scalex;
387    double waves2_freqy, waves2_scaley;
388 
389    /* Auger */
390    double auger_sym, auger_weight;
391    double auger_freq, auger_scale;
392 
393    /* Flux */
394    double flux_spread;
395 
396    /* Mobius */
397    double mobius_re_a, mobius_im_a;
398    double mobius_re_b, mobius_im_b;
399    double mobius_re_c, mobius_im_c;
400    double mobius_re_d, mobius_im_d;
401 
402    /* If perspective is used, precalculate these values */
403    /* from the _angle and _dist                         */
404    double persp_vsin;
405    double persp_vfcos;
406 
407    /* If Julia_N is used, precalculate these values */
408    double julian_rN;
409    double julian_cn;
410 
411    /* If Julia_Scope is used, precalculate these values */
412    double juliascope_rN;
413    double juliascope_cn;
414 
415    /* if Wedge_Julia, precalculate */
416    double wedgeJulia_rN;
417    double wedgeJulia_cn;
418    double wedgeJulia_cf;
419 
420    /* If Radial_Blur is used, precalculate these values */
421    double radialBlur_spinvar;
422    double radialBlur_zoomvar;
423 
424    /* Precalculate these values for waves */
425    double waves_dx2;
426    double waves_dy2;
427 
428    /* If disc2 is used, precalculate these values */
429    double disc2_sinadd;
430    double disc2_cosadd;
431    double disc2_timespi;
432 
433    /* If supershape is used, precalculate these values */
434    double super_shape_pm_4;
435    double super_shape_pneg1_n1;
436 
437    int num_active_vars;
438    double active_var_weights[flam3_nvariations];
439    int varFunc[flam3_nvariations];
440 
441    int motion_freq;
442    int motion_func;
443 
444    struct xform *motion;
445    int num_motion;
446 
447 
448 } flam3_xform;
449 
450 typedef struct {
451    char flame_name[flam3_name_len+1]; /* 64 chars plus a null */
452    double time;
453    int interpolation;
454    int interpolation_type;
455    int palette_interpolation;
456    double hsv_rgb_palette_blend;
457    int num_xforms;
458    int final_xform_index;
459    int final_xform_enable;
460    flam3_xform *xform;
461 
462    /* Xaos implementation */
463    double **chaos;
464    int chaos_enable;
465 
466    int genome_index;                   /* index into source file */
467    char parent_fname[flam3_parent_fn_len];   /* base filename where parent was located */
468    int symmetry;                /* 0 means none */
469    flam3_palette palette;
470    char *input_image;           /* preview/temporary! */
471    int  palette_index;
472    double brightness;           /* 1.0 = normal */
473    double contrast;             /* 1.0 = normal */
474    double gamma;
475    double highlight_power;
476    int  width, height;          /* of the final image */
477    int  spatial_oversample;
478    double center[2];             /* of camera */
479    double rot_center[2];         /* really the center */
480    double rotate;                /* camera */
481    double vibrancy;              /* blend between color algs (0=old,1=new) */
482    double hue_rotation;          /* applies to cmap, 0-1 */
483    double background[3];
484    double zoom;                  /* effects ppu, sample density, scale */
485    double pixels_per_unit;       /* vertically */
486    double spatial_filter_radius; /* radius of spatial filter */
487    int spatial_filter_select; /* selected spatial filter */
488 //   double (*spatial_filter_func)(double); /* spatial filter kernel function */
489 //   double spatial_filter_support; /* size of standard kernel for specific function */
490    double sample_density;        /* samples per pixel (not bucket) */
491    /* in order to motion blur more accurately we compute the logs of the
492    sample density many times and average the results. */
493    /* nbatches is the number of times the buckets are filtered into
494    the abucket log accumulator */
495    /* ntemporal_samples is the number of time steps per batch.  this many
496    interpolated control points are used per batch and accumulated */
497    int nbatches;
498    int ntemporal_samples;
499 
500    /* Density estimation parameters for blurring low density hits */
501    double estimator;             /* Filter width for bin with one hit */
502    double estimator_curve;              /* Exponent on decay function ( MAX / a^(k-1) ) */
503    double estimator_minimum;         /* Minimum filter width used -
504                                     forces filter to be used of at least this width on all pts */
505 
506    /* XML Edit structure */
507    xmlDocPtr edits;
508 
509    /* Small-gamma linearization threshold */
510    double gam_lin_thresh;
511 
512    /* for cmap_interpolated hack */
513    int palette_index0;
514    double hue_rotation0;
515    int palette_index1;
516    double hue_rotation1;
517    double palette_blend;
518 
519    int temporal_filter_type; /* Temporal filters */
520    double temporal_filter_width, temporal_filter_exp;
521 
522    int palette_mode;
523 
524 
525 } flam3_genome;
526 
527 typedef struct {
528 	int from;
529 	int to;
530 	double scalar;
531 } flam3_chaos_entry;
532 
533 /* xform manipulation */
534 
535 void flam3_add_motion_element(flam3_xform *xf);
536 void flam3_add_xforms(flam3_genome *cp, int num_to_add, int interp_padding, int final_flag);
537 void flam3_delete_xform(flam3_genome *thiscp, int idx_to_delete);
538 void flam3_copy_xform(flam3_xform *dest, flam3_xform *src);
539 void flam3_copy(flam3_genome *dest, flam3_genome *src);
540 void flam3_copyx(flam3_genome *dest, flam3_genome *src, int num_std, int num_final);
541 void flam3_copy_params(flam3_xform *dest, flam3_xform *src, int varn);
542 void flam3_delete_motion_elements(flam3_xform *xf);
543 
544 EXPORT int flam3_xform_preview(flam3_genome *cp, int xi, double range, int numvals, int depth, double *result, randctx *rc);
545 EXPORT unsigned short* flam3_create_xform_distrib(flam3_genome *cp);
546 int flam3_create_chaos_distrib(flam3_genome *cp, int xi, unsigned short *xform_distrib);
547 int flam3_check_unity_chaos(flam3_genome *cp);
548 void clear_cp(flam3_genome *cp, int def_flag);
549 
550 /* samples is array nsamples*4 long of x,y,color triples.
551    using (samples[0], samples[1]) as starting XY point and
552    (samples[2], samples[3]) as starting color coordinate,
553    perform fuse iterations and throw them away, then perform
554    nsamples iterations and save them in the samples array */
555 EXPORT int flam3_iterate(flam3_genome *g, int nsamples, int fuse, double *samples,
556                      unsigned short *xform_distrib, randctx *rc);
557 
558 void apply_motion_parameters(flam3_xform *xf, flam3_xform *addto, double blend);
559 
560 /* genomes is array ngenomes long, with times set and in ascending order.
561    interpolate to the requested time and return in result */
562 EXPORT void flam3_interpolate(flam3_genome *genomes, int ngenomes, double time, double stagger, flam3_genome *result);
563 
564 /* print genome to given file with extra_attributes if not NULL */
565 EXPORT void flam3_print(FILE *f, flam3_genome *g, char *extra_attributes, int print_edits);
566 void flam3_print_xform(FILE *f, flam3_xform *x, int final_flag, int numstd, double *chaos_row, int motion_flag);
567 EXPORT char *flam3_print_to_string(flam3_genome *cp);
568 
569 /* ivars is a list of variations to use, or flam3_variation_random     */
570 /* ivars_n is the number of values in ivars to select from.            */
571 /* sym is either a symmetry group or 0 meaning random or no symmetry   */
572 /* spec_xforms specifies the number of xforms to use, setting to 0 makes the number random. */
573 EXPORT void flam3_random(flam3_genome *g, int *ivars, int ivars_n, int sym, int spec_xforms);
574 
575 void add_to_action(char *action, char *addtoaction);
576 
577 EXPORT void flam3_mutate(flam3_genome *cp, int mutate_mode, int *ivars, int ivars_n, int sym, double speed, randctx *rc, char *action);
578 EXPORT void flam3_cross(flam3_genome *cp0, flam3_genome *cp1, flam3_genome *out, int cross_mode, randctx *rc, char *action);
579 
580 /* return NULL in case of error */
581 EXPORT flam3_genome *flam3_parse_xml2(char *s, char *fn, int default_flag, int *ncps);
582 flam3_genome *flam3_parse_from_file(FILE *f, char *fn, int default_flag, int *ncps);
583 
584 void flam3_add_symmetry(flam3_genome *g, int sym);
585 
586 void flam3_improve_colors(flam3_genome *g, int ntries, int change_palette, int color_resolution);
587 EXPORT int flam3_colorhist(flam3_genome *cp, int num_batches, randctx *rc, double *hist);
588 EXPORT int flam3_estimate_bounding_box(flam3_genome *g, double eps, int nsamples,
589              double *bmin, double *bmax, randctx *rc);
590 void flam3_rotate(flam3_genome *g, double angle, int interp_type); /* angle in degrees */
591 
592 double flam3_dimension(flam3_genome *g, int ntries, int clip_to_camera);
593 double flam3_lyapunov(flam3_genome *g, int ntries);
594 
595 void flam3_apply_template(flam3_genome *cp, flam3_genome *templ);
596 
597 EXPORT int flam3_count_nthreads(void);
598 
599 typedef struct {
600 //   double         temporal_filter_radius;
601    double         pixel_aspect_ratio;    /* width over height of each pixel */
602    flam3_genome  *genomes;
603    int            ngenomes;
604    int            verbose;
605    int            bits;
606    int            bytes_per_channel;
607    int            earlyclip;
608    double         time;
609    int            (*progress)(void *, double, int, double);
610    void          *progress_parameter;
611    randctx       rc;
612    int           nthreads;
613    int           sub_batch_size;
614 } flam3_frame;
615 
616 
617 #define flam3_field_both  0
618 #define flam3_field_even  1
619 #define flam3_field_odd   2
620 
621 /* out is pixel array.
622    pixels are rgb or rgba if nchan is 3 or 4. */
623 EXPORT int flam3_render(flam3_frame *f, void *out, int field, int nchan, int transp, stat_struct *stats);
624 
625 EXPORT double flam3_render_memory_required(flam3_frame *f);
626 EXPORT int flam3_make_strip(flam3_genome *cp, int nstrips, int stripnum);
627 void rotate_by(double *p, double *center, double by);
628 
629 
630 double flam3_random01();
631 double flam3_random11();
632 int flam3_random_bit();
633 
634 /* ISAAC random numbers */
635 double flam3_random_isaac_01(randctx *);
636 double flam3_random_isaac_11(randctx *);
637 int flam3_random_isaac_bit(randctx *);
638 
639 EXPORT void flam3_init_frame(flam3_frame *f);
640 
641 /* External memory helpers */
642 EXPORT void *flam3_malloc(size_t size);
643 EXPORT void flam3_free(void *ptr);
644 
645 void flam3_srandom();
646 
647 flam3_genome *sheep_loop(flam3_genome *cp, double blend);
648 flam3_genome *sheep_edge(flam3_genome *cp, double blend, int seqflag, double stagger);
649 
650 /* Motion function indices */
651 #define MOTION_SIN 1
652 #define MOTION_TRIANGLE 2
653 #define MOTION_HILL 3
654 
655 /* Mutation modes */
656 #define MUTATE_NOT_SPECIFIED   -1
657 #define MUTATE_ALL_VARIATIONS  0
658 #define MUTATE_ONE_XFORM_COEFS 1
659 #define MUTATE_ADD_SYMMETRY    2
660 #define MUTATE_POST_XFORMS     3
661 #define MUTATE_COLOR_PALETTE   4
662 #define MUTATE_DELETE_XFORM    5
663 #define MUTATE_ALL_COEFS       6
664 
665 /* Cross modes */
666 #define CROSS_NOT_SPECIFIED   -1
667 #define CROSS_UNION           0
668 #define CROSS_INTERPOLATE     1
669 #define CROSS_ALTERNATE       2
670 
671 /* Filters */
672 /* Spatial filter kernels */
673 #define flam3_gaussian_kernel 0
674 #define flam3_hermite_kernel 1
675 #define flam3_box_kernel 2
676 #define flam3_triangle_kernel 3
677 #define flam3_bell_kernel 4
678 #define flam3_b_spline_kernel 5
679 #define flam3_lanczos3_kernel 6
680 #define flam3_lanczos2_kernel 7
681 #define flam3_mitchell_kernel 8
682 #define flam3_blackman_kernel 9
683 #define flam3_catrom_kernel 10
684 #define flam3_hamming_kernel 11
685 #define flam3_hanning_kernel 12
686 #define flam3_quadratic_kernel 13
687 
688 /* Temporal filters */
689 #define flam3_temporal_box 0
690 #define flam3_temporal_gaussian 1
691 #define flam3_temporal_exp 2
692 
693 
694 #endif
695