Advertisement
Guest User

qazx

a guest
Feb 24th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. void Inversion(unsigned char *arr, unsigned char *new_arr, int height, int width) {
  7.     for (int i = 0; i < height; i++) {
  8.         for (int j = 0; j < width; j++) {
  9.             new_arr[i * width + j] = 255 - arr[i * width + j];
  10.         }
  11.     }
  12. }
  13.  
  14.  
  15. void HorizontalMirrorImage(unsigned char *arr, unsigned char *new_arr, int height, int width) {
  16.     for (int i = 0; i < height; i++) {
  17.         for (int j = 0; j < width; j++) {
  18.             new_arr[i * width + width - j - 1] = arr[i * width + j];
  19.         }
  20.     }
  21. }
  22.  
  23. void HorizontalMirrorImageColor(unsigned char *arr, unsigned char *new_arr, int height, int width) {
  24.     for (int i = 0; i < height; i += 1) {
  25.         for (int j = 0; j < width; j += 3) {
  26.             new_arr[i * width + width - j - 3] = arr[i * width + j];
  27.             new_arr[i * width + width - j - 2] = arr[i * width + j + 1];
  28.             new_arr[i * width + width - j - 1] = arr[i * width + j + 2];
  29.         }
  30.     }
  31. }
  32.  
  33. void VerticalMirrorImage(unsigned char *arr, unsigned char *new_arr, int height, int width) {
  34.     for (int i = 0; i < height; i++) {
  35.         for (int j = 0; j < width; j++) {
  36.             new_arr[(height - i - 1) * width + j] = arr[i * width + j];
  37.         }
  38.     }
  39. }
  40.  
  41. void VerticalMirrorImageColor(unsigned char *arr, unsigned char *new_arr, int height, int width) {
  42.     for (int i = 0; i < height; i += 1) {
  43.         for (int j = 0; j < width; j += 3) {
  44.             new_arr[(height - i - 1) * width + j + 0] = arr[i * width + j];
  45.             new_arr[(height - i - 1) * width + j + 1] = arr[i * width + j + 1];
  46.             new_arr[(height - i - 1) * width + j + 2] = arr[i * width + j + 2];
  47.         }
  48.     }
  49. }
  50.  
  51. void RightTurn(unsigned char *arr, unsigned char *new_arr, int height, int width) {
  52.     for (int i = 0; i < height; i++) {
  53.         for (int j = 0; j < width; j++) {
  54.             new_arr[j * height + height - i - 1] = arr[i * width + j];
  55.         }
  56.     }
  57.  
  58. }
  59.  
  60. void RightTurnColor(unsigned char *arr, unsigned char *new_arr, int height, int width) {
  61.     for (int i = 0; i < height; i += 3) {
  62.         for (int j = 0; j < width; j += 1) {
  63.             new_arr[j * height + height - i - 3] = arr[i * width + 3 * j];
  64.             new_arr[j * height + height - i - 2] = arr[i * width + 3 * j + 1];
  65.             new_arr[j * height + height - i - 1] = arr[i * width + 3 * j + 2];
  66.         }
  67.     }
  68.  
  69. }
  70.  
  71. void LeftTurn(unsigned char *arr, unsigned char *new_arr, int height, int width) {
  72.     for (int i = 0; i < height; i++) {
  73.         for (int j = 0; j < width; j++) {
  74.             new_arr[(width - j - 1) * height + i] = arr[i * width + j];
  75.         }
  76.     }
  77.  
  78. }
  79.  
  80. void LeftTurnColor(unsigned char *arr, unsigned char *new_arr, int height, int width) {
  81.     for (int i = 0; i < height; i += 3) {
  82.         for (int j = 0; j < width; j += 1) {
  83.             new_arr[(width - j - 1) * height + i] = arr[i * width + 3 * j];
  84.             new_arr[(width - j - 1) * height + i + 1] = arr[i * width + 3 * j + 1];
  85.             new_arr[(width - j - 1) * height + i + 2] = arr[i * width + 3 * j + 2];
  86.         }
  87.     }
  88.  
  89. }
  90.  
  91.  
  92. int main(int argc, char **argv) {
  93.     int type, width, height, dummy, BytesInPixel, Check;
  94.  
  95.  
  96.     FILE *File, *NewFile;
  97.     File = fopen(argv[1], "rb");
  98.  
  99.  
  100.     if (fscanf(File, "P%d", &type) == 0) {
  101.         return -1;
  102.     }; //&width, &height, &dummy);
  103.  
  104.  
  105.     fscanf(File, "%d %d\n%d\n", &width, &height, &dummy);
  106.  
  107.     if ((width < 1) || (height < 1)) {
  108.         return -1;
  109.     }
  110.  
  111.  
  112.     if (dummy != 255) {
  113.         return -1;
  114.     }
  115.  
  116.  
  117.     if (type == 5) {
  118.         BytesInPixel = 1;
  119.     } else if (type == 6) {
  120.         BytesInPixel = 3;
  121.     } else return -1;
  122.  
  123.  
  124.     unsigned char *arr = (unsigned char *) malloc((sizeof(unsigned char)) * BytesInPixel * width * height);
  125.     if (arr == NULL) {
  126.         return -1;
  127.     }
  128.  
  129.  
  130.     Check = fread(arr, sizeof(unsigned char), BytesInPixel * width * height, File);
  131.     Check += fread(arr, sizeof(unsigned char), 1, File);
  132.     if (Check != BytesInPixel * width * height) {
  133.         return -1;
  134.     }
  135.  
  136.     unsigned char *arr_new = (unsigned char *) malloc((sizeof(unsigned char)) * BytesInPixel * width * height);
  137.     if (arr_new == NULL) {
  138.         return -1;
  139.     }
  140.  
  141.     if (atoi(argv[3]) == 0) {
  142.         Inversion(arr, arr_new, height, width * BytesInPixel);
  143.     }
  144.     if (atoi(argv[3]) == 1) {
  145.         if (type == 5) {
  146.             HorizontalMirrorImage(arr, arr_new, height, width * BytesInPixel);
  147.         } else {
  148.             HorizontalMirrorImageColor(arr, arr_new, height, width * BytesInPixel);
  149.         }
  150.     }
  151.  
  152.     if (atoi(argv[3]) == 2) {
  153.         if (type == 5) {
  154.             VerticalMirrorImage(arr, arr_new, height, width * BytesInPixel);
  155.         } else {
  156.             VerticalMirrorImageColor(arr, arr_new, height, width * BytesInPixel);
  157.         }
  158.     }
  159.  
  160.     if (atoi(argv[3]) == 3) {
  161.         if (type == 5) {
  162.             RightTurn(arr, arr_new, height, width);
  163.         } else {
  164.             RightTurnColor(arr, arr_new, height * BytesInPixel, width);
  165.         }
  166.     }
  167.  
  168.     if (atoi(argv[3]) == 4) {
  169.         if (type == 5) {
  170.             LeftTurn(arr, arr_new, height, width);
  171.         } else {
  172.             LeftTurnColor(arr, arr_new, height * BytesInPixel, width);
  173.         }
  174.     }
  175.     if ((atoi(argv[3]) != 0) && (atoi(argv[3]) != 1) && (atoi(argv[3]) != 2) && (atoi(argv[3]) != 3) &&
  176.         (atoi(argv[3]) != 4)) {
  177.         return -1;
  178.     }
  179.     NewFile = fopen(argv[2], "wb");
  180.     fprintf(NewFile, "P%d\n", type);
  181.     if (atoi(argv[3]) < 3) {
  182.         fprintf(NewFile, "%d %d\n", width, height);
  183.     } else {
  184.         fprintf(NewFile, "%d %d\n", height, width);
  185.     }
  186.     fprintf(NewFile, "%d\n", dummy);
  187.     fwrite(arr_new, sizeof(unsigned char), BytesInPixel * height * width, NewFile);
  188.     fclose(File);
  189.     fclose(NewFile);
  190.     free(arr);
  191.     free(arr_new);
  192.     return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement