Advertisement
Guest User

Untitled

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