-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathBasicBitmap.cpp
5955 lines (5297 loc) · 159 KB
/
BasicBitmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
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
//=====================================================================
//
// BasicBitmap.cpp - Simple Bitmap Library
// https://github.com/skywind3000/BasicBitmap
//
// Created by: skywind, 2011, 2012, 2015, 2018
// Last Modified: 2018/03/15 14:54
//
// As the purpose of providing a simple, high-performance and platform
// independent Bitmap library, this file is created based on a subset
// of my vector graphic library: https://code.google.com/p/pixellib
//
// FEATURES:
//
// - common pixel format supported (from A8R8G8B8 to A4R4G4B4)
// - blitting with or without a transparent color
// - converting between different pixel formats
// - loading bmp/tga from memory or file and saving bmp to file
// - loading png/jpg with gdiplus (only in windows xp or above)
// - blending with different compositor
// - scaling with different filters (nearest, linear, bilinear)
//
// As a platform independent implementation, this class is written
// in pure C/C++. But all the core routines can be replaced by
// external implementations (sse2 eg.) using SetDriver/SetFunction.
//
// INTERFACES:
//
// - Fill: fill color in rectangle
// - Clear: clear the whole bitmap
// - Blit: blit from source bitmap with same bpp
// - Convert: convert from different pixel-format
// - SetPixel: draw pixel in raw color
// - GetPixel: read pixel in raw color
// - SetColor: draw pixel in A8R8G8B8
// - GetColor: read pixel in A8R8G8B8
// - Scale: scale bitmap using different filter and blend op
// - DrawLine: draw a line
// - QuickText: draw text with internal mini-8x8 ascii font
// - SampleBilinear: sample pixel with bilinear
// - SampleBicubic: sample pixel with bicubic
// - Resample: resample bitmap
// - LoadBmpFromMemory: load bmp file from memory
// - LoadTgaFromMemory: load tga file from memory
// - LoadBmp: load bmp file
// - LoadTga: load tga file
// - SaveBmp: save bmp file
// - SavePPM: save ppm file
// - DownSampleBy2: down sample 2x2 pixels into one pixel
// - SetDIBitsToDevice: (windows) draw bitmap to hdc
// - GetDIBits: (windows) get DIB bits to bitmap
// - GdiPlusInit: (windows) initialize gdiplus
// - GdiPlusLoadImageFromMemory: (windows) load jpg/png from memory
// - GdiPlusLoadImage: (windows) use gdiplus to load jpg/png
// - CreateBitmapInDIB: (windows) create bitmap with DIB section
//
// HISTORY:
//
// 2011.2.9 skywind create this file based on a subset of pixellib
// 2011.2.11 skywind immigrate blitting/blending/convertion/scaling
// 2011.2.13 skywind immigrate tga/bmp loader
//
//=====================================================================
#include "BasicBitmap.h"
#ifndef PIXEL_NO_SYSTEM
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>
#include <ObjBase.h>
#ifndef _WIN32
#define _WIN32
#endif
#endif
#endif
#ifndef PIXEL_NO_STDIO
#include <stdio.h>
#endif
#ifdef __WATCOMC__
#pragma warning 555 10 // turn off while is always false
#endif
#if defined(__BORLANDC__) && !defined(__MSDOS__)
#pragma warn -8004
#pragma warn -8057
#endif
//=====================================================================
// Internal Macros
//=====================================================================
#define pixel_round(x, n) (((x) + (n) - 1) & (~((n) - 1)))
#define PIXEL_ALIGN 16
#define PIXEL_STACK_BUFFER 2048
//---------------------------------------------------------------------
// MACRO: Pixel Assembly & Disassembly
//---------------------------------------------------------------------
/* lookup table for scaling 1 bit colors up to 8 bits */
const IUINT32 _pixel_scale_1[2] = { 0, 255 };
/* lookup table for scaling 2 bit colors up to 8 bits */
const IUINT32 _pixel_scale_2[4] = { 0, 85, 170, 255 };
/* lookup table for scaling 3 bit colors up to 8 bits */
const IUINT32 _pixel_scale_3[8] = { 0, 36, 72, 109, 145, 182, 218, 255 };
/* lookup table for scaling 4 bit colors up to 8 bits */
const IUINT32 _pixel_scale_4[16] = {
0, 16, 32, 49, 65, 82, 98, 115,
139, 156, 172, 189, 205, 222, 238, 255
};
/* lookup table for scaling 5 bit colors up to 8 bits */
const IUINT32 _pixel_scale_5[32] = {
0, 8, 16, 24, 32, 41, 49, 57,
65, 74, 82, 90, 98, 106, 115, 123,
131, 139, 148, 156, 164, 172, 180, 189,
197, 205, 213, 222, 230, 238, 246, 255
};
/* lookup table for scaling 6 bit colors up to 8 bits */
const IUINT32 _pixel_scale_6[64] = {
0, 4, 8, 12, 16, 20, 24, 28,
32, 36, 40, 44, 48, 52, 56, 60,
64, 68, 72, 76, 80, 85, 89, 93,
97, 101, 105, 109, 113, 117, 121, 125,
129, 133, 137, 141, 145, 149, 153, 157,
161, 165, 170, 174, 178, 182, 186, 190,
194, 198, 202, 206, 210, 214, 218, 222,
226, 230, 234, 238, 242, 246, 250, 255
};
unsigned char pixel_blend_lut[2048 * 2];
unsigned char pixel_clip_vector[256 * 3];
unsigned char *pixel_clip_256 = &pixel_clip_vector[256];
#define PIXEL_CLIP_256(x) pixel_clip_256[x]
//=====================================================================
// Memory Management
//=====================================================================
void *(*_internal_hook_malloc)(size_t size) = NULL;
void (*_internal_hook_free)(void *ptr) = NULL;
void *(*_internal_hook_memcpy)(void *dst, const void *src, size_t n) = NULL;
// allocate aligned memory
void *internal_align_malloc(size_t size, size_t n) {
size_t need = size + n + sizeof(void*);
char *ptr = NULL;
char *dst;
if (_internal_hook_malloc) {
ptr = (char*)_internal_hook_malloc(need);
} else {
ptr = (char*)malloc(need);
}
if (ptr == NULL) return NULL;
dst = (char*)(((size_t)ptr + sizeof(void*) + n - 1) & (~(n - 1)));
*(char**)(dst - sizeof(char*)) = ptr;
return dst;
}
// free aligned memory
void internal_align_free(void *ptr) {
char *dst = (char*)ptr;
ptr = *(char**)(dst - sizeof(char*));
assert(ptr);
*(char**)(dst - sizeof(char*)) = NULL;
if (_internal_hook_free) {
_internal_hook_free(ptr);
} else {
free(ptr);
}
}
// copy from destination to source
void *internal_memcpy(void *dst, const void *src, size_t size)
{
if (_internal_hook_memcpy) {
return _internal_hook_memcpy(dst, src, size);
}
#ifndef PIXEL_NO_MEMCPY
return memcpy(dst, src, size);
#else
unsigned char *dd = (unsigned char*)dst;
const unsigned char *ss = (const unsigned char*)src;
for (; size >= 8; size -= 8) {
*(IUINT32*)(dd + 0) = *(const IUINT32*)(ss + 0);
*(IUINT32*)(dd + 4) = *(const IUINT32*)(ss + 4);
dd += 8;
ss += 8;
}
for (; size > 0; size--) *dd++ = *ss++;
return dst;
#endif
}
//---------------------------------------------------------------------
// memory encode / decode
//---------------------------------------------------------------------
/* encode 8 bits unsigned int */
inline char *basic_encode_8u(char *p, IUINT8 c)
{
*(unsigned char*)p++ = c;
return p;
}
/* decode 8 bits unsigned int */
inline const char *basic_decode_8u(const char *p, IUINT8 *c)
{
*c = *(unsigned char*)p++;
return p;
}
/* encode 16 bits unsigned int (lsb) */
inline char *basic_encode_16u(char *p, IUINT16 w)
{
#if IWORDS_BIG_ENDIAN
*(unsigned char*)(p + 0) = (w & 255);
*(unsigned char*)(p + 1) = (w >> 8);
#else
*(unsigned short*)(p) = w;
#endif
p += 2;
return p;
}
/* decode 16 bits unsigned int (lsb) */
inline const char *basic_decode_16u(const char *p, IUINT16 *w)
{
#if IWORDS_BIG_ENDIAN
*w = *(const unsigned char*)(p + 1);
*w = *(const unsigned char*)(p + 0) + (*w << 8);
#else
*w = *(const unsigned short*)p;
#endif
p += 2;
return p;
}
/* encode 32 bits unsigned int (lsb) */
inline char *basic_encode_32u(char *p, IUINT32 l)
{
#if IWORDS_BIG_ENDIAN
*(unsigned char*)(p + 0) = (unsigned char)((l >> 0) & 0xff);
*(unsigned char*)(p + 1) = (unsigned char)((l >> 8) & 0xff);
*(unsigned char*)(p + 2) = (unsigned char)((l >> 16) & 0xff);
*(unsigned char*)(p + 3) = (unsigned char)((l >> 24) & 0xff);
#else
*(IUINT32*)p = l;
#endif
p += 4;
return p;
}
/* decode 32 bits unsigned int (lsb) */
inline const char *basic_decode_32u(const char *p, IUINT32 *l)
{
#if IWORDS_BIG_ENDIAN
*l = *(const unsigned char*)(p + 3);
*l = *(const unsigned char*)(p + 2) + (*l << 8);
*l = *(const unsigned char*)(p + 1) + (*l << 8);
*l = *(const unsigned char*)(p + 0) + (*l << 8);
#else
*l = *(const IUINT32*)p;
#endif
p += 4;
return p;
}
//---------------------------------------------------------------------
// initialize tables
//---------------------------------------------------------------------
void PixelInitLut()
{
static int inited = 0;
if (inited) {
return;
}
else {
int i;
for (i = 0; i < 256; i++) {
pixel_clip_256[i] = (IUINT8)i;
pixel_clip_256[i - 256] = 0;
pixel_clip_256[i + 256] = 255;
}
for (i = 0; i < 2048; i++) {
IUINT32 da = _pixel_scale_5[i >> 6];
IUINT32 sa = _pixel_scale_6[i & 63];
IUINT32 FA = da + ((255 - da) * sa) / 255;
IUINT32 SA = (FA != 0)? ((sa * 255) / FA) : 0;
pixel_blend_lut[i * 2 + 0] = (unsigned char)SA;
pixel_blend_lut[i * 2 + 1] = (unsigned char)FA;
}
}
inited = 1;
}
//---------------------------------------------------------------------
// Internal Macros
//---------------------------------------------------------------------
#define RGBA_FROM_A8R8G8B8(c, r, g, b, a) _pixel_disasm_8888(c, a, r, g, b)
#define RGBA_FROM_X8R8G8B8(c, r, g, b, a) do { \
_pixel_disasm_X888(c, r, g, b); (a) = 255; } while (0)
#define RGBA_FROM_P8R8G8B8(c, r, g, b, a) do { \
_pixel_from_P8R8G8B8(c, r, g, b, a); } while (0)
#define RGBA_FROM_A8B8G8R8(c, r, g, b, a) _pixel_disasm_8888(c, a, b, g, r)
#define RGBA_FROM_R8G8B8(c, r, g, b, a) do { \
_pixel_disasm_888(c, r, g, b); (a) = 255; } while (0)
#define RGBA_FROM_R5G6B5(c, r, g, b, a) do { \
_pixel_disasm_565(c, r, g, b); (a) = 255; } while (0)
#define RGBA_FROM_X1R5G5B5(c, r, g, b, a) do { \
_pixel_disasm_X555(c, r, g, b); (a) = 255; } while (0)
#define RGBA_FROM_A1R5G5B5(c, r, g, b, a) _pixel_disasm_1555(c, a, r, g, b)
#define RGBA_FROM_A4R4G4B4(c, r, g, b, a) _pixel_disasm_4444(c, a, r, g, b)
#define RGBA_FROM_G8(c, r, g, b, a) do { \
(r) = (g) = (b) = (c); (a) = 255; } while (0)
#define RGBA_TO_A8R8G8B8(r, g, b, a) _pixel_asm_8888(a, r, g, b)
#define RGBA_TO_A8B8G8R8(r, g, b, a) _pixel_asm_8888(a, b, g, r)
#define RGBA_TO_X8R8G8B8(r, g, b, a) _pixel_asm_8888(0, r, g, b)
#define RGBA_TO_P8R8G8B8(r, g, b, a) _pixel_RGBA_to_P8R8G8B8(r, g, b, a)
#define RGBA_TO_R8G8B8(r, g, b, a) _pixel_asm_888(r, g, b)
#define RGBA_TO_R5G6B5(r, g, b, a) _pixel_asm_565(r, g, b)
#define RGBA_TO_X1R5G5B5(r, g, b, a) _pixel_asm_1555(0, r, g, b)
#define RGBA_TO_A1R5G5B5(r, g, b, a) _pixel_asm_1555(a, r, g, b)
#define RGBA_TO_A4R4G4B4(r, g, b, a) _pixel_asm_4444(a, r, g, b)
#define RGBA_TO_G8(r, g, b, a) _pixel_to_gray(r, g, b)
#define _pixel_RGBA_to_P8R8G8B8(r, g, b, a) ( \
(((a)) << 24) | \
((((r) * _pixel_norm(a)) >> 8) << 16) | \
((((g) * _pixel_norm(a)) >> 8) << 8) | \
((((b) * _pixel_norm(a)) >> 8) << 0))
#define _pixel_from_P8R8G8B8(c, r, g, b, a) do { \
IUINT32 __SA = ((c) >> 24); \
IUINT32 __FA = (__SA); \
(a) = __SA; \
if (__FA > 0) { \
(r) = ((((c) >> 16) & 0xff) * 255) / __FA; \
(g) = ((((c) >> 8) & 0xff) * 255) / __FA; \
(b) = ((((c) >> 0) & 0xff) * 255) / __FA; \
} else { \
(r) = 0; (g) = 0; (b) = 0; \
} \
} while (0)
#define RGBA_TO_PIXEL(fmt, r, g, b, a) RGBA_TO_##fmt(r, g, b, a)
#define RGBA_FROM_PIXEL(fmt, c, r, g, b, a) RGBA_FROM_##fmt(c, r, g, b, a)
//---------------------------------------------------------------------
// BLEND
//---------------------------------------------------------------------
/* blend onto a static surface (no alpha channel) */
#define BLEND_STATIC(sr, sg, sb, sa, dr, dg, db, da) do { \
IINT32 SA = _pixel_norm(sa); \
(dr) = (((((IINT32)(sr)) - ((IINT32)(dr))) * SA) >> 8) + (dr); \
(dg) = (((((IINT32)(sg)) - ((IINT32)(dg))) * SA) >> 8) + (dg); \
(db) = (((((IINT32)(sb)) - ((IINT32)(db))) * SA) >> 8) + (db); \
(da) = 255; \
} while (0)
/* blend onto a normal surface (with alpha channel) */
#define BLEND_NORMAL(sr, sg, sb, sa, dr, dg, db, da) do { \
IINT32 SA = _pixel_norm(sa); \
IINT32 DA = _pixel_norm(da); \
IINT32 FA = DA + (((256 - DA) * SA) >> 8); \
SA = (FA != 0)? ((SA << 8) / FA) : (0); \
(da) = _pixel_unnorm(FA); \
(dr) = (((((IINT32)(sr)) - ((IINT32)(dr))) * SA) >> 8) + (dr); \
(dg) = (((((IINT32)(sg)) - ((IINT32)(dg))) * SA) >> 8) + (dg); \
(db) = (((((IINT32)(sb)) - ((IINT32)(db))) * SA) >> 8) + (db); \
} while (0)
/* blend onto a normal surface (with alpha channel) in a fast way */
/* looking up alpha values from lut instead of div. calculation */
/* lut must be inited by ipixel_lut_init() */
#define BLEND_NORMAL_FAST(sr, sg, sb, sa, dr, dg, db, da) do { \
IUINT32 __lutpos = (((da) & 0xf8) << 4) | (((sa) & 0xfc) >> 1); \
IINT32 SA = pixel_blend_lut[(__lutpos) + 0]; \
IINT32 FA = pixel_blend_lut[(__lutpos) + 1]; \
SA = _pixel_norm((SA)); \
(da) = FA; \
(dr) = (((((IINT32)(sr)) - ((IINT32)(dr))) * SA) >> 8) + (dr); \
(dg) = (((((IINT32)(sg)) - ((IINT32)(dg))) * SA) >> 8) + (dg); \
(db) = (((((IINT32)(sb)) - ((IINT32)(db))) * SA) >> 8) + (db); \
} while (0)
/* equation used in sdl */
#define BLEND_NORMAL_FLAT(sr, sg, sb, sa, dr, dg, db, da) do { \
(dr) = ((((sr) - (dr)) * (sa)) / 255) + (dr); \
(dg) = ((((sg) - (dg)) * (sa)) / 255) + (dg); \
(db) = ((((sb) - (db)) * (sa)) / 255) + (db); \
(da) = (sa) + (da)- ((sa) * (da)) / 255; \
} while (0)
/* additive blend */
#define BLEND_ADDITIVE(sr, sg, sb, sa, dr, dg, db, da) do { \
IINT32 XA = _pixel_norm(sa); \
IINT32 XR = (sr) * XA; \
IINT32 XG = (sg) * XA; \
IINT32 XB = (sb) * XA; \
XR = XR >> 8; \
XG = XG >> 8; \
XB = XB >> 8; \
XA = (sa) + (da); \
XR += (dr); \
XG += (dg); \
XB += (db); \
(dr) = PIXEL_CLIP_256(XR); \
(dg) = PIXEL_CLIP_256(XG); \
(db) = PIXEL_CLIP_256(XB); \
(da) = PIXEL_CLIP_256(XA); \
} while (0)
/* premultiplied src over */
#define BLEND_SRCOVER(sr, sg, sb, sa, dr, dg, db, da) do { \
IUINT32 SA = 255 - (sa); \
(dr) = (dr) * SA; \
(dg) = (dg) * SA; \
(db) = (db) * SA; \
(da) = (da) * SA; \
(dr) = _pixel_fast_div_255(dr) + (sr); \
(dg) = _pixel_fast_div_255(dg) + (sg); \
(db) = _pixel_fast_div_255(db) + (sb); \
(da) = _pixel_fast_div_255(da) + (sa); \
} while (0)
/* premutiplied 32bits blending:
dst = src + (255 - src.alpha) * dst / 255 */
#define BLEND_PARGB(color_dst, color_src) do { \
IUINT32 __A = 255 - ((color_src) >> 24); \
IUINT32 __DST_RB = (color_dst) & 0xff00ff; \
IUINT32 __DST_AG = ((color_dst) >> 8) & 0xff00ff; \
__DST_RB *= __A; \
__DST_AG *= __A; \
__DST_RB += ((__DST_RB + 0x01010101) >> 8) & 0x00ff00ff; \
__DST_AG += ((__DST_AG + 0x01010101) >> 8) & 0xff00ff00; \
__DST_RB >>= 8; \
__DST_AG &= 0xff00ff00; \
__A = (__DST_RB & 0xff00ff) | __DST_AG; \
(color_dst) = __A + (color_src); \
} while (0)
/* premutiplied 32bits blending (with coverage):
tmp = src * coverage / 255,
dst = tmp + (255 - tmp.alpha) * dst / 255 */
#define BLEND_PARGB_COVER(color_dst, color_src, coverage) do { \
IUINT32 __r1 = (color_src) & 0xff00ff; \
IUINT32 __r2 = ((color_src) >> 8) & 0xff00ff; \
IUINT32 __r3 = _pixel_norm(coverage); \
IUINT32 __r4; \
__r1 *= __r3; \
__r2 *= __r3; \
__r3 = (color_dst) & 0xff00ff; \
__r4 = ((color_dst) >> 8) & 0xff00ff; \
__r1 = ((__r1) >> 8) & 0xff00ff; \
__r2 = (__r2) & 0xff00ff00; \
__r1 = __r1 | __r2; \
__r2 = 255 - (__r2 >> 24); \
__r3 *= __r2; \
__r4 *= __r2; \
__r3 = ((__r3 + (__r3 >> 8)) >> 8) & 0xff00ff; \
__r4 = (__r4 + (__r4 >> 8)) & 0xff00ff00; \
(color_dst) = (__r3 | __r4) + (__r1); \
} while (0)
//=====================================================================
// STATIC DEFINITION
//=====================================================================
BasicBitmap::PixelBlit BasicBitmap::PixelBlitNormal1 = NULL;
BasicBitmap::PixelBlit BasicBitmap::PixelBlitNormal2 = NULL;
BasicBitmap::PixelBlit BasicBitmap::PixelBlitNormal3 = NULL;
BasicBitmap::PixelBlit BasicBitmap::PixelBlitNormal4 = NULL;
BasicBitmap::PixelBlit BasicBitmap::PixelBlitMask1 = NULL;
BasicBitmap::PixelBlit BasicBitmap::PixelBlitMask2 = NULL;
BasicBitmap::PixelBlit BasicBitmap::PixelBlitMask3 = NULL;
BasicBitmap::PixelBlit BasicBitmap::PixelBlitMask4 = NULL;
//=====================================================================
// BasicBitmap
//=====================================================================
//---------------------------------------------------------------------
// Constructor: create new bitmap
// pixel format for bpp = 32 is A8R8G8B8
// for bpp = 24 is R8G8B8, for bpp = 16 is R5G6B5,
// for bpp = 15 is A1R5G5B5 and for bpp = 8 is G8.
//---------------------------------------------------------------------
BasicBitmap::BasicBitmap(int width, int height, PixelFmt fmt)
{
_bits = NULL;
_lines = NULL;
_w = _h = _bpp = 0;
_pitch = 0;
_borrow = false;
int hr = Initialize(width, height, fmt, NULL, -1);
if (hr != 0) {
#ifndef PIXEL_USE_EXCEPTION
assert(hr == 0);
#else
throw BasicError("Initialize bitmap error", hr);
#endif
}
}
//---------------------------------------------------------------------
// Constructor: create new bitmap with external bit buffer
//---------------------------------------------------------------------
BasicBitmap::BasicBitmap(int width, int height, PixelFmt fmt, void *mem, long pitch)
{
_bits = NULL;
_lines = NULL;
_w = _h = _bpp = 0;
_pitch = 0;
_borrow = false;
int hr = Initialize(width, height, fmt, mem, pitch);
if (hr != 0) {
#ifndef PIXEL_USE_EXCEPTION
assert(hr == 0);
#else
throw BasicError("Initialize bitmap error", hr);
#endif
}
}
//---------------------------------------------------------------------
// copy constructor
//---------------------------------------------------------------------
BasicBitmap::BasicBitmap(const BasicBitmap &src)
{
_bits = NULL;
_lines = NULL;
_w = _h = _bpp = 0;
_pitch = 0;
_borrow = false;
int hr = Initialize(src._w, src._h, src._fmt, NULL, -1);
if (hr != 0) {
#ifndef PIXEL_USE_EXCEPTION
assert(hr == 0);
#else
throw BasicError("Initialize bitmap error", hr);
#endif
}
Blit(0, 0, &src, NULL);
}
//---------------------------------------------------------------------
// move constructor
//---------------------------------------------------------------------
#if __cplusplus >= 201103 || (defined(_MSC_VER) && _MSC_VER >= 1900)
BasicBitmap::BasicBitmap(BasicBitmap &&src)
{
_bits = src._bits;
_lines = src._lines;
_w = src._w;
_h = src._h;
_bpp = src._bpp;
_pixelsize = src._pixelsize;
_pitch = src._pitch;
_borrow = src._borrow;
_fmt = src._fmt;
_mask = src._mask;
src._bits = NULL;
src._lines = NULL;
src._w = 0;
src._h = 0;
src._bpp = 32;
src._pixelsize = 4;
src._pitch = 0;
src._mask = 0;
src._fmt = A8R8G8B8;
src._borrow = false;
}
#endif
//---------------------------------------------------------------------
// Destructor
//---------------------------------------------------------------------
BasicBitmap::~BasicBitmap()
{
Destroy();
}
//---------------------------------------------------------------------
// Initialize allocate buffer
// pixel format for bpp = 32 is A8R8G8B8,
// for bpp = 24 is R8G8B8, for bpp = 16 is R5G6B5,
// for bpp = 15 is A1R5G5B5 and for bpp = 8 is G8.
//---------------------------------------------------------------------
int BasicBitmap::Initialize(int width, int height, PixelFmt fmt, void *mem, long pitch)
{
int bpp = 0;
switch (fmt) {
case A8R8G8B8: bpp = 32; break;
case A8B8G8R8: bpp = 32; break;
case X8R8G8B8: bpp = 32; break;
case R8G8B8: bpp = 24; break;
case R5G6B5: bpp = 16; break;
case A1R5G5B5: bpp = 16; break;
case X1R5G5B5: bpp = 16; break;
case A4R4G4B4: bpp = 16; break;
case G8: bpp = 8; break;
case UNKNOW:
return -1;
}
int pixelsize = (bpp + 1) / 8;
if (bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32)
return -2;
Destroy();
_w = width;
_h = height;
_bpp = bpp;
_fmt = fmt;
if (mem == NULL) {
_pitch = pixel_round(width * pixelsize, 4);
_bits = (IUINT8*)internal_align_malloc(_pitch * height, PIXEL_ALIGN);
if (_bits == NULL) {
_w = _h = _bpp = 0;
return -3;
}
_borrow = false;
} else {
_pitch = pitch;
_bits = (unsigned char*)mem;
_borrow = true;
}
long size_lines = sizeof(char*) * height;
char *ptr = (char*)malloc(size_lines);
_lines = (unsigned char**)ptr;
if (_lines == NULL) {
if (_borrow == false) {
internal_align_free(_bits);
_bits = NULL;
_w = _h = _bpp = 0;
return -4;
}
}
for (int j = 0; j < height; j++) {
_lines[j] = _bits + j * _pitch;
}
_pixelsize = pixelsize;
_mask = 0;
return 0;
}
//---------------------------------------------------------------------
// dispose memory
//---------------------------------------------------------------------
int BasicBitmap::Destroy()
{
if (_bits) {
if (_borrow == false) {
internal_align_free(_bits);
}
_bits = NULL;
}
if (_lines) {
free(_lines);
}
_w = _h = _bpp = 0;
_pitch = 0;
return 0;
}
//---------------------------------------------------------------------
// fill rectangle with given color
//---------------------------------------------------------------------
void BasicBitmap::Fill(int x, int y, int w, int h, IUINT32 color)
{
int i, j;
if (x < 0) w += x, x = 0;
if (y < 0) h += y, y = 0;
if (w < 0 || h < 0) return;
if (x + w >= _w) w = _w - x;
if (y + h >= _h) h = _h - y;
switch (_pixelsize) {
case 1:
for (j = 0; j < h; j++) {
IUINT8 *dst = Line(y + j) + x;
IUINT8 cc = (IUINT8)(color & 0xff);
for (i = w; i > 0; i--) {
*dst++ = cc;
}
}
break;
case 2:
for (j = 0; j < h; j++) {
IUINT16 *dst = ((IUINT16*)Line(y + j)) + x;
IUINT16 cc = (IUINT16)(color & 0xffff);
for (i = w; i > 0; i--) {
*dst++ = cc;
}
}
break;
case 3:
for (j = 0; j < h; j++) {
IUINT8 *dst = Line(y + j);
int l = x;
int r = x + w;
for (; l < r; l++) {
_pixel_store(24, dst, l, color);
}
}
break;
case 4:
for (j = 0; j < h; j++) {
IUINT32 *dst = ((IUINT32*)Line(y + j)) + x;
for (i = w; i > 0; i--) {
*dst++ = color;
}
}
break;
}
}
//---------------------------------------------------------------------
// Constructor: create new bitmap
//---------------------------------------------------------------------
void BasicBitmap::Clear(IUINT32 color)
{
Fill(0, 0, _w, _h, color);
}
//---------------------------------------------------------------------
// blit normal
//---------------------------------------------------------------------
int BasicBitmap::BlitNormal(int bpp, void *dbits, long dpitch, int dx, const
void *sbits, long spitch, int sx, int w, int h, IUINT32 mask, int flip)
{
int pixelsize = (bpp + 7) >> 3;
int y, x, x1, x2, sx0, sxd, endx;
if (flip & PIXEL_FLAG_VFLIP) {
sbits = (const IUINT8*)sbits + spitch * (h - 1);
spitch = -spitch;
}
switch (pixelsize) {
case 1:
if ((flip & PIXEL_FLAG_HFLIP) == 0) {
long size = w * pixelsize;
for (y = 0; y < h; y++) {
internal_memcpy((IUINT8*)dbits + dx,
(const IUINT8*)sbits + sx, size);
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
} else {
for (y = 0; y < h; y++) {
const IUINT8 *src = (const IUINT8*)sbits + sx + w - 1;
IUINT8 *dst = (IUINT8*)dbits + dx;
for (x = w; x > 0; x--) *dst++ = *src--;
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
}
break;
case 2:
if ((flip & PIXEL_FLAG_HFLIP) == 0) {
long size = w * pixelsize;
for (y = 0; y < h; y++) {
internal_memcpy((IUINT16*)dbits + dx,
(const IUINT16*)sbits + sx, size);
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
} else {
for (y = 0; y < h; y++) {
const IUINT16 *src = (const IUINT16*)sbits + sx + w - 1;
IUINT16 *dst = (IUINT16*)dbits + dx;
for (x = w; x > 0; x--) *dst++ = *src--;
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
}
break;
case 3:
if (flip & PIXEL_FLAG_HFLIP) {
sx0 = sx + w - 1;
sxd = -1;
} else {
sx0 = sx;
sxd = 1;
}
endx = dx + w;
for (y = 0; y < h; y++) {
IUINT32 cc;
for (x1 = dx, x2 = sx0; x1 < endx; x1++, x2 += sxd) {
cc = _pixel_fetch(24, sbits, x2);
_pixel_store(24, dbits, x1, cc);
}
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
break;
case 4:
if ((flip & PIXEL_FLAG_HFLIP) == 0) {
long size = w * pixelsize;
for (y = 0; y < h; y++) {
internal_memcpy((IUINT32*)dbits + dx,
(const IUINT32*)sbits + sx, size);
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
} else {
for (y = 0; y < h; y++) {
const IUINT32 *src = (const IUINT32*)sbits + sx + w - 1;
IUINT32 *dst = (IUINT32*)dbits + dx;
for (x = w; x > 0; x--) *dst++ = *src--;
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
}
break;
}
return mask - mask;
}
//---------------------------------------------------------------------
// blit mask
//---------------------------------------------------------------------
int BasicBitmap::BlitMask(int bpp, void *dbits, long dpitch, int dx, const
void *sbits, long spitch, int sx, int w, int h, IUINT32 mask, int flip)
{
int pixelsize = (bpp + 7) >> 3;
int y, x1, x2, sx0, sxd, endx;
if (flip & PIXEL_FLAG_VFLIP) {
sbits = (const IUINT8*)sbits + spitch * (h - 1);
spitch = -spitch;
}
switch (pixelsize) {
case 1:
if ((flip & PIXEL_FLAG_HFLIP) == 0) {
IUINT8 cmask = (IUINT8)(mask & 0xff);
for (y = 0; y < h; y++) {
const IUINT8 *src = (const IUINT8*)sbits + sx;
IUINT8 *dst = (IUINT8*)dbits + dx;
IUINT8 *dstend = dst + w;
for (; dst < dstend; src++, dst++) {
if (src[0] != cmask) dst[0] = src[0];
}
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
} else {
IUINT8 cmask = (IUINT8)(mask & 0xff);
for (y = 0; y < h; y++) {
const IUINT8 *src = (const IUINT8*)sbits + sx + w - 1;
IUINT8 *dst = (IUINT8*)dbits + dx;
IUINT8 *dstend = dst + w;
for (; dst < dstend; src--, dst++) {
if (src[0] != cmask) dst[0] = src[0];
}
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
}
break;
case 2:
if ((flip & PIXEL_FLAG_HFLIP) == 0) {
IUINT16 cmask = (IUINT16)(mask & 0xffff);
for (y = 0; y < h; y++) {
const IUINT16 *src = (const IUINT16*)sbits + sx;
IUINT16 *dst = (IUINT16*)dbits + dx;
IUINT16 *dstend = dst + w;
for (; dst < dstend; src++, dst++) {
if (src[0] != cmask) dst[0] = src[0];
}
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
} else {
IUINT16 cmask = (IUINT16)(mask & 0xffff);
for (y = 0; y < h; y++) {
const IUINT16 *src = (const IUINT16*)sbits + sx + w - 1;
IUINT16 *dst = (IUINT16*)dbits + dx;
IUINT16 *dstend = dst + w;
for (; dst < dstend; src--, dst++) {
if (src[0] != cmask) dst[0] = src[0];
}
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
}
break;
case 3:
if (flip & PIXEL_FLAG_HFLIP) {
sx0 = sx + w - 1;
sxd = -1;
} else {
sx0 = sx;
sxd = 1;
}
endx = dx + w;
for (y = 0; y < h; y++) {
IUINT32 cc;
for (x1 = dx, x2 = sx0; x1 < endx; x1++, x2 += sxd) {
cc = _pixel_fetch(24, sbits, x2);
if (cc != mask) _pixel_store(24, dbits, x1, cc);
}
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
break;
case 4:
if ((flip & PIXEL_FLAG_HFLIP) == 0) {
IUINT32 cmask = (IUINT32)mask;
for (y = 0; y < h; y++) {
const IUINT32 *src = (const IUINT32*)sbits + sx;
IUINT32 *dst = (IUINT32*)dbits + dx;
IUINT32 *dstend = dst + w;
for (; dst < dstend; src++, dst++) {
if (src[0] != cmask) dst[0] = src[0];
}
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
} else {
IUINT32 cmask = (IUINT32)mask;
for (y = 0; y < h; y++) {
const IUINT32 *src = (const IUINT32*)sbits + sx + w - 1;
IUINT32 *dst = (IUINT32*)dbits + dx;
IUINT32 *dstend = dst + w;
for (; dst < dstend; src--, dst++) {
if (src[0] != cmask) dst[0] = src[0];
}
dbits = (IUINT8*)dbits + dpitch;
sbits = (const IUINT8*)sbits + spitch;
}
}
break;
}
return 0;
}
//---------------------------------------------------------------------
// ClipRect - clip the rectangle from the src clip and dst clip then