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>
- #define GREYMAX 65535
- struct obraz
- {
- char* name;
- char* standard;
- int width;
- int range; // zakres szarosci
- int hight;
- int** valueOfPicture;
- char* comments;
- };
- // read and write to file
- void odczyt_PGM(char** fileName, struct obraz* ObrazX);
- bool isComment(FILE** fp);
- bool zapis_PGM(struct obraz obrazX);
- // the Base
- void base_control(struct obraz** Base, struct obraz* activeImage, int* imageCount);
- void activeImage_operations(struct obraz* activeImage); // submenu
- // baseOfImages functions
- void base_addImage(struct obraz** baseOfImages, int* imageCount); //
- void getString(char** buffer); // use in base add
- void delete(struct obraz** baseOfImages, int* imageCount); //
- void display_base(struct obraz* baseOfImages, int imageCount); //
- void select_image(struct obraz** baseOfImages, struct obraz* activeImage, int 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 */
- struct obraz* baseOfImages = NULL; // base of images
- struct obraz activeImage; // image for operations
- char menuControl;
- int imageCount = 0; // count of images in base
- base_control(&baseOfImages, &activeImage, &imageCount);
- for(int i = 0; i < imageCount; i++){ // czyszczenie pikseli
- for(int j = 0; j < baseOfImages[i].hight; j++)
- free(baseOfImages[i].valueOfPicture[j]);
- free(baseOfImages[i].valueOfPicture);
- free(baseOfImages[i].name);
- free(baseOfImages[i].standard);
- free(baseOfImages[i].comments);
- }
- free(baseOfImages);
- // freeing the base dynmic arrays and variables
- return 0;
- }
- void odczyt_PGM(char** name, struct obraz* obrazX) // true - sucessfull read | false -error
- {
- obrazX->name = malloc(strlen(*name) * sizeof(obrazX->name));
- while(**name != '\0')
- *obrazX->name++ = **name++;
- free(*name);
- FILE* fp = fopen(obrazX->name, "rb"); // gdzies cos z alokacja // blendy mozna przerobic na ferror
- if(ferror(fp) == 0) // flaga blendu nie ustawiona
- {
- if(isComment(&fp)){
- fscanf(fp, "%s ", obrazX->comments++); // wrzuć do komentarza
- }
- else{
- obrazX->standard = malloc(3 * sizeof(obrazX->standard));
- fscanf(fp, "%s ", obrazX->standard); // standard
- }
- if(isComment(&fp)){
- fscanf(fp, "%s ", obrazX->comments++);
- }
- else
- fscanf(fp, "%d", &(obrazX->width)); // width
- if(isComment(&fp)){
- fscanf(fp, "%s", obrazX->comments++);
- }
- else
- fscanf(fp, "%d", &(obrazX->hight)); // hight
- if(isComment(&fp)){
- fscanf(fp, "%s", obrazX->comments++);
- }
- else
- fscanf(fp, "%d", &(obrazX->range)); // grey range
- // allokacja pamieci na piksele
- 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++);
- }
- else
- fscanf(fp, "%d", &(obrazX->valueOfPicture[j][i]) );
- }
- 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 wskaźnikow
- //free(obrazX.name);
- }
- else{
- fclose(fp);
- fputs("error with opening file\n", stderr);
- }
- }
- bool isComment(FILE** fp)
- {
- int c;
- if(c = getchar() == '#')
- {
- fseek(*fp, -1, SEEK_CUR);
- return true;
- }
- else
- return false;
- }
- // 1 add image // tu pomyśl z alokacja tu i w odczycie
- void base_addImage(struct obraz** baseOfImages, int* imageCount)
- {
- // arg do add image i delete
- int i;
- int bufferSize = 64;
- char* fileName = malloc( bufferSize * sizeof(*fileName));
- *imageCount++; // count
- //baseOfImages[imageCount].valueOfPicture = malloc(baseOfImages[imageCount].hight * sizeof(baseOfImages[imageCount].valueOfPicture)); // for base of images dynamicly allocated 2D matrix of pixels
- //for(i = 0; i < baseOfImages[imageCount].width; i++)
- //baseOfImages[imageCount].valueOfPicture[i] = malloc(baseOfImages[imageCount].width * sizeof(baseOfImages[imageCount].valueOfPicture[i]));
- printf("write in name of file which you want to add to base\n");
- getString(&fileName);
- odczyt_PGM(&fileName, baseOfImages[*imageCount]); // !!!!!! przekazywanie bazy[i] może być słabe !!!
- //free(fileName); // tu czyszcze buffora z getString
- }
- //**
- void getString(char** buffer) // przekazywanie podwojnych wskaznikow **
- {
- int bufferSize = 64;
- printf("a");
- int strLen;
- char c;
- for(strLen = 0; (c = getchar()) != '\n' && c != ' ' && c != '\t'; strLen++ )
- {
- printf("a");
- strLen++;
- if(strLen >= bufferSize){
- bufferSize *= 2;
- *buffer = realloc(*buffer, bufferSize * sizeof(**buffer));
- }
- **buffer++ = c;
- }
- printf("%d", strLen);
- *buffer = realloc(*buffer , strLen * sizeof(**buffer));
- }
- // 2 delete image
- void base_delete(struct obraz** baseOfImages, int* imageCount)
- {
- int toDeleat;
- scanf("%d", &toDeleat);
- *imageCount--;
- // zamień element to dealeat z ostatnim i reallocuj pamięć
- *baseOfImages = realloc(*baseOfImages, *imageCount * sizeof(struct obraz));
- }
- // 3 display images list
- void base_display(struct obraz** baseOfImages, int imageCount)
- {
- 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, int 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
- 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, int* imageCount)
- {
- char c;
- char baseControl;
- char baseMenuOnOff = true;
- fflush(stdout);
- 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();
- while(baseMenuOnOff)
- {
- 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':
- baseMenuOnOff = false;
- break;
- default:
- printf("option beyond menu\n");
- break;
- }
- // errors display
- while(c != EOF){
- c = fgetc(stderr);
- printf("%c", c);
- }
- }
- //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)
- {
- int histogram[obrazX->range];
- 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);
- }
- 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;
- 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++)
- //pomysl median[k] =
- // wybierz mini macierz zrób z niej tablice i posortuj
- // wyciagnij srodkowy element
- free(median);
- }
- // sort for filter
- 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