Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bmp.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- // реализация очереди, для обхода по фону
- int *qi;
- int *qj;
- int *was;
- int head;
- int tail;
- int H, W;
- void push(int i, int j){
- if (*(was + (i * W + j))){
- return;
- }
- *(was + (i * W + j)) = 1;
- *(qi + head) = i;
- *(qj + head) = j;
- head++;
- }
- void pop(){
- tail++;
- }
- int front_i(){
- return *(qi + tail);
- }
- int front_j(){
- return *(qj + tail);
- }
- int q_size(){
- return head - tail;
- }
- int *get_was(){
- return was;
- }
- void build (int h, int w){
- H = h;
- W = w;
- head = 0;
- tail = 0;
- qi = (int *)malloc(w * h * sizeof(int));
- qj = (int *)malloc(w * h * sizeof(int));
- was = (int *)malloc(w * h * sizeof(int));
- }
- // сравнение 2 строк
- int strf(char *a, char *b){
- if (strlen(a) != strlen(b)){
- return 0;
- }
- for (int i = 0; i < strlen(a); i++){
- if (a[i] != b[i]){
- return 0;
- }
- }
- return 1;
- }
- // проверка на соотвествие критериям отбора фона
- int check(int i, int j, int x, int y, bmp_image image, double p){
- if (x < 0 || x >= H || y < 0 || y >= W){
- return 0;
- }
- if (fabs(image.pixel_array[0 * H * W + i * W + j] - image.pixel_array[0 * H * W + x * W + y]) >= p){
- return 0;
- }
- if (fabs(image.pixel_array[1 * H * W + i * W + j] - image.pixel_array[1 * H * W + x * W + y]) >= p){
- return 0;
- }
- if (fabs(image.pixel_array[2 * H * W + i * W + j] - image.pixel_array[2 * H * W + x * W + y]) >= p){
- return 0;
- }
- return 1;
- }
- void bfs(double k, bmp_image image, int si, int sj){
- push(si, sj);
- while (q_size()){
- int i = front_i();
- int j = front_j();
- pop();
- if (check(si, sj, i - 1, j - 1, image, k)){
- push(i - 1, j - 1);
- }
- if (check(si, sj, i - 1, j , image, k)){
- push(i - 1, j );
- }
- if (check(si, sj, i - 1, j + 1, image, k)){
- push(i - 1, j + 1);
- }
- if (check(si, sj, i , j - 1, image, k)){
- push(i , j - 1);
- }
- if (check(si, sj, i , j + 1, image, k)){
- push(i , j + 1);
- }
- if (check(si, sj, i + 1, j - 1, image, k)){
- push(i + 1, j - 1);
- }
- if (check(si, sj, i + 1, j , image, k)){
- push(i + 1, j );
- }
- if (check(si, sj, i + 1, j + 1, image, k)){
- push(i + 1, j + 1);
- }
- }
- }
- // основная функция
- void bmp_replace(bmp_image image, bmp_image background, double k, int argc, char *argv[])
- {
- /* Получаем линейные размеры изображения */
- unsigned int w = image.header.width;
- unsigned int h = image.header.height;
- build(h, w);
- for (int i = 0; i < argc; i++){
- if (strf(argv[i], "DL") || strf(argv[i], "LD")){
- bfs(k, image, 0, 0);
- }
- if (strf(argv[i], "LU") || strf(argv[i], "UL")){
- bfs(k, image, h - 1, 0);
- }
- if (strf(argv[i], "RD") || strf(argv[i], "DR")){
- bfs(k, image, 0, w -1);
- }
- if (strf(argv[i], "RU") || strf(argv[i], "UR")){
- bfs(k, image, h - 1, w - 1);
- }
- if (strf(argv[i], "D")){
- bfs(k, image, 0, w / 2);
- }
- if (strf(argv[i], "U")){
- bfs(k, image, h - 1, w / 2);
- }
- if (strf(argv[i], "L")){
- bfs(k, image, h / 2, 0);
- }
- if (strf(argv[i], "R")){
- bfs(k, image, h / 2, w - 1);
- }
- if (strf(argv[i], "C")){
- bfs(k, image, h / 2, w / 2);
- }
- }
- int *was = get_was();
- /* Для всех строк пикселей */
- for (unsigned int i = 0; i < h; i++) {
- /* Для каждого пикселя */
- for (unsigned int j = 0; j < w; j++) {
- if (!was[i * w + j]){
- continue;
- }
- image.pixel_array[0 * h * w + i * w + j] = background.pixel_array[0 * h * w + i * w + j];
- image.pixel_array[1 * h * w + i * w + j] = background.pixel_array[1 * h * w + i * w + j];
- image.pixel_array[2 * h * w + i * w + j] = background.pixel_array[2 * h * w + i * w + j];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement