Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- * Laborator 01 - Zaklady pocitacove grafiky - IZG
- * ihulik@fit.vutbr.cz
- *
- * $Id: $
- *
- * Popis: Hlavicky funkci pro funkce studentu
- *
- * Opravy a modifikace:
- * - ibobak@fit.vutbr.cz, orderedDithering
- */
- #include "student.h"
- #include "globals.h"
- #include <time.h>
- const int M[] = {
- 0, 204, 51, 255,
- 68, 136, 187, 119,
- 34, 238, 17, 221,
- 170, 102, 153, 85
- };
- const int M_SIDE = 4;
- /******************************************************************************
- ******************************************************************************
- Funkce vraci pixel z pozice x, y. Je nutne hlidat frame_bufferu, pokud
- je dana souradnice mimo hranice, funkce vraci barvu (0, 0, 0).
- Ukol za 0.25 bodu */
- S_RGBA getPixel(int x, int y)
- {
- int offset = y * width + x;
- S_RGBA color = frame_buffer[offset];
- if (x < 0 || x > width || y < 0 || y > height) {
- color = COLOR_BLACK; //vraci barvu (0, 0, 0)
- } else {
- color = frame_buffer[offset];
- }
- return color;
- }
- /******************************************************************************
- ******************************************************************************
- Funkce vlozi pixel na pozici x, y. Je nutne hlidat frame_bufferu, pokud
- je dana souradnice mimo hranice, funkce neprovadi zadnou zmenu.
- Ukol za 0.25 bodu */
- void putPixel(int x, int y, S_RGBA color)
- {
- // width: x
- // height: y
- int offset = y * width + x;
- if (x < 0 || x > width || y < 0 || y > height) {
- frame_buffer[offset] = COLOR_BLACK; //vraci barvu (0, 0, 0)
- } else {
- frame_buffer[offset] = color;
- }
- }
- /******************************************************************************
- ******************************************************************************
- Funkce prevadi obrazek na odstiny sedi. Vyuziva funkce GetPixel a PutPixel.
- Ukol za 0.5 bodu */
- void grayScale()
- {
- for (int y = 0; y < height; ++y) {
- for (int x = 0; x < width; ++x)
- {
- S_RGBA pixel = getPixel(x, y);
- int intensity = 0.299 * pixel.red \
- + 0.587 * pixel.green \
- + 0.114 * pixel.blue;
- S_RGBA ret_pixel = { intensity, intensity, intensity };
- putPixel(x, y, ret_pixel);
- // if (intensity > 127) {
- // putPixel(x, y, COLOR_BLACK);
- // } else {
- // putPixel(x, y, COLOR_WHITE);
- // }
- }
- }
- }
- /******************************************************************************
- ******************************************************************************
- Funkce prevadi obrazek na cernobily pomoci algoritmu maticoveho rozptyleni.
- Ukol za 1 bod */
- void orderedDithering()
- {
- grayScale();
- for (int y = 0; y < height; ++y) {
- for (int x = 0; x < width; ++x)
- {
- S_RGBA pixel = getPixel(x, y);
- int i = x % M_SIDE;
- int j = y % M_SIDE;
- if (pixel.red < M[j*M_SIDE+i]) {
- putPixel(x, y, COLOR_BLACK);
- }
- else
- putPixel(x, y, COLOR_WHITE);
- }
- }
- }
- /******************************************************************************
- ******************************************************************************
- Funkce prevadi obrazek na cernobily pomoci algoritmu distribuce chyby.
- Ukol za 1 bod */
- void errorDistribution()
- {
- // todo
- }
- /******************************************************************************
- ******************************************************************************
- Funkce prevadi obrazek na cernobily pomoci metody prahovani.
- Demonstracni funkce */
- void thresholding(int Threshold)
- {
- /* Prevedeme obrazek na grayscale */
- grayScale();
- /* Projdeme vsechny pixely obrazku */
- for (int y = 0; y < height; ++y)
- for (int x = 0; x < width; ++x)
- {
- /* Nacteme soucasnou barvu */
- S_RGBA color = getPixel(x, y);
- /* Porovname hodnotu cervene barevne slozky s prahem.
- Muzeme vyuzit jakoukoli slozku (R, G, B), protoze
- obrazek je sedotonovy, takze R=G=B */
- if (color.red > Threshold)
- putPixel(x, y, COLOR_WHITE);
- else
- putPixel(x, y, COLOR_BLACK);
- }
- }
- /******************************************************************************
- ******************************************************************************
- Funkce prevadi obrazek na cernobily pomoci nahodneho rozptyleni.
- Vyuziva funkce GetPixel, PutPixel a GrayScale.
- Demonstracni funkce. */
- void randomDithering()
- {
- /* Prevedeme obrazek na grayscale */
- grayScale();
- /* Inicializace generatoru pseudonahodnych cisel */
- srand((unsigned int)time(NULL));
- /* Projdeme vsechny pixely obrazku */
- for (int y = 0; y < height; ++y)
- for (int x = 0; x < width; ++x)
- {
- /* Nacteme soucasnou barvu */
- S_RGBA color = getPixel(x, y);
- /* Porovname hodnotu cervene barevne slozky s nahodnym prahem.
- Muzeme vyuzit jakoukoli slozku (R, G, B), protoze
- obrazek je sedotonovy, takze R=G=B */
- if (color.red > rand()%255)
- {
- putPixel(x, y, COLOR_WHITE);
- }
- else
- putPixel(x, y, COLOR_BLACK);
- }
- }
- /*****************************************************************************/
- /*****************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement