Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #pragma warning(disable:4996)
  4. #define _CRT_SECURE_NO_DEPRECATE
  5. using namespace std;
  6.  
  7. typedef unsigned char uchar;
  8.  
  9. struct RGB {
  10. uchar R;
  11. uchar G;
  12. uchar B;
  13. };
  14. struct Image {
  15. char format[2];
  16. size_t width;
  17. size_t height;
  18. size_t grade;
  19. size_t size;
  20. vector<RGB> imageP6;
  21. vector<uchar> imageP5;
  22. };
  23.  
  24. enum Command {
  25. REVERS = 0,
  26. HORIZONTAL_MIRROR = 1,
  27. VERTICAL_MIRROR = 2,
  28. LEFT_ROTATE = 3,
  29. RIGHT_ROTATE = 4
  30. };
  31.  
  32. void ReadImage(char* file, Image& image) {
  33. FILE* f = fopen(file, "rb");
  34. if (f == nullptr)
  35. throw exception("ERROR. File not opend");
  36. fscanf(f, "%c%c\n%lu %lu\n%lu\n", &image.format[0], &image.format[1], &image.width, &image.height, &image.grade);
  37. if (!strcmp(image.format, "P5") || !strcmp(image.format, "P6"))
  38. throw exception("ERROR. Unknow format");
  39. if (image.width < 0 || image.height < 0 || image.grade != 255)
  40. throw exception("ERROR. Bad header");
  41. image.size = image.width * image.height;
  42. uchar* buffer = new uchar[image.size * 3];
  43. fread(buffer, sizeof(uchar), image.size * 3, f);
  44. fclose(f);
  45. image.imageP6.resize(image.size);
  46. for (size_t i = 0; i < image.size; i++) {
  47. image.imageP6[i].R = buffer[i * 3 + 0];
  48. image.imageP6[i].G = buffer[i * 3 + 1];
  49. image.imageP6[i].B = buffer[i * 3 + 2];
  50. }
  51. delete[] buffer;
  52. }
  53. void WriteImage(char* file, Image& image) {
  54. FILE* f = fopen(file, "wb");
  55. if (strcmp(image.format, "P6")) {
  56. if (strcmp(file, ".pgm")) {
  57. throw exception("ERROR. Formats not converge");
  58. }
  59. }
  60. else {
  61. if (strcmp(file, ".pnm")) {
  62. throw exception("ERROR. Formats not converge");
  63. }
  64. }
  65. if (f == nullptr)
  66. throw exception("ERROR. File not opend");
  67. fprintf(f, "%c%c\n%lu %lu\n%d\n", image.format[0], image.format[1], image.width, image.height, image.grade);
  68. uchar* buffer = new uchar[image.size * 3];
  69. for (size_t i = 0; i < image.size; i++) {
  70. buffer[i * 3 + 0] = image.imageP6[i].R;
  71. buffer[i * 3 + 1] = image.imageP6[i].G;
  72. buffer[i * 3 + 2] = image.imageP6[i].B;
  73. }
  74. fwrite(buffer, sizeof(uchar), image.size * 3, f);
  75. delete[] buffer;
  76. fclose(f);
  77. }
  78.  
  79. void ReversImage(Image& image) {
  80. for (size_t y = 0; y < image.height; y++) {
  81. for (size_t x = 0; x < image.width; x++) {
  82. image.imageP6[y * image.width + x].R = -image.imageP6[y * image.width + x].R;
  83. image.imageP6[y * image.width + x].G = -image.imageP6[y * image.width + x].G;
  84. image.imageP6[y * image.width + x].B = -image.imageP6[y * image.width + x].B;
  85. }
  86. }
  87. }
  88. void HorisontalMirrorImage(Image& image) {
  89. for (size_t y = 0; y < image.height / 2; y++) {
  90. for (size_t x = 0; x < image.width; x++) {
  91. swap(image.imageP6[y * image.width + x].R, image.imageP6[image.size - image.width - y * image.width + x].R);
  92. swap(image.imageP6[y * image.width + x].G, image.imageP6[image.size - image.width - y * image.width + x].G);
  93. swap(image.imageP6[y * image.width + x].B, image.imageP6[image.size - image.width - y * image.width + x].B);
  94. }
  95. }
  96. }
  97. void VerticalMirrorImage(Image& image) {
  98. for (size_t y = 0; y < image.height; y++) {
  99. for (size_t x = 0; x < image.width / 2; x++) {
  100. swap(image.imageP6[y * image.width + x].R, image.imageP6[y * image.width + image.width - 1 - x].R);
  101. swap(image.imageP6[y * image.width + x].G, image.imageP6[y * image.width + image.width - 1 - x].G);
  102. swap(image.imageP6[y * image.width + x].B, image.imageP6[y * image.width + image.width - 1 - x].B);
  103. }
  104. }
  105. }
  106. void LeftRotateImage(Image& image) {
  107.  
  108. }
  109. void RightRotateImage(Image& image) {
  110.  
  111. }
  112.  
  113. int main(int argc, char** argv) {
  114. try {
  115. if (argc > 4)
  116. throw exception("ERROR. Many argumets");
  117. if (argc < 4)
  118. throw exception("ERROR. Few argumets");
  119. Image image;
  120. ReadImage(argv[1], image);
  121. switch (atoi(argv[3]))
  122. {
  123. case REVERS:
  124. ReversImage(image);
  125. break;
  126. case HORIZONTAL_MIRROR:
  127. HorisontalMirrorImage(image);
  128. break;
  129. case VERTICAL_MIRROR:
  130. VerticalMirrorImage(image);
  131. break;
  132. case LEFT_ROTATE:
  133. LeftRotateImage(image);
  134. break;
  135. case RIGHT_ROTATE:
  136. RightRotateImage(image);
  137. break;
  138. default:
  139. throw exception("ERROR. Command not found");
  140. break;
  141. }
  142. WriteImage(argv[2], image);
  143. cout << "Success!" << endl;
  144. }
  145. catch (const exception& error) {
  146. cerr << error.what() << endl;
  147. return -1;
  148. }
  149. return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement