-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ImageSharp project #7
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d1446e2
Add ImageSharp project
slang25 b31e16f
Use Rgb24 instead of Rgba32
slang25 2b74361
Target netstandard2.0
slang25 2eb645d
Fix the encoder
slang25 dc87b17
Go back to Rgba32
slang25 00c485c
Support encoding rgb24 & rgba32, only decode to rgb24
slang25 175e57d
Remove Linq in decode
slang25 68ef5ba
Add explicit guard for supported pixel types
slang25 6056870
Update Blurhash.ImageSharp/Decoder.cs
slang25 665e819
Inline unneeded local function
slang25 707a74e
Use WrapMemory
slang25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Blurhash.Core\Blurhash.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0007" /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Blurhash.Core; | ||
using SixLabors.ImageSharp; | ||
using SixLabors.ImageSharp.PixelFormats; | ||
|
||
namespace Blurhash.ImageSharp | ||
{ | ||
public class Decoder : CoreDecoder | ||
{ | ||
/// <summary> | ||
/// Decodes a Blurhash string into a <c>System.Drawing.Image</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 Image<Rgb24> Decode(string blurhash, int outputWidth, int outputHeight, double punch = 1.0) | ||
{ | ||
var pixelData = base.CoreDecode(blurhash, outputWidth, outputHeight, 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>Image<Rgb24></c> in 24bpp-RGB representation</returns> | ||
internal static Image<Rgb24> ConvertToBitmap(Pixel[,] pixelData) | ||
{ | ||
var width = pixelData.GetLength(0); | ||
var height = pixelData.GetLength(1); | ||
|
||
Rgb24[] data = new Rgb24[width*height]; | ||
for (int y = 0; y < height; y++) | ||
{ | ||
Span<Rgb24> rowSpan = data.AsSpan().Slice(y * width, width); | ||
for (int x = 0; x < width; x++) | ||
{ | ||
var pixel = pixelData[x, y]; | ||
ref Rgb24 dest = ref rowSpan[x]; | ||
dest.R = (byte) MathUtils.LinearTosRgb(pixel.Red); | ||
dest.G = (byte) MathUtils.LinearTosRgb(pixel.Green); | ||
dest.B = (byte) MathUtils.LinearTosRgb(pixel.Blue); | ||
} | ||
} | ||
|
||
return Image.WrapMemory<Rgb24>(Configuration.Default, new Memory<Rgb24>(data), width, height); | ||
} | ||
} | ||
} |
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,74 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Net.Mime; | ||
using System.Runtime.InteropServices; | ||
using System.Threading.Tasks; | ||
using Blurhash.Core; | ||
using SixLabors.ImageSharp; | ||
using SixLabors.ImageSharp.Advanced; | ||
using SixLabors.ImageSharp.PixelFormats; | ||
using SixLabors.ImageSharp.Processing; | ||
|
||
namespace Blurhash.ImageSharp | ||
{ | ||
public class Encoder : CoreEncoder | ||
{ | ||
/// <summary> | ||
/// Encodes a picture into a Blurhash string | ||
/// </summary> | ||
/// <param name="image">The picture 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(Image<Rgb24> image, int componentsX, int componentsY) | ||
{ | ||
return CoreEncode(ConvertBitmap(image), componentsX, componentsY); | ||
} | ||
|
||
/// <summary> | ||
/// Encodes a picture into a Blurhash string | ||
/// </summary> | ||
/// <param name="image">The picture 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(Image<Rgba32> image, int componentsX, int componentsY) | ||
{ | ||
return CoreEncode(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> | ||
internal static Pixel[,] ConvertBitmap<T>(Image<T> sourceBitmap) where T : struct, IPixel<T> | ||
{ | ||
if (typeof(T) != typeof(Rgba32) && typeof(T) != typeof(Rgb24)) | ||
throw new ArgumentOutOfRangeException(nameof(sourceBitmap), "Only Rgba32 and Rgb24 are supported"); | ||
|
||
var width = sourceBitmap.Width; | ||
var height = sourceBitmap.Height; | ||
var bytesPerPixel = sourceBitmap.PixelType.BitsPerPixel / 8; | ||
var stride = width * 3; | ||
|
||
var result = new Pixel[width, height]; | ||
|
||
for (int y = 0; y < height; y++) | ||
{ | ||
var rgbValues = MemoryMarshal.AsBytes(sourceBitmap.GetPixelRowSpan(y)); | ||
slang25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var index = stride; | ||
|
||
for (var x = 0; x < width; x++) | ||
{ | ||
result[x, y].Red = MathUtils.SRgbToLinear(rgbValues[index]); | ||
slang25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result[x, y].Green = MathUtils.SRgbToLinear(rgbValues[index + 1]); | ||
result[x, y].Blue = MathUtils.SRgbToLinear(rgbValues[index + 2]); | ||
index += bytesPerPixel; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering the implementation, you should consume
Image<Rgb24>
or use proper conversion withPixelOperations<T>.ToRgb24(...)
.