forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIImageExtensions.iOS.cs
More file actions
104 lines (95 loc) · 2.48 KB
/
Copy pathUIImageExtensions.iOS.cs
File metadata and controls
104 lines (95 loc) · 2.48 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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using CoreGraphics;
using UIKit;
using Windows.UI;
namespace Uno.UI.Extensions
{
internal static class NSUIImageExtensions
{
internal static UIImage AsMonochrome(this UIImage image, Color foreground)
{
var width = (int)image.Size.Width;
var height = (int)image.Size.Height;
var imageRect = new CGRect(0, 0, image.Size.Width, image.Size.Height);
var rawData = new byte[width * height * 4];
var outputData = new byte[width * height * 4];
var handle = GCHandle.Alloc(rawData);
try
{
using (var colorSpace = CGColorSpace.CreateDeviceRGB())
{
using (var context = new CGBitmapContext(
data: rawData,
width: (nint)imageRect.Width,
height: (nint)imageRect.Height,
bitsPerComponent: 8,
bytesPerRow: (nint)imageRect.Width * 4,
colorSpace: colorSpace,
bitmapInfo: CGImageAlphaInfo.PremultipliedLast
)
)
{
context.DrawImage(imageRect, image.CGImage);
var foregroundColor = foreground.R | (foreground.R << 8) | (foreground.R << 16) | (0xFF << 24);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
var index = x * 4 + y * height * 4;
var red = rawData[index + 0];
var green = rawData[index + 1];
var blue = rawData[index + 2];
var alpha = rawData[index + 3];
if (red + green + blue != 0)
{
outputData[index + 0] = foreground.R;
outputData[index + 1] = foreground.G;
outputData[index + 2] = foreground.B;
outputData[index + 3] = 0xFF;
}
else
{
outputData[index + 0] = 0;
outputData[index + 1] = 0;
outputData[index + 2] = 0;
outputData[index + 3] = 0;
}
}
}
}
}
}
finally
{
handle.Free();
}
using (var dataProvider = new CGDataProvider(outputData, 0, outputData.Length))
{
using (var colorSpace = CGColorSpace.CreateDeviceRGB())
{
var bitsPerComponent = 8;
var bytesPerPixel = 4;
using (var cgImage = new CGImage(
width,
height,
bitsPerComponent,
bitsPerComponent * bytesPerPixel,
bytesPerPixel * width,
colorSpace,
CGImageAlphaInfo.Last,
dataProvider,
null,
false,
CGColorRenderingIntent.Default
))
{
return UIImage.FromImage(cgImage);
}
}
}
}
}
}