Advertisement
kolychestiy

bmp_replace.c

Dec 12th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.54 KB | None | 0 0
  1. #include "bmp.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. // реализация очереди, для обхода по фону
  8. int *qi;
  9. int *qj;
  10.  
  11. int *was;
  12.  
  13. int head;
  14. int tail;
  15.  
  16. int H, W;
  17.  
  18. void push(int i, int j){
  19.     if (*(was + (i * W + j))){
  20.         return;
  21.     }
  22.  
  23.     *(was + (i * W + j)) = 1;
  24.     *(qi + head) = i;
  25.     *(qj + head) = j;
  26.  
  27.     head++;
  28. }
  29.  
  30. void pop(){
  31.     tail++;
  32. }
  33.  
  34. int front_i(){
  35.     return *(qi + tail);
  36. }
  37. int front_j(){
  38.     return *(qj + tail);
  39. }
  40.  
  41. int q_size(){
  42.     return head - tail;
  43. }
  44.  
  45. int *get_was(){
  46.     return was;
  47. }
  48.  
  49. void build (int h, int w){
  50.     H = h;
  51.     W = w;
  52.  
  53.     head = 0;
  54.     tail = 0;
  55.  
  56.     qi = (int *)malloc(w * h * sizeof(int));
  57.     qj = (int *)malloc(w * h * sizeof(int));
  58.  
  59.     was = (int *)malloc(w * h * sizeof(int));        
  60. }
  61.  
  62.  
  63.  
  64.  
  65. // сравнение 2 строк
  66. int strf(char *a, char *b){
  67.     if (strlen(a) != strlen(b)){
  68.         return 0;
  69.     }
  70.  
  71.     for (int i = 0; i < strlen(a); i++){
  72.         if (a[i] != b[i]){
  73.             return 0;
  74.         }
  75.     }
  76.  
  77.     return 1;
  78. }
  79.  
  80. // проверка на соотвествие критериям отбора фона
  81. int check(int i, int j, int x, int y, bmp_image image, double p){
  82.     if (x < 0 || x >= H || y < 0 || y >= W){
  83.         return 0;
  84.     }
  85.  
  86.     if (fabs(image.pixel_array[0 * H * W + i * W + j] - image.pixel_array[0 * H * W + x * W + y]) >= p){
  87.         return 0;
  88.     }
  89.     if (fabs(image.pixel_array[1 * H * W + i * W + j] - image.pixel_array[1 * H * W + x * W + y]) >= p){
  90.         return 0;
  91.     }
  92.     if (fabs(image.pixel_array[2 * H * W + i * W + j] - image.pixel_array[2 * H * W + x * W + y]) >= p){
  93.         return 0;
  94.     }
  95.  
  96.     return 1;
  97. }
  98.  
  99.  
  100. void bfs(double k, bmp_image image, int si, int sj){
  101.     push(si, sj);
  102.  
  103.     while (q_size()){
  104.  
  105.         int i = front_i();
  106.         int j = front_j();
  107.  
  108.         pop();
  109.  
  110.         if (check(si, sj, i - 1, j - 1, image, k)){
  111.             push(i - 1, j - 1);
  112.         }
  113.         if (check(si, sj, i - 1, j    , image, k)){
  114.             push(i - 1, j    );
  115.         }
  116.         if (check(si, sj, i - 1, j + 1, image, k)){
  117.             push(i - 1, j + 1);
  118.         }
  119.         if (check(si, sj, i    , j - 1, image, k)){
  120.             push(i    , j - 1);
  121.         }
  122.         if (check(si, sj, i    , j + 1, image, k)){
  123.             push(i    , j + 1);
  124.         }
  125.         if (check(si, sj, i + 1, j - 1, image, k)){
  126.             push(i + 1, j - 1);
  127.         }
  128.         if (check(si, sj, i + 1, j    , image, k)){
  129.             push(i + 1, j    );
  130.         }
  131.         if (check(si, sj, i + 1, j + 1, image, k)){
  132.             push(i + 1, j + 1);
  133.         }
  134.  
  135.     }
  136.  
  137.  
  138. }
  139.  
  140.  
  141. // основная функция
  142. void bmp_replace(bmp_image image, bmp_image background, double k, int argc, char *argv[])
  143. {
  144.  
  145.     /* Получаем линейные размеры изображения */
  146.     unsigned int w = image.header.width;
  147.     unsigned int h = image.header.height;
  148.  
  149.     build(h, w);
  150.  
  151.     for (int i = 0; i < argc; i++){
  152.         if (strf(argv[i], "DL") || strf(argv[i], "LD")){
  153.             bfs(k, image, 0, 0);
  154.         }
  155.         if (strf(argv[i], "LU") || strf(argv[i], "UL")){
  156.             bfs(k, image, h - 1, 0);
  157.         }
  158.         if (strf(argv[i], "RD") || strf(argv[i], "DR")){
  159.             bfs(k, image, 0, w -1);
  160.         }
  161.         if (strf(argv[i], "RU") || strf(argv[i], "UR")){
  162.             bfs(k, image, h - 1, w - 1);
  163.         }
  164.  
  165.         if (strf(argv[i], "D")){
  166.             bfs(k, image, 0, w / 2);
  167.         }
  168.         if (strf(argv[i], "U")){
  169.             bfs(k, image, h - 1, w / 2);
  170.         }
  171.         if (strf(argv[i], "L")){
  172.             bfs(k, image, h / 2, 0);
  173.         }
  174.         if (strf(argv[i], "R")){
  175.             bfs(k, image, h / 2, w - 1);
  176.         }
  177.  
  178.         if (strf(argv[i], "C")){
  179.             bfs(k, image, h / 2, w / 2);
  180.         }
  181.     }
  182.  
  183.     int *was = get_was();
  184.  
  185.     /* Для всех строк пикселей */
  186.     for (unsigned int i = 0; i < h; i++) {
  187.         /* Для каждого пикселя */
  188.         for (unsigned int j = 0; j < w; j++) {
  189.            
  190.             if (!was[i * w + j]){
  191.                 continue;
  192.             }
  193.  
  194.             image.pixel_array[0 * h * w + i * w + j] = background.pixel_array[0 * h * w + i * w + j];
  195.             image.pixel_array[1 * h * w + i * w + j] = background.pixel_array[1 * h * w + i * w + j];
  196.             image.pixel_array[2 * h * w + i * w + j] = background.pixel_array[2 * h * w + i * w + j];
  197.         }
  198.     }
  199.  
  200. }
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement