From 87fc82a978cee6ad999b8cd9a5126889f2ba4fd8 Mon Sep 17 00:00:00 2001 From: David Thomas Date: Fri, 13 Jun 2025 16:53:42 +0100 Subject: [PATCH] Flip dither matrix when zig-zagging --- src/render/error_diffusion.cpp | 7 ++++--- src/render/error_diffusion.h | 3 ++- src/render/ordered_dither.cpp | 4 ++-- src/render/ordered_dither.h | 3 ++- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/render/error_diffusion.cpp b/src/render/error_diffusion.cpp index f4fe13018..c3fe905ba 100644 --- a/src/render/error_diffusion.cpp +++ b/src/render/error_diffusion.cpp @@ -158,7 +158,8 @@ void ErrorDiffusionDither::finish() doc::color_t ErrorDiffusionDither::ditherRgbToIndex2D(const int x, const int y, const doc::RgbMap* rgbmap, - const doc::Palette* palette) + const doc::Palette* palette, + const int direction) { const ErrorDiffusionMatrix& matrix = getCurrentMatrix(); @@ -214,11 +215,11 @@ doc::color_t ErrorDiffusionDither::ditherRgbToIndex2D(const int x, for (int my = 0; my < matrix.height; ++my) { // Use circular buffer indexing - int bufferRow = (m_currentRowOffset + my) % matrix.height; // hoist + int bufferRow = (m_currentRowOffset + my) % matrix.height; int bufferRowIndex = bufferRow * m_width; for (int mx = 0; mx < matrix.width; ++mx) { - const int coeff = matrix.coefficients[my][mx]; + const int coeff = direction > 0 ? matrix.coefficients[my][mx] : matrix.coefficients[my][matrix.width - 1 - mx]; if (coeff == 0) continue; diff --git a/src/render/error_diffusion.h b/src/render/error_diffusion.h index bbe5b1369..1d4d8db79 100644 --- a/src/render/error_diffusion.h +++ b/src/render/error_diffusion.h @@ -57,7 +57,8 @@ public: doc::color_t ditherRgbToIndex2D(const int x, const int y, const doc::RgbMap* rgbmap, - const doc::Palette* palette) override; + const doc::Palette* palette, + const int direction) override; private: const ErrorDiffusionMatrix& getCurrentMatrix() const; diff --git a/src/render/ordered_dither.cpp b/src/render/ordered_dither.cpp index b27b41492..400200147 100644 --- a/src/render/ordered_dither.cpp +++ b/src/render/ordered_dither.cpp @@ -261,7 +261,7 @@ void dither_rgb_image_to_indexed(DitheringAlgorithmBase& algorithm, dstIt += w - 1; for (int x = w - 1; x >= 0; --x, --dstIt) { ASSERT(dstIt == doc::get_pixel_address_fast(dstImage, x, y)); - *dstIt = algorithm.ditherRgbToIndex2D(x, y, rgbmap, palette); + *dstIt = algorithm.ditherRgbToIndex2D(x, y, rgbmap, palette, -1); if (delegate) { if (!delegate->continueTask()) return; @@ -272,7 +272,7 @@ void dither_rgb_image_to_indexed(DitheringAlgorithmBase& algorithm, else { // Even row: go from left-to-right for (int x = 0; x < w; ++x, ++dstIt) { ASSERT(dstIt == doc::get_pixel_address_fast(dstImage, x, y)); - *dstIt = algorithm.ditherRgbToIndex2D(x, y, rgbmap, palette); + *dstIt = algorithm.ditherRgbToIndex2D(x, y, rgbmap, palette, +1); if (delegate) { if (!delegate->continueTask()) diff --git a/src/render/ordered_dither.h b/src/render/ordered_dither.h index 849155528..7cb8dba3a 100644 --- a/src/render/ordered_dither.h +++ b/src/render/ordered_dither.h @@ -45,7 +45,8 @@ public: virtual doc::color_t ditherRgbToIndex2D(const int x, const int y, const doc::RgbMap* rgbmap, - const doc::Palette* palette) + const doc::Palette* palette, + const int direction) { return 0; }