-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.c
118 lines (99 loc) · 2.57 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
/*
* © 2014 Louis-Guillaume Gagnon <[email protected]>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <complex.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
struct Color {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
};
int NITER = 500;
size_t XDIM = 3000;
size_t YDIM = 2000;
double REALMIN = -2;
double REALMAX = 1;
#define REALSTEP ((REALMAX - REALMIN) / XDIM)
double IMAGMIN = -1;
double IMAGMAX = 1;
#define IMAGSTEP ((IMAGMAX - IMAGMIN) / YDIM)
#define OUTFILE stdout
struct Color color0(int niter)
{
struct Color col;
if (niter < 0) {
col.r = col.g = col.b = 0;
} else {
col.r = niter * 255.0 / NITER;
col.g = (col.r * 2) % 255;
col.b = (col.r * 10) % 255;
}
col.a = 255;
return col;
}
struct Color (*color)(int niter) = color0;
/* if return value < 0, c is in the set
* otherwise, number of iterations before diverging is returned
* (i.e. returns n such as z_n > 2 */
int mandelbrot_bounded(double complex c)
{
int i;
double complex z = 0;
for (i = 0; i < NITER && cabs(z) < 2; i++)
z = z * z + c;
return (cabs(z) < 2)? -1 : i;
}
void print_header()
{
uint32_t tmp;
fprintf(OUTFILE, "imagefile");
tmp = htonl(XDIM);
fwrite(&tmp, sizeof(uint32_t), 1, OUTFILE);
tmp = htonl(YDIM);
fwrite(&tmp, sizeof(uint32_t), 1, OUTFILE);
}
void print_mandelbrot()
{
double real, imag;
size_t k, x, y, row_len;
uint8_t *row;
row_len = XDIM * 4;
row = malloc(row_len);
for (y = 0, imag = IMAGMIN; y < YDIM; y++, imag += IMAGSTEP) {
if (y % 100 == 0)
fprintf(stderr, "column %d / %d\n", y, YDIM);
for (x = 0, k = 0, real = REALMIN; x < XDIM; x++, k += 4, real += REALSTEP) {
double complex c = real + imag * I;
struct Color col = color(mandelbrot_bounded(c));
row[k] = col.r;
row[k+1] = col.g;
row[k+2] = col.b;
row[k+3] = col.a;
}
fwrite(row, 1, row_len, OUTFILE);
}
free(row);
}
int main(int argc, char *argv[])
{
print_header();
print_mandelbrot();
return 0;
}