Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef __cplusplus
- #include <cstdlib>
- #include <ctime>
- #else
- #include <stdlib.h>
- #include <time.h>
- #endif
- #ifdef __APPLE__
- #include <SDL/SDL.h>
- #else
- #include <SDL.h>
- #endif
- #include <iostream>
- using namespace std;
- SDL_Surface *screen;
- int width = 900;
- int height = 600;
- char const* tytul = "GKiM - Lab 1 - Nazwisko Imie";
- int initSdl();
- void Funkcja1();
- void Funkcja2();
- void Funkcja3();
- void Funkcja4();
- void Funkcja5();
- void Funkcja6();
- void ladujBMP(char const* nazwa, int x, int y);
- void czyscEkran(Uint8 R, Uint8 G, Uint8 B);
- #ifdef _WIN32
- // to solve problem: https://stackoverflow.com/questions/5259714/undefined-reference-to-winmain16
- int WinMain() {
- return main(1, NULL);
- }
- #endif // _WIN32
- int main ( int argc, char** argv ) {
- if (initSdl() )
- {
- return -1;
- }
- // program main loop
- bool done = false;
- while (!done)
- {
- // message processing loop
- SDL_Event event;
- while (SDL_PollEvent(&event))
- {
- if (SDL_QUIT == event.type)
- {
- done = true;
- }
- else if(SDL_KEYDOWN == event.type)
- {
- if (event.key.keysym.sym == SDLK_ESCAPE)
- done = true;
- if (event.key.keysym.sym == SDLK_1)
- Funkcja1();
- if (event.key.keysym.sym == SDLK_2)
- Funkcja2();
- if (event.key.keysym.sym == SDLK_3)
- Funkcja3();
- if (event.key.keysym.sym == SDLK_4)
- Funkcja4();
- if (event.key.keysym.sym == SDLK_5)
- Funkcja5();
- if (event.key.keysym.sym == SDLK_6)
- Funkcja6();
- if (event.key.keysym.sym == SDLK_a)
- ladujBMP("obrazek1.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_s)
- ladujBMP("obrazek2.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_d)
- ladujBMP("obrazek3.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_b)
- czyscEkran(0, 0, 0);
- }
- } // end of message processing
- } // end main loop
- }
- int initSdl()
- {
- // initialize SDL video
- if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
- {
- printf( "Unable to init SDL: %s\n", SDL_GetError() );
- return 1;
- }
- // make sure SDL cleans up before exit
- atexit(SDL_Quit);
- const int bitDepth = 32;
- // create a new window
- screen = SDL_SetVideoMode(width, height, bitDepth, SDL_HWSURFACE | SDL_DOUBLEBUF);
- if ( !screen )
- {
- printf("Unable to set video: %s\n", SDL_GetError());
- return 1;
- }
- SDL_WM_SetCaption( tytul , NULL );
- return 0;
- }
- Uint8* getPixelAddress(int x, int y)
- {
- if ((x>=0) && (x<width) && (y>=0) && (y<height))
- {
- /* Pobieramy informacji ile bajtów zajmuje jeden pixel */
- const int bpp = screen->format->BytesPerPixel;
- /* Obliczamy adres pixela */
- return (Uint8*)screen->pixels + y * screen->pitch + x * bpp;
- }
- return NULL;
- }
- SDL_Color getPixel(int x, int y)
- {
- Uint8* pixelAddress = getPixelAddress(x, y);
- SDL_Color color = {};
- if (pixelAddress)
- {
- Uint32 col = 0;
- memcpy(&col, pixelAddress, screen->format->BytesPerPixel);
- SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
- }
- return color;
- }
- void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
- {
- Uint8* pixelAddress = getPixelAddress(x, y);
- if (pixelAddress)
- {
- Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
- switch(screen->format->BytesPerPixel)
- {
- case 1: //8-bit
- *pixelAddress = pixel;
- break;
- case 2: //16-bit
- *(Uint16*)pixelAddress = pixel;
- break;
- case 3: //24-bit
- if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
- pixelAddress[0] = (pixel >> 16) & 0xff;
- pixelAddress[1] = (pixel >> 8) & 0xff;
- pixelAddress[2] = pixel & 0xff;
- } else {
- pixelAddress[0] = pixel & 0xff;
- pixelAddress[1] = (pixel >> 8) & 0xff;
- pixelAddress[2] = (pixel >> 16) & 0xff;
- }
- break;
- case 4: //32-bit
- *(Uint32*)pixelAddress = pixel;
- break;
- }
- }
- /* update the screen (aka double buffering) */
- }
- void ladujBMP(char const* nazwa, int x, int y)
- {
- SDL_Surface* bmp = SDL_LoadBMP(nazwa);
- if (!bmp)
- {
- printf("Unable to load bitmap: %s\n", SDL_GetError());
- }
- else
- {
- SDL_Rect dstrect;
- dstrect.x = x;
- dstrect.y = y;
- SDL_BlitSurface(bmp, 0, screen, &dstrect);
- SDL_Flip(screen);
- SDL_FreeSurface(bmp);
- }
- }
- void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
- {
- SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
- SDL_Flip(screen);
- }
- void Funkcja1()
- {
- SDL_Color color;
- int imageW = width/2, imageH = height/2;
- for (int h = 0; h < imageH; ++h) {
- for (int w = 0; w < imageW; ++w) {
- color = getPixel(w, h);
- setPixel(w + imageW, h, color.r, 0, 0);
- setPixel(w, h + imageH, 0, color.g, 0);
- setPixel(w + imageW, h + imageH, 0, 0, color.b);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja2()
- {
- SDL_Color color;
- float c, m, y, k;
- int imageW = width/2, imageH = height/2;
- for (int h = 0; h < imageH; ++h) {
- for (int w = 0; w < imageW; ++w) {
- color = getPixel(w, h);
- c = 255 - color.r;
- m = 255 - color.g;
- y = 255 - color.b;
- setPixel(w + imageW, h, 255 - color.r, 255 - color.g, 255 - color.b);
- setPixel(w, h + imageH, c, m, y);
- setPixel(w + imageW, h + imageH, 255 - c, 255 - m, 255 - y);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja3()
- {
- SDL_Color color;
- int gray;
- float grayCMYK;
- float c, m, y, k;
- int imageW = width/2, imageH = height/2;
- for (int h = 0; h < imageH; ++h) {
- for (int w = 0; w < imageW; ++w) {
- color = getPixel(w, h);
- c = 255 - color.r;
- m = 255 - color.g;
- y = 255 - color.b;
- k = min(min(c, m), y);
- c = (c - k)/(255 - k);
- m = (m - k)/(255 - k);
- y = (y - k)/(255 - k);
- gray = (color.r + color.g + color.b) / 3;
- grayCMYK = ((c + m + y) / 3) * 255;
- setPixel(w + imageW, h, gray, gray, gray);
- setPixel(w, h + imageH, grayCMYK, grayCMYK, grayCMYK);
- setPixel(w + imageW, h + imageH, k, k, k);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja4()
- {
- SDL_Color color;
- float c, m, y, k;
- int imageW = width/2, imageH = height/2;
- for (int h = 0; h < imageH; ++h) {
- for (int w = 0; w < imageW; ++w) {
- color = getPixel(w, h);
- c = 255 - color.r;
- m = 255 - color.g;
- y = 255 - color.b;
- k = min(min(c, m), y);
- c = (c - k)/(255 - k);
- m = (m - k)/(255 - k);
- y = (y - k)/(255 - k);
- setPixel(w, h, c * 255, m * 255, y * 255);
- setPixel(w + imageW, h, c * 255, 0, 0);
- setPixel(w, h + imageH, 0, m * 255, 0);
- setPixel(w + imageW, h + imageH, 0, 0, y * 255);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja5()
- {
- SDL_Color color;
- float y, u, v;
- float r, g, b;
- int imageW = width/2, imageH = height/2;
- for (int h = 0; h < imageH; ++h) {
- for (int w = 0; w < imageW; ++w) {
- color = getPixel(w, h);
- y = (0.257 * color.r) + 0.504 * color.g + 0.098 * color.b + 16;
- u = -(0.148 * color.r) - (0.291 * color.g) + (0.439 * color.b) + 128;
- v = (0.439 * color.r) - (0.368 * color.g) - (0.071 * color.b) + 128;
- setPixel(w + imageW, h, y, y, y);
- setPixel(w/2 + imageW, h/2 + imageH, u, u, u);
- setPixel(w/2 + imageW + imageW/2, h/2 + imageH, v, v, v);
- setPixel(w/2 + imageW, h/2 + imageH + imageH/2, 0, 0, 0);
- setPixel(w/2 + imageW + imageW/2, h/2 + imageH + imageH/2, 0, 0, 0);
- b = 1.164 * (y - 16) + 2.018 * (u - 128);
- g = 1.164 * (y - 16) - 0.813 * (v - 128) - 0.391 * (u - 128);
- r = 1.164 * (y - 16) + 1.596 * (v - 128);
- setPixel(w, h + imageH, r, g, b);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja6()
- {
- //...
- SDL_Flip(screen);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement