Skip to content

Commit 8a591cc

Browse files
committed
Small changes to color dithering algorithm
1 parent 99bb7b1 commit 8a591cc

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/graphics/ImageColor/ImageDitherColor.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ static uint32_t pallete[] = {0xFFFFFF, 0x0000000, 0xFF0000};
3939
static unsigned int width = E_INK_WIDTH, height = E_INK_HEIGHT;
4040

4141
#elif defined(ARDUINO_ESP32S3_DEV)
42-
static uint32_t pallete[] = { 0x424852, 0xA1A8A8, 0xB0AB44, 0x7D4749, 0x4B689A, 0x516A64};
43-
//static uint32_t pallete[] = { 0x000000, 0xFFFFFF, 0xFFFF00, 0xFF0000, 0x0000FF, 0x00FF00};
42+
//static uint32_t pallete[] = { 0x424852, 0xA1A8A8, 0xB0AB44, 0x7D4749, 0x4B689A, 0x516A64};
43+
static uint32_t pallete[] = { 0x000000, 0xFFFFFF, 0xFFFF00, 0xFF0000, 0x0000FF, 0x00FF00};
4444
static unsigned int width = E_INK_WIDTH, height = E_INK_HEIGHT;
4545

4646
#endif
@@ -117,15 +117,17 @@ uint8_t ImageColor::ditherGetPixelBmp(uint32_t px, int i, int j, int w, bool pal
117117
int16_t g = GREEN8(px) + ditherBuffer[1][j % 8][i];
118118
int16_t b = BLUE8(px) + ditherBuffer[2][j % 8][i];
119119

120-
ditherBuffer[0][j % 8][i] = 0;
121-
ditherBuffer[1][j % 8][i] = 0;
122-
ditherBuffer[2][j % 8][i] = 0;
120+
if (i == w - 1) {
121+
int row = j % 8;
122+
memset(ditherBuffer[0][row], 0, w * sizeof(int16_t));
123+
memset(ditherBuffer[1][row], 0, w * sizeof(int16_t));
124+
memset(ditherBuffer[2][row], 0, w * sizeof(int16_t));
125+
}
123126

124127
r = max((int16_t)0, min((int16_t)255, r));
125128
g = max((int16_t)0, min((int16_t)255, g));
126129
b = max((int16_t)0, min((int16_t)255, b));
127130

128-
129131
int closest = findClosestPalette(r, g, b);
130132

131133
int32_t rErr = r - (int32_t)((pallete[closest] >> 16) & 0xFF);

src/graphics/ImageColor/jpegHelpers/ImageJPEG.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,13 @@ bool ImageColor::drawJpegChunk(int16_t x, int16_t y, uint16_t w, uint16_t h, uin
377377

378378
// Carry global error from previous scanline
379379
if (dither && y != _imagePtrJpeg->lastY)
380-
{
381-
_imagePtrJpeg->lastY = y;
382-
}
380+
{
381+
// We moved to a new scanline
382+
int oldRow = _imagePtrJpeg->lastY & (18 - 1);
383+
384+
_imagePtrJpeg->lastY = y;
385+
}
386+
383387

384388
// --- Draw the JPEG MCU block ---
385389
for (int j = 0; j < h; ++j)
@@ -399,7 +403,7 @@ bool ImageColor::drawJpegChunk(int16_t x, int16_t y, uint16_t w, uint16_t h, uin
399403
if (dither)
400404
{
401405
val = _imagePtrJpeg->ditherGetPixelBmp(((uint32_t)r << 16) | ((uint32_t)g << 8) | ((uint32_t)b), i + x,
402-
j + y, E_INK_WIDTH, 0);
406+
j + y, E_INK_HEIGHT, 0); // Changed from E_INK_WIDTH
403407
}
404408
else
405409
{

src/graphics/ImageColor/pngHelpers/ImagePNG.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,16 @@ void pngle_on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t
7777
g = 255 - g;
7878
b = 255 - b;
7979
}
80-
81-
uint8_t px = _imagePtrPng->findClosestPalette(r, g, b);
80+
uint8_t px;
8281

8382
if (_pngDither)
8483
{
85-
px = _imagePtrPng->ditherGetPixelBmp((r << 16) | (g << 8) | (b), x + i, y + j, E_INK_WIDTH, 0);
84+
px = _imagePtrPng->ditherGetPixelBmp((r << 16) | (g << 8) | (b), x + i, y + j, pngle_get_width(pngle), 0); // Changed from e_ink_width
85+
}
86+
else
87+
{
88+
px = _imagePtrPng->findClosestPalette(r, g, b);
89+
8690
}
8791
_imagePtrPng->_inkplate->drawPixel(_pngX + x + i, _pngY + y + j, px);
8892
}

0 commit comments

Comments
 (0)