-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.c
72 lines (42 loc) · 1.24 KB
/
template.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
#include<stdio.h>
#include<stdlib.h>
const unsigned char LIVE = 1;
const unsigned char DEAD = 0;
void write_ppm(unsigned char* gen, char* filename, size_t side_length)
{
size_t i;
FILE *fp = fopen(filename, "wb");
fprintf(fp, "P6\n%zd %zd\n255\n", side_length, side_length);
const unsigned char _black[] = {0x00, 0x00, 0x00};
const unsigned char _white[] = {0xff, 0xff, 0xff};
for(i=0; i<side_length*side_length; ++i)
if(gen[i] == 1)
fwrite(_black, 1 , 3 , fp);
else fwrite(_white, 1 , 3 , fp);
fclose(fp);
}
unsigned char* read_ppm(char* filename, size_t* side_length)
{
size_t h = 0, i;
FILE *fp = fopen(filename, "rb");
fscanf(fp, "P6\n%zd %zd\n255\n", side_length, &h);
if (*side_length != h) exit(1);
unsigned char* gen = malloc( (*side_length) * (*side_length) );
for(i=0;i<(*side_length)*(*side_length);++i)
{
unsigned char buff[3];
fread(buff, 1 , 3 , fp);
if(buff[0] == 0 && buff[1] == 0 && buff[2] == 0) gen[i] = LIVE; // black pixel
else gen[i] = DEAD;
}
fclose(fp);
return gen;
}
int main()
{
size_t side_length;
unsigned char * prev = read_ppm("input64.ppm", &side_length);
write_ppm(prev, "output.ppm", side_length);
free(prev);
return 0;
}