Mike Phares d8013da912 ResultContentCollection
ValidVideoFormatExtensions
2024-11-03 10:33:56 -07:00

96 lines
4.4 KiB
C#

using System.Collections.ObjectModel;
using System.Drawing;
using System.Text;
using View_by_Distance.Shared.Models;
using View_by_Distance.Shared.Models.Methods;
using View_by_Distance.Shared.Models.Properties;
namespace View_by_Distance.BlurHash.Models;
public class C2_BlurHasher : IBlurHasher
{
private readonly IPropertyConfiguration _PropertyConfiguration;
private readonly Dictionary<string, ReadOnlyCollection<string>> _FileGroups;
public C2_BlurHasher(IPropertyConfiguration propertyConfiguration)
{
_FileGroups = [];
_PropertyConfiguration = propertyConfiguration;
}
public void Update(string resultsFullGroupDirectory)
{
_FileGroups.Clear();
ReadOnlyDictionary<string, ReadOnlyCollection<string>> keyValuePairs = Shared.Models.Stateless.Methods.IPath.GetKeyValuePairs(_PropertyConfiguration, resultsFullGroupDirectory, [_PropertyConfiguration.ResultContent, _PropertyConfiguration.ResultSingleton]);
foreach (KeyValuePair<string, ReadOnlyCollection<string>> keyValuePair in keyValuePairs)
_FileGroups.Add(keyValuePair.Key, keyValuePair.Value);
}
string IBlurHasher.Encode(FileHolder fileHolder)
{
string result;
#pragma warning disable CA1416
Image image = Image.FromFile(fileHolder.FullName);
(int componentsX, int componentsY) = image.Width < image.Height ? (4, 3) : (3, 4);
ReadOnlySpan<char> blurHash = System.Drawing.BlurHash.BlurHasher.Encode(image, componentsX, componentsY);
image.Dispose();
#pragma warning restore CA1416
result = blurHash.ToString();
return result;
}
string IBlurHasher.GetFile(FilePath filePath)
{
string result;
if (_FileGroups.Count == 0)
throw new Exception("Call Update first!");
(_, int directoryIndex) = Shared.Models.Stateless.Methods.IPath.GetDirectoryNameAndIndex(_PropertyConfiguration, filePath);
result = Path.Combine(_FileGroups[_PropertyConfiguration.ResultSingleton][directoryIndex], $"{filePath.Name}.csv");
return result;
}
string IBlurHasher.EncodeAndSave(FilePath filePath, FileHolder fileHolder)
{
string result;
if (_FileGroups.Count == 0)
throw new Exception("Call Update first!");
int actualByte;
string extension = ".png";
IBlurHasher blurHasher = this;
string file = blurHasher.GetFile(filePath);
#pragma warning disable CA1416
Image image = Image.FromFile(fileHolder.FullName);
int outputWidth = (int)(image.Width * .25);
int outputHeight = (int)(image.Height * .25);
(int componentsX, int componentsY) = image.Width < image.Height ? (4, 3) : (3, 4);
ReadOnlySpan<char> blurHash = System.Drawing.BlurHash.BlurHasher.Encode(image, componentsX, componentsY);
using Image actualImage = System.Drawing.BlurHash.BlurHasher.Decode(blurHash, outputWidth, outputHeight);
result = blurHash.ToString();
byte[] blurHashBytes = Encoding.UTF8.GetBytes(result);
string joined = string.Join(string.Empty, blurHashBytes.Select(l => l.ToString("000")));
string fileNameWithoutExtension = $"{componentsX}x{componentsY}-{outputWidth}x{outputHeight}-{joined}";
string contents = string.Concat(result, Environment.NewLine, fileNameWithoutExtension, Environment.NewLine, extension);
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(file, contents, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
(_, int directoryIndex) = Shared.Models.Stateless.Methods.IPath.GetDirectoryNameAndIndex(_PropertyConfiguration, filePath);
file = Path.Combine(_FileGroups[_PropertyConfiguration.ResultContent][directoryIndex], $"{fileNameWithoutExtension}{extension}");
if (!File.Exists(file))
{
try
{
using FileStream fileStream = new(file, FileMode.CreateNew);
actualImage.Save(fileStream, System.Drawing.Imaging.ImageFormat.Png);
_ = fileStream.Seek(0, SeekOrigin.Begin);
actualByte = fileStream.ReadByte();
while (actualByte > -1)
actualByte = fileStream.ReadByte();
}
catch (Exception) { }
}
image.Dispose();
#pragma warning restore CA1416
result = blurHash.ToString();
return result;
}
}