Skip to content

Commit

Permalink
feat(NSUImageExtensions): added monochrome image recoloring for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
LHuss committed Aug 20, 2020
1 parent cd16da2 commit 3d112d6
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 76 deletions.
77 changes: 3 additions & 74 deletions src/Uno.UI/Extensions/UIImageExtensions.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,11 @@

namespace Uno.UI.Extensions
{
internal static class NSUIImageExtensions
internal static partial class NSUIImageExtensions
{
internal static UIImage AsMonochrome(this UIImage image, Color foreground)
internal static UIImage FromCGImage(CGImage cgImage)
{
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);

for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
var index = x * 4 + y * height * 4;

var sourceAlpha = rawData[index + 3];

outputData[index + 0] = foreground.R;
outputData[index + 1] = foreground.G;
outputData[index + 2] = foreground.B;
outputData[index + 3] = sourceAlpha;
}
}
}
}
}
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);
}
}
}
return UIImage.FromImage(cgImage);
}
}
}
93 changes: 93 additions & 0 deletions src/Uno.UI/Extensions/UIImageExtensions.iOSmacOS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Runtime.InteropServices;
using CoreGraphics;
using Windows.UI;

#if __IOS__
using _UIImage = UIKit.UIImage;
#elif __MACOS__
using _UIImage = AppKit.NSImage;
using AppKit;
#endif

namespace Uno.UI.Extensions
{
internal static partial 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);

for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
var index = x * 4 + y * height * 4;

var sourceAlpha = rawData[index + 3];

outputData[index + 0] = foreground.R;
outputData[index + 1] = foreground.G;
outputData[index + 2] = foreground.B;
outputData[index + 3] = sourceAlpha;
}
}
}
}
}
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 FromCGImage(cgImage);
}
}
}
}
}
}
18 changes: 18 additions & 0 deletions src/Uno.UI/Extensions/UIImageExtensions.macOS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using CoreGraphics;
using Windows.UI;
using AppKit;

namespace Uno.UI.Extensions
{
internal static partial class NSUIImageExtensions
{
internal static NSImage FromCGImage(CGImage cgImage)
{
return new NSImage(cgImage, new CGSize(cgImage.Width, cgImage.Height));
}
}
}
2 changes: 0 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/Image/Image.iOSmacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,10 @@ private void SetImage(_UIImage image)
)
)
{
#if __IOS__ // MacOS TODO
if (MonochromeColor != null)
{
image = image.AsMonochrome(MonochromeColor.Value);
}
#endif

TryCreateNative();

Expand Down

0 comments on commit 3d112d6

Please sign in to comment.