This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathcombiners.cpp
More file actions
1390 lines (1193 loc) · 45.2 KB
/
combiners.cpp
File metadata and controls
1390 lines (1193 loc) · 45.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode 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 LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "prefix.h"
#ifdef __VISUALC__
#pragma optimize("agt", on)
#pragma optimize("y", off)
#endif
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
static uint32_t g_current_background_colour = 0;
enum BitwiseOperation
{
// Bitwise
OPERATION_CLEAR,
OPERATION_AND,
OPERATION_AND_REVERSE,
OPERATION_COPY,
OPERATION_AND_INVERTED,
OPERATION_NOOP,
OPERATION_XOR,
OPERATION_OR,
OPERATION_NOR,
OPERATION_EQUIV,
OPERATION_INVERT,
OPERATION_OR_REVERSE,
OPERATION_COPY_INVERTED,
OPERATION_OR_INVERTED,
OPERATION_NAND,
OPERATION_SET,
LAST_BITWISE_OPERATION = OPERATION_SET,
};
enum ArithmeticOperation
{
// Arithmetic
OPERATION_BLEND = LAST_BITWISE_OPERATION + 1,
OPERATION_ADD_PIN,
OPERATION_ADD_OVER,
OPERATION_SUB_PIN,
OPERATION_TRANSPARENT,
OPERATION_AD_MAX,
OPERATION_SUB_OVER,
OPERATION_AD_MIN,
LAST_ARITHMETIC_OPERATION = OPERATION_AD_MIN,
};
enum BasicImagingOperation
{
// Basic Imaging Blends
OPERATION_BLEND_CLEAR = LAST_ARITHMETIC_OPERATION + 1,
OPERATION_BLEND_SRC,
OPERATION_BLEND_DST,
OPERATION_BLEND_SRC_OVER,
OPERATION_BLEND_DST_OVER,
OPERATION_BLEND_SRC_IN,
OPERATION_BLEND_DST_IN,
OPERATION_BLEND_SRC_OUT,
OPERATION_BLEND_DST_OUT,
OPERATION_BLEND_SRC_ATOP,
OPERATION_BLEND_DST_ATOP,
OPERATION_BLEND_XOR,
OPERATION_BLEND_PLUS,
OPERATION_BLEND_MULTIPLY,
OPERATION_BLEND_SCREEN,
LAST_BASIC_IMAGING_OPERATION = OPERATION_BLEND_SCREEN,
};
enum AdvancedImagingOperation
{
// Advanced Imaging Blends
OPERATION_BLEND_OVERLAY = LAST_BASIC_IMAGING_OPERATION + 1,
OPERATION_BLEND_DARKEN,
OPERATION_BLEND_LIGHTEN,
OPERATION_BLEND_DODGE,
OPERATION_BLEND_BURN,
OPERATION_BLEND_HARD_LIGHT,
OPERATION_BLEND_SOFT_LIGHT,
OPERATION_BLEND_DIFFERENCE,
OPERATION_BLEND_EXCLUSION,
LAST_ADVANCED_IMAGING_OPERATION = OPERATION_BLEND_EXCLUSION,
};
#define OPERATION_SRC_BIC OPERATION_AND_REVERSE
#define OPERATION_NOT_SRC_BIC OPERATION_AND
typedef void (*surface_combiner_t)(void *p_dst, int32_t p_dst_stride, const void *p_src, uint32_t p_src_stride, uint32_t p_width, uint32_t p_height, uint8_t p_opacity);
#ifdef __VISUALC__
#define INLINE __forceinline
#else
#define INLINE inline
#endif
static INLINE uint32_t _combine(uint32_t u, uint32_t v)
{
u += 0x800080;
v += 0x800080;
return (((u + ((u >> 8) & 0xff00ff)) >> 8) & 0xff00ff) + (((v + ((v >> 8) & 0xff00ff))) & 0xff00ff00);
}
static INLINE uint32_t _low(uint32_t x)
{
return x & 0xff00ff;
}
static INLINE uint32_t _high(uint32_t x)
{
return (x >> 8) & 0xff00ff;
}
static INLINE uint32_t _scale(uint32_t x, uint8_t a)
{
return x * a;
}
static INLINE uint32_t _scale_low(uint32_t x, uint8_t a)
{
return _scale(_low(x), a);
}
static INLINE uint32_t _scale_high(uint32_t x, uint32_t a)
{
return _scale(_high(x), a);
}
static INLINE uint32_t _multiply_low(uint32_t x, uint32_t y)
{
return ((x & 0xff) * (y & 0xff)) | ((x & 0xff0000) * ((y >> 16) & 0xff));
}
static INLINE uint32_t _multiply_high(uint32_t x, uint32_t y)
{
x = x >> 8;
return ((x & 0xff) * ((y >> 8) & 0xff)) | ((x & 0xff0000) * (y >> 24));
}
// r_i = (x_i * a) / 255
static INLINE uint32_t packed_scale_bounded(uint32_t x, uint8_t a)
{
uint32_t u, v;
u = ((x & 0xff00ff) * a) + 0x800080;
u = ((u + ((u >> 8) & 0xff00ff)) >> 8) & 0xff00ff;
v = (((x >> 8) & 0xff00ff) * a) + 0x800080;
v = (v + ((v >> 8) & 0xff00ff)) & 0xff00ff00;
return u + v;
}
// r_i = x_i + a
static INLINE uint32_t packed_translate_bounded(uint32_t x, uint8_t a)
{
uint32_t y;
y = a | a << 8;
y |= y << 16;
return x + y;
}
// r_i = x_i + y_i
static INLINE uint32_t packed_add_bounded(uint32_t x, uint32_t y)
{
return x + y;
}
// r_i = x_i * y_i / 255;
static INLINE uint32_t packed_multiply_bounded(uint32_t x, uint32_t y)
{
return _combine(_multiply_low(x, y), _multiply_high(x, y));
}
// r_i = (x_i * a) / 255 + y_i
static INLINE uint32_t packed_scale_add_bounded(uint32_t x, uint8_t a, uint32_t y)
{
return packed_add_bounded(packed_scale_bounded(x, a), y);
}
// r_i = (x_i * a + y_i * b) / 255
static INLINE uint32_t packed_bilinear_bounded(uint32_t x, uint8_t a, uint32_t y, uint8_t b)
{
uint32_t u, v;
u = (x & 0xff00ff) * a + (y & 0xff00ff) * b + 0x800080;
u = ((u + ((u >> 8) & 0xff00ff)) >> 8) & 0xff00ff;
v = ((x >> 8) & 0xff00ff) * a + ((y >> 8) & 0xff00ff) * b + 0x800080;
v = (v + ((v >> 8) & 0xff00ff)) & 0xff00ff00;
return u | v;
}
static INLINE uint32_t packed_add_saturated(uint32_t x, uint32_t y)
{
uint32_t u, v;
u = _low(x) + _low(y);
u = (u | (0x10000100 - ((u >> 8) & 0xff00ff))) & 0xff00ff;
v = _high(x) + _high(y);
v = (v | (0x10000100 - ((v >> 8) & 0xff00ff))) & 0xff00ff;
return u | (v << 8);
}
static INLINE uint32_t packed_subtract_saturated(uint32_t x, uint32_t y)
{
uint32_t u, v;
u = _low(x) - _low(y);
u = (u & ~((u >> 8) & 0xff00ff)) & 0xff00ff;
v = _high(x) - _high(y);
v = (v & ~((v >> 8) & 0xff00ff)) & 0xff00ff;
return u | (v << 8);
}
static INLINE uint32_t packed_divide_bounded(uint32_t x, uint8_t a)
{
uint32_t u, v, w;
u = ((((x & 0xff0000) << 8) - (x & 0xff0000)) / a) & 0xff0000;
v = ((((x & 0x00ff00) << 8) - (x & 0x00ff00)) / a) & 0x00ff00;
w = ((((x & 0x0000ff) << 8) - (x & 0x0000ff)) / a) & 0x0000ff;
return u | v | w;
}
static INLINE uint32_t packed_inverse(uint32_t x)
{
return ~x;
}
static INLINE uint8_t packed_alpha(uint32_t x)
{
return x >> 24;
}
static INLINE uint8_t packed_inverse_alpha(uint32_t x)
{
return (~x) >> 24;
}
static INLINE uint8_t saturated_add(uint8_t a, uint8_t b)
{
if (a + b > 255)
return 255;
return a + b;
}
static INLINE uint8_t inverse(uint8_t a)
{
return 255 - a;
}
static INLINE uint16_t upscale(uint8_t a)
{
return (a << 8) - a;
}
static INLINE uint32_t upscale(uint16_t a)
{
return (a << 8) - a;
}
static INLINE uint8_t downscale(uint16_t a)
{
uint32_t r = ((a + 0x80) + ((a + 0x80) >> 8)) >> 8;
return uint8_t(r);
}
static INLINE uint16_t scaled_divide(uint16_t n, uint8_t s, uint8_t d)
{
return d != 0 ? uint16_t((n * s) / d) : 0;
}
static INLINE uint16_t long_scaled_divide(uint32_t n, uint8_t s, uint16_t d)
{
return d != 0 ? uint16_t((n * s) / d) : 0;
}
static INLINE uint8_t sqrt(uint16_t n)
{
return 1;
}
template<typename Type> INLINE Type fastmin(Type a, Type b)
{
return a > b ? b : a;
}
template<typename Type> INLINE Type fastmax(Type a, Type b)
{
return a < b ? b : a;
}
template<BitwiseOperation x_combiner, bool x_dst_alpha, bool x_src_alpha> INLINE uint32_t bitwise_combiner(uint32_t dst, uint32_t src)
{
uint8_t sa, da;
uint32_t r, s, d;
if (x_dst_alpha)
da = packed_alpha(dst);
else
da = 255;
if (x_src_alpha)
sa = packed_alpha(src);
else
sa = 255;
if (sa == 0)
return dst;
if (da == 0)
return src;
if (sa != 255)
s = packed_divide_bounded(src, sa);
else
s = src;
if (da != 255)
d = packed_divide_bounded(dst, da);
else
d = dst;
switch(x_combiner)
{
case OPERATION_CLEAR:
r = 0x00000000;
break;
case OPERATION_AND:
r = s & d;
break;
case OPERATION_AND_REVERSE:
r = (s & ~d) & 0xffffff;
break;
case OPERATION_COPY:
r = s;
break;
case OPERATION_AND_INVERTED:
r = (~s & d) & 0xffffff;
break;
case OPERATION_NOOP:
r = d;
break;
case OPERATION_XOR:
r = s ^ d;
break;
case OPERATION_OR:
r = s | d;
break;
case OPERATION_NOR:
r = (~(s | d)) & 0xffffff;
break;
case OPERATION_EQUIV:
r = (~s ^ d) & 0xffffff;
break;
case OPERATION_INVERT:
r = ~d & 0xffffff;
break;
case OPERATION_OR_REVERSE:
r = (s | ~d) & 0xffffff;
break;
case OPERATION_COPY_INVERTED:
r = (~s) & 0xffffff;
break;
case OPERATION_OR_INVERTED:
r = (~s | d) & 0xffffff;
break;
case OPERATION_NAND:
r = (~(s & d)) & 0xffffff;
break;
case OPERATION_SET:
r = 0x00ffffff;
break;
default:
MCUnreachableReturn(0);
}
if (x_src_alpha && x_dst_alpha)
{
uint8_t ra;
ra = downscale(sa * da);
r = packed_bilinear_bounded(src, 255 - da, dst, 255 - sa) + packed_scale_bounded(r | 0xff000000, ra);
}
else if (!x_src_alpha && x_dst_alpha)
r = packed_bilinear_bounded(src, 255 - da, r | 0xff000000, da);
else if (!x_dst_alpha && x_src_alpha)
r = packed_bilinear_bounded(dst, 255 - sa, r | 0xff000000, sa);
return r;
}
extern uint32_t g_current_background_colour;
template<ArithmeticOperation x_combiner, bool x_dst_alpha, bool x_src_alpha> INLINE uint32_t arithmetic_combiner(uint32_t dst, uint32_t src)
{
uint8_t sa, da;
uint32_t s, d;
if (x_dst_alpha)
da = packed_alpha(dst);
else
da = 255;
if (x_src_alpha)
sa = packed_alpha(src);
else
sa = 255;
if (sa == 0)
return dst;
if (da == 0)
return src;
if (sa != 255)
s = packed_divide_bounded(src, sa);
else
s = src;
if (da != 255)
d = packed_divide_bounded(dst, da);
else
d = dst;
uint32_t r;
switch(x_combiner)
{
case OPERATION_BLEND:
{
uint32_t u, v;
u = (((d & 0xff00ff) + (s & 0xff00ff)) >> 1) & 0xff00ff;
v = (((d & 0x00ff00) + (s & 0x00ff00)) >> 1) & 0x00ff00;
r = u | v;
}
break;
case OPERATION_ADD_PIN:
{
uint32_t u, v;
u = (d & 0xff00ff) + (s & 0xff00ff);
u = (u | (0x1000100 - ((u >> 8) & 0xff00ff))) & 0xff00ff;
v = (d & 0x00ff00) + (s & 0x00ff00);
v = (v | (0x0010000 - ((v >> 8) & 0x00ff00))) & 0x00ff00;
r = u | v;
}
break;
case OPERATION_ADD_OVER:
{
uint32_t u, v;
u = ((d & 0xff00ff) + (s & 0xff00ff)) & 0xff00ff;
v = ((d & 0x00ff00) + (s & 0x00ff00)) & 0x00ff00;
r = u | v;
}
break;
case OPERATION_SUB_PIN:
{
uint32_t u, v;
u = (s & 0xff00ff) - (d & 0xff00ff);
u = (u & (((u >> 8) & 0xff00ff) - 0x100101)) & 0xff00ff;
v = (s & 0x00ff00) - (d & 0x00ff00);
v = (v & (((v >> 8) & 0x00ff00) - 0x0010100)) & 0x00ff00;
r = u | v;
}
break;
case OPERATION_TRANSPARENT:
r = (s == g_current_background_colour) ? d : s;
break;
case OPERATION_AD_MAX:
{
uint32_t sr, sg, sb;
sr = s & 0x0000ff; sg = s & 0x00ff00; sb = s & 0xff0000;
uint32_t dr, dg, db;
dr = d & 0x0000ff; dg = d & 0x00ff00; db = d & 0xff0000;
uint32_t rr, rg, rb;
rr = sr > dr ? sr : dr;
rg = sg > dg ? sg : dg;
rb = sb > db ? sb : db;
r = rr | rg | rb;
}
break;
case OPERATION_SUB_OVER:
{
uint32_t u, v;
u = ((s & 0xff00ff) - (d & 0xff00ff)) & 0xff00ff;
v = ((s & 0x00ff00) - (d & 0x00ff00)) & 0x00ff00;
r = u | v;
}
break;
case OPERATION_AD_MIN:
{
uint32_t sr, sg, sb;
sr = s & 0x0000ff; sg = s & 0x00ff00; sb = s & 0xff0000;
uint32_t dr, dg, db;
dr = d & 0x0000ff; dg = d & 0x00ff00; db = d & 0xff0000;
uint32_t rr, rg, rb;
rr = sr < dr ? sr : dr;
rg = sg < dg ? sg : dg;
rb = sb < db ? sb : db;
r = rr | rg | rb;
}
break;
default:
MCUnreachableReturn(0);
}
if (x_src_alpha && x_dst_alpha)
{
uint8_t ra;
ra = downscale(sa * da);
r = packed_bilinear_bounded(src, 255 - da, dst, 255 - sa) + packed_scale_bounded(r | 0xff000000, ra);
}
else if (!x_src_alpha && x_dst_alpha)
r = packed_bilinear_bounded(src, 255 - da, r | 0xff000000, da);
else if (!x_dst_alpha && x_src_alpha)
r = packed_bilinear_bounded(dst, 255 - sa, r | 0xff000000, sa);
return r;
}
template<BasicImagingOperation x_combiner, bool x_dst_alpha, bool x_src_alpha> INLINE uint32_t basic_imaging_combiner(uint32_t dst, uint32_t src)
{
uint32_t r;
switch(x_combiner)
{
case OPERATION_BLEND_CLEAR:
r = 0;
break;
case OPERATION_BLEND_SRC:
r = src;
break;
case OPERATION_BLEND_DST:
r = dst;
break;
case OPERATION_BLEND_SRC_OVER:
if (x_src_alpha)
r = packed_scale_add_bounded(dst, packed_inverse_alpha(src), src);
else
r = src;
break;
case OPERATION_BLEND_DST_OVER:
if (x_dst_alpha)
r = packed_scale_add_bounded(src, packed_inverse_alpha(dst), dst);
else
r = dst;
break;
case OPERATION_BLEND_SRC_IN:
if (x_dst_alpha)
r = packed_scale_bounded(src, packed_alpha(dst));
else
r = src;
break;
case OPERATION_BLEND_DST_IN:
if (x_src_alpha)
r = packed_scale_bounded(dst, packed_alpha(src));
else
r = dst;
break;
case OPERATION_BLEND_SRC_OUT:
if (x_dst_alpha)
r = packed_scale_bounded(src, packed_inverse_alpha(dst));
else
r = 0;
break;
case OPERATION_BLEND_DST_OUT:
if (x_src_alpha)
r = packed_scale_bounded(dst, packed_inverse_alpha(src));
else
r = 0;
break;
case OPERATION_BLEND_SRC_ATOP:
if (x_dst_alpha && x_src_alpha)
r = packed_bilinear_bounded(src, packed_alpha(dst), dst, packed_inverse_alpha(src));
else if (!x_dst_alpha && x_src_alpha)
r = packed_scale_add_bounded(dst, packed_inverse_alpha(src), src);
else if (!x_src_alpha && x_dst_alpha)
r = packed_scale_bounded(src, packed_alpha(dst));
else
r = src;
break;
case OPERATION_BLEND_DST_ATOP:
if (x_dst_alpha && x_src_alpha)
r = packed_bilinear_bounded(dst, packed_alpha(src), src, packed_inverse_alpha(dst));
else if (!x_src_alpha && x_dst_alpha)
r = packed_scale_add_bounded(src, packed_inverse_alpha(dst), dst);
else if (!x_dst_alpha && x_src_alpha)
r = packed_scale_bounded(dst, packed_alpha(src));
else
r = dst;
break;
case OPERATION_BLEND_XOR:
if (x_dst_alpha && x_src_alpha)
r = packed_bilinear_bounded(src, packed_inverse_alpha(dst), dst, packed_inverse_alpha(src));
else if (!x_dst_alpha && x_src_alpha)
r = packed_scale_bounded(dst, packed_inverse_alpha(src));
else if (!x_src_alpha && x_dst_alpha)
r = packed_scale_bounded(src, packed_inverse_alpha(dst));
else
r = 0;
break;
case OPERATION_BLEND_PLUS:
r = packed_add_saturated(src, dst);
break;
case OPERATION_BLEND_MULTIPLY:
if (x_dst_alpha && x_src_alpha)
r = packed_multiply_bounded(src, dst) + packed_bilinear_bounded(src, packed_inverse_alpha(dst), dst, packed_inverse_alpha(src));
else if (!x_dst_alpha && x_src_alpha)
r = packed_multiply_bounded(src, dst) + packed_scale_bounded(dst, packed_inverse_alpha(src));
else if (!x_src_alpha && x_dst_alpha)
r = packed_multiply_bounded(src, dst) + packed_scale_bounded(src, packed_inverse_alpha(dst));
else
r = packed_multiply_bounded(src, dst);
break;
case OPERATION_BLEND_SCREEN:
r = packed_multiply_bounded(src, packed_inverse(dst)) + dst;
break;
default:
MCUnreachableReturn(0);
}
return r;
}
template<AdvancedImagingOperation x_combiner, bool x_dst_alpha, bool x_src_alpha> INLINE uint32_t advanced_imaging_combiner(uint32_t dst, uint32_t src)
{
uint8_t t_src_red, t_src_green, t_src_blue, t_src_alpha;
uint8_t t_dst_red, t_dst_green, t_dst_blue, t_dst_alpha;
uint16_t t_dst_alpha_src_red, t_dst_alpha_src_green, t_dst_alpha_src_blue, t_dst_alpha_src_alpha, t_dst_alpha_dst_alpha;
uint16_t t_dst_alpha_dst_red, t_dst_alpha_dst_green, t_dst_alpha_dst_blue;
uint16_t t_src_alpha_dst_red, t_src_alpha_dst_green, t_src_alpha_dst_blue, t_src_alpha_dst_alpha;
uint16_t t_inv_dst_alpha_src_red, t_inv_dst_alpha_src_green, t_inv_dst_alpha_src_blue, t_inv_dst_alpha_src_alpha ATTRIBUTE_UNUSED;
uint16_t t_inv_src_alpha_dst_red, t_inv_src_alpha_dst_green, t_inv_src_alpha_dst_blue, t_inv_src_alpha_dst_alpha ATTRIBUTE_UNUSED;
uint32_t t_dst_alpha_dst_alpha_src_alpha;
uint8_t t_red, t_green, t_blue, t_alpha;
t_src_red = src & 0xff;
t_src_green = (src >> 8) & 0xff;
t_src_blue = (src >> 16) & 0xff;
if (x_src_alpha)
t_src_alpha = src >> 24;
else
t_src_alpha = 255;
t_dst_red = dst & 0xff;
t_dst_green = (dst >> 8) & 0xff;
t_dst_blue = (dst >>16) & 0xff;
if (x_dst_alpha)
t_dst_alpha = dst >> 24;
else
t_dst_alpha = 255;
if (t_src_alpha == 0)
{
t_src_alpha_dst_red = 0;
t_src_alpha_dst_green = 0;
t_src_alpha_dst_blue = 0;
t_src_alpha_dst_alpha = 0;
t_inv_src_alpha_dst_red = upscale(t_dst_red);
t_inv_src_alpha_dst_green = upscale(t_dst_green);
t_inv_src_alpha_dst_blue = upscale(t_dst_blue);
t_inv_src_alpha_dst_alpha = upscale(t_dst_alpha);
}
else if (t_src_alpha == 255)
{
t_src_alpha_dst_red = upscale(t_dst_red);
t_src_alpha_dst_green = upscale(t_dst_green);
t_src_alpha_dst_blue = upscale(t_dst_blue);
t_src_alpha_dst_alpha = upscale(t_dst_alpha);
t_inv_src_alpha_dst_red = 0;
t_inv_src_alpha_dst_green = 0;
t_inv_src_alpha_dst_blue = 0;
t_inv_src_alpha_dst_alpha = 0;
}
else
{
t_src_alpha_dst_red = t_src_alpha * t_dst_red;
t_src_alpha_dst_green = t_src_alpha * t_dst_green;
t_src_alpha_dst_blue = t_src_alpha * t_dst_blue;
t_src_alpha_dst_alpha = t_src_alpha * t_dst_alpha;
t_inv_src_alpha_dst_red = (255 - t_src_alpha) * t_dst_red;
t_inv_src_alpha_dst_green = (255 - t_src_alpha) * t_dst_green;
t_inv_src_alpha_dst_blue = (255 - t_src_alpha) * t_dst_blue;
t_inv_src_alpha_dst_alpha = (255 - t_src_alpha) * t_dst_alpha;
}
if (t_dst_alpha == 0)
{
t_dst_alpha_src_red = 0;
t_dst_alpha_src_green = 0;
t_dst_alpha_src_blue = 0;
t_dst_alpha_src_alpha = 0;
t_dst_alpha_dst_red = 0;
t_dst_alpha_dst_blue = 0;
t_dst_alpha_dst_green = 0;
t_dst_alpha_dst_alpha = 0;
t_dst_alpha_dst_alpha_src_alpha = 0;
t_inv_dst_alpha_src_red = upscale(t_src_red);
t_inv_dst_alpha_src_green = upscale(t_src_green);
t_inv_dst_alpha_src_blue = upscale(t_src_blue);
t_inv_dst_alpha_src_alpha = upscale(t_src_alpha);
}
else if (t_dst_alpha == 255)
{
t_dst_alpha_src_red = upscale(t_src_red);
t_dst_alpha_src_green = upscale(t_src_green);
t_dst_alpha_src_blue = upscale(t_src_blue);
t_dst_alpha_src_alpha = upscale(t_src_alpha);
t_dst_alpha_dst_red = upscale(t_dst_red);
t_dst_alpha_dst_green = upscale(t_dst_green);
t_dst_alpha_dst_blue = upscale(t_dst_blue);
t_dst_alpha_dst_alpha = upscale((uint8_t)255);
t_dst_alpha_dst_alpha_src_alpha = t_src_alpha == 255 ? upscale(upscale(uint8_t(255))) : upscale(uint16_t(255 * t_src_alpha));
t_inv_dst_alpha_src_red = 0;
t_inv_dst_alpha_src_green = 0;
t_inv_dst_alpha_src_blue = 0;
t_inv_dst_alpha_src_alpha = 0;
}
else
{
t_dst_alpha_src_red = t_dst_alpha * t_src_red;
t_dst_alpha_src_green = t_dst_alpha * t_src_green;
t_dst_alpha_src_blue = t_dst_alpha * t_src_blue;
t_dst_alpha_src_alpha = t_dst_alpha * t_src_alpha;
t_dst_alpha_dst_red = t_dst_alpha * t_dst_red;
t_dst_alpha_dst_green = t_dst_alpha * t_dst_green;
t_dst_alpha_dst_blue = t_dst_alpha * t_dst_blue;
t_dst_alpha_dst_alpha = t_dst_alpha * t_dst_alpha;
t_dst_alpha_dst_alpha_src_alpha = t_dst_alpha_dst_alpha * t_src_alpha;
t_inv_dst_alpha_src_red = (255 - t_dst_alpha) * t_src_red;
t_inv_dst_alpha_src_green = (255 - t_dst_alpha) * t_src_green;
t_inv_dst_alpha_src_blue = (255 - t_dst_alpha) * t_src_blue;
t_inv_dst_alpha_src_alpha = (255 - t_dst_alpha) * t_src_alpha;
}
switch(x_combiner)
{
case OPERATION_BLEND_OVERLAY:
t_red = 2 * t_dst_red < t_dst_alpha ?
downscale(2 * t_src_red * t_dst_red + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red) :
downscale(t_src_alpha_dst_alpha - 2 * (t_dst_alpha - t_dst_red) * (t_src_alpha - t_src_red) + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red);
t_green = 2 * t_dst_green < t_dst_alpha ?
downscale(2 * t_src_green * t_dst_green + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green) :
downscale(t_src_alpha_dst_alpha - 2 * (t_dst_alpha - t_dst_green) * (t_src_alpha - t_src_green) + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green);
t_blue = 2 * t_dst_blue < t_dst_alpha ?
downscale(2 * t_src_blue * t_dst_blue + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue) :
downscale(t_src_alpha_dst_alpha - 2 * (t_dst_alpha - t_dst_blue) * (t_src_alpha - t_src_blue) + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue);
t_alpha = t_src_alpha + t_dst_alpha - downscale(t_src_alpha_dst_alpha);
break;
case OPERATION_BLEND_DARKEN:
t_red = downscale(fastmin(t_dst_alpha_src_red, t_src_alpha_dst_red) + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red);
t_green = downscale(fastmin(t_dst_alpha_src_green, t_src_alpha_dst_green) + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green);
t_blue = downscale(fastmin(t_dst_alpha_src_blue, t_src_alpha_dst_blue) + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue);
t_alpha = t_src_alpha + t_dst_alpha - downscale(t_src_alpha_dst_alpha);
break;
case OPERATION_BLEND_LIGHTEN:
t_red = downscale(fastmax(t_dst_alpha_src_red, t_src_alpha_dst_red) + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red);
t_green = downscale(fastmax(t_dst_alpha_src_green, t_src_alpha_dst_green) + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green);
t_blue = downscale(fastmax(t_dst_alpha_src_blue, t_src_alpha_dst_blue) + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue);
t_alpha = t_src_alpha + t_dst_alpha - downscale(t_src_alpha_dst_alpha);
break;
case OPERATION_BLEND_DODGE:
t_red = t_dst_alpha_src_red + t_src_alpha_dst_red >= t_src_alpha_dst_alpha ?
downscale(t_dst_alpha_src_alpha + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red) :
downscale(scaled_divide(t_src_alpha_dst_red, t_src_alpha, t_src_alpha - t_src_red) + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red);
t_green = t_dst_alpha_src_green + t_src_alpha_dst_green >= t_src_alpha_dst_alpha ?
downscale(t_dst_alpha_src_alpha + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green) :
downscale(scaled_divide(t_src_alpha_dst_green, t_src_alpha, t_src_alpha - t_src_green) + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green);
t_blue = t_dst_alpha_src_blue + t_src_alpha_dst_blue >= t_src_alpha_dst_alpha ?
downscale(t_dst_alpha_src_alpha + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue) :
downscale(scaled_divide(t_src_alpha_dst_blue, t_src_alpha, t_src_alpha - t_src_blue) + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue);
t_alpha = t_src_alpha + t_dst_alpha - downscale(t_src_alpha_dst_alpha);
break;
case OPERATION_BLEND_BURN:
t_red = t_dst_alpha_src_red + t_src_alpha_dst_red <= t_src_alpha_dst_alpha ?
downscale(t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red) :
downscale(scaled_divide(t_dst_alpha_src_red + t_src_alpha_dst_red - t_src_alpha_dst_alpha, t_src_alpha, t_src_red) + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red);
t_green = t_dst_alpha_src_green + t_src_alpha_dst_green <= t_src_alpha_dst_alpha ?
downscale(t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green) :
downscale(scaled_divide(t_dst_alpha_src_green + t_src_alpha_dst_green - t_src_alpha_dst_alpha, t_src_alpha, t_src_green) + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green);
t_blue = t_dst_alpha_src_blue + t_src_alpha_dst_blue <= t_src_alpha_dst_alpha ?
downscale(t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue) :
downscale(scaled_divide(t_dst_alpha_src_blue + t_src_alpha_dst_blue - t_src_alpha_dst_alpha, t_src_alpha, t_src_blue) + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue);
t_alpha = t_src_alpha + t_dst_alpha - downscale(t_src_alpha_dst_alpha);
break;
case OPERATION_BLEND_HARD_LIGHT:
t_red = 2 * t_src_red < t_src_alpha ?
downscale(2 * t_src_red * t_dst_red + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red) :
downscale(t_src_alpha_dst_alpha - 2 * (t_dst_alpha - t_dst_red) * (t_src_alpha - t_src_red) + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red);
t_green = 2 * t_src_green < t_src_alpha ?
downscale(2 * t_src_green * t_dst_green + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green) :
downscale(t_src_alpha_dst_alpha - 2 * (t_dst_alpha - t_dst_green) * (t_src_alpha - t_src_green) + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green);
t_blue = 2 * t_src_blue < t_src_alpha ?
downscale(2 * t_src_blue * t_dst_blue + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue) :
downscale(t_src_alpha_dst_alpha - 2 * (t_dst_alpha - t_dst_blue) * (t_src_alpha - t_src_blue) + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue);
t_alpha = t_src_alpha + t_dst_alpha - downscale(t_src_alpha_dst_alpha);
break;
case OPERATION_BLEND_SOFT_LIGHT:
if (2 * t_src_red < t_src_alpha)
t_red = downscale(scaled_divide(t_src_alpha_dst_alpha + (t_dst_alpha - t_dst_red) * (t_src_alpha - 2 * t_src_red), t_dst_red, t_dst_alpha) + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red);
else if (8 * t_dst_red < t_dst_alpha)
t_red = downscale(long_scaled_divide(t_dst_alpha_dst_alpha_src_alpha - (t_dst_alpha - t_dst_red) * (2 * t_src_red - t_src_alpha) * (3 * t_dst_alpha - 8 * t_dst_red), t_dst_red, t_dst_alpha_dst_alpha) + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red);
else
t_red = downscale(t_src_alpha_dst_red + (sqrt(t_dst_alpha_dst_red) - t_dst_red) * (2 * t_src_red - t_src_alpha) + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red);
if (2 * t_src_green < t_src_alpha)
t_green = downscale(scaled_divide(t_src_alpha_dst_alpha + (t_dst_alpha - t_dst_green) * (t_src_alpha - 2 * t_src_green), t_dst_green, t_dst_alpha) + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green);
else if (8 * t_dst_green < t_dst_alpha)
t_green = downscale(long_scaled_divide(t_dst_alpha_dst_alpha_src_alpha - (t_dst_alpha - t_dst_green) * (2 * t_src_green - t_src_alpha) * (3 * t_dst_alpha - 8 * t_dst_green), t_dst_green, t_dst_alpha_dst_alpha) + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green);
else
t_green = downscale(t_src_alpha_dst_green + (sqrt(t_dst_alpha_dst_green) - t_dst_green) * (2 * t_src_green - t_src_alpha) + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green);
if (2 * t_src_blue < t_src_alpha)
t_blue = downscale(scaled_divide(t_src_alpha_dst_alpha + (t_dst_alpha - t_dst_blue) * (t_src_alpha - 2 * t_src_blue), t_dst_blue, t_dst_alpha) + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue);
else if (8 * t_dst_blue < t_dst_alpha)
t_blue = downscale(long_scaled_divide(t_dst_alpha_dst_alpha_src_alpha - (t_dst_alpha - t_dst_blue) * (2 * t_src_blue - t_src_alpha) * (3 * t_dst_alpha - 8 * t_dst_blue), t_dst_blue, t_dst_alpha_dst_alpha) + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue);
else
t_blue = downscale(t_src_alpha_dst_blue + (sqrt(t_dst_alpha_dst_blue) - t_dst_blue) * (2 * t_src_blue - t_src_alpha) + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue);
t_alpha = t_src_alpha + t_dst_alpha - downscale(t_src_alpha_dst_alpha);
break;
case OPERATION_BLEND_DIFFERENCE:
t_red = t_src_red + t_dst_red - 2 * downscale(fastmin(t_dst_alpha_src_red, t_src_alpha_dst_red));
t_green = t_src_green + t_dst_green - 2 * downscale(fastmin(t_dst_alpha_src_green, t_src_alpha_dst_green));
t_blue = t_src_blue + t_dst_blue - 2 * downscale(fastmin(t_dst_alpha_src_blue, t_src_alpha_dst_blue));
t_alpha = t_src_alpha + t_dst_alpha - downscale(t_src_alpha_dst_alpha);
break;
case OPERATION_BLEND_EXCLUSION:
t_red = downscale(t_src_red * (t_dst_alpha - t_dst_red) + t_dst_red * (t_src_alpha - t_src_red) + t_inv_dst_alpha_src_red + t_inv_src_alpha_dst_red);
t_green = downscale(t_src_green * (t_dst_alpha - t_dst_green) + t_dst_green * (t_src_alpha - t_src_green) + t_inv_dst_alpha_src_green + t_inv_src_alpha_dst_green);
t_blue = downscale(t_src_blue * (t_dst_alpha - t_dst_blue) + t_dst_blue * (t_src_alpha - t_src_blue) + t_inv_dst_alpha_src_blue + t_inv_src_alpha_dst_blue);
t_alpha = t_src_alpha + t_dst_alpha - downscale(t_src_alpha_dst_alpha);
break;
default:
MCUnreachableReturn(0);
}
if (x_dst_alpha)
return t_red | (t_green << 8) | (t_blue << 16) | (t_alpha << 24);
return t_red | (t_green << 8) | (t_blue << 16);
}
// These are commented out until a particular bug in Visual Studio (described
// below) is fixed.
//
/*template<BitwiseOperation x_combiner, bool x_dst_alpha, bool x_src_alpha> INLINE uint32_t pixel_combine(uint32_t dst, uint32_t src)
{
return bitwise_combiner<x_combiner, x_dst_alpha, x_src_alpha>(dst, src);
}
template<ArithmeticOperation x_combiner, bool x_dst_alpha, bool x_src_alpha> INLINE uint32_t pixel_combine(uint32_t dst, uint32_t src)
{
return arithmetic_combiner<x_combiner, x_dst_alpha, x_src_alpha>(dst, src);
}
template<BasicImagingOperation x_combiner, bool x_dst_alpha, bool x_src_alpha> INLINE uint32_t pixel_combine(uint32_t dst, uint32_t src)
{
return basic_imaging_combiner<x_combiner, x_dst_alpha, x_src_alpha>(dst, src);
}
template<AdvancedImagingOperation x_combiner, bool x_dst_alpha, bool x_src_alpha> INLINE uint32_t pixel_combine(uint32_t dst, uint32_t src)
{
return advanced_imaging_combiner<x_combiner, x_dst_alpha, x_src_alpha>(dst, src);
}*/
// Nasty shim to work around a Visual Studio compiler bug (it doesn't handle
// templates whose parameters are overloaded on enums properly so it always
// tries to use the AdvancedImagingOperation form of the template)
template<int x_combiner, bool x_dst_alpha, bool x_src_alpha> INLINE uint32_t pixel_combine(uint32_t dst, uint32_t src)
{
// These should get collapsed at compile-time so no need to worry about a
// performance hit (assuming the compiler is half-way sensible...)
if (x_combiner <= LAST_BITWISE_OPERATION)
return bitwise_combiner<BitwiseOperation(x_combiner), x_dst_alpha, x_src_alpha>(dst, src);
else if (x_combiner <= LAST_ARITHMETIC_OPERATION)
return arithmetic_combiner<ArithmeticOperation(x_combiner), x_dst_alpha, x_src_alpha>(dst, src);
else if (x_combiner <= LAST_BASIC_IMAGING_OPERATION)
return basic_imaging_combiner<BasicImagingOperation(x_combiner), x_dst_alpha, x_src_alpha>(dst, src);
else if (x_combiner <= LAST_ADVANCED_IMAGING_OPERATION)
return advanced_imaging_combiner<AdvancedImagingOperation(x_combiner), x_dst_alpha, x_src_alpha>(dst, src);
else
MCUnreachable();
}
template<class T, T x_combiner, bool x_dst_alpha, bool x_src_alpha>
INLINE void surface_combine(void *p_dst, int32_t p_dst_stride, const void *p_src, uint32_t p_src_stride, uint32_t p_width, uint32_t p_height, uint8_t p_opacity)
{
if (p_opacity == 0)
return;
uint32_t *t_dst_ptr;
uint32_t t_dst_stride;
t_dst_ptr = (uint32_t *)p_dst;
t_dst_stride = (p_dst_stride >> 2) - p_width;
uint32_t *t_src_ptr;
uint32_t t_src_stride;
t_src_ptr = (uint32_t *)p_src;
t_src_stride = (p_src_stride >> 2) - p_width;
for(; p_height > 0; --p_height, t_dst_ptr += t_dst_stride, t_src_ptr += t_src_stride)
{
for(uint32_t t_width = p_width; t_width > 0; --t_width)
{
uint32_t t_src, t_dst;
uint32_t t_pixel;
t_src = *t_src_ptr++;
t_dst = *t_dst_ptr;
t_pixel = pixel_combine<x_combiner, x_dst_alpha, x_src_alpha>(t_dst, t_src);
if (p_opacity != 255)
t_pixel = packed_bilinear_bounded(t_pixel, p_opacity, t_dst, 255 - p_opacity);
*t_dst_ptr++ = t_pixel;
}
}
}
// These are commented out until a particular bug in Visual Studio (described
// below) is fixed.
//
/*template <BitwiseOperation x_combiner, bool x_dst_alpha, bool x_src_alpha>
void surface_combine(void *p_dst, int32_t p_dst_stride, const void *p_src, uint32_t p_src_stride, uint32_t p_width, uint32_t p_height, uint8_t p_opacity)
{
return surface_combine<BitwiseOperation, x_combiner, x_dst_alpha, x_src_alpha>(p_dst, p_dst_stride, p_src, p_src_stride, p_width, p_height, p_opacity);
}
template <ArithmeticOperation x_combiner, bool x_dst_alpha, bool x_src_alpha>
void surface_combine(void *p_dst, int32_t p_dst_stride, const void *p_src, uint32_t p_src_stride, uint32_t p_width, uint32_t p_height, uint8_t p_opacity)
{
return surface_combine<ArithmeticOperation, x_combiner, x_dst_alpha, x_src_alpha>(p_dst, p_dst_stride, p_src, p_src_stride, p_width, p_height, p_opacity);
}
template <BasicImagingOperation x_combiner, bool x_dst_alpha, bool x_src_alpha>
void surface_combine(void *p_dst, int32_t p_dst_stride, const void *p_src, uint32_t p_src_stride, uint32_t p_width, uint32_t p_height, uint8_t p_opacity)
{
return surface_combine<BasicImagingOperation, x_combiner, x_dst_alpha, x_src_alpha>(p_dst, p_dst_stride, p_src, p_src_stride, p_width, p_height, p_opacity);
}
template <AdvancedImagingOperation x_combiner, bool x_dst_alpha, bool x_src_alpha>
void surface_combine(void *p_dst, int32_t p_dst_stride, const void *p_src, uint32_t p_src_stride, uint32_t p_width, uint32_t p_height, uint8_t p_opacity)
{
return surface_combine<AdvancedImagingOperation, x_combiner, x_dst_alpha, x_src_alpha>(p_dst, p_dst_stride, p_src, p_src_stride, p_width, p_height, p_opacity);
}*/
// Nasty shim to work around a Visual Studio compiler bug (it doesn't handle
// templates whose parameters are overloaded on enums properly so it always
// tries to use the AdvancedImagingOperation form of the template)
template<int x_combiner, bool x_dst_alpha, bool x_src_alpha>
INLINE void surface_combine(void *p_dst, int32_t p_dst_stride, const void *p_src, uint32_t p_src_stride, uint32_t p_width, uint32_t p_height, uint8_t p_opacity)
{
// These should get collapsed at compile-time so no need to worry about a
// performance hit (assuming the compiler is half-way sensible...)
if (x_combiner <= LAST_BITWISE_OPERATION)
return surface_combine<BitwiseOperation, BitwiseOperation(x_combiner), x_dst_alpha, x_src_alpha>(p_dst, p_dst_stride, p_src, p_src_stride, p_width, p_height, p_opacity);
else if (x_combiner <= LAST_ARITHMETIC_OPERATION)
return surface_combine<ArithmeticOperation, ArithmeticOperation(x_combiner), x_dst_alpha, x_src_alpha>(p_dst, p_dst_stride, p_src, p_src_stride, p_width, p_height, p_opacity);
else if (x_combiner <= LAST_BASIC_IMAGING_OPERATION)