Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. void game_of_life(int** life, int len, int wid){
  7. int N, x, y, temp[len][wid], inf = 0;
  8. for(y = 0; y < len; y++) {
  9. for (x = 0; x < wid; x++)
  10. { temp[y][x] = life[y][x]; }
  11. }
  12. for(y = 1; y < len - 1; y++){
  13. for(x = 1; x < wid - 1; x++){
  14. N = life[y + 1][x - 1] +
  15. life[y + 1][x] +
  16. life[y + 1][x + 1] +
  17. life[y][x - 1] +
  18. life[y][x + 1] +
  19. life[y - 1][x - 1] +
  20. life[y - 1][x] +
  21. life[y - 1][x + 1];
  22. if(temp[y][x] == 1){
  23. if(N == 2)
  24. {temp[y][x] = life[y][x];}
  25. if(N == 3)
  26. {temp[y][x] = life[y][x];}
  27. if(N < 2)
  28. {temp[y][x] = 0;}
  29. if(N > 3)
  30. {temp[y][x] = 0;}
  31. }
  32. else{ if(N == 3)
  33. {temp[y][x] = 1;} }
  34. N = 0;
  35. }
  36. }
  37. for(y = 0; y < len; y++)
  38. for(x = 0; x < wid; x++){
  39. if(life[y][x] == temp[y][x])
  40. {inf++;}
  41. life[y][x] = temp[y][x];
  42. }
  43. if(inf == len * wid)
  44. {exit(0);}
  45. }
  46. struct picture{
  47. int height;
  48. int wide;
  49. int size;
  50. };
  51.  
  52. int main(int command, char* commands[]){
  53. struct picture image;
  54. unsigned char header[54];
  55. int k, l, m, maxiter;
  56. int freq = 1;
  57. char* dir_name;
  58. FILE* file;
  59. for(int i = 0; i < command; i++){
  60. if(!strcmp("--input", commands[i])){
  61. file = fopen(commands[1 + 1], "rb");
  62. }
  63. if(!strcmp("--output", commands[i])){
  64. dir_name = commands[i + 1];
  65. mkdir(dir_name);
  66. }
  67. if(!strcmp("--max_iter", commands[i])){
  68. maxiter = strtol(commands[i + 1], 0, 10);
  69. }
  70. if(!strcmp("--dump_freq", commands[i])){
  71. freq = strtol(commands[i + 1], 0, 10);
  72. }
  73. }
  74. fread(header, 1, 54, file);
  75. image.wide = header[21] * 256 * 256 * 256 +
  76. header[20] * 256 * 256 +
  77. header[19] * 256 +
  78. header[18];
  79. image.height = header[25] * 256 * 256 * 256 +
  80. header[24] * 256 * 256 +
  81. header[23] * 256 +
  82. header[22];
  83. image.size = header[5] * 256 * 256 * 256 +
  84. header[4] * 256 * 256 +
  85. header[3] * 256 +
  86. header[2];
  87. unsigned char image_byte[image.size - 54];
  88. fread(image_byte, 1, image.size, file);
  89. int** img = (int**)malloc(image.height * sizeof(int*));
  90. for(int i = 0; i < image.height; i++)
  91. img[i] = (int*)malloc(image.wide * sizeof(int));
  92. k = -(image.wide % 4);
  93. for(int i = image.height - 1; i >= 0; i--){
  94. k += (image.wide % 4);
  95. for(int j = 0; j < image.wide; j++){
  96. if(image_byte[k] == 255)
  97. img[i][j] = 0;
  98. else
  99. img[i][j] = 1;
  100. k += 3;
  101. }
  102. }
  103. for (l = 0; l <= maxiter; l++){
  104. if(l % freq == 0){
  105. char filename[l];
  106. char path[20];
  107. strcpy(path, dir_name);
  108. strcat(strcat(path, "\\"), strcat(itoa (l, filename, 10), ".bmp"));
  109. FILE* life = fopen(path, "w");
  110. fwrite(header, 1, 54, life);
  111. m = 0;
  112. for (int i = image.height - 1; i >= 0; i--){
  113. for (int j = 0; j < image.wide; j++){
  114. for (k = 0; k < 3; k++) {
  115. if (img[i][j] == 1)
  116. image_byte[m] = 0;
  117. else
  118. image_byte[m] = 255;
  119. m++;
  120. }
  121. }
  122. while (m % 4 != 0) {
  123. image_byte[m] = 0;
  124. m++;
  125. }
  126. }
  127. fwrite(image_byte, 1, image.size, life);
  128. fclose(life);
  129. }
  130. game_of_life(img, image.height, image.wide);
  131. }
  132. free(img);
  133. return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement