Advertisement
mrlantan

Untitled

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