Back to using BlurHasher but not linked yet
This commit is contained in:
@ -12,8 +12,9 @@ public static class Core
|
||||
/// <param name="componentsY">The number of components used on the Y-Axis for the DCT</param>
|
||||
/// <param name="progressCallback">An optional progress handler to receive progress updates</param>
|
||||
/// <returns>The resulting BlurHash string</returns>
|
||||
public static string Encode(Pixel[,] pixels, int componentsX, int componentsY, IProgress<int>? progressCallback = null)
|
||||
public static ReadOnlySpan<char> Encode(Pixel[,] pixels, int componentsX, int componentsY, IProgress<int>? progressCallback = null)
|
||||
{
|
||||
Span<char> results = new char[4 + 2 * componentsX * componentsY];
|
||||
if (componentsX < 1)
|
||||
throw new ArgumentException("componentsX needs to be at least 1");
|
||||
if (componentsX > 9)
|
||||
@ -119,13 +120,14 @@ public static class Core
|
||||
EncodeAc(factors[factorIndex].Red, factors[factorIndex].Green, factors[factorIndex].Blue, maximumValue).EncodeBase83(resultBuffer.Slice(6 + (factorIndex - 1) * 2, 2));
|
||||
}
|
||||
|
||||
return resultBuffer.ToString();
|
||||
resultBuffer.CopyTo(results);
|
||||
return results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a BlurHash string into a 2-dimensional array of pixels
|
||||
/// </summary>
|
||||
/// <param name="blurhash">The blurhash string to decode</param>
|
||||
/// <param name="blurHash">The blurHash string to decode</param>
|
||||
/// <param name="pixels">
|
||||
/// A two-dimensional array that will be filled with the pixel data.<br />
|
||||
/// First dimension is the width, second dimension is the height
|
||||
@ -133,30 +135,28 @@ public static class Core
|
||||
/// <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>
|
||||
/// <param name="progressCallback">An optional progress handler to receive progress updates</param>
|
||||
/// <returns>A 2-dimensional array of <see cref="Pixel"/>s </returns>
|
||||
public static void Decode(string blurhash, Pixel[,] pixels, double punch = 1.0, IProgress<int>? progressCallback = null)
|
||||
public static void Decode(ReadOnlySpan<char> blurHash, Pixel[,] pixels, double punch = 1.0, IProgress<int>? progressCallback = null)
|
||||
{
|
||||
if (blurhash.Length < 6)
|
||||
if (blurHash.Length < 6)
|
||||
{
|
||||
throw new ArgumentException("BlurHash value needs to be at least 6 characters", nameof(blurhash));
|
||||
throw new ArgumentException("BlurHash value needs to be at least 6 characters", nameof(blurHash));
|
||||
}
|
||||
|
||||
ReadOnlySpan<char> blurhashSpan = blurhash.AsSpan();
|
||||
|
||||
int outputWidth = pixels.GetLength(0);
|
||||
int outputHeight = pixels.GetLength(1);
|
||||
|
||||
int sizeFlag = blurhashSpan[..1].DecodeBase83();
|
||||
int sizeFlag = blurHash[..1].DecodeBase83();
|
||||
|
||||
int componentsY = sizeFlag / 9 + 1;
|
||||
int componentsX = sizeFlag % 9 + 1;
|
||||
int componentCount = componentsX * componentsY;
|
||||
|
||||
if (blurhash.Length != 4 + 2 * componentsX * componentsY)
|
||||
if (blurHash.Length != 4 + 2 * componentsX * componentsY)
|
||||
{
|
||||
throw new ArgumentException("BlurHash value is missing data", nameof(blurhash));
|
||||
throw new ArgumentException("BlurHash value is missing data", nameof(blurHash));
|
||||
}
|
||||
|
||||
double quantizedMaximumValue = blurhashSpan.Slice(1, 1).DecodeBase83();
|
||||
double quantizedMaximumValue = blurHash.Slice(1, 1).DecodeBase83();
|
||||
double maximumValue = (quantizedMaximumValue + 1.0) / 166.0;
|
||||
|
||||
Pixel[,] coefficients = new Pixel[componentsX, componentsY];
|
||||
@ -167,12 +167,12 @@ public static class Core
|
||||
{
|
||||
if (xComponent == 0 && yComponent == 0)
|
||||
{
|
||||
int value = blurhashSpan.Slice(2, 4).DecodeBase83();
|
||||
int value = blurHash.Slice(2, 4).DecodeBase83();
|
||||
coefficients[xComponent, yComponent] = DecodeDc(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
int value = blurhashSpan.Slice(4 + componentIndex * 2, 2).DecodeBase83();
|
||||
int value = blurHash.Slice(4 + componentIndex * 2, 2).DecodeBase83();
|
||||
coefficients[xComponent, yComponent] = DecodeAc(value, maximumValue * punch);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user