-
Notifications
You must be signed in to change notification settings - Fork 79
/
ImagePNG.cpp
406 lines (352 loc) · 10.7 KB
/
ImagePNG.cpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
**************************************************
* @file ImagePNG.cpp
* @brief Basic functionalities to work with PNG images
*
* https://github.com/e-radionicacom/Inkplate-Arduino-library
* For support, please reach over forums: forum.e-radionica.com/en
* For more info about the product, please check: www.inkplate.io
*
* This code is released under the GNU Lesser General Public
*License v3.0: https://www.gnu.org/licenses/lgpl-3.0.en.html Please review the
*LICENSE file included with this example. If you have any questions about
*licensing, please contact [email protected] Distributed as-is; no
*warranty is given.
*
* @authors Soldered.com
***************************************************/
#include "Image.h"
#include "../libs/pngle/pngle.h"
#include "defines.h"
extern Image *_imagePtrPng;
static bool _pngInvert = 0;
static bool _pngDither = 0;
static int16_t lastY = -1;
static uint16_t _pngX = 0;
static uint16_t _pngY = 0;
static Image::Position _pngPosition = Image::_npos;
/**
* @brief pngle_on_draw
*
* @param pngle_t *pngle
* pointer to image
* @param uint32_t x
* x plane position
* @param uint32_t y
* y plane position
* @param uint32_t w
* image width
* @param uint32_t h
* image height
* @param uint8_t rgba[4]
* color
*/
void pngle_on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4])
{
if (_pngPosition != Image::_npos)
{
_imagePtrPng->getPointsForPosition(_pngPosition, pngle_get_width(pngle), pngle_get_height(pngle), E_INK_WIDTH,
E_INK_HEIGHT, &_pngX, &_pngY);
lastY = _pngY;
_pngPosition = Image::_npos;
}
if (rgba[3])
for (int j = 0; j < h; ++j)
for (int i = 0; i < w; ++i)
{
uint8_t r = rgba[0];
uint8_t g = rgba[1];
uint8_t b = rgba[2];
pngle_ihdr_t *ihdr = pngle_get_ihdr(pngle);
if (ihdr->depth == 1)
r = g = b = (b ? 0xFF : 0);
#if defined(ARDUINO_INKPLATECOLOR) || defined(ARDUINO_INKPLATE2) || defined(ARDUINO_INKPLATE4) || \
defined(ARDUINO_INKPLATE7)
if (_pngInvert)
{
r = 255 - r;
g = 255 - g;
b = 255 - b;
}
uint8_t px = _imagePtrPng->findClosestPalette(r, g, b);
#else
uint8_t px = RGB3BIT(r, g, b);
#endif
if (_pngDither)
{
#if defined(ARDUINO_INKPLATECOLOR) || defined(ARDUINO_INKPLATE2) || defined(ARDUINO_INKPLATE4) || \
defined(ARDUINO_INKPLATE7)
px = _imagePtrPng->ditherGetPixelBmp((r << 16) | (g << 8) | (b), x + i, y + j,
_imagePtrPng->width(), 0);
#else
px = _imagePtrPng->ditherGetPixelBmp(RGB8BIT(r, g, b), x + i, y + j, _imagePtrPng->width(), 0);
if (_pngInvert)
px = 7 - px;
if (_imagePtrPng->getDisplayMode() == INKPLATE_1BIT)
px = (~px >> 2) & 1;
#endif
}
_imagePtrPng->drawPixel(_pngX + x + i, _pngY + y + j, px);
}
if (lastY != y)
{
lastY = y;
_imagePtrPng->ditherSwap(_imagePtrPng->width());
}
}
/**
* @brief drawPngFromSd function draws png image from sd file
*
* @param char *fileName
* pointer to png file
* @param int x
* x position for top left image corner
* @param int y
* y position for top left image corner
* @param bool dither
* 1 if using dither, 0 if not
* @param bool invert
* 1 if using invert, 0 if not
*
* @return 1 if drawn successfully, 0 if not
*/
bool Image::drawPngFromSd(const char *fileName, int x, int y, bool dither, bool invert)
{
SdFile dat;
if (dat.open(fileName, O_RDONLY))
{
return drawPngFromSd(&dat, x, y, dither, invert);
}
return 0;
}
/**
* @brief drawPngFromSd function draws png image from sd file
*
* @param SdFile *p
* pointer to png file
* @param int x
* x position for top left image corner
* @param int y
* y position for top left image corner
* @param bool dither
* 1 if using dither, 0 if not
* @param bool invert
* 1 if using invert, 0 if not
*
* @return 1 if drawn successfully, 0 if not
*/
bool Image::drawPngFromSd(SdFile *p, int x, int y, bool dither, bool invert)
{
_pngDither = dither;
_pngInvert = invert;
lastY = y;
bool ret = 1;
uint32_t remain = 0;
if (dither)
memset(ditherBuffer, 0, sizeof ditherBuffer);
pngle_t *pngle = pngle_new();
_pngX = x;
_pngY = y;
pngle_set_draw_callback(pngle, pngle_on_draw);
uint32_t total = p->fileSize();
uint8_t buff[2048];
uint32_t pnt = 0;
while (pnt < total)
{
uint32_t toread = p->available();
if (toread > 0)
{
int len = p->read(buff, min((uint32_t)2048, toread));
int fed = pngle_feed(pngle, buff, len);
if (fed < 0)
{
ret = 0;
break;
}
remain = remain + len - fed;
pnt += len;
}
}
p->close();
pngle_destroy(pngle);
return ret;
}
/**
* @brief drawPngFromWeb function draws png image from web
*
* @param char *url
* pointer to png file
* @param int x
* x position for top left image corner
* @param int y
* y position for top left image corner
* @param bool dither
* 1 if using dither, 0 if not
* @param bool invert
* 1 if using invert, 0 if not
*
* @return 1 if drawn successfully, 0 if not
*/
bool Image::drawPngFromWeb(const char *url, int x, int y, bool dither, bool invert)
{
_pngDither = dither;
_pngInvert = invert;
lastY = y;
bool ret = 1;
if (dither)
memset(ditherBuffer, 0, sizeof ditherBuffer);
pngle_t *pngle = pngle_new();
_pngX = x;
_pngY = y;
pngle_set_draw_callback(pngle, pngle_on_draw);
int32_t defaultLen = E_INK_WIDTH * E_INK_HEIGHT * 8 + 100;
uint8_t *buf = 0;
if (strncmp(url, "http://", 7) == 0)
{
buf = downloadFile(url, &defaultLen);
}
else if (strncmp(url, "https://", 8) == 0)
{
buf = downloadFileHTTPS(url, &defaultLen);
}
if (!buf)
return 0;
if (pngle_feed(pngle, buf, defaultLen) < 0)
ret = 0;
pngle_destroy(pngle);
free(buf);
return ret;
}
/**
* @brief drawPngFromWeb function draws png image from sd file
*
* @param WiFiClient *s
* pointer to png file
* @param int x
* x position for top left image corner
* @param int y
* y position for top left image corner
* @param int32_t len
* Image length
* @param bool dither
* 1 if using dither, 0 if not
* @param bool invert
* 1 if using invert, 0 if not
*
* @return 1 if drawn successfully, 0 if not
*/
bool Image::drawPngFromWeb(WiFiClient *s, int x, int y, int32_t len, bool dither, bool invert)
{
_pngDither = dither;
_pngInvert = invert;
lastY = y;
bool ret = 1;
if (dither)
memset(ditherBuffer, 0, sizeof ditherBuffer);
pngle_t *pngle = pngle_new();
_pngX = x;
_pngY = y;
pngle_set_draw_callback(pngle, pngle_on_draw);
uint8_t *buff = downloadFile(s, len);
if (!buff)
return 0;
if (pngle_feed(pngle, buff, len) < 0)
ret = 0;
pngle_destroy(pngle);
free(buff);
return ret;
}
/**
* @brief drawPngFromWebAtPosition function draws png image from web at
* screen position
*
* @param char *url
* pointer to png file
* @param Position &position
* Image position (center, topLeft, bottomLeft, topRight,
* bottomRight, _npos)
* @param bool dither
* 1 if using dither, 0 if not
* @param bool invert
* 1 if using invert, 0 if not
*
* @return 1 if drawn successfully, 0 if not
*/
bool Image::drawPngFromWebAtPosition(const char *url, const Position &position, const bool dither, const bool invert)
{
_pngDither = dither;
_pngInvert = invert;
bool ret = 1;
if (dither)
memset(ditherBuffer, 0, sizeof ditherBuffer);
pngle_t *pngle = pngle_new();
_pngPosition = position;
pngle_set_draw_callback(pngle, pngle_on_draw);
int32_t defaultLen = E_INK_WIDTH * E_INK_HEIGHT * 4 + 100;
uint8_t *buff = downloadFile(url, &defaultLen);
if (!buff)
return 0;
if (pngle_feed(pngle, buff, defaultLen) < 0)
ret = 0;
pngle_destroy(pngle);
free(buff);
_pngPosition = _npos;
return ret;
}
/**
* @brief drawPngFromSdAtPosition function draws png image from sd card at
* screen position
*
* @param char *fileName
* pointer to png file
* @param Position &position
* Image position (center, topLeft, bottomLeft, topRight,
* bottomRight, _npos)
* @param bool dither
* 1 if using dither, 0 if not
* @param bool invert
* 1 if using invert, 0 if not
*
* @return 1 if drawn successfully, 0 if not
*/
bool Image::drawPngFromSdAtPosition(const char *fileName, const Position &position, const bool dither,
const bool invert)
{
SdFile dat;
if (!dat.open(fileName, O_RDONLY))
{
return 0;
}
_pngDither = dither;
_pngInvert = invert;
bool ret = 1;
uint32_t remain = 0;
if (dither)
memset(ditherBuffer, 0, sizeof ditherBuffer);
pngle_t *pngle = pngle_new();
_pngPosition = position;
pngle_set_draw_callback(pngle, pngle_on_draw);
uint32_t total = dat.fileSize();
uint8_t buff[2048];
uint32_t pnt = 0;
while (pnt < total)
{
uint32_t toread = dat.available();
if (toread > 0)
{
int len = dat.read(buff, min((uint32_t)2048, toread));
int fed = pngle_feed(pngle, buff, len);
if (fed < 0)
{
ret = 0;
break;
}
remain = remain + len - fed;
pnt += len;
}
}
dat.close();
pngle_destroy(pngle);
_pngPosition = _npos;
return ret;
}