Files
.vscode
Compare
Date-Group
Distance
Face
FaceParts
FaceRecognitionDotNet
Instance
Map
Metadata
Not-Copy-Copy
PrepareForOld
Property
Property-Compare
Resize
Shared
.vscode
Models
Methods
Properties
Stateless
Methods
%ClassName%.cs .ai
Age.cs
Container.cs
DistanceHolder.cs
Face.cs
FaceDistance.cs
FaceDistanceContainer.cs
FaceFileSystem.cs
FacePoint.cs
FileHolder.cs
FileSystem.cs
I%ClassName%.cs .ai
IAge.cs
IBackgroundPage.cs
IContainer.cs
IDirectoryFileSystem.cs
IDistanceHolder.cs
IFace.cs
IFaceDistance.cs
IFaceDistanceContainer.cs
IFaceFileSystem.cs
IFacePoint.cs
IFileHolder.cs
IFileSystem.cs
IIndex.cs
IItem.cs
ILocation.cs
IMapping.cs
IMetadataFile.cs
IMetadataFileCollection.cs
IMetadataFileId.cs
IMethodName.cs
IPath.cs
IPerson.cs
IPersonAddress.cs
IPersonAddressCity.cs
IPersonAddressState.cs
IPersonAddressStreet.cs
IPersonAddressZipCode.cs
IPersonBirthday.cs
IPersonComment.cs
IPersonContainer.cs
IPersonEmail.cs
IPersonId.cs
IPersonName.cs
IPersonNameAlias.cs
IPersonNameFirst.cs
IPersonNameLast.cs
IPersonNameMiddle.cs
IPersonNumber.cs
IPersonURL.cs
IProperty.cs
IRelativePaths.cs
ISorting.cs
ISortingContainer.cs
IStorage.cs
IWorkingDirectory.cs
ImageHelper.cs
Index.cs
Item.cs
Location.cs
Mapping.cs
MetadataFile.cs
MetadataFileCollection.cs
MetadataFileId.cs
Person.cs
PersonAddress.cs
PersonAddressCity.cs
PersonAddressState.cs
PersonAddressStreet.cs
PersonAddressZipCode.cs
PersonBirthday.cs
PersonComment.cs
PersonContainer.cs
PersonEmail.cs
PersonId.cs
PersonName.cs
PersonNameAlias.cs
PersonNameFirst.cs
PersonNameLast.cs
PersonNameMiddle.cs
PersonNumber.cs
PersonURL.cs
Property.cs
RelativePaths.cs
Sorting.cs
SortingContainer.cs
Storage.cs
WorkingDirectory.cs
XPath.cs
FacePart.cs
IExif.cs
ILocation.cs
ImageFormat.cs
Mode.cs
Model.cs
PredictorModel.cs
%ClassName%.cs .ai
Console.cs
Container.cs
DirectoryFileSystem.cs
DistanceHolder.cs
Face.cs
FaceDistance.cs
FaceDistanceContainer.cs
FaceEncoding.cs
FaceFileSystem.cs
FacePoint.cs
FileHolder.cs
FileSystem.cs
Item.cs
Location.cs
Mapping.cs
MetadataFile.cs
MetadataFileCollection.cs
MetadataFileId.cs
Navigate.cs
OutputResolution.cs
Person.cs
PersonAddress.cs
PersonAddressCity.cs
PersonAddressState.cs
PersonAddressStreet.cs
PersonAddressZipCode.cs
PersonBirthday.cs
PersonComment.cs
PersonContainer.cs
PersonEmail.cs
PersonId.cs
PersonImport.cs
PersonName.cs
PersonNameAlias.cs
PersonNameFirst.cs
PersonNameLast.cs
PersonNameMiddle.cs
PersonNumber.cs
PersonURL.cs
Property.cs
RelativePaths.cs
SaveContainer.cs
Sorting.cs
SortingContainer.cs
Storage.cs
XPath.cs
Phares
Sample-Data
View-by-Distance.Shared.csproj
Tests
TestsWithFaceRecognitionDotNet
.editorconfig
.gitattributes
.gitignore
.txt
View-by-Distance-MKLink-Console.sln
package.json
view-by-distance-mklink-con…/Shared/Models/Stateless/Methods/ImageHelper.cs
2022-08-22 09:10:19 -07:00

