Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void QuantImage(Image& image, const std::vector<RGB>& ok_pixels) {
- Points points;
- for (auto pixel : ok_pixels) {
- points.push_back({static_cast<double>(pixel.r / 255.0),
- static_cast<double>(pixel.g / 255.0),
- static_cast<double>(pixel.b / 255.0)});
- }
- KdTree tree(points);
- std::vector<std::vector<RGB>> double_image(image.Height(), std::vector<RGB>(image.Width()));
- for (int height = 0; height < image.Height(); ++height) {
- for (int width = 0; width < image.Width(); ++width) {
- double_image[height][width] = image.GetPixel(height, width) * (1 / 255.0);
- }
- }
- for (int height = 0; height < image.Height(); ++height) {
- for (int width = 0; width < image.Width(); ++width) {
- auto old_color = double_image[height][width];
- auto index = tree.GetNearest({static_cast<double>(old_color.r),
- static_cast<double>(old_color.g),
- static_cast<double>(old_color.b)});
- auto new_color = ok_pixels[index] * (1 / 255.0);
- RGB quant_error = old_color - new_color;
- double_image[height][width] = new_color;
- image.SetPixel(double_image[height][width] * 255.0, height, width);
- if (width < image.Width() - 1) {
- double_image[height][width + 1] = double_image[height][width + 1] + quant_error * (7.0 / 16.0);
- }
- if (height < image.Height() - 1) {
- if (width > 0) {
- double_image[height + 1][width - 1] = double_image[height + 1][width - 1] + quant_error * (3 / 16);
- }
- double_image[height + 1][width] = double_image[height + 1][width] + quant_error * (5 / 16);
- if (width < image.Width() - 1) {
- double_image[height + 1][width + 1] = double_image[height + 1][width + 1] + quant_error * (1 / 16);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement