diff --git a/Blurhash.Microsoft.Extensions.SkiaSharp/Blurhash.Microsoft.Extensions.SkiaSharp.csproj b/Blurhash.Microsoft.Extensions.SkiaSharp/Blurhash.Microsoft.Extensions.SkiaSharp.csproj
new file mode 100644
index 0000000..917e796
--- /dev/null
+++ b/Blurhash.Microsoft.Extensions.SkiaSharp/Blurhash.Microsoft.Extensions.SkiaSharp.csproj
@@ -0,0 +1,36 @@
+
+
+
+ netstandard2.0
+ Blurhash.Microsoft.Extensions.SkiaSharp
+ Markus Palcer, Marcin Badurowicz
+ $(NoWarn);CS1998
+ Bridge library to use Blurhash with SkiaSharp and ServiceExtensions
+ 2.0.0
+ true
+ blurhash;image encoding;skiasharp
+ https://github.com/ktos/Blurhash.SkiaSharp
+ MIT
+ Copyright © 2020-2022 Marcin Badurowicz, Markus Palcer
+
+
+
+ true
+
+
+
+ true
+ E:\Marcin\!Projekty\Blurhash.SkiaSharp\src\Blurhash.SkiaSharp.xml
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Blurhash.Microsoft.Extensions.SkiaSharp/BlurhasherImpl.cs b/Blurhash.Microsoft.Extensions.SkiaSharp/BlurhasherImpl.cs
new file mode 100644
index 0000000..b1242ae
--- /dev/null
+++ b/Blurhash.Microsoft.Extensions.SkiaSharp/BlurhasherImpl.cs
@@ -0,0 +1,45 @@
+using Blurhash.Microsoft.Extensions.Core;
+using Microsoft.Extensions.DependencyInjection;
+using SkiaSharp;
+
+namespace Blurhash.Microsoft.Extensions.SkiaSharp
+{
+ ///
+ public class BlurhasherImpl : Blurhasher, IBlurhasher
+ {
+ public BlurhasherImpl(IImageConverter imageConverter) : base(imageConverter)
+ {
+ }
+ }
+
+ ///
+ /// The blurhash algorithm abstraction for
+ ///
+ public interface IBlurhasher : IBlurhasher
+ {
+ }
+
+ public static class Extensions
+ {
+ ///
+ /// Adds the blurhash core and the converter for to the given
+ /// Also enables you to request
+ ///
+ public static IServiceCollection AddBlurhash(this IServiceCollection serviceCollection)
+ {
+ return serviceCollection.AddBlurhashCore()
+ .AddSingleton, ImageConverter>()
+ .AddSingleton();
+ }
+ }
+
+ ///
+ public class ImageConverter : IImageConverter
+ {
+ ///
+ public Pixel[,] ImageToPixels(SKBitmap image) => Blurhash.SkiaSharp.Blurhasher.ConvertBitmap(image);
+
+ ///
+ public SKBitmap PixelsToImage(Pixel[,] pixels) => Blurhash.SkiaSharp.Blurhasher.ConvertToBitmap(pixels);
+ }
+}
\ No newline at end of file
diff --git a/src/Blurhash.SkiaSharp.csproj b/src/Blurhash.SkiaSharp.csproj
index fe985c1..ae0ba02 100644
--- a/src/Blurhash.SkiaSharp.csproj
+++ b/src/Blurhash.SkiaSharp.csproj
@@ -6,12 +6,12 @@
Markus Palcer, Marcin Badurowicz
$(NoWarn);CS1998
Bridge library to use Blurhash with SkiaSharp
- 1.0.0
+ 2.0.0
true
blurhash;image encoding;skiasharp
https://github.com/ktos/Blurhash.SkiaSharp
MIT
- Copyright © 2020 Marcin Badurowicz, Markus Palcer
+ Copyright © 2020-2022 Marcin Badurowicz, Markus Palcer
@@ -24,8 +24,8 @@
-
-
+
+
diff --git a/src/Encoder.cs b/src/Blurhasher.cs
similarity index 51%
rename from src/Encoder.cs
rename to src/Blurhasher.cs
index 60f1cf9..f045e1e 100644
--- a/src/Encoder.cs
+++ b/src/Blurhasher.cs
@@ -1,15 +1,62 @@
-using Blurhash.Core;
-using SkiaSharp;
+using SkiaSharp;
using System;
+using System.Linq;
namespace Blurhash.SkiaSharp
{
///
- /// The Blurhash encoder for SkiaSharp
- /// Creates a very compact hash from an image to use as a blurred image placeholder
+ /// The Blurhash encoder/decoder for SkiaSharp
///
- public class Encoder : CoreEncoder
+ public static class Blurhasher
{
+ ///
+ /// Decodes a Blurhash string into a SKBitmap
+ ///
+ /// The blurhash string to decode
+ /// The desired width of the output in pixels
+ /// The desired height of the output in pixels
+ /// A value that affects the contrast of the decoded image. 1 means normal, smaller values will make the effect more subtle, and larger values will make it stronger.
+ /// The decoded preview
+ public static SKBitmap Decode(string blurhash, int outputWidth, int outputHeight, double punch = 1.0)
+ {
+ var pixelData = new Pixel[outputWidth, outputHeight];
+ Core.Decode(blurhash, pixelData, punch);
+ return ConvertToBitmap(pixelData);
+ }
+
+ ///
+ /// Converts the library-independent representation of pixels into a bitmap
+ ///
+ /// The library-independent representation of the image
+ /// A SKBitmap in Bgra8888 representation
+ public static SKBitmap ConvertToBitmap(Blurhash.Pixel[,] pixelData)
+ {
+ var width = pixelData.GetLength(0);
+ var height = pixelData.GetLength(1);
+
+ var data = Enumerable.Range(0, height)
+ .SelectMany(y => Enumerable.Range(0, width).Select(x => (x, y)))
+ .Select(tuple => pixelData[tuple.x, tuple.y])
+ .SelectMany(pixel => new byte[]
+ {
+ (byte) MathUtils.LinearTosRgb(pixel.Blue), (byte) MathUtils.LinearTosRgb(pixel.Green),
+ (byte) MathUtils.LinearTosRgb(pixel.Red), 255
+ })
+ .ToArray();
+
+ SKBitmap bitmap = new SKBitmap(width, height, SKColorType.Bgra8888, SKAlphaType.Opaque);
+
+ unsafe
+ {
+ fixed (byte* ptr = data)
+ {
+ bitmap.SetPixels((IntPtr)ptr);
+ }
+ }
+
+ return bitmap;
+ }
+
///
/// Encodes a SKBitmap into a Blurhash string
///
@@ -17,16 +64,16 @@ public class Encoder : CoreEncoder
/// The number of components used on the X-Axis for the DCT
/// The number of components used on the Y-Axis for the DCT
/// The resulting Blurhash string
- public string Encode(SKBitmap image, int componentsX, int componentsY)
+ public static string Encode(SKBitmap image, int componentsX, int componentsY)
{
- return CoreEncode(ConvertBitmap(image), componentsX, componentsY);
+ return Core.Encode(ConvertBitmap(image), componentsX, componentsY);
}
///
/// Converts the given bitmap to the library-independent representation used within the Blurhash-core
///
/// The bitmap to encode
- public unsafe static Pixel[,] ConvertBitmap(SKBitmap sourceBitmap)
+ public static unsafe Pixel[,] ConvertBitmap(SKBitmap sourceBitmap)
{
SKPixmap pixmap = sourceBitmap.PeekPixels();
byte* bmpPtr = (byte*)pixmap.GetPixels().ToPointer();
@@ -42,7 +89,7 @@ public string Encode(SKBitmap image, int componentsX, int componentsY)
byte red, green, blue, alpha;
if (sourceBitmap.ColorType == SKColorType.Rgba8888)
{
- // SKColorType.Rgba8888 is used by iOS and Android
+ // SKColorType.Rgba8888 is used by iOS and Android
red = *bmpPtr++;
green = *bmpPtr++;
blue = *bmpPtr++;
diff --git a/src/Decoder.cs b/src/Decoder.cs
deleted file mode 100644
index aab08a3..0000000
--- a/src/Decoder.cs
+++ /dev/null
@@ -1,61 +0,0 @@
-using Blurhash.Core;
-using SkiaSharp;
-using System;
-using System.Linq;
-
-namespace Blurhash.SkiaSharp
-{
- ///
- /// The Blurhash decoder for SkiaSharp
- /// Creates a bitmap placeholder from a Blurhash
- ///
- public class Decoder : CoreDecoder
- {
- ///
- /// Decodes a Blurhash string into a SKBitmap
- ///
- /// The blurhash string to decode
- /// The desired width of the output in pixels
- /// The desired height of the output in pixels
- /// A value that affects the contrast of the decoded image. 1 means normal, smaller values will make the effect more subtle, and larger values will make it stronger.
- /// The decoded preview
- public SKBitmap Decode(string blurhash, int outputWidth, int outputHeight, double punch = 1.0)
- {
- var pixelData = CoreDecode(blurhash, outputWidth, outputHeight, punch);
- return ToSKBitmap(pixelData);
- }
-
- ///
- /// Converts the library-independent representation of pixels into a bitmap
- ///
- /// The library-independent representation of the image
- /// A SKBitmap in Bgra8888 representation
- private static SKBitmap ToSKBitmap(Blurhash.Core.Pixel[,] pixelData)
- {
- var width = pixelData.GetLength(0);
- var height = pixelData.GetLength(1);
-
- var data = Enumerable.Range(0, height)
- .SelectMany(y => Enumerable.Range(0, width).Select(x => (x, y)))
- .Select(tuple => pixelData[tuple.x, tuple.y])
- .SelectMany(pixel => new byte[]
- {
- (byte) MathUtils.LinearTosRgb(pixel.Blue), (byte) MathUtils.LinearTosRgb(pixel.Green),
- (byte) MathUtils.LinearTosRgb(pixel.Red), 255
- })
- .ToArray();
-
- SKBitmap bitmap = new SKBitmap(width, height, SKColorType.Bgra8888, SKAlphaType.Opaque);
-
- unsafe
- {
- fixed (byte* ptr = data)
- {
- bitmap.SetPixels((IntPtr)ptr);
- }
- }
-
- return bitmap;
- }
- }
-}
\ No newline at end of file