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),
- static_cast<double>(pixel.g),
- static_cast<double>(pixel.b)});
- }
- KdTree tree(points);
- for (int height = 0; height < image.Height(); ++height) {
- for (int width = 0; width < image.Width(); ++width) {
- auto old_color = image.GetPixel(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];
- RGB quant_error = old_color - new_color;
- image.SetPixel(new_color, height, width);
- if (width < image.Width() - 1) {
- RGB new_color = image.GetPixel(height, width + 1) + (quant_error * (7 / 16.0));
- new_color.Clamp();
- image.SetPixel(new_color, height, width + 1);
- }
- if (height < image.Height() - 1) {
- if (width > 0) {
- RGB new_color = image.GetPixel(height + 1, width - 1) + (quant_error * (3 / 16.0));
- new_color.Clamp();
- image.SetPixel(new_color, height + 1, width - 1);
- }
- RGB new_color = image.GetPixel(height + 1, width) + (quant_error * (5 / 16.0));
- new_color.Clamp();
- image.SetPixel(new_color, height + 1, width);
- if (width < image.Width() - 1) {
- new_color = image.GetPixel(height + 1, width + 1) + (quant_error * (1 / 16.0));
- new_color.Clamp();
- image.SetPixel(new_color, height + 1,width + 1);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement