-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSDL_gfxPrimitives.c
6864 lines (5949 loc) · 159 KB
/
SDL_gfxPrimitives.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
/*
SDL_gfxPrimitives.c: graphics primitives for SDL surfaces
Copyright (C) 2001-2023 Andreas Schiffler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Andreas Schiffler -- aschiffler at ferzkopp dot net
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "SDL_gfxPrimitives.h"
#include "SDL_rotozoom.h"
#include "SDL_gfxPrimitives_font.h"
#include "SDL_gfxBlitFunc.h"
/* -===================- */
#define DEFAULT_ALPHA_PIXEL_ROUTINE
#undef EXPERIMENTAL_ALPHA_PIXEL_ROUTINE
#define ALPHA_PIXEL_ADDITIVE_BLEND
/* ---- Structures */
/*!
\brief The structure passed to the internal Bresenham iterator.
*/
typedef struct {
Sint16 x, y;
int dx, dy, s1, s2, swapdir, error;
Uint32 count;
} SDL_gfxBresenhamIterator;
/*!
\brief The structure passed to the internal Murphy iterator.
*/
typedef struct {
Uint32 color;
SDL_Surface *dst;
int u, v; /* delta x , delta y */
int ku, kt, kv, kd; /* loop constants */
int oct2;
int quad4;
Sint16 last1x, last1y, last2x, last2y, first1x, first1y, first2x, first2y, tempx, tempy;
} SDL_gfxMurphyIterator;
/* ----- Defines for pixel clipping tests */
#define clip_xmin(surface) surface->clip_rect.x
#define clip_xmax(surface) surface->clip_rect.x+surface->clip_rect.w-1
#define clip_ymin(surface) surface->clip_rect.y
#define clip_ymax(surface) surface->clip_rect.y+surface->clip_rect.h-1
/*!
\brief Internal pixel drawing - fast, no blending, no locking, clipping.
\param dst The surface to draw on.
\param x The horizontal coordinate of the pixel.
\param y The vertical position of the pixel.
\param color The color value of the pixel to draw.
\returns Returns 0 on success, -1 on failure.
*/
int fastPixelColorNolock(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color)
{
int bpp;
Uint8 *p;
/*
* Honor clipping setup at pixel level
*/
if ((x >= clip_xmin(dst)) && (x <= clip_xmax(dst)) && (y >= clip_ymin(dst)) && (y <= clip_ymax(dst))) {
/*
* Get destination format
*/
bpp = dst->format->BytesPerPixel;
p = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
switch (bpp) {
case 1:
*p = color;
break;
case 2:
*(Uint16 *) p = color;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (color >> 16) & 0xff;
p[1] = (color >> 8) & 0xff;
p[2] = color & 0xff;
} else {
p[0] = color & 0xff;
p[1] = (color >> 8) & 0xff;
p[2] = (color >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *) p = color;
break;
} /* switch */
}
return (0);
}
/*!
\brief Internal pixel drawing - fast, no blending, no locking, no clipping.
Function is faster but dangerous since no clipping check is done.
Code needs to make sure we stay in surface bounds before calling.
\param dst The surface to draw on.
\param x The horizontal coordinate of the pixel.
\param y The vertical position of the pixel.
\param color The color value of the pixel to draw.
\returns Returns 0 on success, -1 on failure.
*/
int fastPixelColorNolockNoclip(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color)
{
int bpp;
Uint8 *p;
/*
* Get destination format
*/
bpp = dst->format->BytesPerPixel;
p = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
switch (bpp) {
case 1:
*p = color;
break;
case 2:
*(Uint16 *) p = color;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (color >> 16) & 0xff;
p[1] = (color >> 8) & 0xff;
p[2] = color & 0xff;
} else {
p[0] = color & 0xff;
p[1] = (color >> 8) & 0xff;
p[2] = (color >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *) p = color;
break;
} /* switch */
return (0);
}
/*!
\brief Internal pixel drawing - fast, no blending, locking, clipping.
\param dst The surface to draw on.
\param x The horizontal coordinate of the pixel.
\param y The vertical position of the pixel.
\param color The color value of the pixel to draw.
\returns Returns 0 on success, -1 on failure.
*/
int fastPixelColor(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color)
{
int result;
/*
* Lock the surface
*/
if (SDL_MUSTLOCK(dst)) {
if (SDL_LockSurface(dst) < 0) {
return (-1);
}
}
result = fastPixelColorNolock(dst, x, y, color);
/*
* Unlock surface
*/
if (SDL_MUSTLOCK(dst)) {
SDL_UnlockSurface(dst);
}
return (result);
}
/*!
\brief Internal pixel drawing - fast, no blending, locking, RGB input.
\param dst The surface to draw on.
\param x The horizontal coordinate of the pixel.
\param y The vertical position of the pixel.
\param r The red value of the pixel to draw.
\param g The green value of the pixel to draw.
\param b The blue value of the pixel to draw.
\param a The alpha value of the pixel to draw.
\returns Returns 0 on success, -1 on failure.
*/
int fastPixelRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
Uint32 color;
/*
* Setup color
*/
color = SDL_MapRGBA(dst->format, r, g, b, a);
/*
* Draw
*/
return (fastPixelColor(dst, x, y, color));
}
/*!
\brief Internal pixel drawing - fast, no blending, no locking RGB input.
\param dst The surface to draw on.
\param x The horizontal coordinate of the pixel.
\param y The vertical position of the pixel.
\param r The red value of the pixel to draw.
\param g The green value of the pixel to draw.
\param b The blue value of the pixel to draw.
\param a The alpha value of the pixel to draw.
\returns Returns 0 on success, -1 on failure.
*/
int fastPixelRGBANolock(SDL_Surface * dst, Sint16 x, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
Uint32 color;
/*
* Setup color
*/
color = SDL_MapRGBA(dst->format, r, g, b, a);
/*
* Draw
*/
return (fastPixelColorNolock(dst, x, y, color));
}
/*!
\brief Internal pixel drawing function with alpha blending where input color in in destination format.
Contains two alternative 32 bit alpha blending routines which can be enabled at the source
level with the defines DEFAULT_ALPHA_PIXEL_ROUTINE or EXPERIMENTAL_ALPHA_PIXEL_ROUTINE.
Only the bits up to the surface depth are significant in the color value.
\param dst The surface to draw on.
\param x The horizontal coordinate of the pixel.
\param y The vertical position of the pixel.
\param color The color value of the pixel to draw.
\param alpha The blend factor to apply while drawing.
\returns Returns 0 on success, -1 on failure.
*/
int _putPixelAlpha(SDL_Surface *dst, Sint16 x, Sint16 y, Uint32 color, Uint8 alpha)
{
SDL_PixelFormat *format;
Uint32 Rmask, Gmask, Bmask, Amask;
Uint32 Rshift, Gshift, Bshift, Ashift;
Uint32 sR, sG, sB;
Uint32 dR, dG, dB, dA;
if (dst == NULL)
{
return (-1);
}
if (x >= clip_xmin(dst) && x <= clip_xmax(dst) &&
y >= clip_ymin(dst) && y <= clip_ymax(dst))
{
format = dst->format;
switch (format->BytesPerPixel) {
case 1:
{ /* Assuming 8-bpp */
Uint8 *pixel = (Uint8 *) dst->pixels + y * dst->pitch + x;
if (alpha == 255) {
*pixel = color;
} else {
Uint8 R, G, B;
SDL_Palette *palette = format->palette;
SDL_Color *colors = palette->colors;
SDL_Color dColor = colors[*pixel];
SDL_Color sColor = colors[color];
dR = dColor.r;
dG = dColor.g;
dB = dColor.b;
sR = sColor.r;
sG = sColor.g;
sB = sColor.b;
R = dR + ((sR - dR) * alpha >> 8);
G = dG + ((sG - dG) * alpha >> 8);
B = dB + ((sB - dB) * alpha >> 8);
*pixel = SDL_MapRGB(format, R, G, B);
}
}
break;
case 2:
{ /* Probably 15-bpp or 16-bpp */
Uint16 *pixel = (Uint16 *) dst->pixels + y * dst->pitch / 2 + x;
if (alpha == 255) {
*pixel = color;
} else {
Uint16 R, G, B, A;
Uint16 dc = *pixel;
Rmask = format->Rmask;
Gmask = format->Gmask;
Bmask = format->Bmask;
Amask = format->Amask;
dR = (dc & Rmask);
dG = (dc & Gmask);
dB = (dc & Bmask);
R = (dR + (((color & Rmask) - dR) * alpha >> 8)) & Rmask;
G = (dG + (((color & Gmask) - dG) * alpha >> 8)) & Gmask;
B = (dB + (((color & Bmask) - dB) * alpha >> 8)) & Bmask;
*pixel = R | G | B;
if (Amask!=0) {
dA = (dc & Amask);
A = (dA + (((color & Amask) - dA) * alpha >> 8)) & Amask;
*pixel |= A;
}
}
}
break;
case 3:
{ /* Slow 24-bpp mode, usually not used */
Uint8 R, G, B;
Uint8 Rshift8, Gshift8, Bshift8;
Uint8 *pixel = (Uint8 *) dst->pixels + y * dst->pitch + x * 3;
Rshift = format->Rshift;
Gshift = format->Gshift;
Bshift = format->Bshift;
Rshift8 = Rshift >> 3;
Gshift8 = Gshift >> 3;
Bshift8 = Bshift >> 3;
sR = (color >> Rshift) & 0xFF;
sG = (color >> Gshift) & 0xFF;
sB = (color >> Bshift) & 0xFF;
if (alpha == 255) {
*(pixel + Rshift8) = sR;
*(pixel + Gshift8) = sG;
*(pixel + Bshift8) = sB;
} else {
dR = *((pixel) + Rshift8);
dG = *((pixel) + Gshift8);
dB = *((pixel) + Bshift8);
R = dR + ((sR - dR) * alpha >> 8);
G = dG + ((sG - dG) * alpha >> 8);
B = dB + ((sB - dB) * alpha >> 8);
*((pixel) + Rshift8) = R;
*((pixel) + Gshift8) = G;
*((pixel) + Bshift8) = B;
}
}
break;
#ifdef DEFAULT_ALPHA_PIXEL_ROUTINE
case 4:
{ /* Probably :-) 32-bpp */
Uint32 R, G, B, A;
Uint32 *pixel = (Uint32 *) dst->pixels + y * dst->pitch / 4 + x;
if (alpha == 255) {
*pixel = color;
} else {
Uint32 dc = *pixel;
Rmask = format->Rmask;
Gmask = format->Gmask;
Bmask = format->Bmask;
Amask = format->Amask;
Rshift = format->Rshift;
Gshift = format->Gshift;
Bshift = format->Bshift;
Ashift = format->Ashift;
dR = (dc & Rmask) >> Rshift;
dG = (dc & Gmask) >> Gshift;
dB = (dc & Bmask) >> Bshift;
R = ((dR + ((((color & Rmask) >> Rshift) - dR) * alpha >> 8)) << Rshift) & Rmask;
G = ((dG + ((((color & Gmask) >> Gshift) - dG) * alpha >> 8)) << Gshift) & Gmask;
B = ((dB + ((((color & Bmask) >> Bshift) - dB) * alpha >> 8)) << Bshift) & Bmask;
*pixel = R | G | B;
if (Amask!=0) {
dA = (dc & Amask) >> Ashift;
#ifdef ALPHA_PIXEL_ADDITIVE_BLEND
A = (dA | GFX_ALPHA_ADJUST_ARRAY[alpha & 255]) << Ashift; // make destination less transparent...
#else
A = ((dA + ((((color & Amask) >> Ashift) - dA) * alpha >> 8)) << Ashift) & Amask;
#endif
*pixel |= A;
}
}
}
break;
#endif
#ifdef EXPERIMENTAL_ALPHA_PIXEL_ROUTINE
case 4:{ /* Probably :-) 32-bpp */
if (alpha == 255) {
*((Uint32 *) dst->pixels + y * dst->pitch / 4 + x) = color;
} else {
Uint32 *pixel = (Uint32 *) dst->pixels + y * dst->pitch / 4 + x;
Uint32 dR, dG, dB, dA;
Uint32 dc = *pixel;
Uint32 surfaceAlpha, preMultR, preMultG, preMultB;
Uint32 aTmp;
Rmask = format->Rmask;
Gmask = format->Gmask;
Bmask = format->Bmask;
Amask = format->Amask;
dR = (color & Rmask);
dG = (color & Gmask);
dB = (color & Bmask);
dA = (color & Amask);
Rshift = format->Rshift;
Gshift = format->Gshift;
Bshift = format->Bshift;
Ashift = format->Ashift;
preMultR = (alpha * (dR >> Rshift));
preMultG = (alpha * (dG >> Gshift));
preMultB = (alpha * (dB >> Bshift));
surfaceAlpha = ((dc & Amask) >> Ashift);
aTmp = (255 - alpha);
if (A = 255 - ((aTmp * (255 - surfaceAlpha)) >> 8 )) {
aTmp *= surfaceAlpha;
R = (preMultR + ((aTmp * ((dc & Rmask) >> Rshift)) >> 8)) / A << Rshift & Rmask;
G = (preMultG + ((aTmp * ((dc & Gmask) >> Gshift)) >> 8)) / A << Gshift & Gmask;
B = (preMultB + ((aTmp * ((dc & Bmask) >> Bshift)) >> 8)) / A << Bshift & Bmask;
}
*pixel = R | G | B | (A << Ashift & Amask);
}
}
break;
#endif
}
}
return (0);
}
/*!
\brief Pixel draw with blending enabled if a<255.
\param dst The surface to draw on.
\param x X (horizontal) coordinate of the pixel.
\param y Y (vertical) coordinate of the pixel.
\param color The color value of the pixel to draw (0xRRGGBBAA).
\returns Returns 0 on success, -1 on failure.
*/
int pixelColor(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color)
{
Uint8 alpha;
Uint32 mcolor;
int result = 0;
/*
* Lock the surface
*/
if (SDL_MUSTLOCK(dst)) {
if (SDL_LockSurface(dst) < 0) {
return (-1);
}
}
/*
* Setup color
*/
alpha = color & 0x000000ff;
mcolor =
SDL_MapRGBA(dst->format, (color & 0xff000000) >> 24,
(color & 0x00ff0000) >> 16, (color & 0x0000ff00) >> 8, alpha);
/*
* Draw
*/
result = _putPixelAlpha(dst, x, y, mcolor, alpha);
/*
* Unlock the surface
*/
if (SDL_MUSTLOCK(dst)) {
SDL_UnlockSurface(dst);
}
return (result);
}
/*!
\brief Pixel draw with blending enabled if a<255 - no surface locking.
\param dst The surface to draw on.
\param x X (horizontal) coordinate of the pixel.
\param y Y (vertical) coordinate of the pixel.
\param color The color value of the pixel to draw (0xRRGGBBAA).
\returns Returns 0 on success, -1 on failure.
*/
int pixelColorNolock(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color)
{
Uint8 alpha;
Uint32 mcolor;
int result = 0;
/*
* Setup color
*/
alpha = color & 0x000000ff;
mcolor =
SDL_MapRGBA(dst->format, (color & 0xff000000) >> 24,
(color & 0x00ff0000) >> 16, (color & 0x0000ff00) >> 8, alpha);
/*
* Draw
*/
result = _putPixelAlpha(dst, x, y, mcolor, alpha);
return (result);
}
/*!
\brief Internal function to draw filled rectangle with alpha blending.
Assumes color is in destination format.
\param dst The surface to draw on.
\param x1 X coordinate of the first corner (upper left) of the rectangle.
\param y1 Y coordinate of the first corner (upper left) of the rectangle.
\param x2 X coordinate of the second corner (lower right) of the rectangle.
\param y2 Y coordinate of the second corner (lower right) of the rectangle.
\param color The color value of the rectangle to draw (0xRRGGBBAA).
\param alpha Alpha blending amount for pixels.
\returns Returns 0 on success, -1 on failure.
*/
int _filledRectAlpha(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color, Uint8 alpha)
{
SDL_PixelFormat *format;
Uint32 Rmask, Gmask, Bmask, Amask;
Uint32 Rshift, Gshift, Bshift, Ashift;
Uint32 sR, sG, sB, sA;
Uint32 dR, dG, dB, dA;
Sint16 x, y;
if (dst == NULL) {
return (-1);
}
format = dst->format;
switch (format->BytesPerPixel) {
case 1:
{ /* Assuming 8-bpp */
Uint8 *row, *pixel;
Uint8 R, G, B;
SDL_Color *colors = format->palette->colors;
SDL_Color dColor;
SDL_Color sColor = colors[color];
sR = sColor.r;
sG = sColor.g;
sB = sColor.b;
for (y = y1; y <= y2; y++) {
row = (Uint8 *) dst->pixels + y * dst->pitch;
for (x = x1; x <= x2; x++) {
if (alpha == 255) {
*(row + x) = color;
} else {
pixel = row + x;
dColor = colors[*pixel];
dR = dColor.r;
dG = dColor.g;
dB = dColor.b;
R = dR + ((sR - dR) * alpha >> 8);
G = dG + ((sG - dG) * alpha >> 8);
B = dB + ((sB - dB) * alpha >> 8);
*pixel = SDL_MapRGB(format, R, G, B);
}
}
}
}
break;
case 2:
{ /* Probably 15-bpp or 16-bpp */
Uint16 *row, *pixel;
Uint16 R, G, B, A;
Uint16 dc;
Rmask = format->Rmask;
Gmask = format->Gmask;
Bmask = format->Bmask;
Amask = format->Amask;
sR = (color & Rmask);
sG = (color & Gmask);
sB = (color & Bmask);
sA = (color & Amask);
for (y = y1; y <= y2; y++) {
row = (Uint16 *) dst->pixels + y * dst->pitch / 2;
for (x = x1; x <= x2; x++) {
if (alpha == 255) {
*(row + x) = color;
} else {
pixel = row + x;
dc = *pixel;
dR = (dc & Rmask);
dG = (dc & Gmask);
dB = (dc & Bmask);
R = (dR + ((sR - dR) * alpha >> 8)) & Rmask;
G = (dG + ((sG - dG) * alpha >> 8)) & Gmask;
B = (dB + ((sB - dB) * alpha >> 8)) & Bmask;
*pixel = R | G | B;
if (Amask!=0) {
dA = (dc & Amask);
A = (dA + ((sA - dA) * alpha >> 8)) & Amask;
*pixel |= A;
}
}
}
}
}
break;
case 3:
{ /* Slow 24-bpp mode, usually not used */
Uint8 *row, *pixel;
Uint8 R, G, B;
Uint8 Rshift8, Gshift8, Bshift8;
Rshift = format->Rshift;
Gshift = format->Gshift;
Bshift = format->Bshift;
Rshift8 = Rshift >> 3;
Gshift8 = Gshift >> 3;
Bshift8 = Bshift >> 3;
sR = (color >> Rshift) & 0xff;
sG = (color >> Gshift) & 0xff;
sB = (color >> Bshift) & 0xff;
for (y = y1; y <= y2; y++) {
row = (Uint8 *) dst->pixels + y * dst->pitch;
for (x = x1; x <= x2; x++) {
pixel = row + x * 3;
if (alpha == 255) {
*(pixel + Rshift8) = sR;
*(pixel + Gshift8) = sG;
*(pixel + Bshift8) = sB;
} else {
dR = *((pixel) + Rshift8);
dG = *((pixel) + Gshift8);
dB = *((pixel) + Bshift8);
R = dR + ((sR - dR) * alpha >> 8);
G = dG + ((sG - dG) * alpha >> 8);
B = dB + ((sB - dB) * alpha >> 8);
*((pixel) + Rshift8) = R;
*((pixel) + Gshift8) = G;
*((pixel) + Bshift8) = B;
}
}
}
}
break;
#ifdef DEFAULT_ALPHA_PIXEL_ROUTINE
case 4:
{ /* Probably :-) 32-bpp */
Uint32 *row, *pixel;
Uint32 R, G, B, A;
Uint32 dc;
Rmask = format->Rmask;
Gmask = format->Gmask;
Bmask = format->Bmask;
Amask = format->Amask;
Rshift = format->Rshift;
Gshift = format->Gshift;
Bshift = format->Bshift;
Ashift = format->Ashift;
sR = (color & Rmask) >> Rshift;
sG = (color & Gmask) >> Gshift;
sB = (color & Bmask) >> Bshift;
sA = (color & Amask) >> Ashift;
for (y = y1; y <= y2; y++) {
row = (Uint32 *) dst->pixels + y * dst->pitch / 4;
for (x = x1; x <= x2; x++) {
if (alpha == 255) {
*(row + x) = color;
} else {
pixel = row + x;
dc = *pixel;
dR = (dc & Rmask) >> Rshift;
dG = (dc & Gmask) >> Gshift;
dB = (dc & Bmask) >> Bshift;
R = ((dR + ((sR - dR) * alpha >> 8)) << Rshift) & Rmask;
G = ((dG + ((sG - dG) * alpha >> 8)) << Gshift) & Gmask;
B = ((dB + ((sB - dB) * alpha >> 8)) << Bshift) & Bmask;
*pixel = R | G | B;
if (Amask!=0) {
dA = (dc & Amask) >> Ashift;
#ifdef ALPHA_PIXEL_ADDITIVE_BLEND
A = (dA | GFX_ALPHA_ADJUST_ARRAY[sA & 255]) << Ashift; // make destination less transparent...
#else
A = ((dA + ((sA - dA) * alpha >> 8)) << Ashift) & Amask;
#endif
*pixel |= A;
}
}
}
}
}
break;
#endif
#ifdef EXPERIMENTAL_ALPHA_PIXEL_ROUTINE
case 4:{ /* Probably :-) 32-bpp */
Uint32 *row, *pixel;
Uint32 dR, dG, dB, dA;
Uint32 dc;
Uint32 surfaceAlpha, preMultR, preMultG, preMultB;
Uint32 aTmp;
Rmask = format->Rmask;
Gmask = format->Gmask;
Bmask = format->Bmask;
Amask = format->Amask;
dR = (color & Rmask);
dG = (color & Gmask);
dB = (color & Bmask);
dA = (color & Amask);
Rshift = format->Rshift;
Gshift = format->Gshift;
Bshift = format->Bshift;
Ashift = format->Ashift;
preMultR = (alpha * (dR >> Rshift));
preMultG = (alpha * (dG >> Gshift));
preMultB = (alpha * (dB >> Bshift));
for (y = y1; y <= y2; y++) {
row = (Uint32 *) dst->pixels + y * dst->pitch / 4;
for (x = x1; x <= x2; x++) {
if (alpha == 255) {
*(row + x) = color;
} else {
pixel = row + x;
dc = *pixel;
surfaceAlpha = ((dc & Amask) >> Ashift);
aTmp = (255 - alpha);
if (A = 255 - ((aTmp * (255 - surfaceAlpha)) >> 8 )) {
aTmp *= surfaceAlpha;
R = (preMultR + ((aTmp * ((dc & Rmask) >> Rshift)) >> 8)) / A << Rshift & Rmask;
G = (preMultG + ((aTmp * ((dc & Gmask) >> Gshift)) >> 8)) / A << Gshift & Gmask;
B = (preMultB + ((aTmp * ((dc & Bmask) >> Bshift)) >> 8)) / A << Bshift & Bmask;
}
*pixel = R | G | B | (A << Ashift & Amask);
}
}
}
}
break;
#endif
}
return (0);
}
/*!
\brief Draw filled rectangle of RGBA color with alpha blending.
\param dst The surface to draw on.
\param x1 X coordinate of the first corner (upper left) of the rectangle.
\param y1 Y coordinate of the first corner (upper left) of the rectangle.
\param x2 X coordinate of the second corner (lower right) of the rectangle.
\param y2 Y coordinate of the second corner (lower right) of the rectangle.
\param color The color value of the rectangle to draw (0xRRGGBBAA).
\returns Returns 0 on success, -1 on failure.
*/
int filledRectAlpha(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color)
{
Uint8 alpha;
Uint32 mcolor;
int result = 0;
/*
* Lock the surface
*/
if (SDL_MUSTLOCK(dst)) {
if (SDL_LockSurface(dst) < 0) {
return (-1);
}
}
/*
* Setup color
*/
alpha = color & 0x000000ff;
mcolor =
SDL_MapRGBA(dst->format, (color & 0xff000000) >> 24,
(color & 0x00ff0000) >> 16, (color & 0x0000ff00) >> 8, alpha);
/*
* Draw
*/
result = _filledRectAlpha(dst, x1, y1, x2, y2, mcolor, alpha);
/*
* Unlock the surface
*/
if (SDL_MUSTLOCK(dst)) {
SDL_UnlockSurface(dst);
}
return (result);
}
/*!
\brief Internal function to draw horizontal line of RGBA color with alpha blending.
\param dst The surface to draw on.
\param x1 X coordinate of the first point (i.e. left) of the line.
\param x2 X coordinate of the second point (i.e. right) of the line.
\param y Y coordinate of the points of the line.
\param color The color value of the line to draw (0xRRGGBBAA).
\returns Returns 0 on success, -1 on failure.
*/
int _HLineAlpha(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color)
{
return (filledRectAlpha(dst, x1, y, x2, y, color));
}
/*!
\brief Internal function to draw vertical line of RGBA color with alpha blending.
\param dst The surface to draw on.
\param x X coordinate of the points of the line.
\param y1 Y coordinate of the first point (top) of the line.
\param y2 Y coordinate of the second point (bottom) of the line.
\param color The color value of the line to draw (0xRRGGBBAA).
\returns Returns 0 on success, -1 on failure.
*/
int _VLineAlpha(SDL_Surface * dst, Sint16 x, Sint16 y1, Sint16 y2, Uint32 color)
{
return (filledRectAlpha(dst, x, y1, x, y2, color));
}
/*!
\brief Pixel draw with blending enabled and using alpha weight on color.
\param dst The surface to draw on.
\param x The horizontal coordinate of the pixel.
\param y The vertical position of the pixel.
\param color The color value of the pixel to draw (0xRRGGBBAA).
\param weight The weight multiplied into the alpha value of the pixel.
\returns Returns 0 on success, -1 on failure.
*/
int pixelColorWeight(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color, Uint32 weight)
{
Uint32 a;
/*
* Get alpha
*/
a = (color & (Uint32) 0x000000ff);
/*
* Modify Alpha by weight
*/
a = ((a * weight) >> 8);
return (pixelColor(dst, x, y, (color & (Uint32) 0xffffff00) | (Uint32) a));
}
/*!
\brief Pixel draw with blending enabled and using alpha weight on color - no locking.
\param dst The surface to draw on.
\param x The horizontal coordinate of the pixel.
\param y The vertical position of the pixel.
\param color The color value of the pixel to draw (0xRRGGBBAA).
\param weight The weight multiplied into the alpha value of the pixel.
\returns Returns 0 on success, -1 on failure.
*/
int pixelColorWeightNolock(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color, Uint32 weight)
{
Uint32 a;
/*
* Get alpha
*/
a = (color & (Uint32) 0x000000ff);
/*
* Modify Alpha by weight
*/
a = ((a * weight) >> 8);
return (pixelColorNolock(dst, x, y, (color & (Uint32) 0xffffff00) | (Uint32) a));
}
/*!
\brief Pixel draw with blending enabled if a<255.
\param dst The surface to draw on.
\param x X (horizontal) coordinate of the pixel.
\param y Y (vertical) coordinate of the pixel.
\param r The red color value of the pixel to draw.
\param g The green color value of the pixel to draw.
\param b The blue color value of the pixel to draw.
\param a The alpha value of the pixel to draw.
\returns Returns 0 on success, -1 on failure.
*/
int pixelRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
Uint32 color;
/*
* Check Alpha