Files
.config
.vscode
BlurHash
BlurHash.Core
BlurHash.System.Drawing.Common
Compare
Copy-Distinct
Date-Group
Delete-By-Distinct
Delete-By-Relative
Distance
Drag-Drop-Explorer
Drag-Drop-Move
Drag-Drop-Search
Drag-Drop-Set-Property-Item
Duplicate-Search
Face
FaceParts
FaceRecognitionDotNet
Instance
Map
Metadata
Metadata-Query
Mirror-Length
Move-By-Id
Offset-Date-Time-Original
PhotoPrism
PrepareForOld
Property
Property-Compare
Rename
Resize
Set-Created-Date
Shared
Tests
TestsWithFaceRecognitionDotNet
ThumbHash
.vscode
Models
SpanOwner.cs
ThumbHash.Channel.cs
ThumbHash.RGBA.cs
ThumbHash.cs
ThumbHasher.cs
ThumbHash.csproj
.editorconfig
.gitattributes
.gitignore
.prettierignore
.txt
View-by-Distance-MKLink-Console.sln
package-lock.json
package.json
view-by-distance-mklink-con…/ThumbHash/Models/ThumbHasher.cs

57 lines
1.9 KiB
C#

using SkiaSharp;
using View_by_Distance.Shared.Models.Methods;
namespace View_by_Distance.ThumbHash.Models;
public class C2_ThumbHasher : IThumbHasher
{
private static SKBitmap GetBitmap(string path)
{
SKBitmap result;
using SKBitmap skBitmap = SKBitmap.Decode(path);
result = skBitmap.Copy(SKColorType.Rgba8888);
return result;
}
private static byte[] Encode(SKBitmap skBitmap) =>
ThumbHash.RgbaToThumbHash(skBitmap.Width, skBitmap.Height, skBitmap.GetPixelSpan());
private static byte[] Encode(string path)
{
byte[] results;
using SKBitmap skBitmap = GetBitmap(path);
results = Encode(skBitmap);
return results;
}
byte[] IThumbHasher.Encode(string path) =>
Encode(path);
private static MemoryStream GetMemoryStream(byte[] thumbHashBytes, int width, int height)
{
MemoryStream result = new();
(int w, int h) = (width / 2, height / 2);
using SKManagedWStream skManagedWStream = new(result);
byte[] bytes = ThumbHash.ThumbHashToRgba(thumbHashBytes, w, h);
SKImageInfo skImageInfo = new(w, h, SKColorType.Rgba8888, SKAlphaType.Unpremul);
using SKImage skImage = SKImage.FromPixelCopy(skImageInfo, bytes);
using SKData sdData = skImage.Encode(SKEncodedImageFormat.Png, 100);
sdData.SaveTo(result);
return result;
}
MemoryStream IThumbHasher.GetMemoryStream(byte[] thumbHashBytes, int width, int height) =>
GetMemoryStream(thumbHashBytes, width, height);
(byte[], MemoryStream) IThumbHasher.EncodeAndSave(string path, int width, int height)
{
byte[] results;
MemoryStream result;
using SKBitmap skBitmap = GetBitmap(path);
results = Encode(skBitmap);
result = GetMemoryStream(results, width, height);
return (results, result);
}
}