Flip dither matrix when zig-zagging

This commit is contained in:
David Thomas 2025-06-13 16:53:42 +01:00
parent b43e3e6ac1
commit 87fc82a978
No known key found for this signature in database
GPG Key ID: 553E822E460EE293
4 changed files with 10 additions and 7 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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<doc::IndexedTraits>(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<doc::IndexedTraits>(dstImage, x, y));
*dstIt = algorithm.ditherRgbToIndex2D(x, y, rgbmap, palette);
*dstIt = algorithm.ditherRgbToIndex2D(x, y, rgbmap, palette, +1);
if (delegate) {
if (!delegate->continueTask())

View File

@ -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;
}