Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <time.h>
  6.  
  7. #define GREYMAX 65535
  8.  
  9. struct obraz
  10. {
  11. char* name;
  12. char* standard;
  13. int width;
  14. int range; // zakres szarosci
  15. int hight;
  16. int** valueOfPicture;
  17. };
  18.  
  19. /* deklaracje */
  20. bool odczyt_PGM(char* nazwaPliku, struct obraz ObrazX);
  21. bool zapis_PGM(char* nazwaPliku, struct obraz obrazX);
  22.  
  23. void progowanie(struct obraz obrazX);
  24. void histogram(struct obraz obrazX);
  25. void Zaszumienie(struct obraz obrazX);
  26. void filtr_medianowy(struct obraz obrazX);
  27. void rotate(struct obraz obrazX);
  28. void swap(int* A, int* B);
  29.  
  30.  
  31.  
  32. int main()
  33. {
  34. /* [5] base of images */
  35. struct obraz* baseOfImages = NULL; // base of images
  36.  
  37. char menuControl;
  38.  
  39.  
  40.  
  41. printf("wybor obrazu pliku z obrazem:\n");
  42. printf("[1] cells\n[2] flir\n[3] gradient\n[4] merc\n");
  43. menuControl = getchar();
  44. switch (menuControl)
  45. {
  46. case '1':
  47. //odczyt_PGM(nameOfImage);
  48. break;
  49. }
  50.  
  51.  
  52.  
  53.  
  54. // freeing the base dyanmic variables
  55. return 0;
  56. }
  57.  
  58. bool odczyt_PGM(char* name, struct obraz obrazX) // true - sucessfull read | false -error
  59. {
  60. FILE* fp = fopen(obrazX.name, "rb"); // gdzies cos z alokacja
  61. if(fp != 0)
  62. {
  63. fscanf(fp, "%s", obrazX.standard);
  64. fscanf(fp, "%d", &(obrazX.width));
  65. fscanf(fp, "%d", &(obrazX.hight));
  66. fscanf(fp, "%d", &(obrazX.range)); // zakres szarosci
  67. // allokacja pamieci na piksele
  68. obrazX.valueOfPicture = malloc(obrazX.hight * sizeof(obrazX.valueOfPicture) );
  69. for(int i = 0; i < obrazX.hight; i++)
  70. obrazX.valueOfPicture[i] = malloc(obrazX.width * sizeof(obrazX.valueOfPicture[i]) );
  71.  
  72. for(int i = 0; i < obrazX.hight; i++)
  73. for(int j = 0; j < obrazX.width; j++)
  74. fscanf(fp, "%d", &(obrazX.valueOfPicture[j][i]) );
  75. fclose(fp);
  76. for(int i = 0; i < obrazX.hight; i++) // przemysl jeszcze
  77. free(obrazX.valueOfPicture[i]); // każdy wiersz
  78. free(obrazX.valueOfPicture); // tablica wskaźnikow
  79. return true;
  80. }
  81. else{
  82. fclose(fp);
  83. return false;
  84. }
  85. }
  86.  
  87. bool zapis_PGM(char* nazwaPliku, struct obraz obrazX)
  88. {
  89. //char *buffer = file_buffor(obrazX);
  90. FILE* fp;
  91. fp = fopen(nazwaPliku, "w");
  92. if(fp != NULL)
  93. {
  94. fprintf(fp, "%s\n", obrazX.standard);
  95. fprintf(fp, "%d ", obrazX.width);
  96. fprintf(fp, "%d\n", obrazX.hight);
  97. fprintf(fp, "%d", obrazX.range);
  98. for(int i = 0; i < obrazX.hight; i++)
  99. {
  100. fprintf(fp, "\n");
  101. for(int j; j < obrazX.width; j++)
  102. fprintf(fp, "%d ", obrazX.valueOfPicture[i][j]);
  103. }
  104. fclose(fp);
  105. return true;
  106. }
  107. else{
  108. fclose(fp);
  109. return false;
  110. }
  111. /*
  112. size_t bufferLength = fwrite(buffer, sizeof( buffer ), strlen( buffer ), fp); // check with ferror - error handler
  113. if(strlen(buffer) == bufferLength)
  114. return true;
  115. else
  116. return false;
  117. }
  118. else{
  119. fclose(fp);
  120. return false;
  121. }
  122. */
  123. }
  124. /*
  125. messages() // stores comments
  126. {
  127.  
  128. }
  129. */
  130. void image_base(struct obraz* baseOfImages)
  131. {
  132. int imageCount = 0; // count of images in base
  133. int selectedImage; // selecting active image
  134. struct obraz activeImage; // image for operations
  135. // add image
  136. imageCount++; // count
  137. baseOfImages[imageCount].valueOfPicture = malloc(baseOfImages[imageCount].hight * sizeof(baseOfImages[imageCount].valueOfPicture)); // for base of images dynamicly allocated 2D matrix of pixels
  138. for(int i = 0; i < baseOfImages[imageCount].width; i++)
  139. baseOfImages[imageCount].valueOfPicture[i] = malloc(baseOfImages[imageCount].width * sizeof(baseOfImages[imageCount].valueOfPicture[i]));
  140.  
  141. // deleat image
  142. int toDeleat;
  143. scanf("%d", &toDeleat);
  144. imageCount--;
  145. // zamień element to dealeat z ostatnim i reallocuj pamięć
  146. baseOfImages = realloc(baseOfImages, imageCount * sizeof(baseOfImages));
  147. // display images list
  148. for(int i = 0; i < imageCount; i++)
  149. printf("[%d] %s", i+1, baseOfImages[i].name);
  150. // select of activeImage
  151. for(int i = 0; i < imageCount; i++)
  152. printf("[%d] %s", i+1, baseOfImages[i].name); // display images list
  153. for(int i = strlen(baseOfImages[selectedImage].name); i > 0; i--)
  154. activeImage.name = baseOfImages[selectedImage].name; // name
  155. for(int i = strlen(baseOfImages[selectedImage].standard); i > 0; i-- )
  156. *(activeImage.standard)++ = *(baseOfImages[selectedImage].standard)++; //standard
  157. activeImage.hight = baseOfImages[selectedImage].hight; // hight
  158. activeImage.width = baseOfImages[selectedImage].width; // width
  159. activeImage.range = baseOfImages[selectedImage].range; // range
  160. for(int i =0; i < baseOfImages[selectedImage].hight; i++)
  161. for(int j = 0; baseOfImages[selectedImage].width; j++)
  162. activeImage.valueOfPicture[i][j] = baseOfImages[selectedImage].valueOfPicture[i][j]; // value
  163.  
  164.  
  165.  
  166.  
  167. char baseControl;
  168.  
  169. printf("Base control panel\n\n");
  170. printf("[1] add new image\n"); // dodaj zapytanie o nazwe
  171. printf("[2] deleat image from base\n");
  172. printf("[3] list images in base\n");
  173. printf("[4] select image for later operations\n");
  174. baseControl = getchar();
  175. if(baseControl != 3){
  176. printf("Wpisz nazwe obrazu do Operacji\n");
  177. scanf("%s", activeImage.name);
  178. }
  179. switch(baseControl)
  180. {
  181. case '1':
  182. break;
  183. case '2':
  184. break;
  185. case '3':
  186. break;
  187. case '4':
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193.  
  194. //progowanie
  195. void progowanie(struct obraz obrazX)
  196. {
  197. int prog = (int) (3.0 / 8 * obrazX.range); // prog 3/8 zakresu
  198. for(int i = 0; i < obrazX.hight; i++)
  199. for(int j = 0; j < obrazX.width; j++)
  200. {
  201. if(obrazX.valueOfPicture[i][j] >= prog)
  202. obrazX.valueOfPicture[i][j] = 0; // min - black
  203. else
  204. obrazX.valueOfPicture[i][j] = obrazX.range; // max - white
  205. }
  206. }
  207. //histogram
  208. void histogram(struct obraz obrazX)
  209. {
  210. int histogram[obrazX.range];
  211. for(int i = 0; i < obrazX.hight; i++)
  212. for(int j = 0; j < obrazX.width; j++)
  213. histogram[ obrazX.valueOfPicture[i][j] ]++;
  214. // zapis histogramu do CSV
  215. FILE* fp;
  216. fp = fopen("histogram.CSV", "w"); // można sprawdzać czy plik sie otworzyl
  217. for(int i = 0; i < obrazX.range; i++)
  218. fprintf(fp, "%d;%d\n", i, *(histogram + i) );
  219. printf("utworzono histogram dla obrazu %s", obrazX.name);
  220. }
  221.  
  222. void negatyw(struct obraz obrazX)
  223. {
  224. for(int i = 0; i < obrazX.hight; i++)
  225. for(int j = 0; j < obrazX.width; j++)
  226. obrazX.valueOfPicture[i][j] = obrazX.range - obrazX.valueOfPicture[i][j];
  227. }
  228.  
  229. // szum sol pieprz
  230. void Zaszumienie(struct obraz obrazX)
  231. {
  232. int jump;
  233. bool saltPeper;
  234. srand(time(NULL));
  235. for(int i = 0; i < obrazX.hight; i++)
  236. {
  237. jump = rand()%5;
  238. saltPeper = rand()%2;
  239. for(int j = 0; j < obrazX.width; j++)
  240. {
  241. if(saltPeper == 0)
  242. obrazX.valueOfPicture[i][j+jump] = 0;
  243. else
  244. obrazX.valueOfPicture[i][j+jump] = obrazX.range;
  245. }
  246. }
  247. }
  248.  
  249. // do zrobienia
  250. // filtr medianowy
  251. void filtr_medianowy(struct obraz obrazX)
  252. {
  253. int filterRange;
  254. int OdlegloscProgu = filterRange/2 ;
  255. // for first 2
  256. // for last 2
  257. // no changes
  258. for(int i = OdlegloscProgu; i < (obrazX.hight -OdlegloscProgu); i++) // dla 'każdego' elementu tablicy
  259. for(int j = OdlegloscProgu; j < (obrazX.width - OdlegloscProgu); j++)
  260.  
  261. // wybierz mini macierz zrób z niej tablice i posortuj
  262. // wyciagnij srodkowy element
  263.  
  264. }
  265.  
  266. // obrot o 90 stopni
  267. void rotate(struct obraz obrazX)
  268. {
  269. struct obraz obrazXcopy;
  270. memcpy(&obrazX, &obrazXcopy, sizeof(obrazX)); // skopiowanie do obrazka roboczego
  271. swap(&obrazX.width, &obrazX.width); // zamiana wyskokosci z szerokowscia
  272. realloc(obrazX.valueOfPicture, obrazX.hight * sizeof(obrazX.valueOfPicture)); // realloc na wysokosc
  273. for(int i = 0; i < obrazX.hight; i++) // realloc na szerokosc czyli trzeba liczba kolumn x ilosc wierszy
  274. realloc(obrazX.valueOfPicture[i], obrazX.width * sizeof(obrazX.valueOfPicture[i]));
  275. // wpisywanie do obroconego obrazka
  276. for(int i = 0; i < obrazX.hight; i++)
  277. for(int j = 0; j < obrazX.width; j++)
  278. obrazX.valueOfPicture[i][j] = obrazXcopy.valueOfPicture[j][i];
  279. free(&obrazXcopy);
  280. }
  281.  
  282. void swap(int* A, int* B)
  283. {
  284. int temp = *A;
  285. *A = *B;
  286. *B = temp;
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement