-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevolve_row.c
217 lines (187 loc) · 6.86 KB
/
evolve_row.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 <assert.h>
#include <time.h>
#include "evolve_pixel.h"
#include "evolve_row.h"
#include "image.h"
void evolve_row_single_parent(Pixel *dst_row, const Pixel *src_row,
const size_t size) {
size_t i;
for (i = 0; i < size; ++i) {
evolve_pixel_single_parent(dst_row + i, src_row + i);
}
}
void evolve_row_dad_mom_genes(Pixel *dst_row, const Pixel *src_row,
const size_t size) {
size_t i;
const Pixel *dad_pixel, *mom_pixel;
/* Evolve first pixel. */
dad_pixel = src_row + size - 1;
mom_pixel = src_row + 1;
evolve_pixel_dad_mom_genes(dst_row, dad_pixel, mom_pixel);
/* Evolve middle pixels (all except first and last). */
for (i = 1; i < size - 1; ++i) {
dad_pixel = src_row + i - 1;
mom_pixel = src_row + i + 1;
evolve_pixel_dad_mom_genes(dst_row + i, dad_pixel, mom_pixel);
}
/* Evolve last pixel. */
dad_pixel = src_row + size - 2;
mom_pixel = src_row;
evolve_pixel_dad_mom_genes(dst_row + size - 1, dad_pixel, mom_pixel);
}
void evolve_row_dad_or_mom(Pixel *dst_row, const Pixel *src_row,
const size_t size) {
size_t i;
const Pixel *dad_pixel, *mom_pixel;
/* Evolve first pixel. */
dad_pixel = src_row + size - 1;
mom_pixel = src_row + 1;
evolve_pixel_dad_or_mom(dst_row, dad_pixel, mom_pixel);
/* Evolve middle pixels (all except first and last). */
for (i = 1; i < size - 1; ++i) {
dad_pixel = src_row + i - 1;
mom_pixel = src_row + i + 1;
evolve_pixel_dad_or_mom(dst_row + i, dad_pixel, mom_pixel);
}
/* Evolve last pixel. */
dad_pixel = src_row + size - 2;
mom_pixel = src_row;
evolve_pixel_dad_or_mom(dst_row + size - 1, dad_pixel, mom_pixel);
}
void evolve_row_3_parent_genes(Pixel *dst_row, const Pixel *src_row,
const size_t size) {
size_t i;
const Pixel *parent_pixel1, *parent_pixel2, *parent_pixel3;
/* Evolve first pixel. */
parent_pixel1 = src_row + size - 1;
parent_pixel2 = src_row;
parent_pixel3 = src_row + 1;
evolve_pixel_3_parent_genes(dst_row, parent_pixel1, parent_pixel2,
parent_pixel3);
/* Evolve middle pixels (all except first and last). */
for (i = 1; i < size - 1; ++i) {
parent_pixel1 = src_row + i - 1;
parent_pixel2 = src_row + i;
parent_pixel3 = src_row + i + 1;
evolve_pixel_3_parent_genes(dst_row + i, parent_pixel1, parent_pixel2,
parent_pixel3);
}
/* Evolve last pixel. */
parent_pixel1 = src_row + size - 2;
parent_pixel2 = src_row + size - 1;
parent_pixel3 = src_row;
evolve_pixel_3_parent_genes(dst_row + size - 1, parent_pixel1,
parent_pixel2, parent_pixel3);
}
void evolve_row_dad_mom_average(Pixel *dst_row, const Pixel *src_row,
const size_t size) {
size_t i;
const Pixel *dad_pixel, *mom_pixel;
/* Evolve first pixel. */
dad_pixel = src_row + size - 1;
mom_pixel = src_row + 1;
evolve_pixel_dad_mom_average(dst_row, dad_pixel, mom_pixel);
/* Evolve middle pixels (all except first and last). */
for (i = 1; i < size - 1; ++i) {
dad_pixel = src_row + i - 1;
mom_pixel = src_row + i + 1;
evolve_pixel_dad_mom_average(dst_row + i, dad_pixel, mom_pixel);
}
/* Evolve last pixel. */
dad_pixel = src_row + size - 2;
mom_pixel = src_row;
evolve_pixel_dad_mom_average(dst_row + size - 1, dad_pixel, mom_pixel);
}
void evolve_row_dad_mom_dad_above(Pixel *dst_row, const Pixel *src_row,
const size_t size) {
size_t i;
const Pixel *dad_pixel, *mom_pixel;
/* Evolve all except last). */
for (i = 0; i < size - 1; ++i) {
dad_pixel = src_row + i;
mom_pixel = src_row + i + 1;
evolve_pixel_dad_mom_average(dst_row + i, dad_pixel, mom_pixel);
}
/* Evolve last pixel. */
dad_pixel = src_row + size - 1;
mom_pixel = src_row;
evolve_pixel_dad_mom_average(dst_row + size - 1, dad_pixel, mom_pixel);
}
Image *generate_image(size_t width, size_t height, Row_evolver row_evolver) {
size_t j;
Image *image;
image = malloc_image(width, height);
assert(image->width == width);
assert(image->height == height);
if (image) {
const Pixel *src_row;
Pixel *dst_row;
/* Set the first row to random RGB values. */
set_random_row(image->pixels, image->width);
/*
* Evolve all other rows iteratively, starting with second row based on
* the first row.
*/
for (j = 1; j < image->height; ++j) {
dst_row = image->pixels + j * image->width;
src_row = image->pixels + (j - 1) * image->width;
(*row_evolver)(dst_row, src_row, image->width);
}
return image;
} else {
fprintf(stderr, "Failed to allocate image.\n");
exit(1);
}
}
void main_row_generation(int argc, char *argv[]) {
unsigned long width, height;
int strategy;
Row_evolver chosen_row_evolver;
Image *image;
FILE *file;
Row_evolver row_evolvers[6] = {
&evolve_row_single_parent, &evolve_row_dad_mom_genes,
&evolve_row_dad_or_mom, &evolve_row_3_parent_genes,
&evolve_row_dad_mom_average, &evolve_row_dad_mom_dad_above};
if (argc != 5) {
fprintf(stderr,
"Got %d arguments, need 4: width, height, strategy index, file"
" name.\n",
argc - 1);
exit(1);
}
if (1 != sscanf(argv[1], "%lu", &width)) {
fprintf(stderr, "Enter width as a positive integer.\n");
exit(1);
}
if (1 != sscanf(argv[2], "%lu", &height)) {
fprintf(stderr, "Enter height as a positive integer.\n");
exit(1);
}
strategy = atoi(argv[3]);
if (1 <= strategy && strategy <= 6) {
chosen_row_evolver = row_evolvers[strategy - 1];
} else {
fprintf(stderr, "You entered %d, which is invalid.\n", strategy);
fprintf(stderr, "Available strategies include:\n");
fprintf(stderr, "\t1. evolve_row_single_parent\n");
fprintf(stderr, "\t2. evolve_row_dad_mom_genes\n");
fprintf(stderr, "\t3. evolve_row_dad_or_mom\n");
fprintf(stderr, "\t4. evolve_row_3_parent_genes\n");
fprintf(stderr, "\t5. evolve_row_dad_mom_average\n");
fprintf(stderr, "\t6. evolve_row_dad_mom_dad_above\n");
exit(1);
}
srand((unsigned int)time(NULL));
image = generate_image((size_t)width, (size_t)height, chosen_row_evolver);
file = fopen(argv[4], "w");
if (file) {
write_image_P6(file, image);
fclose(file);
} else {
fprintf(stderr, "Failed to open file.\n");
exit(1);
}
free_image(image);
}