-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageScaler.cpp
More file actions
263 lines (225 loc) · 7.66 KB
/
ImageScaler.cpp
File metadata and controls
263 lines (225 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* @file ImageScaler.cpp
* @brief JPEG image scaling implementation using TJpgDec.
*
* Bypasses the Inkplate SDK's hardcoded setJpgScale(1) by calling
* TJpgDec directly with a custom callback that replicates the SDK's
* Floyd-Steinberg dithering and grayscale conversion.
*/
#include "ImageScaler.h"
extern SDHandler sdHandler;
// Static member definitions
Inkplate *ImageScaler::s_display = nullptr;
int16_t ImageScaler::s_lastY = -1;
int16_t ImageScaler::s_blockW = -1;
int16_t ImageScaler::s_blockH = -1;
uint8_t ImageScaler::s_ditherBuffer[2][E_INK_WIDTH + 20] = {};
uint8_t ImageScaler::s_jpegDitherBuffer[18][18] = {};
ImageScaler::ImageScaler(Inkplate *display)
: _display(display) {}
bool ImageScaler::isJpeg(const char *path)
{
String p = String(path);
p.toLowerCase();
return p.endsWith(".jpg") || p.endsWith(".jpeg");
}
/**
* Pick the smallest TJpgDec scale factor (largest image) that fits within maxW x maxH.
* Returns 1, 2, 4, or 8.
*/
uint8_t ImageScaler::pickBestScaleFactor(int origW, int origH, int maxW, int maxH)
{
const uint8_t scales[] = {1, 2, 4, 8};
for (int i = 0; i < 4; i++)
{
int sw = (origW + scales[i] - 1) / scales[i];
int sh = (origH + scales[i] - 1) / scales[i];
if (sw <= maxW && sh <= maxH)
return scales[i];
}
return 8;
}
bool ImageScaler::getScaledDimensions(const char *path, int maxW, int maxH,
int &outW, int &outH)
{
int origW, origH;
if (!sdHandler.getImageDimensions(String(path), origW, origH))
return false;
if (isJpeg(path) && (origW > maxW || origH > maxH))
{
uint8_t scale = pickBestScaleFactor(origW, origH, maxW, maxH);
outW = (origW + scale - 1) / scale;
outH = (origH + scale - 1) / scale;
}
else
{
outW = origW;
outH = origH;
}
return true;
}
bool ImageScaler::drawImageFitTo(const char *path, int x, int y, int maxW, int maxH,
bool dither, bool invert)
{
int origW, origH;
if (!sdHandler.getImageDimensions(String(path), origW, origH))
return false;
if (isJpeg(path) && (origW > maxW || origH > maxH))
{
uint8_t scale = pickBestScaleFactor(origW, origH, maxW, maxH);
int scaledW = (origW + scale - 1) / scale;
// Center horizontally within maxW
int offsetX = (maxW - scaledW) / 2;
Serial.printf("[ImageScaler] JPEG %dx%d -> 1/%d = %dx%d\n",
origW, origH, scale, scaledW, (origH + scale - 1) / scale);
return drawJpegWithScale(path, x + offsetX, y, scale, dither, invert);
}
else
{
// Image fits or is not JPEG: draw at original size, centered
if (origW < maxW)
{
int offsetX = (maxW - origW) / 2;
x += offsetX;
}
return _display->drawImage(path, x, y, dither, invert);
}
}
bool ImageScaler::drawJpegWithScale(const char *path, int x, int y, uint8_t scaleFactor,
bool dither, bool invert)
{
SdFile dat;
if (!dat.open(path, O_RDONLY))
{
Serial.printf("[ImageScaler] Failed to open: %s\n", path);
return false;
}
uint32_t total = dat.fileSize();
uint8_t *buff = (uint8_t *)ps_malloc(total);
if (!buff)
{
Serial.println("[ImageScaler] ps_malloc failed");
dat.close();
return false;
}
// Read entire file into PSRAM buffer
uint32_t pnt = 0;
while (pnt < total)
{
uint32_t toread = dat.available();
if (toread > 0)
{
int bytesRead = dat.read(buff + pnt, toread);
if (bytesRead > 0)
pnt += bytesRead;
}
}
dat.close();
// Initialize static state for the callback
s_display = _display;
s_lastY = -1;
s_blockW = -1;
s_blockH = -1;
memset(s_ditherBuffer, 0, sizeof(s_ditherBuffer));
memset(s_jpegDitherBuffer, 0, sizeof(s_jpegDitherBuffer));
// Configure TJpgDec with our scale and callback
TJpgDec.setJpgScale(scaleFactor);
TJpgDec.setCallback(jpegChunkCallback);
JRESULT result = TJpgDec.drawJpg(x, y, buff, total, dither, invert);
free(buff);
if (result != JDR_OK)
Serial.printf("[ImageScaler] drawJpg error: %d\n", result);
return result == JDR_OK;
}
/**
* TJpgDec decode callback — replicates the Inkplate SDK's drawJpegChunk
* for non-color boards with identical Floyd-Steinberg dithering.
* Uses Adafruit_GFX public interface for batched pixel writes.
*/
bool ImageScaler::jpegChunkCallback(int16_t x, int16_t y, uint16_t w, uint16_t h,
uint16_t *bitmap, bool dither, bool invert)
{
if (!s_display)
return false;
// Dither row transition: propagate error from previous row of blocks
if (dither && y != s_lastY)
{
for (int i = 0; i < E_INK_WIDTH + 20; ++i)
{
s_ditherBuffer[0][i] = s_ditherBuffer[1][i];
s_ditherBuffer[1][i] = 0;
}
s_lastY = y;
}
// Use Adafruit_GFX public interface for batched writes
Adafruit_GFX *gfx = static_cast<Adafruit_GFX *>(s_display);
gfx->startWrite();
bool is1Bit = (s_display->getDisplayMode() == INKPLATE_1BIT);
for (int j = 0; j < h; ++j)
{
for (int i = 0; i < w; ++i)
{
uint32_t rgb = bitmap[j * w + i];
uint8_t r = _RED(rgb);
uint8_t g = _GREEN(rgb);
uint8_t b = _BLUE(rgb);
uint32_t val;
if (dither)
{
// Floyd-Steinberg dithering (matches SDK's ditherGetPixelJpeg)
uint8_t px = RGB8BIT(r, g, b);
if (s_blockW == -1)
{
s_blockW = w;
s_blockH = h;
}
if (is1Bit)
px = (uint16_t)px >> 1;
uint16_t oldPixel = min((uint16_t)0xFF,
(uint16_t)((uint16_t)px +
(uint16_t)s_jpegDitherBuffer[j + 1][i + 1] +
(j ? (uint16_t)0 : (uint16_t)s_ditherBuffer[0][x + i])));
uint8_t newPixel = oldPixel & (is1Bit ? B10000000 : B11100000);
uint8_t quantError = oldPixel - newPixel;
s_jpegDitherBuffer[j + 1 + 1][i + 0 + 1] += (quantError * 5) >> 4;
s_jpegDitherBuffer[j + 0 + 1][i + 1 + 1] += (quantError * 7) >> 4;
s_jpegDitherBuffer[j + 1 + 1][i + 1 + 1] += (quantError * 1) >> 4;
s_jpegDitherBuffer[j + 1 + 1][i - 1 + 1] += (quantError * 3) >> 4;
val = newPixel >> 5;
}
else
{
val = RGB3BIT(r, g, b);
}
if (is1Bit)
{
val = (~val >> 2) & 1;
if (invert)
val = 1 - val;
}
else
{
if (invert)
val = 7 - val;
}
gfx->writePixel(x + i, y + j, val);
}
}
// Propagate dithering errors between horizontal blocks
if (dither)
{
for (int i = 0; i < 18; ++i)
{
if (x + i)
s_ditherBuffer[1][x + i - 1] += s_jpegDitherBuffer[s_blockH - 1 + 2][i];
s_jpegDitherBuffer[i][0 + 1] = s_jpegDitherBuffer[i][s_blockW - 1 + 2];
}
for (int j = 0; j < 18; ++j)
for (int i = 0; i < 18; ++i)
if (i != 1)
s_jpegDitherBuffer[j][i] = 0;
s_jpegDitherBuffer[17][1] = 0;
}
gfx->endWrite();
return true;
}