-
Notifications
You must be signed in to change notification settings - Fork 106
/
blitbuffer.c
3454 lines (3325 loc) · 175 KB
/
blitbuffer.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
/*
KOReader: blitbuffer implementation for jit-disabled platforms
Copyright (C) 2011 Hans-Werner Hilse <[email protected]>
2017 Huang Xin <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "blitbuffer.h"
static const char*
get_bbtype_name(int bbtype)
{
switch (bbtype) {
case TYPE_BB4:
return "BB4";
case TYPE_BB8:
return "BB8";
case TYPE_BB8A:
return "BB8A";
case TYPE_BBRGB16:
return "BBRGB16";
case TYPE_BBRGB24:
return "BBRGB24";
case TYPE_BBRGB32:
return "BBRGB32";
default:
return "Unknown!";
}
}
#define ColorRGB32_To_Color8(color) \
(Color8){(4898U*color->r + 9618U*color->g + 1869U*color->b) >> 14U}
#define ColorRGB32_To_Color8A(color) \
(Color8A){(4898U*color->r + 9618U*color->g + 1869U*color->b) >> 14U, color->alpha}
#define ColorRGB32_To_Color16(color) \
(ColorRGB16){((color->r & 0xF8) << 8U) + ((color->g & 0xFC) << 3U) + ((color->b >> 3U))}
#define ColorRGB32_To_Color24(color) \
(ColorRGB24){color->r, color->g, color->b}
#define Color8_To_Color8A(color) \
(Color8A){color->a, 0xFF}
#define Color8A_To_Color8(color) \
(Color8){color->a}
#define Color8A_To_Color24(color) \
(ColorRGB24){color->a, color->a, color->a}
#define Color8A_To_Color16(color) \
(ColorRGB16){((color->a & 0xF8) << 8U) + ((color->a & 0xFC) << 3U) + ((color->a >> 3U))}
#define Color8A_To_Color32(color) \
(ColorRGB32){color->a, color->a, color->a, color->alpha}
#define ColorRGB16_GetR(v) (((v >> 11U) << 3U) + ((v >> 11U) >> 2U))
#define ColorRGB16_GetG(v) ((((v >> 5U) & 0x3F) << 2U) + (((v >> 5U) & 0x3F) >> 4U))
#define ColorRGB16_GetB(v) (((v & 0x001F) << 3U) + ((v & 0x001F) >> 2U))
#define ColorRGB16_To_A(v) \
((39919*ColorRGB16_GetR(v) + \
39185*ColorRGB16_GetG(v) + \
15220*ColorRGB16_GetB(v)) >> 14U)
#define RGB_To_RGB16(r, g, b) (((r & 0xF8) << 8U) + ((g & 0xFC) << 3U) + (b >> 3U))
// NOTE: `A` was a *terrible* variable name to settle on. It's actually luminance, e.g., grayscale, a.k.a., Y8.
#define RGB_To_A(r, g, b) ((4898U*r + 9618U*g + 1869U*b) >> 14U)
// Helpers to pack pixels manually, without going through the Color structs.
#define Y8_To_Y8A(v) (0xFFu << 8U | v)
#define RGB_To_RGB32(r, g, b) ((uint32_t) (0xFFu << 24U) | (uint32_t) (b << 16U) | (uint32_t) (g << 8U) | r)
#define Y8_To_RGB32(v) ((uint32_t) (0xFFu << 24U) | (uint32_t) (v << 16U) | (uint32_t) (v << 8U) | v)
// __auto_type was introduced in GCC 4.9 (and Clang ~3.8)...
// NOTE: Inspired from glibc's __GNUC_PREREQ && __glibc_clang_prereq macros (from <features.h>),
// which we of course can't use because some of our TCs use a glibc version old enough not to have the clang one...
#if (defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 8))) || \
((defined(__GNUC__) && !defined(__clang__)) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)))
//#warning "Auto Type :)"
#define DIV_255(V) \
({ \
__auto_type _v = (V) + 128; \
(((_v >> 8U) + _v) >> 8U); \
})
#define ColorRGB32_To_PMUL(color) \
(ColorRGB32){DIV_255(color->r * color->alpha), DIV_255(color->g * color->alpha), DIV_255(color->b * color->alpha), color->alpha}
// MIN/MAX with no side-effects,
// c.f., https://gcc.gnu.org/onlinedocs/cpp/Duplication-of-Side-Effects.html#Duplication-of-Side-Effects
// & https://dustri.org/b/min-and-max-macro-considered-harmful.html
#define MIN(X, Y) \
({ \
__auto_type x_ = (X); \
__auto_type y_ = (Y); \
(x_ < y_) ? x_ : y_; \
})
#define MAX(X, Y) \
({ \
__auto_type x__ = (X); \
__auto_type y__ = (Y); \
(x__ > y__) ? x__ : y__; \
})
#else
#warning "TypeOf :("
#define DIV_255(V) \
({ \
typeof (V) _v = (V) + 128; \
(((_v >> 8U) + _v) >> 8U); \
})
#define MIN(X, Y) \
({ \
typeof (X) x_ = (X); \
typeof (Y) y_ = (Y); \
(x_ < y_) ? x_ : y_; \
})
#define MAX(X, Y) \
({ \
typeof (X) x__ = (X); \
typeof (Y) y__ = (Y); \
(x__ > y__) ? x__ : y__; \
})
#endif
// Likely/Unlikely branch tagging
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
// NOTE: See Pillow's transpose operations, or Qt5 qMemRotate stuff for cache-efficient ways of rotating an image data buffer,
// instead of handling the rotation per-pixel, at plotting time.
// I have no idea if it'd be an efficient method here, since it requires an extra buffer in which to do the rotation,
// just so that new buffer can be used for the memcpy-based fast paths...
#define BB_GET_PIXEL(bb, rotation, COLOR, x, y, pptr) \
({ \
if (rotation == 0) { \
*pptr = (COLOR*)(bb->data + (y) * bb->stride) + (x); \
} else if (rotation == 1) { \
*pptr = (COLOR*)(bb->data + (x) * bb->stride) + bb->w - (y) - 1; \
} else if (rotation == 2) { \
*pptr = (COLOR*)(bb->data + (bb->h - (y) - 1) * bb->stride) + bb->w - (x) - 1; \
} else if (rotation == 3) { \
*pptr = (COLOR*)(bb->data + (bb->h - (x) - 1) * bb->stride) + (y); \
} \
})
#define SET_ALPHA_FROM_A(bb, bb_type, bb_rotation, x, y, alpha) \
({ \
if (bb_type == TYPE_BB8) { \
const Color8 * restrict srcptr; \
BB_GET_PIXEL(bb, bb_rotation, Color8, x, y, &srcptr); \
*alpha = srcptr->a; \
} else if (bb_type == TYPE_BB8A) { \
const Color8A * restrict srcptr; \
BB_GET_PIXEL(bb, bb_rotation, Color8A, x, y, &srcptr); \
*alpha = srcptr->a; \
} else if (bb_type == TYPE_BBRGB16) { \
const ColorRGB16 * restrict srcptr; \
BB_GET_PIXEL(bb, bb_rotation, ColorRGB16, x, y, &srcptr); \
*alpha = (uint8_t) ColorRGB16_To_A(srcptr->v); \
} else if (bb_type == TYPE_BBRGB24) { \
const ColorRGB24 * restrict srcptr; \
BB_GET_PIXEL(bb, bb_rotation, ColorRGB24, x, y, &srcptr); \
*alpha = (uint8_t) RGB_To_A(srcptr->r, srcptr->g, srcptr->b); \
} else if (bb_type == TYPE_BBRGB32) { \
const ColorRGB32 * restrict srcptr; \
BB_GET_PIXEL(bb, bb_rotation, ColorRGB32, x, y, &srcptr); \
*alpha = (uint8_t) RGB_To_A(srcptr->r, srcptr->g, srcptr->b); \
} \
})
static inline void BB8_SET_PIXEL_CLAMPED(BlitBuffer * restrict bb, int rotation, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const Color8 * restrict color) {
if (likely(x < width && y < height)) {
Color8 * restrict pixel;
BB_GET_PIXEL(bb, rotation, Color8, x, y, &pixel);
*pixel = *color;
}
}
static inline void BB8A_SET_PIXEL_CLAMPED(BlitBuffer * restrict bb, int rotation, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const Color8A * restrict color) {
if (likely(x < width && y < height)) {
Color8A * restrict pixel;
BB_GET_PIXEL(bb, rotation, Color8A, x, y, &pixel);
*pixel = *color;
}
}
static inline void BBRGB16_SET_PIXEL_CLAMPED(BlitBuffer * restrict bb, int rotation, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const ColorRGB16 * restrict color) {
if (likely(x < width && y < height)) {
ColorRGB16 * restrict pixel;
BB_GET_PIXEL(bb, rotation, ColorRGB16, x, y, &pixel);
*pixel = *color;
}
}
static inline void BBRGB24_SET_PIXEL_CLAMPED(BlitBuffer * restrict bb, int rotation, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const ColorRGB24 * restrict color) {
if (likely(x < width && y < height)) {
ColorRGB24 * restrict pixel;
BB_GET_PIXEL(bb, rotation, ColorRGB24, x, y, &pixel);
*pixel = *color;
}
}
static inline void BBRGB32_SET_PIXEL_CLAMPED(BlitBuffer * restrict bb, int rotation, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const ColorRGB32 * restrict color) {
if (likely(x < width && y < height)) {
ColorRGB32 * restrict pixel;
BB_GET_PIXEL(bb, rotation, ColorRGB32, x, y, &pixel);
*pixel = *color;
}
}
static inline void BB8_BLEND_PIXEL_CLAMPED(BlitBuffer * restrict bb, int rotation, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const Color8A * restrict color) {
if (likely(x < width && y < height)) {
Color8 * restrict pixel;
BB_GET_PIXEL(bb, rotation, Color8, x, y, &pixel);
const uint8_t alpha = color->alpha;
const uint8_t ainv = alpha ^ 0xff;
pixel->a = (uint8_t) DIV_255(alpha * color->a + ainv * pixel->a);
}
}
static inline void BB8A_BLEND_PIXEL_CLAMPED(BlitBuffer * restrict bb, int rotation, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const Color8A * restrict color) {
if (likely(x < width && y < height)) {
Color8A * restrict pixel;
BB_GET_PIXEL(bb, rotation, Color8A, x, y, &pixel);
const uint8_t alpha = color->alpha;
const uint8_t ainv = alpha ^ 0xff;
pixel->a = (uint8_t) DIV_255(alpha * color->a + ainv * pixel->a);
pixel->alpha = 0xff;
}
}
static inline void BBRGB16_BLEND_PIXEL_CLAMPED(BlitBuffer * restrict bb, int rotation, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const Color8A * restrict color) {
if (likely(x < width && y < height)) {
ColorRGB16 * restrict pixel;
BB_GET_PIXEL(bb, rotation, ColorRGB16, x, y, &pixel);
const uint8_t alpha = color->alpha;
const uint8_t ainv = alpha ^ 0xff;
const uint8_t r = (uint8_t) DIV_255(alpha * color->a + ainv * ColorRGB16_GetR(pixel->v));
const uint8_t g = (uint8_t) DIV_255(alpha * color->a + ainv * ColorRGB16_GetG(pixel->v));
const uint8_t b = (uint8_t) DIV_255(alpha * color->a + ainv * ColorRGB16_GetB(pixel->v));
pixel->v = (uint16_t) RGB_To_RGB16(r, g, b);
}
}
static inline void BBRGB24_BLEND_PIXEL_CLAMPED(BlitBuffer * restrict bb, int rotation, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const ColorRGB32 * restrict color) {
if (likely(x < width && y < height)) {
ColorRGB24 * restrict pixel;
BB_GET_PIXEL(bb, rotation, ColorRGB24, x, y, &pixel);
const uint8_t alpha = color->alpha;
const uint8_t ainv = alpha ^ 0xff;
pixel->r = (uint8_t) DIV_255(alpha * color->r + ainv * pixel->r);
pixel->g = (uint8_t) DIV_255(alpha * color->g + ainv * pixel->g);
pixel->b = (uint8_t) DIV_255(alpha * color->b + ainv * pixel->b);
}
}
static inline void BBRGB32_BLEND_PIXEL_CLAMPED(BlitBuffer * restrict bb, int rotation, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const ColorRGB32 * restrict color) {
if (likely(x < width && y < height)) {
ColorRGB32 * restrict pixel;
BB_GET_PIXEL(bb, rotation, ColorRGB32, x, y, &pixel);
const uint8_t alpha = color->alpha;
const uint8_t ainv = alpha ^ 0xff;
pixel->r = (uint8_t) DIV_255(alpha * color->r + ainv * pixel->r);
pixel->g = (uint8_t) DIV_255(alpha * color->g + ainv * pixel->g);
pixel->b = (uint8_t) DIV_255(alpha * color->b + ainv * pixel->b);
pixel->alpha = (uint8_t) 0xff;
}
}
static inline unsigned int BB_GET_WIDTH(BlitBuffer * restrict bb) {
if ((GET_BB_ROTATION(bb) & 1U) == 0U) {
return bb->w;
} else {
return bb->h;
}
}
static inline unsigned int BB_GET_HEIGHT(BlitBuffer * restrict bb) {
if ((GET_BB_ROTATION(bb) & 1U) == 0U) {
return bb->h;
} else {
return bb->w;
}
}
void BB_fill(BlitBuffer * restrict bb, uint8_t v) {
// Handle any target pitch properly
const int bb_type = GET_BB_TYPE(bb);
if (bb_type == TYPE_BB8) {
//fprintf(stdout, "%s: BB8 fill\n", __FUNCTION__);
uint8_t * restrict p = bb->data;
memset(p, v, bb->stride*bb->h);
} else if (bb_type == TYPE_BB8A) {
// We do NOT want to stomp on the alpha byte here...
const uint16_t src = (uint16_t) Y8_To_Y8A(v);
//fprintf(stdout, "%s: BB8A fill\n", __FUNCTION__);
uint16_t * restrict p = (uint16_t *) bb->data;
size_t px_count = bb->pixel_stride*bb->h;
while (px_count--) {
*p++ = src;
}
} else if (bb_type == TYPE_BBRGB16) {
// Again, RGB565 means we can't use a straight memset
const uint16_t src = (uint16_t) RGB_To_RGB16(v, v, v);
//fprintf(stdout, "%s: BBRGB16 fill\n", __FUNCTION__);
uint16_t * restrict p = (uint16_t *) bb->data;
size_t px_count = bb->pixel_stride*bb->h;
while (px_count--) {
*p++ = src;
}
} else if (bb_type == TYPE_BBRGB24) {
//fprintf(stdout, "%s: BBRGB24 fill\n", __FUNCTION__);
uint8_t * restrict p = bb->data;
memset(p, v, bb->stride*bb->h);
} else if (bb_type == TYPE_BBRGB32) {
// And here either, as we want to preserve the alpha byte
const uint32_t src = (uint32_t) Y8_To_RGB32(v);
//fprintf(stdout, "%s: BBRGB32 fill\n", __FUNCTION__);
uint32_t * restrict p = (uint32_t *) bb->data;
size_t px_count = bb->pixel_stride*bb->h;
while (px_count--) {
*p++ = src;
}
}
}
void BB_fill_rect(BlitBuffer * restrict bb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint8_t v) {
const int rotation = GET_BB_ROTATION(bb);
unsigned int rx, ry, rw, rh;
// Compute rotated rectangle coordinates & size
switch (rotation) {
case 0:
rx = x;
ry = y;
rw = w;
rh = h;
break;
case 1:
rx = bb->w - (y + h);
ry = x;
rw = h;
rh = w;
break;
case 2:
rx = bb->w - (x + w);
ry = bb->h - (y + h);
rw = w;
rh = h;
break;
case 3:
rx = y;
ry = bb->h - (x + w);
rw = h;
rh = w;
break;
}
// Handle any target pitch properly
const int bb_type = GET_BB_TYPE(bb);
switch (bb_type) {
case TYPE_BB8:
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines (e.g., BB_fill())
//fprintf(stdout, "%s: Full BB8 paintRect\n", __FUNCTION__);
uint8_t * restrict p = bb->data + bb->stride*ry;
memset(p, v, bb->stride*rh);
} else {
// Scanline per scanline
//fprintf(stdout, "%s: Scanline BB8 paintRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
uint8_t * restrict p = bb->data + bb->stride*j + rx;
memset(p, v, rw);
}
}
break;
case TYPE_BB8A:
// We do NOT want to stomp on the alpha byte here...
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines
const uint16_t src = (uint16_t) Y8_To_Y8A(v);
//fprintf(stdout, "%s: Full BB8A paintRect\n", __FUNCTION__);
uint16_t * restrict p = (uint16_t *) (bb->data + bb->stride*ry);
size_t px_count = bb->pixel_stride*rh;
while (px_count--) {
*p++ = src;
}
} else {
// Scanline per scanline
const uint16_t src = (uint16_t) Y8_To_Y8A(v);
//fprintf(stdout, "%s: Scanline BB8A paintRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
uint16_t * restrict p = (uint16_t *) (bb->data + bb->stride*j) + rx;
size_t px_count = rw;
while (px_count--) {
*p++ = src;
}
}
}
break;
case TYPE_BBRGB16:
// Again, RGB565 means we can't use a straight memset
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines
const uint16_t src = (uint16_t) RGB_To_RGB16(v, v, v);
//fprintf(stdout, "%s: Full BBRGB16 paintRect\n", __FUNCTION__);
uint16_t * restrict p = (uint16_t *) (bb->data + bb->stride*ry);
size_t px_count = bb->pixel_stride*rh;
while (px_count--) {
*p++ = src;
}
} else {
// Scanline per scanline
const uint16_t src = (uint16_t) RGB_To_RGB16(v, v, v);
//fprintf(stdout, "%s: Sanline BBRGB16 paintRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
uint16_t * restrict p = (uint16_t *) (bb->data + bb->stride*j) + rx;
size_t px_count = rw;
while (px_count--) {
*p++ = src;
}
}
}
break;
case TYPE_BBRGB24:
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines
//fprintf(stdout, "%s: Full BBRGB24 paintRect\n", __FUNCTION__);
uint8_t * restrict p = bb->data + bb->stride*ry;
memset(p, v, bb->stride*rh);
} else {
// Scanline per scanline
//fprintf(stdout, "%s: Scanline BBRGB24 paintRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
uint8_t * restrict p = bb->data + bb->stride*j + (rx * 3U);
memset(p, v, (rw * 3U));
}
}
break;
case TYPE_BBRGB32:
// And here either, as we want to preserve the alpha byte
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines
const uint32_t src = (uint32_t) Y8_To_RGB32(v);
//fprintf(stdout, "%s: Full BBRGB32 paintRect\n", __FUNCTION__);
uint32_t * restrict p = (uint32_t *) (bb->data + bb->stride*ry);
size_t px_count = bb->pixel_stride*rh;
while (px_count--) {
*p++ = src;
}
} else {
// Scanline per scanline
const uint32_t src = (uint32_t) Y8_To_RGB32(v);
//fprintf(stdout, "%s: Pixel BBRGB32 paintRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
uint32_t * restrict p = (uint32_t *) (bb->data + bb->stride*j) + rx;
size_t px_count = rw;
while (px_count--) {
*p++ = src;
}
}
}
break;
}
}
void BB_fill_rect_RGB32(BlitBuffer * restrict bb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, const ColorRGB32 * restrict color) {
const int rotation = GET_BB_ROTATION(bb);
unsigned int rx, ry, rw, rh;
// Compute rotated rectangle coordinates & size
switch (rotation) {
case 0:
rx = x;
ry = y;
rw = w;
rh = h;
break;
case 1:
rx = bb->w - (y + h);
ry = x;
rw = h;
rh = w;
break;
case 2:
rx = bb->w - (x + w);
ry = bb->h - (y + h);
rw = w;
rh = h;
break;
case 3:
rx = y;
ry = bb->h - (x + w);
rw = h;
rh = w;
break;
}
// Handle any target pitch properly
const int bb_type = GET_BB_TYPE(bb);
switch (bb_type) {
case TYPE_BB8:
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines (e.g., BB_fill())
//fprintf(stdout, "%s: Full BB8 paintRect\n", __FUNCTION__);
uint8_t * restrict p = bb->data + bb->stride*ry;
memset(p, RGB_To_A(color->r, color->g, color->b), bb->stride*rh);
} else {
// Scanline per scanline
//fprintf(stdout, "%s: Scanline BB8 paintRect\n", __FUNCTION__);
const uint8_t source_y8 = RGB_To_A(color->r, color->g, color->b);
for (unsigned int j = ry; j < ry+rh; j++) {
uint8_t * restrict p = bb->data + bb->stride*j + rx;
memset(p, source_y8, rw);
}
}
break;
case TYPE_BB8A:
// We do NOT want to stomp on the alpha byte here...
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines
const uint16_t src = (uint16_t) Y8_To_Y8A(RGB_To_A(color->r, color->g, color->b));
//fprintf(stdout, "%s: Full BB8A paintRect\n", __FUNCTION__);
uint16_t * restrict p = (uint16_t *) (bb->data + bb->stride*ry);
size_t px_count = bb->pixel_stride*rh;
while (px_count--) {
*p++ = src;
}
} else {
// Scanline per scanline
const uint16_t src = (uint16_t) Y8_To_Y8A(RGB_To_A(color->r, color->g, color->b));
//fprintf(stdout, "%s: Scanline BB8A paintRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
uint16_t * restrict p = (uint16_t *) (bb->data + bb->stride*j) + rx;
size_t px_count = rw;
while (px_count--) {
*p++ = src;
}
}
}
break;
case TYPE_BBRGB16:
// Again, RGB565 means we can't use a straight memset
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines
const ColorRGB16 src = ColorRGB32_To_Color16(color);
//fprintf(stdout, "%s: Full BBRGB16 paintRect\n", __FUNCTION__);
ColorRGB16 * restrict p = (ColorRGB16 *) (bb->data + bb->stride*ry);
size_t px_count = bb->pixel_stride*rh;
while (px_count--) {
*p++ = src;
}
} else {
// Scanline per scanline
const ColorRGB16 src = ColorRGB32_To_Color16(color);
//fprintf(stdout, "%s: Sanline BBRGB16 paintRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
ColorRGB16 * restrict p = (ColorRGB16 *) (bb->data + bb->stride*j) + rx;
size_t px_count = rw;
while (px_count--) {
*p++ = src;
}
}
}
break;
case TYPE_BBRGB24:
{
// Pixel per pixel
const ColorRGB24 src = ColorRGB32_To_Color24(color);
//fprintf(stdout, "%s: Pixel BBRGB24 paintRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
for (unsigned int k = rx; k < rx+rw; k++) {
uint8_t * restrict p = bb->data + bb->stride*j + (k * 3U);
memcpy(p, &src, 3);
}
}
}
break;
case TYPE_BBRGB32:
// And here either, as we want to preserve the alpha byte
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines
//fprintf(stdout, "%s: Full BBRGB32 paintRect\n", __FUNCTION__);
ColorRGB32 * restrict p = (ColorRGB32 *) (bb->data + bb->stride*ry);
size_t px_count = bb->pixel_stride*rh;
while (px_count--) {
*p++ = *color;
}
} else {
// Scanline per scanline
//fprintf(stdout, "%s: Pixel BBRGB32 paintRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
ColorRGB32 * restrict p = (ColorRGB32 *) (bb->data + bb->stride*j) + rx;
size_t px_count = rw;
while (px_count--) {
*p++ = *color;
}
}
}
break;
}
}
void BB_blend_rect(BlitBuffer * restrict bb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, const Color8A * restrict color) {
const int bb_type = GET_BB_TYPE(bb);
const int bb_rotation = GET_BB_ROTATION(bb);
const uint8_t alpha = color->alpha;
const uint8_t ainv = alpha ^ 0xFF;
switch (bb_type) {
case TYPE_BB8:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
Color8 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, Color8, i, j, &dstptr);
dstptr->a = (uint8_t) DIV_255(dstptr->a * ainv + color->a * alpha);
}
}
break;
case TYPE_BB8A:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
Color8A * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, Color8A, i, j, &dstptr);
dstptr->a = (uint8_t) DIV_255(dstptr->a * ainv + color->a * alpha);
}
}
break;
case TYPE_BBRGB16:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB16 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB16, i, j, &dstptr);
const uint8_t r = (uint8_t) DIV_255(ColorRGB16_GetR(dstptr->v) * ainv + color->a * alpha);
const uint8_t g = (uint8_t) DIV_255(ColorRGB16_GetG(dstptr->v) * ainv + color->a * alpha);
const uint8_t b = (uint8_t) DIV_255(ColorRGB16_GetB(dstptr->v) * ainv + color->a * alpha);
dstptr->v = (uint16_t) RGB_To_RGB16(r, g, b);
}
}
break;
case TYPE_BBRGB24:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB24 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB24, i, j, &dstptr);
dstptr->r = (uint8_t) DIV_255(dstptr->r * ainv + color->a * alpha);
dstptr->g = (uint8_t) DIV_255(dstptr->g * ainv + color->a * alpha);
dstptr->b = (uint8_t) DIV_255(dstptr->b * ainv + color->a * alpha);
}
}
break;
case TYPE_BBRGB32:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB32 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB32, i, j, &dstptr);
dstptr->r = (uint8_t) DIV_255(dstptr->r * ainv + color->a * alpha);
dstptr->g = (uint8_t) DIV_255(dstptr->g * ainv + color->a * alpha);
dstptr->b = (uint8_t) DIV_255(dstptr->b * ainv + color->a * alpha);
}
}
break;
}
}
void BB_blend_RGB32_over_rect(BlitBuffer * restrict bb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, const ColorRGB32 * restrict color) {
const int bb_type = GET_BB_TYPE(bb);
const int bb_rotation = GET_BB_ROTATION(bb);
const uint8_t alpha = color->alpha;
const uint8_t ainv = alpha ^ 0xFF;
switch (bb_type) {
case TYPE_BB8:
{
const uint8_t source_y8 = RGB_To_A(color->r, color->g, color->b);
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
Color8 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, Color8, i, j, &dstptr);
dstptr->a = (uint8_t) DIV_255(dstptr->a * ainv + source_y8 * alpha);
}
}
}
break;
case TYPE_BB8A:
{
const uint8_t source_y8 = RGB_To_A(color->r, color->g, color->b);
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
Color8A * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, Color8A, i, j, &dstptr);
dstptr->a = (uint8_t) DIV_255(dstptr->a * ainv + source_y8 * alpha);
}
}
}
break;
case TYPE_BBRGB16:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB16 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB16, i, j, &dstptr);
const uint8_t r = (uint8_t) DIV_255(ColorRGB16_GetR(dstptr->v) * ainv + color->r * alpha);
const uint8_t g = (uint8_t) DIV_255(ColorRGB16_GetG(dstptr->v) * ainv + color->g * alpha);
const uint8_t b = (uint8_t) DIV_255(ColorRGB16_GetB(dstptr->v) * ainv + color->b * alpha);
dstptr->v = (uint16_t) RGB_To_RGB16(r, g, b);
}
}
break;
case TYPE_BBRGB24:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB24 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB24, i, j, &dstptr);
dstptr->r = (uint8_t) DIV_255(dstptr->r * ainv + color->r * alpha);
dstptr->g = (uint8_t) DIV_255(dstptr->g * ainv + color->g * alpha);
dstptr->b = (uint8_t) DIV_255(dstptr->b * ainv + color->b * alpha);
}
}
break;
case TYPE_BBRGB32:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB32 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB32, i, j, &dstptr);
dstptr->r = (uint8_t) DIV_255(dstptr->r * ainv + color->r * alpha);
dstptr->g = (uint8_t) DIV_255(dstptr->g * ainv + color->g * alpha);
dstptr->b = (uint8_t) DIV_255(dstptr->b * ainv + color->b * alpha);
}
}
break;
}
}
// Dumb multiply blending mode (used for painting book highlights)
void BB_blend_RGB_multiply_rect(BlitBuffer * restrict bb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, const ColorRGB24 * restrict color) {
const int bb_type = GET_BB_TYPE(bb);
const int bb_rotation = GET_BB_ROTATION(bb);
switch (bb_type) {
case TYPE_BB8:
{
const uint8_t source_y8 = RGB_To_A(color->r, color->g, color->b);
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
Color8 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, Color8, i, j, &dstptr);
dstptr->a = (uint8_t) DIV_255(dstptr->a * source_y8);
}
}
}
break;
case TYPE_BB8A:
{
const uint8_t source_y8 = RGB_To_A(color->r, color->g, color->b);
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
Color8A * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, Color8A, i, j, &dstptr);
dstptr->a = (uint8_t) DIV_255(dstptr->a * source_y8);
}
}
}
break;
case TYPE_BBRGB16:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB16 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB16, i, j, &dstptr);
const uint8_t r = (uint8_t) DIV_255(ColorRGB16_GetR(dstptr->v) * color->r);
const uint8_t g = (uint8_t) DIV_255(ColorRGB16_GetG(dstptr->v) * color->g);
const uint8_t b = (uint8_t) DIV_255(ColorRGB16_GetB(dstptr->v) * color->b);
dstptr->v = (uint16_t) RGB_To_RGB16(r, g, b);
}
}
break;
case TYPE_BBRGB24:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB24 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB24, i, j, &dstptr);
dstptr->r = (uint8_t) DIV_255(dstptr->r * color->r);
dstptr->g = (uint8_t) DIV_255(dstptr->g * color->g);
dstptr->b = (uint8_t) DIV_255(dstptr->b * color->b);
}
}
break;
case TYPE_BBRGB32:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB32 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB32, i, j, &dstptr);
dstptr->r = (uint8_t) DIV_255(dstptr->r * color->r);
dstptr->g = (uint8_t) DIV_255(dstptr->g * color->g);
dstptr->b = (uint8_t) DIV_255(dstptr->b * color->b);
}
}
break;
}
}
// Fancier variant if we ever want to honor color's alpha...
// Function name is a slight misnommer, as we're essentially doing (color MUL rect) OVER rect
void BB_blend_RGB32_multiply_rect(BlitBuffer * restrict bb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, const ColorRGB32 * restrict color) {
const int bb_type = GET_BB_TYPE(bb);
const int bb_rotation = GET_BB_ROTATION(bb);
const uint8_t alpha = color->alpha;
const uint8_t ainv = alpha ^ 0xFF;
switch (bb_type) {
case TYPE_BB8:
{
const uint8_t source_y8 = RGB_To_A(color->r, color->g, color->b);
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
Color8 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, Color8, i, j, &dstptr);
dstptr->a = (uint8_t) DIV_255(dstptr->a * ainv + DIV_255(dstptr->a * source_y8) * alpha);
}
}
}
break;
case TYPE_BB8A:
{
const uint8_t source_y8 = RGB_To_A(color->r, color->g, color->b);
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
Color8A * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, Color8A, i, j, &dstptr);
dstptr->a = (uint8_t) DIV_255(dstptr->a * ainv + DIV_255(dstptr->a * source_y8) * alpha);
}
}
}
break;
case TYPE_BBRGB16:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB16 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB16, i, j, &dstptr);
const uint8_t dr = ColorRGB16_GetR(dstptr->v);
const uint8_t dg = ColorRGB16_GetR(dstptr->v);
const uint8_t db = ColorRGB16_GetR(dstptr->v);
const uint8_t r = (uint8_t) DIV_255(dr * ainv + DIV_255(dr * color->r) * alpha);
const uint8_t g = (uint8_t) DIV_255(dg * ainv + DIV_255(dg * color->g) * alpha);
const uint8_t b = (uint8_t) DIV_255(db * ainv + DIV_255(db * color->b) * alpha);
dstptr->v = (uint16_t) RGB_To_RGB16(r, g, b);
}
}
break;
case TYPE_BBRGB24:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB24 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB24, i, j, &dstptr);
dstptr->r = (uint8_t) DIV_255(dstptr->r * ainv + DIV_255(dstptr->r * color->r) * alpha);
dstptr->g = (uint8_t) DIV_255(dstptr->g * ainv + DIV_255(dstptr->g * color->g) * alpha);
dstptr->b = (uint8_t) DIV_255(dstptr->b * ainv + DIV_255(dstptr->b * color->b) * alpha);
}
}
break;
case TYPE_BBRGB32:
for (unsigned int j = y; j < y + h; j++) {
for (unsigned int i = x; i < x + w; i++) {
ColorRGB32 * restrict dstptr;
BB_GET_PIXEL(bb, bb_rotation, ColorRGB32, i, j, &dstptr);
dstptr->r = (uint8_t) DIV_255(dstptr->r * ainv + DIV_255(dstptr->r * color->r) * alpha);
dstptr->g = (uint8_t) DIV_255(dstptr->g * ainv + DIV_255(dstptr->g * color->g) * alpha);
dstptr->b = (uint8_t) DIV_255(dstptr->b * ainv + DIV_255(dstptr->b * color->b) * alpha);
}
}
break;
}
}
void BB_invert_rect(BlitBuffer * restrict bb, unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
const int rotation = GET_BB_ROTATION(bb);
unsigned int rx, ry, rw, rh;
// Compute rotated rectangle coordinates & size
switch (rotation) {
case 0:
rx = x;
ry = y;
rw = w;
rh = h;
break;
case 1:
rx = bb->w - (y + h);
ry = x;
rw = h;
rh = w;
break;
case 2:
rx = bb->w - (x + w);
ry = bb->h - (y + h);
rw = w;
rh = h;
break;
case 3:
rx = y;
ry = bb->h - (x + w);
rw = h;
rh = w;
break;
}
// Handle any target pitch properly
const int bb_type = GET_BB_TYPE(bb);
switch (bb_type) {
case TYPE_BB8:
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines
//fprintf(stdout, "%s: Full BB8 invertRect\n", __FUNCTION__);
uint8_t * restrict p = bb->data + bb->stride*ry;
size_t px_count = bb->pixel_stride*rh;
while (px_count--) {
*p++ ^= 0xFF;
}
} else {
// Scanline per scanline
//fprintf(stdout, "%s: Scanline BB8 invertRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
uint8_t * restrict p = bb->data + bb->stride*j + rx;
size_t px_count = rw;
while (px_count--) {
*p++ ^= 0xFF;
}
}
}
break;
case TYPE_BB8A:
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines
//fprintf(stdout, "%s: Full BB8A invertRect\n", __FUNCTION__);
uint16_t * restrict p = (uint16_t*) (bb->data + bb->stride*ry);
size_t px_count = bb->pixel_stride*rh;
while (px_count--) {
*p++ ^= 0x00FF;
}
} else {
// Scanline per scanline
//fprintf(stdout, "%s: Scanline BB8A invertRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
uint16_t * restrict p = (uint16_t*) (bb->data + bb->stride*j) + rx;
size_t px_count = rw;
while (px_count--) {
*p++ ^= 0x00FF;
}
}
}
break;
case TYPE_BBRGB16:
// NOTE: Not actually accurate, but RGB565 is the worst.
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines
//fprintf(stdout, "%s: Full BBRGB16 invertRect\n", __FUNCTION__);
uint16_t * restrict p = (uint16_t*) (bb->data + bb->stride*ry);
size_t px_count = bb->pixel_stride*rh;
while (px_count--) {
*p++ ^= 0xFFFF;
}
} else {
// Scanline per scanline
//fprintf(stdout, "%s: Scanline BBRGB16 invertRect\n", __FUNCTION__);
for (unsigned int j = ry; j < ry+rh; j++) {
uint16_t * restrict p = (uint16_t*) (bb->data + bb->stride*j) + rx;
size_t px_count = rw;
while (px_count--) {
*p++ ^= 0xFFFF;
}
}
}
break;
case TYPE_BBRGB24:
if (rx == 0 && rw == bb->w) {
// Single step for contiguous scanlines
//fprintf(stdout, "%s: Full BBRGB24 invertRect\n", __FUNCTION__);
uint8_t * restrict p = bb->data + bb->stride*ry;
size_t px_count = bb->pixel_stride*rh;
while (px_count--) {
*p++ ^= 0xFF;
*p++ ^= 0xFF;
*p++ ^= 0xFF;
}