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>
- #include <time.h>
- #include <iostream>
- #include <iomanip>
- #define M_PI 3.14159265358979323846
- using namespace std;
- SDL_Surface *screen;
- int width = 512;
- int height = 340;
- char const* tytul = "GKiM2019 - Lab 11 - Czechowicz Konrad";
- struct YCbCr {
- float y;
- float cb;
- float cr;
- };
- struct HSV {
- float h;
- float s;
- float v;
- };
- const int ROZMIAR_BLOKU = 8;
- struct macierz {
- float dct[ROZMIAR_BLOKU][ROZMIAR_BLOKU];
- Uint8 dane[ROZMIAR_BLOKU][ROZMIAR_BLOKU];
- };
- 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);
- macierz dct(Uint8 wartosci[ROZMIAR_BLOKU][ROZMIAR_BLOKU]);
- macierz idct(float DCT[ROZMIAR_BLOKU][ROZMIAR_BLOKU]);
- void Funkcja1();
- void Funkcja2();
- void Funkcja3();
- void Funkcja4();
- void Funkcja5();
- void Funkcja6();
- void Funkcja7();
- void Funkcja8();
- void Funkcja9();
- void Funkcja1() {
- float kolor = 0;
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- setPixel(x+128, y+64, kolor, kolor, kolor);
- kolor += 256.0/(ROZMIAR_BLOKU*ROZMIAR_BLOKU);
- }
- }
- for(int y = 1; y < ROZMIAR_BLOKU; y+=2) {
- for(int x = 1; x < ROZMIAR_BLOKU; x+=2) {
- setPixel(x + 128, y + 64 + ROZMIAR_BLOKU, 255, 255, 255);
- setPixel(x + 128-1, y + 64 + ROZMIAR_BLOKU - 1, 128, 128, 128);
- setPixel(x + 128, y + 64 + ROZMIAR_BLOKU-1, 0, 0, 0);
- setPixel(x + 128-1, y + 64 + ROZMIAR_BLOKU, 0, 0, 0);
- }
- }
- SDL_Flip(screen);
- }
- void wyswietlDane(macierz blok) {
- cout << "Dane pikselowe w macierzy: " << endl;
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- cout << (int)blok.dane[x][y] << " ";
- }
- cout << endl;
- }
- }
- void wyswietlDaneDCT(macierz blok) {
- cout << "Wspolczynniki transformaty w macierzy: " << endl;
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- cout << (float)blok.dct[x][y] << " ";
- }
- cout << endl;
- }
- }
- void Funkcja2() {
- macierz blok;
- macierz blokDCT;
- macierz blokDane;
- //pierwszy blok
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- blok.dane[x][y] = getPixel(x+128, y+64).r;
- blok.dct[x][y] = 0;
- }
- }
- wyswietlDane(blok);
- blokDCT = dct(blok.dane);
- wyswietlDaneDCT(blokDCT);
- blokDane = idct(blokDCT.dct);
- wyswietlDane(blokDane);
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- setPixel(x+128+(width/2), y+64, blokDane.dane[x][y], blokDane.dane[x][y], blokDane.dane[x][y]);
- }
- }
- //drugi blok
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- blok.dane[x][y] = getPixel(x+128, y+64+ROZMIAR_BLOKU).r;
- blok.dct[x][y] = 0;
- }
- }
- wyswietlDane(blok);
- blokDCT = dct(blok.dane);
- wyswietlDaneDCT(blokDCT);
- blokDane = idct(blokDCT.dct);
- wyswietlDane(blokDane);
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- setPixel(x+128+(width/2), y+64+ROZMIAR_BLOKU, blokDane.dane[x][y], blokDane.dane[x][y], blokDane.dane[x][y]);
- }
- }
- SDL_Flip(screen);
- }
- macierz zaokraglenieDCT(macierz blok) {
- macierz wynik = blok;
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- wynik.dct[x][y] = round(blok.dct[x][y]);
- }
- }
- return wynik;
- }
- macierz utnijMacierz(macierz wejscie, int ile) {
- macierz wynik = wejscie;
- if(ile>0)
- wynik.dct[ROZMIAR_BLOKU-1][ROZMIAR_BLOKU-1] = 0;
- int a, b;
- if(ile>1) {
- for(int j = ile; j > 0; j--) {
- for(int i = j-1; i >= 0; i--) {
- a = i+1;
- b = j-i;
- wynik.dct[ROZMIAR_BLOKU-a][ROZMIAR_BLOKU-b] = 0;
- }
- }
- }
- return wynik;
- }
- void Funkcja3() {
- macierz blok;
- macierz blokDCT;
- macierz blokDane;
- //pierwszy blok
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- blok.dane[x][y] = getPixel(x+128, y+64).r;
- blok.dct[x][y] = 0;
- }
- }
- wyswietlDane(blok);
- blokDCT = dct(blok.dane);
- wyswietlDaneDCT(blokDCT);
- blokDCT = zaokraglenieDCT(blokDCT);
- wyswietlDaneDCT(blokDCT);
- blokDCT = utnijMacierz(blokDCT, 5);
- wyswietlDane(blokDCT);
- blokDane = idct(blokDCT.dct);
- wyswietlDane(blokDane);
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- setPixel(x+128+(width/2), y+64, blokDane.dane[x][y], blokDane.dane[x][y], blokDane.dane[x][y]);
- }
- }
- //drugi blok
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- blok.dane[x][y] = getPixel(x+128, y+64+ROZMIAR_BLOKU).r;
- blok.dct[x][y] = 0;
- }
- }
- wyswietlDane(blok);
- blokDCT = dct(blok.dane);
- wyswietlDaneDCT(blokDCT);
- blokDCT = zaokraglenieDCT(blokDCT);
- wyswietlDaneDCT(blokDCT);
- blokDCT = utnijMacierz(blokDCT, 20);
- wyswietlDane(blokDCT);
- blokDane = idct(blokDCT.dct);
- wyswietlDane(blokDane);
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- setPixel(x+128+(width/2), y+64+ROZMIAR_BLOKU, blokDane.dane[x][y], blokDane.dane[x][y], blokDane.dane[x][y]);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja4() {
- macierz blok;
- macierz blokDCT;
- macierz blokDane;
- //drugi blok
- for(int y = 0; y < height/8; y++) {
- for(int x = 0; x < width/8; x++) {
- for(int y = 0; y < ROZMIAR_BLOKU; y++) {
- for(int x = 0; x < ROZMIAR_BLOKU; x++) {
- blok.dane[x][y] = getPixel(x, y).r;
- blok.dct[x][y] = 0;
- }
- }
- }
- }
- wyswietlDane(blok);
- blokDCT = dct(blok.dane);
- wyswietlDaneDCT(blokDCT);
- blokDCT = zaokraglenieDCT(blokDCT);
- wyswietlDaneDCT(blokDCT);
- blokDCT = utnijMacierz(blokDCT, 1);
- wyswietlDane(blokDCT);
- blokDane = idct(blokDCT.dct);
- wyswietlDane(blokDane);
- for(int y = 0; y < height/2; y++) {
- for(int x = 0; x < width/2; x++) {
- setPixel(x+(width/2), y, blokDane.dane[x][y], blokDane.dane[x][y], blokDane.dane[x][y]);
- }
- }
- 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);
- }
- macierz dct(Uint8 wartosci[ROZMIAR_BLOKU][ROZMIAR_BLOKU])
- {
- float wynik[ROZMIAR_BLOKU][ROZMIAR_BLOKU];
- // obliczamy DCT
- for (unsigned v = 0; v < ROZMIAR_BLOKU; ++v) {
- for (unsigned u = 0; u < ROZMIAR_BLOKU; ++u) {
- const double cu = (u == 0) ? 1.0 / sqrt(2) : 1.0;
- const double cv = (v == 0) ? 1.0 / sqrt(2) : 1.0;
- double dctCoeff = 0;
- for (unsigned y = 0; y < ROZMIAR_BLOKU; ++y) {
- for (unsigned x = 0; x < ROZMIAR_BLOKU; ++x) {
- double uCosFactor = cos((double)(2 * x + 1) * M_PI * (double)u / (2 * (double) ROZMIAR_BLOKU));
- double vCosFactor = cos((double)(2 * y + 1) * M_PI * (double)v / (2 * (double) ROZMIAR_BLOKU));
- double pixel = (double)wartosci[x][y];
- dctCoeff += pixel * uCosFactor * vCosFactor;
- }
- }
- dctCoeff *= (2.0 / (double) ROZMIAR_BLOKU) * cu * cv;
- wynik[u][v] = dctCoeff;
- }
- }
- macierz rezultat;
- for (int j=0; j<ROZMIAR_BLOKU; j++) {
- for (int i=0; i<ROZMIAR_BLOKU; i++) {
- rezultat.dct[i][j] = wynik[i][j];
- rezultat.dane[i][j] = wartosci[i][j];
- }
- }
- return rezultat;
- }
- macierz idct(float DCT[ROZMIAR_BLOKU][ROZMIAR_BLOKU]) {
- int wynik[ROZMIAR_BLOKU][ROZMIAR_BLOKU];
- // obliczamy DCT
- for (unsigned x = 0; x < ROZMIAR_BLOKU; ++x) {
- for (unsigned y = 0; y < ROZMIAR_BLOKU; ++y) {
- double pixel = 0;
- for (unsigned u = 0; u < ROZMIAR_BLOKU; ++u) {
- for (unsigned v = 0; v < ROZMIAR_BLOKU; ++v) {
- const double cu = (u == 0) ? 1.0 / sqrt(2) : 1.0;
- const double cv = (v == 0) ? 1.0 / sqrt(2) : 1.0;
- double uCosFactor = cos((double)(2 * x + 1) * M_PI * (double)u / (2 * (double) ROZMIAR_BLOKU));
- double vCosFactor = cos((double)(2 * y + 1) * M_PI * (double)v / (2 * (double) ROZMIAR_BLOKU));
- double coeff = DCT[u][v];
- pixel += coeff * uCosFactor * vCosFactor * cu * cv;
- }
- }
- pixel *= (2.0 / (double) ROZMIAR_BLOKU) ;
- wynik[x][y] = pixel;
- }
- }
- macierz rezultat;
- for (int j=0; j<ROZMIAR_BLOKU; j++) {
- for (int i=0; i<ROZMIAR_BLOKU; i++) {
- if (wynik[i][j]>255) wynik[i][j]=255;
- if (wynik[i][j]<0) wynik[i][j]=0;
- rezultat.dane[i][j] = wynik[i][j];
- rezultat.dct[i][j] = DCT[i][j];
- }
- }
- return rezultat;
- }
- 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_f)
- ladujBMP("obrazek4.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_z)
- ladujBMP("obrazek5.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_x)
- ladujBMP("obrazek6.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_c)
- ladujBMP("obrazek7.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_v)
- ladujBMP("obrazek8.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