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;
- };
- /* deklaracje */
- bool odczyt_PGM(char* nazwaPliku, struct obraz ObrazX);
- bool zapis_PGM(char* nazwaPliku, struct obraz obrazX);
- void progowanie(struct obraz obrazX);
- void histogram(struct obraz obrazX);
- void Zaszumienie(struct obraz obrazX);
- void filtr_medianowy(struct obraz obrazX);
- void rotate(struct obraz obrazX);
- void swap(int* A, int* B);
- int main()
- {
- /* [5] base of images */
- struct obraz* baseOfImages = NULL; // base of images
- char menuControl;
- printf("wybor obrazu pliku z obrazem:\n");
- printf("[1] cells\n[2] flir\n[3] gradient\n[4] merc\n");
- menuControl = getchar();
- switch (menuControl)
- {
- case '1':
- //odczyt_PGM(nameOfImage);
- break;
- }
- // freeing the base dyanmic variables
- return 0;
- }
- bool odczyt_PGM(char* name, struct obraz obrazX) // true - sucessfull read | false -error
- {
- FILE* fp = fopen(obrazX.name, "rb"); // gdzies cos z alokacja
- if(fp != 0)
- {
- fscanf(fp, "%s", obrazX.standard);
- fscanf(fp, "%d", &(obrazX.width));
- fscanf(fp, "%d", &(obrazX.hight));
- fscanf(fp, "%d", &(obrazX.range)); // zakres szarosci
- // 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]) );
- for(int i = 0; i < obrazX.hight; i++)
- for(int j = 0; j < obrazX.width; j++)
- fscanf(fp, "%d", &(obrazX.valueOfPicture[j][i]) );
- fclose(fp);
- for(int i = 0; i < obrazX.hight; i++) // przemysl jeszcze
- free(obrazX.valueOfPicture[i]); // każdy wiersz
- free(obrazX.valueOfPicture); // tablica wskaźnikow
- return true;
- }
- else{
- fclose(fp);
- return false;
- }
- }
- bool zapis_PGM(char* nazwaPliku, struct obraz obrazX)
- {
- //char *buffer = file_buffor(obrazX);
- FILE* fp;
- fp = fopen(nazwaPliku, "w");
- if(fp != NULL)
- {
- fprintf(fp, "%s\n", obrazX.standard);
- fprintf(fp, "%d ", obrazX.width);
- fprintf(fp, "%d\n", obrazX.hight);
- fprintf(fp, "%d", obrazX.range);
- for(int i = 0; i < obrazX.hight; i++)
- {
- fprintf(fp, "\n");
- for(int j; j < obrazX.width; j++)
- fprintf(fp, "%d ", obrazX.valueOfPicture[i][j]);
- }
- fclose(fp);
- return true;
- }
- else{
- fclose(fp);
- return false;
- }
- /*
- size_t bufferLength = fwrite(buffer, sizeof( buffer ), strlen( buffer ), fp); // check with ferror - error handler
- if(strlen(buffer) == bufferLength)
- return true;
- else
- return false;
- }
- else{
- fclose(fp);
- return false;
- }
- */
- }
- /*
- messages() // stores comments
- {
- }
- */
- void image_base(struct obraz* baseOfImages)
- {
- int imageCount = 0; // count of images in base
- int selectedImage; // selecting active image
- struct obraz activeImage; // image for operations
- // add image
- imageCount++; // count
- baseOfImages[imageCount].valueOfPicture = malloc(baseOfImages[imageCount].hight * sizeof(baseOfImages[imageCount].valueOfPicture)); // for base of images dynamicly allocated 2D matrix of pixels
- for(int i = 0; i < baseOfImages[imageCount].width; i++)
- baseOfImages[imageCount].valueOfPicture[i] = malloc(baseOfImages[imageCount].width * sizeof(baseOfImages[imageCount].valueOfPicture[i]));
- // deleat image
- int toDeleat;
- scanf("%d", &toDeleat);
- imageCount--;
- // zamień element to dealeat z ostatnim i reallocuj pamięć
- baseOfImages = realloc(baseOfImages, imageCount * sizeof(baseOfImages));
- // display images list
- for(int i = 0; i < imageCount; i++)
- printf("[%d] %s", i+1, baseOfImages[i].name);
- // select of activeImage
- for(int i = 0; i < imageCount; i++)
- printf("[%d] %s", i+1, baseOfImages[i].name); // display images list
- for(int i = strlen(baseOfImages[selectedImage].name); i > 0; i--)
- activeImage.name = baseOfImages[selectedImage].name; // 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
- char baseControl;
- 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");
- baseControl = getchar();
- if(baseControl != 3){
- printf("Wpisz nazwe obrazu do Operacji\n");
- scanf("%s", activeImage.name);
- }
- switch(baseControl)
- {
- case '1':
- break;
- case '2':
- break;
- case '3':
- break;
- case '4':
- break;
- default:
- 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
- void filtr_medianowy(struct obraz obrazX)
- {
- int filterRange;
- int OdlegloscProgu = filterRange/2 ;
- // 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++)
- // wybierz mini macierz zrób z niej tablice i posortuj
- // wyciagnij srodkowy element
- }
- // 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
- 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
- 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