-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathTJpg_Decoder.cpp
More file actions
671 lines (539 loc) · 20.2 KB
/
TJpg_Decoder.cpp
File metadata and controls
671 lines (539 loc) · 20.2 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
TJpg_Decoder.cpp
Created by Bodmer 18/10/19
Latest version here:
https://github.com/Bodmer/TJpg_Decoder
*/
#include "TJpg_Decoder.h"
// Create a class instance to be used by the sketch (defined as extern in header)
TJpg_Decoder TJpgDec;
/***************************************************************************************
** Function name: TJpg_Decoder
** Description: Constructor
***************************************************************************************/
TJpg_Decoder::TJpg_Decoder(){
// Setup a pointer to this class for static functions
thisPtr = this;
}
/***************************************************************************************
** Function name: ~TJpg_Decoder
** Description: Destructor
***************************************************************************************/
TJpg_Decoder::~TJpg_Decoder(){
// Bye
}
/***************************************************************************************
** Function name: setJpgScale
** Description: Set the reduction scale factor (1, 2, 4 or 8)
***************************************************************************************/
void TJpg_Decoder::setSwapBytes(bool swapBytes){
_swap = swapBytes;
}
/***************************************************************************************
** Function name: setJpgScale
** Description: Set the reduction scale factor (1, 2, 4 or 8)
***************************************************************************************/
void TJpg_Decoder::setJpgScale(uint8_t scaleFactor)
{
switch (scaleFactor)
{
case 1:
jpgScale = 0;
break;
case 2:
jpgScale = 1;
break;
case 4:
jpgScale = 2;
break;
case 8:
jpgScale = 3;
break;
default:
jpgScale = 0;
}
}
/***************************************************************************************
** Function name: setCallback
** Description: Set the sketch callback function to render decoded blocks
***************************************************************************************/
void TJpg_Decoder::setCallback(SketchCallback sketchCallback)
{
tft_output = sketchCallback;
}
/***************************************************************************************
** Function name: jd_input (declared static)
** Description: Called by tjpgd.c to get more data
***************************************************************************************/
unsigned int TJpg_Decoder::jd_input(JDEC* jdec, uint8_t* buf, unsigned int len)
{
TJpg_Decoder *thisPtr = TJpgDec.thisPtr;
jdec = jdec; // Supress warning
// Handle an array input
if (thisPtr->jpg_source == TJPG_ARRAY) {
// Avoid running off end of array
if (thisPtr->array_index + len > thisPtr->array_size) {
len = thisPtr->array_size - thisPtr->array_index;
}
// If buf is valid then copy len bytes to buffer
if (buf) memcpy_P(buf, (const uint8_t *)(thisPtr->array_data + thisPtr->array_index), len);
// Move pointer
thisPtr->array_index += len;
}
#ifdef TJPGD_LOAD_FFS
// Handle SPIFFS input
else if (thisPtr->jpg_source == TJPG_FS_FILE) {
// Check how many bytes are available
uint32_t bytesLeft = thisPtr->jpgFile.available();
if (bytesLeft < len) len = bytesLeft;
if (buf) {
// Read into buffer, pointer moved as well
thisPtr->jpgFile.read(buf, len);
}
else {
// Buffer is null, so skip data by moving pointer
thisPtr->jpgFile.seek(thisPtr->jpgFile.position() + len);
}
}
#endif
#if defined (TJPGD_LOAD_SD_LIBRARY)
// Handle SD library input
else if (thisPtr->jpg_source == TJPG_SD_FILE) {
// Check how many bytes are available
uint32_t bytesLeft = thisPtr->jpgSdFile.available();
if (bytesLeft < len) len = bytesLeft;
if (buf) {
// Read into buffer, pointer moved as well
thisPtr->jpgSdFile.read(buf, len);
}
else {
// Buffer is null, so skip data by moving pointer
thisPtr->jpgSdFile.seek(thisPtr->jpgSdFile.position() + len);
}
}
#endif
#ifdef TJPGD_LOAD_HTTP_LIBRARY
else if (thisPtr->jpg_source == TJPG_STREAM_FILE)
{
if (thisPtr->array_index + len > thisPtr->array_size)
{
len = thisPtr->array_size - thisPtr->array_index;
}
if(thisPtr->jpg_http->connected())
{
uint8_t _buff[len];
WiFiClient* const stream = thisPtr->jpg_http->getStreamPtr();
if (stream->available()) stream->readBytes(_buff, len);
yield();
if (buf)
{
memcpy_P(buf,_buff,len);
}
thisPtr->array_index = thisPtr->array_index + len;
}
}
#endif
return len;
}
/***************************************************************************************
** Function name: jd_output (declared static)
** Description: Called by tjpgd.c with an image block for rendering
***************************************************************************************/
// Pass image block back to the sketch for rendering, may be a complete or partial MCU
int TJpg_Decoder::jd_output(JDEC* jdec, void* bitmap, JRECT* jrect)
{
// This is a static function so create a pointer to access other members of the class
TJpg_Decoder *thisPtr = TJpgDec.thisPtr;
jdec = jdec; // Supress warning as ID is not used
// Retrieve rendering parameters and add any offset
int16_t x = jrect->left + thisPtr->jpeg_x;
int16_t y = jrect->top + thisPtr->jpeg_y;
uint16_t w = jrect->right + 1 - jrect->left;
uint16_t h = jrect->bottom + 1 - jrect->top;
// Pass the image block and rendering parameters in a callback to the sketch
return thisPtr->tft_output(x, y, w, h, (uint16_t*)bitmap);
}
#if defined (TJPGD_LOAD_HTTP_LIBRARY)
/***************************************************************************************
** Function name: drawJpg
** Description: Draw a named jpg file at x,y (name in char array)
***************************************************************************************/
JRESULT TJpg_Decoder::drawJpgFromStream(int32_t x, int32_t y, char* _url) {
JDEC jdec;
JRESULT jresult = JDR_OK;
jpg_source = TJPG_STREAM_FILE;
jpeg_x = x;
jpeg_y = y;
array_size = 0;
array_index = 0;
jdec.swap = _swap;
jpg_http = new HTTPClient;
jpg_http->begin(_url);
long httpCode = jpg_http->GET();
if (httpCode == HTTP_CODE_OK)
{
array_size = jpg_http->getSize();
Serial.println(array_size);
array_index = 0;
}
// Analyse input data
jresult = jd_prepare(&jdec, jd_input, workspace, TJPGD_WORKSPACE_SIZE, 0);
// Extract image and render
if (jresult == JDR_OK)jresult = jd_decomp(&jdec, jd_output, jpgScale);
if(jpg_http->connected())
{
WiFiClient* const stream = jpg_http->getStreamPtr();
stream->stop();
stream->flush();
}
jpg_http->end();
delete jpg_http;
jpg_http = NULL;
return jresult;
}
/***************************************************************************************
** Function name: getJpgSizeFromStream
** Description: Get width and height of a jpg file (name in char array)
***************************************************************************************/
// Generic file call for SD or SPIFFS, uses leading / to distinguish SPIFFS files
JRESULT TJpg_Decoder::getJpgSizeFromStream(uint16_t *w, uint16_t *h, char *_url){
JDEC jdec;
JRESULT jresult = JDR_OK;
*w = 0;
*h = 0;
array_index = 0;
jpg_source = TJPG_STREAM_FILE;
if(!jpg_http||!jpg_http->connected()){
Serial.println("get size");
jpg_http = new HTTPClient;
jpg_http->begin(_url);
long httpCode = jpg_http->GET();
if (httpCode == HTTP_CODE_OK)
{
array_size = jpg_http->getSize();
}
jresult = jd_prepare(&jdec, jd_input, workspace, TJPGD_WORKSPACE_SIZE, 0);
if (jresult == JDR_OK) {
*w = jdec.width;
*h = jdec.height;
}
if(jpg_http->connected())
{
WiFiClient* const stream = jpg_http->getStreamPtr();
stream->stop();
stream->flush();
}
jpg_http->end();
delete jpg_http;
jpg_http = NULL;
}
return jresult;
}
#endif
#if defined (TJPGD_LOAD_SD_LIBRARY) || defined (TJPGD_LOAD_FFS)
/***************************************************************************************
** Function name: drawJpg
** Description: Draw a named jpg file at x,y (name in char array)
***************************************************************************************/
// Generic file call for SD or SPIFFS, uses leading / to distinguish SPIFFS files
JRESULT TJpg_Decoder::drawJpg(int32_t x, int32_t y, const char *pFilename){
#if defined (ESP8266) || defined (ESP32)
#if defined (TJPGD_LOAD_SD_LIBRARY)
if (*pFilename == '/')
#endif
return drawFsJpg(x, y, pFilename);
#endif
#if defined (TJPGD_LOAD_SD_LIBRARY)
return drawSdJpg(x, y, pFilename);
#endif
return JDR_INP;
}
/***************************************************************************************
** Function name: drawJpg
** Description: Draw a named jpg file at x,y (name in String)
***************************************************************************************/
// Generic file call for SD or SPIFFS, uses leading / to distinguish SPIFFS files
JRESULT TJpg_Decoder::drawJpg(int32_t x, int32_t y, const String& pFilename){
#if defined (ESP8266) || defined (ESP32)
#if defined (TJPGD_LOAD_SD_LIBRARY)
if (pFilename.charAt(0) == '/')
#endif
return drawFsJpg(x, y, pFilename);
#endif
#if defined (TJPGD_LOAD_SD_LIBRARY)
return drawSdJpg(x, y, pFilename);
#endif
return JDR_INP;
}
/***************************************************************************************
** Function name: getJpgSize
** Description: Get width and height of a jpg file (name in char array)
***************************************************************************************/
// Generic file call for SD or SPIFFS, uses leading / to distinguish SPIFFS files
JRESULT TJpg_Decoder::getJpgSize(uint16_t *w, uint16_t *h, const char *pFilename){
#if defined (ESP8266) || defined (ESP32)
#if defined (TJPGD_LOAD_SD_LIBRARY)
if (*pFilename == '/')
#endif
return getFsJpgSize(w, h, pFilename);
#endif
#if defined (TJPGD_LOAD_SD_LIBRARY)
return getSdJpgSize(w, h, pFilename);
#endif
return JDR_INP;
}
/***************************************************************************************
** Function name: getJpgSize
** Description: Get width and height of a jpg file (name in String)
***************************************************************************************/
// Generic file call for SD or SPIFFS, uses leading / to distinguish SPIFFS files
JRESULT TJpg_Decoder::getJpgSize(uint16_t *w, uint16_t *h, const String& pFilename){
#if defined (ESP8266) || defined (ESP32)
#if defined (TJPGD_LOAD_SD_LIBRARY)
if (pFilename.charAt(0) == '/')
#endif
return getFsJpgSize(w, h, pFilename);
#endif
#if defined (TJPGD_LOAD_SD_LIBRARY)
return getSdJpgSize(w, h, pFilename);
#endif
return JDR_INP;
}
#endif
#ifdef TJPGD_LOAD_FFS
/***************************************************************************************
** Function name: drawFsJpg
** Description: Draw a named jpg file at x,y (name in char array)
***************************************************************************************/
// Call specific to SPIFFS
JRESULT TJpg_Decoder::drawFsJpg(int32_t x, int32_t y, const char *pFilename, fs::FS &fs) {
// Check if file exists
if ( !fs.exists(pFilename) )
{
Serial.println(F("Jpeg file not found"));
return JDR_INP;
}
return drawFsJpg(x, y, fs.open( pFilename, "r"));
}
/***************************************************************************************
** Function name: drawFsJpg
** Description: Draw a named jpg file at x,y (name in String)
***************************************************************************************/
JRESULT TJpg_Decoder::drawFsJpg(int32_t x, int32_t y, const String& pFilename, fs::FS &fs) {
// Check if file exists
if ( !fs.exists(pFilename) )
{
Serial.println(F("Jpeg file not found"));
return JDR_INP;
}
return drawFsJpg(x, y, fs.open( pFilename, "r"));
}
/***************************************************************************************
** Function name: drawFsJpg
** Description: Draw a jpg with opened file handle at x,y
***************************************************************************************/
JRESULT TJpg_Decoder::drawFsJpg(int32_t x, int32_t y, fs::File inFile) {
JDEC jdec;
JRESULT jresult = JDR_OK;
jpg_source = TJPG_FS_FILE;
jpeg_x = x;
jpeg_y = y;
jdec.swap = _swap;
jpgFile = inFile;
jresult = jd_prepare(&jdec, jd_input, workspace, TJPGD_WORKSPACE_SIZE, (unsigned int)0);
// Extract image and render
if (jresult == JDR_OK) {
jresult = jd_decomp(&jdec, jd_output, jpgScale);
}
// Close file
if (jpgFile) jpgFile.close();
return jresult;
}
/***************************************************************************************
** Function name: getFsJpgSize
** Description: Get width and height of a jpg saved in SPIFFS or LittleFS
***************************************************************************************/
// Call specific to SPIFFS
JRESULT TJpg_Decoder::getFsJpgSize(uint16_t *w, uint16_t *h, const char *pFilename, fs::FS &fs) {
// Check if file exists
if ( !fs.exists(pFilename) )
{
Serial.println(F("Jpeg file not found"));
return JDR_INP;
}
return getFsJpgSize(w, h, fs.open( pFilename, "r"));
}
/***************************************************************************************
** Function name: getFsJpgSize
** Description: Get width and height of a jpg saved in SPIFFS or LittleFS
***************************************************************************************/
JRESULT TJpg_Decoder::getFsJpgSize(uint16_t *w, uint16_t *h, const String& pFilename, fs::FS &fs) {
// Check if file exists
if ( !fs.exists(pFilename) )
{
Serial.println(F("Jpeg file not found"));
return JDR_INP;
}
return getFsJpgSize(w, h, fs.open( pFilename, "r"));
}
/***************************************************************************************
** Function name: getFsJpgSize
** Description: Get width and height of a jpg saved in SPIFFS
***************************************************************************************/
JRESULT TJpg_Decoder::getFsJpgSize(uint16_t *w, uint16_t *h, fs::File inFile) {
JDEC jdec;
JRESULT jresult = JDR_OK;
*w = 0;
*h = 0;
jpg_source = TJPG_FS_FILE;
jpgFile = inFile;
jresult = jd_prepare(&jdec, jd_input, workspace, TJPGD_WORKSPACE_SIZE, 0);
if (jresult == JDR_OK) {
*w = jdec.width;
*h = jdec.height;
}
// Close file
if (jpgFile) jpgFile.close();
return jresult;
}
#endif
#if defined (TJPGD_LOAD_SD_LIBRARY)
/***************************************************************************************
** Function name: drawSdJpg
** Description: Draw a named jpg SD file at x,y (name in char array)
***************************************************************************************/
// Call specific to SD
JRESULT TJpg_Decoder::drawSdJpg(int32_t x, int32_t y, const char *pFilename) {
// Check if file exists
if ( !SD.exists(pFilename) )
{
Serial.println(F("Jpeg file not found"));
return JDR_INP;
}
return drawSdJpg(x, y, SD.open( pFilename, FILE_READ));
}
/***************************************************************************************
** Function name: drawSdJpg
** Description: Draw a named jpg SD file at x,y (name in String)
***************************************************************************************/
JRESULT TJpg_Decoder::drawSdJpg(int32_t x, int32_t y, const String& pFilename) {
// Check if file exists
if ( !SD.exists(pFilename) )
{
Serial.println(F("Jpeg file not found"));
return JDR_INP;
}
return drawSdJpg(x, y, SD.open( pFilename, FILE_READ));
}
/***************************************************************************************
** Function name: drawSdJpg
** Description: Draw a jpg with opened SD file handle at x,y
***************************************************************************************/
JRESULT TJpg_Decoder::drawSdJpg(int32_t x, int32_t y, File inFile) {
JDEC jdec;
JRESULT jresult = JDR_OK;
jpg_source = TJPG_SD_FILE;
jpeg_x = x;
jpeg_y = y;
jdec.swap = _swap;
jpgSdFile = inFile;
jresult = jd_prepare(&jdec, jd_input, workspace, TJPGD_WORKSPACE_SIZE, 0);
// Extract image and render
if (jresult == JDR_OK) {
jresult = jd_decomp(&jdec, jd_output, jpgScale);
}
// Close file
if (jpgSdFile) jpgSdFile.close();
return jresult;
}
/***************************************************************************************
** Function name: getSdJpgSize
** Description: Get width and height of a jpg saved in SPIFFS
***************************************************************************************/
// Call specific to SD
JRESULT TJpg_Decoder::getSdJpgSize(uint16_t *w, uint16_t *h, const char *pFilename) {
// Check if file exists
if ( !SD.exists(pFilename) )
{
Serial.println(F("Jpeg file not found"));
return JDR_INP;
}
return getSdJpgSize(w, h, SD.open( pFilename, FILE_READ));
}
/***************************************************************************************
** Function name: getSdJpgSize
** Description: Get width and height of a jpg saved in SPIFFS
***************************************************************************************/
JRESULT TJpg_Decoder::getSdJpgSize(uint16_t *w, uint16_t *h, const String& pFilename) {
// Check if file exists
if ( !SD.exists(pFilename) )
{
Serial.println(F("Jpeg file not found"));
return JDR_INP;
}
return getSdJpgSize(w, h, SD.open( pFilename, FILE_READ));
}
/***************************************************************************************
** Function name: getSdJpgSize
** Description: Get width and height of a jpg saved in SPIFFS
***************************************************************************************/
JRESULT TJpg_Decoder::getSdJpgSize(uint16_t *w, uint16_t *h, File inFile) {
JDEC jdec;
JRESULT jresult = JDR_OK;
*w = 0;
*h = 0;
jpg_source = TJPG_SD_FILE;
jpgSdFile = inFile;
jresult = jd_prepare(&jdec, jd_input, workspace, TJPGD_WORKSPACE_SIZE, 0);
if (jresult == JDR_OK) {
*w = jdec.width;
*h = jdec.height;
}
// Close file
if (jpgSdFile) jpgSdFile.close();
return jresult;
}
#endif
/***************************************************************************************
** Function name: drawJpg
** Description: Draw a jpg saved in a FLASH memory array
***************************************************************************************/
JRESULT TJpg_Decoder::drawJpg(int32_t x, int32_t y, const uint8_t jpeg_data[], uint32_t data_size) {
JDEC jdec;
JRESULT jresult = JDR_OK;
jpg_source = TJPG_ARRAY;
array_index = 0;
array_data = jpeg_data;
array_size = data_size;
jpeg_x = x;
jpeg_y = y;
jdec.swap = _swap;
// Analyse input data
jresult = jd_prepare(&jdec, jd_input, workspace, TJPGD_WORKSPACE_SIZE, 0);
// Extract image and render
if (jresult == JDR_OK) {
jresult = jd_decomp(&jdec, jd_output, jpgScale);
}
return jresult;
}
/***************************************************************************************
** Function name: getJpgSize
** Description: Get width and height of a jpg saved in a FLASH memory array
***************************************************************************************/
JRESULT TJpg_Decoder::getJpgSize(uint16_t *w, uint16_t *h, const uint8_t jpeg_data[], uint32_t data_size) {
JDEC jdec;
JRESULT jresult = JDR_OK;
*w = 0;
*h = 0;
jpg_source = TJPG_ARRAY;
array_index = 0;
array_data = jpeg_data;
array_size = data_size;
// Analyse input data
jresult = jd_prepare(&jdec, jd_input, workspace, TJPGD_WORKSPACE_SIZE, 0);
if (jresult == JDR_OK) {
*w = jdec.width;
*h = jdec.height;
}
return jresult;
}