-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractal.c
1219 lines (1110 loc) · 38.5 KB
/
fractal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* QEmacs, character based fractal rendering
*
* Copyright (c) 2017 Charlie Gordon.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <math.h>
#include "qe.h"
#include "qfribidi.h"
#include "variables.h"
/*---------------- Fractint formula syntax ----------------*/
static const char fractint_keywords[] = {
"if|else|elseif|endif|pixel"
};
static const char fractint_types[] = {
""
};
enum {
FRACTINT_STYLE_DEFAULT = 0,
FRACTINT_STYLE_PREPROCESS = QE_STYLE_PREPROCESS,
FRACTINT_STYLE_COMMENT = QE_STYLE_COMMENT,
FRACTINT_STYLE_DEFINITION = QE_STYLE_TYPE,
FRACTINT_STYLE_NUMBER = QE_STYLE_NUMBER,
FRACTINT_STYLE_COLORS = QE_STYLE_STRING,
FRACTINT_STYLE_KEYWORD = QE_STYLE_KEYWORD,
FRACTINT_STYLE_FUNCTION = QE_STYLE_FUNCTION,
FRACTINT_STYLE_STRING = QE_STYLE_STRING,
FRACTINT_STYLE_STRING_Q = QE_STYLE_STRING_Q,
FRACTINT_STYLE_TYPE = QE_STYLE_TYPE,
};
/* fractint-mode colorization states */
enum {
IN_FRACTINT_COMMENT = 0x01, /* inside multiline comment */
IN_FRACTINT_BLOCK = 0x02, /* inside definition block */
IN_FRACTINT_COLORS = 0x04, /* inside a color palette definition */
IN_FRACTINT_STRING = 0x10, /* double-quoted string */
IN_FRACTINT_STRING_Q = 0x20, /* single-quoted string */
};
static void fractint_colorize_line(QEColorizeContext *cp,
unsigned int *str, int n, ModeDef *syn)
{
int i = 0, start, indent, c, state, state1, style, klen, delim;
char kbuf[64];
for (indent = 0; qe_isblank(str[indent]); indent++)
continue;
state = cp->colorize_state;
start = i;
c = 0;
style = FRACTINT_STYLE_DEFAULT;
if (i >= n)
goto the_end;
if (state) {
/* if already in a state, go directly in the code parsing it */
if (state & IN_FRACTINT_COMMENT)
goto parse_comment;
if (state & IN_FRACTINT_COLORS) {
i = indent;
goto parse_colors;
}
if (state & IN_FRACTINT_STRING)
goto parse_string;
if (state & IN_FRACTINT_STRING_Q)
goto parse_string_q;
}
while (i < n) {
start = i;
c = str[i++];
switch (c) {
case ';': /* line comment */
style = FRACTINT_STYLE_COMMENT;
i = n;
break;
case ':': /* iteration definition */
style = FRACTINT_STYLE_KEYWORD;
break;
case '\'': /* character constant */
parse_string_q:
state1 = IN_FRACTINT_STRING_Q;
style = FRACTINT_STYLE_STRING_Q;
delim = '\'';
goto string;
case '\"': /* string literal */
parse_string:
state1 = IN_FRACTINT_STRING;
style = FRACTINT_STYLE_STRING;
delim = '\"';
string:
while (i < n) {
c = str[i++];
if (c == '\\') {
if (i >= n) {
/* continuation line */
state |= state1;
break;
}
i++;
} else
if (c == delim) {
break;
}
}
break;
case '{':
if (state & IN_FRACTINT_BLOCK) {
/* a '{' inside a definition seems to start a comment */
/* consider it part of the comment, otherwise skip it */
goto parse_comment;
}
if (str[i] == '-' || str[i] == '=') {
/* invalid block, parse as comment */
start++;
goto parse_comment;
}
state |= IN_FRACTINT_BLOCK;
break;
case '}':
state &= ~IN_FRACTINT_COMMENT;
state &= ~IN_FRACTINT_BLOCK;
break;
case ' ':
case '\t':
case '\r':
continue;
default:
if (!(state & IN_FRACTINT_BLOCK)) {
klen = 0;
kbuf[klen++] = qe_tolower(c);
while (i < n && str[i] != '{') {
if (str[i] != ' ' && klen < countof(kbuf) - 1)
kbuf[klen++] = qe_tolower(str[i]);
i++;
}
if (kbuf[klen - 1] == '=')
klen--;
kbuf[klen] = '\0';
if (i >= n) {
style = FRACTINT_STYLE_COMMENT;
break;
}
if (strequal(kbuf, "comment")) {
SET_COLOR(str, start, i, FRACTINT_STYLE_PREPROCESS);
start = i + 1;
parse_comment:
state |= IN_FRACTINT_COMMENT;
for (; i < n; i++) {
if (str[i] == '}') {
break;
}
}
style = FRACTINT_STYLE_COMMENT;
} else {
eb_add_property(cp->b, cp->offset + start,
QE_PROP_TAG, qe_strdup(kbuf));
style = FRACTINT_STYLE_DEFINITION;
}
break;
}
if (c == '.' || qe_isdigit(c)) {
int j;
// Integers:
// 0x[0-9a-fA-F]+ //
// [0-9][0-9]* //
// Floats:
// [0-9][0-9]*\.[0-9]*([eE][-\+]?[0-9]+)?
// [0-9][0-9]*(\.[0-9]*)?[eE][-\+]?[0-9]+
// number suffixes:
if (c == '0' && str[i] == 'x' && qe_isxdigit(str[i + 1])) {
for (i += 3; qe_isxdigit(str[i]); i++)
continue;
} else {
while (qe_isdigit(str[i]))
i++;
if (c != '.' && str[i] == '.' && qe_isdigit(str[i + 1])) {
for (i += 2; qe_isdigit(str[i]); i++)
continue;
}
if (str[i] == 'e' || str[i] == 'E') {
j = i + 1;
if (str[j] == '+' || str[j] == '-')
j++;
if (qe_isdigit(str[j])) {
for (i = j + 1; qe_isdigit(str[i]); i++)
continue;
}
}
}
if (str[i] == 'i' || str[i] == 'I') {
/* imaginary number */
i++;
}
if (!qe_isalpha_(str[i])) {
style = FRACTINT_STYLE_NUMBER;
break;
}
i = start + 1;
}
if (qe_isalpha_(c)) {
/* identifiers match:
* "[a-zA-Z_\x80-\xff][a-zA-Z_0-9\x80-\xff]*"
*/
klen = 0;
kbuf[klen++] = qe_tolower(c);
while (qe_isalnum_(str[i]) || str[i] == '.') {
if (klen < countof(kbuf) - 1)
kbuf[klen++] = qe_tolower(str[i]);
i++;
}
kbuf[klen] = '\0';
if (strfind(syn->keywords, kbuf)) {
style = FRACTINT_STYLE_KEYWORD;
break;
}
if (strfind(syn->types, kbuf)) {
style = FRACTINT_STYLE_TYPE;
break;
}
if (check_fcall(str, i)) {
/* function call */
style = FRACTINT_STYLE_FUNCTION;
break;
}
if (strequal(kbuf, "colors") && str[i] == '=') {
start = ++i;
parse_colors:
state &= ~IN_FRACTINT_COLORS;
for (; i < n; i++) {
c = str[i];
if (!qe_isalnum_(c) && c != '`' && c != '<' && c != '>')
break;
}
if (i == n - 1 && str[i] == '\\') {
state |= IN_FRACTINT_COLORS;
i++;
}
style = FRACTINT_STYLE_COLORS;
break;
}
break;
}
continue;
}
if (style) {
SET_COLOR(str, start, i, style);
style = 0;
}
}
the_end:
/* set style on eol char */
SET_COLOR1(str, n, style);
cp->colorize_state = state;
}
static int fractint_mode_probe(ModeDef *mode, ModeProbeData *pd)
{
if (match_extension(pd->filename, mode->extensions)) {
/* This is a quick and dirty hack: assume Fractint formula
* files are located somewhere below a directory with a
* name relating to fractals.
*/
if (strstr(pd->real_filename, "frac")) {
/* Favor Fractint mode for formula files */
return 82;
} else {
/* Favor Visual Basic Form mode */
return 78;
}
}
return 1;
}
static ModeDef fractint_mode = {
.name = "Fractint",
.extensions = "frm|par|ifs|l",
.mode_probe = fractint_mode_probe,
.colorize_func = fractint_colorize_line,
.keywords = fractint_keywords,
.types = fractint_types,
.fallback = &c_mode,
};
/*---------------- Interactive fractal explorer ----------------*/
#define USE_BITMAP_API 0 /* Using device bitmap API */
#define USE_DRAW_PICTURE 1 /* Using qe_draw_picture() */
static ModeDef fractal_mode;
#if 1
typedef long double fnum_t;
#define MFT "%.21Lg"
#else
typedef double fnum_t;
#define MFT "%.16Lg"
#endif
typedef struct { fnum_t a, b; } cnum_t;
typedef struct FractalState FractalState;
struct FractalState {
QEModeData base;
int width, height; /* fractal size in pixels */
int type; /* fractal type 0..8 */
int maxiter; /* maximum iteration number */
int cb, nc; /* color palette base and length */
int rot; /* rotation in degrees */
int zoom; /* zoom level in dB */
fnum_t scale; /* zoom factor = pow(10, -mzoom/10) */
fnum_t bailout; /* maximum squared module (default 4.0) */
fnum_t x, y; /* center position */
fnum_t m0, m1, m2, m3; /* rotation matrix */
int shift; /* color animation base */
QEColor colors[256]; /* color palette */
QEditScreen *screen; /* for bmp_free() */
QEBitmap *disp_bmp; /* device image */
#if USE_DRAW_PICTURE
QEPicture *ip;
#endif
};
const char fractal_default_parameters[] = {
" type=0"
" maxiter=215"
" rot=0"
" zoom=0"
" bailout=4"
// This place zooms to level 180, scale=1e-18:
" x=-0.747698434201463097446 y=0.0794508470293983774563"
// This place on the X axis is interesting:
//" x=-1.78935604483808219844, y=0"
};
static inline FractalState *fractal_get_state(EditState *e, int status)
{
return qe_get_buffer_mode_data(e->b, &fractal_mode, status ? e : NULL);
}
static fnum_t cmod2(cnum_t z) {
return z.a * z.a + z.b * z.b;
}
static cnum_t cpower(cnum_t z, int exp) {
cnum_t r = { 1, 0 };
fnum_t a;
while (exp > 0) {
if (exp & 1) {
a = r.a;
r.a = a * z.a - r.b * z.b;
r.b = a * z.b + r.b * z.a;
}
exp >>= 1;
a = z.a;
z.a = a * a - z.b * z.b;
z.b = 2 * a * z.b;
}
return r;
}
static int mandelbrot_func(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) {
fnum_t a, b, c;
int i;
for (a = b = 0, i = maxiter; a * a + b * b <= bailout && --i > 0;) {
c = a;
a = a * a - b * b + x;
b = 2 * c * b + y;
}
return maxiter - i;
}
static int mandelbrot3_func(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) {
fnum_t a, b, c;
int i;
for (a = b = 0, i = maxiter; a * a + b * b <= bailout && --i > 0;) {
c = a;
a = a * a * a - 3 * a * b * b + x;
b = 3 * c * c * b - b * b * b + y;
}
return maxiter - i;
}
static int mandelbrot4_func(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) {
fnum_t a, b, a2, b2;
int i;
for (a = b = 0, i = maxiter; a * a + b * b <= bailout && --i > 0;) {
a2 = a * a - b * b;
b2 = 2 * a * b;
a = a2 * a2 - b2 * b2 + x;
b = 2 * a2 * b2 + y;
}
return maxiter - i;
}
static int mandelbrot5_func(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) {
fnum_t a, b, a2, b2, a3, b3;
int i;
for (a = b = 0, i = maxiter; a * a + b * b <= bailout && --i > 0;) {
a3 = a * a * a - 3 * a * b * b;
b3 = 3 * a * a * b - b * b * b;
a2 = a * a - b * b;
b2 = 2 * a * b;
a = a2 * a3 - b2 * b3 + x;
b = b2 * a3 + a2 * b3 + y;
}
return maxiter - i;
}
static int mandelbrot6_func(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) {
fnum_t a, b, a3, b3;
int i;
for (a = b = 0, i = maxiter; a * a + b * b <= bailout && --i > 0;) {
a3 = a * a * a - 3 * a * b * b;
b3 = 3 * a * a * b - b * b * b;
a = a3 * a3 - b3 * b3 + x;
b = 2 * a3 * b3 + y;
}
return maxiter - i;
}
static int mandelbrot7_func(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) {
cnum_t z = { 0, 0 };
int i;
for (i = maxiter; cmod2(z) <= bailout && --i > 0;) {
z = cpower(z, 7);
z.a += x;
z.b += y;
}
return maxiter - i;
}
static int mandelbrot8_func(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) {
cnum_t z = { 0, 0 };
int i;
for (i = maxiter; cmod2(z) <= bailout && --i > 0;) {
z = cpower(z, 8);
z.a += x;
z.b += y;
}
return maxiter - i;
}
static int mandelbrot9_func(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) {
cnum_t z = { 0, 0 };
int i;
for (i = maxiter; cmod2(z) <= bailout && --i > 0;) {
z = cpower(z, 9);
z.a += x;
z.b += y;
}
return maxiter - i;
}
static int mandelbrot10_func(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) {
cnum_t z = { 0, 0 };
int i;
for (i = maxiter; cmod2(z) <= bailout && --i > 0;) {
z = cpower(z, 10);
z.a += x;
z.b += y;
}
return maxiter - i;
}
static struct FractalType {
const char *name;
const char *formula;
int (*func)(fnum_t x, fnum_t y, fnum_t bailout, int maxiter);
} const fractal_type[] = {
{ "Mandelbrot", "z=z^2+c", mandelbrot_func },
{ "Mandelbrot3", "z=z^3+c", mandelbrot3_func },
{ "Mandelbrot4", "z=z^4+c", mandelbrot4_func },
{ "Mandelbrot5", "z=z^5+c", mandelbrot5_func },
{ "Mandelbrot6", "z=z^6+c", mandelbrot6_func },
{ "Mandelbrot7", "z=z^7+c", mandelbrot7_func },
{ "Mandelbrot8", "z=z^8+c", mandelbrot8_func },
{ "Mandelbrot9", "z=x^9+c", mandelbrot9_func },
{ "Mandelbrot10", "z=x^10+c", mandelbrot10_func },
};
static void fractal_invalidate(FractalState *ms) {
/* This will force fractal image recomputation */
/* XXX: color changes should not cause recomputation
if the fractal is computed as a paletted image */
ms->width = ms->height = 0;
}
static void fractal_set_rotation(FractalState *ms, int rot) {
ms->rot = rot;
/* compute rotation matrix */
ms->m0 = cos(-ms->rot * M_PI / 180.0);
ms->m1 = sin(-ms->rot * M_PI / 180.0);
ms->m2 = -ms->m1;
ms->m3 = ms->m0;
fractal_invalidate(ms);
}
static void fractal_set_zoom(FractalState *ms, int level) {
ms->zoom = level;
ms->scale = pow(10.0, -ms->zoom / 10.0);
fractal_invalidate(ms);
}
static int strmatchword(const char *str, const char *val, const char **ptr) {
if (strstart(str, val, &str) && !qe_isword(*str)) {
if (ptr)
*ptr = str;
return 1;
}
return 0;
}
static int fractal_get_color(const char *p, int *dac) {
/* convert a fractint 3 character color spec: 0-9A-Z_-z -> 0..63 */
int i;
for (i = 0; i < 3; i++) {
int c = p[i];
if (c >= '0' && c <= '9')
c -= '0';
else
if (c >= 'A' && c <= 'Z')
c -= 'A' - 10;
else
if (c >= '_' && c <= 'z')
c -= '_' - 36;
else
return 0;
dac[i] = (c << 2) | (c >> 4);
}
return 1;
}
static int fractal_set_colors(FractalState *ms, const char *p, const char **pp) {
/* Set the default colors */
memcpy(ms->colors, xterm_colors, 256 * sizeof(*ms->colors));
ms->cb = 16;
ms->nc = 216;
fractal_invalidate(ms);
if (p) {
if (strmatchword(p, "gray256", pp)) {
int c;
for (c = 1; c < 256; c++) {
ms->colors[256 - c] = QERGB(c, c, c);
}
ms->cb = 1;
ms->nc = 255;
} else
if (strmatchword(p, "gray", pp)) {
ms->cb = 232;
ms->nc = 24;
} else
if (!strmatchword(p, "default", pp)) {
int i, j, n;
int last[3] = { 0, 0, 0 }, dac[3] = { 0, 0, 0 };
/* parse a color fractint colors spec */
for (i = 0; i < 256; i++) {
if (!*p || *p == ',' || *p == ' ')
break;
n = 0;
if (*p == '<') {
if (i == 0)
return 0;
n = clamp(strtol(p + 1, (char **)&p, 10), 1, 255 - i);
if (*p != '>')
return 0;
p++;
memcpy(last, dac, sizeof dac);
i += n;
}
if (!fractal_get_color(p, dac)) {
return 0;
}
p += 3;
for (j = 1, ++n; j < n; j++) {
ms->colors[i - j] = QERGB((last[0] * j + dac[0] * (n - j)) / n,
(last[1] * j + dac[1] * (n - j)) / n,
(last[2] * j + dac[2] * (n - j)) / n);
}
ms->colors[i] = QERGB(dac[0], dac[1], dac[2]);
}
if (pp)
*pp = p;
ms->cb = 1;
ms->nc = i - ms->cb;
}
}
return 1;
}
static void fractal_set_parameters(EditState *s, FractalState *ms, const char *parms)
{
const char *p;
ms->width = ms->height = 0; /* force redraw */
for (p = parms;;) {
p += strspn(p, ";, \t\r\n");
if (*p == '\0')
break;
if (strstart(p, "type=", &p)) {
ms->type = clamp(strtol(p, (char **)&p, 0), 0, countof(fractal_type));
} else
if (strstart(p, "maxiter=", &p)) {
ms->maxiter = strtol(p, (char **)&p, 0);
} else
if (strstart(p, "colors=", &p)) {
if (!fractal_set_colors(ms, p, &p)) {
put_status(s, "invalid colors: %s", p);
p += strcspn(p, ", ");
}
} else
if (strstart(p, "cb=", &p)) {
ms->cb = strtol(p, (char **)&p, 0);
} else
if (strstart(p, "nc=", &p)) {
ms->nc = strtol(p, (char **)&p, 0);
} else
if (strstart(p, "shift=", &p)) {
ms->shift = strtol(p, (char **)&p, 0);
} else
if (strstart(p, "rot=", &p)) {
fractal_set_rotation(ms, strtol(p, (char **)&p, 0));
} else
if (strstart(p, "zoom=", &p)) {
fractal_set_zoom(ms, strtol(p, (char **)&p, 0));
} else
if (strstart(p, "bailout=", &p)) {
ms->bailout = strtold(p, (char **)&p);
} else
if (strstart(p, "x=", &p)) {
ms->x = strtold(p, (char **)&p);
} else
if (strstart(p, "y=", &p)) {
ms->y = strtold(p, (char **)&p);
} else {
put_status(s, "invalid parameter: %s", p);
break;
}
}
}
static void do_fractal_draw(EditState *s, FractalState *ms)
{
#if USE_BITMAP_API
int width = ms->width, height = ms->height;
int maxiter = ms->maxiter + ms->zoom, nc = ms->nc;
int i, nx, ny, shift;
fnum_t x, y, dx, dy;
int (*func)(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) =
fractal_type[ms->type].func;
unsigned char *palette8 = NULL;
uint32_t *palette32 = NULL;
QEPicture pict;
if (s->width == 0 || s->height == 0 || width == 0 || height == 0 || nc == 0)
return;
if (s->width == s->cols) {
/* character based, assume 80x25 4/3 aspect ratio, 2 pixels per char */
dx = 3.2 * ms->scale / width;
dy = dx * 1.2;
} else {
/* pixel based, assume 100% pixel aspect ratio */
dy = dx = 3.2 * ms->scale / width;
}
if (ms->disp_bmp == NULL
|| ms->disp_bmp->width != width
|| ms->disp_bmp->height != height) {
bmp_free(ms->screen, &ms->disp_bmp);
/* create the displayed bitmap and put the image in it */
ms->screen = s->screen;
ms->disp_bmp = bmp_alloc(ms->screen, width, height, 0);
}
if (!ms->disp_bmp)
return;
bmp_lock(ms->screen, ms->disp_bmp, &pict, 0, 0, width, height);
/* Compute shifted palette */
shift = nc + ms->shift % nc; /* 0 < shift < 2 * nc */
if (pict.format == QEBITMAP_FORMAT_8BIT) {
palette8 = qe_malloc_array(unsigned char, maxiter + 1);
for (i = 0; i <= maxiter; i++) {
palette8[i] = (i >= maxiter) ? 0 : (ms->cb + (i + shift) % nc) & 255;
}
} else
if (pict.format == QEBITMAP_FORMAT_RGBA32) {
palette32 = qe_malloc_array(uint32_t, maxiter + 1);
for (i = 0; i <= maxiter; i++) {
palette32[i] = ms->colors[(i >= maxiter) ? 0 : (ms->cb + (i + shift) % nc) & 255];
}
}
/* Compute fractal bitmap */
for (ny = 0, y = -dy * height / 2; ny < height; ny++, y += dy) {
if (pict.format == QEBITMAP_FORMAT_8BIT) {
unsigned char *pb = pict.data[0] + ny * pict.linesize[0];
for (nx = 0, x = -dx * width / 2; nx < width; nx++, x += dx) {
fnum_t xr = ms->x + x * ms->m0 + y * ms->m1;
fnum_t yr = ms->y + x * ms->m2 + y * ms->m3;
pb[nx] = palette8[(*func)(xr, yr, ms->bailout, maxiter)];
}
} else
if (pict.format == QEBITMAP_FORMAT_RGBA32) {
uint32_t *pb = (uint32_t *)(void*)(pict.data[0] + ny * pict.linesize[0]);
for (nx = 0, x = -dx * width / 2; nx < width; nx++, x += dx) {
fnum_t xr = ms->x + x * ms->m0 + y * ms->m1;
fnum_t yr = ms->y + x * ms->m2 + y * ms->m3;
pb[nx] = palette32[(*func)(xr, yr, ms->bailout, maxiter)];
}
}
}
bmp_unlock(ms->screen, ms->disp_bmp);
qe_free(&palette8);
qe_free(&palette32);
edit_invalidate(s, 1);
#elif USE_DRAW_PICTURE
int width = ms->width, height = ms->height, zoom = ms->zoom;
int maxiter = ms->maxiter + zoom, cb = ms->cb, nc = ms->nc;
int i, nx, ny;
fnum_t xc = ms->x, yc = ms->y, scale = ms->scale;
fnum_t bailout = ms->bailout;
fnum_t x, y, dx, dy, xr, yr;
int (*func)(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) =
fractal_type[ms->type].func;
if (s->width == 0 || s->height == 0 || width == 0 || height == 0 || nc == 0)
return;
if (s->width == s->cols) {
/* character based, assume 80x25 4/3 aspect ratio, 2 pixels per char */
dx = 3.2 * scale / width;
dy = dx * 1.2;
} else {
/* pixel based, assume 100% pixel aspect ratio */
dy = dx = 3.2 * scale / width;
}
if (ms->ip == NULL || ms->ip->width != width || ms->ip->height != height) {
qe_free_picture(&ms->ip);
ms->ip = qe_create_picture(width, height, QEBITMAP_FORMAT_8BIT, 0);
}
if (!ms->ip)
return;
for (ny = 0, y = -dy * height / 2; ny < height; ny++, y += dy) {
unsigned char *pb = ms->ip->data[0] + ny * ms->ip->linesize[0];
for (nx = 0, x = -dx * width / 2; nx < width; nx++, x += dx) {
xr = xc + x * ms->m0 + y * ms->m1;
yr = yc + x * ms->m2 + y * ms->m3;
i = (*func)(xr, yr, bailout, maxiter);
pb[nx] = (i >= maxiter) ? 0 : cb + i % nc;
}
}
edit_invalidate(s, 1);
#else
int width = ms->width, height = ms->height / 2;
int maxiter = ms->maxiter + ms->zoom, nc = ms->nc;
int i, nx, ny, fg, bg, shift;
fnum_t x, y, dx, dy;
int (*func)(fnum_t x, fnum_t y, fnum_t bailout, int maxiter) =
fractal_type[ms->type].func;
unsigned char *palette8 = NULL;
if (s->width == 0 || s->height == 0 || width == 0 || height == 0 || nc == 0)
return;
dx = 3.2 * ms->scale / width;
if (s->width == s->cols) {
/* character based, assume 80x25 4/3 aspect ratio, 2 pixels per char */
dy = dx * 2.4;
} else {
/* pixel based, assume 100% pixel aspect ratio */
dy = dx * width / s->width * s->height / height;
}
/* Compute shifted palette */
shift = nc + ms->shift % nc; /* 0 < shift < 2 * nc */
palette8 = qe_malloc_array(unsigned char, maxiter + 1);
for (i = 0; i <= maxiter; i++) {
palette8[i] = (i >= maxiter) ? 0 : (ms->cb + (i + shift) % nc) & 255;
}
s->b->flags &= ~BF_READONLY;
eb_delete_range(s->b, 0, s->b->total_size);
for (ny = 0, y = -dy * height / 2; ny < height; ny++, y += dy) {
for (nx = 0, x = -dx * width / 2; nx < width; nx++, x += dx) {
fnum_t xr = ms->x + x * ms->m0 + y * ms->m1;
fnum_t yr = ms->y + x * ms->m2 + y * ms->m3;
bg = palette8[(*func)(xr, yr, ms->bailout, maxiter)];
xr += dy / 2 * ms->m1;
yr += dy / 2 * ms->m3;
fg = palette8[(*func)(xr, yr, ms->bailout, maxiter)];
s->b->cur_style = QE_TERM_COMPOSITE | QE_TERM_MAKE_COLOR(fg, bg);
eb_insert_uchar(s->b, s->b->total_size, fg == bg ? ' ' : 0x2584);
}
s->b->cur_style = QE_STYLE_DEFAULT;
eb_insert_uchar(s->b, s->b->total_size, '\n');
}
s->b->flags |= BF_READONLY;
qe_free(&palette8);
#endif
}
#if USE_BITMAP_API
static void fractal_display(EditState *s) {
FractalState *ms = fractal_get_state(s, 0);
QEColor col = qe_styles[QE_STYLE_GUTTER].bg_color;
if (s->display_invalid) {
if (ms && ms->disp_bmp) {
int w = min(s->width, ms->disp_bmp->width);
int h = min(s->height, ms->disp_bmp->height / s->screen->dpy.yfactor);
int x = (s->width - w) / 2;
int y = (s->height - h) / 2;
bmp_draw(s->screen, ms->disp_bmp,
s->xleft + x, s->ytop + y, w, h, 0, 0, 0);
fill_border(s, x, y, w, h, col);
} else {
fill_rectangle(s->screen, s->xleft, s->ytop, s->width, s->height, col);
}
s->display_invalid = 0;
}
if (s->qe_state->active_window == s) {
/* Update cursor */
int xc = s->xleft;
int yc = s->ytop;
int w = s->char_width;
int h = s->line_height;
if (s->screen->dpy.dpy_cursor_at) {
/* hardware cursor */
s->screen->dpy.dpy_cursor_at(s->screen, xc, yc, w, h);
} else {
xor_rectangle(s->screen, xc, yc, w, h, QERGB(0xFF, 0xFF, 0xFF));
}
}
}
#endif
#if USE_DRAW_PICTURE
static void fractal_display(EditState *s) {
FractalState *ms = fractal_get_state(s, 0);
QEColor col = qe_styles[QE_STYLE_GUTTER].bg_color;
if (s->display_invalid) {
if (ms && ms->ip) {
int w = min(s->width, ms->ip->width);
int h = min(s->height, ms->ip->height / s->screen->dpy.yfactor);
int x0 = (s->width - w) / 2;
int y0 = (s->height - h) / 2;
uint32_t palette[256];
int c;
palette[0] = ms->colors[0];
for (c = 1; c < 256; c++) {
palette[c] = ms->colors[(c + ms->shift) & 255];
}
ms->ip->palette = palette;
ms->ip->palette_size = 256;
qe_draw_picture(s->screen, s->xleft + x0, s->ytop + y0, w, h,
ms->ip, 0, 0, w, h * s->screen->dpy.yfactor,
0, QERGB(128, 128, 128));
ms->ip->palette = NULL;
fill_border(s, x0, y0, w, h, col);
} else {
fill_rectangle(s->screen, s->xleft, s->ytop, s->width, s->height, col);
}
s->display_invalid = 0;
}
if (s->qe_state->active_window == s) {
/* Update cursor */
int xc = s->xleft;
int yc = s->ytop;
int w = s->char_width;
int h = s->line_height;
if (s->screen->dpy.dpy_cursor_at) {
/* hardware cursor */
s->screen->dpy.dpy_cursor_at(s->screen, xc, yc, w, h);
} else {
xor_rectangle(s->screen, xc, yc, w, h, QERGB(0xFF, 0xFF, 0xFF));
}
}
}
#endif
static void do_fractal_move(EditState *s, int deltax, int deltay) {
FractalState *ms = fractal_get_state(s, 1);
if (ms) {
fnum_t dx = deltax * ms->scale / 40;
fnum_t dy = deltay * ms->scale / 40;
ms->x += dx * ms->m0 + dy * ms->m1;
ms->y += dx * ms->m2 + dy * ms->m3;
fractal_invalidate(ms);
}
}
static void do_fractal_move_x(EditState *s, int delta) {
do_fractal_move(s, delta, 0);
}
static void do_fractal_move_y(EditState *s, int delta) {
do_fractal_move(s, 0, delta);
}
static void do_fractal_zoom(EditState *s, int delta) {
FractalState *ms = fractal_get_state(s, 1);
if (ms) {
fractal_set_zoom(ms, ms->zoom + delta);
}
}
static void do_fractal_rotate(EditState *s, int delta) {
FractalState *ms = fractal_get_state(s, 1);
if (ms) {
fractal_set_rotation(ms, delta ? ms->rot + delta : 0);
}
}
static void do_fractal_shift_colors(EditState *s, int delta) {
FractalState *ms = fractal_get_state(s, 1);
if (ms) {
ms->shift += delta;
#if USE_BITMAP_API
fractal_invalidate(ms);
#else
edit_invalidate(s, 1);
#endif
}
}
static void do_fractal_set_colors(EditState *s, int type) {
FractalState *ms = fractal_get_state(s, 1);
if (ms) {
ms->shift = 0;
if (type == 0) {
fractal_set_colors(ms, NULL, NULL);
} else
if (type == 1) {
fractal_set_colors(ms, "gray", NULL);
}
#if USE_BITMAP_API
fractal_invalidate(ms);
#else
edit_invalidate(s, 1);
#endif
}
}
static void do_fractal_iter(EditState *s, int delta) {
FractalState *ms = fractal_get_state(s, 1);
if (ms) {
ms->maxiter += delta;
fractal_invalidate(ms);
}
}
static void do_fractal_bailout(EditState *s, int delta) {
FractalState *ms = fractal_get_state(s, 1);
if (ms) {
ms->bailout += delta;
fractal_invalidate(ms);
}
}
static void do_fractal_set_parameters(EditState *s, const char *params) {
FractalState *ms = fractal_get_state(s, 1);
if (ms) {