-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathImageViewer.cpp
More file actions
1631 lines (1451 loc) · 56.1 KB
/
Copy pathImageViewer.cpp
File metadata and controls
1631 lines (1451 loc) · 56.1 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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ImageViewer.h
//
//
//
#include "ImageViewer.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define IMAGEDEBUG true // define image debug to send processing data to serial port
#define IMAGEDEBUG1 true // define even more image debug to send processing data to serial port
#define IMAGEDEBUG2 true // define maximum image debug to send processing data to serial port
const int ESP_MIN_HEAP = 2048; // if the free heap space every falls below this number, abort.
//
// code based on ESP8266 WiFi Image Viewer by James Eckert - http://jeplans.com/default.php?targp=ESP2Electronics2
// (although I was never able to get that PHP conversion to work, so I wrote my own in ASP.Net)
// see https://github.com/gojimmypi/imageConvert2BMP
// Other resources that I found helpful:
// http://stackoverflow.com/questions/5751749/how-can-i-read-bmp-pixel-values-into-an-array
// https://msdn.microsoft.com/en-us/library/dd183391(v=vs.85).aspx
// http://www.instructables.com/id/Arduino-TFT-display-of-bitmap-images-from-an-SD-Ca/?ALLSTEPS
// https://github.com/Bodmer/TFT_ILI9341
// https://github.com/magore/esp8266_ili9341
// https://github.com/adafruit/Adafruit-GFX-Library
// http://stackoverflow.com/questions/19131556/how-to-get-rgb888-24-bit-and-rgb565-16-bit-framebuffer-dump-from-a-jpg-ima
// http://www.avrfreaks.net/forum/displaying-image-tft-display
// https://en.wikipedia.org/wiki/RGBA_color_space
// https://sourceforge.net/projects/easybmp/?source=typ_redirect
// https://bytes.com/topic/c/answers/554304-using-32-bit-bitmaps-c
//*******************************************************************************************************************************************
//
//*******************************************************************************************************************************************
void imageViewDelay() {
Serial.print("Waiting.");
delay(1000); Serial.print(".");
delay(1000); Serial.print(".");
delay(1000); Serial.print(".");
delay(1000); Serial.print(".");
}
//*******************************************************************************************************************************************
//
//*******************************************************************************************************************************************
int ah2i(uint8_t s)
{
int a = 0;
uint8_t c = s;
if (c >= 48 && c <= 57) {
a = c - 48;
}
else if (c >= 65 && c <= 70) {
a = 16;
a += (c - 65);
}
else if (c >= 97 && c <= 102) {
a = 16;
a += (c - 97);
}
return a;
}
//*******************************************************************************************************************************************
//
//*******************************************************************************************************************************************
uint16_t read16(WiFiClient * f) {
uint16_t result;
((uint8_t *)&result)[0] = f->read(); // LSB
((uint8_t *)&result)[1] = f->read(); // MSB
return result;
}
//*******************************************************************************************************************************************
//
//*******************************************************************************************************************************************
uint32_t read32(WiFiClient * f) {
uint32_t result;
((uint8_t *)&result)[0] = f->read(); // LSB
((uint8_t *)&result)[1] = f->read();
((uint8_t *)&result)[2] = f->read();
((uint8_t *)&result)[3] = f->read(); // MSB
return result;
}
//*******************************************************************************************************************************************
//
//*******************************************************************************************************************************************
uint16_t read16(unsigned char * d, int fromIndex) {
uint16_t result;
((uint8_t *)&result)[0] = d[fromIndex]; // LSB
((uint8_t *)&result)[1] = d[fromIndex + 1]; // MSB
return result;
}
//*******************************************************************************************************************************************
//
//*******************************************************************************************************************************************
uint32_t read32(unsigned char * d, int fromIndex) {
uint32_t result;
((uint8_t *)&result)[0] = d[fromIndex]; // LSB
((uint8_t *)&result)[1] = d[fromIndex + 1];
((uint8_t *)&result)[2] = d[fromIndex + 2];
((uint8_t *)&result)[3] = d[fromIndex + 3]; // MSB
return result;
}
//*******************************************************************************************************************************************
//
//*******************************************************************************************************************************************
unsigned char* bmpRawReader(WiFiClient * f) {
// based on code from http://stackoverflow.com/questions/9296059/read-pixel-value-in-bmp-file
uint8_t info[54];
f->readBytes(info, 54); // read the 54-byte header
// extract image height and width from header
int width = *(int*)&info[18]; // compiler complains: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
int height = *(int*)&info[22];
int size = 3 * width * height;
unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
f->readBytes(data, size);
for (int i = 0; i < size; i += 3)
{
unsigned char tmp = data[i];
data[i] = data[i + 2];
data[i + 2] = tmp;
}
return data;
}
//*******************************************************************************************************************************************
//
//*******************************************************************************************************************************************
boolean bmpReadHeader(WiFiClient * f)
{
// read header
uint32_t tmp;
uint8_t bmpDepth;
if (read16(f) != 0x4D42) {
Serial.println("Magic bytes missing");
return false;
}
// read file size
tmp = read32(f);
Serial.print("size 0x");
Serial.println(tmp, HEX);
// read and ignore creator bytes
read32(f);
uint32_t offset = read32(f);
Serial.print("offset ");
Serial.println(offset, DEC);
// read DIB header
tmp = read32(f);
Serial.print("header size ");
Serial.println(tmp, DEC);
int bmp_width = read32(f);
int bmp_height = read32(f);
Serial.print("Width:");
Serial.println(bmp_width);
Serial.print("Height:");
Serial.println(bmp_height);
//if (bmp_width != 240 || bmp_height != 320) // if image is not 320x240, return false
//{
// Serial.println("Wrong dimensions");
// return false;
//}
if (read16(f) != 1)
{
Serial.println("Wrong # color planes");
return false;
}
bmpDepth = read16(f);
Serial.print("bitdepth ");
Serial.println(bmpDepth, DEC);
if (read32(f) != 0) {
Serial.println("Compression not supported!");
return false;
}
read32(f); //imagesizes
read32(f);//horizontal resolution
read32(f);//vertical resolution
read32(f);//number of colors
read32(f);//number of important colors
return true;
}
// http://cppcoder.blogspot.com/2007/11/bmp-file-format.html
int len = 1; // we'll determine to total length of the image byte stream
unsigned char* pImageBMP;
int currentStreamPosition = 0;
int currentStreamPayloadPosition = 0; // there are often chunks of data returned in separate "payloads"
int totalBytesRead = 0;
bool abortMidStream = false;
// ***************************************************************************************************************************************************************
// byteInStream - forward-reading only; get byte at [position] in a stream of web data. if it is not currently loaded into memory,
// discard prior chunk of data and read the next one until found. (works best when incrementing by 1 only!)
//
// TODO - add maximum amount of time to read before giving up
// ***************************************************************************************************************************************************************
uint8_t byteInStream(WiFiClient * stream, int position) {
uint32_t thisPayloadBytesAvailable = 0;
int thisPayloadByteCount = 0;
int thisAttemptCount = 0;
boolean foundPayload = false;
abortMidStream = false; // if we lose our connection mid-stream, we may need to abort
if (position > totalBytesRead) {
// we only ever do anything here if the requested position in stream is greater than the total bytes we've read
int startMillis = millis();
#ifdef IMAGEDEBUG
Serial.print("byteInStream - reading next segment position ");
Serial.print(position);
Serial.print(" at time: ");
Serial.print(startMillis);
Serial.print("; len =");
Serial.println(len);
yield();
#endif
while ((totalBytesRead < len) && (!foundPayload)) {
thisPayloadBytesAvailable = stream->available();
if ((totalBytesRead < len) && (thisPayloadBytesAvailable == 0)) {
yield();
thisPayloadBytesAvailable = stream->available();
}
if (thisPayloadBytesAvailable > 0) {
if (thisPayloadBytesAvailable < ESP.getFreeHeap() - ESP_MIN_HEAP) {
delete pImageBMP; // without deleting old data, we run out of heap space!
pImageBMP = new unsigned char[thisPayloadBytesAvailable]; // allocation of just enough memory for this incremenal payload of data
}
else {
abortMidStream = true;
Serial.println("Out of heap space while rendering!");
break; // abort while loop when out of space
}
//Serial.print("Reading.... ");
yield();
//thisPayloadByteCount = stream->read(data + totalBytesRead, thisPayloadBytesAvailable); // the old code appended to one big data array
if (stream->connected()) {
thisPayloadByteCount = stream->read(pImageBMP, thisPayloadBytesAvailable);
currentStreamPosition = totalBytesRead + 1; // the prior [total bytes read] + 1 is the new stream position
totalBytesRead += thisPayloadByteCount; // this is the ending position of the stream payload we now have
//Serial.print(thisPayloadByteCount);
//Serial.println(" bytes. Done.");
//Serial.printf("settings heap size: %u\n", ESP.getFreeHeap());
//Serial.print(" total read = ");
//Serial.println(totalBytesRead);
foundPayload = true; // once we find and read the next payload, we exit the while loop
}
else {
Serial.println("Error: byteInStream called with disconnected stream!");
yield();
abortMidStream = true;
break; // abort while loop when not connected
}
}
else {
yield();
delay(1);
thisAttemptCount++;
if (thisAttemptCount > 1000) {
abortMidStream = true;
#ifdef IMAGEDEBUG
Serial.println("No payload available for more than 1000 attempts for stream.available!");
#endif
break; // give up trying to read
}
}
}
//typically less than 1 milis to read
#ifdef IMAGEDEBUG
Serial.print("done reading. ");
Serial.print(millis() - startMillis);
Serial.print(" millis to read. Available wait loops: ");
Serial.println(thisAttemptCount);
#endif
}
else {
// TODO - allow bi-directional reading? we'd likely need to start all over at the beginning. probably not the most efficient...
}
if (abortMidStream) {
#ifdef IMAGEDEBUG
Serial.println("Error: byteInStream aborted mid-stream, returning NAN!");
#endif // IMAGEDEBUG
return NAN; // we have no data
}
else
{
int thisPayloadPosition = position - currentStreamPosition + 1;
if (thisPayloadPosition >= 0) {
return pImageBMP[position - currentStreamPosition + 1]; // TODO, reference position in payload only, not full stream
}
else {
return (uint8_t)0;
#ifdef IMAGEDEBUG
Serial.println("Error: requested position not currently in memory (currently supporting forward-reading only)");
#endif
} // else requested position is not in memory, not already processed, and stream cannot be reversed
} // else not aborting mid-stream
}
// ***************************************************************************************************************************************************************
// screenIsValidX - is a given X-ccordinate between 0 and [SCREEN_WIDTH - 1] ?
// ***************************************************************************************************************************************************************
int screenIsValidX(int thisX) {
return ( (thisX < SCREEN_WIDTH) && (thisX >= 0) ? true : false);
}
// ***************************************************************************************************************************************************************
// screenIsValidY - is a given Y-ccordinate between 0 and [SCREEN_HEIGHT - 1] ?
// ***************************************************************************************************************************************************************
int screenIsValidY(int thisY) {
return ((thisY < SCREEN_HEIGHT) && (thisY >= 0) ? true : false);
}
// ***************************************************************************************************************************************************************
// screenSafeX: ensure requested X-coordinate lands on screen, based on a resepctive starting offset value
// ***************************************************************************************************************************************************************
int screenSafeX(int thisX, int startX) {
if (isnan(thisX) || isnan(startX)) return 0;
int resultX = thisX + startX;
if (screenIsValidX(resultX)) {
return resultX;
}
else if (resultX < 0) {
return 0;
}
else if (resultX > SCREEN_WIDTH) {
return SCREEN_WIDTH;
}
else {
return 0; // we'll likely never end up here. this is just to appease compiler warning. wanted screenIsValidY listed first for performance
}
}
// ***************************************************************************************************************************************************************
// screenSafeY: ensure requested Y-coordinate lands on screen, based on a resepctive starting offset value
// ***************************************************************************************************************************************************************
int screenSafeY(int thisY, int startY) {
if (isnan(thisY) || isnan(startY)) return 0;
int resultY = thisY + startY;
if (screenIsValidY(resultY)) {
return resultY;
}
else if (resultY < 0) {
return 0;
}
else if (resultY > SCREEN_HEIGHT) {
return SCREEN_HEIGHT;
}
else {
return 0; // we'll likely never end up here. this is just to appease compiler warning. wanted screenIsValidY listed firsdt for performance
}
}
// ***************************************************************************************************************************************************************
// ***************************************************************************************************************************************************************
// bmpDrawFromUrlStream - get a BMP image at web location imageUrl, and draw it at an offset from (0,0) at (startX, startY)
// as many images will be bigger than all of available ESP8266 memory, we are rendering image as
// data is being read from the stream.
// ***************************************************************************************************************************************************************
// ***************************************************************************************************************************************************************
void bmpDrawFromUrlStream(Adafruit_ILI9341 * tftPtr, String imageUrl, int startX, int startY)
{
pImageBMP = new unsigned char[len]; // we allocate / delete data as needed;this pointer to BMP image byte array contains only part of an image at any given time
uint32_t time = millis(); // we'll keep track of how long it takes to render
Serial.print("Starting bmpDrawFromUrl 1.03: ");
Serial.print(imageUrl);
Serial.print(" - ");
currentStreamPosition = 0; // new images always start at byte position 0
currentStreamPayloadPosition = 0; // there are often chunks of data returned in separate "payloads"; this is the relative position in that payload
totalBytesRead = 0; // how many bytes of total image size have been read ant any given time.
len = 1; // total length of image data, including all expected chunks of data in all payloads read
HTTPClient http;
http.begin(imageUrl);
int httpCode = http.GET();
//if (!http.connected()) {
// Serial.println("HTTPClient not connected. Aborting bmpDrawFromUrl");
// return;
//}
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
// file found at server
if (httpCode == HTTP_CODE_OK) {
Serial.printf("Initial heap size: %u\n", ESP.getFreeHeap());
unsigned long DrawTime = millis();
// get length of document (is -1 when Server sends no Content-Length header)
len = http.getSize(); // TODO - this is no longer needed locally
// unsigned char tmp;
Serial.printf("settings heap size: %u\n", ESP.getFreeHeap());
Serial.print(" length = ");
Serial.println(len);
WiFiClient * stream = http.getStreamPtr();
unsigned char testChar = byteInStream(stream, 1); // read the first chunk of data. TODO ensure we read at least 54 bytes of header.
Serial.print("Step 1 ");
Serial.println(testChar);
// int thisBytesAvailable = stream->available();
// data = new unsigned char[thisBytesAvailable];
//
// Serial.print("thisBytesAvailable = ");
// Serial.print(thisBytesAvailable);
// Serial.println("Step 2");
Serial.printf("settings heap size: %u\n", ESP.getFreeHeap());
// totalBytesRead = stream->read(data, thisBytesAvailable);
Serial.print("Initial Read = ");
Serial.println(totalBytesRead);
// const int HEADER_SIZE = 54;
// stream->read(info, HEADER_SIZE); // read the 54-byte header
// extract image height and width from header
int bfOffBits = read32(pImageBMP, 10); // *(int*)&data[10]; // typically 1078
int biSize = read32(pImageBMP, 14); // Specifies the size of the BITMAPINFOHEADER structure, in bytes. (typically 40 bytes)
int biWidth = read32(pImageBMP, 18); // *(int*)data + 18;
int biHeight = read32(pImageBMP, 22); // *(int*)data + 22;
int biBitCount = read32(pImageBMP, 28); // Specifies the number of bits per pixel.
int biSizeImage = read32(pImageBMP, 34); // *(int*)data + 34;
Serial.print("filelen = ");
Serial.println(len);
Serial.print("biSize = ");
Serial.println(biSize);
Serial.print("Offset = "); // Specifies the offset from the beginning of the file to the bitmap data.
Serial.println(bfOffBits);
Serial.print("biSize = "); // Specifies the size of the BITMAPINFOHEADER structure, in bytes.
Serial.println(biSize);
Serial.print("biWidth = ");
Serial.println(biWidth);
Serial.print("biHeight = ");
Serial.println(biHeight);
Serial.print("biBitCount = ");
Serial.println(biBitCount);
Serial.print("biSizeImage = ");
Serial.println(biSizeImage);
// int size = 3 * biWidth * biHeight;
// int maxWait = 100;
// int thisWait = 0;
// moved to byteInStream
//int thisBytesRead = HEADER_SIZE;
//Serial.print("Initial bytes available: ");
//Serial.println(thisBytesAvailable);
//while (i < len) {
// thisBytesAvailable = stream->available();
// if ((i < len) && (thisBytesAvailable == 0)) {
// yield();
// thisBytesAvailable = stream->available();
// }
// if (thisBytesAvailable > 0) {
// Serial.print("Reading.... ");
// yield();
// thisResult = stream->read(data + i, thisBytesAvailable);
// i += thisResult;
// Serial.print(thisResult);
// Serial.println(" bytes. Done.");
// delay(1);
// }
//}
Serial.print("Render! totalBytesRead = ");
Serial.println(totalBytesRead);
// ***************************************************************************************************************************************************************
//
// ***************************************************************************************************************************************************************
if (biBitCount == 1) {
Serial.println("B/W not rendered");
tftPtr->println("B/W not rendered");
} // BW
// ***************************************************************************************************************************************************************
//
// ***************************************************************************************************************************************************************
else if (biBitCount == 4) {
Serial.println("4 bit not rendered");
} // 4 - bit
// ***************************************************************************************************************************************************************
//
// ***************************************************************************************************************************************************************
else if (biBitCount == 8) { // (((biWidth * biHeight) + HEADER_SIZE) == len) {
Serial.println("8 bit pic");
tftPtr->println("8-bit not rendered");
for (int i = 0; i < biHeight; i++)
{
// thisWait = 0;
yield();
//while ((stream->available() == 0) && (thisWait++ < maxWait)) {
// Serial.print(".");
// delay(10);
//}
for (int j = 0; j < biWidth; j++)
{
tftPtr->drawPixel(screenSafeX(i, startX), screenSafeY(j, startY), pImageBMP[bfOffBits + biWidth * i + j]);
}
}
} // end of 8 - bit
// ***************************************************************************************************************************************************************
// 24 bit BMP handler
// ***************************************************************************************************************************************************************
else if (biBitCount == 24) {
Serial.println("Rendering 24 bit bmpDrawFromUrl");
//int count = bfOffBits;
int count = 0; // note that we start at the START of the data [biSizeImage - count++] and work ourselves to the beginning of the data
int extra = biWidth % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
//for (int i = 0; i < biHeight; i++) // rows of data make up height
for (int i = biHeight - 1; i >= 0; i--) // rows of data make up height
{
count += extra;
yield();
uint8_t r = 0; uint8_t g = 0; uint8_t b = 0;
//for (int j = 0; j <= biWidth - 1; j++) // renders reverse image
for (int j = biWidth - 1; j >= 0; j--)
{
for (int k = 0; k < 3; k++) {
switch (k) {
case 0:
b = byteInStream(stream, count++); // r = data[len - ++count];
break;
case 1:
g = byteInStream(stream, count++); // pImageBMP[len - ++count];
break;
case 2:
r = byteInStream(stream, count++); // b = data[len - ++count];
break;
}
}
if (abortMidStream) { break; }
tftPtr->drawPixel(screenSafeX(i, startX), screenSafeY(j, startY), tftPtr->color565(r, g, b));
}
if (abortMidStream) { break; }
}
} // 24 bit
// ***************************************************************************************************************************************************************
// 32 bit BMP handler
// ***************************************************************************************************************************************************************
else if (biBitCount == 32) {
Serial.println("Rendering 32 bit bmpDrawFromUrl");
int count = bfOffBits; // note that we start at the START of the data [biSizeImag] and work ourselves to the END of the data
//int extra = 0; // The nubmer of bytes in a row (cols) will already be a multiple of 4. (32 / 8) so we don'n need to pad with extra bytes as done in 24 bit
// show the first chunk of data
//for (int j = 0; j < 66; j++) {
// for (int i = 0; i < 16; i++) {
// Serial.print(data[(j * 16) + i], HEX);
// Serial.print(" ");
// }
// Serial.println();
//}
//
// scan rows of data, starting at bottom of the image, working our way up
for (int i = 0; i < biHeight; i++) // [biHeight] rows of data make up height of our image
{
//count += extra; // we don't need to pad to 4 byte boundries, as 32 bits is already 4 bytes! (note difference from 24 bit rendering)
yield(); // give the OS a bit of time after showing each row.
uint8_t r = 0; uint8_t g = 0; uint8_t b = 0; uint8_t a = 0;
// a loop to fetch a row of data from our picture
for (int j = 0; j < biWidth; j++)
{
// a small loop to read 4 bytes in the stream; one pixel-worth of data to display
for (int k = 0; k < 4; k++) { // there are 4 bytes per pixel: BGRA
switch (k) {
case 3:
a = byteInStream(stream, count++); // data[count++]; // this is alpha, which we we simply ignore. TODO what if it is not 0xFF?
break;
case 2:
r = byteInStream(stream, count++); // red
break;
case 1:
g = byteInStream(stream, count++); // green
break;
case 0:
b = byteInStream(stream, count++); // blue
break;
}
// show the pixel data (this can REALLY slow down a rendering, particularly for full screen)
//Serial.print(count);
//Serial.print(" : ");
//Serial.print(r, HEX);
//Serial.print(" ");
//Serial.print(g, HEX);
//Serial.print(" ");
//Serial.print(b, HEX);
//Serial.print(" ");
//Serial.print(a, HEX);
//Serial.println();
}
if (abortMidStream) { break; }
// draw a single pixel of data from our byte stream
tftPtr->drawPixel(screenSafeX(i, startX), screenSafeY(j, startY), tftPtr->color565(r, g, b));
}
// Serial.println("Current index: ");
// Serial.println(len - count);
if (abortMidStream) { break; }
}
} // 32 bit
// ***************************************************************************************************************************************************************
//
// ***************************************************************************************************************************************************************
else {
Serial.print("Unsupported bit depth: ");
Serial.println(biBitCount);
tftPtr->print("Unsupported bit depth: ");
tftPtr->println(biBitCount);
}
stream->flush();
stream->stopAll();
delete pImageBMP; // once we process the data, we're done with it.
} // http file found
else
{ // show an HTTP error code
Serial.print("HTTP ");
Serial.println(httpCode);
}
}
http.end();
Serial.print(millis() - time, DEC);
Serial.println(" ms");
}
// bmpDraw - load entire image into memory, then render it
// ***************************************************************************************************************************************************************
//
// ***************************************************************************************************************************************************************
void bmpDraw(Adafruit_ILI9341 * tftPtr, char * imagePath)
{
uint32_t time = millis();
Serial.print("Starting bmpDraw 1.02: ");
Serial.print(imagePath);
Serial.print(" - ");
HTTPClient http;
http.begin(imagePath);
int httpCode = http.GET();
if (httpCode > 0) {
// HTTP header has been sent and Server response header has been handled
// file found at server
if (httpCode == HTTP_CODE_OK) {
Serial.printf("settings heap size: %u\n", ESP.getFreeHeap());
//unsigned long DrawTime = millis();
// get lenght of document (is -1 when Server sends no Content-Length header)
uint32_t len = http.getSize();
if (len < 1) {
Serial.println("Error: Bad content length.");
Serial.println();
Serial.println();
return;
}
if (len < (ESP.getFreeHeap() - ESP_MIN_HEAP)) {
pImageBMP = new unsigned char[len]; // allocation of just enough memory for this incremenal payload of data
}
else {
Serial.println("Not enough heap space while rendering!\n\r (try bmpDrawFromUrlStream, instead).");
Serial.print(ESP.getFreeHeap());
Serial.println(" heap bytes free.");
Serial.print(ESP_MIN_HEAP);
Serial.println(" minimum bytes free configured.");
Serial.print(len);
Serial.println(" bytes needed for this image.");
Serial.println();
Serial.println();
return;
}
//unsigned char tmp;
Serial.printf("After allocation heap size: %u\n", ESP.getFreeHeap());
Serial.print(" length = ");
Serial.println(len);
WiFiClient * stream = http.getStreamPtr();
Serial.println("Step 1");
int thisBytesAvailable = stream->available();
Serial.print("thisBytesAvailable = ");
Serial.print(thisBytesAvailable);
int thisResult = 0;
Serial.println("Step 2");
Serial.printf("settings heap size: %u\n", ESP.getFreeHeap());
thisResult = stream->read(pImageBMP, thisBytesAvailable);
int i = thisResult;
Serial.print("Initial Read = ");
Serial.println(i);
// const int HEADER_SIZE = 54;
// stream->read(info, HEADER_SIZE); // read the 54-byte header
// extract image height and width from header
int bfOffBits = read32(pImageBMP, 10); // *(int*)&data[10]; // typically 1078
int biSize = read32(pImageBMP , 14); // Specifies the size of the BITMAPINFOHEADER structure, in bytes. (typically 40 bytes)
int biWidth = read32(pImageBMP, 18); // *(int*)data + 18;
int biHeight = read32(pImageBMP, 22); // *(int*)data + 22;
int biBitCount = read32(pImageBMP, 28); // Specifies the number of bits per pixel.
int biSizeImage = read32(pImageBMP, 34); // *(int*)data + 34;
Serial.print("filelen = ");
Serial.println(len);
Serial.print("biSize = ");
Serial.println(biSize);
Serial.print("Offset = "); // Specifies the offset from the beginning of the file to the bitmap data.
Serial.println(bfOffBits);
Serial.print("biSize = "); // Specifies the size of the BITMAPINFOHEADER structure, in bytes.
Serial.println(biSize);
Serial.print("biWidth = ");
Serial.println(biWidth);
Serial.print("biHeight = ");
Serial.println(biHeight);
Serial.print("biBitCount = ");
Serial.println(biBitCount);
Serial.print("biSizeImage = ");
Serial.println(biSizeImage);
int size = 3 * biWidth * biHeight; // for Serial.print info only
Serial.print("computed size = ");
Serial.println(size);
// int maxWait = 100;
// int thisWait = 0;
//int thisBytesRead = HEADER_SIZE;
Serial.print("Initial bytes available: ");
Serial.println(thisBytesAvailable);
while (i < len) {
thisBytesAvailable = stream->available();
if ((i < len) && (thisBytesAvailable == 0)) {
yield();
thisBytesAvailable = stream->available();
}
if (thisBytesAvailable > 0) {
//Serial.print("Reading.... ");
yield();
thisResult = stream->read(pImageBMP + i, thisBytesAvailable);
i += thisResult;
//Serial.print(thisResult);
//Serial.println(" bytes. Done.");
//delay(1);
}
}
Serial.print("Render! i = ");
Serial.println(i);
// ***************************************************************************************************************************************************************
//
// ***************************************************************************************************************************************************************
if (biBitCount == 1) {
Serial.println("B/W not rendered");
tftPtr->println("B/W not rendered");
} // BW
// ***************************************************************************************************************************************************************
//
// ***************************************************************************************************************************************************************
else if (biBitCount == 4) {
Serial.println("4 bit not rendered");
} // 4 - bit
// ***************************************************************************************************************************************************************
//
// ***************************************************************************************************************************************************************
else if (biBitCount == 8) { // (((biWidth * biHeight) + HEADER_SIZE) == len) {
Serial.println("8 bit pic");
tftPtr->println("8-bit not rendered");
for (int i = 0; i < biHeight; i++)
{
// thisWait = 0;
yield();
//while ((stream->available() == 0) && (thisWait++ < maxWait)) {
// Serial.print(".");
// delay(10);
//}
for (int j = 0; j < biWidth; j++)
{
tftPtr->drawPixel(j, i, pImageBMP[bfOffBits + biWidth * i + j]);
}
}
} // 8 - bit
// ***************************************************************************************************************************************************************
//
// ***************************************************************************************************************************************************************
else if (biBitCount == 24) {
Serial.println("bmp24");
//int count = bfOffBits;
int count = 0; // note that we start at the END of the data [biSizeImage - count++] and work ourselves to the beginning of the data
int extra = biWidth % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
uint8_t r; uint8_t g; uint8_t b;
r = 0, g = 0, b = 0;
//for (int i = 0; i < biHeight; i++) // rows of data make up height
for (int i = biHeight -1; i >= 0; i--) // rows of data make up height
{
count += extra;
yield();
//for (int j = 0; j <= biWidth - 1; j++) // renders reverse image
for (int j = biWidth - 1; j >=0; j--)
{
for (int k = 0; k < 3; k++) {
switch (k) {
case 0:
r= pImageBMP[len - ++count];
break;
case 1:
g = pImageBMP[len - ++count];
break;
case 2:
b = pImageBMP[len - ++count];
break;
}
}
tftPtr->drawPixel(i, j, tftPtr->color565(r, g, b));
}
}
} // 24 bit
// ***************************************************************************************************************************************************************
//
// ***************************************************************************************************************************************************************
else if (biBitCount == 32) {
Serial.println("bmpDraw render 32 bit");
//int count = bfOffBits;
int count = 0; // note that we start at the END of the data [biSizeImage - count++] and work ourselves to the beginning of the data
// int extra = 0; // biWidth % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
//for (int i = 0; i < biHeight; i++) // rows of data make up height
// preint a preview of data
//for (int i = len-1; i > len - 20; i--) {
// Serial.print(pImageBMP[i], HEX);
// Serial.print(" ");
//}
//Serial.println();
for (int i = biHeight - 1; i >= 0; i--) // rows of data make up height
{
//count += extra;
yield();
uint8_t r = 0; uint8_t g = 0; uint8_t b = 0; uint8_t a = 0;
//for (int j = 0; j <= biWidth - 1; j++) // renders reverse image
for (int j = biWidth - 1; j >= 0; j--)
{
for (int k = 0; k < 4; k++) {
switch (k) {
case 0:
a = pImageBMP[len - ++count]; // this is alpha, which we we simply ignore
// if this line is commented out, don't forgot to still increment [count]
break;
case 1:
r = pImageBMP[len - ++count]; // red
break;
case 2:
g = pImageBMP[len - ++count]; // green
break;
case 3:
b = pImageBMP[len - ++count]; // blue
break;
}
//Serial.print(r, HEX);
//Serial.print(" ");
//Serial.print(g, HEX);
//Serial.print(" ");
//Serial.print(b, HEX);
//Serial.print(" ");
//Serial.print(a, HEX);
//Serial.println();
}
tftPtr->drawPixel(i, j, tftPtr->color565(r, g, b)); // show the pixel!
}
}
} // 32 bit
else {
Serial.print("Unsupported bit depth: ");
Serial.println(biBitCount);
tftPtr->print("Unsupported bit depth: ");
tftPtr->println(biBitCount);
}
stream->flush();
stream->stopAll();
delete pImageBMP;
} // if httpcode = 200
else {
Serial.print("HTTP ");
Serial.println(httpCode);
}
}
Serial.print(millis() - time, DEC);
Serial.println(" ms");
http.end();
}
//void bmpDraw2(Adafruit_ILI9341 * tftPtr, char * imagePath)
//{
// HTTPClient http;
// http.begin(imagePath);
//
// Serial.print("Starting bmpDraw: ");
// Serial.print(imagePath);
// Serial.print(" - ");
// uint32_t time = millis();
// int httpCode = http.GET();
// Serial.println(httpCode);
// if (httpCode > 0) {
// // HTTP header has been send and Server response header has been handled
// // file found at server
// if (httpCode == HTTP_CODE_OK) {
// unsigned long DrawTime = millis();
// // get lenght of document (is -1 when Server sends no Content-Length header)
// int len = http.getSize();
// Serial.print(" length = ");
// Serial.println(len);
// WiFiClient * stream = http.getStreamPtr();
// bmpReadHeader(stream);
// for (int i = 61; i >= 0; i--)
// {
// //Serial.println("i"); delay(100);
// for (int j = 0; j<137; j++)
// {
// //Serial.println("j"); delay(100);
// size_t size = stream->available();
// //Serial.println("size=" + String(size)); yield(); delay(1);
// if (size) {