Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <time.h>
- // czy na pewno powinienne wartoć z nagłówka pliku PGM wklepywać na sztywno?
- #define GREYMAX 65535
- /*
- #define _CRT_SECURE_NO_DEPRECATE
- #ifndef _MSC_VER
- #endif
- */
- struct obraz
- {
- char* name;
- char standard[10];
- int width;
- int hight;
- int range; // zakres szarosci
- int** valueOfPicture;
- char comments[200];
- };
- // read and write to file
- void odczyt_PGM(struct obraz* ObrazX);
- bool isComment(FILE** fp);
- bool zapis_PGM(struct obraz obrazX);
- // the Base
- void base_control(struct obraz** Base, struct obraz* activeImage, size_t* imageCount);
- void activeImage_operations(struct obraz* activeImage); // submenu
- // baseOfImages functions
- void base_addImage(struct obraz** baseOfImages, size_t* imageCount); //
- char* getString(); // use in base add
- void delete(struct obraz** baseOfImages, size_t* imageCount); //
- void display_base(struct obraz* baseOfImages, size_t imageCount); //
- void select_image(struct obraz** baseOfImages, struct obraz* activeImage, size_t imageCount); //
- // operations on activeImage
- void progowanie(struct obraz* obrazX); //
- void histogram(struct obraz* obrazX); //
- void Zaszumienie(struct obraz* obrazX); ///
- void negatyw(struct obraz* obrazX); //
- void rotate(struct obraz* obrazX); //
- void filtr_medianowy(struct obraz* obrazX); //
- void quick_sort(int arr[], int low, int high);
- int partition(int arr[], int low, int high);
- void swap(int* A, int* B);
- int main()
- {
- /* [5] base of images */
- // limit 5 obrazow
- struct obraz** baseOfImages = (struct obraz**) malloc(1 * sizeof(struct obraz*)); // base of images
- struct obraz activeImage; // image for operations
- char menuControl;
- size_t imageCount = 0; // count of images in base
- //odczyt_PGM()
- base_control(baseOfImages, &activeImage, &imageCount);
- free(baseOfImages);
- return 0;
- }
- void odczyt_PGM(struct obraz* obrazX) // true - sucessfull read | false -error
- {
- FILE* fp;
- fp = fopen(obrazX->name, "r");
- printf("obrazX->name %s\n", obrazX->name);
- if (ferror(fp) == 0) // flaga blendu nie ustawiona
- {
- fscanf(fp, "%s ", obrazX->standard);
- isComment(&fp);
- fscanf(fp, "%d", &obrazX->width);
- isComment(&fp);
- fscanf(fp, "%d", &obrazX->hight);
- isComment(&fp);
- fscanf(fp, "%d", &(obrazX->range));
- isComment(&fp);
- // allokacja pamieci na piksele
- // tu się musi psuć aaaaaaaa
- obrazX->valueOfPicture = malloc(obrazX->hight * sizeof(obrazX->valueOfPicture));
- for (int i = 0; i < obrazX->hight; i++)
- obrazX->valueOfPicture[i] = malloc(obrazX->width * sizeof(obrazX->valueOfPicture[i])); // obsługę errorów
- // wpisywanie do pikseli
- for (int i = 0; i < obrazX->hight; i++)
- for (int j = 0; j < obrazX->width; j++)
- {
- if (isComment(&fp)) {
- fscanf(fp, "#%s ", obrazX->comments);
- j--;
- }
- else
- fscanf(fp, "%d", &(obrazX->valueOfPicture[i][j]));
- }
- if(fp != NULL && fp != 0)
- fclose(fp);
- //for(int i = 0; i < obrazX.hight; i++) // przemysl jeszcze tu chyba nie potrzebne zwalnianie
- //free(obrazX.valueOfPicture[i]); // każdy wiersz
- //free(obrazX.valueOfPicture); // tablica wskanikow
- //free(obrazX.name);
- }
- else {
- if (fp != NULL && fp != 0)
- fclose(fp);
- fputs("error with opening file\n", stderr);
- }
- }
- bool isComment(FILE** fp)
- {
- char buf[5];
- if (fgetc(*fp) == '#')
- {
- fgets(buf, 5, *fp);
- return true;
- }
- else{
- fseek(*fp, -1, SEEK_CUR);
- return false;
- }
- }
- // 1 add image // tu pomyl z alokacja tu i w odczycie
- void base_addImage(struct obraz** baseOfImages, size_t* imageCount)
- {
- struct obraz* tmp;
- printf("write in name of file which you want to add to base\n");
- char* fileName = getString();
- printf("nazwa wpisana do fileName %s\n", fileName);
- *imageCount = *imageCount + 1;
- tmp = realloc(*baseOfImages, *imageCount * sizeof(struct obraz*));
- if(tmp != NULL){
- *baseOfImages = tmp;
- baseOfImages[*imageCount - 1] = malloc(sizeof(struct obraz));
- baseOfImages[*imageCount - 1]->name = fileName;
- odczyt_PGM(baseOfImages[*imageCount - 1]);
- } else{
- *imageCount = *imageCount - 1;
- }
- }
- char* getString()
- {
- char* str = calloc(256, sizeof(char));// nie, nie przekroczysz tego rozmiaru, więc daj sobie spokój z reallockiem tutaj
- scanf("%256s", str);
- return str;
- }
- // 2 delete image
- void base_delete(struct obraz** baseOfImages, size_t* imageCount)
- {
- int toDeleat;
- scanf("%d", &toDeleat);
- *imageCount--;
- // zamień element to dealet z ostatnim i reallocuj pamięć
- *baseOfImages = realloc(*baseOfImages, *imageCount * sizeof(struct obraz));
- }
- // 3 display images list
- void base_display(struct obraz** baseOfImages, size_t imageCount)
- {
- if(imageCount >= 5)
- imageCount = 4;
- for (int i = 0; i < imageCount; i++)
- printf("[%d] %s", i + 1, baseOfImages[i]->name);
- if (imageCount == 0)
- printf("Base is empty\n");
- }
- // 4 select of activeImage
- void select_image(struct obraz** baseOfImages, struct obraz* activeImage, size_t imageCount)
- {
- int selectedImage; // selecting active image
- scanf("%d", &selectedImage);
- selectedImage--; // differenc betwen display and indexing
- for (int i = strlen(baseOfImages[selectedImage]->name); i > 0; i--)
- *(activeImage->name + i) = *(baseOfImages[selectedImage]->name + i); // name
- //for(int i = strlen(baseOfImages[selectedImage]->standard); i > 0; i-- )
- //*activeImage->standard++ = *baseOfImages[selectedImage]->standard++; //standard
- sscanf(activeImage->standard, "%s", baseOfImages[selectedImage]->standard);
- activeImage->hight = baseOfImages[selectedImage]->hight; // hight
- activeImage->width = baseOfImages[selectedImage]->width; // width
- activeImage->range = baseOfImages[selectedImage]->range; // range
- for (int i = 0; i < baseOfImages[selectedImage]->hight; i++)
- for (int j = 0; baseOfImages[selectedImage]->width; j++)
- activeImage->valueOfPicture[i][j] = baseOfImages[selectedImage]->valueOfPicture[i][j]; // value
- }
- void base_control(struct obraz** baseOfImages, struct obraz* activeImage, size_t* imageCount)
- {
- char baseControl = 'b';
- while (baseControl != '6') // pętla leci w niejskończonoć bez pobierania znaków ...
- {
- printf("Base control panel:\n\n");
- printf("[1] add new image\n"); // dodaj zapytanie o nazwe
- printf("[2] deleat image from base\n");
- printf("[3] list images in base\n");
- printf("[4] select image for later operations\n");
- printf("[5] panel of operations on active Image\n");
- printf("[6] leave program\n");
- baseControl = getchar();
- switch (baseControl)
- {
- case '1': // zczytaj z pliku do bazy
- base_addImage(baseOfImages, imageCount);
- //printf("%s added to Base\n", baseOfImages[imageCount].name);
- break;
- case '2':
- base_display(baseOfImages, *imageCount); // display and delete
- base_delete(baseOfImages, imageCount);
- break;
- case '3':
- base_display(baseOfImages, *imageCount);
- break;
- case '4':
- base_display(baseOfImages, *imageCount); // display and select
- select_image(baseOfImages, activeImage, *imageCount);
- break;
- case '5': // operacje na obrazie
- activeImage_operations(activeImage);
- break;
- case '6':
- break;
- default:
- printf("option beyond menu\n");
- break;
- }
- // check
- printf("ilosc obrazów w bazie %zu\n\n", *imageCount);
- if(*imageCount > 0 && *imageCount < 5){
- printf("name: %s\n", baseOfImages[*imageCount-1]->name);
- printf("standard: %s\n", baseOfImages[*imageCount-1]->standard);
- printf("width: %d\n", baseOfImages[*imageCount-1]->width);
- printf("high: %d\n", baseOfImages[*imageCount-1]->hight);
- printf("greyRange: %d", baseOfImages[*imageCount-1]->range);
- //printf("comments %s\n", baseOfImages[*imageCount-1]->comments);
- printf("\n%lu\n", baseOfImages[*imageCount - 1]->valueOfPicture[0][0]);
- }
- }
- //if(baseControl != 3){
- //printf("Wpisz nazwe obrazu do Operacji\n");
- //scanf("%s", activeImage.name);
- //}
- }
- void activeImage_operations(struct obraz* activeImage)
- {
- char operationsControl;
- system("clear");
- while (getchar() != '\n'); // czyszczenie buffora
- printf("Operations menu\n\n");
- printf("[1] progowanie\n");
- printf("[2] histogram\n");
- printf("[3] negatyw\n");
- printf("[4] zaszumienie\n");
- printf("[5] filtr\n");
- printf("[6] rotate\n");
- operationsControl = getchar();
- switch (operationsControl)
- {
- case '1':
- progowanie(activeImage);
- break;
- case '2':
- histogram(activeImage);
- break;
- case '3':
- negatyw(activeImage);
- break;
- case '4':
- Zaszumienie(activeImage);
- break;
- case '5':
- filtr_medianowy(activeImage);
- break;
- case '6':
- rotate(activeImage);
- break;
- default:
- printf("wybrano blendna operacje");
- break;
- }
- }
- //progowanie
- void progowanie(struct obraz* obrazX)
- {
- int prog = (int)(3.0 / 8 * obrazX->range); // prog 3/8 zakresu
- for (int i = 0; i < obrazX->hight; i++)
- for (int j = 0; j < obrazX->width; j++)
- {
- if (obrazX->valueOfPicture[i][j] >= prog)
- obrazX->valueOfPicture[i][j] = 0; // min - black
- else
- obrazX->valueOfPicture[i][j] = obrazX->range; // max - white
- }
- }
- //histogram
- void histogram(struct obraz* obrazX)
- {
- // jeli `range` nie jest na sztywno wpisane w kodzie, to musisz pobawić się pamięciš i dynamicznie zarezerwować potrzebne miejsce
- int* histogram = calloc(obrazX->range, sizeof(int));
- if (histogram == NULL)
- exit(666);// no nie mamy pamięci... wymyl co, co chcesz tutaj robić ;)
- for (int i = 0; i < obrazX->hight; i++)
- for (int j = 0; j < obrazX->width; j++)
- histogram[obrazX->valueOfPicture[i][j]]++;
- // zapis histogramu do CSV
- FILE* fp;
- fp = fopen("histogram.CSV", "w"); // można sprawdzać czy plik sie otworzyl
- for (int i = 0; i < obrazX->range; i++)
- fprintf(fp, "%d;%d\n", i, *(histogram + i));
- printf("utworzono histogram dla obrazu %s", obrazX->name);
- if (fp != NULL && fp != 0)
- fclose(fp); // Panie, a kto tu posprzšta?
- }
- void negatyw(struct obraz* obrazX)
- {
- for (int i = 0; i < obrazX->hight; i++)
- for (int j = 0; j < obrazX->width; j++)
- obrazX->valueOfPicture[i][j] = obrazX->range - obrazX->valueOfPicture[i][j];
- }
- // szum sol pieprz
- void Zaszumienie(struct obraz* obrazX)
- {
- int jump;
- bool saltPeper;
- srand(time(NULL));
- for (int i = 0; i < obrazX->hight; i++)
- {
- jump = rand() % 5;
- saltPeper = rand() % 2;
- for (int j = 0; j < obrazX->width; j++)
- {
- if (saltPeper == 0)
- obrazX->valueOfPicture[i][j + jump] = 0;
- else
- obrazX->valueOfPicture[i][j + jump] = obrazX->range;
- }
- }
- }
- // do zrobienia
- // filtr medianowy
- // nieskonczony
- void filtr_medianowy(struct obraz* obrazX)
- {
- int filterRange = 5; // no jak nic tutaj nie będzie, to dupa... i wyciek pamięci numer n-ty
- int* median;
- int OdlegloscProgu = filterRange / 2;
- printf("wybierz zakres filra\n");
- scanf("%d", &filterRange);
- median = malloc(filterRange * filterRange * sizeof(median));
- // for first 2
- // for last 2
- // no changes
- for (int i = OdlegloscProgu; i < (obrazX->hight - OdlegloscProgu); i++) // dla 'każdego' elementu tablicy
- for (int j = OdlegloscProgu; j < (obrazX->width - OdlegloscProgu); j++)
- for (int k = 0; k < (filterRange * filterRange); k++)
- printf(" "); // no nothing
- //pomysl median[k] =
- // wybierz mini macierz zrób z niej tablice i posortuj
- // wyciagnij srodkowy element
- //free(median);
- }
- // sort for filter
- // słyszałe o `qsort` z stdlib.h ?
- void quick_sort(int arr[], int low, int high)
- {
- if (low < high)
- {
- int pi = partition(arr, low, high);
- quick_sort(arr, low, pi - 1);
- quick_sort(arr, pi + 1, high);
- }
- }
- int partition(int arr[], int low, int high)
- {
- int pivot = arr[high]; // pivot
- int i = (low - 1);
- for (int j = low; j <= high - 1; j++)
- {
- if (arr[j] <= pivot)
- {
- i++;
- swap(&arr[i], &arr[j]);
- }
- }
- swap(&arr[i + 1], &arr[high]);
- return (i + 1);
- }
- // obrot o 90 stopni
- void rotate(struct obraz* obrazX)
- {
- struct obraz obrazXcopy;
- memcpy(&obrazX, &obrazXcopy, sizeof(obrazX)); // skopiowanie do obrazka roboczego
- swap(&obrazX->width, &obrazX->width); // zamiana wyskokosci z szerokowscia
- obrazX->valueOfPicture = realloc(obrazX->valueOfPicture, obrazX->hight * sizeof(obrazX->valueOfPicture)); // realloc na wysokosc
- for (int i = 0; i < obrazX->hight; i++) // realloc na szerokosc czyli trzeba liczba kolumn x ilosc wierszy
- obrazX->valueOfPicture[i] = realloc(obrazX->valueOfPicture[i], obrazX->width * sizeof(obrazX->valueOfPicture[i]));
- // wpisywanie do obroconego obrazka
- for (int i = 0; i < obrazX->hight; i++)
- for (int j = 0; j < obrazX->width; j++)
- obrazX->valueOfPicture[i][j] = obrazXcopy.valueOfPicture[j][i];
- //free(&obrazXcopy);
- }
- void swap(int* A, int* B) {
- int temp = *A;
- *A = *B;
- *B = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement