Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. #ifdef __cplusplus
  2. #include <cstdlib>
  3. #include <ctime>
  4. #else
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #endif
  8.  
  9. #ifdef __APPLE__
  10. #include <SDL/SDL.h>
  11. #else
  12. #include <SDL.h>
  13. #endif
  14.  
  15. #include <iostream>
  16. using namespace std;
  17.  
  18. SDL_Surface *screen;
  19. int width = 900;
  20. int height = 600;
  21. char const* tytul = "GKiM - Lab 1 - Nazwisko Imie";
  22.  
  23.  
  24. int initSdl();
  25.  
  26. void Funkcja1();
  27. void Funkcja2();
  28. void Funkcja3();
  29. void Funkcja4();
  30. void Funkcja5();
  31. void Funkcja6();
  32.  
  33. void ladujBMP(char const* nazwa, int x, int y);
  34.  
  35. void czyscEkran(Uint8 R, Uint8 G, Uint8 B);
  36.  
  37. #ifdef _WIN32
  38. // to solve problem: https://stackoverflow.com/questions/5259714/undefined-reference-to-winmain16
  39. int WinMain() {
  40. return main(1, NULL);
  41. }
  42. #endif // _WIN32
  43.  
  44. int main ( int argc, char** argv ) {
  45. if (initSdl() )
  46. {
  47. return -1;
  48. }
  49.  
  50. // program main loop
  51. bool done = false;
  52. while (!done)
  53. {
  54. // message processing loop
  55. SDL_Event event;
  56. while (SDL_PollEvent(&event))
  57. {
  58. if (SDL_QUIT == event.type)
  59. {
  60. done = true;
  61. }
  62. else if(SDL_KEYDOWN == event.type)
  63. {
  64. if (event.key.keysym.sym == SDLK_ESCAPE)
  65. done = true;
  66. if (event.key.keysym.sym == SDLK_1)
  67. Funkcja1();
  68. if (event.key.keysym.sym == SDLK_2)
  69. Funkcja2();
  70. if (event.key.keysym.sym == SDLK_3)
  71. Funkcja3();
  72. if (event.key.keysym.sym == SDLK_4)
  73. Funkcja4();
  74. if (event.key.keysym.sym == SDLK_5)
  75. Funkcja5();
  76. if (event.key.keysym.sym == SDLK_6)
  77. Funkcja6();
  78. if (event.key.keysym.sym == SDLK_a)
  79. ladujBMP("obrazek1.bmp", 0, 0);
  80. if (event.key.keysym.sym == SDLK_s)
  81. ladujBMP("obrazek2.bmp", 0, 0);
  82. if (event.key.keysym.sym == SDLK_d)
  83. ladujBMP("obrazek3.bmp", 0, 0);
  84. if (event.key.keysym.sym == SDLK_b)
  85. czyscEkran(0, 0, 0);
  86. }
  87. } // end of message processing
  88. } // end main loop
  89. }
  90.  
  91. int initSdl()
  92. {
  93. // initialize SDL video
  94. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  95. {
  96. printf( "Unable to init SDL: %s\n", SDL_GetError() );
  97. return 1;
  98. }
  99.  
  100. // make sure SDL cleans up before exit
  101. atexit(SDL_Quit);
  102.  
  103. const int bitDepth = 32;
  104.  
  105. // create a new window
  106. screen = SDL_SetVideoMode(width, height, bitDepth, SDL_HWSURFACE | SDL_DOUBLEBUF);
  107. if ( !screen )
  108. {
  109. printf("Unable to set video: %s\n", SDL_GetError());
  110. return 1;
  111. }
  112.  
  113. SDL_WM_SetCaption( tytul , NULL );
  114.  
  115. return 0;
  116. }
  117.  
  118.  
  119. Uint8* getPixelAddress(int x, int y)
  120. {
  121. if ((x>=0) && (x<width) && (y>=0) && (y<height))
  122. {
  123. /* Pobieramy informacji ile bajtów zajmuje jeden pixel */
  124. const int bpp = screen->format->BytesPerPixel;
  125.  
  126. /* Obliczamy adres pixela */
  127. return (Uint8*)screen->pixels + y * screen->pitch + x * bpp;
  128. }
  129. return NULL;
  130. }
  131.  
  132. SDL_Color getPixel(int x, int y)
  133. {
  134. Uint8* pixelAddress = getPixelAddress(x, y);
  135.  
  136. SDL_Color color = {};
  137. if (pixelAddress)
  138. {
  139. Uint32 col = 0;
  140.  
  141. memcpy(&col, pixelAddress, screen->format->BytesPerPixel);
  142. SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
  143. }
  144.  
  145. return color;
  146. }
  147.  
  148. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
  149. {
  150. Uint8* pixelAddress = getPixelAddress(x, y);
  151. if (pixelAddress)
  152. {
  153. Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
  154.  
  155. switch(screen->format->BytesPerPixel)
  156. {
  157. case 1: //8-bit
  158. *pixelAddress = pixel;
  159. break;
  160.  
  161. case 2: //16-bit
  162. *(Uint16*)pixelAddress = pixel;
  163. break;
  164.  
  165. case 3: //24-bit
  166. if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
  167. pixelAddress[0] = (pixel >> 16) & 0xff;
  168. pixelAddress[1] = (pixel >> 8) & 0xff;
  169. pixelAddress[2] = pixel & 0xff;
  170. } else {
  171. pixelAddress[0] = pixel & 0xff;
  172. pixelAddress[1] = (pixel >> 8) & 0xff;
  173. pixelAddress[2] = (pixel >> 16) & 0xff;
  174. }
  175. break;
  176. case 4: //32-bit
  177. *(Uint32*)pixelAddress = pixel;
  178. break;
  179. }
  180. }
  181. /* update the screen (aka double buffering) */
  182. }
  183.  
  184. void ladujBMP(char const* nazwa, int x, int y)
  185. {
  186. SDL_Surface* bmp = SDL_LoadBMP(nazwa);
  187. if (!bmp)
  188. {
  189. printf("Unable to load bitmap: %s\n", SDL_GetError());
  190. }
  191. else
  192. {
  193. SDL_Rect dstrect;
  194. dstrect.x = x;
  195. dstrect.y = y;
  196. SDL_BlitSurface(bmp, 0, screen, &dstrect);
  197. SDL_Flip(screen);
  198. SDL_FreeSurface(bmp);
  199. }
  200. }
  201.  
  202. void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
  203. {
  204. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
  205. SDL_Flip(screen);
  206. }
  207.  
  208.  
  209. void Funkcja1()
  210. {
  211. SDL_Color color;
  212. int imageW = width/2, imageH = height/2;
  213. for (int h = 0; h < imageH; ++h) {
  214. for (int w = 0; w < imageW; ++w) {
  215. color = getPixel(w, h);
  216. setPixel(w + imageW, h, color.r, 0, 0);
  217. setPixel(w, h + imageH, 0, color.g, 0);
  218. setPixel(w + imageW, h + imageH, 0, 0, color.b);
  219. }
  220. }
  221.  
  222. SDL_Flip(screen);
  223. }
  224.  
  225. void Funkcja2()
  226. {
  227. SDL_Color color;
  228. float c, m, y, k;
  229. int imageW = width/2, imageH = height/2;
  230. for (int h = 0; h < imageH; ++h) {
  231. for (int w = 0; w < imageW; ++w) {
  232. color = getPixel(w, h);
  233. c = 255 - color.r;
  234. m = 255 - color.g;
  235. y = 255 - color.b;
  236.  
  237. setPixel(w + imageW, h, 255 - color.r, 255 - color.g, 255 - color.b);
  238. setPixel(w, h + imageH, c, m, y);
  239. setPixel(w + imageW, h + imageH, 255 - c, 255 - m, 255 - y);
  240. }
  241. }
  242.  
  243. SDL_Flip(screen);
  244. }
  245.  
  246.  
  247. void Funkcja3()
  248. {
  249. SDL_Color color;
  250. int gray;
  251. float grayCMYK;
  252. float c, m, y, k;
  253. int imageW = width/2, imageH = height/2;
  254. for (int h = 0; h < imageH; ++h) {
  255. for (int w = 0; w < imageW; ++w) {
  256. color = getPixel(w, h);
  257.  
  258. c = 255 - color.r;
  259. m = 255 - color.g;
  260. y = 255 - color.b;
  261.  
  262. k = min(min(c, m), y);
  263. c = (c - k)/(255 - k);
  264. m = (m - k)/(255 - k);
  265. y = (y - k)/(255 - k);
  266.  
  267. gray = (color.r + color.g + color.b) / 3;
  268. grayCMYK = ((c + m + y) / 3) * 255;
  269. setPixel(w + imageW, h, gray, gray, gray);
  270. setPixel(w, h + imageH, grayCMYK, grayCMYK, grayCMYK);
  271. setPixel(w + imageW, h + imageH, k, k, k);
  272. }
  273. }
  274.  
  275. SDL_Flip(screen);
  276. }
  277.  
  278.  
  279. void Funkcja4()
  280. {
  281. SDL_Color color;
  282. float c, m, y, k;
  283. int imageW = width/2, imageH = height/2;
  284. for (int h = 0; h < imageH; ++h) {
  285. for (int w = 0; w < imageW; ++w) {
  286. color = getPixel(w, h);
  287.  
  288. c = 255 - color.r;
  289. m = 255 - color.g;
  290. y = 255 - color.b;
  291.  
  292. k = min(min(c, m), y);
  293. c = (c - k)/(255 - k);
  294. m = (m - k)/(255 - k);
  295. y = (y - k)/(255 - k);
  296.  
  297. setPixel(w, h, c * 255, m * 255, y * 255);
  298. setPixel(w + imageW, h, c * 255, 0, 0);
  299. setPixel(w, h + imageH, 0, m * 255, 0);
  300. setPixel(w + imageW, h + imageH, 0, 0, y * 255);
  301. }
  302. }
  303.  
  304. SDL_Flip(screen);
  305. }
  306.  
  307.  
  308. void Funkcja5()
  309. {
  310. SDL_Color color;
  311. float y, u, v;
  312. float r, g, b;
  313. int imageW = width/2, imageH = height/2;
  314. for (int h = 0; h < imageH; ++h) {
  315. for (int w = 0; w < imageW; ++w) {
  316. color = getPixel(w, h);
  317.  
  318. y = (0.257 * color.r) + 0.504 * color.g + 0.098 * color.b + 16;
  319. u = -(0.148 * color.r) - (0.291 * color.g) + (0.439 * color.b) + 128;
  320. v = (0.439 * color.r) - (0.368 * color.g) - (0.071 * color.b) + 128;
  321.  
  322. setPixel(w + imageW, h, y, y, y);
  323. setPixel(w/2 + imageW, h/2 + imageH, u, u, u);
  324. setPixel(w/2 + imageW + imageW/2, h/2 + imageH, v, v, v);
  325. setPixel(w/2 + imageW, h/2 + imageH + imageH/2, 0, 0, 0);
  326. setPixel(w/2 + imageW + imageW/2, h/2 + imageH + imageH/2, 0, 0, 0);
  327.  
  328. b = 1.164 * (y - 16) + 2.018 * (u - 128);
  329. g = 1.164 * (y - 16) - 0.813 * (v - 128) - 0.391 * (u - 128);
  330. r = 1.164 * (y - 16) + 1.596 * (v - 128);
  331. setPixel(w, h + imageH, r, g, b);
  332. }
  333. }
  334.  
  335. SDL_Flip(screen);
  336. }
  337.  
  338. void Funkcja6()
  339. {
  340.  
  341.  
  342.  
  343. //...
  344.  
  345.  
  346. SDL_Flip(screen);
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement