Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef __cplusplus
- #include <cstdlib>
- #else
- #include <stdlib.h>
- #endif
- #include <SDL/SDL.h>
- #include <math.h>
- #define pi 3.1415926536
- #include <time.h>
- #include <iostream>
- #include <fstream>
- using namespace std;
- SDL_Surface *screen;
- int width = 512;
- int height = 340;
- char const* tytul = "GKiM2019 - Lab 10 - Wójcik Krystian";
- SDL_Color paleta[256];
- int ileKolorow = 0;
- struct YCbCr {
- float y;
- float cb;
- float cr;
- };
- struct HSV {
- float h;
- float s;
- float v;
- };
- void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B);
- SDL_Color getPixel (int x, int y);
- void czyscEkran(Uint8 R, Uint8 G, Uint8 B);
- void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B);
- void Okrag(int x, int y, int r, Uint8 R, Uint8 G, Uint8 B);
- void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B);
- YCbCr RGBtoYCbCr(SDL_Color rgb);
- SDL_Color YCbCrtoRGB(YCbCr ycbcr);
- SDL_Color HSVtoRGB(HSV hsv);
- HSV RGBtoHSV(SDL_Color rgb);
- void Funkcja1();
- void Funkcja2();
- void Funkcja3();
- void Funkcja4();
- void Funkcja5();
- void Funkcja6();
- void Funkcja7();
- void Funkcja8();
- void Funkcja9();
- bool porownajKolory(SDL_Color k1, SDL_Color k2){
- bool wynik = true;
- if(k1.r != k2.r) wynik=false;
- if(k1.g != k2.g) wynik=false;
- if(k1.b != k2.b) wynik=false;
- return wynik;
- }
- int dodajKolor(SDL_Color kolor){
- cout<<ileKolorow<<": ["<<
- (int)kolor.r<<", "<<(int)kolor.g<<", "<<(int)kolor.b<<"]"<<endl;
- paleta[ileKolorow] = kolor;
- ileKolorow++;
- return (ileKolorow-1);
- }
- int sprawdzKolor(SDL_Color kolor){
- int wynik = -1;
- if(ileKolorow>0){
- for(int k=0; k<ileKolorow;k++){
- if(porownajKolory(kolor, paleta[k])){
- wynik = k;
- }
- }
- }
- if(wynik<0){
- wynik = dodajKolor(kolor);
- }
- return wynik;
- }
- void Funkcja1() {
- ileKolorow = 0;
- SDL_Color kolor;
- for(int y =0; y<(height/2); y++){
- for(int x =0; x<(width/2); x++){
- kolor = getPixel(x, y);
- sprawdzKolor(kolor);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja2() {
- SDL_Color kolor;
- Uint16 szerokosc = width/2;
- Uint16 wysokosc = height/2;
- Uint8 wartosc = 0;
- cout<<"Zapisujemy za pomoca cout plik obrazek.bin"<<endl;
- ofstream wyjscie("obrazek.bin");
- wyjscie<<"KW"<<szerokosc<<wysokosc;
- for(int y=0;y<wysokosc;y++){
- for(int x=0; x<szerokosc; x++){
- kolor = getPixel(x, y);
- wartosc = sprawdzKolor(kolor);
- wyjscie<<wartosc;
- }
- }
- wyjscie.close();
- SDL_Flip(screen);
- }
- void Funkcja3() {
- SDL_Color kolor;
- Uint16 szerokosc = width/2;
- Uint16 wysokosc = height/2;
- Uint8 kolory = ileKolorow;
- Uint8 wartosc = 0;
- char identyfikator[] = "KW";
- cout<<"Zapisujemy za pomoca write plik obrazek.bin"<<endl;
- ofstream wyjscie("obrazek2.bin", ios::binary);
- wyjscie.write((char*)&identyfikator, sizeof(char)*2);
- wyjscie.write((char*)&szerokosc, sizeof(Uint16));
- wyjscie.write((char*)&wysokosc, sizeof(Uint16));
- wyjscie.write((char*)&kolory, sizeof(Uint8));
- for(int y=0;y<wysokosc;y++){
- for(int x=0; x<szerokosc; x++){
- kolor = getPixel(x, y);
- wartosc = sprawdzKolor(kolor);
- wyjscie.write((char*)&wartosc, sizeof(Uint8));
- }
- }
- wyjscie.close();
- SDL_Flip(screen);
- }
- void Funkcja4() {
- SDL_Color kolor;
- Uint8 wartosc;
- Uint16 szerokosc;
- Uint16 wysokosc;
- Uint8 kolory;
- char identyfikator[] = " ";
- cout<<"Odczytujemy za pomoca read plik obrazek2.bin" << endl;
- ifstream wejscie("obrazek2.bin", ios::binary);
- wejscie.read((char*)&identyfikator, sizeof(char)*2);
- wejscie.read((char*)&szerokosc, sizeof(Uint16));
- wejscie.read((char*)&wysokosc, sizeof(Uint16));
- wejscie.read((char*)&kolory, sizeof(Uint8));
- cout<<"Parametry: "<<endl;
- cout<<"Identyfikator: "<<identyfikator<<endl;
- cout<<"Szerokosc: "<<szerokosc<<endl;
- cout<<"Wysokosc: "<<wysokosc<<endl;
- cout<<"Kolory: "<<(int)kolory<<endl;
- for(int y=0;y<wysokosc;y++){
- for(int x=0; x<szerokosc; x++){
- wejscie.read((char*)&wartosc, sizeof(Uint8));
- kolor = paleta[wartosc];
- setPixel(x+(width/2), y, kolor.r, kolor.g, kolor.b);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja5() {
- //...
- SDL_Flip(screen);
- }
- void Funkcja6() {
- //...
- SDL_Flip(screen);
- }
- void Funkcja7() {
- //...
- SDL_Flip(screen);
- }
- void Funkcja8() {
- //...
- SDL_Flip(screen);
- }
- void Funkcja9() {
- //...
- SDL_Flip(screen);
- }
- void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B) {
- setPixel(x1,y1,R,G,B);
- setPixel(x2,y2,R,G,B);
- // zmienne pomocnicze
- int d, dx, dy, ai, bi, xi, yi;
- int x = x1, y = y1;
- // ustalenie kierunku rysowania
- if (x1 < x2)
- {
- xi = 1;
- dx = x2 - x1;
- }
- else
- {
- xi = -1;
- dx = x1 - x2;
- }
- // ustalenie kierunku rysowania
- if (y1 < y2)
- {
- yi = 1;
- dy = y2 - y1;
- }
- else
- {
- yi = -1;
- dy = y1 - y2;
- }
- // pierwszy piksel
- setPixel(x, y, R, G, B);
- // oś wiodąca OX
- if (dx > dy)
- {
- ai = (dy - dx) * 2;
- bi = dy * 2;
- d = bi - dx;
- // pętla po kolejnych x
- while (x != x2)
- {
- // test współczynnika
- if (d >= 0)
- {
- x += xi;
- y += yi;
- d += ai;
- }
- else
- {
- d += bi;
- x += xi;
- }
- setPixel(x, y, R, G, B);
- }
- }
- // oś wiodąca OY
- else
- {
- ai = ( dx - dy ) * 2;
- bi = dx * 2;
- d = bi - dy;
- // pętla po kolejnych y
- while (y != y2)
- {
- // test współczynnika
- if (d >= 0)
- {
- x += xi;
- y += yi;
- d += ai;
- }
- else
- {
- d += bi;
- y += yi;
- }
- setPixel(x, y, R, G, B);
- }
- }
- SDL_Flip(screen);
- }
- void RysujOkrag(int x0, int y0, int x, int y, Uint8 R, Uint8 G, Uint8 B){
- setPixel(x+x0, y+y0, R, G, B);
- setPixel(y+x0, x+y0, R, G, B);
- setPixel(y+x0, -x+y0, R, G, B);
- setPixel(x+x0, -y+y0, R, G, B);
- setPixel(-x+x0, -y+y0, R, G, B);
- setPixel(-y+x0, -x+y0, R, G, B);
- setPixel(-y+x0, x+y0, R, G, B);
- setPixel(-x+x0, y+y0, R, G, B);;
- }
- void Okrag(int x0, int y0, int r, Uint8 R, Uint8 G, Uint8 B) {
- int x =0;
- int y=r;
- int d = 3-2*r;
- while (x <= y){
- if (d<0){
- d=d+4*x+6;
- RysujOkrag(x0, y0, x, y, R, G, B);
- } else{
- d=d+4*(x-y)+10;
- y--;
- RysujOkrag(x0, y0, x, y, R, G, B);
- }
- x++;
- }
- SDL_Flip(screen);
- }
- void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B) {
- int xc = 0,
- yc = b;
- int aa = a * a,
- aa2 = aa + aa,
- bb = b * b,
- bb2 = bb + bb;
- int d = bb - aa * b + (aa / 4),
- dx = 0,
- dy = aa2 * b;
- while (dx < dy) {
- setPixel (x - xc,y - yc, R, G, B);
- setPixel (x - xc,y + yc, R, G, B);
- setPixel (x + xc,y - yc, R, G, B);
- setPixel (x + xc,y + yc, R, G, B);
- if (d > 0){
- yc = yc-1;
- dy -= aa2;
- d -= dy;
- }
- xc =xc+1;
- dx += bb2;
- d += bb + dx;
- }
- d += (3 * ((aa - bb) / 2) - (dx + dy) / 2);
- while (yc >= 0) {
- setPixel (x - xc,y - yc, R, G, B);
- setPixel (x - xc,y + yc, R, G, B);
- setPixel (x + xc,y - yc, R, G, B);
- setPixel (x + xc,y + yc, R, G, B);
- if (d < 0) {
- xc =xc+1;
- dx += bb2;
- d += (bb + dx);
- }
- yc = yc-1;
- dy -= aa2;
- d += aa - dy;
- }
- SDL_Flip(screen);
- }
- YCbCr RGBtoYCbCr(SDL_Color rgb) {
- YCbCr ycbcr;
- ycbcr.y = 0.299 * rgb.r + 0.587*rgb.g + 0.114*rgb.b;
- ycbcr.cb = 128 - 0.168736 * rgb.r - 0.331264*rgb.g + 0.5*rgb.b;
- ycbcr.cr = 128 + 0.5 * rgb.r - 0.418688*rgb.g - 0.081312*rgb.b;
- return ycbcr;
- }
- float normalizacja(float wartosc) {
- float wynik = wartosc;
- if (wartosc > 255) wartosc = 255;
- if (wartosc < 0) wartosc = 0;
- return wartosc;
- }
- SDL_Color YCbCrtoRGB(YCbCr ycbcr) {
- SDL_Color rgb;
- int R, G, B;
- R = ycbcr.y + 1.402*(ycbcr.cr-128);
- G = ycbcr.y - 0.344136*(ycbcr.cb-128) - 0.714136*(ycbcr.cr-128);
- B = ycbcr.y + 1.772*(ycbcr.cb-128);
- R = normalizacja(R);
- G = normalizacja(G);
- B = normalizacja(B);
- rgb = {.r=R, .g=G, .b=B};
- return rgb;
- }
- HSV RGBtoHSV(SDL_Color rgb) {
- HSV hsv;
- unsigned char rgbMin, rgbMax;
- rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
- rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);
- hsv.v = rgbMax;
- if (hsv.v == 0) {
- hsv.h = 0;
- hsv.s = 0;
- return hsv;
- }
- hsv.s = 255.0*(rgbMax-rgbMin)/hsv.v;
- if (hsv.s == 0) {
- hsv.h = 0;
- return hsv;
- }
- if (rgbMax == rgb.r) {
- hsv.h = 0 + 43.0*(rgb.g-rgb.b)/(rgbMax-rgbMin);
- }
- else if (rgbMax == rgb.g) {
- hsv.h = 85 + 43.0*(rgb.b-rgb.r)/(rgbMax-rgbMin);
- }
- else {
- hsv.h = 171 + 43.0*(rgb.r-rgb.g)/(rgbMax-rgbMin);
- }
- if (hsv.h>255) hsv.h-=255;
- if (hsv.h<0) hsv.h+=255;
- hsv.s = normalizacja(hsv.s);
- hsv.v = normalizacja(hsv.v);
- return hsv;
- }
- SDL_Color HSVtoRGB(HSV hsv) {
- SDL_Color rgb;
- int R, G, B;
- if (hsv.h < 0) hsv.h += 255;
- if (hsv.h > 255) hsv.h -= 255;
- hsv.s = normalizacja(hsv.s);
- hsv.v = normalizacja(hsv.v);
- unsigned char region, p, q, t;
- double h;
- unsigned int s, v, reszta;
- if (hsv.s == 0) {
- R = hsv.v;
- G = hsv.v;
- B = hsv.v;
- }
- h = hsv.h;
- s = hsv.s;
- v = hsv.v;
- region = h / 43;
- reszta = (h - (region * 43) ) * 6;
- p = (v * (255 - s)) >> 8;
- q = (v * (255 - ((s * reszta) >> 8 ))) >> 8;
- t = (v * (255 - ((s * (255 - reszta)) >> 8 ))) >> 8;
- switch (region) {
- case 0:
- R = v; G = t; B = p;
- break;
- case 1:
- R = q; G = v; B = p;
- break;
- case 2:
- R = p; G = v; B = t;
- break;
- case 3:
- R = p; G = q; B = v;
- break;
- case 4:
- R = t; G = p; B = v;
- break;
- default:
- R = v; G = p; B = q;
- break;
- }
- rgb = {R, G, B};
- return rgb;
- }
- void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
- {
- if ((x>=0) && (x<width) && (y>=0) && (y<height))
- {
- /* Zamieniamy poszczególne składowe koloru na format koloru piksela */
- Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
- /* Pobieramy informację ile bajtów zajmuje jeden piksel */
- int bpp = screen->format->BytesPerPixel;
- /* Obliczamy adres piksela */
- Uint8 *p1 = (Uint8 *)screen->pixels + (y*2) * screen->pitch + (x*2) * bpp;
- Uint8 *p2 = (Uint8 *)screen->pixels + (y*2+1) * screen->pitch + (x*2) * bpp;
- Uint8 *p3 = (Uint8 *)screen->pixels + (y*2) * screen->pitch + (x*2+1) * bpp;
- Uint8 *p4 = (Uint8 *)screen->pixels + (y*2+1) * screen->pitch + (x*2+1) * bpp;
- /* Ustawiamy wartość piksela, w zależności od formatu powierzchni*/
- switch(bpp)
- {
- case 1: //8-bit
- *p1 = pixel;
- *p2 = pixel;
- *p3 = pixel;
- *p4 = pixel;
- break;
- case 2: //16-bit
- *(Uint16 *)p1 = pixel;
- *(Uint16 *)p2 = pixel;
- *(Uint16 *)p3 = pixel;
- *(Uint16 *)p4 = pixel;
- break;
- case 3: //24-bit
- if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
- p1[0] = (pixel >> 16) & 0xff;
- p1[1] = (pixel >> 8) & 0xff;
- p1[2] = pixel & 0xff;
- p2[0] = (pixel >> 16) & 0xff;
- p2[1] = (pixel >> 8) & 0xff;
- p2[2] = pixel & 0xff;
- p3[0] = (pixel >> 16) & 0xff;
- p3[1] = (pixel >> 8) & 0xff;
- p3[2] = pixel & 0xff;
- p4[0] = (pixel >> 16) & 0xff;
- p4[1] = (pixel >> 8) & 0xff;
- p4[2] = pixel & 0xff;
- } else {
- p1[0] = pixel & 0xff;
- p1[1] = (pixel >> 8) & 0xff;
- p1[2] = (pixel >> 16) & 0xff;
- p2[0] = pixel & 0xff;
- p2[1] = (pixel >> 8) & 0xff;
- p2[2] = (pixel >> 16) & 0xff;
- p3[0] = pixel & 0xff;
- p3[1] = (pixel >> 8) & 0xff;
- p3[2] = (pixel >> 16) & 0xff;
- p4[0] = pixel & 0xff;
- p4[1] = (pixel >> 8) & 0xff;
- p4[2] = (pixel >> 16) & 0xff;
- }
- break;
- case 4: //32-bit
- *(Uint32 *)p1 = pixel;
- *(Uint32 *)p2 = pixel;
- *(Uint32 *)p3 = pixel;
- *(Uint32 *)p4 = pixel;
- break;
- }
- /* ewentualna aktualizacja obrazu (aka double buffering) */
- }
- }
- SDL_Color getPixel(int x, int y) {
- SDL_Color color ;
- Uint32 col = 0 ;
- if ((x>=0) && (x<width) && (y>=0) && (y<height)) {
- //określamy pozycję
- char* pPosition=(char*)screen->pixels ;
- //przesunięcie względem y
- pPosition+=(screen->pitch*y*2) ;
- //przesunięcie względem x
- pPosition+=(screen->format->BytesPerPixel*x*2);
- //kopiujemy dane piksela
- memcpy(&col, pPosition, screen->format->BytesPerPixel);
- //konwertujemy kolor
- SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
- }
- return ( color ) ;
- }
- SDL_Color getPixelSurface(int x, int y, SDL_Surface *surface) {
- SDL_Color color ;
- Uint32 col = 0 ;
- //określamy pozycję
- char* pPosition=(char*)surface->pixels ;
- //przesunięcie względem y
- pPosition+=(surface->pitch*y) ;
- //przesunięcie względem x
- pPosition+=(surface->format->BytesPerPixel*x);
- //kopiujemy dane piksela
- memcpy(&col, pPosition, surface->format->BytesPerPixel);
- //konwertujemy kolor
- SDL_GetRGB(col, surface->format, &color.r, &color.g, &color.b);
- return ( color ) ;
- }
- void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
- {
- SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
- SDL_Flip(screen);
- }
- 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_Color kolor;
- for (int yy=0; yy<bmp->h; yy++) {
- for (int xx=0; xx<bmp->w; xx++) {
- kolor = getPixelSurface(xx, yy, bmp);
- setPixel(xx, yy, kolor.r, kolor.g, kolor.b);
- }
- }
- SDL_FreeSurface(bmp);
- SDL_Flip(screen);
- }
- }
- int main ( int argc, char** argv )
- {
- // console output
- freopen( "CON", "wt", stdout );
- freopen( "CON", "wt", stderr );
- // 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);
- // create a new window
- screen = SDL_SetVideoMode(width*2, height*2, 32,
- SDL_HWSURFACE|SDL_DOUBLEBUF);
- if ( !screen )
- {
- printf("Unable to set video: %s\n", SDL_GetError());
- return 1;
- }
- SDL_WM_SetCaption( tytul , NULL );
- // program main loop
- bool done = false;
- while (!done)
- {
- // message processing loop
- SDL_Event event;
- while (SDL_PollEvent(&event))
- {
- // check for messages
- switch (event.type)
- {
- // exit if the window is closed
- case SDL_QUIT:
- done = true;
- break;
- // check for keypresses
- case SDL_KEYDOWN:
- {
- // exit if ESCAPE is pressed
- 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_7)
- Funkcja7();
- 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_z)
- ladujBMP("obrazek4.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_x)
- ladujBMP("obrazek5.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_c)
- ladujBMP("obrazek6.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_l)
- Linia(rand()%width, rand()%height, rand()%width, rand()%height, 100+rand()%155, 100+rand()%155, 100+rand()%155);
- if (event.key.keysym.sym == SDLK_o)
- Okrag(rand()%width, rand()%height, rand()%200, 100+rand()%155, 100+rand()%155, 100+rand()%155);
- if (event.key.keysym.sym == SDLK_e)
- Elipsa(rand()%width, rand()%height, rand()%200, rand()%200, 100+rand()%155, 100+rand()%155, 100+rand()%155);
- if (event.key.keysym.sym == SDLK_b)
- czyscEkran(0, 0, 10); break;
- }
- } // end switch
- } // end of message processing
- } // end main loop
- // all is well ;)
- printf("Exited cleanly\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement