Advertisement
mrlantan

Untitled

Dec 6th, 2020
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. void QuantImage(Image& image, const std::vector<RGB>& ok_pixels) {
  2.     Points points;
  3.     for (auto pixel : ok_pixels) {
  4.         points.push_back({static_cast<double>(pixel.r),
  5.                           static_cast<double>(pixel.g),
  6.                           static_cast<double>(pixel.b)});
  7.     }
  8.  
  9.     KdTree tree(points);
  10.  
  11.     for (int height = 0; height < image.Height(); ++height) {
  12.         for (int width = 0; width < image.Width(); ++width) {
  13.             auto old_color = image.GetPixel(height, width);
  14.             auto index = tree.GetNearest({static_cast<double>(old_color.r),
  15.                                           static_cast<double>(old_color.g),
  16.                                           static_cast<double>(old_color.b)});
  17.             auto new_color = ok_pixels[index];
  18.             RGB quant_error = old_color - new_color;
  19.             image.SetPixel(new_color, height, width);
  20.  
  21.             if (width < image.Width() - 1) {
  22.                 RGB new_color = image.GetPixel(height, width + 1) + (quant_error * (7 / 16.0));
  23.                 new_color.Clamp();
  24.                 image.SetPixel(new_color, height, width + 1);
  25.             }
  26.  
  27.             if (height < image.Height() - 1) {
  28.                 if (width > 0) {
  29.                     RGB new_color = image.GetPixel(height + 1, width - 1) + (quant_error * (3 / 16.0));
  30.                     new_color.Clamp();
  31.                     image.SetPixel(new_color, height + 1, width - 1);
  32.                 }
  33.  
  34.                 RGB new_color = image.GetPixel(height + 1, width) + (quant_error * (5 / 16.0));
  35.                 new_color.Clamp();
  36.                 image.SetPixel(new_color, height + 1, width);
  37.  
  38.                 if (width < image.Width() - 1) {
  39.                     new_color = image.GetPixel(height + 1, width + 1) + (quant_error * (1 / 16.0));
  40.                     new_color.Clamp();
  41.                     image.SetPixel(new_color, height + 1,width + 1);
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement