Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "bmp_header.h"
  4. #include<string.h>
  5.  
  6. typedef struct {
  7. unsigned char r, g, b;
  8. }pixel;
  9.  
  10.  
  11.  
  12. typedef struct {
  13. int x,y;
  14. struct nod *next;
  15. }nod;
  16.  
  17.  
  18. //Functia pentru task 2
  19. pixel filtru ( pixel **img, pixel **F, int height, int width, int mat[3][3])
  20. {
  21. int red=0;
  22. int x, y;
  23.  
  24.  
  25. //Am parcurs matricele si am calculat pentru fiecare caz particular valoarea pixelului
  26. for ( x = 0; x < height; x++ )
  27. {
  28. for ( y = 0; y < width; y++ )
  29. {
  30.  
  31.  
  32. if ( 0 < x && x < height-1 && 0 < y && y < width-1 )
  33. {
  34. red = img[x-1][y-1].r*mat[0][0] + img[x-1][y].r*mat[0][1] + img[x-1][y+1].r*mat[0][2]+
  35. img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2]+
  36. img[x+1][y-1].r*mat[2][0] +img[x+1][y].r*mat[2][1] + img[x+1][y+1].r*mat[2][2];
  37. }
  38.  
  39.  
  40.  
  41. if ( x == 0 )
  42. {
  43. if ( y == 0 )
  44. {
  45. red = img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2]+
  46. img[x+1][y].r*mat[2][1] + img[x+1][y+1].r*mat[2][2];
  47. }
  48.  
  49. if ( y == (width-1) )
  50. {
  51. red = img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1]+
  52. img[x+1][y-1].r*mat[2][0] +img[x+1][y].r*mat[2][1];
  53. }
  54.  
  55. if ( 0 < y && y < (width-1) )
  56. {
  57. red = img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2]+
  58. img[x+1][y-1].r*mat[2][0] +img[x+1][y].r*mat[2][1] + img[x+1][y+1].r*mat[2][2];
  59. }
  60. }
  61.  
  62.  
  63.  
  64. if ( x == height -1)
  65. {
  66. if ( y == 0 )
  67. {
  68. red = img[x-1][y].r*mat[0][1] + img[x-1][y+1].r*mat[0][2]+
  69. img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2];
  70. }
  71.  
  72. if ( y == (width-1) )
  73. {
  74. red = img[x-1][y-1].r*mat[0][0] + img[x-1][y].r*mat[0][1]+
  75. img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1];
  76. }
  77.  
  78. if ( 0 < y && y < (width-1) )
  79. {
  80. red = img[x-1][y-1].r*mat[0][0] + img[x-1][y].r*mat[0][1] + img[x-1][y+1].r*mat[0][2]+
  81. img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2];
  82. }
  83. }
  84.  
  85.  
  86.  
  87. if ( y == 0 && 0 < x && x < (height-1) )
  88. {
  89. red = img[x-1][y].r*mat[0][1] + img[x-1][y+1].r*mat[0][2]+
  90. img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2]+
  91. img[x+1][y].r*mat[2][1] + img[x+1][y+1].r*mat[2][2];
  92. }
  93.  
  94.  
  95.  
  96. if ( y == width-1 && 0 < x && x < (height-1) )
  97. {
  98. red = img[x-1][y-1].r*mat[0][0] + img[x-1][y].r*mat[0][1]+
  99. img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1]+
  100. img[x+1][y-1].r*mat[2][0] +img[x+1][y].r*mat[2][1];
  101. }
  102.  
  103.  
  104. if (-1 < red && red < 256)
  105. {
  106. F[x][y].r = red;
  107. F[x][y].g = red;
  108. F[x][y].b = red;
  109. }
  110. else
  111. {
  112. if ( red > 255)
  113. {
  114. F[x][y].r = 255;
  115. F[x][y].g = 255;
  116. F[x][y].b = 255;
  117. }
  118. else
  119. {
  120. F[x][y].r = 0;
  121. F[x][y].g = 0;
  122. F[x][y].b = 0;
  123. }
  124. }
  125.  
  126.  
  127. }
  128. }
  129.  
  130. }
  131.  
  132. void fill ( pixel **img, int height, int width, int x, int y, int **viz, int id, int tresh, pixel val)
  133. {
  134. int dx[]={-1, 0, 1, 0}, dy[]={0, 1, 0, -1};
  135. int i;
  136. int modul;
  137. nod *aux = calloc(1, sizeof(nod));
  138.  
  139. aux->x = x;
  140. aux->y = y;
  141. aux->next = NULL;
  142. nod *cap = calloc(1, sizeof(nod));
  143. nod *coada = calloc(1, sizeof(nod));
  144. cap = aux;
  145. coada = aux;
  146.  
  147. do
  148. {
  149. aux = cap;
  150. img[aux->x][aux->y] = val;
  151. viz[aux->x][aux->y] = id;
  152. for ( i=0; i<4; i++)
  153. {
  154. if ( 0 <= aux->x+dx[i] && aux->x+dx[i]<height && 0 <= aux->y+dy[i] && aux->y+dy[i]<width && viz[aux->x+dx[i]][aux->y+dy[i]] == 0)
  155. {
  156. modul = abs(img[aux->x][aux->y].r - img[aux->x+dx[i]][aux->y+dy[i]].r) +
  157. abs(img[aux->x][aux->y].g - img[aux->x+dx[i]][aux->y+dy[i]].g) +
  158. abs(img[aux->x][aux->y].b - img[aux->x+dx[i]][aux->y+dy[i]].b);
  159.  
  160. if ( modul <= tresh )
  161. {
  162. nod *vecin = calloc(1, sizeof(nod));
  163. vecin->x = aux->x+dx[i];
  164. vecin->y = aux->y+dy[i];
  165. vecin->next = NULL;
  166. coada->next = vecin;
  167. coada = vecin;
  168. }
  169. }
  170.  
  171. }
  172. cap = aux->next;
  173. } while(cap->next != NULL);
  174.  
  175.  
  176. }
  177.  
  178. int main()
  179. {
  180. pixel **img, **aux;
  181. bmp_fileheader file;
  182. bmp_infoheader info;
  183. FILE *bin_file, *input_text, *arhiva_file;
  184. char image_name[100], arhiva[100], image_bw[100], image_dec[100];
  185. char image_f1[100], image_f2[100], image_f3[100];
  186. int prag, k;
  187. short i, j;
  188.  
  189. //Citire din input.txt
  190. input_text = fopen ("input.txt", "rt");
  191.  
  192. if (input_text == NULL)
  193. {
  194. fprintf(stderr, "ERROR: Can't open file");
  195. return -1;
  196. }
  197.  
  198. fscanf(input_text, "%s", image_name);
  199. fscanf(input_text, "%d", &prag);
  200. fscanf(input_text, "%s", arhiva);
  201.  
  202. fclose(input_text);
  203.  
  204.  
  205. //Citire fisier binar al imaginii
  206. bin_file = fopen (image_name, "rb");
  207. if (bin_file == NULL)
  208. {
  209. printf("ERROR: Can't open file");
  210. return -1;
  211. }
  212.  
  213. //Fileheader
  214. fread(&file.fileMarker1, sizeof(char), 1, bin_file);
  215. fread(&file.fileMarker2, sizeof(char), 1, bin_file);
  216. fread(&file.bfSize, sizeof(int), 1, bin_file);
  217. fread(&file.unused1, sizeof(short), 1, bin_file);
  218. fread(&file.unused2, sizeof(short), 1, bin_file);
  219. fread(&file.imageDataOffset, sizeof(int), 1, bin_file);
  220.  
  221.  
  222. //Infoheader
  223. fread(&info.biSize, sizeof(int), 1, bin_file);
  224. fread(&info.width, sizeof(int), 1, bin_file);
  225. fread(&info.height, sizeof(int), 1, bin_file);
  226. fread(&info.planes, sizeof(short), 1, bin_file);
  227. fread(&info.bitPix, sizeof(short), 1, bin_file);
  228. fread(&info.biCompression, sizeof(int), 1, bin_file);
  229. fread(&info.biSizeImage, sizeof(int), 1, bin_file);
  230. fread(&info.biXPelsPerMeter, sizeof(int), 1, bin_file);
  231. fread(&info.biYPelsPerMeter, sizeof(int), 1, bin_file);
  232. fread(&info.biClrUsed, sizeof(int), 1, bin_file);
  233. fread(&info.biClrImportant, sizeof(int), 1, bin_file);
  234.  
  235.  
  236. //Citirea matricei de pixeli
  237.  
  238. img = calloc(info.height, sizeof(pixel *));
  239. for ( i=0; i<info.height; i++)
  240. {
  241. img[i] = calloc(info.width, sizeof(pixel));
  242. }
  243. fseek(bin_file, file.imageDataOffset, SEEK_SET);
  244.  
  245. for ( i=0; i<info.height; i++ )
  246. {
  247. for ( j=0; j<info.width; j++)
  248. {
  249. fread(&img[i][j].b, sizeof(char), 1, bin_file);
  250. fread(&img[i][j].g, sizeof(char), 1, bin_file);
  251. fread(&img[i][j].r, sizeof(char), 1, bin_file);
  252. }
  253. }
  254.  
  255. aux = calloc(info.height, sizeof(pixel *));
  256. for ( i=0; i<info.height; i++)
  257. {
  258. aux[i] = calloc(info.width, sizeof(pixel));
  259. }
  260.  
  261. for ( i=0; i<info.height; i++ )
  262. {
  263. for ( j=0; j<info.width; j++)
  264. {
  265. aux[i][j].b = img[i][j].b;
  266. aux[i][j].g = img[i][j].g;
  267. aux[i][j].r = img[i][j].r;
  268. }
  269. }
  270.  
  271. fclose(bin_file);
  272.  
  273.  
  274. //Task 1 - Transformarea imaginii in alb-negru
  275. int an;
  276. for ( i=info.height-1; i>=0; i-- )
  277. {
  278. for ( j=0; j<info.width; j++)
  279. {
  280. an = (img[i][j].b + img[i][j].g + img[i][j].r)/3;
  281. img[i][j].b = an;
  282. img[i][j].g = an;
  283. img[i][j].r = an;
  284. }
  285. }
  286.  
  287. //Crearea numelui imaginii in var image_bw
  288. strcpy(image_bw, image_name);
  289. strcpy(image_bw+strlen(image_bw)-4,"_black_white.bmp");
  290.  
  291.  
  292. //Crearea fisierului cu imagine alb-negru
  293. int pad_num = info.width % 4, pad0 = 0, var;
  294.  
  295. FILE *out1 = fopen(image_bw,"wb");
  296.  
  297. fwrite( &file, sizeof(file), 1, out1);
  298. fwrite( &info, sizeof(info), 1, out1);
  299. for(i=0;i<84;i++)
  300. {
  301. fputc( 0, out1);
  302. }
  303. for ( i=0; i<info.height; i++ )
  304. {
  305. for ( j=0; j<info.width; j++)
  306. {
  307. fwrite( &img[i][j], sizeof(pixel), 1, out1);
  308. }
  309. if (pad_num != 0)
  310. {
  311. var = pad_num;
  312. while (var != 0)
  313. {
  314. fwrite(&pad0, sizeof(int), 1, out1);
  315. var--;
  316. }
  317. }
  318. }
  319. fclose(out1);
  320.  
  321. //Task 2
  322.  
  323. //Cele 3 matrice-filtru
  324. int a[3][3] = {-1,-1,-1,-1,8,-1,-1,-1,-1};
  325. int b[3][3] = {0,1,0,1,-4,1,0,1,0};
  326. int c[3][3] = {-1,0,1,0,0,0,1,0,-1}; // Este explicat in readme motivul pentru care am schimbat filtrul 3
  327.  
  328.  
  329. //Crearea unei noi matrice
  330. pixel **F1;
  331.  
  332. F1 = calloc(info.height, sizeof(pixel *));
  333. for ( i=0; i<info.height; i++)
  334. {
  335. F1[i] = calloc(info.width, sizeof(pixel));
  336. }
  337.  
  338.  
  339. filtru(img, F1, info.height, info.width, a);
  340.  
  341. //Crearea numelui imaginii
  342. strcpy(image_f1, image_name);
  343. strcpy(image_f1+strlen(image_f1)-4,"_f1.bmp");
  344.  
  345. //Crearea fisierului "imagine_f1.bmp"
  346. pad_num = info.width % 4;
  347. pad0 = 0;
  348.  
  349. FILE *out2 = fopen(image_f1,"wb");
  350.  
  351. fwrite( &file, sizeof(file), 1, out2);
  352. fwrite( &info, sizeof(info), 1, out2);
  353. for(i=0;i<84;i++)
  354. {
  355. fputc( 0, out2);
  356. }
  357. for ( i=0; i<info.height; i++ )
  358. {
  359. for ( j=0; j<info.width; j++)
  360. {
  361. fwrite( &F1[i][j], sizeof(pixel), 1, out2);
  362. }
  363. if (pad_num != 0)
  364. {
  365. var = pad_num;
  366. while (var != 0)
  367. {
  368. fwrite(&pad0, sizeof(int), 1, out2);
  369. var--;
  370. }
  371. }
  372. }
  373. fclose(out2);
  374.  
  375.  
  376. //Crearea imaginii image_f2.bmp
  377. filtru(img, F1, info.height, info.width, b);
  378.  
  379. //Crearea numelui imaginii
  380. strcpy(image_f1, image_name);
  381. strcpy(image_f1+strlen(image_f1)-4,"_f2.bmp");
  382.  
  383.  
  384. //Crearea fisierului
  385. pad_num = info.width % 4;
  386. pad0 = 0;
  387.  
  388. out2 = fopen(image_f1,"wb");
  389.  
  390. fwrite( &file, sizeof(file), 1, out2);
  391. fwrite( &info, sizeof(info), 1, out2);
  392. for(i=0;i<84;i++)
  393. {
  394. fputc( 0, out2);
  395. }
  396. for ( i=0; i<info.height; i++ )
  397. {
  398. for ( j=0; j<info.width; j++)
  399. {
  400. fwrite( &F1[i][j], sizeof(pixel), 1, out2);
  401. }
  402. if (pad_num != 0)
  403. {
  404. var = pad_num;
  405. while (var != 0)
  406. {
  407. fwrite(&pad0, sizeof(int), 1, out2);
  408. var--;
  409. }
  410. }
  411. }
  412. fclose(out2);
  413.  
  414. //Crearea imaginii image_f3.bmp
  415. filtru(img, F1, info.height, info.width, c);
  416.  
  417.  
  418. //Crearea numelui imaginii
  419. strcpy(image_f1, image_name);
  420. strcpy(image_f1+strlen(image_f1)-4,"_f3.bmp");
  421.  
  422.  
  423. //Crearea fisierului
  424. pad_num = info.width % 4;
  425. pad0 = 0;
  426.  
  427. out2 = fopen(image_f1,"wb");
  428.  
  429. fwrite( &file, sizeof(file), 1, out2);
  430. fwrite( &info, sizeof(info), 1, out2);
  431. for(i=0;i<84;i++)
  432. {
  433. fputc( 0, out2);
  434. }
  435. for ( i=0; i<info.height; i++ )
  436. {
  437. for ( j=0; j<info.width; j++)
  438. {
  439. fwrite( &F1[i][j], sizeof(pixel), 1, out2);
  440. }
  441. if (pad_num != 0)
  442. {
  443. var = pad_num;
  444. while (var != 0)
  445. {
  446. fwrite(&pad0, sizeof(int), 1, out2);
  447. var--;
  448. }
  449. }
  450. }
  451. fclose(out2);
  452.  
  453. for ( i=0; i<info.height; i++ )
  454. {
  455. for ( j=0; j<info.width; j++)
  456. {
  457. img[i][j].b = aux[i][j].b;
  458. img[i][j].g = aux[i][j].g;
  459. img[i][j].r = aux[i][j].r;
  460. }
  461. }
  462.  
  463. for ( i=0; i<info.height; i++ )
  464. {
  465. free(aux[i]);
  466. free(F1[i]);
  467. }
  468. free(aux);
  469. free(F1);
  470.  
  471. //Task 3
  472. int id = 0;
  473. int **viz;
  474. int dx[]={-1, 0, 1, 0}, dy[]={0, 1, 0, -1};
  475.  
  476. viz = calloc(info.height, sizeof(int *));
  477. for ( i=0; i<info.height; i++)
  478. {
  479. viz[i] = calloc(info.width, sizeof(int));
  480. }
  481.  
  482.  
  483. for ( i = info.height-1 ; i >= 0; i--)
  484. {
  485. for ( j=0; j<info.width; j++)
  486. {
  487. if ( viz[i][j] == 0 )
  488. {
  489. id++;
  490. fill(img, info.height, info.width, i, j, viz, id, prag, img[i][j]);
  491. }
  492. }
  493. }
  494.  
  495. //Crearea numelui imaginii
  496.  
  497. char comp_name[100];
  498.  
  499. strcpy(comp_name, image_name);
  500. strcpy(comp_name+strlen(comp_name)-4,"_compressed.bin");
  501.  
  502. FILE *out3 = fopen("airplane_compressed.bin","wb");
  503.  
  504. fwrite( &file, sizeof(file), 1, out3);
  505. fwrite( &info, sizeof(info), 1, out3);
  506. for(i=0;i<84;i++)
  507. {
  508. fputc( 0, out3);
  509. }
  510.  
  511. for ( i=0; i < info.height; i++)
  512. {
  513. for ( j=0; j<info.width; j++)
  514. {
  515. int cnt = 0;
  516. for(k = 1; k < 4; ++k)
  517. if(0 <= i+dx[k] && i+dx[k] < info.height && 0 <= j+dy[k] && j+dy[k] < info.width && viz[i][j] == viz[ i+dx[k] ][j +dy[k] ])
  518. cnt++;
  519. if(cnt < 4 )
  520. {
  521. short ii = i+1, jj = j+1;
  522. fwrite ( &ii, sizeof(short), 1, out3 );
  523. fwrite ( &jj, sizeof(short), 1, out3 );
  524. fwrite ( &img[i][j].r, sizeof(char), 1, out3);
  525. fwrite ( &img[i][j].g, sizeof(char), 1, out3);
  526. fwrite ( &img[i][j].b, sizeof(char), 1, out3);
  527. }
  528.  
  529. }
  530. }
  531.  
  532. fclose(out3);
  533.  
  534.  
  535. for ( i=0; i<info.height; i++ )
  536. {
  537. free(viz[i]);
  538. free(img[i]);
  539. }
  540. free(viz);
  541. free(img);
  542.  
  543.  
  544. //Task 4
  545.  
  546. short col, line;
  547. short col_prec=0;
  548. int nr=1;
  549.  
  550.  
  551. //Deschiderea fisierului si alocarea memoriei pt matrice
  552. arhiva_file = fopen (arhiva, "rb");
  553. if (arhiva_file == NULL)
  554. {
  555. printf("ERROR: Can't open file");
  556. return -1;
  557. }
  558.  
  559. img = calloc(info.height, sizeof(pixel *));
  560. for ( i=0; i<info.height; i++)
  561. {
  562. img[i] = calloc(info.width, sizeof(pixel));
  563. }
  564.  
  565.  
  566. fseek(arhiva_file, file.imageDataOffset, SEEK_SET);
  567.  
  568.  
  569.  
  570.  
  571. //Citirea elementelor si completarea matricei
  572.  
  573. while ( nr != 0)
  574. {
  575. fread( &line, sizeof(short), 1, arhiva_file );
  576. fread( &col, sizeof(short), 1, arhiva_file );
  577. fread( &img[line-1][col-1].b, sizeof(char), 1, arhiva_file );
  578. fread( &img[line-1][col-1].g, sizeof(char), 1, arhiva_file );
  579. fread( &img[line-1][col-1].r, sizeof(char), 1, arhiva_file );
  580.  
  581. //Completarea matricei
  582.  
  583. if ( (col-1-col_prec) > 1)
  584. {
  585. for (k=col_prec+1; k<col; k++)
  586. {
  587. img[line-1][k].b = img[line-1][col_prec].b;
  588. img[line-1][k].g = img[line-1][col_prec].g;
  589. img[line-1][k].r = img[line-1][col_prec].r;
  590. }
  591. }
  592.  
  593. if ( (line-1 == (info.height-1)) && (col-1 == (info.width-1)) )
  594. {
  595. nr = 0;
  596. }
  597.  
  598. col_prec = col-1;
  599. }
  600.  
  601. fclose(arhiva_file);
  602.  
  603.  
  604.  
  605. //Crearea numelui imaginii in var image_bw
  606. strcpy(image_dec, "decompressed.bmp");
  607.  
  608.  
  609. //Crearea fisierului
  610.  
  611. FILE *out4 = fopen(image_dec,"wb");
  612.  
  613. fwrite(&file,sizeof(file),1,out4);
  614. fwrite(&info,sizeof(info),1,out4);
  615. for(i=0;i<84;i++)
  616. {
  617. fputc(0,out4);
  618. }
  619. for ( i=info.height-1; i>=0; i--)
  620. {
  621. for ( j=0; j<info.width; j++)
  622. {
  623. fwrite(&img[i][j],sizeof(pixel),1,out4);
  624. }
  625. if (pad_num != 0)
  626. {
  627. var = pad_num;
  628. while (var != 0)
  629. {
  630. fwrite(&pad0, sizeof(int), 1, out4);
  631. var--;
  632. }
  633. }
  634. }
  635.  
  636. fclose(out4);
  637.  
  638.  
  639. //Eliberare memorie
  640.  
  641. for (i=0; i<info.height; i++)
  642. {
  643. free(img[i]);
  644. }
  645. free(img);
  646.  
  647. return 0;
  648. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement