Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int a, widht, height, maxcol, num;
  8.  
  9. int main() {
  10. FILE * pFile;
  11. FILE * copyFile;
  12.  
  13. pFile = fopen("pict.ppm", "rb");
  14. if (pFile == NULL) {
  15. pFile = fopen("pict.pgm", "rb");
  16. }
  17. if (pFile == NULL) {
  18. printf("%c%c%c %c%c%c%c %c%c%c %c%c%c %c%c%c%c%c \n", 't','h','e','f','i','l','e','w','a','s','n','o','t','f','o','u','n','d');
  19. return 0;
  20. }
  21. fscanf(pFile, "P%i%i%i%i\n", &a, &widht, &height, &maxcol);
  22.  
  23. printf("%c%i \n", 'P', a);
  24. printf("%i %i \n", height, widht);
  25.  
  26. if (a == 5) {
  27. unsigned char * k;
  28. k = (unsigned char*) malloc (sizeof(unsigned char) * height * widht);
  29. if (k != NULL) {
  30. fread(k, sizeof(unsigned char), widht * height, pFile);
  31. copyFile = fopen("pictureconverted.pgm", "wb");
  32.  
  33. cout << "enter number of action" << '\n';
  34. cin >> num;
  35. switch (num) {
  36. case 0:
  37. for (int i = 0 ; i < (widht * height); ++i) {
  38. k[i] = 255 - k[i];
  39. }
  40. fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
  41. fwrite(k, sizeof(unsigned char), widht * height, copyFile);
  42. break;
  43. case 1:
  44. for (int i = 0; i < height / 2; ++i) {
  45. for (int j = 0; j < widht; ++j) {
  46. swap(k[i * widht + j ], k[(height - i - 1) * widht + j]);
  47. }
  48. }
  49. fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
  50. fwrite(k, sizeof(unsigned char), widht * height, copyFile);
  51. break;
  52. case 2:
  53. for (int i = 0; i < height; ++i) {
  54. for (int j = 0; j < widht / 2; ++j) {
  55. swap(k[ i * widht + j], k[(i + 1) * widht - j]);
  56. }
  57. }
  58. fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
  59. fwrite(k, sizeof(unsigned char), widht * height, copyFile);
  60. break;
  61. case 3:
  62. unsigned char * k1;
  63. k1 = (unsigned char*) malloc (sizeof(unsigned char) * widht * height);
  64. for ( int i = 0; i < height; ++i) {
  65. for (int j = 0; j < widht; ++j) {
  66. k1[j * height + height - i - 1] = k[i * widht + j];
  67. }
  68. }
  69. fprintf(copyFile, "P%i\n%i %i\n%i\n", a, height, widht, maxcol);
  70. fwrite(k1, sizeof(unsigned char), height * widht, copyFile);
  71. free(k1);
  72. break;
  73. case 4:
  74. unsigned char * k2;
  75. k2 = (unsigned char*) malloc (sizeof(unsigned char) * widht * height);
  76. for (int i = 0; i < height; ++i) {
  77. for (int j = 0; j < widht; ++j) {
  78. k2[(widht - j + 1) * height + i] = k[i * widht + j];
  79. }
  80. }
  81. fprintf(copyFile, "P%i\n%i %i\n%i\n", a, height, widht, maxcol);
  82. fwrite(k2, sizeof(unsigned char), height * widht, copyFile);
  83. free(k1);
  84. break;
  85. default:
  86. cout << "unavailable action" << '\n';
  87. }
  88.  
  89. free(k);
  90. } else {
  91. printf( "Allocation memory failed");
  92. }
  93. } else {
  94. unsigned char *k;
  95. k = (unsigned char *) malloc(sizeof(unsigned char) * 3 * height * widht);
  96. if (k != NULL) {
  97. fread(k, sizeof(unsigned char), 3 * widht * height, pFile);
  98. copyFile = fopen("pictureconverted.ppm", "wb");
  99. cout << "enter number of action" << '\n';
  100. cin >> num;
  101. switch (num) {
  102. case 0:
  103. fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
  104. for (int i = 0; i < (3 *widht * height); ++i) {
  105. k[i] = 255 - k[i];
  106. }
  107. fwrite(k, sizeof(unsigned char), 3 * widht * height, copyFile);
  108. break;
  109. case 1:
  110. for (int i = 0; i < height / 2; ++i) {
  111. for (int j = 0; j < widht; j += 3) {
  112. swap(k[i * widht + j ], k[(height - i - 1) * widht + j]);
  113. swap(k[(i + 1) * widht + j + 1], k[(height - (i + 1) - 1) * widht + j ]);
  114. swap(k[(i + 2) * widht + j + 2], k[(height - (i + 2) - 1) * widht + j ]);
  115. }
  116. }
  117. fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
  118. fwrite(k, sizeof(unsigned char), 3 * widht * height, copyFile);
  119. break;
  120. case 2:
  121. for (int i = 0; i < height; ++i) {
  122. for (int j = 0; j < widht / 2; j += 3) {
  123. swap(k[ i * widht + j], k[(i + 1) * widht - j]);
  124. swap(k[ (i) * widht + j ], k[(i + 1) * widht - j ]);
  125. swap(k[ i * widht + j ], k[(i + 1) * widht - j ]);
  126. }
  127. }
  128. fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
  129. fwrite(k, sizeof(unsigned char), 3 * widht * height, copyFile);
  130. break;
  131. case 3:
  132. unsigned char * k1;
  133. k1 = (unsigned char*) malloc (sizeof(unsigned char) * widht * height * 3);
  134. for ( int i = 0; i < height; ++i) {
  135. for (int j = 0; j < widht; j += 3) {
  136. k1[j * height + height - i - 1] = k[i * widht + j];
  137. k1[(j + 1) * height + height - i - 1] = k[i * widht + (j + 1)];
  138. k1[(j + 2) * height + height - i - 1] = k[i * widht + (j + 2)];
  139. }
  140. }
  141. fprintf(copyFile, "P%i\n%i %i\n%i\n", a, height, widht, maxcol);
  142. fwrite(k1, sizeof(unsigned char), 3 * height * widht, copyFile);
  143. free(k1);
  144. break;
  145. case 4:
  146. unsigned char * k2;
  147. k2 = (unsigned char*) malloc (sizeof(unsigned char) * widht * height * 3);
  148. for (int i = 0; i < height; ++i) {
  149. for (int j = 0; j < widht; j += 3) {
  150. k2[(widht - j + 1) * height + i] = k[i * widht + j];
  151. k2[(widht - (j + 1) + 1) * height + i] = k[i * widht + (j + 1)];
  152. k2[(widht - (j + 2) + 1) * height + i] = k[i * widht + (j + 2)];
  153. }
  154. }
  155. fprintf(copyFile, "P%i\n%i %i\n%i\n", a, height, widht, maxcol);
  156. fwrite(k2, sizeof(unsigned char), 3 * height * widht, copyFile);
  157. free(k1);
  158. break;
  159. default:
  160. cout << "unavailable action" << '\n';
  161. }
  162.  
  163. free(k);
  164. }
  165. else {
  166. printf("Allocation memory failed");
  167. }
  168. }
  169. fclose(pFile);
  170. fclose(copyFile);
  171. return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement