Skip to content

Commit ce43aaa

Browse files
committed
Fix dither artifacting errors in certain image formats
1 parent 194b119 commit ce43aaa

File tree

5 files changed

+26
-47
lines changed

5 files changed

+26
-47
lines changed

src/graphics/ImageColor/ImageColor.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@
2525
ImageColor *_imagePtrJpeg = nullptr;
2626
ImageColor *_imagePtrPng = nullptr;
2727

28-
// uint8_t (*ImageColor::ditherBuffer)[E_INK_WIDTH + 20] = nullptr;
28+
#if defined(ARDUINO_INKPLATE2) || defined(ARDUINO_ESP32S3_DEV)
2929
__attribute__((section(".ext_ram.bss"))) int16_t ImageColor::ditherBuffer[3][8][E_INK_HEIGHT];
30+
# else
31+
__attribute__((section(".ext_ram.bss"))) int16_t ImageColor::ditherBuffer[3][8][E_INK_WIDTH+200];
32+
#endif
3033

31-
// int16_t (*ImageColor::ditherBuffer)[8][E_INK_WIDTH + 20] = nullptr;
32-
// uint8_t *ImageColor::pixelBuffer = nullptr;
33-
// uint32_t *ImageColor::ditherPalette = nullptr;
34-
// uint8_t *ImageColor::palette = nullptr;
3534

3635
void ImageColor::begin(Inkplate *inkplateptr)
3736
{

src/graphics/ImageColor/ImageColor.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,21 @@ class ImageColor
110110
void getPointsForPosition(const Position &position, const uint16_t imageWidth, const uint16_t imageHeight,
111111
const uint16_t screenWidth, const uint16_t screenHeight, uint16_t *posX, uint16_t *posY);
112112
uint8_t findClosestPalette(int16_t r, int16_t g, int16_t b);
113-
114-
115-
private:
113+
116114
#if defined(ARDUINO_INKPLATE2) || defined(ARDUINO_ESP32S3_DEV)
117115
uint8_t pixelBuffer[E_INK_HEIGHT * 4 + 5];
116+
static int16_t ditherBuffer[3][8][E_INK_HEIGHT];
117+
static const unsigned int width = E_INK_HEIGHT, height = E_INK_WIDTH;
118118
#else
119119
uint8_t pixelBuffer[E_INK_WIDTH * 4 + 5];
120+
static int16_t ditherBuffer[3][8][E_INK_WIDTH + 200];
121+
static const unsigned int width = E_INK_WIDTH, height = E_INK_HEIGHT;
120122
#endif
121-
static uint8_t (*jpegDitherBuffer)[18];
122-
// static int16_t (*ditherBuffer)[8][E_INK_WIDTH + 20];
123-
static int16_t ditherBuffer[3][8][E_INK_WIDTH + 20];
124123

124+
125+
private:
126+
127+
125128
uint32_t ditherPalette[256]; // 8 bit colors, in color, 3x8 bit colors
126129
uint8_t palette[128]; // 2 3 bit colors per byte, _###_###
127130

src/graphics/ImageColor/ImageDitherColor.cpp

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,6 @@
2222

2323
extern ImageColor *_imagePtrJpeg;
2424

25-
26-
#if defined(ARDUINO_INKPLATECOLOR)
27-
28-
static uint32_t pallete[] = {0x000000, 0xFFFFFF, 0x00FF00, 0x0000FF, 0xFF0000, 0xFFFF00, 0xFF8000};
29-
static unsigned int width = E_INK_WIDTH, height = E_INK_HEIGHT;
30-
31-
#elif defined(ARDUINO_INKPLATE2)
32-
33-
static uint32_t pallete[] = {0xFFFFFF, 0x000000, 0xFF0000};
34-
static unsigned int width = E_INK_HEIGHT, height = E_INK_WIDTH;
35-
36-
#elif defined(ARDUINO_INKPLATE4) || defined(ARDUINO_INKPLATE7)
37-
38-
static uint32_t pallete[] = {0xFFFFFF, 0x0000000, 0xFF0000};
39-
static unsigned int width = E_INK_WIDTH, height = E_INK_HEIGHT;
40-
41-
#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};
44-
static unsigned int width = E_INK_WIDTH, height = E_INK_HEIGHT;
45-
46-
#endif
47-
4825
/**
4926
* @brief findClosestPalette return closes pallete for given pixel
5027
*
@@ -117,12 +94,9 @@ uint8_t ImageColor::ditherGetPixelBmp(uint32_t px, int i, int j, int w, bool pal
11794
int16_t g = GREEN8(px) + ditherBuffer[1][j % 8][i];
11895
int16_t b = BLUE8(px) + ditherBuffer[2][j % 8][i];
11996

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-
}
97+
ditherBuffer[0][j % 8][i] = 0;
98+
ditherBuffer[1][j % 8][i] = 0;
99+
ditherBuffer[2][j % 8][i] = 0;
126100

127101
r = max((int16_t)0, min((int16_t)255, r));
128102
g = max((int16_t)0, min((int16_t)255, g));

src/graphics/ImageColor/jpegHelpers/ImageJPEG.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,12 @@ 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-
// We moved to a new scanline
382-
int oldRow = _imagePtrJpeg->lastY & (18 - 1);
380+
{
383381

384-
_imagePtrJpeg->lastY = y;
385-
}
382+
_imagePtrJpeg->lastY = y;
383+
}
384+
385+
unsigned int width = _imagePtrJpeg->width;
386386

387387

388388
// --- Draw the JPEG MCU block ---
@@ -403,7 +403,7 @@ bool ImageColor::drawJpegChunk(int16_t x, int16_t y, uint16_t w, uint16_t h, uin
403403
if (dither)
404404
{
405405
val = _imagePtrJpeg->ditherGetPixelBmp(((uint32_t)r << 16) | ((uint32_t)g << 8) | ((uint32_t)b), i + x,
406-
j + y, E_INK_HEIGHT, 0); // Changed from E_INK_WIDTH
406+
j + y, width, 0);
407407
}
408408
else
409409
{

src/graphics/ImageColor/pngHelpers/ImagePNG.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ void pngle_on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t
5858
_pngPosition = ImageColor::_npos;
5959
}
6060
if (rgba[3])
61+
{
62+
unsigned int width = _imagePtrPng->width;
6163
for (int j = 0; j < h; ++j)
6264
for (int i = 0; i < w; ++i)
6365
{
@@ -81,7 +83,7 @@ void pngle_on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t
8183

8284
if (_pngDither)
8385
{
84-
px = _imagePtrPng->ditherGetPixelBmp((r << 16) | (g << 8) | (b), x + i, y + j, pngle_get_width(pngle), 0); // Changed from e_ink_width
86+
px = _imagePtrPng->ditherGetPixelBmp((r << 16) | (g << 8) | (b), x + i, y + j, width, 0); // Changed from e_ink_width
8587
}
8688
else
8789
{
@@ -90,6 +92,7 @@ void pngle_on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t
9092
}
9193
_imagePtrPng->_inkplate->drawPixel(_pngX + x + i, _pngY + y + j, px);
9294
}
95+
}
9396

9497
if (lastY != y)
9598
{

0 commit comments

Comments
 (0)