Advertisement
IvoB1n

Untitled

Feb 12th, 2020
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.18 KB | None | 0 0
  1. /******************************************************************************
  2.  * Laborator 01 - Zaklady pocitacove grafiky - IZG
  3.  * ihulik@fit.vutbr.cz
  4.  *
  5.  * $Id: $
  6.  *
  7.  * Popis: Hlavicky funkci pro funkce studentu
  8.  *
  9.  * Opravy a modifikace:
  10.  * - ibobak@fit.vutbr.cz, orderedDithering
  11.  */
  12.  
  13. #include "student.h"
  14. #include "globals.h"
  15.  
  16. #include <time.h>
  17.  
  18. const int M[] = {
  19.     0, 204, 51, 255,
  20.     68, 136, 187, 119,
  21.     34, 238, 17, 221,
  22.     170, 102, 153, 85
  23. };
  24.  
  25. const int M_SIDE = 4;
  26.  
  27. /******************************************************************************
  28.  ******************************************************************************
  29.  Funkce vraci pixel z pozice x, y. Je nutne hlidat frame_bufferu, pokud
  30.  je dana souradnice mimo hranice, funkce vraci barvu (0, 0, 0).
  31.  Ukol za 0.25 bodu */
  32. S_RGBA getPixel(int x, int y)
  33. {
  34.     int offset = y * width + x;
  35.     S_RGBA color = frame_buffer[offset];
  36.    
  37.     if (x < 0 || x > width || y < 0 || y > height) {
  38.         color = COLOR_BLACK; //vraci barvu (0, 0, 0)
  39.     } else {
  40.         color = frame_buffer[offset];
  41.     }
  42.  
  43.     return color;
  44. }
  45. /******************************************************************************
  46.  ******************************************************************************
  47.  Funkce vlozi pixel na pozici x, y. Je nutne hlidat frame_bufferu, pokud
  48.  je dana souradnice mimo hranice, funkce neprovadi zadnou zmenu.
  49.  Ukol za 0.25 bodu */
  50. void putPixel(int x, int y, S_RGBA color)
  51. {
  52.     // width: x
  53.     // height: y
  54.     int offset = y * width + x;
  55.     if (x < 0 || x > width || y < 0 || y > height) {
  56.         frame_buffer[offset] = COLOR_BLACK; //vraci barvu (0, 0, 0)
  57.     } else {
  58.         frame_buffer[offset] = color;
  59.     }
  60. }
  61. /******************************************************************************
  62.  ******************************************************************************
  63.  Funkce prevadi obrazek na odstiny sedi. Vyuziva funkce GetPixel a PutPixel.
  64.  Ukol za 0.5 bodu */
  65. void grayScale()
  66. {
  67.     for (int y = 0; y < height; ++y) {
  68.         for (int x = 0; x < width; ++x)
  69.         {
  70.             S_RGBA pixel = getPixel(x, y);
  71.  
  72.             int intensity =  0.299 * pixel.red    \
  73.                             + 0.587 * pixel.green \
  74.                             + 0.114 * pixel.blue;
  75.  
  76.             S_RGBA ret_pixel = { intensity, intensity, intensity };
  77.             putPixel(x, y, ret_pixel);
  78.  
  79.             // if (intensity > 127) {
  80.             //  putPixel(x, y, COLOR_BLACK);
  81.             // } else {
  82.             //  putPixel(x, y, COLOR_WHITE);
  83.             // }
  84.         }
  85.     }
  86. }
  87.  
  88. /******************************************************************************
  89.  ******************************************************************************
  90.  Funkce prevadi obrazek na cernobily pomoci algoritmu maticoveho rozptyleni.
  91.  Ukol za 1 bod */
  92.  
  93. void orderedDithering()
  94. {
  95.     grayScale();
  96.        
  97.     for (int y = 0; y < height; ++y) {
  98.         for (int x = 0; x < width; ++x)
  99.         {
  100.             S_RGBA pixel = getPixel(x, y);
  101.  
  102.             int i = x % M_SIDE;
  103.             int j = y % M_SIDE;
  104.            
  105.             if (pixel.red < M[j*M_SIDE+i]) {
  106.                 putPixel(x, y, COLOR_BLACK);
  107.             }
  108.             else
  109.                 putPixel(x, y, COLOR_WHITE);
  110.         }
  111.     }
  112. }
  113.  
  114.  
  115. /******************************************************************************
  116.  ******************************************************************************
  117.  Funkce prevadi obrazek na cernobily pomoci algoritmu distribuce chyby.
  118.  Ukol za 1 bod */
  119. void errorDistribution()
  120. {  
  121.     // todo
  122. }
  123.  
  124. /******************************************************************************
  125.  ******************************************************************************
  126.  Funkce prevadi obrazek na cernobily pomoci metody prahovani.
  127.  Demonstracni funkce */
  128. void thresholding(int Threshold)
  129. {
  130.     /* Prevedeme obrazek na grayscale */
  131.     grayScale();
  132.  
  133.     /* Projdeme vsechny pixely obrazku */
  134.     for (int y = 0; y < height; ++y)
  135.         for (int x = 0; x < width; ++x)
  136.         {
  137.             /* Nacteme soucasnou barvu */
  138.             S_RGBA color = getPixel(x, y);
  139.  
  140.             /* Porovname hodnotu cervene barevne slozky s prahem.
  141.                Muzeme vyuzit jakoukoli slozku (R, G, B), protoze
  142.                obrazek je sedotonovy, takze R=G=B */
  143.             if (color.red > Threshold)
  144.                 putPixel(x, y, COLOR_WHITE);
  145.             else
  146.                 putPixel(x, y, COLOR_BLACK);
  147.         }
  148. }
  149.  
  150. /******************************************************************************
  151.  ******************************************************************************
  152.  Funkce prevadi obrazek na cernobily pomoci nahodneho rozptyleni.
  153.  Vyuziva funkce GetPixel, PutPixel a GrayScale.
  154.  Demonstracni funkce. */
  155. void randomDithering()
  156. {
  157.     /* Prevedeme obrazek na grayscale */
  158.     grayScale();
  159.  
  160.     /* Inicializace generatoru pseudonahodnych cisel */
  161.     srand((unsigned int)time(NULL));
  162.  
  163.     /* Projdeme vsechny pixely obrazku */
  164.     for (int y = 0; y < height; ++y)
  165.         for (int x = 0; x < width; ++x)
  166.         {
  167.             /* Nacteme soucasnou barvu */
  168.             S_RGBA color = getPixel(x, y);
  169.            
  170.             /* Porovname hodnotu cervene barevne slozky s nahodnym prahem.
  171.                Muzeme vyuzit jakoukoli slozku (R, G, B), protoze
  172.                obrazek je sedotonovy, takze R=G=B */
  173.             if (color.red > rand()%255)
  174.             {
  175.                 putPixel(x, y, COLOR_WHITE);
  176.             }
  177.             else
  178.                 putPixel(x, y, COLOR_BLACK);
  179.         }
  180. }
  181. /*****************************************************************************/
  182. /*****************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement