-
Notifications
You must be signed in to change notification settings - Fork 0
/
evolve_image.c
220 lines (201 loc) · 7.82 KB
/
evolve_image.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
#include <assert.h>
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include "image.h"
#include "evolve_pixel.h"
#include "evolve_image.h"
#define MAX_FILENAME_LENGTH 100
#define WRITE_TO_DISK 0
void evolve_image_4_parent_genes(Image *dst_image, const Image *src_image) {
size_t i, j;
size_t width, height;
assert(dst_image->width == src_image->width);
assert(dst_image->height == src_image->height);
width = dst_image->width;
height = dst_image->height;
/* Need to evolve every pixel of dst_image based on src_image. */
for (j = 0; j < height; ++j) {
for (i = 0; i < width; ++i) {
evolve_pixel_4_parent_genes(
/* destination pixel */
pixel_at(dst_image, i, j),
/* source pixel below */
pixel_at(src_image, i, wrap(j, -1, height)),
/* source pixel above */
pixel_at(src_image, i, wrap(j, 1, height)),
/* source pixel left */
pixel_at(src_image, wrap(i, -1, width), j),
/* source pixel right */
pixel_at(src_image, wrap(i, 1, width), j));
}
}
}
void evolve_image_4_parent_average(Image *dst_image, const Image *src_image) {
size_t i, j;
size_t width, height;
assert(dst_image->width == src_image->width);
assert(dst_image->height == src_image->height);
width = dst_image->width;
height = dst_image->height;
/* Need to evolve every pixel of dst_image based on src_image. */
for (j = 0; j < height; ++j) {
for (i = 0; i < width; ++i) {
evolve_pixel_4_parent_average(
/* destination pixel */
pixel_at(dst_image, i, j),
/* source pixel below */
pixel_at(src_image, i, wrap(j, -1, height)),
/* source pixel above */
pixel_at(src_image, i, wrap(j, 1, height)),
/* source pixel left */
pixel_at(src_image, wrap(i, -1, width), j),
/* source pixel right */
pixel_at(src_image, wrap(i, 1, width), j));
}
}
}
void evolve_image_4_parent_pick_one(Image *dst_image, const Image *src_image) {
size_t i, j;
size_t width, height;
assert(dst_image->width == src_image->width);
assert(dst_image->height == src_image->height);
width = dst_image->width;
height = dst_image->height;
/* Need to evolve every pixel of dst_image based on src_image. */
for (j = 0; j < height; ++j) {
for (i = 0; i < width; ++i) {
evolve_pixel_4_parent_pick_one(
/* destination pixel */
pixel_at(dst_image, i, j),
/* source pixel below */
pixel_at(src_image, i, wrap(j, -1, height)),
/* source pixel above */
pixel_at(src_image, i, wrap(j, 1, height)),
/* source pixel left */
pixel_at(src_image, wrap(i, -1, width), j),
/* source pixel right */
pixel_at(src_image, wrap(i, 1, width), j));
}
}
}
void evolve_image_8_parent_pick_one(Image *dst_image, const Image *src_image) {
size_t i, j;
size_t width, height;
assert(dst_image->width == src_image->width);
assert(dst_image->height == src_image->height);
width = dst_image->width;
height = dst_image->height;
/* Need to evolve every pixel of dst_image based on src_image. */
for (j = 0; j < height; ++j) {
for (i = 0; i < width; ++i) {
evolve_pixel_8_parent_pick_one(
/* destination pixel */
pixel_at(dst_image, i, j),
/* source pixel below */
pixel_at(src_image, i, wrap(j, -1, height)),
/* source pixel below left */
pixel_at(src_image, wrap(i, -1, width), wrap(j, -1, height)),
/* source pixel below right */
pixel_at(src_image, wrap(i, 1, width), wrap(j, -1, height)),
/* source pixel above */
pixel_at(src_image, i, wrap(j, 1, height)),
/* source pixel above left */
pixel_at(src_image, wrap(i, -1, width), wrap(j, 1, height)),
/* source pixel above right */
pixel_at(src_image, wrap(i, 1, width), wrap(j, 1, height)),
/* source pixel left */
pixel_at(src_image, wrap(i, -1, width), j),
/* source pixel right */
pixel_at(src_image, wrap(i, 1, width), j));
}
}
}
void evolve_image_8_parent_extreme(Image *dst_image, const Image *src_image) {
size_t i, j;
size_t width, height;
Pixel **parents = malloc(8 * sizeof(*parents));
assert(dst_image->width == src_image->width);
assert(dst_image->height == src_image->height);
width = dst_image->width;
height = dst_image->height;
/* Need to evolve every pixel of dst_image based on src_image. */
for (j = 0; j < height; ++j) {
for (i = 0; i < width; ++i) {
/* source pixel below */
parents[0] = pixel_at(src_image, i, wrap(j, -1, height));
/* source pixel below left */
parents[1] = pixel_at(src_image, wrap(i, -1, width),
wrap(j, -1, height));
/* source pixel below right */
parents[2] = pixel_at(src_image, wrap(i, 1, width),
wrap(j, -1, height));
/* source pixel above */
parents[3] = pixel_at(src_image, i, wrap(j, 1, height));
/* source pixel above left */
parents[4] = pixel_at(src_image, wrap(i, -1, width),
wrap(j, 1, height));
/* source pixel above right */
parents[5] = pixel_at(src_image, wrap(i, 1, width),
wrap(j, 1, height));
/* source pixel left */
parents[6] = pixel_at(src_image, wrap(i, -1, width), j);
/* source pixel right */
parents[7] = pixel_at(src_image, wrap(i, 1, width), j);
evolve_pixel_8_parent_extreme(pixel_at(dst_image, i, j), parents);
}
}
free(parents);
}
Image **generate_images(size_t n_images, size_t width, size_t height) {
Image **images;
size_t i;
images = malloc(n_images * sizeof(*images));
images[0] = malloc_random_image(width, height);
for (i = 1; i < n_images; ++i) {
images[i] = malloc_image(width, height);
evolve_image_8_parent_extreme(images[i], images[i-1]);
fprintf(stderr, "\33[2K\rGenerated image %lu...", i);
fflush(stderr);
}
fprintf(stderr, "\33[2K\rDone generating.\n");
fflush(stderr);
return images;
}
void write_images(Image **images, size_t n_images) {
size_t i;
for (i = 0; i < n_images; ++i) {
char filename[MAX_FILENAME_LENGTH];
FILE *file;
if (WRITE_TO_DISK) {
sprintf(filename, "images/random%07lu.ppm", i);
file = fopen(filename, "w");
} else {
file = stdout;
}
if (file) {
write_image_P6(file, images[i]);
if (WRITE_TO_DISK) {
fclose(file);
}
fprintf(stderr, "\33[2K\rWrote image %lu...", i);
fflush(stderr);
} else {
fprintf(stderr, "Failed to open file %s\n", filename);
exit(1);
}
}
fprintf(stderr, "\33[2K\rDone writing.\n");
}
void free_images(Image **images, size_t n_images) {
size_t i;
for (i = 0; i < n_images; ++i) {
free_image(images[i]);
}
free(images);
}
void main_image_generation(size_t n_images, size_t width, size_t height) {
Image **images = generate_images(n_images, width, height);
write_images(images, n_images);
free_images(images, n_images);
}