-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
241 lines (195 loc) · 6.01 KB
/
main.cu
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
#include <gd.h>
#include <stdio.h>
#include <fenv.h>
#include <math.h>
#include <errno.h>
// ------------------------
// TO BE CUSTOMIZED BY USER
// ------------------------
// RENDERING PARAMETERS
#define sharpness 4000 // number of pixels specifying PNG pngWidth
#define maxIter 10000 // set higher for highly zoomed-in pictures
// ------------------------
// COMPLEX DOMAIN
double centerRe = -0.0805335270615;
double centerIm = 0.000000426876074501;
double epsilon = 0.00000125107482035;
// -0.0805335270615
// 4.26876074501e-06
// 1.25107482035e-06
// 100000
// See the bottom of this code for a discussion of some output possibilities.
char* filenameF = "output/ZoomSpiral%d.png";
void create_frame(int iteration);
typedef struct RgbColor
{
unsigned char r;
unsigned char g;
unsigned char b;
} RgbColor;
typedef struct HsvColor
{
unsigned char h;
unsigned char s;
unsigned char v;
} HsvColor;
RgbColor HsvToRgb(HsvColor hsv)
{
RgbColor rgb;
unsigned char region, remainder, p, q, t;
if (hsv.s == 0)
{
rgb.r = hsv.v;
rgb.g = hsv.v;
rgb.b = hsv.v;
return rgb;
}
region = hsv.h / 43;
remainder = (hsv.h - (region * 43)) * 6;
p = (hsv.v * (255 - hsv.s)) >> 8;
q = (hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8;
t = (hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8;
switch (region)
{
case 0:
rgb.r = hsv.v; rgb.g = t; rgb.b = p;
break;
case 1:
rgb.r = q; rgb.g = hsv.v; rgb.b = p;
break;
case 2:
rgb.r = p; rgb.g = hsv.v; rgb.b = t;
break;
case 3:
rgb.r = p; rgb.g = q; rgb.b = hsv.v;
break;
case 4:
rgb.r = t; rgb.g = p; rgb.b = hsv.v;
break;
default:
rgb.r = hsv.v; rgb.g = p; rgb.b = q;
break;
}
return rgb;
}
HsvColor RgbToHsv(RgbColor rgb)
{
HsvColor hsv;
unsigned char rgbMin, rgbMax;
rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);
hsv.v = rgbMax;
if (hsv.v == 0)
{
hsv.h = 0;
hsv.s = 0;
return hsv;
}
hsv.s = 255 * long(rgbMax - rgbMin) / hsv.v;
if (hsv.s == 0)
{
hsv.h = 0;
return hsv;
}
if (rgbMax == rgb.r)
hsv.h = 0 + 43 * (rgb.g - rgb.b) / (rgbMax - rgbMin);
else if (rgbMax == rgb.g)
hsv.h = 85 + 43 * (rgb.b - rgb.r) / (rgbMax - rgbMin);
else
hsv.h = 171 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin);
return hsv;
}
__global__
void fillColor(int n, int H, int W, int* color, int* palette, int black, double reStart, double reEnd, double imStart, double imEnd) {
int T = blockIdx.x*blockDim.x + threadIdx.x;
if (T >= n) return;
int x = T % H;
int y = T / H;
double re = reStart + ((double) x / W * (reEnd - reStart));
double im = imEnd - ((double) y / H * (imEnd - imStart));
double nextRe, nextIm, logRe, logIm, powerRe, powerIm;
int toggleOverflow = 0;
int numberOfIterations = 0;
if (re == 0 && im == 0){
color[T] = black;
}
else {
logRe = .5*log(re*re + im*im);
logIm = atan2(im, re);
nextRe = re;
nextIm = im;
while (numberOfIterations < maxIter && toggleOverflow == 0)
{
powerRe = (nextRe * logRe - nextIm * logIm);
powerIm = (nextRe * logIm + nextIm * logRe);
if (powerRe > 700) {
toggleOverflow = 1;
}
nextRe = exp(powerRe) * cos(powerIm);
nextIm = exp(powerRe) * sin(powerIm);
numberOfIterations += 1;
}
}
int shade = (255 * numberOfIterations) / maxIter;
color[T] = numberOfIterations < maxIter ? palette[shade] : black;
}
int main(){
create_frame(5);
}
void create_frame(int iteration) {
FILE* outfile; // defined in stdio
gdImagePtr image; // a GD image object
char filename[80];
int i, T, x, y; // array subscripts
int black, palette[256]; // black, all possible shades of palette
int* d_palette;
HsvColor col_hsv;
RgbColor col_rgb;
double reStart = centerRe - epsilon;
double reEnd = centerRe + epsilon;
double imStart = centerIm - epsilon;
double imEnd = centerIm + epsilon;
int pngWidth = sharpness;
int pngHeight = pngWidth * (imEnd - imStart) / (reEnd - reStart);
int N = pngWidth * pngHeight;
//int** color = make2DintArray(pngWidth, pngHeight);
int* color = (int*) malloc(N*sizeof(int));
int* d_color;
printf("width: %i\n", pngWidth);
printf("height: %i\n", pngHeight);
image = gdImageCreate(pngWidth, pngHeight);
black = gdImageColorAllocate(image, 0, 0, 0);
for (i=0; i<255; i++){
//hue = int(255 * m / MAX_ITER)
//saturation = 255
//value = 255 if m < MAX_ITER else 0
col_hsv.h = i;
col_hsv.s = 255;
col_hsv.v = (i == 255 ? 0 : 255);
col_rgb = HsvToRgb(col_hsv);
palette[i] = gdImageColorAllocate(image, col_rgb.r, col_rgb.g, col_rgb.b);
}
palette[255] = gdImageColorAllocate(image, 0, 0, 0);
//void fillColor(int n, int H, int W, int* color, int* palette, int black) {
cudaMalloc(&d_palette, 256*sizeof(int));
cudaMalloc(&d_color, N*sizeof(int));
cudaMemcpy(d_palette, palette, 256*sizeof(int), cudaMemcpyHostToDevice);
// Calculate power tower convergence / divergence
fillColor<<<(pngWidth*pngHeight+255)/256, 256>>>(N, pngHeight, pngWidth, d_color, d_palette, black, reStart, reEnd, imStart, imEnd);
cudaMemcpy(color, d_color, N*sizeof(int), cudaMemcpyDeviceToHost);
for (T=0; T<pngWidth*pngHeight; T++) {
x = T % pngHeight;
y = T / pngHeight;
gdImageSetPixel(image, x, y, color[T]);
}
// Free 2D array
free(color);
cudaFree(d_color);
cudaFree(d_palette);
// Finally, write the image out to a file.
sprintf(filename, filenameF, iteration);
printf("Creating output file '%s'.\n", filename);
outfile = fopen(filename, "wb");
gdImagePng(image, outfile);
fclose(outfile);
}