-
Notifications
You must be signed in to change notification settings - Fork 1
/
fractald.c
executable file
·1268 lines (1221 loc) · 27.4 KB
/
fractald.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
/* Sample assembly language program for the x86 CPU by
Mark Craig (mandelbrot and julia fractals)
https://www.youtube.com/MrMcSoftware */
/* I chose to use inline assembly because I didn't want to code the Windows
GUI stuff in assembly. My original assembly language fractal program was
written for a CPU I designed. That version was all assembly and was the
basis for this x86 version. */
#include <windows.h>
#include <stdio.h>
#include "dialog.h"
#define MANDELBROT 0
#define JULIA 1
/* Windows GUI variables */
HINSTANCE hin; /* instance handle */
WNDCLASS wc; /* window class */
HWND w = NULL; /* window */
HDC hdc, hdcimg = NULL; /* window DC handle, fractal bitmap DC handle */
HBITMAP himg = NULL; /* fractal bitmap handle */
POINTS pt0, pts, pte; /* zoom: old point, start point, end point */
char gcmd[256]; /* command-line argument */
/* Variables used in assembly part */
/* some of these variables are vestiges of my original assembly language
fractal program for my CPU design which this x86 version is based on.
That CPU design was simpler than x86, so these vars. were necessary. */
unsigned char im[512*512*3]; /* Image buffer */
double aedge1 = -2.0, bedge1 = -1.25, xdelta1 = 3.0, ydelta1 = 2.5;
double aedge2 = -1.061955193, bedge2 = -.264645193;
double xdelta2 = .000230386, ydelta2 = .000230386;
double aedge3 = -1.2125216, bedge3 = -.3355226;
double xdelta3 = .1620652, ydelta3 = .1620652;
double aedge4 = -1.5, bedge4 = -1.4, xdelta4 = 3.0, ydelta4 = 2.8;
double aedge5 = 0.33285461273437479100, bedge5 = 0.57068114093749985900;
double xdelta5 = 0.1395625000000000000e-04, ydelta5 = 0.1395625000000000000e-04;
double a1 = -.8, b1 = .156, a2 = -.742, b2 = .1, a3 = -.4, b3 = .6;
double a4 = .285, b4 = .01, a5 = 0.0, b5 = -.8, a6 = -.835, b6 = -.2321;
double a7 = -.1, b7 = .651, a8 = .3, b8 = 0.0, a = -.8, b = .156;
double xres1 = 512.0, yres1 = 512.0, xres = 512.0, yres = 512.0;
double xres2 = 256.0, yres2 = 256.0;
double one = 1.0, two = 2.0, converge = 4.0, popped;
double m, n, xgap, ygap, ac, bc, az, bz, sizev, temp, tempb;
double aedge, bedge, xdelta, ydelta;
int numpixels = 512*512*3, numpixels1 = 512*512*3, numpixels2 = 512*256*3;
int off = 0, ncols = 255, count, limit = 1023;
int pixel, cyinc = 2, cydir = 1;
int resadd = 0, resadd1 = 0, resadd2 = 256*3;
int imaged[512*512]; /* "count" buffer used for color cycling */
/* Rainbow colortable generated by HSV to RGB conversion (hue: 0 to 360) */
unsigned char Colors[256*3] = {
0,0,0,
255,6,0,
255,12,0,
255,18,0,
255,24,0,
255,30,0,
255,36,0,
255,42,0,
255,48,0,
255,54,0,
255,60,0,
255,66,0,
255,72,0,
255,78,0,
255,84,0,
255,90,0,
255,96,0,
255,102,0,
255,108,0,
255,114,0,
255,120,0,
255,126,0,
255,132,0,
255,138,0,
255,144,0,
255,150,0,
255,156,0,
255,162,0,
255,168,0,
255,174,0,
255,180,0,
255,186,0,
255,192,0,
255,198,0,
255,204,0,
255,210,0,
255,216,0,
255,222,0,
255,228,0,
255,234,0,
255,240,0,
255,246,0,
255,252,0,
252,255,0,
246,255,0,
239,255,0,
233,255,0,
227,255,0,
221,255,0,
215,255,0,
209,255,0,
203,255,0,
197,255,0,
191,255,0,
185,255,0,
179,255,0,
173,255,0,
167,255,0,
161,255,0,
155,255,0,
149,255,0,
143,255,0,
137,255,0,
131,255,0,
125,255,0,
119,255,0,
113,255,0,
107,255,0,
101,255,0,
95,255,0,
89,255,0,
83,255,0,
77,255,0,
71,255,0,
65,255,0,
59,255,0,
53,255,0,
47,255,0,
41,255,0,
35,255,0,
29,255,0,
23,255,0,
17,255,0,
11,255,0,
5,255,0,
0,255,0,
0,255,6,
0,255,12,
0,255,18,
0,255,24,
0,255,30,
0,255,36,
0,255,42,
0,255,48,
0,255,54,
0,255,60,
0,255,66,
0,255,72,
0,255,78,
0,255,84,
0,255,90,
0,255,96,
0,255,102,
0,255,108,
0,255,114,
0,255,120,
0,255,126,
0,255,132,
0,255,138,
0,255,144,
0,255,150,
0,255,156,
0,255,162,
0,255,168,
0,255,174,
0,255,180,
0,255,186,
0,255,192,
0,255,198,
0,255,204,
0,255,210,
0,255,216,
0,255,222,
0,255,228,
0,255,234,
0,255,240,
0,255,246,
0,255,252,
0,251,255,
0,245,255,
0,239,255,
0,233,255,
0,227,255,
0,221,255,
0,215,255,
0,209,255,
0,203,255,
0,197,255,
0,191,255,
0,185,255,
0,179,255,
0,173,255,
0,167,255,
0,161,255,
0,155,255,
0,149,255,
0,143,255,
0,137,255,
0,131,255,
0,125,255,
0,119,255,
0,113,255,
0,107,255,
0,101,255,
0,95,255,
0,89,255,
0,83,255,
0,77,255,
0,71,255,
0,65,255,
0,59,255,
0,53,255,
0,47,255,
0,41,255,
0,35,255,
0,29,255,
0,23,255,
0,17,255,
0,11,255,
0,5,255,
0,0,255,
6,0,255,
12,0,255,
18,0,255,
24,0,255,
30,0,255,
36,0,255,
42,0,255,
48,0,255,
54,0,255,
60,0,255,
66,0,255,
72,0,255,
78,0,255,
84,0,255,
90,0,255,
96,0,255,
102,0,255,
108,0,255,
114,0,255,
120,0,255,
126,0,255,
132,0,255,
138,0,255,
144,0,255,
150,0,255,
156,0,255,
162,0,255,
168,0,255,
174,0,255,
180,0,255,
186,0,255,
192,0,255,
198,0,255,
204,0,255,
210,0,255,
216,0,255,
222,0,255,
228,0,255,
234,0,255,
240,0,255,
246,0,255,
252,0,255,
255,0,251,
255,0,245,
255,0,239,
255,0,233,
255,0,227,
255,0,221,
255,0,215,
255,0,209,
255,0,203,
255,0,197,
255,0,191,
255,0,185,
255,0,179,
255,0,173,
255,0,167,
255,0,161,
255,0,155,
255,0,149,
255,0,143,
255,0,137,
255,0,131,
255,0,125,
255,0,119,
255,0,113,
255,0,107,
255,0,101,
255,0,95,
255,0,89,
255,0,83,
255,0,77,
255,0,71,
255,0,65,
255,0,59,
255,0,53,
255,0,47,
255,0,41,
255,0,35,
255,0,29,
255,0,23,
255,0,17,
255,0,11,
255,0,5,
255,0,5};
/* Variables used in C part */
double aedgeUndo, bedgeUndo, xdeltaUndo, ydeltaUndo; /* zoom undo vars */
int zoomgetpoint = 0, zoomtype, keepitsquare = 1; /* zoom vars */
int speed = 0, dspeed = 10, pause = 0, cycle = 0; /* color cycle vars */
int fractaltype;
/* Now the good stuff. BTW, I purposely chose to use basic x86 assembly -
no SSE*, no AVX-512, etc. */
void Mandelbrot()
{
__asm{
Mandelbrot: mov pixel,0
fld xdelta
fdiv xres
fstp xgap ; xgap=xdelta/xres
fld ydelta
fdiv yres
fstp ygap ; ygap=ydelta/yres
fldz
Loopm: fst m ; for (m=0;m<yres;m++)
fmul ygap
fadd bedge
fstp bc ; bc=m*ygap+bedge
fldz
Loopn: fst n ; for (n=0;n<xres;n++)
fmul xgap
fadd aedge
fstp ac ; ac=n*xgap+aedge
fldz
fst az ; az=0
fst bz ; bz=0
fstp sizev ; sizev=0 (size must be reserved word)
mov count,0 ; count=0
Loopw: fld sizev
fcomp converge
fnstsw ax
test ah,65
je Exitw
mov eax,limit
cmp count,eax ; was cmp count,limit when limit was #define
jg Exitw ; while (size<=converge && count<=limit)
fld az
fst temp ; temp=az
fmul az
fadd ac
fstp az
fld bz
fmul bz
fstp tempb
fld az
fsub tempb
fstp az ; az=az*az-bz*bz+ac
fld bz
fmul temp
fmul two
fadd bc
fstp bz ; bz=temp*bz*2+bc
add count,1 ; count++
fld az
fmul az
fstp tempb
fld bz
fmul bz
fadd tempb
fstp sizev ; size=(az*az+bz*bz)
jmp Loopw ; end while
Exitw: mov eax,limit
cmp count,eax
jle ok
mov count,0 ; if (count>limit) count=0
jmp ok2
ok: mov eax,off
add count,eax
mov eax,count ;
mov edx,0 ;
idiv ncols ; MODI 255
mov count,edx ;
add count,1 ; else count=(count+off)%255+1
ok2: mov eax,count
mov ebx,[pixel]
mov imaged[ebx],eax ; save count for color cycling (not aligned)
imul eax,3
add eax,2
mov cl,Colors[eax]
sub eax,1
mov ebx,[pixel]
mov im[ebx],cl
add pixel,1
mov cl,Colors[eax]
sub eax,1
mov ebx,[pixel]
mov im[ebx],cl
add pixel,1
mov cl,Colors[eax]
mov ebx,[pixel]
mov im[ebx],cl
add pixel,1
fld n
fadd one
fst n
fcom xres
fnstsw ax
test ah,1
jne Loopn ; end Loop n
mov eax,resadd
add pixel,eax
fstp popped
fld m
fadd one
fst m
fcom yres
fnstsw ax
test ah,1
jne Loopm ; end Loop m
fstp popped
;call Cycle
}
ChangeBitmap(himg, im);
BitBlt(hdc, 0, 0, 512, 512, hdcimg, 0, 0, SRCCOPY);
}
void Julia()
{
__asm{
Julia: mov pixel,0
fld xdelta
fdiv xres
fstp xgap ; xgap=xdelta/xres
fld ydelta
fdiv yres
fstp ygap ; ygap=ydelta/yres
fldz
Loopmj: fst m ; for (m=0;m<yres;m++)
fmul ygap
fadd bedge
fstp bc ; bc=m*ygap+bedge
fldz
Loopnj: fst n ; for (n=0;n<xres;n++)
fmul xgap
fadd aedge
fst ac ; ac=n*xgap+aedge
fstp az ; az=ac
fld bc
fstp bz ; bz=bc
fldz
fstp sizev ; sizev=0 (size must be reserved word)
mov count,0 ; count=0
Loopwj: fld sizev
fcomp converge
fnstsw ax
test ah,65
je Exitwj
mov eax,limit
cmp count,eax
jg Exitwj ; while (size<=converge && count<=limit)
fld az
fst temp ; temp=az
fmul az
fadd a
fstp az
fld bz
fmul bz
fstp tempb
fld az
fsub tempb
fstp az ; az=az*az-bz*bz+a
fld bz
fmul temp
fmul two
fadd b
fstp bz ; bz=temp*bz*2+b
add count,1 ; count++
fld az
fmul az
fstp tempb
fld bz
fmul bz
fadd tempb
fstp sizev ; size=(az*az+bz*bz)
jmp Loopwj ; end while
Exitwj: mov eax,limit
cmp count,eax
jle okj
mov count,0 ; if (count>limit) count=0
jmp ok2j
okj: mov eax,off
add count,eax
mov eax,count ;
mov edx,0 ;
idiv ncols ; MODI 255
mov count,edx ;
add count,1 ; else count=(count+off)%255+1
ok2j: mov eax,count
mov ebx,[pixel]
mov imaged[ebx],eax ; save count for color cycling (not aligned)
imul eax,3
add eax,2
mov cl,Colors[eax]
sub eax,1
mov ebx,[pixel]
mov im[ebx],cl
add pixel,1
mov cl,Colors[eax]
sub eax,1
mov ebx,[pixel]
mov im[ebx],cl
add pixel,1
mov cl,Colors[eax]
mov ebx,[pixel]
mov im[ebx],cl
add pixel,1
fld n
fadd one
fst n
fcom xres
fnstsw ax
test ah,1
jne Loopnj ; end Loop n
mov eax,resadd
add pixel,eax
fstp popped
fld m
fadd one
fst m
fcom yres
fnstsw ax
test ah,1
jne Loopmj ; end Loop m
fstp popped
;call Cycle
}
ChangeBitmap(himg, im);
BitBlt(hdc, 0, 0, 512, 512, hdcimg, 0, 0, SRCCOPY);
}
/* Original menu options */
void Choice1()
{
__asm{
Choice1: fld aedge1
fstp aedge
fld bedge1
fstp bedge
fld xdelta1
fstp xdelta
fld ydelta1
fstp ydelta
mov fractaltype,MANDELBROT
call Mandelbrot
}
}
void Choice2()
{
__asm{
Choice2: fld aedge2
fstp aedge
fld bedge2
fstp bedge
fld xdelta2
fstp xdelta
fld ydelta2
fstp ydelta
mov fractaltype,MANDELBROT
call Mandelbrot
}
}
void Choice3()
{
__asm{
Choice3: fld aedge3
fstp aedge
fld bedge3
fstp bedge
fld xdelta3
fstp xdelta
fld ydelta3
fstp ydelta
mov fractaltype,MANDELBROT
call Mandelbrot
}
}
void Choice4()
{
__asm{
Choice4: fld aedge4
fstp aedge
fld bedge4
fstp bedge
fld xdelta4
fstp xdelta
fld ydelta4
fstp ydelta
fld a1
fstp a
fld b1
fstp b
mov fractaltype,JULIA
call Julia
}
}
void Choice5()
{
__asm{
Choice5: fld aedge4
fstp aedge
fld bedge4
fstp bedge
fld xdelta4
fstp xdelta
fld ydelta4
fstp ydelta
fld a2
fstp a
fld b2
fstp b
mov fractaltype,JULIA
call Julia
}
}
void Choice6()
{
__asm{
Choice6: fld aedge4
fstp aedge
fld bedge4
fstp bedge
fld xdelta4
fstp xdelta
fld ydelta4
fstp ydelta
fld a3
fstp a
fld b3
fstp b
mov fractaltype,JULIA
call Julia
}
}
void Choice7()
{
__asm{
Choice7: fld aedge4
fstp aedge
fld bedge4
fstp bedge
fld xdelta4
fstp xdelta
fld ydelta4
fstp ydelta
fld a4
fstp a
fld b4
fstp b
mov fractaltype,JULIA
call Julia
}
}
void Choice8()
{
__asm{
Choice8: fld aedge4
fstp aedge
fld bedge4
fstp bedge
fld xdelta4
fstp xdelta
fld ydelta4
fstp ydelta
fld a5
fstp a
fld b5
fstp b
mov fractaltype,JULIA
call Julia
}
}
void Choice9()
{
__asm{
Choice9: fld aedge4
fstp aedge
fld bedge4
fstp bedge
fld xdelta4
fstp xdelta
fld ydelta4
fstp ydelta
fld a6
fstp a
fld b6
fstp b
mov fractaltype,JULIA
call Julia
}
}
void Choice0()
{
__asm{
Choice0: fld aedge4
fstp aedge
fld bedge4
fstp bedge
fld xdelta4
fstp xdelta
fld ydelta4
fstp ydelta
fld a7
fstp a
fld b7
fstp b
mov fractaltype,JULIA
call Julia
}
}
void Choicea()
{
__asm{
Choicea: fld aedge4
fstp aedge
fld bedge4
fstp bedge
fld xdelta4
fstp xdelta
fld ydelta4
fstp ydelta
fld a8
fstp a
fld b8
fstp b
mov fractaltype,JULIA
call Julia
}
}
void Choiceb()
{
__asm{
Choiceb: fld xres
fcomp xres1
fnstsw ax
test ah,64
jne To128
fld xres1
fstp xres
fld yres1
fstp yres
mov eax,numpixels1
mov numpixels,eax
mov eax,resadd1
mov resadd,eax
jmp resend
To128: fld xres2
fstp xres
fld yres2
fstp yres
mov eax,numpixels2
mov numpixels,eax
mov eax,resadd2
mov resadd,eax
resend:
}
}
void Choicem()
{
__asm{
Choicem: fld aedge5
fstp aedge
fld bedge5
fstp bedge
fld xdelta5
fstp xdelta
fld ydelta5
fstp ydelta
mov fractaltype,MANDELBROT
call Mandelbrot
}
}
/* Colortable cycling */
void Cycle()
{
__asm{
Cycle:
Loopc0: mov eax,cyinc
mul cydir
add [off],eax
mov pixel,0
Loopc: mov ebx,pixel
mov ecx,imaged[ebx]
mov count,ecx
cmp count,0
je ok3
mov eax,off
add count,eax
mov eax,count
mov edx,0
idiv ncols
mov count,edx
add count,1
ok3: mov eax,count
imul eax,3
add eax,2
mov cl,Colors[eax]
sub eax,1
mov ebx,[pixel]
mov im[ebx],cl
add pixel,1
mov cl,Colors[eax]
sub eax,1
mov ebx,[pixel]
mov im[ebx],cl
add pixel,1
mov cl,Colors[eax]
mov ebx,[pixel]
mov im[ebx],cl
add pixel,1
mov eax,numpixels
cmp pixel,eax
jl Loopc
}
ChangeBitmap(himg, im);
BitBlt(hdc, 0, 0, 512, 512, hdcimg, 0, 0, SRCCOPY);
}
/* Windows GUI stuff */
ChangeBitmap(HBITMAP b, unsigned char *bits)
{
BITMAP bm;
BITMAPINFOHEADER bi;
/* would be better to have bi be a global variable and only set once.
Then could call SetDIBits directly instead of ChangeBitmap. */
GetObject(b, sizeof(bm), &bm);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = -bm.bmHeight; // Negative to fix upside-down BMP orientation
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
SetDIBits(hdc, b, 0, bm.bmHeight, bits, &bi, DIB_RGB_COLORS);
}
QuitProgram()
{
if (w) { DestroyWindow(w); w = NULL; }
if (himg) { DeleteObject(himg); }
if (hdcimg) { DeleteDC(hdcimg); }
}
ClearScreen()
{
SelectObject(hdc, wc.hbrBackground);
PatBlt(hdc, 0, 0, 512, 512, PATCOPY);
memset(im, 0, 512*512*3);
memset(imaged, 0, 512*512*4);
ChangeBitmap(himg, im);
}
int GetFilename(char *str)
{
OPENFILENAME fna;
char cwd[MAX_PATH];
int i;
GetCurrentDirectory(MAX_PATH, cwd);
str[0] = '\0';
memset(&fna, 0, sizeof(OPENFILENAME));
fna.lStructSize = sizeof(OPENFILENAME);
fna.hwndOwner = w;
fna.lpstrFilter = "All Files\0*.*\0PPM Files\0*.ppm\0";
fna.lpstrInitialDir = cwd;
fna.nFilterIndex = 1;
fna.lpstrFile = str;
fna.nMaxFile = MAX_PATH;
fna.lpstrTitle = "Save To:";
fna.Flags = OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT;
GetSaveFileName(&fna);
if (str[0] == '\0') return(1);
if (fna.nFileExtension == 0) { strcat(str, ".ppm"); }
return(0);
}
SaveImage()
{
FILE *fp;
char str[MAX_PATH];
unsigned char *p = im;
if (GetFilename(str)) { return; }
if ((fp = fopen(str,"wb")) == NULL)
{
MessageBox(w, "Can't save to desired file", "Error", MB_ICONERROR|MB_APPLMODAL);
return;
}
fprintf(fp, "P6\n512 512\n255\n");
for (n=0; n<512; n++)
for (m=0; m<512; m++)
{
fputc(*(p+2), fp);
fputc(*(p+1), fp);
fputc(*(p), fp);
p+=3;
}
fclose(fp);
}
int PASCAL DialogParams(HWND w0, unsigned msg, WPARAM wp, LPARAM lp)
{
char str[256];
switch (msg)
{
case WM_INITDIALOG:
sprintf(str, "%.10lf", aedge); SetDlgItemText(w0, IDD_AEDGE, str);
sprintf(str, "%.10lf", bedge); SetDlgItemText(w0, IDD_BEDGE, str);
sprintf(str, "%.10lf", xdelta); SetDlgItemText(w0, IDD_XDELTA, str);
sprintf(str, "%.10lf", ydelta); SetDlgItemText(w0, IDD_YDELTA, str);
sprintf(str, "%.10lf", a); SetDlgItemText(w0, IDD_A, str);
sprintf(str, "%.10lf", b); SetDlgItemText(w0, IDD_B, str);
sprintf(str, "%d", limit); SetDlgItemText(w0, IDD_LIMIT, str);
CheckDlgButton(w0, IDD_MANDELBROT, !fractaltype);
CheckDlgButton(w0, IDD_JULIA, fractaltype);
if (fractaltype == MANDELBROT) { SendDlgItemMessage(w0, IDD_A, EM_SETREADONLY, 1, 0); SendDlgItemMessage(w0, IDD_B, EM_SETREADONLY, 1, 0); }
return(TRUE);
case WM_COMMAND:
switch (LOWORD(wp))
{
case IDOK: EndDialog(w0, TRUE); if (fractaltype == MANDELBROT) { Mandelbrot(); } else { Julia(); } return(TRUE); break;
case IDCANCEL: EndDialog(w0, TRUE); return(TRUE); break;
case IDD_AEDGE: GetDlgItemText(w0, IDD_AEDGE, str, 256); sscanf(str, "%lf", &aedge); break;
case IDD_BEDGE: GetDlgItemText(w0, IDD_BEDGE, str, 256); sscanf(str, "%lf", &bedge); break;
case IDD_XDELTA: GetDlgItemText(w0, IDD_XDELTA, str, 256); sscanf(str, "%lf", &xdelta); break;
case IDD_YDELTA: GetDlgItemText(w0, IDD_YDELTA, str, 256); sscanf(str, "%lf", &ydelta); break;
case IDD_A: GetDlgItemText(w0, IDD_A, str, 256); sscanf(str, "%lf", &a); break;
case IDD_B: GetDlgItemText(w0, IDD_B, str, 256); sscanf(str, "%lf", &b); break;
case IDD_LIMIT: GetDlgItemText(w0, IDD_LIMIT, str, 256); sscanf(str, "%d", &limit); break;
case IDD_MANDELBROT: fractaltype = MANDELBROT;
SendDlgItemMessage(w0, IDD_A, EM_SETREADONLY, 1, 0); SendDlgItemMessage(w0, IDD_B, EM_SETREADONLY, 1, 0);
break;
case IDD_JULIA: fractaltype = JULIA;
SendDlgItemMessage(w0, IDD_A, EM_SETREADONLY, 0, 0); SendDlgItemMessage(w0, IDD_B, EM_SETREADONLY, 0, 0);
break;
}
return(TRUE);
}
return(FALSE);
}
long FAR PASCAL WindowProcedure(HWND w, unsigned msg, WPARAM wp, LPARAM lp)
{
PAINTSTRUCT paints;
DLGPROC getdata;
HMENU smenu;
WORD wplow;
HDC h;
POINTS pt;
double ae, be, ae2, be2;
int dx, dy;
char str[256];
switch (msg)
{
case WM_CREATE: