Skip to content

Commit 89e08ca

Browse files
committed
Small fixes to image display logic
1 parent bd19f5f commit 89e08ca

File tree

5 files changed

+76
-32
lines changed

5 files changed

+76
-32
lines changed

src/graphics/Image/Image.cpp

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,55 @@
1616
* @authors Soldered
1717
***************************************************/
1818
#include "Inkplate.h"
19+
#ifndef USE_COLOR_IMAGE
1920
#include "Image.h"
2021
#include "TJpeg/TJpg_Decoder.h"
2122
#include "pgmspace.h"
2223

24+
2325
Image *_imagePtrJpeg = nullptr;
2426
Image *_imagePtrPng = nullptr;
2527

26-
void Image::beginImage(Inkplate *inkplateptr)
28+
uint8_t (*Image::ditherBuffer)[E_INK_WIDTH + 20] = nullptr;
29+
uint8_t (*Image::jpegDitherBuffer)[18] = nullptr;
30+
uint8_t *Image::pixelBuffer = nullptr;
31+
uint32_t *Image::ditherPalette = nullptr;
32+
uint8_t *Image::palette = nullptr;
33+
34+
void Image::begin(Inkplate *inkplateptr)
2735
{
2836
_inkplate = inkplateptr;
2937
_imagePtrJpeg = this;
3038
_imagePtrPng = this;
39+
40+
41+
jpegDitherBuffer = (uint8_t (*)[18])heap_caps_calloc(18, 18, MALLOC_CAP_SPIRAM);
42+
43+
44+
ditherBuffer = (uint8_t (*)[E_INK_WIDTH + 20])heap_caps_calloc(2, (E_INK_WIDTH + 20), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
45+
46+
47+
pixelBuffer = (uint8_t *)heap_caps_calloc(1, (E_INK_WIDTH * 4 + 5), MALLOC_CAP_SPIRAM);
48+
49+
50+
ditherPalette = (uint32_t *)heap_caps_calloc(256, sizeof(uint32_t), MALLOC_CAP_SPIRAM);
51+
52+
53+
palette = (uint8_t *)heap_caps_calloc(128, sizeof(uint8_t), MALLOC_CAP_SPIRAM);
54+
55+
56+
if (!jpegDitherBuffer || !ditherBuffer || !pixelBuffer || !ditherPalette || !palette)
57+
{
58+
Serial.println(" Failed to allocate one or more buffers (SRAM/PSRAM)");
59+
}
60+
else
61+
{
62+
Serial.println(" Buffers allocated successfully in PSRAM");
63+
}
64+
65+
66+
67+
3168
}
3269

3370
/**
@@ -46,9 +83,9 @@ void Image::beginImage(Inkplate *inkplateptr)
4683
*
4784
* @return 1 if succesfuly drawn, 0 if not
4885
*/
49-
bool Image::drawImage(const String path, int x, int y, bool dither, bool invert)
86+
bool Image::draw(const String path, int x, int y, bool dither, bool invert)
5087
{
51-
return drawImage(path.c_str(), x, y, dither, invert);
88+
return draw(path.c_str(), x, y, dither, invert);
5289
};
5390

5491
/**
@@ -67,7 +104,7 @@ bool Image::drawImage(const String path, int x, int y, bool dither, bool invert)
67104
*
68105
* @return 1 if succesfuly drawn, 0 if not
69106
*/
70-
bool Image::drawImage(const char *path, int x, int y, bool dither, bool invert)
107+
bool Image::draw(const char *path, int x, int y, bool dither, bool invert)
71108
{
72109
// Try to get the file extension.
73110
char _fileExtension[5];
@@ -115,7 +152,7 @@ bool Image::drawImage(const char *path, int x, int y, bool dither, bool invert)
115152
*
116153
* @return 1 if succesfuly drawn, 0 if not
117154
*/
118-
bool Image::drawImage(const uint8_t *buf, int x, int y, int16_t w, int16_t h, uint8_t c, uint8_t bg)
155+
bool Image::draw(const uint8_t *buf, int x, int y, int16_t w, int16_t h, uint8_t c, uint8_t bg)
119156
{
120157
if (_inkplate->getDisplayMode() == INKPLATE_1BIT && bg == 0xFF)
121158
_inkplate->drawBitmap(x, y, buf, w, h, c);
@@ -144,10 +181,10 @@ bool Image::drawImage(const uint8_t *buf, int x, int y, int16_t w, int16_t h, ui
144181
*
145182
* @return 1 if succesfuly drawn, 0 if not
146183
*/
147-
bool Image::drawImage(const String path, const Format &format, const int x, const int y, const bool dither,
184+
bool Image::draw(const String path, const Format &format, const int x, const int y, const bool dither,
148185
const bool invert)
149186
{
150-
return drawImage(path.c_str(), format, x, y, dither, invert);
187+
return draw(path.c_str(), format, x, y, dither, invert);
151188
};
152189

153190
/**
@@ -168,7 +205,7 @@ bool Image::drawImage(const String path, const Format &format, const int x, cons
168205
*
169206
* @return 1 if succesfuly drawn, 0 if not
170207
*/
171-
bool Image::drawImage(const char *path, const Format &format, const int x, const int y, const bool dither,
208+
bool Image::draw(const char *path, const Format &format, const int x, const int y, const bool dither,
172209
const bool invert)
173210
{
174211
if (strncmp(path, "http://", 7) == 0 || strncmp(path, "https://", 8) == 0)
@@ -209,7 +246,7 @@ bool Image::drawImage(const char *path, const Format &format, const int x, const
209246
*
210247
* @return 1 if succesfuly drawn, 0 if not
211248
*/
212-
bool Image::drawImage(const char *path, const Format &format, const Position &position, const bool dither,
249+
bool Image::draw(const char *path, const Format &format, const Position &position, const bool dither,
213250
const bool invert)
214251
{
215252
if (strncmp(path, "http://", 7) == 0 || strncmp(path, "https://", 8) == 0)
@@ -374,3 +411,5 @@ void Image::getPointsForPosition(const Position &position, const uint16_t imageW
374411
break;
375412
}
376413
}
414+
415+
#endif

src/graphics/Image/ImageDither.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* @authors Soldered
1717
***************************************************/
1818
#include "Inkplate.h"
19+
#ifndef USE_COLOR_IMAGE
1920
#include "Image.h"
2021

2122

@@ -143,3 +144,4 @@ void Image::ditherSwapBlockJpeg(int x)
143144

144145
jpegDitherBuffer[17][1] = 0;
145146
}
147+
#endif

src/graphics/Image/bmpHelpers/ImageBMP.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
***************************************************/
1818
#include "Inkplate.h"
1919
#include "../Image.h"
20-
20+
#ifndef USE_COLOR_IMAGE
2121

2222
/**
2323
* @brief legalBmp function checks file header for BMP image signature
@@ -517,4 +517,5 @@ bool Image::drawBmpFromSdAtPosition(const char *fileName, const Position &positi
517517
}
518518

519519
return 1;
520-
}
520+
}
521+
#endif

src/graphics/Image/jpegHelpers/ImageJPEG.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
***************************************************/
1818
#include "../../../system/defines.h"
1919
#include "Inkplate.h"
20+
#ifndef USE_COLOR_IMAGE
2021
#include "../Image.h"
21-
#include "../TJpeg/TJpg_Decoder.h"
22+
#include "../../TJpeg/TJpg_Decoder.h"
2223

2324

2425
extern Image *_imagePtrJpeg;
25-
2626
/**
2727
* @brief drawJpegFromSd function draws jpeg image from sd file
2828
*
@@ -369,45 +369,44 @@ bool Image::drawJpegFromBuffer(uint8_t *buff, int32_t len, int x, int y, bool di
369369
* @param int16_t invert
370370
* 1 if using invert, 0 if not
371371
*/
372-
bool Image::drawJpegChunk(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap, bool dither, bool invert)
372+
bool Image::drawJpegChunk(int16_t x, int16_t y, uint16_t w, uint16_t h,
373+
uint16_t *bitmap, bool dither, bool invert)
373374
{
374375
if (!_imagePtrJpeg)
375-
return 0;
376+
return false;
376377

378+
// Carry global error from previous scanline
377379
if (dither && y != _imagePtrJpeg->lastY)
378380
{
379381
_imagePtrJpeg->ditherSwap(E_INK_WIDTH);
380382
_imagePtrJpeg->lastY = y;
381383
}
382384

385+
// --- Draw the JPEG MCU block ---
383386
for (int j = 0; j < h; ++j)
384387
{
385388
for (int i = 0; i < w; ++i)
386389
{
387390
uint32_t rgb = bitmap[j * w + i];
388-
uint32_t val;
389-
390391
uint8_t r = _RED(rgb), g = _GREEN(rgb), b = _BLUE(rgb);
391-
if (dither)
392-
{
393-
val = _imagePtrJpeg->ditherGetPixelJpeg(RGB8BIT(r, g, b), i, j, x, y, w, h);
394-
}
395-
else
396-
{
397-
val = RGB3BIT(r, g, b);
398-
}
392+
uint32_t val = dither
393+
? _imagePtrJpeg->ditherGetPixelJpeg(RGB8BIT(r, g, b), i, j, x, y, w, h)
394+
: RGB3BIT(r, g, b);
399395

400396
if (invert)
401397
val = 7 - val;
402-
// if (_imagePtrJpeg->getDisplayMode() == INKPLATE_1BIT)
403-
// val = (~val >> 2) & 1;
404398

405399
_imagePtrJpeg->_inkplate->drawPixel(x + i, y + j, val);
406400
}
407401
}
408402

409403
if (dither)
404+
{
405+
// Carry the bottom error row to the global buffer
410406
_imagePtrJpeg->ditherSwapBlockJpeg(x);
411407

412-
return 1;
413-
}
408+
}
409+
410+
return true;
411+
}
412+
#endif

src/graphics/Image/pngHelpers/ImagePNG.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*
1616
* @authors Soldered.com
1717
***************************************************/
18+
1819
#include "../../../system/defines.h"
1920
#include "Inkplate.h"
21+
#ifndef USE_COLOR_IMAGE
2022
#include "../Image.h"
21-
#include "../pngle/pngle.h"
23+
#include "../../pngle/pngle.h"
2224

2325

2426
extern Image *_imagePtrPng;
@@ -46,7 +48,7 @@ static Image::Position _pngPosition = Image::_npos;
4648
* @param uint8_t rgba[4]
4749
* color
4850
*/
49-
void pngle_on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4])
51+
void pngle_on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, const uint8_t rgba[4])
5052
{
5153
if (_pngPosition != Image::_npos)
5254
{
@@ -395,4 +397,5 @@ bool Image::drawPngFromSdAtPosition(const char *fileName, const Position &positi
395397
_pngPosition = _npos;
396398

397399
return ret;
398-
}
400+
}
401+
#endif

0 commit comments

Comments
 (0)