-
Notifications
You must be signed in to change notification settings - Fork 1
/
life-game.c
212 lines (203 loc) · 6.82 KB
/
life-game.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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h> // library for Linux / MacOS; in Windows #include <direct.h>
/* Struct of BMP file
* Name Length (bytes) Description
* -----------------------------------------------------
* BitMapFileHeader
* Type 2 signature "BM"
* Size 4 sizeof(file)
* Reserved1 2 reserve (should be all 0)
* Reserved2 2 reserve (should be all 0)
* OffsetBits 4 image offset from the beginning of the file
* ---
* Total: 14 bytes
* BitMapInfoHeader
* Size 4 size of header
* Width 4 ::: 18 - 21 bytes
* Height 4 ::: 22 - 25 bytes
* Planes 2 in 99% -> 1
* BitCount 2 number of bits on the one pixel ::: (now he is equal to 1)
* Compression 4 in 99% -> 0
* SizeImage 4 if (!compression) -> 0
* XpelsPerMeter 4 horizontal resolution of the dot on one meter
* YpelsPerMeter 4 vertical resolution of the dot on one meter
* ColorsUsed 4
* ColorsImportant 4
* ---
* Total: 40 bytes
* :::Total to header: 54 bytes
* ColorTable
* ColorTable
* BitMapArray
* Image Size
* */
int friends(int i, int j, unsigned char **Colors, int h, int w) {
int lives = 0;
for (int i1 = i - 1; i1 <= i + 1; i1++) {
for (int j1 = j - 1; j1 <= j + 1; j1++) {
if (i == i1 && j == j1) {
continue;
}
lives += ((Colors[(i1 + h) % h][(j1 + w) % w] == '1') ? (1) : (0));
}
}
return lives;
}
void next(int h, int w, int realWidth, unsigned char **Colors) {
unsigned char new_area[h][w];
for (int i = h - 1; i >= 0; i--) {
for (int j = 0; j < realWidth; j++) {
int live = friends(i, j, Colors, h, realWidth);
if (Colors[i][j] == '1' && !(live == 2 || live == 3)) {
new_area[i][j] = '0';
}
else if (Colors[i][j] == '0' && live == 3) {
new_area[i][j] = '1';
}
else {
new_area[i][j] = Colors[i][j];
}
}
}
for (int i = h - 1; i >= 0; i--) {
for (int j = 0; j < w; j++) {
Colors[i][j] = new_area[i][j];
}
}
}
struct pall {
unsigned char R;
unsigned char G;
unsigned char B;
unsigned char A;
};
void image(unsigned char **Colors, struct pall Palette[],
int h, int w, char *out, unsigned char header[54], int PalSz) {
FILE *output;
output = fopen(out, "w");
for (int i = 0; i < 54; i++) {
fprintf(output, "%c", header[i]);
}
for (int i = 0; i < PalSz; i++) {
fprintf(output, "%c%c%c%c", Palette[i].R, Palette[i].G, Palette[i].B, Palette[i].A);
}
for (int i = h - 1; i >= 0; i--) {
for (int j = 0; j < w; j += 8) {
int sum = 0;
for (int q = 0; q < 8; q++) {
int tmp = (Colors[i][j + q] == '1') ? (1) : (0);
sum += tmp * (int) pow(2, 7 - q);
}
unsigned char xx = sum;
fprintf(output, "%c", xx);
}
}
fclose(output);
}
int main(int argc, char *argv[]) {
FILE *my_picture;
char *directory;
int gen = 0, freq = 1;
for (int i = 1; i < argc; i += 2) {
if (!strcmp(argv[i], "--input")) { // --input picture.bmp
my_picture = fopen(argv[i + 1], "r");
}
else if (!strcmp(argv[i], "--max_iter")) { // --max_iter gen
char *end;
gen = (int) strtol(argv[i + 1], &end, 10);
}
else if (!strcmp(argv[i], "--dump_freq")) { // --dump_freq N
char *end;
freq = (int) strtol(argv[i + 1], &end, 10);
}
else if (!strcmp(argv[i], "--output")) { // --output directory_name
directory = argv[i + 1];
mkdir(directory, 0777); // 0777 - is a parameter (I have right to read, write, etc)
}
}
if (my_picture == NULL) {
printf("Not such file in directory\n");
return 0;
}
/*
* Take BitMapFileHeader + BitMapInfoHeader and add this info
* to header[sizeof(BitMapFileHeader + BitMapInfoHeader) = 54]
*/
unsigned char header[54];
fread(header, 1, 54, my_picture);
int width;
width = (int) (header[21] * pow(256, 3) +
header[20] * pow(256, 2) +
header[19] * pow(256, 1) +
header[18] * pow(256, 0));
int height;
height = (int) (header[25] * pow(256, 3) +
header[24] * pow(256, 2) +
header[23] * pow(256, 1) +
header[22] * pow(256, 0));
/*
* My Image starts from 63 byte
* Let's fill 54 - 63 bytes in Palette
*/
int OffsetBits;
OffsetBits = (int) (header[13] * pow(256, 3) +
header[12] * pow(256, 2) +
header[11] * pow(256, 1) +
header[10] * pow(256, 0));
struct pall Palette[(OffsetBits - 54) / 4];
for (int i = 0; i < (OffsetBits - 54) / 4; i++) {
unsigned char bytes[4];
fread(bytes, 1, 4, my_picture);
Palette[i].R = bytes[0];
Palette[i].G = bytes[1];
Palette[i].B = bytes[2];
Palette[i].A = bytes[3];
}
/*
* Image from the left to the right,
* from the bottom to top
*/
unsigned char **Colors;
Colors = (unsigned char **) malloc(height * sizeof(unsigned char *));
for (int i = 0; i < height; i++) {
Colors[i] = (unsigned char *) malloc(width * sizeof(unsigned char *));
}
int realWidth = width;
// * Width should be % 4 == 0
width += 32 - width % 32;
// * Filling
for (int i = height - 1; i >= 0; i--) {
for (int j = 0; j < width; j += 8) {
unsigned char byte[1];
fread(byte, 1, 1, my_picture);
int byte_ = byte[0];
int step = 7;
// * Saving colors
while (step >= 0) {
Colors[i][j + step] = ((byte_ % 2) ? ('1') : ('0'));
byte_ /= 2;
step--;
}
}
}
/*
* New generations
*/
for (int i = 1; i <= gen; i++) {
next(height, width, realWidth, Colors);
if (i % freq == 0) {
char out[100];
strcpy(out, directory);
char num[17];
strcat(out, "//kek"); // direction of slashes depends from your OS
sprintf(num, "%d", i); // int to char
strcat(out, num);
strcat(out, ".bmp");
image(Colors, Palette, height, width, out, header, (OffsetBits - 54) / 4);
}
}
fclose(my_picture);
}