-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot.c
193 lines (147 loc) · 4.36 KB
/
mandelbrot.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
#include <stdio.h> /* FILE, printf, fprintf, fwrite, fclose */
#include <stdlib.h> /* malloc, free */
#include "complex.h" /* Complex, addx, multx, absx */
#include "color.h" /* Color */
#include "transfers.h" /* transfer functions */
#include "args.h" /* argument handling */
/* TODO: this is pretty inconsistent, maybe get this from arguments?*/
#define THRESHOLD 2
typedef struct Image
{
Color** data;
int w;
int h;
} Image;
typedef struct Samples
{
float** data; /* float instead of int due to averaging (super-sampling) */
int w;
int h;
} Samples;
Image allocate_image_memory(int w, int h)
{
/* column-first -> our images are lists of columns */
Image img;
Color** cldata = malloc(w * sizeof(unsigned char*));
int i;
for (i = 0; i < w; ++i) {
cldata[i] = malloc(h * sizeof(Color));
}
img.data = cldata;
img.w = w;
img.h = h;
return img;
}
Samples allocate_samples_memory(int w, int h)
{
/* column-first -> our images are lists of columns */
Samples smps;
float** sampledata = malloc(w * sizeof(unsigned char*));
int i;
for (i = 0; i < w; ++i)
{
sampledata[i] = malloc(h * sizeof(float));
}
smps.data = sampledata;
smps.w = w;
smps.h = h;
return smps;
}
void write_ppm(Image img, char* dest)
{
int x, y;
FILE* f = fopen(dest, "wb");
fprintf(f, "P6 %d %d 255 ", img.w, img.h); /* ppm header */
/* row-first to conform to ppm specs. */
for (y = 0; y < img.h; ++y) {
for (x = 0; x < img.w; ++x) {
fwrite(&img.data[x][y], sizeof(Color), 1, f);
}
}
fclose(f);
}
/*
* Returns number of mandelbrot iterations for a point in the complex plane.
*/
int mandelbrot(Complex c, int maxiterations)
{
int iter = 0;
Complex cur_z = { 0, 0 };
/* sample in cardioid? */
float q = (c.r - 0.25)*(c.r - 0.25) + c.i*c.i;
if (q*(q + (c.r - 0.25)) < 0.25*c.i*c.i) {
return maxiterations;
}
/* sample in big bulb (period-2)? */
if ((c.r + 1)*(c.r + 1) + c.i*c.i < 0.0625) {
return maxiterations;
}
/* main mandelbrot loop - this is where the magic happens ;) */
while ((iter < maxiterations) &&
(cur_z.r*cur_z.r + cur_z.i*cur_z.i) < THRESHOLD*THRESHOLD)
{
cur_z = addx(multx(cur_z,cur_z), c);
++iter;
}
return iter;
}
/*
* Samples the section of the complex plane specified by first (top left)
* and second (bottom right), using the resolution of smps and
* calculates for each sample the number of mandelbrot iterations.
*
* Performs simple 4x super-sampling (4 samples per pixel).
*/
void sample_plane(Samples smps, Complex first,
Complex second, int maxiterations)
{
float width = second.r - first.r;
float height = first.i - second.i;
float sample_dist_r = width / (float)smps.w;
float sample_dist_i = height / (float)smps.h;
int x, y;
int tmp_s;
Complex sample = first;
for (x = 0; x < smps.w; ++x) {
for (y = 0; y < smps.h; ++y) {
tmp_s = mandelbrot(sample, maxiterations);
sample.r += sample_dist_r / 2.0;
tmp_s += mandelbrot(sample, maxiterations);
sample.i -= sample_dist_i / 2.0;
tmp_s += mandelbrot(sample, maxiterations);
sample.r -= sample_dist_r / 2.0;
tmp_s += mandelbrot(sample, maxiterations);
smps.data[x][y] = (float)tmp_s / 4;
sample.i -= sample_dist_i / 2.0;
}
sample.i = first.i;
sample.r += sample_dist_r;
}
}
/*
* Applies the callback transfer function for each sample in the sample plane
* and writes the resulting color values into img.
*/
void visualize(Image img, Samples smps, int maxiterations,
Color (transfer_func)(int, int))
{
int x, y;
for (x = 0; x < img.w; ++x) {
for (y = 0; y < img.h; ++y) {
img.data[x][y] = (transfer_func)(smps.data[x][y], maxiterations);
}
}
}
int main(int argc, char* argv[])
{
Args args = parse_args(argc, argv);
Samples samples = allocate_samples_memory(args.width, args.height);
Image img = allocate_image_memory(args.width, args.height);
Complex c1 = args.top_left;
Complex c2 = args.bottom_right;
sample_plane(samples, c1, c2, args.maxiterations);
visualize(img, samples, args.maxiterations, tr_linear);
write_ppm(img, args.filename);
printf("Done!\n");
return 0;
}