Mostly Sorting

Video Merge as 4, 5, and 6
This commit is contained in:
2025-03-22 16:50:47 -07:00
parent d5fa108f81
commit 130e3b6528
24 changed files with 6223 additions and 6119 deletions

View File

@ -66,6 +66,98 @@ public class C_Resize
_ConstructorInfo = constructorInfo;
}
public static byte[] GetBitmapData(Bitmap bitmap)
{
byte[] results;
#pragma warning disable CA1416
Rectangle rectangle = new(0, 0, bitmap.Width, bitmap.Height);
BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, bitmap.PixelFormat);
IntPtr intPtr = bitmapData.Scan0;
int length = bitmapData.Stride * bitmap.Height;
results = new byte[length];
Marshal.Copy(intPtr, results, 0, length);
bitmap.UnlockBits(bitmapData);
#pragma warning restore CA1416
return results;
}
public static (ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension) GetPngLowQuality()
{
(ImageCodecInfo, EncoderParameters, string) result;
#pragma warning disable CA1416
ImageFormat imageFormat = ImageFormat.Png;
ImageCodecInfo[] imageCodecInfoCollection = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo imageCodecInfo = (from l in imageCodecInfoCollection where l.FormatID == imageFormat.Guid select l).First();
EncoderParameters encoderParameters = new(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
if (string.IsNullOrEmpty(imageCodecInfo.FilenameExtension))
throw new NullReferenceException(nameof(imageCodecInfo.FilenameExtension));
result = new(imageCodecInfo, encoderParameters, imageCodecInfo.FilenameExtension.Split(';')[0].ToLower()[1..]);
#pragma warning restore CA1416
return result;
}
public static (ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension) GetJpegLowQuality()
{
(ImageCodecInfo, EncoderParameters, string) result;
#pragma warning disable CA1416
ImageFormat imageFormat = ImageFormat.Jpeg;
ImageCodecInfo[] imageCodecInfoCollection = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo imageCodecInfo = (from l in imageCodecInfoCollection where l.FormatID == imageFormat.Guid select l).First();
EncoderParameters encoderParameters = new(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
if (string.IsNullOrEmpty(imageCodecInfo.FilenameExtension))
throw new NullReferenceException(nameof(imageCodecInfo.FilenameExtension));
result = new(imageCodecInfo, encoderParameters, imageCodecInfo.FilenameExtension.Split(';')[0].ToLower()[1..]);
#pragma warning restore CA1416
return result;
}
public static (ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension) GetTuple(string outputExtension, int outputQuality)
{
(ImageCodecInfo, EncoderParameters, string) result;
#pragma warning disable CA1416
ImageFormat imageFormat = outputExtension switch
{
".gif" => ImageFormat.Gif,
".jfif" => ImageFormat.Jpeg,
".jpe" => ImageFormat.Jpeg,
".jpeg" => ImageFormat.Jpeg,
".jpg" => ImageFormat.Jpeg,
".png" => ImageFormat.Png,
".tif" => ImageFormat.Tiff,
".tiff" => ImageFormat.Tiff,
_ => throw new Exception(),
};
ImageCodecInfo[] imageCodecInfoCollection = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo imageCodecInfo = (from l in imageCodecInfoCollection where l.FormatID == imageFormat.Guid select l).First();
EncoderParameters encoderParameters = new(1);
// encoderParameters.Param[0] = New EncoderParameter(Encoder.Quality, CType(75L, Int32)) 'Default
// encoderParameters.Param[0] = New EncoderParameter(Encoder.Quality, CType(95L, Int32)) 'Paint
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, outputQuality);
if (string.IsNullOrEmpty(imageCodecInfo.FilenameExtension))
throw new NullReferenceException(nameof(imageCodecInfo.FilenameExtension));
result = new(imageCodecInfo, encoderParameters, imageCodecInfo.FilenameExtension.Split(';')[0].ToLower()[1..]);
#pragma warning restore CA1416
return result;
}
public FileHolder GetResizedFileHolder(string cResultsFullGroupDirectory, Item item, bool outputResolutionHasNumber, int id) =>
GetResizedFileHolder(cResultsFullGroupDirectory, item.FilePath, outputResolutionHasNumber, $"{id}{item.FilePath.ExtensionLowered}");
private FileHolder GetResizedFileHolder(string cResultsFullGroupDirectory, FilePath filePath, bool outputResolutionHasNumber, string fileName)
{
FileHolder result;
if (outputResolutionHasNumber)
result = Shared.Models.Stateless.Methods.IFileHolder.Get(Path.Combine(AngleBracketCollection[0].Replace("<>", _PropertyConfiguration.ResultContent), fileName));
else
{
CombinedEnumAndIndex cei = Shared.Models.Stateless.Methods.IPath.GetCombinedEnumAndIndex(_PropertyConfiguration, filePath);
result = Shared.Models.Stateless.Methods.IFileHolder.Get(Path.Combine(cResultsFullGroupDirectory, _PropertyConfiguration.ResultContent, cei.Combined, fileName));
}
return result;
}
public override string ToString()
{
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
@ -96,373 +188,10 @@ public class C_Resize
converted: true));
}
#pragma warning disable CA1416
public static (ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension) GetJpegLowQuality()
{
(ImageCodecInfo, EncoderParameters, string) result;
ImageFormat imageFormat = ImageFormat.Jpeg;
ImageCodecInfo[] imageCodecInfoCollection = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo imageCodecInfo = (from l in imageCodecInfoCollection where l.FormatID == imageFormat.Guid select l).First();
EncoderParameters encoderParameters = new(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
if (string.IsNullOrEmpty(imageCodecInfo.FilenameExtension))
throw new NullReferenceException(nameof(imageCodecInfo.FilenameExtension));
result = new(imageCodecInfo, encoderParameters, imageCodecInfo.FilenameExtension.Split(';')[0].ToLower()[1..]);
return result;
}
public static (ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension) GetPngLowQuality()
{
(ImageCodecInfo, EncoderParameters, string) result;
ImageFormat imageFormat = ImageFormat.Png;
ImageCodecInfo[] imageCodecInfoCollection = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo imageCodecInfo = (from l in imageCodecInfoCollection where l.FormatID == imageFormat.Guid select l).First();
EncoderParameters encoderParameters = new(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
if (string.IsNullOrEmpty(imageCodecInfo.FilenameExtension))
throw new NullReferenceException(nameof(imageCodecInfo.FilenameExtension));
result = new(imageCodecInfo, encoderParameters, imageCodecInfo.FilenameExtension.Split(';')[0].ToLower()[1..]);
return result;
}
public static (ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension) GetTuple(string outputExtension, int outputQuality)
{
(ImageCodecInfo, EncoderParameters, string) result;
ImageFormat imageFormat = outputExtension switch
{
".gif" => ImageFormat.Gif,
".jfif" => ImageFormat.Jpeg,
".jpe" => ImageFormat.Jpeg,
".jpeg" => ImageFormat.Jpeg,
".jpg" => ImageFormat.Jpeg,
".png" => ImageFormat.Png,
".tif" => ImageFormat.Tiff,
".tiff" => ImageFormat.Tiff,
_ => throw new Exception(),
};
ImageCodecInfo[] imageCodecInfoCollection = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo imageCodecInfo = (from l in imageCodecInfoCollection where l.FormatID == imageFormat.Guid select l).First();
EncoderParameters encoderParameters = new(1);
// encoderParameters.Param[0] = New EncoderParameter(Encoder.Quality, CType(75L, Int32)) 'Default
// encoderParameters.Param[0] = New EncoderParameter(Encoder.Quality, CType(95L, Int32)) 'Paint
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, outputQuality);
if (string.IsNullOrEmpty(imageCodecInfo.FilenameExtension))
throw new NullReferenceException(nameof(imageCodecInfo.FilenameExtension));
result = new(imageCodecInfo, encoderParameters, imageCodecInfo.FilenameExtension.Split(';')[0].ToLower()[1..]);
return result;
}
public static byte[] GetBitmapData(Bitmap bitmap)
{
byte[] results;
Rectangle rectangle = new(0, 0, bitmap.Width, bitmap.Height);
BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, bitmap.PixelFormat);
IntPtr intPtr = bitmapData.Scan0;
int length = bitmapData.Stride * bitmap.Height;
results = new byte[length];
Marshal.Copy(intPtr, results, 0, length);
bitmap.UnlockBits(bitmapData);
return results;
}
private void CopyPropertyItems(byte[] bytes, PropertyItem[] propertyItems, Bitmap bitmap)
{
bool hasId = false;
int id = MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagDateTimeDigitized;
int imageWidth = MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagImageWidth;
int imageHeight = MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagImageHeight;
int orientation = MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagOrientation;
foreach (PropertyItem propertyItem in propertyItems)
{
if (propertyItem.Id == id)
hasId = true;
else if (propertyItem.Id == imageWidth)
continue;
else if (propertyItem.Id == imageHeight)
continue;
else if (propertyItem.Id == orientation)
continue;
bitmap.SetPropertyItem(propertyItem);
}
if (!hasId)
{
PropertyItem propertyItem = (PropertyItem)_ConstructorInfo.Invoke(null);
propertyItem.Id = id;
propertyItem.Len = bytes.Length;
propertyItem.Type = 2;
propertyItem.Value = bytes;
bitmap.SetPropertyItem(propertyItem);
}
}
private void SaveResizedSubfile3(MappingFromItem mappingFromItem, int[] resize, byte[] bytes)
{
Bitmap bitmap;
int outputResolutionWidth = resize[_OutputResolutionWidthIndex];
using Bitmap temp = new(mappingFromItem.FilePath.FullName, useIcm: false);
int outputResolutionHeight = resize[_OutputResolutionHeightIndex];
PropertyItem[] propertyItems = temp.PropertyItems;
int outputResolutionOrientation = resize[_OutputResolutionOrientationIndex];
bitmap = new(temp, outputResolutionWidth, outputResolutionHeight);
switch (outputResolutionOrientation) // exif 274
{
case 0:
break;
case 1:
break; // 1 = Horizontal (normal)
case 2:
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
break; // 2 = Mirror horizontal
case 3:
bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
break; // 3 = Rotate 180
case 4:
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
break; // 4 = Mirror vertical
case 5:
bitmap.RotateFlip(RotateFlipType.Rotate270FlipX);
break; // 5 = Mirror horizontal and rotate 270 CW
case 6:
bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
break; // 6 = Rotate 90 CW
case 7:
bitmap.RotateFlip(RotateFlipType.Rotate90FlipX);
break; // 7 = Mirror horizontal and rotate 90 CW
case 8:
bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
break; // 8 = Rotate 270 CW
default:
break;
}
CopyPropertyItems(bytes, propertyItems, bitmap);
bitmap.Save(mappingFromItem.ResizedFileHolder.FullName, _ImageCodecInfo, _EncoderParameters);
bitmap.Dispose();
}
private void SaveResizedSubfile5(MappingFromItem mappingFromItem, int[] resize, byte[] bytes)
{
Bitmap bitmap;
using Bitmap temp = new(mappingFromItem.FilePath.FullName, useIcm: false);
PropertyItem[] propertyItems = temp.PropertyItems;
int tempResolutionWidth = resize[_TempResolutionWidth];
int tempResolutionHeight = resize[_TempResolutionHeight];
int outputResolutionWidth = resize[_OutputResolutionWidthIndex];
int outputResolutionHeight = resize[_OutputResolutionHeightIndex];
int outputResolutionOrientation = resize[_OutputResolutionOrientationIndex];
bitmap = new(temp, tempResolutionWidth, tempResolutionHeight);
switch (outputResolutionOrientation) // exif 274
{
case 0:
break;
case 1:
break; // 1 = Horizontal (normal)
case 2:
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
break; // 2 = Mirror horizontal
case 3:
bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
break; // 3 = Rotate 180
case 4:
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
break; // 4 = Mirror vertical
case 5:
bitmap.RotateFlip(RotateFlipType.Rotate270FlipX);
break; // 5 = Mirror horizontal and rotate 270 CW
case 6:
bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
break; // 6 = Rotate 90 CW
case 7:
bitmap.RotateFlip(RotateFlipType.Rotate90FlipX);
break; // 7 = Mirror horizontal and rotate 90 CW
case 8:
bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
break; // 8 = Rotate 270 CW
default:
break;
}
Bitmap preRotated;
Rectangle rectangle;
if (tempResolutionHeight < tempResolutionWidth)
rectangle = new Rectangle((int)((tempResolutionWidth - outputResolutionWidth) * .5), 0, outputResolutionWidth, outputResolutionHeight);
else
rectangle = new Rectangle(0, (int)((tempResolutionHeight - outputResolutionHeight) * .5), outputResolutionWidth, outputResolutionHeight);
using (preRotated = new Bitmap(outputResolutionWidth, outputResolutionHeight))
{
using (Graphics graphics = Graphics.FromImage(preRotated))
graphics.DrawImage(bitmap, new Rectangle(0, 0, outputResolutionWidth, outputResolutionHeight), rectangle, GraphicsUnit.Pixel);
CopyPropertyItems(bytes, propertyItems, bitmap);
bitmap.Save(mappingFromItem.ResizedFileHolder.FullName, _ImageCodecInfo, _EncoderParameters);
}
bitmap.Dispose();
}
#pragma warning restore CA1416
private void SaveResizedSubfile(Shared.Models.Property property, MappingFromItem mappingFromItem, int[] resize)
{
string dateTimeFormat = IProperty.DateTimeFormat();
DateTime dateTime = property.DateTimeOriginal is not null ? property.DateTimeOriginal.Value : Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(property);
string dateTimeValue = dateTime.ToString(dateTimeFormat);
byte[] bytes = _ASCIIEncoding.GetBytes(dateTimeValue);
if (_ASCIIEncoding.GetString(bytes, 0, bytes.Length) != dateTimeValue)
throw new Exception();
if (resize.Length == 3)
SaveResizedSubfile3(mappingFromItem, resize, bytes);
else if (resize.Length == 5)
SaveResizedSubfile5(mappingFromItem, resize, bytes);
else
throw new Exception();
}
public void SaveResizedSubfile(Property.Models.Configuration configuration, string outputResolution, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, Item item, Shared.Models.Property property, MappingFromItem mappingFromItem, Dictionary<string, int[]> outputResolutionToResize)
{
if (mappingFromItem.ResizedFileHolder is null)
throw new NullReferenceException(nameof(mappingFromItem.ResizedFileHolder));
#pragma warning disable CA1854
if (!outputResolutionToResize.ContainsKey(_Original))
throw new Exception();
if (!outputResolutionToResize.ContainsKey(outputResolution))
throw new Exception();
#pragma warning restore CA1854
FileInfo fileInfo = new(mappingFromItem.ResizedFileHolder.FullName);
bool check = false;
int[] resize = outputResolutionToResize[outputResolution];
int outputResolutionWidth = resize[_OutputResolutionWidthIndex];
int outputResolutionHeight = resize[_OutputResolutionHeightIndex];
int outputResolutionOrientation = resize[_OutputResolutionOrientationIndex];
int[] originalCollection = outputResolutionToResize[_Original];
string[] changesFrom = [nameof(A_Property), nameof(B_Metadata), nameof(C_Resize)];
List<DateTime> dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList();
if (_OverrideForResizeImages)
check = true;
else if (!fileInfo.Exists)
check = true;
if (outputResolutionWidth == originalCollection[_OutputResolutionWidthIndex] && outputResolutionHeight == originalCollection[_OutputResolutionHeightIndex] && outputResolutionOrientation == originalCollection[_OutputResolutionOrientationIndex])
{
if (!check && dateTimes.Count != 0 && dateTimes.Max() > fileInfo.CreationTime.AddDays(1))
check = true;
if (check)
{
// if (fileInfo.Exists)
// File.Delete(fileInfo.FullName);
// File.Copy(mappingFromItem.FilePath.FullName, fileInfo.FullName);
// item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(mappingFromItem.ResizedFileHolder));
// subFileTuples.Add(new Tuple<string, DateTime>(nameof(C_Resize), DateTime.Now));
}
}
else
{
if (!check && dateTimes.Count != 0 && dateTimes.Max() > fileInfo.LastWriteTime)
check = true;
if (check)
{
SaveResizedSubfile(property, mappingFromItem, resize);
item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(mappingFromItem.ResizedFileHolder));
subFileTuples.Add(new Tuple<string, DateTime>(nameof(C_Resize), DateTime.Now));
}
}
}
private static int[] GetCollection(string outputResolution)
{
List<int> results = [];
string[] segments = outputResolution.Split('x');
results.Add(int.Parse(segments[0]));
results.Add(int.Parse(segments[1]));
return results.ToArray();
}
private Dictionary<string, int[]> GetImageResizes(Shared.Models.Property property)
{
Dictionary<string, int[]> results = [];
int[] desired;
int checkWidth;
int checkHeight;
int desiredWidth;
int desiredHeight;
int orientation = property.Orientation is null || string.IsNullOrEmpty(property.Orientation) || !int.TryParse(property.Orientation, out int propertyOrientation) ? 0 : propertyOrientation;
if (property is null || property.Width is null || property.Height is null)
throw new NotSupportedException();
checkWidth = property.Width.Value;
checkHeight = property.Height.Value;
if (!_ValidResolutions.Contains(_Original))
results.Add(_Original, [checkWidth, checkHeight, orientation]);
foreach (string validResolution in _ValidResolutions)
{
if (validResolution == _Original)
{
desiredWidth = checkWidth;
desiredHeight = checkHeight;
}
else
{
desired = GetCollection(validResolution);
desiredWidth = desired[0];
desiredHeight = desired[1];
}
if (checkWidth <= desiredWidth && checkHeight <= desiredHeight)
results.Add(validResolution, [checkWidth, checkHeight, orientation]);
else
{
if (desiredWidth != desiredHeight)
{
if (checkWidth * desiredHeight > desiredWidth * checkHeight)
results.Add(validResolution, [desiredWidth, Convert.ToInt32(desiredWidth * checkHeight / (double)checkWidth), orientation]);
else
results.Add(validResolution, [Convert.ToInt32(desiredHeight * checkWidth / (double)checkHeight), desiredHeight, orientation]);
}
else
{
if (checkWidth * desiredHeight <= desiredWidth * checkHeight)
results.Add(validResolution, [desiredWidth, desiredHeight, orientation, desiredWidth, Convert.ToInt32(desiredWidth * checkHeight / (double)checkWidth)]);
else
results.Add(validResolution, [desiredWidth, desiredHeight, orientation, Convert.ToInt32(desiredHeight * checkWidth / (double)checkHeight), desiredHeight]);
}
}
}
return results;
}
private FileHolder GetResizedFileHolder(string cResultsFullGroupDirectory, FilePath filePath, bool outputResolutionHasNumber, string fileName)
{
FileHolder result;
if (outputResolutionHasNumber)
result = Shared.Models.Stateless.Methods.IFileHolder.Get(Path.Combine(AngleBracketCollection[0].Replace("<>", _PropertyConfiguration.ResultContent), fileName));
else
{
CombinedEnumAndIndex cei = Shared.Models.Stateless.Methods.IPath.GetCombinedEnumAndIndex(_PropertyConfiguration, filePath);
result = Shared.Models.Stateless.Methods.IFileHolder.Get(Path.Combine(cResultsFullGroupDirectory, _PropertyConfiguration.ResultContent, cei.Combined, fileName));
}
return result;
}
public FileHolder GetResizedFileHolder(string cResultsFullGroupDirectory, Item item, bool outputResolutionHasNumber) =>
GetResizedFileHolder(cResultsFullGroupDirectory, item.FilePath, outputResolutionHasNumber, item.FilePath.Name);
public FileHolder GetResizedFileHolder(string cResultsFullGroupDirectory, Item item, bool outputResolutionHasNumber, int id) =>
GetResizedFileHolder(cResultsFullGroupDirectory, item.FilePath, outputResolutionHasNumber, $"{id}{item.FilePath.ExtensionLowered}");
private static void MoveIf(string fileName, CombinedEnumAndIndex cei, string directory, FileInfo fileInfo)
{
string[] segments = directory.Split(cei.Combined);
string? checkDirectory = segments.Length == 1 ?
Path.Combine(segments[0], $"{cei.Combined[2..]}") :
segments.Length == 2 ?
$"{segments[0]}{cei.Combined[2..]}{segments[1]}" :
null;
if (checkDirectory is not null && Directory.Exists(checkDirectory))
{
string checkFile = Path.Combine(checkDirectory, fileName);
if (File.Exists(checkFile))
{
File.Move(checkFile, fileInfo.FullName);
fileInfo.Refresh();
}
}
}
public Dictionary<string, int[]> GetResizeKeyValuePairs(Property.Models.Configuration configuration, string cResultsFullGroupDirectory, FilePath filePath, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, Shared.Models.Property property, MappingFromItem mappingFromItem)
public Dictionary<string, int[]> GetResizeKeyValuePairs(Configuration configuration, string cResultsFullGroupDirectory, FilePath filePath, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, Shared.Models.Property property, MappingFromItem mappingFromItem)
{
Dictionary<string, int[]>? results;
string json;
@ -528,4 +257,285 @@ public class C_Resize
return results;
}
private static void MoveIf(string fileName, CombinedEnumAndIndex cei, string directory, FileInfo fileInfo)
{
string[] segments = directory.Split(cei.Combined);
string? checkDirectory = segments.Length == 1 ?
Path.Combine(segments[0], $"{cei.Combined[2..]}") :
segments.Length == 2 ?
$"{segments[0]}{cei.Combined[2..]}{segments[1]}" :
null;
if (checkDirectory is not null && Directory.Exists(checkDirectory))
{
string checkFile = Path.Combine(checkDirectory, fileName);
if (File.Exists(checkFile))
{
File.Move(checkFile, fileInfo.FullName);
fileInfo.Refresh();
}
}
}
private Dictionary<string, int[]> GetImageResizes(Shared.Models.Property property)
{
Dictionary<string, int[]> results = [];
int[] desired;
int checkWidth;
int checkHeight;
int desiredWidth;
int desiredHeight;
int orientation = property.Orientation is null || string.IsNullOrEmpty(property.Orientation) || !int.TryParse(property.Orientation, out int propertyOrientation) ? 0 : propertyOrientation;
if (property is null || property.Width is null || property.Height is null)
throw new NotSupportedException();
checkWidth = property.Width.Value;
checkHeight = property.Height.Value;
if (!_ValidResolutions.Contains(_Original))
results.Add(_Original, [checkWidth, checkHeight, orientation]);
foreach (string validResolution in _ValidResolutions)
{
if (validResolution == _Original)
{
desiredWidth = checkWidth;
desiredHeight = checkHeight;
}
else
{
desired = GetCollection(validResolution);
desiredWidth = desired[0];
desiredHeight = desired[1];
}
if (checkWidth <= desiredWidth && checkHeight <= desiredHeight)
results.Add(validResolution, [checkWidth, checkHeight, orientation]);
else
{
if (desiredWidth != desiredHeight)
{
if (checkWidth * desiredHeight > desiredWidth * checkHeight)
results.Add(validResolution, [desiredWidth, Convert.ToInt32(desiredWidth * checkHeight / (double)checkWidth), orientation]);
else
results.Add(validResolution, [Convert.ToInt32(desiredHeight * checkWidth / (double)checkHeight), desiredHeight, orientation]);
}
else
{
if (checkWidth * desiredHeight <= desiredWidth * checkHeight)
results.Add(validResolution, [desiredWidth, desiredHeight, orientation, desiredWidth, Convert.ToInt32(desiredWidth * checkHeight / (double)checkWidth)]);
else
results.Add(validResolution, [desiredWidth, desiredHeight, orientation, Convert.ToInt32(desiredHeight * checkWidth / (double)checkHeight), desiredHeight]);
}
}
}
return results;
}
private static int[] GetCollection(string outputResolution)
{
List<int> results = [];
string[] segments = outputResolution.Split('x');
results.Add(int.Parse(segments[0]));
results.Add(int.Parse(segments[1]));
return results.ToArray();
}
public void SaveResizedSubfile(Configuration configuration, string outputResolution, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, Item item, Shared.Models.Property property, MappingFromItem mappingFromItem, Dictionary<string, int[]> outputResolutionToResize)
{
if (mappingFromItem.ResizedFileHolder is null)
throw new NullReferenceException(nameof(mappingFromItem.ResizedFileHolder));
#pragma warning disable CA1854
if (!outputResolutionToResize.ContainsKey(_Original))
throw new Exception();
if (!outputResolutionToResize.ContainsKey(outputResolution))
throw new Exception();
#pragma warning restore CA1854
FileInfo fileInfo = new(mappingFromItem.ResizedFileHolder.FullName);
bool check = false;
int[] resize = outputResolutionToResize[outputResolution];
int outputResolutionWidth = resize[_OutputResolutionWidthIndex];
int outputResolutionHeight = resize[_OutputResolutionHeightIndex];
int outputResolutionOrientation = resize[_OutputResolutionOrientationIndex];
int[] originalCollection = outputResolutionToResize[_Original];
string[] changesFrom = [nameof(A_Property), nameof(B_Metadata), nameof(C_Resize)];
List<DateTime> dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList();
if (_OverrideForResizeImages)
check = true;
else if (!fileInfo.Exists)
check = true;
if (outputResolutionWidth == originalCollection[_OutputResolutionWidthIndex] && outputResolutionHeight == originalCollection[_OutputResolutionHeightIndex] && outputResolutionOrientation == originalCollection[_OutputResolutionOrientationIndex])
{
if (!check && dateTimes.Count != 0 && dateTimes.Max() > fileInfo.CreationTime.AddDays(1))
check = true;
if (check)
{
// if (fileInfo.Exists)
// File.Delete(fileInfo.FullName);
// File.Copy(mappingFromItem.FilePath.FullName, fileInfo.FullName);
// item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(mappingFromItem.ResizedFileHolder));
// subFileTuples.Add(new Tuple<string, DateTime>(nameof(C_Resize), DateTime.Now));
}
}
else
{
if (!check && dateTimes.Count != 0 && dateTimes.Max() > fileInfo.LastWriteTime)
check = true;
if (check)
{
SaveResizedSubfile(property, mappingFromItem, resize);
item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(mappingFromItem.ResizedFileHolder));
subFileTuples.Add(new Tuple<string, DateTime>(nameof(C_Resize), DateTime.Now));
}
}
}
private void SaveResizedSubfile(Shared.Models.Property property, MappingFromItem mappingFromItem, int[] resize)
{
string dateTimeFormat = IProperty.DateTimeFormat;
DateTime dateTime = property.DateTimeOriginal is not null ? property.DateTimeOriginal.Value : Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(property);
string dateTimeValue = dateTime.ToString(dateTimeFormat);
byte[] bytes = _ASCIIEncoding.GetBytes(dateTimeValue);
if (_ASCIIEncoding.GetString(bytes, 0, bytes.Length) != dateTimeValue)
throw new Exception();
if (resize.Length == 3)
SaveResizedSubfile3(mappingFromItem, resize, bytes);
else if (resize.Length == 5)
SaveResizedSubfile5(mappingFromItem, resize, bytes);
else
throw new Exception();
}
private void SaveResizedSubfile3(MappingFromItem mappingFromItem, int[] resize, byte[] bytes)
{
Bitmap bitmap;
#pragma warning disable CA1416
int outputResolutionWidth = resize[_OutputResolutionWidthIndex];
using Bitmap temp = new(mappingFromItem.FilePath.FullName, useIcm: false);
int outputResolutionHeight = resize[_OutputResolutionHeightIndex];
PropertyItem[] propertyItems = temp.PropertyItems;
int outputResolutionOrientation = resize[_OutputResolutionOrientationIndex];
bitmap = new(temp, outputResolutionWidth, outputResolutionHeight);
switch (outputResolutionOrientation) // exif 274
{
case 0:
break;
case 1:
break; // 1 = Horizontal (normal)
case 2:
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
break; // 2 = Mirror horizontal
case 3:
bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
break; // 3 = Rotate 180
case 4:
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
break; // 4 = Mirror vertical
case 5:
bitmap.RotateFlip(RotateFlipType.Rotate270FlipX);
break; // 5 = Mirror horizontal and rotate 270 CW
case 6:
bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
break; // 6 = Rotate 90 CW
case 7:
bitmap.RotateFlip(RotateFlipType.Rotate90FlipX);
break; // 7 = Mirror horizontal and rotate 90 CW
case 8:
bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
break; // 8 = Rotate 270 CW
default:
break;
}
CopyPropertyItems(bytes, propertyItems, bitmap);
bitmap.Save(mappingFromItem.ResizedFileHolder.FullName, _ImageCodecInfo, _EncoderParameters);
bitmap.Dispose();
#pragma warning restore CA1416
}
private void CopyPropertyItems(byte[] bytes, PropertyItem[] propertyItems, Bitmap bitmap)
{
bool hasId = false;
int id = MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagDateTimeDigitized;
int imageWidth = MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagImageWidth;
int imageHeight = MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagImageHeight;
int orientation = MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagOrientation;
#pragma warning disable CA1416
foreach (PropertyItem propertyItem in propertyItems)
{
if (propertyItem.Id == id)
hasId = true;
else if (propertyItem.Id == imageWidth)
continue;
else if (propertyItem.Id == imageHeight)
continue;
else if (propertyItem.Id == orientation)
continue;
bitmap.SetPropertyItem(propertyItem);
}
if (!hasId)
{
PropertyItem propertyItem = (PropertyItem)_ConstructorInfo.Invoke(null);
propertyItem.Id = id;
propertyItem.Len = bytes.Length;
propertyItem.Type = 2;
propertyItem.Value = bytes;
bitmap.SetPropertyItem(propertyItem);
}
#pragma warning restore CA1416
}
private void SaveResizedSubfile5(MappingFromItem mappingFromItem, int[] resize, byte[] bytes)
{
Bitmap bitmap;
#pragma warning disable CA1416
using Bitmap temp = new(mappingFromItem.FilePath.FullName, useIcm: false);
PropertyItem[] propertyItems = temp.PropertyItems;
int tempResolutionWidth = resize[_TempResolutionWidth];
int tempResolutionHeight = resize[_TempResolutionHeight];
int outputResolutionWidth = resize[_OutputResolutionWidthIndex];
int outputResolutionHeight = resize[_OutputResolutionHeightIndex];
int outputResolutionOrientation = resize[_OutputResolutionOrientationIndex];
bitmap = new(temp, tempResolutionWidth, tempResolutionHeight);
switch (outputResolutionOrientation) // exif 274
{
case 0:
break;
case 1:
break; // 1 = Horizontal (normal)
case 2:
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
break; // 2 = Mirror horizontal
case 3:
bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
break; // 3 = Rotate 180
case 4:
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
break; // 4 = Mirror vertical
case 5:
bitmap.RotateFlip(RotateFlipType.Rotate270FlipX);
break; // 5 = Mirror horizontal and rotate 270 CW
case 6:
bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
break; // 6 = Rotate 90 CW
case 7:
bitmap.RotateFlip(RotateFlipType.Rotate90FlipX);
break; // 7 = Mirror horizontal and rotate 90 CW
case 8:
bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
break; // 8 = Rotate 270 CW
default:
break;
}
Bitmap preRotated;
Rectangle rectangle;
if (tempResolutionHeight < tempResolutionWidth)
rectangle = new Rectangle((int)((tempResolutionWidth - outputResolutionWidth) * .5), 0, outputResolutionWidth, outputResolutionHeight);
else
rectangle = new Rectangle(0, (int)((tempResolutionHeight - outputResolutionHeight) * .5), outputResolutionWidth, outputResolutionHeight);
using (preRotated = new Bitmap(outputResolutionWidth, outputResolutionHeight))
{
using (Graphics graphics = Graphics.FromImage(preRotated))
graphics.DrawImage(bitmap, new Rectangle(0, 0, outputResolutionWidth, outputResolutionHeight), rectangle, GraphicsUnit.Pixel);
CopyPropertyItems(bytes, propertyItems, bitmap);
bitmap.Save(mappingFromItem.ResizedFileHolder.FullName, _ImageCodecInfo, _EncoderParameters);
}
bitmap.Dispose();
#pragma warning restore CA1416
}
}