Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct pgm_file {
  5. unsigned int format; //структура файла
  6. unsigned int width;
  7. unsigned int height;
  8. unsigned int colours;
  9. };
  10.  
  11.  
  12. void inverse(unsigned char *data, unsigned char *dataNew, int height, int width) {
  13. for (int i = 0; i < height; ++i) {
  14. for (int j = 0; j < width; ++j) {
  15. dataNew[i * width + j] = 255 - data[i * width + j];
  16. }
  17. }
  18. }
  19.  
  20. void vertical(unsigned char *data, unsigned char *dataNew, int height, int width) {
  21. for (int i = 0; i < height; ++i) {
  22. for (int j = 0; j < width; ++j) {
  23. dataNew[(height - i - 1) * width + j] = data[i * width + j];
  24. }
  25. }
  26. }
  27.  
  28. void horizontalColoured(unsigned char *data, unsigned char *dataNew, int height, int width) {
  29. for (int i = 0; i < height; ++i) {
  30. for (int j = 0; j < width; j += 3) {
  31. dataNew[i * width + width - j - 3] = data[i * width + j + 0];
  32. dataNew[i * width + width - j - 2] = data[i * width + j + 1];
  33. dataNew[i * width + width - j - 1] = data[i * width + j + 2];
  34. }
  35. }
  36. }
  37.  
  38. void horizontalBW(unsigned char *data, unsigned char *dataNew, int height, int width) {
  39. for (int i = 0; i < height; ++i) {
  40. for (int j = 0; j < width; ++j) {
  41. dataNew[i * width + width - j - 1] = data[i * width + j];
  42. }
  43. }
  44. }
  45.  
  46. void rightBW(unsigned char *data, unsigned char *dataNew, int height, int width) {
  47. for (int i = 0; i < height; ++i) {
  48. for (int j = 0; j < width; ++j) {
  49. dataNew[height * j + height - i - 1] = data[i * width + j];
  50. }
  51. }
  52. }
  53.  
  54. void rightColour(unsigned char *data, unsigned char *dataNew, int height, int width) {
  55. for (int i = 0; i < height; ++i) {
  56. for (int j = 0; j < width; ++j) {
  57. dataNew[(height * j + height - i) * 3 - 3] = data[(i * width + j) * 3 + 0];
  58. dataNew[(height * j + height - i) * 3 - 2] = data[(i * width + j) * 3 + 1];
  59. dataNew[(height * j + height - i) * 3 - 1] = data[(i * width + j) * 3 + 2];
  60. }
  61. }
  62. }
  63.  
  64. void leftBW(unsigned char *data, unsigned char *dataNew, int height, int width) {
  65. for (int i = 0; i < height; ++i) {
  66. for (int j = 0; j < width; ++j) {
  67. dataNew[(width - j - 1) * height + i] = data[i * width + j];
  68. }
  69. }
  70. }
  71.  
  72. void leftColour(unsigned char *data, unsigned char *dataNew, int height, int width) {
  73. for (int i = 0; i < height; ++i) {
  74. for (int j = 0; j < width; ++j) {
  75. dataNew[((width - j - 1) * height + i) * 3 + 0] = data[(i * width + j) * 3 + 0];
  76. dataNew[((width - j - 1) * height + i) * 3 + 1] = data[(i * width + j) * 3 + 1];
  77. dataNew[((width - j - 1) * height + i) * 3 + 2] = data[(i * width + j) * 3 + 2];
  78. }
  79. }
  80. }
  81.  
  82.  
  83. int main(int argc, char **argv) {
  84. unsigned int i, j;
  85. int numberofcoloures;
  86.  
  87. if (argc != 4) {
  88. printf("Incorrect format of the input data\n");
  89. return -1;
  90. }
  91.  
  92. struct pgm_file pgm;
  93. FILE *file = fopen(argv[1], "rb");
  94. if (file == nullptr) {
  95. printf("Can`t open input file\n");
  96. return -1;
  97. }
  98.  
  99. FILE *file_new = fopen(argv[2], "rb");
  100. if (file_new == nullptr) {
  101. printf("Can`t open output file\n");
  102. return -1;
  103. }
  104.  
  105. if (fscanf(file, "P%d", &pgm.format) == 0) {
  106. printf("Incorrect format of the file\n");
  107. return -1;
  108. }
  109.  
  110. fscanf(file, "%d %d", &pgm.width, &pgm.height);
  111. fscanf(file, "%d\n", &pgm.colours);
  112.  
  113. if ((pgm.height < 1) || (pgm.width < 1)) {
  114. printf("Incorrect height or width in the FrameHeader\n");
  115. return -1;
  116. }
  117. if (pgm.colours != 255) {
  118. printf("Incorrect colours in the FrameHeader\n");
  119. return -1;
  120. }
  121.  
  122. if (pgm.format==5){
  123. numberofcoloures = 1;
  124. } else {
  125. if (pgm.format==6){
  126. numberofcoloures = 3;
  127. } else {
  128. printf("Incorrect format of the file\n");
  129. }
  130. }
  131.  
  132.  
  133. unsigned char *data = (unsigned char *) malloc(sizeof(unsigned char) * numberofcoloures * pgm.height * pgm.width);
  134. if (data == NULL) {
  135. printf("Error\n");
  136. return -1;
  137. }
  138. int troubleLess = fread(data, sizeof(char), numberofcoloures * (pgm.height * pgm.width), file);
  139. if (troubleLess < numberofcoloures* (pgm.height * pgm.width)) {
  140. printf("Incorrect number of pixels\n");
  141. return -1;
  142. }
  143. int troubleMore = fread(data, sizeof(char), 1, file);
  144. if (troubleMore > 0) {
  145. printf("Incorrect number of pixels\n");
  146. return -1;
  147. }
  148.  
  149. unsigned char *dataNew = (unsigned char *) malloc(sizeof(unsigned char) * numberofcoloures * pgm.height * pgm.width);
  150. if (dataNew == NULL) {
  151. printf("Error\n");
  152. return -1;
  153. }
  154.  
  155. switch (atoi(argv[3])) {
  156. case 0:
  157. inverse(data, dataNew, pgm.height, pgm.width * numberofcoloures);
  158. break;
  159. case 1:
  160. if (pgm.format == 5) {
  161. horizontalBW(data, dataNew, pgm.height, pgm.width * numberofcoloures);
  162. } else {
  163. horizontalColour(data, dataNew, pgm.height, pgm.width * numberofcoloures);
  164. }
  165. break;
  166. case 2:
  167. vertical(data, dataNew, pgm.height, pgm.width * numberofcoloures);
  168. break;
  169. case 3:
  170. if (pgm.format == 5) {
  171. rightBW(data, dataNew, pgm.height, pgm.width);
  172. } else {
  173. rightColour(data, dataNew, pgm.height, pgm.width);
  174. }
  175. break;
  176. case 4:
  177. if (pgm.format == 5) {
  178. leftBW(data, dataNew, pgm.height, pgm.width);
  179. } else {
  180. leftColour(data, dataNew, pgm.height, pgm.width);
  181. }
  182. break;
  183. default:
  184. printf("Error\n");
  185. return -1;
  186. }
  187.  
  188. file_new = fopen(argv[2], "wb");
  189.  
  190. fprintf(file_new, "P%d\n", pgm.format);
  191. if (atoi(argv[3]) < 3) {
  192. fprintf(file_new, "%d %d\n", pgm.width, pgm.height);
  193. } else {
  194. fprintf(file_new, "%d %d\n", pgm.height, pgm.width);
  195. }
  196. fprintf(file_new, "%d\n", pgm.colours);
  197. fwrite(dataNew, sizeof(char), (numberofcoloures * pgm.height * pgm.width), file_new);
  198. fclose(file);
  199. fclose(file_new);
  200. free(data);
  201. free(dataNew);
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement