#ifndef DITHERING_UTILITY_HPP #define DITHERING_UTILITY_HPP #include #include #include #include namespace utility { inline int twoToOne(int x, int y, int width, int height) { while (x < 0) { x += width; } while (y < 0) { y += height; } x = x % width; y = y % height; return x + y * width; } inline std::pair oneToTwo(int i, int width) { return {i % width, i / width}; } inline float dist(int a, int b, int width) { auto axy = utility::oneToTwo(a, width); auto bxy = utility::oneToTwo(b, width); float dx = axy.first - bxy.first; float dy = axy.second - bxy.second; return std::sqrt(dx * dx + dy * dy); } class Cleanup { public: Cleanup(); Cleanup(std::function fn, void *ptr); ~Cleanup(); // allow move Cleanup(Cleanup &&); Cleanup &operator=(Cleanup &&); // deny copy Cleanup(const Cleanup &) = delete; Cleanup &operator=(const Cleanup &) = delete; private: std::optional> fn; void *ptr; }; } // namespace utility #endif