-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BlurHashSharp.ImageSharp project and throw on invalid component c…
…ount
- Loading branch information
Showing
11 changed files
with
325 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using SixLabors.ImageSharp; | ||
using SixLabors.ImageSharp.Formats; | ||
using SixLabors.ImageSharp.PixelFormats; | ||
|
||
namespace BlurHashSharp.ImageSharp; | ||
|
||
/// <summary> | ||
/// The BlurHash encoder for use with the SkiaSharp image library. | ||
/// </summary> | ||
public static class BlurHashEncoder | ||
{ | ||
private static readonly Lazy<Configuration> Lazy = new(() => | ||
{ | ||
var config = Configuration.Default.Clone(); | ||
config.PreferContiguousImageBuffers = true; | ||
return config; | ||
}); | ||
|
||
private static Configuration Configuration => Lazy.Value; | ||
|
||
private static DecoderOptions DecoderOptions => | ||
new DecoderOptions() | ||
{ | ||
Configuration = Configuration | ||
}; | ||
|
||
/// <summary> | ||
/// Encodes the BlurHash representation of the image. | ||
/// </summary> | ||
/// <param name="xComponent">The number x components.</param> | ||
/// <param name="yComponent">The number y components.</param> | ||
/// <param name="filename">The path to an encoded image on the file system.</param> | ||
/// <returns>BlurHash representation of the image.</returns> | ||
public static string Encode(int xComponent, int yComponent, string filename) | ||
{ | ||
using var image = Image.Load<Rgb24>(DecoderOptions, filename); | ||
return EncodeInternal(xComponent, yComponent, image); | ||
} | ||
|
||
/// <summary> | ||
/// Encodes the BlurHash representation of the image. | ||
/// </summary> | ||
/// <param name="xComponent">The number x components.</param> | ||
/// <param name="yComponent">The number y components.</param> | ||
/// <param name="stream">The IO stream of an encoded image.</param> | ||
/// <returns>BlurHash representation of the image.</returns> | ||
public static string Encode(int xComponent, int yComponent, Stream stream) | ||
{ | ||
using var image = Image.Load<Rgb24>(DecoderOptions, stream); | ||
return EncodeInternal(xComponent, yComponent, image); | ||
} | ||
|
||
private static string EncodeInternal(int xComponent, int yComponent, Image<Rgb24> image) | ||
{ | ||
var bytesPerRow = image.Width * (image.PixelType.BitsPerPixel / 8); | ||
Span<byte> buffer; | ||
if (image.DangerousTryGetSinglePixelMemory(out Memory<Rgb24> memory)) | ||
{ | ||
buffer = MemoryMarshal.AsBytes(memory.Span); | ||
} | ||
else | ||
{ | ||
buffer = new byte[image.Height * bytesPerRow]; | ||
image.CopyPixelDataTo(buffer); | ||
} | ||
|
||
return CoreBlurHashEncoder.Encode(xComponent, yComponent, image.Width, image.Height, buffer, bytesPerRow, PixelFormat.RGB888); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/BlurHashSharp.ImageSharp/BlurHashSharp.ImageSharp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0</TargetFrameworks> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<Nullable>enable</Nullable> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<Authors>Bond_009</Authors> | ||
<Version>1.3.3</Version> | ||
<PackageTags>BlurHash;image;bitmap;imagesharp</PackageTags> | ||
<Description>BlurHash encoder for use with the ImageSharp image library.</Description> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<RepositoryUrl>https://github.com/Bond-009/BlurHashSharp</RepositoryUrl> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="../../README.md" Pack="true" PackagePath="/"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../BlurHashSharp/BlurHashSharp.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.