Скачиваний:
39
Добавлен:
10.02.2019
Размер:
4.85 Кб
Скачать
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#define ERROR 0.06

#pragma pack(push,1)
typedef struct BitMapFileHeader{
	uint16_t bfType; 
	uint32_t bfSize; 
	uint16_t  bfReserved1; 
	uint16_t  bfReserved2; 
	uint32_t  bfOffsetBits; 
} BitMapFileHeader;

typedef struct BitMapInfoHeader{
	uint32_t biSize; 
	uint32_t  biWidth; 
	uint32_t biHeight; 
	uint16_t  biPlanes; 
	uint16_t  biBitCount; 
	uint32_t  biCompression; 
	uint32_t  biSizeImage; 
	uint32_t biXpenlsPerMeter;
	uint32_t biYpenlsPerMeter;
	uint32_t biColorUsed;
	uint32_t biColorsImportant;
} BitMapInfoHeader;

typedef struct RGB{
	uint8_t  blue;
	uint8_t  green;
	uint8_t  red;
} RGB;

typedef struct Point{
	uint32_t X;
	uint32_t Y;
} Point;

typedef struct BmpBasket{
	uint32_t  width; //4
	uint32_t height; 
	Point top1;
	Point top2;
	Point top3;
	Point top4;
} BmpBasket;

typedef struct Vector{
	int32_t X;
	int32_t Y;
} Vector;
#pragma pack(pop)

int rectangle(BmpBasket* basket, uint8_t arr[basket->height][basket->width]){
	Vector vector1_2 = {basket->top2.X-basket->top1.X,basket->top2.Y-basket->top1.Y},
		   vector2_3 = {basket->top3.X-basket->top2.X,basket->top3.Y-basket->top2.Y},
		   vector3_4 = {basket->top4.X-basket->top3.X,basket->top4.Y-basket->top3.Y},
		   vector4_1 = {basket->top1.X-basket->top4.X,basket->top1.Y-basket->top4.Y};
		
	double v1_len = sqrt(pow(vector1_2.X,2) + pow(vector1_2.Y,2));
	double v2_len = sqrt(pow(vector2_3.X,2) + pow(vector2_3.Y,2));
	double v3_len = sqrt(pow(vector3_4.X,2) + pow(vector3_4.Y,2));
	double v4_len = sqrt(pow(vector4_1.X,2) + pow(vector4_1.Y,2));
		
	int vector_work = vector1_2.X * vector2_3.X + vector1_2.Y*vector2_3.Y;
	if(vector_work < 0) 
		vector_work *= -1;
	int vector_multiply = v1_len * v2_len;
	
	if((v1_len - v3_len) <= ERROR && (v2_len - v4_len) <= ERROR && vector_multiply != 0
		&& (vector_work/vector_multiply) <= ERROR){	
			for(uint32_t i = basket->top4.Y; i < basket->top1.Y; i++)
				for (uint32_t j = basket->top4.X; j < basket->top3.X; j++)
					if(arr[i][j] != 2)
						return -1;

		return v1_len * v2_len;
	}
	return -1;
}

void search_tops(uint32_t y,uint32_t x,BmpBasket* basket, uint8_t arr[basket->height][basket->width]){ 
    arr[y][x]= 2;
    
    if((y >= basket->top1.Y) && (x <= basket->top1.X)) {
    	basket->top1.X = x; basket->top1.Y = y;
    }
    if((y >= basket->top2.Y) && (x >= basket->top2.X)) {
    	basket->top2.X = x; basket->top2.Y = y;
    }
    if((y <= basket->top3.Y) && (x >= basket->top3.X)) {
    	basket->top3.X = x; basket->top3.Y = y;
    }
    if((y <= basket->top4.Y) && (x <= basket->top4.X)) {
    	basket->top4.X = x; basket->top4.Y = y;
    }
 
    if((x < (basket->width-1)) && (arr[y][x+1] == 1)){
        search_tops(y,x+1,basket,arr);
    }
    if((y < (basket->height-1)) && (arr[y+1][x] == 1)){
        search_tops(y+1,x,basket,arr);
    }
    if((x > 0) && (arr[y][x-1] == 1)){
        search_tops(y,x-1,basket,arr);
    }
    if((y > 0) && (arr[y-1][x] == 1)){
        search_tops(y-1,x,basket,arr);
    }
    return;
}

int main(){
	BitMapFileHeader file_header;
	BitMapInfoHeader info_header;
	RGB rgb;
	BmpBasket basket;
	Point p1_max;
	Point p2_max;
	char input_file[20];
	
	printf("Enter file name: ");
	scanf("%s", input_file);
	
	FILE* file = fopen(input_file,"rb");
	if(!file){
		printf("Fail with file\n");
		return 0;
	}
	fread(&file_header, sizeof(struct BitMapFileHeader), 1, file);
	fread(&info_header, sizeof(struct BitMapInfoHeader), 1, file);
	
	basket.height = info_header.biHeight;
	basket.width = info_header.biWidth;
	uint8_t pixel_grid[basket.height][basket.width];
	
	uint8_t empty_bytes = 0;
	if ((basket.width * 3) % 4) {
		empty_bytes = 4 -( (basket.width * 3) % 4);
	}

	for (int32_t i = basket.height -1; i >= 0 ; i--) {
		for (uint32_t j = 0; j < basket.width; j++) {
			fread(&rgb, sizeof(RGB), 1, file);
			
			if(rgb.red == 255 && rgb.blue == 255 && rgb.green == 255){
				pixel_grid[i][j] = 1;
			}else{
				pixel_grid[i][j] = 0;
			}	
		}
		if (empty_bytes) {
			fread(&rgb, empty_bytes, 1, file);
		}
	}

	int32_t S,S_max = 0;
	for (uint32_t i = 0; i < basket.height; i++) {
		for (uint32_t j = 0; j < basket.width; j++) {
			if(pixel_grid[i][j] == 1){	
			    basket.top1.X = j; basket.top1.Y = i;
    			basket.top2.X = j; basket.top2.Y = i;
    			basket.top3.X = j; basket.top3.Y = i;
    			basket.top4.X = j; basket.top4.Y = i;

				search_tops(i,j,&basket,pixel_grid);
				S = rectangle(&basket,pixel_grid);
					
				if( S > S_max){
					S_max = S;
					p1_max.X = basket.top1.X; p1_max.Y = basket.top1.Y; 
					p2_max.X = basket.top3.X; p2_max.Y = basket.top3.Y; 		
				}
			}
			pixel_grid[i][j] = 2;	
		}	
	}	
	
	if(S_max > 0){
		printf("X1:%5d	Y1:%5d\n",p1_max.X,p1_max.Y);
		printf("X2:%5d	Y2:%5d\n",p2_max.X,p2_max.Y);
	}else{
		printf("Not rectangle\n");
	}

	fclose(file);
	return 0;
}
Соседние файлы в папке Kursach