-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.c
257 lines (207 loc) · 6.83 KB
/
canvas.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
#define _XOPEN_SOURCE 500
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <err.h>
#include "./canvas.h"
#include <png.h>
#include <wand/MagickWand.h>
point randomPoint(point range) {
return (point){
random() % range.x,
random() % range.y
};
}
color randomColor(void) {
return (color) {
random() % 255,
random() % 255,
random() % 255,
};
}
color determinePixelColor(const anchor *anchors, size_t size, point target) {
long min = LONG_MAX;
color c;
for(size_t idx = 0; idx < size; idx++) {
point d = {
anchors[idx].pos.x - target.x,
anchors[idx].pos.y - target.y,
};
long current = d.x * d.x + d.y * d.y;
if(min > current) {
min = current;
c = anchors[idx].col;
}
}
return c;
}
void *calculateChunk(void *arg) {
task_arg *targ = (task_arg*) arg;
point a = {
.x = targ->start % targ->size.x,
.y = targ->start / targ->size.x
};
point b = {
.x = (targ->start + targ->run) % targ->size.x,
.y = (targ->start + targ->run) / targ->size.x
};
for(long x = a.x; x < targ->size.x; x++) {
targ->map[x + a.y * targ->size.x] = determinePixelColor(targ->anchors, targ->anchors_size, (point){x, a.y});
}
for(long y = a.y + 1; y < b.y; y++) {
for(long x = 0; x < targ->size.x; x++) {
targ->map[x + y * targ->size.x] = determinePixelColor(targ->anchors, targ->anchors_size, (point){x, y});
}
}
for(long x = 0; x < b.x; x++) {
targ->map[x + b.y * targ->size.x] = determinePixelColor(targ->anchors, targ->anchors_size, (point){x, b.y});
}
return (void*)(int)1;
}
int generateVoronoi(color *buffer, point size, const anchor *anchors, size_t num_anchors) {
long area = size.x * size.y;
long threads = sysconf(_SC_NPROCESSORS_CONF);
long chunk = area / threads;
long rem = area - threads * chunk;
if(threads == 1) {
for(int y = 0; y < size.y; y++) {
for(int x = 0; x < size.x; x++) {
buffer[x + y * size.x] = determinePixelColor(anchors, num_anchors, (point){x, y});
}
}
} else {
long total = 0;
long thread_count = 0;
bool partition = true;
pthread_t th[threads];
task_arg *args[threads];
while(partition) {
long run;
if(thread_count + 1 == threads || total + chunk + rem >= area) {
run = chunk + rem;
partition = false;
} else run = chunk;
args[thread_count] = malloc(sizeof(task_arg));
args[thread_count]->start = total;
args[thread_count]->size = size;
args[thread_count]->run = run;
args[thread_count]->anchors = anchors;
args[thread_count]->anchors_size = num_anchors;
args[thread_count]->map = buffer;
if(pthread_create(&th[thread_count], NULL, calculateChunk, args[thread_count]) != 0) {
warn("Failed to create thread\n");
return 0;
}
total += run;
thread_count++;
}
for(long t = 0; t < thread_count; t++) {
pthread_join(th[t], NULL);
}
for(long idx = 0; idx < thread_count; idx++) {
free(args[idx]);
}
}
return 1;
}
int generatePNG(const char *filename, const color *color_map, point size) {
FILE *fp;
png_structp pngp = NULL;
png_infop infop = NULL;
png_byte **rows = NULL;
int pixel_size =3;
int depth = 8;
fp = fopen(filename, "wb+");
if(!fp) {
warn("Failed to open %s", filename);
return 0;
}
pngp = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(pngp == NULL) {
warnx("png_create_write_struct()");
return 0;
}
infop = png_create_info_struct(pngp);
if(infop == NULL) {
warnx("Failed call to png_create_info_struct()");
return 0;
}
png_set_IHDR(pngp, infop, size.x, size.y, depth,
PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT
);
rows = png_malloc(pngp, size.y * sizeof(png_byte *));
for(long r = 0; r < size.y; r++) {
png_byte *row = png_malloc(pngp, size.x * pixel_size);
rows[r] = row;
for(long c = 0; c < size.x; c++) {
color col = color_map[c + r * size.x];
*row++ = col.red;
*row++ = col.green;
*row++ = col.blue;
}
}
png_init_io(pngp, fp);
png_set_rows(pngp, infop, rows);
png_write_png(pngp, infop, PNG_TRANSFORM_IDENTITY, NULL);
for(long r = 0; r < size.y; r++) {
png_free(pngp, rows[r]);
}
png_free(pngp, rows);
png_destroy_write_struct(&pngp, &infop);
fclose(fp);
return 1;
}
int generateGIF(const char *filename, anchor *anchors, size_t anchors_size, color *color_map, point size, size_t frames, int velocity, bool keep) {
MagickWandGenesis();
MagickWand *wand = NewMagickWand();
MagickBooleanType status;
/*MagickSetCompression(wand, BZipCompression);*/
point direction[] = {
{-1, 0}, {-1, 1}, {0, 1}, {1, 1},
{1, 0}, {1, -1}, {0, -1}, {-1, -1}
};
static const size_t direction_size = sizeof(direction) / sizeof(direction[0]);
static char filepath[PATH_MAX];
for(size_t frame = 1; frame <= frames; frame++) {
sprintf(filepath, "frame_%zu.png", frame);
for(size_t idx = 0; idx < anchors_size; idx++) {
size_t dir = random() % direction_size;
point step = direction[dir];
anchors[idx].pos.x += step.x * velocity;
anchors[idx].pos.y += step.y * velocity;
}
if(generateVoronoi(color_map, size, anchors, anchors_size) == 0) {
warnx("Failed to generate diagram");
return 0;
}
if(generatePNG(filepath, color_map, size) == 0) {
warnx("Failed to generate PNG image");
return 0;
}
MagickReadImage(wand, filepath);
}
status = MagickWriteImages(wand, filename, MagickTrue);
if(status == MagickFalse) {
ExceptionType severity;
char *description = MagickGetException(wand,&severity);
(void) warnx("%s %s %lu %s\n",GetMagickModule(),description);
description= (char *) MagickRelinquishMemory(description);
return 0;
}
if(!keep) {
for(size_t frame = 1; frame <= frames; frame++) {
sprintf(filepath, "frame_%zu.png", frame);
remove(filepath);
}
}
wand = DestroyMagickWand(wand);
MagickWandTerminus();
return 1;
}