#include #include #include //includes strcmp #include //isspace typedef enum { FEW_ARG, WRONG_ARG, MANY_ARG, NO_HLINE, NO_VLINE, WRONG_FILE, NO_SQUARE } ERRORS; //indicates a type of an error typedef enum { INVALID, VALID }MY_BOOL; typedef struct { int rows; int cols; char *cells; } Bitmap; void Help(); void Error(ERRORS err); void Tisk_bool(MY_BOOL validni); char* alloc_Bitmap(int vyska, int sirka, Bitmap* stru); void free_Bitmap(Bitmap arr); int test(char* soubor, Bitmap* bitmapa); void test_bitmap(Bitmap* neco); char getcolor(Bitmap *bitmap, int x, int y); int find_vline(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2); int find_hline(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2); int find_square(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2); int main(int argc, char *argv[]) { Bitmap struktura; int x1,x2,y1,y2; int found; switch (argc) { case 1: Error(FEW_ARG); break; case 2: if (strcmp(argv[1], "--help" ) == 0) Help(); else Error(WRONG_ARG); break; case 3: if (strcmp(argv[1], "--test" ) == 0) { Tisk_bool(test(argv[2], &struktura)); //zkontroluje a vypíše } else if (strcmp(argv[1], "--hline" ) == 0) { if(test(argv[2], &struktura ) == 1) //soubor obsahuje bitmapu { found = find_hline(&struktura, &x1, &y1, &x2, &y2); if (found == 0) { Error(NO_HLINE); } else { printf("%d %d %d %d\n", x1, y1, x2, y2); } } else { Error(WRONG_FILE); } } else if (strcmp(argv[1], "--vline" ) == 0) { if(test(argv[2], &struktura ) == 1) //soubor obsahuje bitmapu { found = find_vline(&struktura, &x1, &y1, &x2, &y2); if (found == 0) { Error(NO_VLINE); } else { printf("%d %d %d %d\n", x1, y1, x2, y2); } } else { Error(WRONG_FILE); } } else if (strcmp(argv[1], "--square" ) == 0) { if(test(argv[2], &struktura ) == 1) //soubor obsahuje bitmapu { found = find_square(&struktura, &x1, &y1, &x2, &y2); if (found == 0) { Error(NO_SQUARE); } else { printf("%d %d %d %d\n", x1, y1, x2, y2); } } else { Error(WRONG_FILE); } } else Error(WRONG_ARG); break; default: Error(MANY_ARG); break; } free(struktura.cells); return 0; } void Help() { printf("Program: 3rd project for IZP - Searching of shapes\n" "Author: Eliska Chalupova (c) 2013\n \n" "Control: \n" "--help prints how to use the program\n" "--test checks if a file contains a bitmap picture or not\n" "--hline prints coordinates of the first longest horizontal line segment of a picture\n" "--vline prints coordinates of the first longest vertical line segment of a picture\n" "--square prints coordinates of the first biggest square of a picture\n"); } void Error(ERRORS err) { switch(err) { case FEW_ARG: fprintf(stderr, "You have not written enough arguments. For help use --help\n"); break; case WRONG_ARG: fprintf(stderr, "You have used wrong arguments. For help use --help\n"); break; case MANY_ARG: fprintf(stderr, "You have written too many arguments. For help use --help\n"); break; case NO_HLINE: fprintf(stderr, "There is no horizontal line in the file.\n"); break; case NO_VLINE: fprintf(stderr, "There is no vertical line in the file.\n"); break; case NO_SQUARE: fprintf(stderr, "There is no square in the file.\n"); break; case WRONG_FILE:fprintf(stderr, "The file does not contain a correct bitmap.\n"); break; } } void Tisk_bool(MY_BOOL validni) { switch (validni) { case INVALID: printf("Invalid\n"); break; case VALID: printf("Valid\n"); break; } } int test(char* soubor, Bitmap* bitmapa) { FILE* p_file; p_file = fopen(soubor,"r"); if (p_file == NULL) { return INVALID; } else //soubor se otevøel { int RP = 1; //pro switch int pocitadlo = 0; //pocitadlo jednicek a nul int c_predchozi = 0; int radky = 0; int sloupce = 0; int c = 0; Bitmap struktura; char* alok_pole = NULL; int uz_nealokuj = 0; //aby se krok 3 neopakoval vícekrát while ((c = fgetc(p_file))!=EOF) { if(isspace(c)==0) //neni mezera { if(isdigit(c)!=0) //je číslo { int c_cislo = c - '0'; switch(RP) { case 1: radky = c_predchozi*10 + c_cislo; c_predchozi = c_cislo; //printf("radky: %d", radky); //OK bitmapa->rows = radky; break; case 2: sloupce = c_predchozi*10 + c_cislo; c_predchozi = c_cislo; //printf(" sloupce: %d", sloupce); //OK bitmapa->cols = sloupce; break; case 3: if((c == '1' || c == '0') && pocitadlo <= (radky * sloupce)) { alok_pole[pocitadlo] = c; pocitadlo++; } else { if (alok_pole != NULL) { free(alok_pole); alok_pole = NULL; } return INVALID; } break; } } else //není èíslo { if (alok_pole != NULL) { free(alok_pole); alok_pole = NULL; } return INVALID; } } else //je mezera { if(radky!=0 && sloupce == 0) { RP = 2; c_predchozi = 0; } if(sloupce!=0 && uz_nealokuj == 0) { alok_pole = alloc_Bitmap(radky, sloupce, &struktura); //nacte radky, sloupce a alokuje pole o urcite velikosti ve strukture uz_nealokuj = 1; RP = 3; } } } if (pocitadlo < (radky*sloupce)) // { if (alok_pole != NULL) { free(alok_pole); alok_pole = NULL; } return INVALID; } bitmapa->cells = alok_pole; } return VALID; } char* alloc_Bitmap(int vyska, int sirka, Bitmap* stru) { stru->rows = vyska; stru->cols = sirka; stru->cells = malloc((vyska*sirka)*sizeof(char)); return stru->cells; //ukazatel } void free_Bitmap(Bitmap stru) { free(stru.cells); stru.cells = NULL; //!!!!! } void test_bitmap(Bitmap* neco) { for(int i = 0; i < neco->rows*neco->cols; i++) { printf("%c ",neco->cells[i]); } } char getcolor(Bitmap *bitmap, int x, int y) { return bitmap->cells[(((bitmap->cols)*y) + x)]; } int find_vline(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2) { *x1 = 1; *y1 = 3; *x2 = 4; *y2 = 9; return 0; } int find_hline(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2) { int pocitadlo = 0; char predch = '0'; int nejdelsi = 0; int konec = 0; char akt; for (int j = 0; j < bitmap->rows; j++) { for(int i = 0; i < bitmap->cols; i++) { akt = getcolor(bitmap, i, j); printf("%d . ", akt); //if(akt!='1' || akt!='0'){exit(100);} if (i == bitmap->cols - 1) { konec = 1; } if (akt == '1') { pocitadlo++; } if(akt == '0') { pocitadlo = 0; } if ((akt == '0' || konec == 1) && predch == '1' && pocitadlo > nejdelsi) { nejdelsi = pocitadlo; pocitadlo = 0; if (nejdelsi >= 2) { *x2 = i; *y2 = j; *x1 = i - nejdelsi; *y1 = j; } else { return 10; } } predch = akt; } pocitadlo = 0; predch = 0; konec = 0; } return 1; } int find_square(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2) { *x1 = 1; *y1 = 8; *x2 = 4; *y2 = 9; return 0; }