forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegacygradients.cpp
More file actions
1039 lines (891 loc) · 29.2 KB
/
Copy pathlegacygradients.cpp
File metadata and controls
1039 lines (891 loc) · 29.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 "graphics.h"
#include "graphics-internal.h"
////////////////////////////////////////////////////////////////////////////////
#if __BIG_ENDIAN__
#define iman_ 1
#else
#define iman_ 0
#endif
typedef long int32;
#ifdef _LINUX
//IM - fast double -> int code seems to be broken on linux
inline int32 fast_rint(double val) {
return (int32)(val + 0.5);
}
inline int32 fast_floor(double val) {
return (int32)val;
}
#else
/* Fast version of (int)rint()
Works for -2147483648.5 .. 2147483647.49975574019
Requires IEEE floating point.
*/
inline int32 fast_rint(double val) {
val = val + 68719476736.0*65536.0*1.5;
return ((int32*)&val)[iman_];
}
/* Fast version of (int)floor()
Requires IEEE floating point.
Rounds numbers greater than n.9999923668 to n+1 rather than n,
this could be fixed by changing the FP rounding mode and using
the fast_rint() code.
Works for -32728 to 32727.99999236688
The alternative that uses long-long works for -2147483648 to
2147483647.999923688
*/
inline int32 fast_floor(double val) {
val = val + (68719476736.0*1.5);
#if 0
return (int32)((*(long long *)&val)>>16);
#else
return (((int32*)&val)[iman_]>>16);
#endif
}
#endif
#define STOP_DIFF_PRECISION 24
#define STOP_DIFF_MULT ((1 << STOP_DIFF_PRECISION) * (uint4)255)
#define STOP_INT_PRECISION 16
#define STOP_INT_MAX ((1 << STOP_INT_PRECISION) - 1)
#define STOP_INT_MIRROR_MAX ((2 << STOP_INT_PRECISION) - 1)
#define GRADIENT_ROUND_EPSILON (float)0.000005
#define GRADIENT_AA_SCALE (2)
////////////////////////////////////////////////////////////////////////////////
typedef unsigned char uint1;
typedef char int1;
typedef unsigned short uint2;
typedef short int2;
typedef unsigned int uint4;
typedef int int4;
typedef float real4;
typedef double real8;
////////////////////////////////////////////////////////////////////////////////
enum MCGradientFillKind
{
kMCGradientKindNone,
kMCGradientKindLinear = 3,
kMCGradientKindRadial,
kMCGradientKindConical,
kMCGradientKindDiamond,
kMCGradientKindSpiral,
kMCGradientKindXY,
kMCGradientKindSqrtXY
};
struct MCPoint
{
int2 x, y;
};
struct MCGradientFillStop
{
uint4 offset;
uint4 color;
uint4 hw_color;
uint4 difference;
};
struct MCCombiner
{
void (*begin)(MCCombiner *self, int4 y);
void (*advance)(MCCombiner *self, int4 y);
void (*blend)(MCCombiner *self, int4 fx, int4 tx, uint1 value);
void (*combine)(MCCombiner *self, int4 fx, int4 tx, uint1 *mask);
void (*end)(MCCombiner *self);
};
struct MCSurfaceCombiner: public MCCombiner
{
uint4 *bits;
int4 stride;
};
struct MCSolidCombiner: public MCSurfaceCombiner
{
uint4 pixel;
};
struct MCPatternCombiner: public MCSurfaceCombiner
{
uint4 *pattern_bits;
uint4 pattern_stride;
int4 origin_x;
int4 origin_y;
uint4 width;
uint4 height;
uint4 pattern_offset;
};
struct MCGradientCombiner: public MCSurfaceCombiner
{
MCGradientFillStop *ramp;
uint4 ramp_length;
MCPoint origin;
bool mirror;
uint4 repeat;
bool wrap;
};
struct MCGradientAffineCombiner: public MCGradientCombiner
{
int4 x_coef_a, x_coef_b, x_inc;
int4 y_coef_a, y_coef_b, y_inc;
uint4 buffer_width;
uint4* buffer;
};
////////////////////////////////////////////////////////////////////////////////
#ifdef __VISUALC__
#define PACKED_INLINE __forceinline
#else
#define PACKED_INLINE inline
#endif
// r_i = (x_i * a) / 255
PACKED_INLINE uint4 packed_scale_bounded(uint4 x, uint1 a)
{
uint4 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 + y_i * b) / 255
PACKED_INLINE uint4 packed_bilinear_bounded(uint4 x, uint1 a, uint4 y, uint1 b)
{
uint4 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;
}
PACKED_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);
}
PACKED_INLINE uint32_t _multiply_low(uint32_t x, uint32_t y)
{
return ((x & 0xff) * (y & 0xff)) | ((x & 0xff0000) * ((y >> 16) & 0xff));
}
PACKED_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 * y_i / 255;
PACKED_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 + y_i) / 2
PACKED_INLINE uint4 packed_avg(uint4 x, uint4 y)
{
uint4 u, v;
u = (((x & 0xff00ff) + (y & 0xff00ff)) >> 1) & 0xff00ff;
v = ((((x >> 8) & 0xff00ff) + ((y >> 8) & 0xff00ff)) << 7) & 0xff00ff00;
return u | v;
}
PACKED_INLINE float magic_sqrt (float number)
{
float x = number;
float xhalf = 0.5f*x;
int i = *(int*) &x;
i = 0x5f375a84 - ( i >> 1 );
x = *(float*) &i;
x = x*(1.5f-xhalf*x*x);
return number * x;
}
PACKED_INLINE float inv_sqrt (float x)
{
float xhalf = 0.5f*x;
int i = *(int*) &x;
i = 0x5f375a84 - ( i >> 1 );
x = *(float*) &i;
x = x*(1.5f-xhalf*x*x);
return x;
}
////////////////////////////////////////////////////////////////////////////////
inline uint4 MCU_abs(int4 source)
{
return source > 0 ? source : -source;
}
inline int4 MCU_min(int4 one, int4 two) {return one > two ? two : one;}
inline uint4 MCU_min(uint4 one, uint4 two) {return one > two ? two : one;}
inline int4 MCU_max(int4 one, int4 two) {return one > two ? one : two;}
inline uint4 MCU_max(uint4 one, uint4 two) {return one > two ? one : two;}
////////////////////////////////////////////////////////////////////////////////
static void gradient_combiner_begin(MCCombiner *_self, int4 y)
{
MCSolidCombiner *self = (MCSolidCombiner *)_self;
self -> bits += y * self -> stride;
}
static void gradient_combiner_end(MCCombiner *_self)
{
}
static void gradient_affine_combiner_advance(MCCombiner *_self, int4 dy)
{
MCGradientAffineCombiner *self = (MCGradientAffineCombiner *)_self;
self -> bits += dy * self -> stride;
self->x_inc += self->x_coef_b * dy;
self->y_inc += self->y_coef_b * dy;
}
#define FP_2PI ((int4)(2 * M_PI * (1<<8)))
#define FP_INV_2PI ((STOP_INT_MAX << 8) / FP_2PI)
template<MCGradientFillKind x_type> static inline int4 compute_index(int4 p_x, int4 p_y, bool p_mirror, uint4 p_repeat, bool p_wrap)
{
int4 t_index;
switch(x_type)
{
// Per gradient ramp index calculation
case kMCGradientKindLinear:
t_index = p_x;
break;
case kMCGradientKindConical:
{
int4 t_angle = fast_rint((atan2((double)p_y, p_x) * (1<<8)));
if (t_angle < 0)
t_angle += FP_2PI;
t_index = (t_angle * FP_INV_2PI) >> 8;
}
break;
case kMCGradientKindRadial:
{
real8 t_dist = ((real8)(p_x)*p_x + (real8)(p_y)*p_y);
t_index = !p_wrap && t_dist > ((real8)STOP_INT_MAX * STOP_INT_MAX) ? STOP_INT_MAX + 1 : fast_rint(sqrt(t_dist));
}
break;
case kMCGradientKindDiamond:
t_index = MCU_max(MCU_abs(p_x), MCU_abs(p_y));
break;
case kMCGradientKindSpiral:
{
int4 t_angle = fast_rint((atan2((double)p_y, p_x) * (1<<8)));
real8 t_dist = sqrt((real8)(p_x)*p_x + (real8)(p_y)*p_y);
t_index = fast_rint(t_dist);
if (t_angle > 0)
t_angle -= FP_2PI;
t_index -= (t_angle * FP_INV_2PI) >> 8;
t_index %= STOP_INT_MAX;
}
break;
case kMCGradientKindXY:
{
uint4 t_x = MCU_abs(p_x); uint4 t_y = MCU_abs(p_y);
t_index = (int4) ((int64_t)t_x * t_y / STOP_INT_MAX);
}
break;
case kMCGradientKindSqrtXY:
{
real8 t_x = MCU_abs(p_x); real8 t_y = MCU_abs(p_y);
t_index = fast_rint(sqrt(t_x * t_y));
}
break;
default:
//assert (false);
return 0;
}
if (p_mirror)
{
if (p_wrap)
{
if (p_repeat > 1)
t_index = (t_index * p_repeat);
t_index &= STOP_INT_MIRROR_MAX;
if (t_index > STOP_INT_MAX)
{
t_index = STOP_INT_MAX - (t_index & STOP_INT_MAX);
}
}
else
{
if (t_index >= STOP_INT_MAX)
{
if ((p_repeat & 1) == 0)
t_index = -t_index;
}
else if (p_repeat > 1 && t_index > 0)
{
t_index = (t_index * p_repeat);
t_index &= STOP_INT_MIRROR_MAX;
if (t_index > STOP_INT_MAX)
{
t_index = STOP_INT_MAX - (t_index & STOP_INT_MAX);
}
}
}
}
else
{
if (p_wrap)
t_index &= STOP_INT_MAX;
if (p_repeat > 1 && t_index > 0 && t_index < STOP_INT_MAX)
{
t_index = (t_index * p_repeat);
t_index &= 0xFFFF;
}
}
return t_index;
}
template<MCGradientFillKind x_type> static void MCGradientFillBlend(MCCombiner *_self, int4 fx, int4 tx, uint1 alpha)
{
MCGradientAffineCombiner *self = (MCGradientAffineCombiner*)_self;
uint4 *d;
uint4 s;
d = self -> bits;
int4 t_index;
int4 t_x = self->x_inc + self->x_coef_a * ((int4)fx);
int4 t_y = self->y_inc + self->y_coef_a * ((int4)fx);
int4 t_min = (int4)self->ramp[0].offset;
int4 t_max = (int4)self->ramp[self->ramp_length - 1].offset;
if (fx == tx) return;
bool t_mirror = self->mirror;
uint4 t_repeat = self->repeat;
if (alpha == 255)
{
uint4 t_stop_pos = 0;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat);
while (fx < tx)
{
if (t_index <= t_min)
{
s = self->ramp[0].hw_color;
s = packed_scale_bounded(s | 0xFF000000, s >> 24);
while (t_index <= t_min)
{
d[fx] = packed_scale_bounded(d[fx], 255 - (s >> 24)) + s;
fx += 1;
if (fx == tx)
return;
t_x += self->x_coef_a;
t_y += self->y_coef_a;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat);
}
}
if (t_index >= t_max)
{
s = self->ramp[self->ramp_length - 1].hw_color;
s = packed_scale_bounded(s | 0xFF000000, s >> 24);
while (t_index >= t_max)
{
d[fx] = packed_scale_bounded(d[fx], 255 - (s >> 24)) + s;
fx += 1;
if (fx == tx)
return;
t_x += self->x_coef_a;
t_y += self->y_coef_a;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat);
}
}
while (t_index >= t_min && t_index <= t_max)
{
MCGradientFillStop *t_current_stop = &self->ramp[t_stop_pos];
int4 t_current_offset = t_current_stop->offset;
int4 t_current_difference = t_current_stop->difference;
uint4 t_current_color = t_current_stop->hw_color;
MCGradientFillStop *t_next_stop = &self->ramp[t_stop_pos+1];
int4 t_next_offset = t_next_stop->offset;
uint4 t_next_color = t_next_stop->hw_color;
while (t_next_offset >= t_index && t_current_offset <= t_index)
{
uint1 b = ((t_index - t_current_offset) * t_current_difference) >> STOP_DIFF_PRECISION ;
uint1 a = 255 - b;
s = packed_bilinear_bounded(t_current_color, a, t_next_color, b);
d[fx] = packed_bilinear_bounded(d[fx], 255 - (s >> 24), s | 0xFF000000, s >> 24);
fx += 1;
if (fx == tx)
return;
t_x += self->x_coef_a;
t_y += self->y_coef_a;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat);
}
if (t_current_offset > t_index && t_stop_pos > 0)
t_stop_pos -= 1;
else if (t_next_offset < t_index && t_stop_pos < (self->ramp_length - 1))
t_stop_pos += 1;
}
}
}
}
template<MCGradientFillKind x_type> static void MCGradientFillCombine(MCCombiner *_self, int4 fx, int4 tx, uint1 *mask)
{
MCGradientAffineCombiner *self = (MCGradientAffineCombiner*)_self;
uint4 *d;
uint4 s;
d = self -> bits;
int4 t_index;
int4 t_x = self->x_inc + self->x_coef_a * ((int4)fx);
int4 t_y = self->y_inc + self->y_coef_a * ((int4)fx);
int4 t_min = (int4)self->ramp[0].offset;
int4 t_max = (int4)self->ramp[self->ramp_length - 1].offset;
if (fx == tx) return;
bool t_mirror = self->mirror;
uint4 t_repeat = self->repeat;
bool t_wrap = self->wrap;
uint4 t_stop_pos = 0;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat, t_wrap);
while (fx < tx)
{
if (t_index <= t_min)
{
s = self->ramp[0].hw_color;
while (t_index <= t_min)
{
uint4 sa = packed_scale_bounded(s | 0xFF000000, ((s >> 24) * *mask++) / 255);
d[fx] = packed_scale_bounded(d[fx], 255 - (sa >> 24)) + sa;
fx += 1;
if (fx == tx)
return;
t_x += self->x_coef_a;
t_y += self->y_coef_a;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat, t_wrap);
}
}
if (t_index >= t_max)
{
s = self->ramp[self->ramp_length - 1].hw_color;
while (t_index >= t_max)
{
uint4 sa = packed_scale_bounded(s | 0xFF000000, ((s >> 24) * *mask++) / 255);
d[fx] = packed_scale_bounded(d[fx], 255 - (sa >> 24)) + sa;
fx += 1;
if (fx == tx)
return;
t_x += self->x_coef_a;
t_y += self->y_coef_a;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat, t_wrap);
}
}
while (t_index >= t_min && t_index <= t_max)
{
MCGradientFillStop *t_current_stop = &self->ramp[t_stop_pos];
int4 t_current_offset = t_current_stop->offset;
int4 t_current_difference = t_current_stop->difference;
uint4 t_current_color = t_current_stop->hw_color;
MCGradientFillStop *t_next_stop = &self->ramp[t_stop_pos+1];
int4 t_next_offset = t_next_stop->offset;
uint4 t_next_color = t_next_stop->hw_color;
while (t_next_offset >= t_index && t_current_offset <= t_index)
{
uint1 b = ((t_index - t_current_offset) * t_current_difference) >> STOP_DIFF_PRECISION ;
uint1 a = 255 - b;
s = packed_bilinear_bounded(t_current_color, a, t_next_color, b);
uint4 sa = packed_scale_bounded(s | 0xFF000000, ((s >> 24) * *mask++) / 255);
d[fx] = packed_scale_bounded(d[fx], 255 - (sa >> 24)) + sa;
fx += 1;
if (fx == tx)
return;
t_x += self->x_coef_a;
t_y += self->y_coef_a;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat, t_wrap);
}
if (t_current_offset > t_index && t_stop_pos > 0)
t_stop_pos -= 1;
else if (t_next_offset < t_index && t_stop_pos < (self->ramp_length - 1))
t_stop_pos += 1;
}
}
}
template<MCGradientFillKind x_type> static void blend_row(MCCombiner *_self, uint4 fx, uint4 tx, uint4 *p_buff)
{
MCGradientAffineCombiner *self = (MCGradientAffineCombiner*)_self;
uint4 s;
int4 t_index;
int4 t_x = self->x_inc + self->x_coef_a * ((int4)fx);
int4 t_y = self->y_inc + self->y_coef_a * ((int4)fx);
int4 t_min = (int4)self->ramp[0].offset;
int4 t_max = (int4)self->ramp[self->ramp_length - 1].offset;
uint4 t_stop_pos = 0;
bool t_mirror = self->mirror;
uint4 t_repeat = self->repeat;
bool t_wrap = self->wrap;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat, t_wrap);
while (fx < tx)
{
if (t_index <= t_min)
{
s = self->ramp[0].hw_color;
while (t_index <= t_min)
{
*p_buff = s;
fx += 1;
if (fx == tx)
return;
p_buff++;
t_x += self->x_coef_a;
t_y += self->y_coef_a;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat, t_wrap);
}
}
if (t_index >= t_max)
{
s = self->ramp[self->ramp_length - 1].hw_color;
while (t_index >= t_max)
{
*p_buff = s;
fx += 1;
if (fx == tx)
return;
p_buff++;
t_x += self->x_coef_a;
t_y += self->y_coef_a;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat, t_wrap);
}
}
while (t_index >= t_min && t_index <= t_max)
{
MCGradientFillStop *t_current_stop = &self->ramp[t_stop_pos];
int4 t_current_offset = t_current_stop->offset;
int4 t_current_difference = t_current_stop->difference;
uint4 t_current_color = t_current_stop->hw_color;
MCGradientFillStop *t_next_stop = &self->ramp[t_stop_pos+1];
int4 t_next_offset = t_next_stop->offset;
uint4 t_next_color = t_next_stop->hw_color;
while (t_next_offset >= t_index && t_current_offset <= t_index)
{
uint1 b = ((t_index - t_current_offset) * t_current_difference) >> STOP_DIFF_PRECISION ;
uint1 a = 255 - b;
s = packed_bilinear_bounded(t_current_color, a, t_next_color, b);
*p_buff = s;
fx += 1;
if (fx == tx)
return;
p_buff++;
t_x += self->x_coef_a;
t_y += self->y_coef_a;
t_index = compute_index<x_type>(t_x, t_y, t_mirror, t_repeat, t_wrap);
}
if (t_current_offset > t_index && t_stop_pos > 0)
t_stop_pos -= 1;
else if (t_next_offset < t_index && t_stop_pos < (self->ramp_length - 1))
t_stop_pos += 1;
}
}
}
static void gradient_bilinear_affine_combiner_end(MCCombiner *_self)
{
MCGradientAffineCombiner *self = (MCGradientAffineCombiner*)_self;
delete self->buffer;
}
template<MCGradientFillKind x_type> static void MCGradientFillBilinearBlend(MCCombiner *_self, uint4 fx, uint4 tx, uint1 alpha)
{
MCGradientAffineCombiner *self = (MCGradientAffineCombiner*)_self;
uint4 *d;
uint4 s;
d = self -> bits;
if (fx == tx) return;
uint4 *t_buffer = self->buffer;
uint4 t_bufflen = self->buffer_width;
int4 x_a, x_b, x_inc;
int4 y_a, y_b, y_inc;
x_a = self->x_coef_a; x_b = self->x_coef_b; x_inc = self->x_inc;
y_a = self->y_coef_a; y_b = self->y_coef_b; y_inc = self->y_inc;
self->x_coef_a /= GRADIENT_AA_SCALE; self->x_coef_b /= GRADIENT_AA_SCALE;
self->y_coef_a /= GRADIENT_AA_SCALE; self->y_coef_b /= GRADIENT_AA_SCALE;
uint4 dx = (tx - fx) * GRADIENT_AA_SCALE;
uint4 t_fx = fx * GRADIENT_AA_SCALE;
for (int i = 0; i < GRADIENT_AA_SCALE; i++)
{
blend_row<x_type>(self, t_fx, t_fx + dx, t_buffer);
t_buffer += t_bufflen;
self->x_inc += self->x_coef_b;
self->y_inc += self->y_coef_b;
}
self->x_coef_a = x_a; self->x_coef_b = x_b; self->x_inc = x_inc;
self->y_coef_a = y_a; self->y_coef_b = y_b; self->y_inc = y_inc;
uint4 i = 0;
if (alpha == 255)
{
for (; fx < tx; fx++)
{
uint4 u;
uint4 v;
#if GRADIENT_AA_SCALE == 2
// unroll for GRADIENT_AA_SCALE == 2
u = (self->buffer[i*2] & 0xFF00FF) + (self->buffer[i*2 + 1] & 0xFF00FF) + \
(self->buffer[t_bufflen + i*2] & 0xFF00FF) + (self->buffer[t_bufflen + i*2 + 1] & 0xFF00FF);
v = ((self->buffer[i*2] >> 8) & 0xFF00FF) + ((self->buffer[i*2 + 1] >> 8) & 0xFF00FF) + \
((self->buffer[t_bufflen + i*2] >> 8) & 0xFF00FF) + ((self->buffer[t_bufflen + i*2 + 1] >> 8) & 0xFF00FF);
u = (u >> 2) & 0xFF00FF;
v = (v << 6) & 0xFF00FF00;
#endif
i++;
s = u | v;
d[fx] = packed_bilinear_bounded(d[fx], 255 - (s >> 24), s | 0xFF000000, s >> 24);
}
}
}
template<MCGradientFillKind x_type> static void MCGradientFillBilinearCombine(MCCombiner *_self, int4 fx, int4 tx, uint1* mask)
{
MCGradientAffineCombiner *self = (MCGradientAffineCombiner*)_self;
uint4 *d;
uint4 s;
d = self -> bits;
if (fx == tx) return;
uint4 *t_buffer = self->buffer;
uint4 t_bufflen = self->buffer_width;
int4 x_a, x_b, x_inc;
int4 y_a, y_b, y_inc;
x_a = self->x_coef_a; x_b = self->x_coef_b; x_inc = self->x_inc;
y_a = self->y_coef_a; y_b = self->y_coef_b; y_inc = self->y_inc;
self->x_coef_a /= GRADIENT_AA_SCALE; self->x_coef_b /= GRADIENT_AA_SCALE;
self->y_coef_a /= GRADIENT_AA_SCALE; self->y_coef_b /= GRADIENT_AA_SCALE;
uint4 dx = (tx - fx) * GRADIENT_AA_SCALE;
uint4 t_fx = fx * GRADIENT_AA_SCALE;
for (int i = 0; i < GRADIENT_AA_SCALE; i++)
{
blend_row<x_type>(self, t_fx, t_fx + dx, t_buffer);
t_buffer += t_bufflen;
self->x_inc += self->x_coef_b;
self->y_inc += self->y_coef_b;
}
self->x_coef_a = x_a; self->x_coef_b = x_b; self->x_inc = x_inc;
self->y_coef_a = y_a; self->y_coef_b = y_b; self->y_inc = y_inc;
uint4 i = 0;
for (; fx < tx; fx++)
{
uint4 u = (self->buffer[i*2] & 0xFF00FF) + (self->buffer[i*2 + 1] & 0xFF00FF) + \
(self->buffer[t_bufflen + i*2] & 0xFF00FF) + (self->buffer[t_bufflen + i*2 + 1] & 0xFF00FF);
uint4 v = ((self->buffer[i*2] >> 8) & 0xFF00FF) + ((self->buffer[i*2 + 1] >> 8) & 0xFF00FF) + \
((self->buffer[t_bufflen + i*2] >> 8) & 0xFF00FF) + ((self->buffer[t_bufflen + i*2 + 1] >> 8) & 0xFF00FF);
u = (u >> 2) & 0xFF00FF;
v = (v << 6) & 0xFF00FF00;
i++;
s = u | v;
uint1 alpha = (s >> 24) * (*mask++) / 255;
d[fx] = packed_bilinear_bounded(d[fx], 255 - alpha, s | 0xFF000000, alpha);
}
}
////////////////////////////////////////////////////////////////////////////////
void MCGradientFillDeleteCombiner(MCGradientAffineCombiner *p_combiner)
{
if (p_combiner == nil)
return;
MCMemoryDeleteArray(p_combiner->ramp);
MCMemoryDeleteArray(p_combiner->buffer);
MCMemoryDelete(p_combiner);
}
MCGradientAffineCombiner *MCGradientFillCreateCombiner(MCGGradientRef p_gradient_ref, MCGRectangle &r_clip, const MCGAffineTransform &p_transform)
{
// MM-2014-07-31: [[ ThreadedRendering ]] Removed use of single static combiner to make things thread safe.
MCAutoCustomPointer<MCGradientAffineCombiner, MCGradientFillDeleteCombiner> t_combiner;
if (!MCMemoryNew(&t_combiner))
return nil;
(*t_combiner) -> begin = gradient_combiner_begin;
(*t_combiner) -> advance = gradient_affine_combiner_advance;
(*t_combiner) -> combine = NULL;
MCGAffineTransform t_transform;
t_transform = MCGAffineTransformConcat(p_transform, p_gradient_ref -> transform);
int4 vx = (int4) t_transform . a;
int4 vy = (int4) t_transform . b;
int4 wx = (int4) t_transform . c;
int4 wy = (int4) t_transform . d;
int4 d = vy * wx - vx *wy;
uint1 t_kind;
switch (p_gradient_ref -> function)
{
case kMCGGradientFunctionLinear:
t_kind = kMCGradientKindLinear;
break;
case kMCGGradientFunctionRadial:
t_kind = kMCGradientKindRadial;
break;
case kMCGGradientFunctionSweep:
t_kind = kMCGradientKindConical;
break;
case kMCGLegacyGradientDiamond:
t_kind = kMCGradientKindDiamond;
break;
case kMCGLegacyGradientSpiral:
t_kind = kMCGradientKindSpiral;
break;
case kMCGLegacyGradientXY:
t_kind = kMCGradientKindXY;
break;
case kMCGLegacyGradientSqrtXY:
t_kind = kMCGradientKindSqrtXY;
break;
default:
// Unrecognised gradient type
return nil;
}
if (!MCMemoryNewArray(p_gradient_ref -> ramp_length, (*t_combiner)->ramp))
return nil;
uint32_t i;
for (i = 0; i < p_gradient_ref -> ramp_length; i++)
{
(*t_combiner)->ramp[i] . offset = (uint4) (p_gradient_ref -> stops[i] * STOP_INT_MAX);
(*t_combiner)->ramp[i] . color = p_gradient_ref -> colors[i];
if (i != 0)
{
// MM-2013-11-20: [[ Bug 11479 ]] Make sure we don't divide by zero.
if ((*t_combiner)->ramp[i] . offset != (*t_combiner)->ramp[i - 1] . offset)
(*t_combiner)->ramp[i - 1] . difference = (uint4) (STOP_DIFF_MULT / ((*t_combiner)->ramp[i] . offset - (*t_combiner)->ramp[i - 1] . offset));
else
(*t_combiner)->ramp[i - 1] . difference = (uint4) (STOP_DIFF_MULT / STOP_INT_MAX);
}
// AL-2014-07-21: [[ Bug 12867 ]] Ensure RBGA values are always packed in native format
(*t_combiner)->ramp[i] . hw_color = MCGPixelToNative(kMCGPixelFormatBGRA, (*t_combiner)->ramp[i] . color);
}
// MW-2013-10-26: [[ Bug 11315 ]] Index shuold be i - 1 (otherwise memory overrun occurs!).
(*t_combiner)->ramp[i - 1] . difference = (uint4) (STOP_DIFF_MULT / STOP_INT_MAX);
(*t_combiner) -> origin . x = (int2) t_transform . tx;
(*t_combiner) -> origin . y = (int2) t_transform . ty;
(*t_combiner) -> ramp_length = p_gradient_ref -> ramp_length;
(*t_combiner) -> mirror = p_gradient_ref -> mirror;
(*t_combiner) -> repeat = p_gradient_ref -> repeats;
(*t_combiner) -> wrap = p_gradient_ref -> wrap;
if (d != 0)
{
(*t_combiner) -> x_coef_a = STOP_INT_MAX * -wy / d;
(*t_combiner) -> x_coef_b = STOP_INT_MAX * wx / d;
(*t_combiner) -> x_inc = (uint4) (STOP_INT_MAX * (int64_t)((*t_combiner) -> origin . x * wy + ((int64_t) r_clip . origin .y - (*t_combiner) -> origin . y) * wx) / d);
(*t_combiner) -> y_coef_a = STOP_INT_MAX * vy / d;
(*t_combiner) -> y_coef_b = STOP_INT_MAX * -vx / d;
(*t_combiner) -> y_inc = (uint4) (STOP_INT_MAX * -(int64_t)((*t_combiner) -> origin . x * vy + ((int64_t) r_clip . origin .y - (*t_combiner) -> origin . y) * vx) / d);
}
// MM-2014-01-27: [[ UpdateImageFilters ]] Updated to use new libgraphics image filter types.
switch (p_gradient_ref -> filter)
{
case kMCGImageFilterNone:
{
(*t_combiner) -> end = gradient_combiner_end;
(*t_combiner) -> x_inc += ((*t_combiner) -> x_coef_a + (*t_combiner) -> x_coef_b) >> 1;
(*t_combiner) -> y_inc += ((*t_combiner) -> y_coef_a + (*t_combiner) -> y_coef_b) >> 1;
switch (t_kind)
{
case kMCGradientKindConical:
(*t_combiner) -> combine = MCGradientFillCombine<kMCGradientKindConical>;
return t_combiner.Take();
case kMCGradientKindLinear:
(*t_combiner) -> combine = MCGradientFillCombine<kMCGradientKindLinear>;
return t_combiner.Take();
case kMCGradientKindRadial:
(*t_combiner) -> combine = MCGradientFillCombine<kMCGradientKindRadial>;
return t_combiner.Take();
case kMCGradientKindDiamond:
(*t_combiner) -> combine = MCGradientFillCombine<kMCGradientKindDiamond>;
return t_combiner.Take();
case kMCGradientKindSpiral:
(*t_combiner) -> combine = MCGradientFillCombine<kMCGradientKindSpiral>;
return t_combiner.Take();
case kMCGradientKindXY:
(*t_combiner) -> combine = MCGradientFillCombine<kMCGradientKindXY>;
return t_combiner.Take();
case kMCGradientKindSqrtXY:
(*t_combiner) -> combine = MCGradientFillCombine<kMCGradientKindSqrtXY>;
return t_combiner.Take();
}
break;
}
case kMCGImageFilterLow:
case kMCGImageFilterMedium:
case kMCGImageFilterHigh:
{
(*t_combiner) -> end = gradient_bilinear_affine_combiner_end;
(*t_combiner) -> buffer_width = GRADIENT_AA_SCALE * (uint32_t) ceilf(r_clip . size . width);
if (!MCMemoryNewArray(GRADIENT_AA_SCALE * (*t_combiner) -> buffer_width, (*t_combiner) -> buffer))
return nil;
(*t_combiner) -> x_inc += ((*t_combiner) -> x_coef_a + (*t_combiner) -> x_coef_b) >> 2;
(*t_combiner) -> y_inc += ((*t_combiner) -> y_coef_a + (*t_combiner) -> y_coef_b) >> 2;
switch (t_kind)
{
case kMCGradientKindConical:
(*t_combiner) -> combine = MCGradientFillBilinearCombine<kMCGradientKindConical>;
return t_combiner.Take();
case kMCGradientKindLinear:
(*t_combiner) -> combine = MCGradientFillBilinearCombine<kMCGradientKindLinear>;
return t_combiner.Take();
case kMCGradientKindRadial:
(*t_combiner) -> combine = MCGradientFillBilinearCombine<kMCGradientKindRadial>;
return t_combiner.Take();
case kMCGradientKindDiamond:
(*t_combiner) -> combine = MCGradientFillBilinearCombine<kMCGradientKindDiamond>;
return t_combiner.Take();
case kMCGradientKindSpiral:
(*t_combiner) -> combine = MCGradientFillBilinearCombine<kMCGradientKindSpiral>;
return t_combiner.Take();
case kMCGradientKindXY:
(*t_combiner) -> combine = MCGradientFillBilinearCombine<kMCGradientKindXY>;
return t_combiner.Take();
case kMCGradientKindSqrtXY:
(*t_combiner) -> combine = MCGradientFillBilinearCombine<kMCGradientKindSqrtXY>;
return t_combiner.Take();
default:
return NULL;
}
break;
}
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
MCGLegacyGradientShader::MCGLegacyGradientShader(MCGGradientRef p_gradient_ref, MCGRectangle p_clip)
{
m_gradient_ref = MCGGradientRetain(p_gradient_ref);
m_clip = p_clip;
m_y = 0;
m_mask = NULL;
m_gradient_combiner = NULL;
}
MCGLegacyGradientShader::~MCGLegacyGradientShader()
{
MCGGradientRelease(m_gradient_ref);
MCMemoryDeallocate(m_mask);
MCGradientFillDeleteCombiner(m_gradient_combiner);
}
bool MCGLegacyGradientShader::setContext(const SkBitmap& p_bitmap, const SkPaint& p_paint, const SkMatrix& p_matrix)
{
MCGAffineTransform t_matrix;
MCGAffineTransformFromSkMatrix(p_matrix, t_matrix);
m_clip = MCGRectangleApplyAffineTransform(m_clip, t_matrix);
uint32_t t_width;
t_width = (uint32_t) ceilf(m_clip . size . width);
bool t_success;
t_success = true;
if (t_success)
t_success = MCMemoryAllocate(t_width, m_mask);
if (t_success)
{
memset(m_mask, 0xFF, t_width);
m_gradient_combiner = MCGradientFillCreateCombiner(m_gradient_ref, m_clip, t_matrix);
t_success = m_gradient_combiner != NULL;
}
if (t_success)
{