Advertisement
IvoB1n

Untitled

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