Advertisement
LilChicha174

Untitled

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