130 lines
4.7 KiB
C#

using System.Drawing;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class ImageHelper
{
private static bool StartsWith(byte[] thisBytes, byte[] thatBytes)
{
for (int i = 0; i < thatBytes.Length; i += 1)
{
if (thisBytes[i] != thatBytes[i])
return false;
}
return true;
}
private static short ReadLittleEndianInt16(BinaryReader binaryReader)
{
byte[] bytes = new byte[sizeof(short)];
for (int i = 0; i < sizeof(short); i += 1)
bytes[sizeof(short) - 1 - i] = binaryReader.ReadByte();
return BitConverter.ToInt16(bytes, 0);
}
private static int ReadLittleEndianInt32(BinaryReader binaryReader)
{
byte[] bytes = new byte[sizeof(int)];
for (int i = 0; i < sizeof(int); i += 1)
bytes[sizeof(int) - 1 - i] = binaryReader.ReadByte();
return BitConverter.ToInt32(bytes, 0);
}
private static Size DecodeBitmap(BinaryReader binaryReader)
{
_ = binaryReader.ReadBytes(16);
int width = binaryReader.ReadInt32();
int height = binaryReader.ReadInt32();
return new Size(width, height);
}
private static Size DecodeGif(BinaryReader binaryReader)
{
int width = binaryReader.ReadInt16();
int height = binaryReader.ReadInt16();
return new Size(width, height);
}
private static Size DecodePng(BinaryReader binaryReader)
{
_ = binaryReader.ReadBytes(8);
int width = ReadLittleEndianInt32(binaryReader);
int height = ReadLittleEndianInt32(binaryReader);
return new Size(width, height);
}
private static Size DecodeJfif(BinaryReader binaryReader)
{
while (binaryReader.ReadByte() == 0xff)
{
byte marker = binaryReader.ReadByte();
short chunkLength = ReadLittleEndianInt16(binaryReader);
if (marker == 0xc0)
{
_ = binaryReader.ReadByte();
int height = ReadLittleEndianInt16(binaryReader);
int width = ReadLittleEndianInt16(binaryReader);
return new Size(width, height);
}
_ = binaryReader.ReadBytes(chunkLength - 2);
}
throw new ArgumentException("Could not recognize image format.");
}
/// <summary>
/// Gets the dimensions of an image.
/// </summary>
/// <param name="path">The path of the image to get the dimensions of.</param>
/// <returns>The dimensions of the specified image.</returns>
/// <exception cref="ArgumentException">The image was of an unrecognized format.</exception>
internal static Size GetDimensions(BinaryReader binaryReader, int? faceRight, int? faceBottom)
{
Size? result = null;
Dictionary<byte[], Func<BinaryReader, Size>> _ImageFormatDecoders = new()
{
{ new byte[] { 0x42, 0x4D }, DecodeBitmap },
{ new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif },
{ new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif },
{ new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng },
{ new byte[] { 0xff, 0xd8 }, DecodeJfif },
};
int maxMagicBytesLength = _ImageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;
byte[] magicBytes = new byte[maxMagicBytesLength];
for (int i = 0; i < maxMagicBytesLength; i += 1)
{
magicBytes[i] = binaryReader.ReadByte();
foreach (KeyValuePair<byte[], Func<BinaryReader, Size>> kvPair in _ImageFormatDecoders)
{
if (!StartsWith(magicBytes, kvPair.Key))
continue;
result = kvPair.Value(binaryReader);
break;
}
if (result is not null)
break;
}
if (result is null)
{
if (faceRight is null || faceBottom is null)
throw new Exception("face is null!");
result = new(faceRight.Value, faceBottom.Value);
}
return result.Value;
}
/// <summary>
/// Gets the dimensions of an image.
/// </summary>
/// <param name="path">The path of the image to get the dimensions of.</param>
/// <returns>The dimensions of the specified image.</returns>
/// <exception cref="ArgumentException">The image was of an unrecognized format.</exception>
internal static Size GetDimensions(string path, int? faceRight, int? faceBottom)
{
Size result;
using BinaryReader binaryReader = new(File.OpenRead(path));
result = GetDimensions(binaryReader, faceRight, faceBottom);
return result;
}
}