#pragma once
#include "image.hpp"
template<uint count, uint max_>
float posterize(float x) {
  static_assert(count != 0);
  return int(x * (float(count) / float(max_))) * (float(max_) / float(count));
}
template <uint color_num, uint mtx_size, const float mtx[]>
void Image::make_dither_bayer() {
  for (int y = 0; y < Y; y++)
  for (int x = 0; x < X; x++) {
    const uint index = (y % mtx_size) * mtx_size + (x % mtx_size);
    nauto col = *fast_get_ptr(x, y);
    bool is_red = col.is_red();
    float new_col = col.val;
    if (is_red) {
      new_col -= float(Color::GRAY_COUNT);
      new_col += (float(Color::RED_COUNT) / float(color_num)) * mtx[index];
      new_col = posterize<color_num, Color::RED_COUNT>(new_col);
      new_col += float(Color::GRAY_COUNT);
      new_col = std::clamp(new_col, float(Color::RED_START), float(Color::RED));
    } else {
      new_col += (float(Color::GRAY_COUNT) / float(color_num)) * mtx[index];
      new_col = posterize<color_num, Color::GRAY_COUNT>(new_col);
      new_col = std::clamp(new_col, 0.f, float(Color::GRAY_COUNT));
    }
    col.val = new_col;
  }
}