Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void game_of_life(int** life, int len, int wid){
- int N, x, y, temp[len][wid], inf = 0;
- for(y = 0; y < len; y++) {
- for (x = 0; x < wid; x++)
- { temp[y][x] = life[y][x]; }
- }
- for(y = 1; y < len - 1; y++){
- for(x = 1; x < wid - 1; x++){
- N = life[y + 1][x - 1] +
- life[y + 1][x] +
- life[y + 1][x + 1] +
- life[y][x - 1] +
- life[y][x + 1] +
- life[y - 1][x - 1] +
- life[y - 1][x] +
- life[y - 1][x + 1];
- if(temp[y][x] == 1){
- if(N == 2)
- {temp[y][x] = life[y][x];}
- if(N == 3)
- {temp[y][x] = life[y][x];}
- if(N < 2)
- {temp[y][x] = 0;}
- if(N > 3)
- {temp[y][x] = 0;}
- }
- else{ if(N == 3)
- {temp[y][x] = 1;} }
- N = 0;
- }
- }
- for(y = 0; y < len; y++)
- for(x = 0; x < wid; x++){
- if(life[y][x] == temp[y][x])
- {inf++;}
- life[y][x] = temp[y][x];
- }
- if(inf == len * wid)
- {exit(0);}
- }
- struct picture{
- int height;
- int wide;
- int size;
- };
- int main(int command, char* commands[]){
- struct picture image;
- unsigned char header[54];
- int k, l, m, maxiter;
- int freq = 1;
- char* dir_name;
- FILE* file;
- for(int i = 0; i < command; i++){
- if(!strcmp("--input", commands[i])){
- file = fopen(commands[1 + 1], "rb");
- }
- if(!strcmp("--output", commands[i])){
- dir_name = commands[i + 1];
- mkdir(dir_name);
- }
- if(!strcmp("--max_iter", commands[i])){
- maxiter = strtol(commands[i + 1], 0, 10);
- }
- if(!strcmp("--dump_freq", commands[i])){
- freq = strtol(commands[i + 1], 0, 10);
- }
- }
- fread(header, 1, 54, file);
- image.wide = header[21] * 256 * 256 * 256 +
- header[20] * 256 * 256 +
- header[19] * 256 +
- header[18];
- image.height = header[25] * 256 * 256 * 256 +
- header[24] * 256 * 256 +
- header[23] * 256 +
- header[22];
- image.size = header[5] * 256 * 256 * 256 +
- header[4] * 256 * 256 +
- header[3] * 256 +
- header[2];
- unsigned char image_byte[image.size - 54];
- fread(image_byte, 1, image.size, file);
- int** img = (int**)malloc(image.height * sizeof(int*));
- for(int i = 0; i < image.height; i++)
- img[i] = (int*)malloc(image.wide * sizeof(int));
- k = -(image.wide % 4);
- for(int i = image.height - 1; i >= 0; i--){
- k += (image.wide % 4);
- for(int j = 0; j < image.wide; j++){
- if(image_byte[k] == 255)
- img[i][j] = 0;
- else
- img[i][j] = 1;
- k += 3;
- }
- }
- for (l = 0; l <= maxiter; l++){
- if(l % freq == 0){
- char filename[l];
- char path[20];
- strcpy(path, dir_name);
- strcat(strcat(path, "\\"), strcat(itoa (l, filename, 10), ".bmp"));
- FILE* life = fopen(path, "w");
- fwrite(header, 1, 54, life);
- m = 0;
- for (int i = image.height - 1; i >= 0; i--){
- for (int j = 0; j < image.wide; j++){
- for (k = 0; k < 3; k++) {
- if (img[i][j] == 1)
- image_byte[m] = 0;
- else
- image_byte[m] = 255;
- m++;
- }
- }
- while (m % 4 != 0) {
- image_byte[m] = 0;
- m++;
- }
- }
- fwrite(image_byte, 1, image.size, life);
- fclose(life);
- }
- game_of_life(img, image.height, image.wide);
- }
- free(img);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement