Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ktos/Blurhash.SkiaSharp
Browse files Browse the repository at this point in the history
  • Loading branch information
ktos committed May 24, 2022
2 parents a6190e1 + d6fd321 commit 4731fe7
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Blurhash.Microsoft.Extensions.SkiaSharp</PackageId>
<Authors>Markus Palcer, Marcin Badurowicz</Authors>
<NoWarn>$(NoWarn);CS1998</NoWarn>
<Description>Bridge library to use Blurhash with SkiaSharp and ServiceExtensions</Description>
<PackageVersion>2.0.0</PackageVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>blurhash;image encoding;skiasharp</PackageTags>
<PackageProjectUrl>https://github.com/ktos/Blurhash.SkiaSharp</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Copyright>Copyright © 2020-2022 Marcin Badurowicz, Markus Palcer</Copyright>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DocumentationFile>E:\Marcin\!Projekty\Blurhash.SkiaSharp\src\Blurhash.SkiaSharp.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blurhash.Core" Version="2.0.0" />
<PackageReference Include="SkiaSharp" Version="2.88.0" />
<PackageReference Include="Blurhash.Microsoft.Extensions.Core" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Blurhash.SkiaSharp.csproj" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions Blurhash.Microsoft.Extensions.SkiaSharp/BlurhasherImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Blurhash.Microsoft.Extensions.Core;
using Microsoft.Extensions.DependencyInjection;
using SkiaSharp;

namespace Blurhash.Microsoft.Extensions.SkiaSharp
{
/// <inheritdoc cref="Blurhash.Microsoft.Extensions.SkiaSharp.IBlurhasher" />
public class BlurhasherImpl : Blurhasher<SKBitmap>, IBlurhasher
{
public BlurhasherImpl(IImageConverter<SKBitmap> imageConverter) : base(imageConverter)
{
}
}

/// <summary>
/// The blurhash algorithm abstraction for <see cref="SKBitmap"/>
/// </summary>
public interface IBlurhasher : IBlurhasher<SKBitmap>
{
}

public static class Extensions
{
/// <summary>
/// Adds the blurhash core and the converter for <see cref="SKBitmap"/> to the given <see cref="IServiceCollection"/><br />
/// Also enables you to request <see cref="IBlurhasher"/>
/// </summary>
public static IServiceCollection AddBlurhash(this IServiceCollection serviceCollection)
{
return serviceCollection.AddBlurhashCore()
.AddSingleton<IImageConverter<SKBitmap>, ImageConverter>()
.AddSingleton<IBlurhasher, BlurhasherImpl>();
}
}

/// <inheritdoc />
public class ImageConverter : IImageConverter<SKBitmap>
{
/// <inheritdoc />
public Pixel[,] ImageToPixels(SKBitmap image) => Blurhash.SkiaSharp.Blurhasher.ConvertBitmap(image);

/// <inheritdoc />
public SKBitmap PixelsToImage(Pixel[,] pixels) => Blurhash.SkiaSharp.Blurhasher.ConvertToBitmap(pixels);
}
}
8 changes: 4 additions & 4 deletions src/Blurhash.SkiaSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<Authors>Markus Palcer, Marcin Badurowicz</Authors>
<NoWarn>$(NoWarn);CS1998</NoWarn>
<Description>Bridge library to use Blurhash with SkiaSharp</Description>
<PackageVersion>1.0.0</PackageVersion>
<PackageVersion>2.0.0</PackageVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>blurhash;image encoding;skiasharp</PackageTags>
<PackageProjectUrl>https://github.com/ktos/Blurhash.SkiaSharp</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Copyright>Copyright © 2020 Marcin Badurowicz, Markus Palcer</Copyright>
<Copyright>Copyright © 2020-2022 Marcin Badurowicz, Markus Palcer</Copyright>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -24,8 +24,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blurhash.Core" Version="1.2.0" />
<PackageReference Include="SkiaSharp" Version="2.80.3" />
<PackageReference Include="Blurhash.Core" Version="2.0.0" />
<PackageReference Include="SkiaSharp" Version="2.88.0" />
</ItemGroup>

</Project>
65 changes: 56 additions & 9 deletions src/Encoder.cs → src/Blurhasher.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,79 @@
using Blurhash.Core;
using SkiaSharp;
using SkiaSharp;
using System;
using System.Linq;

namespace Blurhash.SkiaSharp
{
/// <summary>
/// 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
/// </summary>
public class Encoder : CoreEncoder
public static class Blurhasher
{
/// <summary>
/// Decodes a Blurhash string into a <c>SKBitmap</c>
/// </summary>
/// <param name="blurhash">The blurhash string to decode</param>
/// <param name="outputWidth">The desired width of the output in pixels</param>
/// <param name="outputHeight">The desired height of the output in pixels</param>
/// <param name="punch">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.</param>
/// <returns>The decoded preview</returns>
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);
}

/// <summary>
/// Converts the library-independent representation of pixels into a bitmap
/// </summary>
/// <param name="pixelData">The library-independent representation of the image</param>
/// <returns>A <c>SKBitmap</c> in Bgra8888 representation</returns>
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;
}

/// <summary>
/// Encodes a <c>SKBitmap</c> into a Blurhash string
/// </summary>
/// <param name="image">The bitmap to encode</param>
/// <param name="componentsX">The number of components used on the X-Axis for the DCT</param>
/// <param name="componentsY">The number of components used on the Y-Axis for the DCT</param>
/// <returns>The resulting Blurhash string</returns>
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);
}

/// <summary>
/// Converts the given bitmap to the library-independent representation used within the Blurhash-core
/// </summary>
/// <param name="sourceBitmap">The bitmap to encode</param>
public unsafe static Pixel[,] ConvertBitmap(SKBitmap sourceBitmap)
public static unsafe Pixel[,] ConvertBitmap(SKBitmap sourceBitmap)
{
SKPixmap pixmap = sourceBitmap.PeekPixels();
byte* bmpPtr = (byte*)pixmap.GetPixels().ToPointer();
Expand All @@ -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++;
Expand Down
61 changes: 0 additions & 61 deletions src/Decoder.cs

This file was deleted.

0 comments on commit 4731fe7

Please sign in to comment.