Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.29 KB | None | 0 0
  1. #ifdef __cplusplus
  2. #include <cstdlib>
  3. #else
  4. #include <stdlib.h>
  5.  
  6. #endif
  7. #include <SDL/SDL.h>
  8. #include <math.h>
  9. #include <time.h>
  10. #include <iostream>
  11. #include <iomanip>
  12. #define M_PI 3.14159265358979323846
  13. using namespace std;
  14.  
  15. SDL_Surface *screen;
  16. int width = 512;
  17. int height = 340;
  18. char const* tytul = "GKiM2019 - Lab 11 - Czechowicz Konrad";
  19.  
  20. struct YCbCr {
  21. float y;
  22. float cb;
  23. float cr;
  24. };
  25.  
  26. struct HSV {
  27. float h;
  28. float s;
  29. float v;
  30. };
  31.  
  32. const int ROZMIAR_BLOKU = 8;
  33.  
  34. struct macierz {
  35. float dct[ROZMIAR_BLOKU][ROZMIAR_BLOKU];
  36. Uint8 dane[ROZMIAR_BLOKU][ROZMIAR_BLOKU];
  37. };
  38.  
  39. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B);
  40. SDL_Color getPixel (int x, int y);
  41.  
  42. void czyscEkran(Uint8 R, Uint8 G, Uint8 B);
  43.  
  44. void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B);
  45. void Okrag(int x, int y, int r, Uint8 R, Uint8 G, Uint8 B);
  46. void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B);
  47. YCbCr RGBtoYCbCr(SDL_Color rgb);
  48. SDL_Color YCbCrtoRGB(YCbCr ycbcr);
  49. SDL_Color HSVtoRGB(HSV hsv);
  50. HSV RGBtoHSV(SDL_Color rgb);
  51.  
  52. macierz dct(Uint8 wartosci[ROZMIAR_BLOKU][ROZMIAR_BLOKU]);
  53. macierz idct(float DCT[ROZMIAR_BLOKU][ROZMIAR_BLOKU]);
  54.  
  55. void Funkcja1();
  56. void Funkcja2();
  57. void Funkcja3();
  58. void Funkcja4();
  59. void Funkcja5();
  60. void Funkcja6();
  61. void Funkcja7();
  62. void Funkcja8();
  63. void Funkcja9();
  64.  
  65.  
  66. void Funkcja1() {
  67.  
  68. float kolor = 0;
  69.  
  70. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  71. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  72. setPixel(x+128, y+64, kolor, kolor, kolor);
  73. kolor += 256.0/(ROZMIAR_BLOKU*ROZMIAR_BLOKU);
  74. }
  75. }
  76.  
  77. for(int y = 1; y < ROZMIAR_BLOKU; y+=2) {
  78. for(int x = 1; x < ROZMIAR_BLOKU; x+=2) {
  79. setPixel(x + 128, y + 64 + ROZMIAR_BLOKU, 255, 255, 255);
  80. setPixel(x + 128-1, y + 64 + ROZMIAR_BLOKU - 1, 128, 128, 128);
  81. setPixel(x + 128, y + 64 + ROZMIAR_BLOKU-1, 0, 0, 0);
  82. setPixel(x + 128-1, y + 64 + ROZMIAR_BLOKU, 0, 0, 0);
  83. }
  84. }
  85.  
  86. SDL_Flip(screen);
  87. }
  88.  
  89. void wyswietlDane(macierz blok) {
  90. cout << "Dane pikselowe w macierzy: " << endl;
  91. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  92. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  93. cout << (int)blok.dane[x][y] << " ";
  94. }
  95. cout << endl;
  96. }
  97. }
  98.  
  99. void wyswietlDaneDCT(macierz blok) {
  100. cout << "Wspolczynniki transformaty w macierzy: " << endl;
  101. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  102. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  103. cout << (float)blok.dct[x][y] << " ";
  104. }
  105. cout << endl;
  106. }
  107. }
  108.  
  109.  
  110. void Funkcja2() {
  111.  
  112. macierz blok;
  113. macierz blokDCT;
  114. macierz blokDane;
  115.  
  116. //pierwszy blok
  117. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  118. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  119. blok.dane[x][y] = getPixel(x+128, y+64).r;
  120. blok.dct[x][y] = 0;
  121. }
  122. }
  123. wyswietlDane(blok);
  124. blokDCT = dct(blok.dane);
  125. wyswietlDaneDCT(blokDCT);
  126.  
  127. blokDane = idct(blokDCT.dct);
  128. wyswietlDane(blokDane);
  129.  
  130. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  131. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  132. setPixel(x+128+(width/2), y+64, blokDane.dane[x][y], blokDane.dane[x][y], blokDane.dane[x][y]);
  133. }
  134. }
  135.  
  136.  
  137. //drugi blok
  138. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  139. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  140. blok.dane[x][y] = getPixel(x+128, y+64+ROZMIAR_BLOKU).r;
  141. blok.dct[x][y] = 0;
  142. }
  143. }
  144. wyswietlDane(blok);
  145. blokDCT = dct(blok.dane);
  146. wyswietlDaneDCT(blokDCT);
  147.  
  148. blokDane = idct(blokDCT.dct);
  149. wyswietlDane(blokDane);
  150.  
  151. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  152. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  153. setPixel(x+128+(width/2), y+64+ROZMIAR_BLOKU, blokDane.dane[x][y], blokDane.dane[x][y], blokDane.dane[x][y]);
  154. }
  155. }
  156.  
  157. SDL_Flip(screen);
  158. }
  159.  
  160. macierz zaokraglenieDCT(macierz blok) {
  161. macierz wynik = blok;
  162.  
  163. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  164. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  165. wynik.dct[x][y] = round(blok.dct[x][y]);
  166. }
  167. }
  168. return wynik;
  169. }
  170.  
  171. macierz utnijMacierz(macierz wejscie, int ile) {
  172. macierz wynik = wejscie;
  173.  
  174. if(ile>0)
  175. wynik.dct[ROZMIAR_BLOKU-1][ROZMIAR_BLOKU-1] = 0;
  176.  
  177. int a, b;
  178. if(ile>1) {
  179. for(int j = ile; j > 0; j--) {
  180. for(int i = j-1; i >= 0; i--) {
  181. a = i+1;
  182. b = j-i;
  183. wynik.dct[ROZMIAR_BLOKU-a][ROZMIAR_BLOKU-b] = 0;
  184. }
  185. }
  186. }
  187.  
  188. return wynik;
  189. }
  190.  
  191. void Funkcja3() {
  192.  
  193. macierz blok;
  194. macierz blokDCT;
  195. macierz blokDane;
  196.  
  197. //pierwszy blok
  198. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  199. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  200. blok.dane[x][y] = getPixel(x+128, y+64).r;
  201. blok.dct[x][y] = 0;
  202. }
  203. }
  204. wyswietlDane(blok);
  205.  
  206. blokDCT = dct(blok.dane);
  207. wyswietlDaneDCT(blokDCT);
  208.  
  209. blokDCT = zaokraglenieDCT(blokDCT);
  210. wyswietlDaneDCT(blokDCT);
  211.  
  212. blokDCT = utnijMacierz(blokDCT, 5);
  213. wyswietlDane(blokDCT);
  214.  
  215. blokDane = idct(blokDCT.dct);
  216. wyswietlDane(blokDane);
  217.  
  218. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  219. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  220. setPixel(x+128+(width/2), y+64, blokDane.dane[x][y], blokDane.dane[x][y], blokDane.dane[x][y]);
  221. }
  222. }
  223.  
  224.  
  225. //drugi blok
  226. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  227. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  228. blok.dane[x][y] = getPixel(x+128, y+64+ROZMIAR_BLOKU).r;
  229. blok.dct[x][y] = 0;
  230. }
  231. }
  232. wyswietlDane(blok);
  233.  
  234. blokDCT = dct(blok.dane);
  235. wyswietlDaneDCT(blokDCT);
  236.  
  237. blokDCT = zaokraglenieDCT(blokDCT);
  238. wyswietlDaneDCT(blokDCT);
  239.  
  240. blokDCT = utnijMacierz(blokDCT, 20);
  241. wyswietlDane(blokDCT);
  242.  
  243. blokDane = idct(blokDCT.dct);
  244. wyswietlDane(blokDane);
  245.  
  246. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  247. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  248. setPixel(x+128+(width/2), y+64+ROZMIAR_BLOKU, blokDane.dane[x][y], blokDane.dane[x][y], blokDane.dane[x][y]);
  249. }
  250. }
  251.  
  252.  
  253. SDL_Flip(screen);
  254. }
  255.  
  256. void Funkcja4() {
  257.  
  258. macierz blok;
  259. macierz blokDCT;
  260. macierz blokDane;
  261.  
  262.  
  263.  
  264. //drugi blok
  265. for(int y = 0; y < height/8; y++) {
  266. for(int x = 0; x < width/8; x++) {
  267. for(int y = 0; y < ROZMIAR_BLOKU; y++) {
  268. for(int x = 0; x < ROZMIAR_BLOKU; x++) {
  269. blok.dane[x][y] = getPixel(x, y).r;
  270. blok.dct[x][y] = 0;
  271. }
  272. }
  273. }
  274. }
  275. wyswietlDane(blok);
  276.  
  277. blokDCT = dct(blok.dane);
  278. wyswietlDaneDCT(blokDCT);
  279.  
  280. blokDCT = zaokraglenieDCT(blokDCT);
  281. wyswietlDaneDCT(blokDCT);
  282.  
  283. blokDCT = utnijMacierz(blokDCT, 1);
  284. wyswietlDane(blokDCT);
  285.  
  286. blokDane = idct(blokDCT.dct);
  287. wyswietlDane(blokDane);
  288.  
  289. for(int y = 0; y < height/2; y++) {
  290. for(int x = 0; x < width/2; x++) {
  291. setPixel(x+(width/2), y, blokDane.dane[x][y], blokDane.dane[x][y], blokDane.dane[x][y]);
  292. }
  293. }
  294.  
  295.  
  296. SDL_Flip(screen);
  297. }
  298.  
  299.  
  300. void Funkcja5() {
  301.  
  302. //...
  303.  
  304. SDL_Flip(screen);
  305. }
  306.  
  307.  
  308. void Funkcja6() {
  309.  
  310. //...
  311.  
  312. SDL_Flip(screen);
  313. }
  314.  
  315.  
  316. void Funkcja7() {
  317.  
  318. //...
  319.  
  320. SDL_Flip(screen);
  321. }
  322.  
  323.  
  324. void Funkcja8() {
  325.  
  326. //...
  327.  
  328. SDL_Flip(screen);
  329. }
  330.  
  331.  
  332. void Funkcja9() {
  333.  
  334. //...
  335.  
  336. SDL_Flip(screen);
  337. }
  338.  
  339.  
  340. macierz dct(Uint8 wartosci[ROZMIAR_BLOKU][ROZMIAR_BLOKU])
  341. {
  342. float wynik[ROZMIAR_BLOKU][ROZMIAR_BLOKU];
  343.  
  344. // obliczamy DCT
  345. for (unsigned v = 0; v < ROZMIAR_BLOKU; ++v) {
  346. for (unsigned u = 0; u < ROZMIAR_BLOKU; ++u) {
  347. const double cu = (u == 0) ? 1.0 / sqrt(2) : 1.0;
  348. const double cv = (v == 0) ? 1.0 / sqrt(2) : 1.0;
  349. double dctCoeff = 0;
  350.  
  351. for (unsigned y = 0; y < ROZMIAR_BLOKU; ++y) {
  352. for (unsigned x = 0; x < ROZMIAR_BLOKU; ++x) {
  353. double uCosFactor = cos((double)(2 * x + 1) * M_PI * (double)u / (2 * (double) ROZMIAR_BLOKU));
  354. double vCosFactor = cos((double)(2 * y + 1) * M_PI * (double)v / (2 * (double) ROZMIAR_BLOKU));
  355. double pixel = (double)wartosci[x][y];
  356. dctCoeff += pixel * uCosFactor * vCosFactor;
  357. }
  358. }
  359. dctCoeff *= (2.0 / (double) ROZMIAR_BLOKU) * cu * cv;
  360. wynik[u][v] = dctCoeff;
  361. }
  362. }
  363.  
  364. macierz rezultat;
  365. for (int j=0; j<ROZMIAR_BLOKU; j++) {
  366. for (int i=0; i<ROZMIAR_BLOKU; i++) {
  367. rezultat.dct[i][j] = wynik[i][j];
  368. rezultat.dane[i][j] = wartosci[i][j];
  369. }
  370. }
  371.  
  372. return rezultat;
  373. }
  374.  
  375. macierz idct(float DCT[ROZMIAR_BLOKU][ROZMIAR_BLOKU]) {
  376. int wynik[ROZMIAR_BLOKU][ROZMIAR_BLOKU];
  377. // obliczamy DCT
  378. for (unsigned x = 0; x < ROZMIAR_BLOKU; ++x) {
  379. for (unsigned y = 0; y < ROZMIAR_BLOKU; ++y) {
  380.  
  381. double pixel = 0;
  382.  
  383. for (unsigned u = 0; u < ROZMIAR_BLOKU; ++u) {
  384. for (unsigned v = 0; v < ROZMIAR_BLOKU; ++v) {
  385. const double cu = (u == 0) ? 1.0 / sqrt(2) : 1.0;
  386. const double cv = (v == 0) ? 1.0 / sqrt(2) : 1.0;
  387. double uCosFactor = cos((double)(2 * x + 1) * M_PI * (double)u / (2 * (double) ROZMIAR_BLOKU));
  388. double vCosFactor = cos((double)(2 * y + 1) * M_PI * (double)v / (2 * (double) ROZMIAR_BLOKU));
  389. double coeff = DCT[u][v];
  390. pixel += coeff * uCosFactor * vCosFactor * cu * cv;
  391. }
  392. }
  393. pixel *= (2.0 / (double) ROZMIAR_BLOKU) ;
  394. wynik[x][y] = pixel;
  395. }
  396. }
  397.  
  398. macierz rezultat;
  399. for (int j=0; j<ROZMIAR_BLOKU; j++) {
  400. for (int i=0; i<ROZMIAR_BLOKU; i++) {
  401. if (wynik[i][j]>255) wynik[i][j]=255;
  402. if (wynik[i][j]<0) wynik[i][j]=0;
  403. rezultat.dane[i][j] = wynik[i][j];
  404. rezultat.dct[i][j] = DCT[i][j];
  405. }
  406. }
  407. return rezultat;
  408. }
  409.  
  410. void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B) {
  411. setPixel(x1,y1,R,G,B);
  412. setPixel(x2,y2,R,G,B);
  413. // zmienne pomocnicze
  414. int d, dx, dy, ai, bi, xi, yi;
  415. int x = x1, y = y1;
  416. // ustalenie kierunku rysowania
  417. if (x1 < x2)
  418. {
  419. xi = 1;
  420. dx = x2 - x1;
  421. }
  422. else
  423. {
  424. xi = -1;
  425. dx = x1 - x2;
  426. }
  427. // ustalenie kierunku rysowania
  428. if (y1 < y2)
  429. {
  430. yi = 1;
  431. dy = y2 - y1;
  432. }
  433. else
  434. {
  435. yi = -1;
  436. dy = y1 - y2;
  437. }
  438. // pierwszy piksel
  439. setPixel(x, y, R, G, B);
  440. // oś wiodąca OX
  441. if (dx > dy)
  442. {
  443. ai = (dy - dx) * 2;
  444. bi = dy * 2;
  445. d = bi - dx;
  446. // pętla po kolejnych x
  447. while (x != x2)
  448. {
  449. // test współczynnika
  450. if (d >= 0)
  451. {
  452. x += xi;
  453. y += yi;
  454. d += ai;
  455. }
  456. else
  457. {
  458. d += bi;
  459. x += xi;
  460. }
  461. setPixel(x, y, R, G, B);
  462. }
  463. }
  464. // oś wiodąca OY
  465. else
  466. {
  467. ai = ( dx - dy ) * 2;
  468. bi = dx * 2;
  469. d = bi - dy;
  470. // pętla po kolejnych y
  471. while (y != y2)
  472. {
  473. // test współczynnika
  474. if (d >= 0)
  475. {
  476. x += xi;
  477. y += yi;
  478. d += ai;
  479. }
  480. else
  481. {
  482. d += bi;
  483. y += yi;
  484. }
  485. setPixel(x, y, R, G, B);
  486. }
  487. }
  488. SDL_Flip(screen);
  489. }
  490.  
  491. void RysujOkrag(int x0, int y0, int x, int y, Uint8 R, Uint8 G, Uint8 B){
  492. setPixel(x+x0, y+y0, R, G, B);
  493. setPixel(y+x0, x+y0, R, G, B);
  494. setPixel(y+x0, -x+y0, R, G, B);
  495. setPixel(x+x0, -y+y0, R, G, B);
  496. setPixel(-x+x0, -y+y0, R, G, B);
  497. setPixel(-y+x0, -x+y0, R, G, B);
  498. setPixel(-y+x0, x+y0, R, G, B);
  499. setPixel(-x+x0, y+y0, R, G, B);;
  500. }
  501.  
  502.  
  503. void Okrag(int x0, int y0, int r, Uint8 R, Uint8 G, Uint8 B) {
  504. int x =0;
  505. int y=r;
  506. int d = 3-2*r;
  507.  
  508. while (x <= y){
  509. if (d<0){
  510. d=d+4*x+6;
  511. RysujOkrag(x0, y0, x, y, R, G, B);
  512. } else{
  513. d=d+4*(x-y)+10;
  514. y--;
  515. RysujOkrag(x0, y0, x, y, R, G, B);
  516. }
  517. x++;
  518. }
  519. SDL_Flip(screen);
  520. }
  521.  
  522. void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B) {
  523. int xc = 0,
  524. yc = b;
  525. int aa = a * a,
  526. aa2 = aa + aa,
  527. bb = b * b,
  528. bb2 = bb + bb;
  529. int d = bb - aa * b + (aa / 4),
  530. dx = 0,
  531. dy = aa2 * b;
  532.  
  533. while (dx < dy) {
  534. setPixel (x - xc,y - yc, R, G, B);
  535. setPixel (x - xc,y + yc, R, G, B);
  536. setPixel (x + xc,y - yc, R, G, B);
  537. setPixel (x + xc,y + yc, R, G, B);
  538. if (d > 0){
  539. yc = yc-1;
  540. dy -= aa2;
  541. d -= dy;
  542. }
  543. xc =xc+1;
  544. dx += bb2;
  545. d += bb + dx;
  546. }
  547. d += (3 * ((aa - bb) / 2) - (dx + dy) / 2);
  548.  
  549. while (yc >= 0) {
  550. setPixel (x - xc,y - yc, R, G, B);
  551. setPixel (x - xc,y + yc, R, G, B);
  552. setPixel (x + xc,y - yc, R, G, B);
  553. setPixel (x + xc,y + yc, R, G, B);
  554. if (d < 0) {
  555. xc =xc+1;
  556. dx += bb2;
  557. d += (bb + dx);
  558. }
  559. yc = yc-1;
  560. dy -= aa2;
  561. d += aa - dy;
  562. }
  563. SDL_Flip(screen);
  564. }
  565.  
  566. YCbCr RGBtoYCbCr(SDL_Color rgb) {
  567. YCbCr ycbcr;
  568.  
  569. ycbcr.y = 0.299 * rgb.r + 0.587*rgb.g + 0.114*rgb.b;
  570. ycbcr.cb = 128 - 0.168736 * rgb.r - 0.331264*rgb.g + 0.5*rgb.b;
  571. ycbcr.cr = 128 + 0.5 * rgb.r - 0.418688*rgb.g - 0.081312*rgb.b;
  572.  
  573. return ycbcr;
  574. }
  575.  
  576. float normalizacja(float wartosc) {
  577. float wynik = wartosc;
  578. if (wartosc > 255) wartosc = 255;
  579. if (wartosc < 0) wartosc = 0;
  580. return wartosc;
  581. }
  582.  
  583. SDL_Color YCbCrtoRGB(YCbCr ycbcr) {
  584. SDL_Color rgb;
  585. int R, G, B;
  586.  
  587. R = ycbcr.y + 1.402*(ycbcr.cr-128);
  588. G = ycbcr.y - 0.344136*(ycbcr.cb-128) - 0.714136*(ycbcr.cr-128);
  589. B = ycbcr.y + 1.772*(ycbcr.cb-128);
  590.  
  591. R = normalizacja(R);
  592. G = normalizacja(G);
  593. B = normalizacja(B);
  594.  
  595. rgb = {.r=R, .g=G, .b=B};
  596. return rgb;
  597. }
  598.  
  599. HSV RGBtoHSV(SDL_Color rgb) {
  600. HSV hsv;
  601. unsigned char rgbMin, rgbMax;
  602.  
  603. rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
  604. rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);
  605.  
  606. hsv.v = rgbMax;
  607. if (hsv.v == 0) {
  608. hsv.h = 0;
  609. hsv.s = 0;
  610. return hsv;
  611. }
  612.  
  613. hsv.s = 255.0*(rgbMax-rgbMin)/hsv.v;
  614.  
  615. if (hsv.s == 0) {
  616. hsv.h = 0;
  617. return hsv;
  618. }
  619.  
  620. if (rgbMax == rgb.r) {
  621. hsv.h = 0 + 43.0*(rgb.g-rgb.b)/(rgbMax-rgbMin);
  622. }
  623. else if (rgbMax == rgb.g) {
  624. hsv.h = 85 + 43.0*(rgb.b-rgb.r)/(rgbMax-rgbMin);
  625. }
  626. else {
  627. hsv.h = 171 + 43.0*(rgb.r-rgb.g)/(rgbMax-rgbMin);
  628. }
  629.  
  630. if (hsv.h>255) hsv.h-=255;
  631. if (hsv.h<0) hsv.h+=255;
  632.  
  633. hsv.s = normalizacja(hsv.s);
  634. hsv.v = normalizacja(hsv.v);
  635.  
  636. return hsv;
  637. }
  638.  
  639.  
  640. SDL_Color HSVtoRGB(HSV hsv) {
  641. SDL_Color rgb;
  642. int R, G, B;
  643.  
  644. if (hsv.h < 0) hsv.h += 255;
  645. if (hsv.h > 255) hsv.h -= 255;
  646. hsv.s = normalizacja(hsv.s);
  647. hsv.v = normalizacja(hsv.v);
  648.  
  649. unsigned char region, p, q, t;
  650. double h;
  651. unsigned int s, v, reszta;
  652.  
  653. if (hsv.s == 0) {
  654. R = hsv.v;
  655. G = hsv.v;
  656. B = hsv.v;
  657. }
  658.  
  659. h = hsv.h;
  660. s = hsv.s;
  661. v = hsv.v;
  662.  
  663.  
  664. region = h / 43;
  665. reszta = (h - (region * 43) ) * 6;
  666.  
  667. p = (v * (255 - s)) >> 8;
  668. q = (v * (255 - ((s * reszta) >> 8 ))) >> 8;
  669. t = (v * (255 - ((s * (255 - reszta)) >> 8 ))) >> 8;
  670.  
  671. switch (region) {
  672. case 0:
  673. R = v; G = t; B = p;
  674. break;
  675. case 1:
  676. R = q; G = v; B = p;
  677. break;
  678. case 2:
  679. R = p; G = v; B = t;
  680. break;
  681. case 3:
  682. R = p; G = q; B = v;
  683. break;
  684. case 4:
  685. R = t; G = p; B = v;
  686. break;
  687. default:
  688. R = v; G = p; B = q;
  689. break;
  690. }
  691. rgb = {R, G, B};
  692. return rgb;
  693. }
  694.  
  695.  
  696. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
  697. {
  698. if ((x>=0) && (x<width) && (y>=0) && (y<height))
  699. {
  700. /* Zamieniamy poszczególne składowe koloru na format koloru piksela */
  701. Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
  702.  
  703. /* Pobieramy informację ile bajtów zajmuje jeden piksel */
  704. int bpp = screen->format->BytesPerPixel;
  705.  
  706. /* Obliczamy adres piksela */
  707. Uint8 *p1 = (Uint8 *)screen->pixels + (y*2) * screen->pitch + (x*2) * bpp;
  708. Uint8 *p2 = (Uint8 *)screen->pixels + (y*2+1) * screen->pitch + (x*2) * bpp;
  709. Uint8 *p3 = (Uint8 *)screen->pixels + (y*2) * screen->pitch + (x*2+1) * bpp;
  710. Uint8 *p4 = (Uint8 *)screen->pixels + (y*2+1) * screen->pitch + (x*2+1) * bpp;
  711.  
  712. /* Ustawiamy wartość piksela, w zależności od formatu powierzchni*/
  713. switch(bpp)
  714. {
  715. case 1: //8-bit
  716. *p1 = pixel;
  717. *p2 = pixel;
  718. *p3 = pixel;
  719. *p4 = pixel;
  720. break;
  721.  
  722. case 2: //16-bit
  723. *(Uint16 *)p1 = pixel;
  724. *(Uint16 *)p2 = pixel;
  725. *(Uint16 *)p3 = pixel;
  726. *(Uint16 *)p4 = pixel;
  727. break;
  728.  
  729. case 3: //24-bit
  730. if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
  731. p1[0] = (pixel >> 16) & 0xff;
  732. p1[1] = (pixel >> 8) & 0xff;
  733. p1[2] = pixel & 0xff;
  734. p2[0] = (pixel >> 16) & 0xff;
  735. p2[1] = (pixel >> 8) & 0xff;
  736. p2[2] = pixel & 0xff;
  737. p3[0] = (pixel >> 16) & 0xff;
  738. p3[1] = (pixel >> 8) & 0xff;
  739. p3[2] = pixel & 0xff;
  740. p4[0] = (pixel >> 16) & 0xff;
  741. p4[1] = (pixel >> 8) & 0xff;
  742. p4[2] = pixel & 0xff;
  743. } else {
  744. p1[0] = pixel & 0xff;
  745. p1[1] = (pixel >> 8) & 0xff;
  746. p1[2] = (pixel >> 16) & 0xff;
  747. p2[0] = pixel & 0xff;
  748. p2[1] = (pixel >> 8) & 0xff;
  749. p2[2] = (pixel >> 16) & 0xff;
  750. p3[0] = pixel & 0xff;
  751. p3[1] = (pixel >> 8) & 0xff;
  752. p3[2] = (pixel >> 16) & 0xff;
  753. p4[0] = pixel & 0xff;
  754. p4[1] = (pixel >> 8) & 0xff;
  755. p4[2] = (pixel >> 16) & 0xff;
  756. }
  757. break;
  758.  
  759. case 4: //32-bit
  760. *(Uint32 *)p1 = pixel;
  761. *(Uint32 *)p2 = pixel;
  762. *(Uint32 *)p3 = pixel;
  763. *(Uint32 *)p4 = pixel;
  764. break;
  765.  
  766. }
  767. /* ewentualna aktualizacja obrazu (aka double buffering) */
  768. }
  769. }
  770.  
  771. SDL_Color getPixel(int x, int y) {
  772. SDL_Color color ;
  773. Uint32 col = 0 ;
  774. if ((x>=0) && (x<width) && (y>=0) && (y<height)) {
  775. //określamy pozycję
  776. char* pPosition=(char*)screen->pixels ;
  777. //przesunięcie względem y
  778. pPosition+=(screen->pitch*y*2) ;
  779. //przesunięcie względem x
  780. pPosition+=(screen->format->BytesPerPixel*x*2);
  781. //kopiujemy dane piksela
  782. memcpy(&col, pPosition, screen->format->BytesPerPixel);
  783. //konwertujemy kolor
  784. SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
  785. }
  786. return ( color ) ;
  787. }
  788.  
  789. SDL_Color getPixelSurface(int x, int y, SDL_Surface *surface) {
  790. SDL_Color color ;
  791. Uint32 col = 0 ;
  792. //określamy pozycję
  793. char* pPosition=(char*)surface->pixels ;
  794. //przesunięcie względem y
  795. pPosition+=(surface->pitch*y) ;
  796. //przesunięcie względem x
  797. pPosition+=(surface->format->BytesPerPixel*x);
  798. //kopiujemy dane piksela
  799. memcpy(&col, pPosition, surface->format->BytesPerPixel);
  800. //konwertujemy kolor
  801. SDL_GetRGB(col, surface->format, &color.r, &color.g, &color.b);
  802. return ( color ) ;
  803. }
  804.  
  805. void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
  806. {
  807. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
  808. SDL_Flip(screen);
  809.  
  810. }
  811.  
  812. void ladujBMP(char const* nazwa, int x, int y)
  813. {
  814. SDL_Surface* bmp = SDL_LoadBMP(nazwa);
  815. if (!bmp)
  816. {
  817. printf("Unable to load bitmap: %s\n", SDL_GetError());
  818. }
  819. else
  820. {
  821. SDL_Color kolor;
  822. for (int yy=0; yy<bmp->h; yy++) {
  823. for (int xx=0; xx<bmp->w; xx++) {
  824. kolor = getPixelSurface(xx, yy, bmp);
  825. setPixel(xx, yy, kolor.r, kolor.g, kolor.b);
  826. }
  827. }
  828. SDL_FreeSurface(bmp);
  829. SDL_Flip(screen);
  830. }
  831.  
  832. }
  833.  
  834.  
  835. int main ( int argc, char** argv )
  836. {
  837. // console output
  838. freopen( "CON", "wt", stdout );
  839. freopen( "CON", "wt", stderr );
  840.  
  841. // initialize SDL video
  842. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  843. {
  844. printf( "Unable to init SDL: %s\n", SDL_GetError() );
  845. return 1;
  846. }
  847.  
  848. // make sure SDL cleans up before exit
  849. atexit(SDL_Quit);
  850.  
  851. // create a new window
  852. screen = SDL_SetVideoMode(width*2, height*2, 32,
  853. SDL_HWSURFACE|SDL_DOUBLEBUF);
  854. if ( !screen )
  855. {
  856. printf("Unable to set video: %s\n", SDL_GetError());
  857. return 1;
  858. }
  859.  
  860. SDL_WM_SetCaption( tytul , NULL );
  861. // program main loop
  862. bool done = false;
  863. while (!done)
  864. {
  865. // message processing loop
  866. SDL_Event event;
  867. while (SDL_PollEvent(&event))
  868. {
  869. // check for messages
  870. switch (event.type)
  871. {
  872. // exit if the window is closed
  873. case SDL_QUIT:
  874. done = true;
  875. break;
  876.  
  877. // check for keypresses
  878. case SDL_KEYDOWN:
  879. {
  880. // exit if ESCAPE is pressed
  881. if (event.key.keysym.sym == SDLK_ESCAPE)
  882. done = true;
  883. if (event.key.keysym.sym == SDLK_1)
  884. Funkcja1();
  885. if (event.key.keysym.sym == SDLK_2)
  886. Funkcja2();
  887. if (event.key.keysym.sym == SDLK_3)
  888. Funkcja3();
  889. if (event.key.keysym.sym == SDLK_4)
  890. Funkcja4();
  891. if (event.key.keysym.sym == SDLK_5)
  892. Funkcja5();
  893. if (event.key.keysym.sym == SDLK_6)
  894. Funkcja6();
  895. if (event.key.keysym.sym == SDLK_7)
  896. Funkcja7();
  897.  
  898. if (event.key.keysym.sym == SDLK_a)
  899. ladujBMP("obrazek1.bmp", 0, 0);
  900. if (event.key.keysym.sym == SDLK_s)
  901. ladujBMP("obrazek2.bmp", 0, 0);
  902. if (event.key.keysym.sym == SDLK_d)
  903. ladujBMP("obrazek3.bmp", 0, 0);
  904. if (event.key.keysym.sym == SDLK_f)
  905. ladujBMP("obrazek4.bmp", 0, 0);
  906. if (event.key.keysym.sym == SDLK_z)
  907. ladujBMP("obrazek5.bmp", 0, 0);
  908. if (event.key.keysym.sym == SDLK_x)
  909. ladujBMP("obrazek6.bmp", 0, 0);
  910. if (event.key.keysym.sym == SDLK_c)
  911. ladujBMP("obrazek7.bmp", 0, 0);
  912. if (event.key.keysym.sym == SDLK_v)
  913. ladujBMP("obrazek8.bmp", 0, 0);
  914.  
  915. if (event.key.keysym.sym == SDLK_l)
  916. Linia(rand()%width, rand()%height, rand()%width, rand()%height, 100+rand()%155, 100+rand()%155, 100+rand()%155);
  917. if (event.key.keysym.sym == SDLK_o)
  918. Okrag(rand()%width, rand()%height, rand()%200, 100+rand()%155, 100+rand()%155, 100+rand()%155);
  919. if (event.key.keysym.sym == SDLK_e)
  920. Elipsa(rand()%width, rand()%height, rand()%200, rand()%200, 100+rand()%155, 100+rand()%155, 100+rand()%155);
  921. if (event.key.keysym.sym == SDLK_b)
  922. czyscEkran(0, 0, 10); break;
  923. }
  924. } // end switch
  925. } // end of message processing
  926.  
  927. } // end main loop
  928.  
  929.  
  930. // all is well ;)
  931. printf("Exited cleanly\n");
  932. return 0;
  933. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement