Advertisement
LilChicha174

Untitled

May 25th, 2022
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 KB | None | 0 0
  1. int check_X_Y(struct Png *image, int x0, int y0, int *x1, int *y1, int *x2, int *y2, int *x3, int
  2. *y3, int *x4, int *y4, int width_pixel, int red0, int green0, int blue0) {
  3.     int x1t = 0, y1t = 0, x2t = 0, y2t = 0, x3t = 0, y3t = 0, x4t = 0, y4t = 0;
  4.     int flag = 0;
  5.     png_byte *row = image->row_pointers[y0];
  6.     for (int x = x0; x < image->width; x++) {
  7.         png_byte *ptr = &(row[x * width_pixel]);
  8.         if (ptr[0] == red0 && ptr[1] == green0 && ptr[2] == blue0) {
  9.             if (flag == 0) {
  10.                 x1t = x, y1t = y0;
  11.                 flag++;
  12.             }
  13.         } else {
  14.             if (x != (x0 + 1)) {
  15.                 x2t = x - 1, y2t = y0;
  16.                 flag++;
  17.                 break;
  18.             } else {
  19.                 return 0;
  20.             }
  21.         }
  22.     }
  23.     for (int y = y0; y < image->height; y++) {
  24.         png_byte *row = image->row_pointers[y];
  25.         png_byte *ptr = &(row[x0 * width_pixel]);
  26.         if (ptr[0] != red0 || ptr[1] != green0 || ptr[2] != blue0) {
  27.             if (y != (y0 + 1) && flag == 2) {
  28.                 y3t = y - 1, x3t = x0;
  29.                 flag++;
  30.             }
  31.         }
  32.     }
  33.     for (int y = y0; y < image->height; y++) {
  34.         png_byte *row = image->row_pointers[y];
  35.         png_byte *ptr = &(row[x1t * width_pixel]);
  36.         if (ptr[0] != red0 || ptr[1] != green0 || ptr[2] != blue0) {
  37.             if (y != (y0 + 1) && flag == 3) {
  38.                 y4t = y - 1, x4t = x2t;
  39.                 flag++;
  40.             }
  41.         }
  42.     }
  43.     if (y4t != y3t)
  44.         return 0;
  45.     row = image->row_pointers[y3t];
  46.  
  47.     for (int x = x3t; x < x4t; x++) {
  48.         png_byte *ptr = &(row[x * width_pixel]);
  49.         if (ptr[0] != red0 || ptr[1] != green0 || ptr[2] != blue0) {
  50.             return 0;
  51.         }
  52.     }
  53.     *x1 = x1t, *y1 = y1t;
  54.     *x2 = x2t, *y2 = y2t;
  55.     *x3 = x3t, *y3 = y3t;
  56.     *x4 = x4t, *y4 = y4t;
  57.     return 1;
  58.  
  59. }
  60.  
  61. //перекраска прямоугольника
  62. void pouring_rectangle(struct Png *image, int width_pixel, int x, int y, int Red, int Green, int
  63. Blue, int Red_l, int Green_l, int Blue_l) {
  64.     if (!is_not_painted(image, width_pixel, x, y, Red, Green, Blue, Red, Green, Blue)) {
  65.         png_byte *row = image->row_pointers[y];
  66.         png_byte *ptr = &(row[x * width_pixel]);
  67.         ptr[0] = Red_l;
  68.         ptr[1] = Green_l;
  69.         ptr[2] = Blue_l;
  70.         pouring_rectangle(image, width_pixel, x + 1, y, Red, Green, Blue, Red_l, Green_l, Blue_l);
  71.         pouring_rectangle(image, width_pixel, x - 1, y, Red, Green, Blue, Red_l, Green_l, Blue_l);
  72.         pouring_rectangle(image, width_pixel, x, y + 1, Red, Green, Blue, Red_l, Green_l, Blue_l);
  73.         pouring_rectangle(image, width_pixel, x, y - 1, Red, Green, Blue, Red_l, Green_l, Blue_l);
  74.     }
  75. }
  76.  
  77. // поиск прямоугольника макс площади
  78. int is_rectangle(struct Png *image, int width_pixel, int red0, int green0, int blue0, int red1, int
  79. green1, int blue1) {
  80.     int x1, y1, x2, y2, x3, y3, x4, y4;
  81.     int square, square_max = 0;
  82.     int x1_max, y1_max, x2_max, y2_max, x3_max, y3_max, x4_max, y4_max;
  83.     for (int y = 0; y < image->height; y++) {
  84.         png_byte *row = image->row_pointers[y];
  85.         for (int x = 0; x < image->width; x++) {
  86.             png_byte *ptr = &(row[x * width_pixel]);
  87.             if (ptr[0] == red0 && ptr[1] == green0 && ptr[2] == blue0) {
  88.                 if (check_X_Y(image, x, y, &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4,
  89.                               width_pixel, red0, green0, blue0)) {
  90.                     int square = sqrt((x2 - x1) * (x2 - x1)) * sqrt((y3 - y1) * (y3 - y1));
  91.                     if (square > square_max) {
  92.                         square_max = square;
  93.                         x1_max = x1, y1_max = y1, x2_max = x2, y2_max = y2, x3_max = x3, y3_max =
  94.                                 y3, x4_max = x4, y4_max = y4;
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.     }
  100.     pouring_rectangle(image, width_pixel, x1_max, y1_max, red0, green0, blue0, red1, green1, blue1);
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement