-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot_seq.c
executable file
·217 lines (173 loc) · 5.6 KB
/
mandelbrot_seq.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/************ START CHANGES ************/
#include <string.h>
#ifndef BENCH
#define BENCH 0
#endif
/************* END CHANGES *************/
double c_x_min;
double c_x_max;
double c_y_min;
double c_y_max;
double pixel_width;
double pixel_height;
int iteration_max = 200;
int image_size;
unsigned char **image_buffer;
int i_x_max;
int i_y_max;
int image_buffer_size;
int gradient_size = 16;
int colors[17][3] = {
{66, 30, 15},
{25, 7, 26},
{9, 1, 47},
{4, 4, 73},
{0, 7, 100},
{12, 44, 138},
{24, 82, 177},
{57, 125, 209},
{134, 181, 229},
{211, 236, 248},
{241, 233, 191},
{248, 201, 95},
{255, 170, 0},
{204, 128, 0},
{153, 87, 0},
{106, 52, 3},
{16, 16, 16},
};
void allocate_image_buffer(){
int rgb_size = 3;
image_buffer = (unsigned char **) malloc(sizeof(unsigned char *) * image_buffer_size);
for(int i = 0; i < image_buffer_size; i++){
image_buffer[i] = (unsigned char *) malloc(sizeof(unsigned char) * rgb_size);
};
}
/************ START CHANGES ************/
void init(int argc, char *argv[]){
if(argc == 3){
sscanf(argv[2], "%d", &image_size);
if(!strcmp(argv[1], "full")){
c_x_min = -2.5;
c_x_max = 1.5;
c_y_min = -2.0;
c_y_max = 2.0;
} else
if(!strcmp(argv[1], "seahorse")){
c_x_min = -0.8;
c_x_max = -0.7;
c_y_min = 0.05;
c_y_max = 0.15;
} else
if(!strcmp(argv[1], "elephant")){
c_x_min = 0.175;
c_x_max = 0.375;
c_y_min = -0.1;
c_y_max = 0.1;
} else
if(!strcmp(argv[1], "spiral")){
c_x_min = -0.188;
c_x_max = -0.012;
c_y_min = 0.554;
c_y_max = 0.754;
} else {
exit(-1);
};
} else
if(argc < 6){
printf("usage: ./mandelbrot_seq c_x_min c_x_max c_y_min c_y_max image_size\n");
printf("examples with image_size = 11500:\n");
printf(" Full Picture: ./mandelbrot_seq -2.5 1.5 -2.0 2.0 11500\n");
printf(" Seahorse Valley: ./mandelbrot_seq -0.8 -0.7 0.05 0.15 11500\n");
printf(" Elephant Valley: ./mandelbrot_seq 0.175 0.375 -0.1 0.1 11500\n");
printf(" Triple Spiral Valley: ./mandelbrot_seq -0.188 -0.012 0.554 0.754 11500\n");
exit(0);
}
else{
sscanf(argv[1], "%lf", &c_x_min);
sscanf(argv[2], "%lf", &c_x_max);
sscanf(argv[3], "%lf", &c_y_min);
sscanf(argv[4], "%lf", &c_y_max);
sscanf(argv[5], "%d", &image_size);
};
i_x_max = image_size;
i_y_max = image_size;
image_buffer_size = image_size * image_size;
pixel_width = (c_x_max - c_x_min) / i_x_max;
pixel_height = (c_y_max - c_y_min) / i_y_max;
}
/************* END CHANGES *************/
void update_rgb_buffer(int iteration, int x, int y){
int color;
if(iteration == iteration_max){
image_buffer[(i_y_max * y) + x][0] = colors[gradient_size][0];
image_buffer[(i_y_max * y) + x][1] = colors[gradient_size][1];
image_buffer[(i_y_max * y) + x][2] = colors[gradient_size][2];
}
else{
color = iteration % gradient_size;
image_buffer[(i_y_max * y) + x][0] = colors[color][0];
image_buffer[(i_y_max * y) + x][1] = colors[color][1];
image_buffer[(i_y_max * y) + x][2] = colors[color][2];
};
}
void write_to_file(){
FILE * file;
char * filename = "output.ppm";
char * comment = "# ";
int max_color_component_value = 255;
file = fopen(filename,"wb");
fprintf(file, "P6\n %s\n %d\n %d\n %d\n", comment,
i_x_max, i_y_max, max_color_component_value);
for(int i = 0; i < image_buffer_size; i++){
fwrite(image_buffer[i], 1 , 3, file);
};
fclose(file);
}
void compute_mandelbrot(){
double z_x;
double z_y;
double z_x_squared;
double z_y_squared;
double escape_radius_squared = 4;
int iteration;
int i_x;
int i_y;
double c_x;
double c_y;
for(i_y = 0; i_y < i_y_max; i_y++){
c_y = c_y_min + i_y * pixel_height;
if(fabs(c_y) < pixel_height / 2){
c_y = 0.0;
};
for(i_x = 0; i_x < i_x_max; i_x++){
c_x = c_x_min + i_x * pixel_width;
z_x = 0.0;
z_y = 0.0;
z_x_squared = 0.0;
z_y_squared = 0.0;
for(iteration = 0;
iteration < iteration_max && \
((z_x_squared + z_y_squared) < escape_radius_squared);
iteration++){
z_y = 2 * z_x * z_y + c_y;
z_x = z_x_squared - z_y_squared + c_x;
z_x_squared = z_x * z_x;
z_y_squared = z_y * z_y;
};
/************ START CHANGES ************/
if(!BENCH) update_rgb_buffer(iteration, i_x, i_y);
};
};
}
int main(int argc, char *argv[]){
init(argc, argv);
if(!BENCH) allocate_image_buffer();
compute_mandelbrot();
if(!BENCH) write_to_file();
return 0;
}
/************* END CHANGES *************/