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 pathutils.cpp
More file actions
1321 lines (1088 loc) · 40.3 KB
/
utils.cpp
File metadata and controls
1321 lines (1088 loc) · 40.3 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"
#include <SkShader.h>
#include <SkGradientShader.h>
#include <SkMallocPixelRef.h>
#include <SkData.h>
////////////////////////////////////////////////////////////////////////////////
MCGSolidColorRef kMCGBlackSolidColor = nullptr;
#ifdef _DEBUG
size_t MCGObject::s_object_count = 0;
#endif
void MCGraphicsInitialize(void)
{
MCGPlatformInitialize();
MCGTextMeasureCacheInitialize();
MCGBlendModesInitialize();
MCGSolidColor::Create(0.0, 0.0, 0.0, 1.0, kMCGBlackSolidColor);
}
void MCGraphicsFinalize(void)
{
MCGRelease(kMCGBlackSolidColor);
MCGPlatformFinalize();
MCGTextMeasureCacheFinalize();
MCGBlendModesFinalize();
#ifdef _DEBUG
MCLog("Graphic object count = %d", MCGObject::s_object_count);
#endif
}
void MCGraphicsCompact(void)
{
MCGTextMeasureCacheCompact();
}
////////////////////////////////////////////////////////////////////////////////
bool MCGSolidColor::Apply(SkPaint& r_paint)
{
r_paint.setColor(MCGColorToSkColor(m_color));
return true;
}
bool MCGSolidColor::Create(MCGFloat p_red, MCGFloat p_green, MCGFloat p_blue, MCGFloat p_alpha, MCGSolidColorRef& r_solid_color)
{
MCGSolidColorRef t_color = new (nothrow) MCGSolidColor;
if (t_color == nullptr)
{
return false;
}
t_color->m_color = MCGColorMakeRGBA(p_red, p_green, p_blue, p_alpha);
r_solid_color = t_color;
return true;
}
////////////////////////////////////////////////////////////////////////////////
MCGPattern::~MCGPattern(void)
{
MCGImageRelease(m_image);
}
bool MCGPattern::Create(MCGImageRef p_image, MCGAffineTransform p_transform, MCGImageFilter p_filter, MCGPatternRef& r_pattern)
{
MCGPatternRef t_pattern = new (nothrow) MCGPattern;
if (t_pattern == nullptr)
{
return false;
}
t_pattern->m_image = MCGImageRetain(p_image);
t_pattern->m_transform = p_transform;
t_pattern->m_filter = p_filter;
r_pattern = t_pattern;
return true;
}
bool MCGPattern::Apply(SkPaint& r_paint)
{
SkMatrix t_transform;
MCGAffineTransformToSkMatrix(m_transform, t_transform);
sk_sp<SkShader> t_shader =
SkShader::MakeBitmapShader(*m_image->bitmap, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &t_transform);
if (t_shader == nullptr)
{
return false;
}
switch (m_filter)
{
case kMCGImageFilterNone:
r_paint.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality);
break;
case kMCGImageFilterLow:
r_paint.setFilterQuality(SkFilterQuality::kLow_SkFilterQuality);
break;
case kMCGImageFilterMedium:
r_paint.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
break;
case kMCGImageFilterHigh:
r_paint.setFilterQuality(SkFilterQuality::kHigh_SkFilterQuality);
break;
}
r_paint.setShader(t_shader);
return true;
}
////////////////////////////////////////////////////////////////////////////////
MCGRamp::~MCGRamp(void)
{
MCMemoryDeleteArray(m_stops);
MCMemoryDeleteArray(m_colors);
}
bool MCGRamp::Create(const MCGFloat *p_stops, const MCGColor *p_colors, size_t p_ramp_length, MCGRampRef& r_ramp)
{
MCGRampRef t_ramp = new (nothrow) MCGRamp;
if (t_ramp == nullptr)
{
return false;
}
if (MCMemoryNewArray(p_ramp_length, t_ramp->m_stops) &&
MCMemoryNewArray(p_ramp_length, t_ramp->m_colors))
{
for(size_t i = 0; i < p_ramp_length; i++)
{
t_ramp->m_stops[i] = p_stops[i];
t_ramp->m_colors[i] = MCGColorToSkColor(p_colors[i]);
}
t_ramp->m_ramp_length = p_ramp_length;
}
else
{
MCGRelease(t_ramp);
return false;
}
r_ramp = t_ramp;
return true;
}
bool MCGRamp::Create4f(const MCGFloat *p_stops, const MCGColor4f *p_colors, size_t p_ramp_length, MCGRampRef& r_ramp)
{
MCGRampRef t_ramp = new (nothrow) MCGRamp;
if (t_ramp == nullptr)
{
return false;
}
if (MCMemoryNewArray(p_ramp_length, t_ramp->m_stops) &&
MCMemoryNewArray(p_ramp_length, t_ramp->m_colors))
{
for(size_t i = 0; i < p_ramp_length; i++)
{
t_ramp->m_stops[i] = p_stops[i];
t_ramp->m_colors[i] = MCGColorMakeRGBA(p_colors[i].red, p_colors[i].green, p_colors[i].blue, p_colors[i].alpha);
}
t_ramp->m_ramp_length = p_ramp_length;
}
else
{
MCGRelease(t_ramp);
return false;
}
r_ramp = t_ramp;
return true;
}
/**/
static SkShader::TileMode MCGGradientSpreadMethodToSkTileMode(MCGGradientSpreadMethod p_method)
{
switch(p_method)
{
case kMCGGradientSpreadMethodPad:
return SkShader::kClamp_TileMode;
case kMCGGradientSpreadMethodRepeat:
return SkShader::kRepeat_TileMode;
case kMCGGradientSpreadMethodReflect:
return SkShader::kMirror_TileMode;
}
MCUnreachableReturn(SkShader::kClamp_TileMode);
}
MCGGradient::~MCGGradient(void)
{
MCGRelease(m_ramp);
}
/**/
bool MCGLinearGradient::Create(MCGPoint p_from, MCGPoint p_to, MCGRampRef p_ramp, MCGGradientSpreadMethod p_spread_method, MCGAffineTransform p_transform, MCGGradientRef& r_gradient)
{
MCGLinearGradientRef t_gradient = new (nothrow) MCGLinearGradient;
if (t_gradient == nullptr)
{
return false;
}
t_gradient->m_from = p_from;
t_gradient->m_to = p_to;
t_gradient->m_ramp = MCGRetain(p_ramp);
t_gradient->m_spread_method = p_spread_method;
t_gradient->m_transform = p_transform;
r_gradient = t_gradient;
return true;
}
bool MCGLinearGradient::Apply(SkPaint& r_paint)
{
SkMatrix t_transform;
MCGAffineTransformToSkMatrix(m_transform, t_transform);
SkPoint t_points[2] = {MCGPointToSkPoint(m_from), MCGPointToSkPoint(m_to)};
sk_sp<SkShader> t_shader =
SkGradientShader::MakeLinear(t_points,
m_ramp->GetColors(),
m_ramp->GetStops(),
m_ramp->GetLength(),
MCGGradientSpreadMethodToSkTileMode(m_spread_method),
0,
&t_transform);
if (t_shader == nullptr)
{
return false;
}
r_paint.setShader(t_shader);
return true;
}
/**/
bool MCGRadialGradient::Create(MCGPoint p_focal_point, MCGFloat p_radius, MCGRampRef p_ramp, MCGGradientSpreadMethod p_spread_method, MCGAffineTransform p_transform, MCGGradientRef& r_gradient)
{
MCGRadialGradientRef t_gradient = new (nothrow) MCGRadialGradient;
if (t_gradient == nullptr)
{
return false;
}
t_gradient->m_focal_point = p_focal_point;
t_gradient->m_radius = p_radius;
t_gradient->m_ramp = MCGRetain(p_ramp);
t_gradient->m_spread_method = p_spread_method;
t_gradient->m_transform = p_transform;
r_gradient = t_gradient;
return true;
}
bool MCGRadialGradient::Apply(SkPaint& r_paint)
{
SkMatrix t_transform;
MCGAffineTransformToSkMatrix(m_transform, t_transform);
sk_sp<SkShader> t_shader =
SkGradientShader::MakeRadial(MCGPointToSkPoint(m_focal_point),
m_radius,
m_ramp->GetColors(),
m_ramp->GetStops(),
m_ramp->GetLength(),
MCGGradientSpreadMethodToSkTileMode(m_spread_method),
0,
&t_transform);
if (t_shader == nullptr)
{
return false;
}
r_paint.setShader(t_shader);
return true;
}
/**/
bool MCGConicalGradient::Create(MCGPoint p_center_point, MCGFloat p_radius, MCGPoint p_focal_point, MCGFloat p_focal_radius, MCGRampRef p_ramp, MCGGradientSpreadMethod p_spread_method, MCGAffineTransform p_transform, MCGGradientRef& r_gradient)
{
MCGConicalGradientRef t_gradient = new (nothrow) MCGConicalGradient;
if (t_gradient == nullptr)
{
return false;
}
t_gradient->m_center_point = p_center_point;
t_gradient->m_radius = p_radius;
t_gradient->m_focal_point = p_focal_point;
t_gradient->m_focal_radius = p_focal_radius;
t_gradient->m_ramp = MCGRetain(p_ramp);
t_gradient->m_spread_method = p_spread_method;
t_gradient->m_transform = p_transform;
r_gradient = t_gradient;
return true;
}
bool MCGConicalGradient::Apply(SkPaint& r_paint)
{
SkMatrix t_transform;
MCGAffineTransformToSkMatrix(m_transform, t_transform);
sk_sp<SkShader> t_shader =
SkGradientShader::MakeTwoPointConical(MCGPointToSkPoint(m_center_point),
m_radius,
MCGPointToSkPoint(m_focal_point),
m_focal_radius,
m_ramp->GetColors(),
m_ramp->GetStops(),
m_ramp->GetLength(),
MCGGradientSpreadMethodToSkTileMode(m_spread_method),
0,
&t_transform);
if (t_shader == nullptr)
{
return false;
}
r_paint.setShader(t_shader);
return true;
}
/**/
bool MCGSweepGradient::Create(MCGPoint p_center, MCGRampRef p_ramp, MCGGradientSpreadMethod p_spread_method, MCGAffineTransform p_transform, MCGGradientRef& r_gradient)
{
MCGSweepGradientRef t_gradient = new (nothrow) MCGSweepGradient;
if (t_gradient == nullptr)
{
return false;
}
t_gradient->m_center = p_center;
t_gradient->m_ramp = MCGRetain(p_ramp);
t_gradient->m_spread_method = p_spread_method;
t_gradient->m_transform = p_transform;
r_gradient = t_gradient;
return true;
}
bool MCGSweepGradient::Apply(SkPaint& r_paint)
{
SkMatrix t_transform;
MCGAffineTransformToSkMatrix(m_transform, t_transform);
sk_sp<SkShader> t_shader =
SkGradientShader::MakeSweep(m_center.x, m_center.y,
m_ramp->GetColors(),
m_ramp->GetStops(),
m_ramp->GetLength(),
0,
&t_transform);
if (t_shader == nullptr)
{
return false;
}
r_paint.setShader(t_shader);
return true;
}
bool MCGGeneralizedGradient::Create(MCGGradientFunction p_function, MCGRampRef p_ramp, bool p_mirror, bool p_wrap, uint32_t p_repeats, MCGAffineTransform p_transform, MCGImageFilter p_filter, MCGGradientRef& r_gradient)
{
MCGGeneralizedGradientRef t_gradient = new (nothrow) MCGGeneralizedGradient;
if (t_gradient == nullptr)
{
return false;
}
t_gradient->m_function = p_function;
t_gradient->m_ramp = MCGRetain(p_ramp);
t_gradient->m_mirror = p_mirror;
t_gradient->m_wrap = p_wrap;
t_gradient->m_repeats = p_repeats;
t_gradient->m_filter = p_filter;
t_gradient->m_transform = p_transform;
r_gradient = t_gradient;
return true;
}
bool MCGGeneralizedGradient::Apply(SkPaint& r_paint)
{
return MCGGeneralizedGradientShaderApply(this, r_paint);
}
////////////////////////////////////////////////////////////////////////////////
static void MCGDashesDestroy(MCGDashesRef self)
{
if (self != NULL)
{
MCMemoryDeleteArray(self -> lengths);
MCMemoryDelete(self);
}
}
bool MCGDashesCreate(MCGFloat p_phase, const MCGFloat *p_lengths, uindex_t p_arity, MCGDashesRef& r_dashes)
{
bool t_success;
t_success = true;
__MCGDashes *t_dashes;
t_dashes = NULL;
if (t_success)
t_success = MCMemoryNew(t_dashes);
MCGFloat *t_lengths = NULL;
if (t_success)
t_success = MCMemoryNewArray(p_arity, t_lengths);
if (t_success)
{
for (uint32_t i = 0; i < p_arity; i++)
t_lengths[i] = p_lengths[i];
t_dashes -> lengths = t_lengths;
t_dashes -> count = p_arity;
t_dashes -> phase = p_phase;
t_dashes -> references = 1;
}
if (t_success)
r_dashes = t_dashes;
else
{
MCMemoryDeleteArray(t_lengths);
MCMemoryDelete(t_dashes);
}
return t_success;
}
MCGDashesRef MCGDashesRetain(MCGDashesRef self)
{
if (self != NULL)
self -> references++;
return self;
}
void MCGDashesRelease(MCGDashesRef self)
{
if (self != NULL)
{
self -> references--;
if (self -> references <= 0)
MCGDashesDestroy(self);
}
}
bool MCGDashesToSkDashPathEffect(MCGDashesRef self, sk_sp<SkPathEffect>& r_path_effect)
{
if (self -> count <= 0)
{
r_path_effect = nullptr;
return true;
}
// Skia won't except odd numbers of dashes, so we must replicate in that case.
uint32_t t_dash_count =
(self -> count % 2) == 0 ? self -> count : self -> count * 2;
MCAutoPointer<SkScalar[]> t_dashes = new (nothrow) SkScalar[t_dash_count];
if (!t_dashes)
return false;
for (uint32_t i = 0; i < t_dash_count; i++)
t_dashes[i] = MCGFloatToSkScalar(self -> lengths[i % self -> count]);
sk_sp<SkPathEffect> t_dash_effect =
SkDashPathEffect::Make(*t_dashes, (int)t_dash_count, MCGFloatToSkScalar(self -> phase));
if (t_dash_effect == nullptr)
return false;
r_path_effect = t_dash_effect;
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool MCGMaskCreateWithInfoAndRelease(const MCGDeviceMaskInfo& p_info, MCGMaskRef& r_mask)
{
bool t_success;
t_success = true;
__MCGMask *t_mask;
t_mask = NULL;
if (t_success)
t_success = MCMemoryNew(t_mask);
if (t_success)
{
switch (p_info . format)
{
case kMCGMaskFormat_A1:
t_mask -> mask . fFormat = SkMask::kBW_Format;
t_mask -> mask . fRowBytes = p_info . width;
break;
case kMCGMaskFormat_A8:
t_mask -> mask . fFormat = SkMask::kA8_Format;
t_mask -> mask . fRowBytes = p_info . width;
break;
case kMCGMaskFormat_LCD32:
MCUnreachable();
break;
}
t_mask -> mask . fBounds . setXYWH(p_info . x, p_info . y, p_info . width, p_info . height);
t_mask -> mask . fImage = (uint8_t *)p_info . data;
r_mask = t_mask;
}
return t_success;
}
void MCGMaskRelease(MCGMaskRef self)
{
if (self != NULL && self -> mask . fImage != NULL)
SkMask::FreeImage(self -> mask . fImage);
MCMemoryDelete(self);
}
MCGRectangle MCGMaskGetBounds(MCGMaskRef self)
{
if (self == NULL)
return MCGRectangleMake(0.0f, 0.0f, 0.0f, 0.0f);
MCGRectangle t_bounds;
t_bounds . origin . x = (MCGFloat)self -> mask . fBounds . x();
t_bounds . origin . y = (MCGFloat)self -> mask . fBounds . y();
t_bounds . size . width = (MCGFloat)self -> mask . fBounds . width();
t_bounds . size . height = (MCGFloat)self -> mask . fBounds . height();
return t_bounds;
}
////////////////////////////////////////////////////////////////////////////////
void MCGBlendModesInitialize(void)
{
}
void MCGBlendModesFinalize(void)
{
}
SkBlendMode MCGBlendModeToSkBlendMode(MCGBlendMode p_mode)
{
switch (p_mode)
{
case kMCGBlendModeClear:
return SkBlendMode::kClear;
case kMCGBlendModeCopy:
case kMCGBlendModeSourceOver:
return SkBlendMode::kSrcOver;
case kMCGBlendModeSourceIn:
return SkBlendMode::kSrcIn;
case kMCGBlendModeSourceOut:
return SkBlendMode::kSrcOut;
case kMCGBlendModeSourceAtop:
return SkBlendMode::kSrcATop;
case kMCGBlendModeDestinationOver:
return SkBlendMode::kDstOver;
case kMCGBlendModeDestinationIn:
return SkBlendMode::kDstIn;
case kMCGBlendModeDestinationOut:
return SkBlendMode::kDstOut;
case kMCGBlendModeDestinationAtop:
return SkBlendMode::kDstATop;
case kMCGBlendModeXor:
return SkBlendMode::kXor;
case kMCGBlendModePlusDarker:
case kMCGBlendModePlusLighter:
return SkBlendMode::kPlus;
case kMCGBlendModeMultiply:
return SkBlendMode::kMultiply;
case kMCGBlendModeScreen:
return SkBlendMode::kScreen;
case kMCGBlendModeOverlay:
return SkBlendMode::kOverlay;
case kMCGBlendModeDarken:
return SkBlendMode::kDarken;
case kMCGBlendModeLighten:
return SkBlendMode::kLighten;
case kMCGBlendModeColorDodge:
return SkBlendMode::kColorDodge;
case kMCGBlendModeColorBurn:
return SkBlendMode::kColorBurn;
case kMCGBlendModeSoftLight:
return SkBlendMode::kSoftLight;
case kMCGBlendModeHardLight:
return SkBlendMode::kHardLight;
case kMCGBlendModeDifference:
return SkBlendMode::kDifference;
case kMCGBlendModeExclusion:
return SkBlendMode::kExclusion;
case kMCGBlendModeHue:
return SkBlendMode::kHue;
case kMCGBlendModeSaturation:
return SkBlendMode::kSaturation;
case kMCGBlendModeColor:
return SkBlendMode::kColor;
case kMCGBlendModeLuminosity:
return SkBlendMode::kLuminosity;
default:
return SkBlendMode::kSrcOver;
MCUnreachableReturn(SkBlendMode::kSrcOver);
}
}
////////////////////////////////////////////////////////////////////////////////
bool MCGRasterToSkBitmap(const MCGRaster& p_raster, MCGPixelOwnershipType p_ownership, SkBitmap& r_bitmap)
{
bool t_success;
t_success = true;
if (t_success)
{
SkColorType t_color_type = MCGRasterFormatToSkBitmapConfig(p_raster . format);
SkImageInfo t_info = SkImageInfo::Make(p_raster.width, p_raster.height, t_color_type, p_raster.format == kMCGRasterFormat_xRGB ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
r_bitmap.setInfo(t_info);
// for non-premultiplied bitmaps, allocate the space then set pixels one by one
// for premultiplied bitmaps, just set the pixels in the target directly
// if the copy pixels flag is set, allocate space and copy the pixels from the raster first, then set in target
// IM-2014-09-16: [[ Bug 13458 ]] Don't copy if we're taking ownership. instead we'll premultiply in place below
if (p_raster . format == kMCGRasterFormat_U_ARGB && p_ownership != kMCGPixelOwnershipTypeTake)
p_ownership = kMCGPixelOwnershipTypeCopy;
switch (p_ownership)
{
case kMCGPixelOwnershipTypeBorrow:
r_bitmap.installPixels(t_info, p_raster.pixels, p_raster.stride);
break;
case kMCGPixelOwnershipTypeTake:
{
sk_sp<SkData> t_data;
if (t_success)
{
t_data = SkData::MakeFromMalloc(p_raster.pixels, p_raster.stride * p_raster.height);
t_success = t_data != nullptr;
}
SkMallocPixelRef *t_pixelref = nullptr;
if (t_success)
{
t_pixelref = SkMallocPixelRef::NewWithData(t_info, p_raster.stride, nullptr, t_data.get());
t_success = t_pixelref != nullptr;
}
if (t_success)
{
r_bitmap.setPixelRef(t_pixelref);
t_pixelref -> unref();
}
if (!t_success)
{
if (t_pixelref != NULL)
t_pixelref -> unref();
}
break;
}
case kMCGPixelOwnershipTypeCopy:
t_success = r_bitmap.tryAllocPixels(t_info);
break;
}
}
if (t_success)
{
if (p_raster . format == kMCGRasterFormat_U_ARGB)
{
r_bitmap.lockPixels();
// for non-premultiplied bitmaps, loop through the source bitmap pixel by pixel
// premultiplying each pixel before writing to destination bitmap
uint8_t *t_dst_ptr;
t_dst_ptr = (uint8_t*)r_bitmap.getPixels();
uint32_t t_dst_stride;
t_dst_stride = r_bitmap.rowBytes();
uint8_t *t_src_ptr;
uint32_t t_src_stride;
// IM-2014-09-16: [[ Bug 13458 ]] If we're taking ownership then the bitmap pixels are
// both source and destination
if (p_ownership == kMCGPixelOwnershipTypeTake)
{
t_src_ptr = t_dst_ptr;
t_src_stride = t_dst_stride;
}
else
{
t_src_ptr = (uint8_t*)p_raster.pixels;
t_src_stride = p_raster.stride;
}
for (uint32_t y = 0; y < p_raster . height; y++)
{
uint32_t *t_src_pixel;
t_src_pixel = (uint32_t*)t_src_ptr;
uint32_t *t_dst_pixel;
t_dst_pixel = (uint32_t*)t_dst_ptr;
// IM-2014-07-23: [[ Bug 12892 ]] Use MCGPixel function to premultiply native format pixels
for (uint32_t x = 0; x < p_raster . width; x++)
*t_dst_pixel++ = MCGPixelPreMultiplyNative(*t_src_pixel++);
t_dst_ptr += t_dst_stride;
t_src_ptr += t_src_stride;
}
r_bitmap.unlockPixels();
}
else if (p_ownership == kMCGPixelOwnershipTypeCopy)
MCMemoryCopy(r_bitmap . getPixels(), p_raster . pixels, p_raster . height * p_raster . stride);
}
return t_success;
}
////////////////////////////////////////////////////////////////////////////////
MCGPoint MCGPointApplyAffineTransform(const MCGPoint& p_point, const MCGAffineTransform& p_transform)
{
MCGPoint t_transformed_pt;
t_transformed_pt . x = p_transform . a * p_point . x + p_transform . c * p_point . y + p_transform . tx;
t_transformed_pt . y = p_transform . b * p_point . x + p_transform . d * p_point . y + p_transform . ty;
return t_transformed_pt;
}
MCGSize MCGSizeApplyAffineTransform(const MCGSize& p_point, const MCGAffineTransform& p_transform)
{
MCGSize t_transformed_pt;
t_transformed_pt . width = p_transform . a * p_point . width + p_transform . c * p_point . height;
t_transformed_pt . height = p_transform . b * p_point . width + p_transform . d * p_point . height;
return t_transformed_pt;
}
MCGRectangle MCGRectangleApplyAffineTransform(const MCGRectangle& p_rect, const MCGAffineTransform& p_transform)
{
MCGRectangle t_transformed_rect;
if (p_transform . b == 0.0f && p_transform . c == 0.0f)
{
// we are just translating or scaling (or both) the rectangle
// so the basic rectangle shape remains the same, we just need to translate the origin and scale the width and height
t_transformed_rect . origin = MCGPointApplyAffineTransform(p_rect . origin, p_transform);
t_transformed_rect . size = MCGSizeApplyAffineTransform(p_rect . size, p_transform);
}
else
{
// we are skewing the rectangle
// so we'll need to work out where the four new corners of the skewed rectangle lie
// and then return the rectangle which bounds the new skewed rectangle
MCGPoint t_top_left;
t_top_left = MCGPointApplyAffineTransform(p_rect . origin, p_transform);
MCGPoint t_top_right;
t_top_right . x = p_rect . origin . x + p_rect . size . width;
t_top_right . y = p_rect . origin . y;
t_top_right = MCGPointApplyAffineTransform(t_top_right, p_transform);
MCGPoint t_bottom_left;
t_bottom_left . x = p_rect . origin . x;
t_bottom_left . y = p_rect . origin . y + p_rect . size . height;
t_bottom_left = MCGPointApplyAffineTransform(t_bottom_left, p_transform);
MCGPoint t_bottom_right;
t_bottom_right . x = p_rect . origin . x + p_rect . size . width;
t_bottom_right . y = p_rect . origin . y + p_rect . size . height;
t_bottom_right = MCGPointApplyAffineTransform(t_bottom_right, p_transform);
MCGFloat t_max_x;
t_max_x = MCMax(MCMax(MCMax(t_top_left . x, t_top_right . x), t_bottom_left . x), t_bottom_right . x);
MCGFloat t_min_x;
t_min_x = MCMin(MCMin(MCMin(t_top_left . x, t_top_right . x), t_bottom_left . x), t_bottom_right . x);
MCGFloat t_max_y;
t_max_y = MCMax(MCMax(MCMax(t_top_left . y, t_top_right . y), t_bottom_left . y), t_bottom_right . y);
MCGFloat t_min_y;
t_min_y = MCMin(MCMin(MCMin(t_top_left . y, t_top_right . y), t_bottom_left . y), t_bottom_right . y);
t_transformed_rect . origin . x = t_min_x;
t_transformed_rect . origin . y = t_min_y;
t_transformed_rect . size . width = t_max_x - t_min_x;
t_transformed_rect . size . height = t_max_y - t_min_y;
}
return t_transformed_rect;
}
////////////////////////////////////////////////////////////////////////////////
MCGRectangle MCGRectangleIntersection(const MCGRectangle &p_rect_1, const MCGRectangle &p_rect_2)
{
MCGRectangle t_intersection;
t_intersection . origin . x = MCMax(p_rect_1 . origin . x, p_rect_2 . origin . x);
t_intersection . origin . y = MCMax(p_rect_1 . origin . y, p_rect_2 . origin . y);
MCGFloat t_right, t_bottom;
t_right = MCMin(p_rect_1 . origin . x + p_rect_1 . size . width, p_rect_2 . origin . x + p_rect_2 . size . width);
t_bottom = MCMin(p_rect_1 . origin . y + p_rect_1 . size . height, p_rect_2 . origin . y + p_rect_2 . size . height);
t_intersection . size . width = MCMax(0.0f, t_right - t_intersection . origin . x);
t_intersection . size . height = MCMax(0.0f, t_bottom - t_intersection . origin . y);
return t_intersection;
}
MCGRectangle MCGRectangleUnion(const MCGRectangle &p_rect_1, const MCGRectangle &p_rect_2)
{
if (MCGRectangleIsEmpty(p_rect_1))
return p_rect_2;
else if (MCGRectangleIsEmpty(p_rect_2))
return p_rect_1;
MCGRectangle t_union;
t_union . origin . x = MCMin(p_rect_1 . origin . x, p_rect_2 . origin . x);
t_union . origin . y = MCMin(p_rect_1 . origin . y, p_rect_2 . origin . y);
MCGFloat t_right, t_bottom;
t_right = MCMax(p_rect_1 . origin . x + p_rect_1 . size . width, p_rect_2 . origin . x + p_rect_2 . size . width);
t_bottom = MCMax(p_rect_1 . origin . y + p_rect_1 . size . height, p_rect_2 . origin . y + p_rect_2 . size . height);
t_union . size . width = t_right - t_union . origin . x;
t_union . size . height = t_bottom - t_union . origin . y;
return t_union;
}
MCGIntegerRectangle MCGIntegerRectangleIntersection(const MCGIntegerRectangle &p_rect_1, const MCGIntegerRectangle &p_rect_2)
{
int32_t t_left, t_top;
t_left = MCMax(p_rect_1.origin.x, p_rect_2.origin.x);
t_top = MCMax(p_rect_1.origin.y, p_rect_2.origin.y);
// IM-2014-10-22: [[ Bug 13746 ]] Cast to signed ints to fix unsigned arithmetic overflow
int32_t t_right, t_bottom;
t_right = MCMin(p_rect_1.origin.x + (int32_t)p_rect_1.size.width, p_rect_2.origin.x + (int32_t)p_rect_2.size.width);
t_bottom = MCMin(p_rect_1.origin.y + (int32_t)p_rect_1.size.height, p_rect_2.origin.y + (int32_t)p_rect_2.size.height);
t_right = MCMax(t_left, t_right);
t_bottom = MCMax(t_top, t_bottom);
return MCGIntegerRectangleMake(t_left, t_top, t_right - t_left, t_bottom - t_top);
}
MCGIntegerRectangle MCGRectangleGetBounds(const MCGRectangle &p_rect)
{
int32_t t_left, t_right, t_top, t_bottom;
t_left = floor(p_rect.origin.x);
t_top = floor(p_rect.origin.y);
t_right = ceil(p_rect.origin.x + p_rect.size.width);
t_bottom = ceil(p_rect.origin.y + p_rect.size.height);
int32_t t_width, t_height;
t_width = t_right - t_left;
t_height = t_bottom - t_top;
// [[ Bug 11349 ]] Out of bounds content displayed since getting integer
// bounds of an empty rect is not empty.
if (p_rect . size . width == 0.0f || p_rect . size . height == 0.0f)
{
t_width = 0;
t_height = 0;
}
return MCGIntegerRectangleMake(t_left, t_top, t_width, t_height);
}
MCGIntegerRectangle MCGIntegerRectangleGetTransformedBounds(const MCGIntegerRectangle &p_rect, const MCGAffineTransform &p_transform)
{
return MCGRectangleGetBounds(MCGRectangleApplyAffineTransform(MCGIntegerRectangleToMCGRectangle(p_rect), p_transform));
}
////////////////////////////////////////////////////////////////////////////////
void MCGAffineTransformToSkMatrix(const MCGAffineTransform &p_transform, SkMatrix &r_matrix)
{
r_matrix . setAll(MCGFloatToSkScalar(p_transform . a),
MCGFloatToSkScalar(p_transform . c),
MCGFloatToSkScalar(p_transform . tx),
MCGFloatToSkScalar(p_transform . b),
MCGFloatToSkScalar(p_transform . d),
MCGFloatToSkScalar(p_transform . ty),
SkIntToScalar(0),
SkIntToScalar(0),
SK_Scalar1);
}
void MCGAffineTransformFromSkMatrix(const SkMatrix &p_matrix, MCGAffineTransform &r_transform)
{
r_transform . a = p_matrix[0];
r_transform . c = p_matrix[1];
r_transform . tx = p_matrix[2];
r_transform . b = p_matrix[3];
r_transform . d = p_matrix[4];
r_transform . ty = p_matrix[5];
}
MCGAffineTransform MCGAffineTransformMakeIdentity(void)
{
MCGAffineTransform t_transform;
t_transform . a = 1.0f;
t_transform . b = 0.0f;
t_transform . c = 0.0f;
t_transform . d = 1.0f;
t_transform . tx = 0.0f;
t_transform . ty = 0.0f;
return t_transform;
}
MCGAffineTransform MCGAffineTransformMakeRotation(MCGFloat p_angle)
{
MCGAffineTransform t_transform;
p_angle = MCGFloatDegreesToRadians(p_angle);
t_transform . a = MCGFloatCos(p_angle);
t_transform . b = MCGFloatSin(p_angle);
t_transform . c = -1.0f * t_transform . b;
t_transform . d = t_transform . a;
t_transform . tx = 0.0f;
t_transform . ty = 0.0f;
return t_transform;
}
MCGAffineTransform MCGAffineTransformMakeTranslation(MCGFloat p_xoffset, MCGFloat p_yoffset)
{
MCGAffineTransform t_transform;
t_transform . a = 1.0f;
t_transform . b = 0.0f;
t_transform . c = 0.0f;
t_transform . d = 1.0f;
t_transform . tx = p_xoffset;
t_transform . ty = p_yoffset;
return t_transform;
}
MCGAffineTransform MCGAffineTransformMakeScale(MCGFloat p_xscale, MCGFloat p_yscale)
{
MCGAffineTransform t_transform;
t_transform . a = p_xscale;
t_transform . b = 0.0f;