Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.76 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. // czy na pewno powinienneœ wartoœć z nagłówka pliku PGM wklepywać na sztywno?
  8. #define GREYMAX 65535
  9. /*
  10. #define _CRT_SECURE_NO_DEPRECATE
  11. #ifndef _MSC_VER
  12. #endif
  13. */
  14. struct obraz
  15. {
  16. char* name;
  17. char standard[10];
  18. int width;
  19. int hight;
  20. int range; // zakres szarosci
  21. int** valueOfPicture;
  22. char comments[200];
  23. };
  24.  
  25. // read and write to file
  26. void odczyt_PGM(struct obraz* ObrazX);
  27. bool isComment(FILE** fp);
  28. bool zapis_PGM(struct obraz obrazX);
  29. // the Base
  30. void base_control(struct obraz** Base, struct obraz* activeImage, size_t* imageCount);
  31. void activeImage_operations(struct obraz* activeImage); // submenu
  32. // baseOfImages functions
  33. void base_addImage(struct obraz** baseOfImages, size_t* imageCount); //
  34. char* getString(); // use in base add
  35. void delete(struct obraz** baseOfImages, size_t* imageCount); //
  36. void display_base(struct obraz* baseOfImages, size_t imageCount); //
  37. void select_image(struct obraz** baseOfImages, struct obraz* activeImage, size_t imageCount); //
  38. // operations on activeImage
  39. void progowanie(struct obraz* obrazX); //
  40. void histogram(struct obraz* obrazX); //
  41. void Zaszumienie(struct obraz* obrazX); ///
  42. void negatyw(struct obraz* obrazX); //
  43. void rotate(struct obraz* obrazX); //
  44.  
  45. void filtr_medianowy(struct obraz* obrazX); //
  46. void quick_sort(int arr[], int low, int high);
  47. int partition(int arr[], int low, int high);
  48. void swap(int* A, int* B);
  49.  
  50.  
  51.  
  52. int main()
  53. {
  54. /* [5] base of images */
  55. // limit 5 obrazow
  56. struct obraz** baseOfImages = (struct obraz**) malloc(1 * sizeof(struct obraz*)); // base of images
  57. struct obraz activeImage; // image for operations
  58. char menuControl;
  59. size_t imageCount = 0; // count of images in base
  60.  
  61. //odczyt_PGM()
  62. base_control(baseOfImages, &activeImage, &imageCount);
  63.  
  64. free(baseOfImages);
  65. return 0;
  66. }
  67.  
  68. void odczyt_PGM(struct obraz* obrazX) // true - sucessfull read | false -error
  69. {
  70. FILE* fp;
  71. fp = fopen(obrazX->name, "r");
  72. printf("obrazX->name %s\n", obrazX->name);
  73. if (ferror(fp) == 0) // flaga blendu nie ustawiona
  74. {
  75. fscanf(fp, "%s ", obrazX->standard);
  76. isComment(&fp);
  77.  
  78. fscanf(fp, "%d", &obrazX->width);
  79. isComment(&fp);
  80.  
  81. fscanf(fp, "%d", &obrazX->hight);
  82. isComment(&fp);
  83.  
  84. fscanf(fp, "%d", &(obrazX->range));
  85. isComment(&fp);
  86.  
  87. // allokacja pamieci na piksele
  88. // tu się musi psuć aaaaaaaa
  89. obrazX->valueOfPicture = malloc(obrazX->hight * sizeof(obrazX->valueOfPicture));
  90. for (int i = 0; i < obrazX->hight; i++)
  91. obrazX->valueOfPicture[i] = malloc(obrazX->width * sizeof(obrazX->valueOfPicture[i])); // obsługę errorów
  92. // wpisywanie do pikseli
  93. for (int i = 0; i < obrazX->hight; i++)
  94. for (int j = 0; j < obrazX->width; j++)
  95. {
  96. if (isComment(&fp)) {
  97. fscanf(fp, "#%s ", obrazX->comments);
  98. j--;
  99. }
  100. else
  101. fscanf(fp, "%d", &(obrazX->valueOfPicture[i][j]));
  102. }
  103.  
  104. if(fp != NULL && fp != 0)
  105. fclose(fp);
  106. //for(int i = 0; i < obrazX.hight; i++) // przemysl jeszcze tu chyba nie potrzebne zwalnianie
  107. //free(obrazX.valueOfPicture[i]); // każdy wiersz
  108. //free(obrazX.valueOfPicture); // tablica wskaŸnikow
  109. //free(obrazX.name);
  110. }
  111. else {
  112. if (fp != NULL && fp != 0)
  113. fclose(fp);
  114. fputs("error with opening file\n", stderr);
  115. }
  116. }
  117.  
  118. bool isComment(FILE** fp)
  119. {
  120. char buf[5];
  121. if (fgetc(*fp) == '#')
  122. {
  123. fgets(buf, 5, *fp);
  124. return true;
  125. }
  126. else{
  127. fseek(*fp, -1, SEEK_CUR);
  128. return false;
  129. }
  130. }
  131.  
  132. // 1 add image // tu pomyœl z alokacja tu i w odczycie
  133. void base_addImage(struct obraz** baseOfImages, size_t* imageCount)
  134. {
  135. struct obraz* tmp;
  136. printf("write in name of file which you want to add to base\n");
  137. char* fileName = getString();
  138. printf("nazwa wpisana do fileName %s\n", fileName);
  139.  
  140. *imageCount = *imageCount + 1;
  141.  
  142. tmp = realloc(*baseOfImages, *imageCount * sizeof(struct obraz*));
  143.  
  144. if(tmp != NULL){
  145. *baseOfImages = tmp;
  146.  
  147. baseOfImages[*imageCount - 1] = malloc(sizeof(struct obraz));
  148. baseOfImages[*imageCount - 1]->name = fileName;
  149.  
  150. odczyt_PGM(baseOfImages[*imageCount - 1]);
  151. } else{
  152. *imageCount = *imageCount - 1;
  153. }
  154. }
  155.  
  156. char* getString()
  157. {
  158. char* str = calloc(256, sizeof(char));// nie, nie przekroczysz tego rozmiaru, więc daj sobie spokój z reallockiem tutaj
  159. scanf("%256s", str);
  160. return str;
  161. }
  162.  
  163. // 2 delete image
  164. void base_delete(struct obraz** baseOfImages, size_t* imageCount)
  165. {
  166. int toDeleat;
  167. scanf("%d", &toDeleat);
  168. *imageCount--;
  169. // zamień element to dealet z ostatnim i reallocuj pamięć
  170. *baseOfImages = realloc(*baseOfImages, *imageCount * sizeof(struct obraz));
  171. }
  172.  
  173. // 3 display images list
  174. void base_display(struct obraz** baseOfImages, size_t imageCount)
  175. {
  176. if(imageCount >= 5)
  177. imageCount = 4;
  178.  
  179. for (int i = 0; i < imageCount; i++)
  180. printf("[%d] %s", i + 1, baseOfImages[i]->name);
  181. if (imageCount == 0)
  182. printf("Base is empty\n");
  183. }
  184.  
  185. // 4 select of activeImage
  186. void select_image(struct obraz** baseOfImages, struct obraz* activeImage, size_t imageCount)
  187. {
  188. int selectedImage; // selecting active image
  189. scanf("%d", &selectedImage);
  190. selectedImage--; // differenc betwen display and indexing
  191.  
  192. for (int i = strlen(baseOfImages[selectedImage]->name); i > 0; i--)
  193. *(activeImage->name + i) = *(baseOfImages[selectedImage]->name + i); // name
  194. //for(int i = strlen(baseOfImages[selectedImage]->standard); i > 0; i-- )
  195. //*activeImage->standard++ = *baseOfImages[selectedImage]->standard++; //standard
  196. sscanf(activeImage->standard, "%s", baseOfImages[selectedImage]->standard);
  197. activeImage->hight = baseOfImages[selectedImage]->hight; // hight
  198. activeImage->width = baseOfImages[selectedImage]->width; // width
  199. activeImage->range = baseOfImages[selectedImage]->range; // range
  200. for (int i = 0; i < baseOfImages[selectedImage]->hight; i++)
  201. for (int j = 0; baseOfImages[selectedImage]->width; j++)
  202. activeImage->valueOfPicture[i][j] = baseOfImages[selectedImage]->valueOfPicture[i][j]; // value
  203. }
  204.  
  205. void base_control(struct obraz** baseOfImages, struct obraz* activeImage, size_t* imageCount)
  206. {
  207. char baseControl = 'b';
  208.  
  209. while (baseControl != '6') // pętla leci w niejskończonoœć bez pobierania znaków ...
  210. {
  211. printf("Base control panel:\n\n");
  212. printf("[1] add new image\n"); // dodaj zapytanie o nazwe
  213. printf("[2] deleat image from base\n");
  214. printf("[3] list images in base\n");
  215. printf("[4] select image for later operations\n");
  216. printf("[5] panel of operations on active Image\n");
  217. printf("[6] leave program\n");
  218.  
  219. baseControl = getchar();
  220.  
  221. switch (baseControl)
  222. {
  223. case '1': // zczytaj z pliku do bazy
  224. base_addImage(baseOfImages, imageCount);
  225. //printf("%s added to Base\n", baseOfImages[imageCount].name);
  226. break;
  227. case '2':
  228. base_display(baseOfImages, *imageCount); // display and delete
  229. base_delete(baseOfImages, imageCount);
  230. break;
  231. case '3':
  232. base_display(baseOfImages, *imageCount);
  233. break;
  234. case '4':
  235. base_display(baseOfImages, *imageCount); // display and select
  236. select_image(baseOfImages, activeImage, *imageCount);
  237. break;
  238. case '5': // operacje na obrazie
  239. activeImage_operations(activeImage);
  240. break;
  241. case '6':
  242. break;
  243. default:
  244. printf("option beyond menu\n");
  245. break;
  246. }
  247. // check
  248. printf("ilosc obrazów w bazie %zu\n\n", *imageCount);
  249.  
  250. if(*imageCount > 0 && *imageCount < 5){
  251. printf("name: %s\n", baseOfImages[*imageCount-1]->name);
  252. printf("standard: %s\n", baseOfImages[*imageCount-1]->standard);
  253. printf("width: %d\n", baseOfImages[*imageCount-1]->width);
  254. printf("high: %d\n", baseOfImages[*imageCount-1]->hight);
  255. printf("greyRange: %d", baseOfImages[*imageCount-1]->range);
  256. //printf("comments %s\n", baseOfImages[*imageCount-1]->comments);
  257.  
  258. printf("\n%lu\n", baseOfImages[*imageCount - 1]->valueOfPicture[0][0]);
  259. }
  260. }
  261. //if(baseControl != 3){
  262. //printf("Wpisz nazwe obrazu do Operacji\n");
  263. //scanf("%s", activeImage.name);
  264. //}
  265. }
  266.  
  267. void activeImage_operations(struct obraz* activeImage)
  268. {
  269. char operationsControl;
  270. system("clear");
  271. while (getchar() != '\n'); // czyszczenie buffora
  272. printf("Operations menu\n\n");
  273. printf("[1] progowanie\n");
  274. printf("[2] histogram\n");
  275. printf("[3] negatyw\n");
  276. printf("[4] zaszumienie\n");
  277. printf("[5] filtr\n");
  278. printf("[6] rotate\n");
  279. operationsControl = getchar();
  280. switch (operationsControl)
  281. {
  282. case '1':
  283. progowanie(activeImage);
  284. break;
  285. case '2':
  286. histogram(activeImage);
  287. break;
  288. case '3':
  289. negatyw(activeImage);
  290. break;
  291. case '4':
  292. Zaszumienie(activeImage);
  293. break;
  294. case '5':
  295. filtr_medianowy(activeImage);
  296. break;
  297. case '6':
  298. rotate(activeImage);
  299. break;
  300. default:
  301. printf("wybrano blendna operacje");
  302. break;
  303. }
  304. }
  305.  
  306.  
  307. //progowanie
  308. void progowanie(struct obraz* obrazX)
  309. {
  310. int prog = (int)(3.0 / 8 * obrazX->range); // prog 3/8 zakresu
  311. for (int i = 0; i < obrazX->hight; i++)
  312. for (int j = 0; j < obrazX->width; j++)
  313. {
  314. if (obrazX->valueOfPicture[i][j] >= prog)
  315. obrazX->valueOfPicture[i][j] = 0; // min - black
  316. else
  317. obrazX->valueOfPicture[i][j] = obrazX->range; // max - white
  318. }
  319. }
  320. //histogram
  321. void histogram(struct obraz* obrazX)
  322. {
  323. // jeœli `range` nie jest na sztywno wpisane w kodzie, to musisz pobawić się pamięciš i dynamicznie zarezerwować potrzebne miejsce
  324. int* histogram = calloc(obrazX->range, sizeof(int));
  325.  
  326. if (histogram == NULL)
  327. exit(666);// no nie mamy pamięci... wymyœl coœ, co chcesz tutaj robić ;)
  328.  
  329. for (int i = 0; i < obrazX->hight; i++)
  330. for (int j = 0; j < obrazX->width; j++)
  331. histogram[obrazX->valueOfPicture[i][j]]++;
  332. // zapis histogramu do CSV
  333. FILE* fp;
  334. fp = fopen("histogram.CSV", "w"); // można sprawdzać czy plik sie otworzyl
  335. for (int i = 0; i < obrazX->range; i++)
  336. fprintf(fp, "%d;%d\n", i, *(histogram + i));
  337. printf("utworzono histogram dla obrazu %s", obrazX->name);
  338.  
  339. if (fp != NULL && fp != 0)
  340. fclose(fp); // Panie, a kto tu posprzšta?
  341. }
  342.  
  343. void negatyw(struct obraz* obrazX)
  344. {
  345. for (int i = 0; i < obrazX->hight; i++)
  346. for (int j = 0; j < obrazX->width; j++)
  347. obrazX->valueOfPicture[i][j] = obrazX->range - obrazX->valueOfPicture[i][j];
  348. }
  349.  
  350. // szum sol pieprz
  351. void Zaszumienie(struct obraz* obrazX)
  352. {
  353. int jump;
  354. bool saltPeper;
  355. srand(time(NULL));
  356. for (int i = 0; i < obrazX->hight; i++)
  357. {
  358. jump = rand() % 5;
  359. saltPeper = rand() % 2;
  360. for (int j = 0; j < obrazX->width; j++)
  361. {
  362. if (saltPeper == 0)
  363. obrazX->valueOfPicture[i][j + jump] = 0;
  364. else
  365. obrazX->valueOfPicture[i][j + jump] = obrazX->range;
  366. }
  367. }
  368. }
  369.  
  370. // do zrobienia
  371. // filtr medianowy
  372. // nieskonczony
  373. void filtr_medianowy(struct obraz* obrazX)
  374. {
  375. int filterRange = 5; // no jak nic tutaj nie będzie, to dupa... i wyciek pamięci numer n-ty
  376. int* median;
  377. int OdlegloscProgu = filterRange / 2;
  378. printf("wybierz zakres filra\n");
  379. scanf("%d", &filterRange);
  380. median = malloc(filterRange * filterRange * sizeof(median));
  381. // for first 2
  382. // for last 2
  383. // no changes
  384. for (int i = OdlegloscProgu; i < (obrazX->hight - OdlegloscProgu); i++) // dla 'każdego' elementu tablicy
  385. for (int j = OdlegloscProgu; j < (obrazX->width - OdlegloscProgu); j++)
  386. for (int k = 0; k < (filterRange * filterRange); k++)
  387. printf(" "); // no nothing
  388. //pomysl median[k] =
  389. // wybierz mini macierz zrób z niej tablice i posortuj
  390. // wyciagnij srodkowy element
  391. //free(median);
  392. }
  393.  
  394. // sort for filter
  395. // słyszałeœ o `qsort` z stdlib.h ?
  396. void quick_sort(int arr[], int low, int high)
  397. {
  398. if (low < high)
  399. {
  400. int pi = partition(arr, low, high);
  401. quick_sort(arr, low, pi - 1);
  402. quick_sort(arr, pi + 1, high);
  403. }
  404. }
  405.  
  406. int partition(int arr[], int low, int high)
  407. {
  408. int pivot = arr[high]; // pivot
  409. int i = (low - 1);
  410. for (int j = low; j <= high - 1; j++)
  411. {
  412. if (arr[j] <= pivot)
  413. {
  414. i++;
  415. swap(&arr[i], &arr[j]);
  416. }
  417. }
  418. swap(&arr[i + 1], &arr[high]);
  419. return (i + 1);
  420. }
  421.  
  422. // obrot o 90 stopni
  423. void rotate(struct obraz* obrazX)
  424. {
  425. struct obraz obrazXcopy;
  426. memcpy(&obrazX, &obrazXcopy, sizeof(obrazX)); // skopiowanie do obrazka roboczego
  427. swap(&obrazX->width, &obrazX->width); // zamiana wyskokosci z szerokowscia
  428. obrazX->valueOfPicture = realloc(obrazX->valueOfPicture, obrazX->hight * sizeof(obrazX->valueOfPicture)); // realloc na wysokosc
  429. for (int i = 0; i < obrazX->hight; i++) // realloc na szerokosc czyli trzeba liczba kolumn x ilosc wierszy
  430. obrazX->valueOfPicture[i] = realloc(obrazX->valueOfPicture[i], obrazX->width * sizeof(obrazX->valueOfPicture[i]));
  431. // wpisywanie do obroconego obrazka
  432. for (int i = 0; i < obrazX->hight; i++)
  433. for (int j = 0; j < obrazX->width; j++)
  434. obrazX->valueOfPicture[i][j] = obrazXcopy.valueOfPicture[j][i];
  435. //free(&obrazXcopy);
  436. }
  437.  
  438. void swap(int* A, int* B) {
  439. int temp = *A;
  440. *A = *B;
  441. *B = temp;
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement