/
jswrap_graphics.c
4618 lines (4352 loc) · 162 KB
/
jswrap_graphics.c
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
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* This file is designed to be parsed during the build process
*
* Contains JavaScript Graphics Draw Functions
* ----------------------------------------------------------------------------
*/
#include "jswrap_graphics.h"
#include "jswrap_math.h" // for jswrap_math_cos/sin
#include "jsutils.h"
#include "jsinteractive.h"
#include "lcd_arraybuffer.h"
#include "lcd_js.h"
#ifdef USE_LCD_SDL
#include "lcd_sdl.h"
#endif
#ifdef USE_LCD_FSMC
#include "lcd_fsmc.h"
#endif
#ifdef USE_LCD_ST7789_8BIT
#include "lcd_st7789_8bit.h"
#endif
#include "jswrap_functions.h" // for asURL
#include "jswrap_object.h" // for getFonts
#ifdef DICKENS
#include "jsflash.h" // for saveScreenshot
#include "lcd_spilcd.h"
#define ESPR_LINE_FONTS
#endif
#include "bitmap_font_4x6.h"
#include "bitmap_font_6x8.h"
#include "vector_font.h"
#ifdef ESPR_PBF_FONTS
#include "pbf_font.h"
#endif
#ifdef ESPR_LINE_FONTS
#include "line_font.h"
#endif
#ifdef GRAPHICS_PALETTED_IMAGES
#if defined(ESPR_GRAPHICS_12BIT)
#define PALETTE_BPP 12
// 8 color RGB - [0,0,0,0,0,0,0,0].map((x,i)=>((i&4)?0xF00:0)|((i&2)?0xF0:0)|((i&1)?0xF:0))
const uint16_t PALETTE_3BIT[16] = { 0, 15, 240, 255, 3840, 3855, 4080, 4095 };
// 16 color 12 bit MAC OS palette
const uint16_t PALETTE_4BIT[16] = { 0x0,0x444,0x888,0xbbb,0x963,0x630,0x60,0xa0,0x9f,0xc,0x309,0xf09,0xd00,0xf60,0xff0,0xfff };
// 256 color 12 bit Web-safe palette
const uint16_t PALETTE_8BIT[256] = { 0x0,0x3,0x6,0x9,0xc,0xf,0x30,0x33,0x36,0x39,0x3c,0x3f,0x60,0x63,0x66,0x69,0x6c,0x6f,0x90,0x93,0x96,0x99,0x9c,0x9f,0xc0,0xc3,0xc6,0xc9,0xcc,0xcf,0xf0,0xf3,0xf6,0xf9,0xfc,0xff,0x300,0x303,0x306,0x309,0x30c,0x30f,0x330,0x333,0x336,0x339,0x33c,0x33f,0x360,0x363,0x366,0x369,0x36c,0x36f,0x390,0x393,0x396,0x399,0x39c,0x39f,0x3c0,0x3c3,0x3c6,0x3c9,0x3cc,0x3cf,0x3f0,0x3f3,0x3f6,0x3f9,0x3fc,0x3ff,0x600,0x603,0x606,0x609,0x60c,0x60f,0x630,0x633,0x636,0x639,0x63c,0x63f,0x660,0x663,0x666,0x669,0x66c,0x66f,0x690,0x693,0x696,0x699,0x69c,0x69f,0x6c0,0x6c3,0x6c6,0x6c9,0x6cc,0x6cf,0x6f0,0x6f3,0x6f6,0x6f9,0x6fc,0x6ff,0x900,0x903,0x906,0x909,0x90c,0x90f,0x930,0x933,0x936,0x939,0x93c,0x93f,0x960,0x963,0x966,0x969,0x96c,0x96f,0x990,0x993,0x996,0x999,0x99c,0x99f,0x9c0,0x9c3,0x9c6,0x9c9,0x9cc,0x9cf,0x9f0,0x9f3,0x9f6,0x9f9,0x9fc,0x9ff,0xc00,0xc03,0xc06,0xc09,0xc0c,0xc0f,0xc30,0xc33,0xc36,0xc39,0xc3c,0xc3f,0xc60,0xc63,0xc66,0xc69,0xc6c,0xc6f,0xc90,0xc93,0xc96,0xc99,0xc9c,0xc9f,0xcc0,0xcc3,0xcc6,0xcc9,0xccc,0xccf,0xcf0,0xcf3,0xcf6,0xcf9,0xcfc,0xcff,0xf00,0xf03,0xf06,0xf09,0xf0c,0xf0f,0xf30,0xf33,0xf36,0xf39,0xf3c,0xf3f,0xf60,0xf63,0xf66,0xf69,0xf6c,0xf6f,0xf90,0xf93,0xf96,0xf99,0xf9c,0xf9f,0xfc0,0xfc3,0xfc6,0xfc9,0xfcc,0xfcf,0xff0,0xff3,0xff6,0xff9,0xffc,0xfff,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xfff };
#else // default to 16 bit
#define PALETTE_BPP 16
// 8 color RGB - [0,0,0,0,0,0,0,0].map((x,i)=>((i&4)?0xF800:0)|((i&2)?0x7E0:0)|((i&1)?0x1F:0))
const uint16_t PALETTE_3BIT[16] = { 0, 31, 2016, 2047, 63488, 63519, 65504, 65535 };
// 16 color MAC OS palette
#if LCD_BPP==3 // for 3bpp LCDs we use a more saturated palette that maps better to the less vibrant LCD
const uint16_t PALETTE_4BIT[16] = { 0x0,0x4228,0x8c51,0xbdd7,0xcc40,0x8a20,0x320,0x7e0,0x7ff,0x1f,0x8811,0xf81f,0xf800,0xfc40,0xffe0,0xffff };
#else // otherwise we use the standard Mac pallete
const uint16_t PALETTE_4BIT[16] = { 0x0,0x4228,0x8c51,0xbdd7,0x9b26,0x6180,0x320,0x540,0x4df,0x19,0x3013,0xf813,0xd800,0xfb20,0xffe0,0xffff };
#endif
// 256 color 16 bit Web-safe palette
const uint16_t PALETTE_8BIT[256] = {
0x0,0x6,0xc,0x13,0x19,0x1f,0x180,0x186,0x18c,0x193,0x199,0x19f,0x320,0x326,0x32c,0x333,0x339,0x33f,0x4c0,
0x4c6,0x4cc,0x4d3,0x4d9,0x4df,0x660,0x666,0x66c,0x673,0x679,0x67f,0x7e0,0x7e6,0x7ec,0x7f3,0x7f9,0x7ff,
0x3000,0x3006,0x300c,0x3013,0x3019,0x301f,0x3180,0x3186,0x318c,0x3193,0x3199,0x319f,0x3320,0x3326,0x332c,
0x3333,0x3339,0x333f,0x34c0,0x34c6,0x34cc,0x34d3,0x34d9,0x34df,0x3660,0x3666,0x366c,0x3673,0x3679,0x367f,
0x37e0,0x37e6,0x37ec,0x37f3,0x37f9,0x37ff,0x6000,0x6006,0x600c,0x6013,0x6019,0x601f,0x6180,0x6186,0x618c,
0x6193,0x6199,0x619f,0x6320,0x6326,0x632c,0x6333,0x6339,0x633f,0x64c0,0x64c6,0x64cc,0x64d3,0x64d9,0x64df,
0x6660,0x6666,0x666c,0x6673,0x6679,0x667f,0x67e0,0x67e6,0x67ec,0x67f3,0x67f9,0x67ff,0x9800,0x9806,0x980c,
0x9813,0x9819,0x981f,0x9980,0x9986,0x998c,0x9993,0x9999,0x999f,0x9b20,0x9b26,0x9b2c,0x9b33,0x9b39,0x9b3f,
0x9cc0,0x9cc6,0x9ccc,0x9cd3,0x9cd9,0x9cdf,0x9e60,0x9e66,0x9e6c,0x9e73,0x9e79,0x9e7f,0x9fe0,0x9fe6,0x9fec,
0x9ff3,0x9ff9,0x9fff,0xc800,0xc806,0xc80c,0xc813,0xc819,0xc81f,0xc980,0xc986,0xc98c,0xc993,0xc999,0xc99f,
0xcb20,0xcb26,0xcb2c,0xcb33,0xcb39,0xcb3f,0xccc0,0xccc6,0xcccc,0xccd3,0xccd9,0xccdf,0xce60,0xce66,0xce6c,
0xce73,0xce79,0xce7f,0xcfe0,0xcfe6,0xcfec,0xcff3,0xcff9,0xcfff,0xf800,0xf806,0xf80c,0xf813,0xf819,0xf81f,
0xf980,0xf986,0xf98c,0xf993,0xf999,0xf99f,0xfb20,0xfb26,0xfb2c,0xfb33,0xfb39,0xfb3f,0xfcc0,0xfcc6,0xfccc,
0xfcd3,0xfcd9,0xfcdf,0xfe60,0xfe66,0xfe6c,0xfe73,0xfe79,0xfe7f,0xffe0,0xffe6,0xffec,0xfff3,0xfff9,0xffff,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xffff
};
// convert 16 bit to 8 bit with
// (c=>[(c&0xF800)>>8,(c&0x7E0)>>3,(c&0x1F)<<3])(0x0661)
// 16 bit to hex with
// (c=>"#"+[(c&0xF800)>>8,(c&0x7E0)>>3,(c&0x1F)<<3].map(c=>((c+8)>>4).toString(16)).join(""))(0x0661)
#endif
// map Mac to web-safe palette. Must be uint16_t because drawImage uses uint16_t* for palette
const uint16_t PALETTE_4BIT_TO_8BIT[16] = { 0, 43, 129, 172, 121, 78, 12, 18, 23, 4, 39, 183, 144, 192, 210, 215 };
// g2=Graphics.createArrayBuffer(8,8,8);print((new Uint8Array(8)).map((n,i)=>g2.toColor((i&4)>>2,(i&2)>>1,i&1)).join(","));
// map Mac to web-safe palette. Must be uint16_t because drawImage uses uint16_t* for palette
const uint16_t PALETTE_3BIT_TO_8BIT[8] = { 0,5,30,35,180,185,210,215 };
// g2=Graphics.createArrayBuffer(8,8,4);print((new Uint8Array(8)).map((n,i)=>g2.toColor((i&4)>>2,(i&2)>>1,i&1)).join(","));
// map Mac to web-safe palette. Must be uint16_t because drawImage uses uint16_t* for palette
const uint16_t PALETTE_3BIT_TO_4BIT[8] = { 0,9,7,8,12,11,14,15 };
#endif
// ==========================================================================================
void _jswrap_graphics_freeImageInfo(GfxDrawImageInfo *info) {
jsvUnLock(info->buffer);
}
/** Parse an image into GfxDrawImageInfo. See drawImage for image format docs. Returns true on success.
* if 'image' is a string or ArrayBuffer, imageOffset is the offset within that (usually 0)
*/
bool _jswrap_graphics_parseImage(JsGraphics *gfx, JsVar *image, size_t imageOffset, GfxDrawImageInfo *info) {
memset(info, 0, sizeof(GfxDrawImageInfo));
if (jsvIsObject(image)) {
#ifndef SAVE_ON_FLASH
if (jsvIsInstanceOf(image,"Graphics")) { // handle a Graphics instance
JsGraphics ig;
if (!graphicsGetFromVar(&ig, image)) return false;
if (ig.data.type!=JSGRAPHICSTYPE_ARRAYBUFFER) return false; // if not arraybuffer Graphics, bail out
info->width = ig.data.width;
info->height = ig.data.height;
info->bpp = ig.data.bpp;
} else
#endif
{ // Normal image object
info->width = (int)jsvObjectGetIntegerChild(image, "width");
info->height = (int)jsvObjectGetIntegerChild(image, "height");
info->bpp = (int)jsvObjectGetIntegerChild(image, "bpp");
if (info->bpp<=0) info->bpp=1;
}
// Get the buffer for image data - this is the same in Graphics and Image objects
JsVar *buf = jsvObjectGetChildIfExists(image, "buffer");
info->buffer = jsvGetArrayBufferBackingString(buf, &info->bitmapOffset);
jsvUnLock(buf);
info->bitmapOffset += (uint32_t)imageOffset;
JsVar *v;
v = jsvObjectGetChildIfExists(image, "transparent");
info->isTransparent = v!=0;
info->transparentCol = (unsigned int)jsvGetIntegerAndUnLock(v);
#ifndef SAVE_ON_FLASH_EXTREME
v = jsvObjectGetChildIfExists(image, "palette");
if (v) {
if (jsvIsArrayBuffer(v) && v->varData.arraybuffer.type==ARRAYBUFFERVIEW_UINT16) {
size_t l = 0;
info->palettePtr = (uint16_t *)jsvGetDataPointer(v, &l);
jsvUnLock(v);
if (l==2 || l==4 || l==8 || l==16 || l==256) {
info->paletteMask = (uint32_t)(l-1);
} else {
info->palettePtr = 0;
}
} else
jsvUnLock(v);
if (!info->palettePtr) {
jsExceptionHere(JSET_ERROR, "Palette specified, but must be a flat Uint16Array of 2,4,8,16,256 elements");
return false;
}
}
#endif
} else if (jsvIsString(image) || jsvIsArrayBuffer(image)) {
if (jsvIsArrayBuffer(image)) {
info->buffer = jsvGetArrayBufferBackingString(image, &info->bitmapOffset);
} else {
info->buffer = jsvLockAgain(image);
}
JsvStringIterator it;
jsvStringIteratorNewUTF8(&it, info->buffer, imageOffset);
info->width = jsvStringIteratorGetUTF8CharAndNext(&it);
info->height = jsvStringIteratorGetUTF8CharAndNext(&it);
info->bpp = jsvStringIteratorGetUTF8CharAndNext(&it);
info->bitmapOffset += (uint32_t)imageOffset;
if (info->bpp & 128) {
info->bpp = info->bpp&127;
info->isTransparent = true;
info->transparentCol = (unsigned int)jsvStringIteratorGetUTF8CharAndNext(&it);
info->headerLength = 4;
} else {
info->headerLength = 3;
}
info->bitmapOffset += info->headerLength;
if (info->bpp & 64) { // included palette data
info->bpp = info->bpp&63;
#ifndef SAVE_ON_FLASH_EXTREME
if (info->bpp > 8) {
jsExceptionHere(JSET_ERROR, "Can't have palette on >8 bit images");
_jswrap_graphics_freeImageInfo(info);
jsvStringIteratorFree(&it);
return false;
}
unsigned int paletteEntries = 1<<info->bpp;
info->paletteMask = (uint32_t)paletteEntries-1;
if (paletteEntries*2 <= (int)sizeof(info->_simplePalette)) {
// if it'll fit, put the palette data in _simplePalette
uint32_t n = info->bitmapOffset;
for (unsigned int i=0;i<paletteEntries;i++) {
info->_simplePalette[i] =
((unsigned char)jsvStringIteratorGetUTF8CharAndNext(&it)) |
((unsigned char)jsvStringIteratorGetUTF8CharAndNext(&it))<<8;
n+=2;
}
info->palettePtr = info->_simplePalette;
} else if (info->bpp<=8) { // otherwise if data is memory-mapped, use direct
unsigned int imgStart = info->bitmapOffset + paletteEntries*2;
size_t dataLen = 0;
char *dataPtr = jsvGetDataPointer(info->buffer, &dataLen);
if (info->bpp<=8 && dataPtr && imgStart<dataLen) {
info->paletteMask = (uint32_t)(paletteEntries-1);
info->palettePtr = (uint16_t*)&dataPtr[info->bitmapOffset+info->headerLength];
}
}
// could allocate a flat string and copy data in here
if (!info->palettePtr) {
jsExceptionHere(JSET_ERROR, "Unable to get pointer to palette. Image in flash?");
_jswrap_graphics_freeImageInfo(info);
jsvStringIteratorFree(&it);
return false;
}
// modify image start
info->headerLength += (unsigned short)(paletteEntries*2);
info->bitmapOffset += (unsigned short)(paletteEntries*2);
#else
jsExceptionHere(JSET_ERROR, "Image Palette not supported on this build");
#endif
}
jsvStringIteratorFree(&it);
} else {
jsExceptionHere(JSET_ERROR, "First argument must be Object or String");
return 0;
}
if (!info->isTransparent)
info->transparentCol = 0xFFFFFFFF;
if (info->palettePtr==0) {
if (info->bpp==1) {
info->_simplePalette[0] = (uint16_t)gfx->data.bgColor;
info->_simplePalette[1] = (uint16_t)gfx->data.fgColor;
info->palettePtr = info->_simplePalette;
info->paletteMask = 1;
#ifdef GRAPHICS_PALETTED_IMAGES
} else if (info->bpp==2) { // Blend from bg to fg
info->_simplePalette[0] = (uint16_t)gfx->data.bgColor;
info->_simplePalette[1] = (uint16_t)graphicsBlendGfxColor(gfx, 85);
info->_simplePalette[2] = (uint16_t)graphicsBlendGfxColor(gfx, 171);
info->_simplePalette[3] = (uint16_t)gfx->data.fgColor;
info->palettePtr = info->_simplePalette;
info->paletteMask = 3;
} else if (gfx->data.bpp==PALETTE_BPP && info->bpp==3) { // palette is 16 bits, so don't use it for other things
info->palettePtr = PALETTE_3BIT;
info->paletteMask = 7;
} else if (gfx->data.bpp==PALETTE_BPP && info->bpp==4) { // palette is 16 bits, so don't use it for other things
info->palettePtr = PALETTE_4BIT;
info->paletteMask = 15;
} else if (gfx->data.bpp==PALETTE_BPP && info->bpp==8) { // palette is 16 bits, so don't use it for other things
info->palettePtr = PALETTE_8BIT;
info->paletteMask = 255;
} else if (gfx->data.bpp==8 && info->bpp==4) {
info->palettePtr = PALETTE_4BIT_TO_8BIT;
info->paletteMask = 15;
} else if (gfx->data.bpp==8 && info->bpp==3) {
info->palettePtr = PALETTE_3BIT_TO_8BIT;
info->paletteMask = 7;
} else if (gfx->data.bpp==4 && info->bpp==3) {
info->palettePtr = PALETTE_3BIT_TO_4BIT;
info->paletteMask = 7;
#endif
}
}
if ((!jsvIsString(info->buffer)) ||
info->width<=0 ||
info->height<=0 ||
info->bpp>32) {
jsExceptionHere(JSET_ERROR, "Expecting valid Image");
_jswrap_graphics_freeImageInfo(info);
return false;
}
info->bitMask = (unsigned int)((1L<<info->bpp)-1L);
info->pixelsPerByteMask = (unsigned int)((info->bpp<8)?(8/info->bpp)-1:0);
info->stride = (info->width*info->bpp + 7)>>3;
info->bitmapLength = (unsigned short)((info->width*info->height*info->bpp + 7)>>3);
return true;
}
bool _jswrap_drawImageLayerGetPixel(GfxDrawImageLayer *l, uint32_t *result) {
int qx = l->qx+127;
int qy = l->qy+127;
if (qx>=0 && qy>=0 && qx<l->mx && qy<l->my) {
unsigned int colData = 0;
int imagex = qx>>8;
int imagey = qy>>8;
// TODO: getter callback for speed?
#ifndef SAVE_ON_FLASH
if (l->img.bpp==8) { // fast path for 8 bits
jsvStringIteratorGoto(&l->it, l->img.buffer, (size_t)((int)l->img.bitmapOffset+imagex+(imagey*l->img.stride)));
colData = (unsigned char)jsvStringIteratorGetChar(&l->it);
} else
#endif
{
int pixelOffset = (imagex+(imagey*l->img.width));
int bitOffset = pixelOffset*l->img.bpp;
jsvStringIteratorGoto(&l->it, l->img.buffer, (size_t)((int)l->img.bitmapOffset+(bitOffset>>3)));
bitOffset &= 7; // so now it's bits within a byte
// get first byte
colData = (unsigned char)jsvStringIteratorGetChar(&l->it);
// it may not fit within a byte, so if so grab more data
int b;
for (b=8-(bitOffset+l->img.bpp);b<0;b+=8) {
jsvStringIteratorNext(&l->it);
colData = (colData<<8) | (unsigned char)jsvStringIteratorGetChar(&l->it);
}
// finally shift down to the right size
colData = (colData>>b) & l->img.bitMask;
}
if (l->img.transparentCol!=colData) {
if (l->img.palettePtr) colData = l->img.palettePtr[colData&l->img.paletteMask];
*result = colData;
return true;
}
}
return false;
}
NO_INLINE void _jswrap_drawImageLayerInit(GfxDrawImageLayer *l) {
// image max
l->mx = l->img.width<<8;
l->my = l->img.height<<8;
// step values for blitting rotated image
double vcos = jswrap_math_cos(l->rotate);
double vsin = jswrap_math_sin(l->rotate);
l->sx = (int)((vcos/l->scale)*256 + 0.5);
l->sy = (int)((vsin/l->scale)*256 + 0.5);
// work out actual image width and height
int iw = (int)(0.5 + l->scale*(l->img.width*fabs(vcos) + l->img.height*fabs(vsin)));
int ih = (int)(0.5 + l->scale*(l->img.width*fabs(vsin) + l->img.height*fabs(vcos)));
// if rotating, offset our start position from center
if (l->center) {
l->x1 -= iw/2;
l->y1 -= ih/2;
}
l->x2 = l->x1 + iw;
l->y2 = l->y1 + ih;
// work out start position in the image
int centerx = l->img.width*128;
int centery = l->img.height*128;
l->px = centerx - (1 + (l->sx*iw) + (l->sy*ih)) / 2;
l->py = centery - (1 + (l->sx*ih) - (l->sy*iw)) / 2;
// handle repetition
if (l->repeat) {
// for the range we're in, it's quicker/easier than modulo
while (l->px < 0) l->px += l->mx;
while (l->px >= l->mx) l->px -= l->mx;
while (l->py < 0) l->py += l->my;
while (l->py >= l->my) l->py -= l->my;
}
}
NO_INLINE void _jswrap_drawImageLayerSetStart(GfxDrawImageLayer *l, int x, int y) {
int dx = x - l->x1;
int dy = y - l->y1;
l->px += l->sx*dx + l->sy*dy;
l->py += l->sx*dy - l->sy*dx;
}
NO_INLINE void _jswrap_drawImageLayerStartX(GfxDrawImageLayer *l) {
l->qx = l->px;
l->qy = l->py;
}
ALWAYS_INLINE void _jswrap_drawImageLayerNextX(GfxDrawImageLayer *l) {
l->qx += l->sx;
l->qy -= l->sy;
}
// Handle repeats
ALWAYS_INLINE void _jswrap_drawImageLayerNextXRepeat(GfxDrawImageLayer *l) {
if (l->repeat) {
// for the range we're in, it's quicker/easier than modulo
if (l->qx < 0) l->qx += l->mx;
if (l->qx >= l->mx) l->qx -= l->mx;
if (l->qy < 0) l->qy += l->my;
if (l->qy >= l->my) l->qy -= l->my;
}
}
NO_INLINE void _jswrap_drawImageLayerNextY(GfxDrawImageLayer *l) {
l->px += l->sy;
l->py += l->sx;
if (l->repeat) {
// for the range we're in, it's quicker/easier than modulo
if (l->px < 0) l->px += l->mx;
if (l->px >= l->mx) l->px -= l->mx;
if (l->py < 0) l->py += l->my;
if (l->py >= l->my) l->py -= l->my;
}
}
/* Draw an image 1:1 at xPos,yPos. If parseFullImage=true we ensure
we leave the StringIterator pointing right at the end of the image. If not
we can optimise if the image is clipped/offscreen. */
NO_INLINE void _jswrap_drawImageSimple(JsGraphics *gfx, int xPos, int yPos, GfxDrawImageInfo *img, JsvStringIterator *it, bool parseFullImage) {
int bits=0;
uint32_t colData=0;
int x1 = xPos, y1 = yPos, x2 = xPos+img->width-1, y2 = yPos+img->height-1;
if (!jsvStringIteratorHasChar(it)) return; // no data
#ifndef SAVE_ON_FLASH
graphicsSetModifiedAndClip(gfx,&x1,&y1,&x2,&y2, true); // ensure we clip Y, coords were already rotated
/* force a skip forward as many bytes as we need. Ideally we would use
jsvStringIteratorGotoUTF8 but we don't have the UTF8 index or
source string here. This is still better than trying to render every pixel! */
if (y2<y1 || x2<x1) { // offscreen - skip everything and exit
if (parseFullImage) {
bits = -img->bpp*img->width*img->height;
while (bits < 0) {
jsvStringIteratorNextUTF8(it);
bits += 8;
}
}
return;
} else // onscreen. y1!=yPos if clipped - ensure we skip enough bytes
bits = -(y1-yPos)*img->bpp*img->width;
#endif
JsGraphicsSetPixelFn setPixel = graphicsGetSetPixelUnclippedFn(gfx, xPos, y1, xPos+img->width-1, y2, true);
for (int y=y1;y<=y2;y++) {
for (int x=xPos;x<xPos+img->width;x++) {
// Get the data we need...
while (bits < img->bpp) {
colData = (colData<<8) | ((unsigned char)jsvStringIteratorGetUTF8CharAndNext(it));
bits += 8;
}
// extract just the bits we want
unsigned int col = (colData>>(bits-img->bpp))&img->bitMask;
bits -= img->bpp;
// Try and write pixel!
if (img->transparentCol!=col) {
if (img->palettePtr) col = img->palettePtr[col&img->paletteMask];
setPixel(gfx, x, y, col);
}
}
}
#ifndef SAVE_ON_FLASH
if (parseFullImage) {
/* If we didn't render the last bit of the image, and the caller needs
the StringIterator to point to the end of the image, skip forward */
bits += img->bpp-(yPos+img->height-(1+y2))*img->bpp*img->width;
while (bits < 0) {
jsvStringIteratorNextUTF8(it);
bits += 8;
}
}
#endif
}
// ==========================================================================================
/*JSON{
"type" : "class",
"class" : "Graphics",
"typescript" : "Graphics<IsBuffer extends boolean = boolean>"
}
This class provides Graphics operations that can be applied to a surface.
Use Graphics.createXXX to create a graphics object that renders in the way you
want. See [the Graphics page](https://www.espruino.com/Graphics) for more
information.
**Note:** On boards that contain an LCD, there is a built-in `g` object of
type `Graphics`. For instance to draw a line you'd type:
```g.drawLine(0,0,100,100)```
*/
/*JSON{
"type" : "method",
"class" : "Graphics",
"params" : [ ["all","bool","[optional] (only on some devices) If `true` then copy all pixels, not just those that have changed."] ],
"name" : "flip"
}
On instances of graphics that drive a display with an offscreen buffer, calling
this function will copy the contents of the offscreen buffer to the screen.
Call this when you have drawn something to Graphics and you want it shown on the
screen.
If a display does not have an offscreen buffer, it may not have a `g.flip()`
method.
On Bangle.js 1, there are different graphics modes chosen with
`Bangle.setLCDMode()`. The default mode is unbuffered and in this mode
`g.flip()` does not affect the screen contents.
On some devices, this command will attempt to only update the areas of the
screen that have changed in order to increase speed. If you have accessed the
`Graphics.buffer` directly then you may need to use `Graphics.flip(true)` to
force a full update of the screen.
*/
/*JSON{
"type" : "property",
"class" : "Graphics",
"name" : "buffer",
"return" : ["JsVar","An ArrayBuffer (or not defined on Graphics instances not created with `Graphics.createArrayBuffer`)"],
"typescript" : "buffer: IsBuffer extends true ? ArrayBuffer : undefined"
}
On Graphics instances with an offscreen buffer, this is an `ArrayBuffer` that
provides access to the underlying pixel data.
```
g=Graphics.createArrayBuffer(8,8,8)
g.drawLine(0,0,7,7)
print(new Uint8Array(g.buffer))
new Uint8Array([
255, 0, 0, 0, 0, 0, 0, 0,
0, 255, 0, 0, 0, 0, 0, 0,
0, 0, 255, 0, 0, 0, 0, 0,
0, 0, 0, 255, 0, 0, 0, 0,
0, 0, 0, 0, 255, 0, 0, 0,
0, 0, 0, 0, 0, 255, 0, 0,
0, 0, 0, 0, 0, 0, 255, 0,
0, 0, 0, 0, 0, 0, 0, 255])
```
*/
/*JSON{
"type" : "idle",
"generate" : "jswrap_graphics_idle"
}*/
bool jswrap_graphics_idle() {
graphicsIdle();
return false;
}
/*JSON{
"type" : "init",
"generate" : "jswrap_graphics_init",
"sortorder" : -100
}*/
void jswrap_graphics_init() {
// sortorder is first because we don't want subsequent
// _init to a) not have GFX and b) not get their theme
// settings overwritten
#ifdef USE_LCD_FSMC
JsVar *parent = jspNewObject("g", "Graphics");
if (parent) {
JsVar *parentObj = jsvSkipName(parent);
jsvObjectSetChild(execInfo.hiddenRoot, JS_GRAPHICS_VAR, parentObj);
JsGraphics gfx;
gfx.data.type = JSGRAPHICSTYPE_FSMC;
graphicsStructInit(&gfx,320,240,16);
gfx.graphicsVar = parentObj;
lcdInit_FSMC(&gfx);
lcdSetCallbacks_FSMC(&gfx);
graphicsSplash(&gfx);
graphicsSetVarInitial(&gfx);
jsvUnLock2(parentObj, parent);
}
#endif
#ifdef GRAPHICS_THEME
/// Global color scheme colours
graphicsTheme.fg = (JsGraphicsThemeColor)-1;
graphicsTheme.bg = (JsGraphicsThemeColor)0;
graphicsTheme.fg2 = (JsGraphicsThemeColor)-1;
graphicsTheme.bg2 = (JsGraphicsThemeColor)0;
graphicsTheme.fgH = (JsGraphicsThemeColor)-1;
graphicsTheme.bgH = (JsGraphicsThemeColor)0;
graphicsTheme.dark = true;
#endif
}
/*JSON{
"type" : "staticmethod",
"class" : "Graphics",
"name" : "getInstance",
"generate" : "jswrap_graphics_getInstance",
"return" : ["JsVar","An instance of `Graphics` or undefined"],
"typescript" : "getInstance(): Graphics | undefined"
}
On devices like Pixl.js or HYSTM boards that contain a built-in display this
will return an instance of the graphics class that can be used to access that
display.
Internally, this is stored as a member called `gfx` inside the 'hiddenRoot'.
*/
JsVar *jswrap_graphics_getInstance() {
return jsvObjectGetChildIfExists(execInfo.hiddenRoot, JS_GRAPHICS_VAR);
}
static bool isValidBPP(int bpp) {
return bpp==1 || bpp==2 || bpp==4 || bpp==8 || bpp==16 || bpp==24 || bpp==32; // currently one colour can't ever be spread across multiple bytes
}
/*JSON{
"type" : "staticmethod",
"class" : "Graphics",
"name" : "createArrayBuffer",
"generate" : "jswrap_graphics_createArrayBuffer",
"params" : [
["width","int32","Pixels wide"],
["height","int32","Pixels high"],
["bpp","int32","Number of bits per pixel"],
["options","JsVar",[
"An object of other options. `{ zigzag : true/false(default), vertical_byte : true/false(default), msb : true/false(default), color_order: 'rgb'(default),'bgr',etc }`",
"`zigzag` = whether to alternate the direction of scanlines for rows",
"`vertical_byte` = whether to align bits in a byte vertically or not",
"`msb` = when bits<8, store pixels most significant bit first, when bits>8, store most significant byte first",
"`interleavex` = Pixels 0,2,4,etc are from the top half of the image, 1,3,5,etc from the bottom half. Used for P3 LED panels.",
"`color_order` = re-orders the colour values that are supplied via setColor"
]]
],
"return" : ["JsVar","The new Graphics object"],
"return_object" : "Graphics",
"typescript" : "createArrayBuffer(width: number, height: number, bpp: number, options?: { zigzag?: boolean, vertical_byte?: boolean, msb?: boolean, color_order?: \"rgb\" | \"rbg\" | \"brg\" | \"bgr\" | \"grb\" | \"gbr\" }): Graphics<true>;"
}
Create a Graphics object that renders to an Array Buffer. This will have a field
called 'buffer' that can get used to get at the buffer itself
*/
JsVar *jswrap_graphics_createArrayBuffer(int width, int height, int bpp, JsVar *options) {
if (width<=0 || height<=0 || width>32767 || height>32767) {
jsExceptionHere(JSET_ERROR, "Invalid Size");
return 0;
}
if (!isValidBPP(bpp)) {
jsExceptionHere(JSET_ERROR, "Invalid BPP");
return 0;
}
JsVar *parent = jspNewObject(0, "Graphics");
if (!parent) return 0; // low memory
JsGraphics gfx;
gfx.data.type = JSGRAPHICSTYPE_ARRAYBUFFER;
graphicsStructInit(&gfx,width,height,bpp);
gfx.data.flags = JSGRAPHICSFLAGS_NONE;
gfx.graphicsVar = parent;
if (jsvIsObject(options)) {
if (jsvObjectGetBoolChild(options, "zigzag"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_ARRAYBUFFER_ZIGZAG);
if (jsvObjectGetBoolChild(options, "msb"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_ARRAYBUFFER_MSB);
if (jsvObjectGetBoolChild(options, "interleavex"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_ARRAYBUFFER_INTERLEAVEX);
if (jsvObjectGetBoolChild(options, "vertical_byte")) {
if (gfx.data.bpp==1)
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_ARRAYBUFFER_VERTICAL_BYTE);
else {
jsExceptionHere(JSET_ERROR, "vertical_byte only works for 1bpp ArrayBuffers");
return 0;
}
if (gfx.data.height&7) {
jsExceptionHere(JSET_ERROR, "height must be a multiple of 8 when using vertical_byte");
return 0;
}
}
JsVar *colorv = jsvObjectGetChildIfExists(options, "color_order");
if (colorv) {
if (jsvIsStringEqual(colorv, "rgb")) ; // The default
else if (!jsvIsStringEqual(colorv, "brg"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_COLOR_BRG);
else if (!jsvIsStringEqual(colorv, "bgr"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_COLOR_BGR);
else if (!jsvIsStringEqual(colorv, "gbr"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_COLOR_GBR);
else if (!jsvIsStringEqual(colorv, "grb"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_COLOR_GRB);
else if (!jsvIsStringEqual(colorv, "rbg"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_COLOR_RBG);
else
jsWarn("color_order must be 3 characters");
jsvUnLock(colorv);
}
}
lcdInit_ArrayBuffer(&gfx);
graphicsSetVarInitial(&gfx);
return parent;
}
/*JSON{
"type" : "staticmethod",
"class" : "Graphics",
"name" : "createCallback",
"ifndef" : "SAVE_ON_FLASH",
"generate" : "jswrap_graphics_createCallback",
"params" : [
["width","int32","Pixels wide"],
["height","int32","Pixels high"],
["bpp","int32","Number of bits per pixel"],
["callback","JsVar","A function of the form ```function(x,y,col)``` that is called whenever a pixel needs to be drawn, or an object with: ```{setPixel:function(x,y,col),fillRect:function(x1,y1,x2,y2,col)}```. All arguments are already bounds checked."]
],
"return" : ["JsVar","The new Graphics object"],
"return_object" : "Graphics",
"typescript" : "createCallback(width: number, height: number, bpp: number, callback: ((x: number, y: number, col: number) => void) | { setPixel: (x: number, y: number, col: number) => void; fillRect: (x1: number, y1: number, x2: number, y2: number, col: number) => void }): Graphics<false>;"
}
Create a Graphics object that renders by calling a JavaScript callback function
to draw pixels
*/
JsVar *jswrap_graphics_createCallback(int width, int height, int bpp, JsVar *callback) {
if (width<=0 || height<=0 || width>32767 || height>32767) {
jsExceptionHere(JSET_ERROR, "Invalid Size");
return 0;
}
if (!isValidBPP(bpp)) {
jsExceptionHere(JSET_ERROR, "Invalid BPP");
return 0;
}
JsVar *callbackSetPixel = 0;
JsVar *callbackFillRect = 0;
if (jsvIsObject(callback)) {
jsvUnLock(callbackSetPixel);
callbackSetPixel = jsvObjectGetChildIfExists(callback, "setPixel");
callbackFillRect = jsvObjectGetChildIfExists(callback, "fillRect");
} else
callbackSetPixel = jsvLockAgain(callback);
if (!jsvIsFunction(callbackSetPixel)) {
jsExceptionHere(JSET_ERROR, "Expecting Callback Function or an Object but got %t", callbackSetPixel);
jsvUnLock2(callbackSetPixel, callbackFillRect);
return 0;
}
if (!jsvIsUndefined(callbackFillRect) && !jsvIsFunction(callbackFillRect)) {
jsExceptionHere(JSET_ERROR, "Expecting Callback Function or an Object but got %t", callbackFillRect);
jsvUnLock2(callbackSetPixel, callbackFillRect);
return 0;
}
JsVar *parent = jspNewObject(0, "Graphics");
if (!parent) return 0; // low memory
JsGraphics gfx;
gfx.data.type = JSGRAPHICSTYPE_JS;
graphicsStructInit(&gfx,width,height,bpp);
gfx.graphicsVar = parent;
lcdInit_JS(&gfx, callbackSetPixel, callbackFillRect);
graphicsSetVarInitial(&gfx);
jsvUnLock2(callbackSetPixel, callbackFillRect);
return parent;
}
#ifdef USE_LCD_SDL
/*JSON{
"type" : "staticmethod",
"class" : "Graphics",
"name" : "createSDL",
"ifdef" : "USE_LCD_SDL",
"generate" : "jswrap_graphics_createSDL",
"params" : [
["width","int32","Pixels wide"],
["height","int32","Pixels high"],
["bpp","int32","Bits per pixel (8,16,24 or 32 supported)"]
],
"return" : ["JsVar","The new Graphics object"],
"return_object" : "Graphics"
}
Create a Graphics object that renders to SDL window (Linux-based devices only)
*/
JsVar *jswrap_graphics_createSDL(int width, int height, int bpp) {
if (width<=0 || height<=0 || width>32767 || height>32767) {
jsExceptionHere(JSET_ERROR, "Invalid Size");
return 0;
}
JsVar *parent = jspNewObject(0, "Graphics");
if (!parent) return 0; // low memory
JsGraphics gfx;
gfx.data.type = JSGRAPHICSTYPE_SDL;
graphicsStructInit(&gfx,width,height,bpp);
gfx.graphicsVar = parent;
lcdInit_SDL(&gfx);
graphicsSetVarInitial(&gfx);
return parent;
}
#endif
/*TYPESCRIPT
type ImageObject = {
width: number;
height: number;
bpp?: number;
buffer: ArrayBuffer | string;
transparent?: number;
palette?: Uint16Array;
};
type Image = string | ImageObject | ArrayBuffer | Graphics<true>;
*/
/*JSON{
"type" : "staticmethod",
"class" : "Graphics",
"name" : "createImage",
"#if" : "!defined(SAVE_ON_FLASH) && !defined(ESPRUINOBOARD)",
"generate" : "jswrap_graphics_createImage",
"params" : [
["str","JsVar","A String containing a newline-separated image - space/. is 0, anything else is 1"]
],
"return" : ["JsVar","An Image object that can be used with `Graphics.drawImage`"],
"typescript" : "createImage(str: string): ImageObject;"
}
Create a simple Black and White image for use with `Graphics.drawImage`.
Use as follows:
```
var img = Graphics.createImage(`
XXXXXXXXX
X X
X X X
X X X
X X
XXXXXXXXX
`);
g.drawImage(img, x,y);
var img = Graphics.createImage(`
.....
.XXX.
.X.X.
.XXX.
.....
`);
g.drawImage(img, x,y);
```
If the characters at the beginning and end of the string are newlines, they will
be ignored. Spaces are treated as `0`, and any other character is a `1`
*/
JsVar *jswrap_graphics_createImage(JsVar *data) {
if (!jsvIsString(data)) {
jsExceptionHere(JSET_TYPEERROR, "Expecting String");
return 0;
}
int x=0,y=0;
int width=0, height=0;
size_t startCharacter = 0;
JsvStringIterator it;
// First iterate and work out width and height
jsvStringIteratorNew(&it,data,0);
char ch;
while (jsvStringIteratorHasChar(&it)) {
ch = jsvStringIteratorGetCharAndNext(&it);
if (ch=='\n') {
if (x==0 && y==0 && !startCharacter) {
startCharacter = 1; // ignore first character
} else {
x=0;
y++;
if (y+1>height) height=y+1;
}
} else {
x++;
if (x>width) width=x;
}
}
if (height && ch=='\n') height--; // if the last char was a newline, ignore it
jsvStringIteratorFree(&it);
// Sorted - now create the object, set it up and create the buffer
JsVar *img = jsvNewObject();
if (!img) return 0;
jsvObjectSetChildAndUnLock(img,"width",jsvNewFromInteger(width));
jsvObjectSetChildAndUnLock(img,"height",jsvNewFromInteger(height));
// bpp is 1, no need to set it
int len = (width*height+7)>>3;
JsVar *buffer = jsvNewStringOfLength((unsigned)len, NULL);
if (!buffer) { // not enough memory
jsvUnLock(img);
return 0;
}
// Now set the characters!
x=0;
y=0;
jsvStringIteratorNew(&it,data,startCharacter);
while (jsvStringIteratorHasChar(&it)) {
char ch = jsvStringIteratorGetCharAndNext(&it);
if (ch=='\n') {
x=0;
y++;
} else {
if (ch!=' ' && ch!='.') {
/* a pixel to set. This'll be slowish for non-flat strings,
* but this is here for relatively small bitmaps
* anyway so it's not a big deal */
size_t idx = (size_t)(y*width + x);
jsvSetCharInString(buffer, idx>>3, (char)(128>>(idx&7)), true/*OR*/);
}
x++;
}
}
jsvStringIteratorFree(&it);
jsvObjectSetChildAndUnLock(img, "buffer", buffer);
return img;
}
/*JSON{
"type" : "method",
"class" : "Graphics",
"name" : "getWidth",
"generate_full" : "jswrap_graphics_getWidthOrHeight(parent, false)",
"return" : ["int","The width of this Graphics instance"]
}
The width of this Graphics instance
*/
/*JSON{
"type" : "method",
"class" : "Graphics",
"name" : "getHeight",
"generate_full" : "jswrap_graphics_getWidthOrHeight(parent, true)",
"return" : ["int","The height of this Graphics instance"]
}
The height of this Graphics instance
*/
int jswrap_graphics_getWidthOrHeight(JsVar *parent, bool height) {
JsGraphics gfx; if (!graphicsGetFromVar(&gfx, parent)) return 0;
return height ? graphicsGetHeight(&gfx) : graphicsGetWidth(&gfx);
}
/*JSON{
"type" : "method",
"class" : "Graphics",
"name" : "getBPP",
"generate" : "jswrap_graphics_getBPP",
"return" : ["int","The bits per pixel of this Graphics instance"]
}
The number of bits per pixel of this Graphics instance
**Note:** Bangle.js 2 behaves a little differently here. The display is 3 bit,
so `getBPP` returns 3 and `asBMP`/`asImage`/etc return 3 bit images. However in
order to allow dithering, the colors returned by `Graphics.getColor` and
`Graphics.theme` are actually 16 bits.
*/
int jswrap_graphics_getBPP(JsVar *parent) {
JsGraphics gfx; if (!graphicsGetFromVar(&gfx, parent)) return 0;
#ifdef LCD_CONTROLLER_LPM013M126
if (gfx.data.type==JSGRAPHICSTYPE_MEMLCD)
return 3;
#endif
return gfx.data.bpp;
}
/*JSON{
"type" : "method",
"class" : "Graphics",
"name" : "reset",
"generate" : "jswrap_graphics_reset",
"return" : ["JsVar","The instance of Graphics this was called on, to allow call chaining"],
"return_object" : "Graphics"
}
Reset the state of Graphics to the defaults (e.g. Color, Font, etc) that would
have been used when Graphics was initialised.
*/
JsVar *jswrap_graphics_reset(JsVar *parent) {
JsGraphics gfx; if (!graphicsGetFromVar(&gfx, parent)) return 0;
#ifndef SAVE_ON_FLASH
// If we had a custom font, remove it.
jsvUnLock(jswrap_graphics_setFontSizeX(parent, 1+JSGRAPHICS_FONTSIZE_4X6, false));
#endif
// properly reset state
graphicsStructResetState(&gfx);
graphicsSetVar(&gfx); // gfx data changed because modified area
// reset font, which will unreference any custom fonts stored inside the instance
return jswrap_graphics_setFontSizeX(parent, 1+JSGRAPHICS_FONTSIZE_4X6, false);
}
/*JSON{
"type" : "method",
"class" : "Graphics",
"name" : "clear",
"generate" : "jswrap_graphics_clear",
"params" : [
["reset","bool","[optional] If `true`, resets the state of Graphics to the default (eg. Color, Font, etc) as if calling `Graphics.reset`"]
],
"return" : ["JsVar","The instance of Graphics this was called on, to allow call chaining"],
"return_object" : "Graphics"
}
Clear the LCD with the Background Color
*/
JsVar *jswrap_graphics_clear(JsVar *parent, bool resetState) {
if (resetState) jsvUnLock(jswrap_graphics_reset(parent));
JsGraphics gfx; if (!graphicsGetFromVar(&gfx, parent)) return 0;
graphicsClear(&gfx);
graphicsSetVar(&gfx); // gfx data changed because modified area
return jsvLockAgain(parent);
}
void _jswrap_graphics_getRect(JsVar *opt, int *x1, int *y1, int *x2, int *y2, int *r) {
*r = 0;
if (jsvIsObject(opt)) {
int w = -1,h = -1;
jsvConfigObject configs[] = {
{"x", JSV_INTEGER, x1},
{"y", JSV_INTEGER, y1},
{"x1", JSV_INTEGER, x1},
{"y1", JSV_INTEGER, y1},
{"x2", JSV_INTEGER, x2},
{"y2", JSV_INTEGER, y2},
{"w", JSV_INTEGER, &w},
{"h", JSV_INTEGER, &h},
{"r", JSV_INTEGER, r}
};
jsvReadConfigObject(opt, configs, sizeof(configs) / sizeof(jsvConfigObject));
if (w>=0) *x2 = *x1 + w;
if (h>=0) *y2 = *y1 + h;
} else
*x1 = jsvGetInteger(opt);
}