| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | using MetadataExtractor; |
| | | 3 | | using MetadataExtractor.Formats.Avi; |
| | | 4 | | using MetadataExtractor.Formats.Bmp; |
| | | 5 | | using MetadataExtractor.Formats.Exif; |
| | | 6 | | using MetadataExtractor.Formats.Gif; |
| | | 7 | | using MetadataExtractor.Formats.Heif; |
| | | 8 | | using MetadataExtractor.Formats.Jpeg; |
| | | 9 | | using MetadataExtractor.Formats.Png; |
| | | 10 | | using MetadataExtractor.Formats.QuickTime; |
| | | 11 | | using MetadataExtractor.Formats.WebP; |
| | | 12 | | using Directory = MetadataExtractor.Directory; |
| | | 13 | | |
| | | 14 | | namespace Anichron.Worker.Ingestion.Extraction; |
| | | 15 | | |
| | | 16 | | /// <summary>Reads a file's stored size and the orientation it is shown in.</summary> |
| | | 17 | | internal static class Frames |
| | | 18 | | { |
| | | 19 | | private const string heicPrimaryItemProperties = "HEIC Primary Item Properties"; |
| | | 20 | | |
| | | 21 | | public static (int Width, int Height, int Orientation) Of(IReadOnlyList<Directory> directories) |
| | 186 | 22 | | { |
| | 186 | 23 | | var cameraInfo = directories.OfType<ExifIfd0Directory>().FirstOrDefault(); |
| | 186 | 24 | | var heicPrimary = directories.OfType<HeicImagePropertiesDirectory>() |
| | 186 | 25 | | .FirstOrDefault(d => d.Name == heicPrimaryItemProperties); |
| | 186 | 26 | | var videoTrack = directories.OfType<QuickTimeTrackHeaderDirectory>().FirstOrDefault(HasSize); |
| | | 27 | | |
| | 186 | 28 | | var (width, height) = GetSize( |
| | 186 | 29 | | new SizeSource(heicPrimary, HeicImagePropertiesDirectory.TagImageWidth, HeicImagePropertiesDirectory.TagImag |
| | 186 | 30 | | SizeSource.First<JpegDirectory>(directories, JpegDirectory.TagImageWidth, JpegDirectory.TagImageHeight), |
| | 186 | 31 | | SizeSource.First<PngDirectory>(directories, PngDirectory.TagImageWidth, PngDirectory.TagImageHeight), |
| | 186 | 32 | | SizeSource.First<GifHeaderDirectory>(directories, GifHeaderDirectory.TagImageWidth, GifHeaderDirectory.TagIm |
| | 186 | 33 | | SizeSource.First<WebPDirectory>(directories, WebPDirectory.TagImageWidth, WebPDirectory.TagImageHeight), |
| | 186 | 34 | | SizeSource.First<BmpHeaderDirectory>(directories, BmpHeaderDirectory.TagImageWidth, BmpHeaderDirectory.TagIm |
| | 186 | 35 | | new SizeSource(cameraInfo, ExifDirectoryBase.TagImageWidth, ExifDirectoryBase.TagImageHeight), |
| | 186 | 36 | | new SizeSource(videoTrack, QuickTimeTrackHeaderDirectory.TagWidth, QuickTimeTrackHeaderDirectory.TagHeight), |
| | 186 | 37 | | SizeSource.First<AviDirectory>(directories, AviDirectory.TagWidth, AviDirectory.TagHeight)); |
| | | 38 | | |
| | 186 | 39 | | return (width, height, GetOrientation(heicPrimary, videoTrack, cameraInfo)); |
| | 186 | 40 | | } |
| | | 41 | | |
| | | 42 | | private static bool HasSize(QuickTimeTrackHeaderDirectory track) |
| | 23 | 43 | | => new SizeSource(track, QuickTimeTrackHeaderDirectory.TagWidth, QuickTimeTrackHeaderDirectory.TagHeight).Read() |
| | | 44 | | |
| | | 45 | | private static (int Width, int Height) GetSize(params SizeSource[] sources) |
| | 186 | 46 | | => sources.Select(source => source.Read()).FirstOrDefault(size => size is not null) ?? (0, 0); |
| | | 47 | | |
| | | 48 | | private static int GetOrientation( |
| | | 49 | | HeicImagePropertiesDirectory? heicPrimary, QuickTimeTrackHeaderDirectory? videoTrack, ExifIfd0Directory? cameraI |
| | 186 | 50 | | { |
| | 186 | 51 | | if (heicPrimary is not null) |
| | 9 | 52 | | return ExifOrientation.OfDisplayRotation(RotationOf(heicPrimary, HeicImagePropertiesDirectory.TagRotation)); |
| | 177 | 53 | | if (videoTrack is not null) |
| | 16 | 54 | | return ExifOrientation.OfDisplayRotation(RotationOf(videoTrack, QuickTimeTrackHeaderDirectory.TagRotation)); |
| | 161 | 55 | | return cameraInfo is not null |
| | 161 | 56 | | && cameraInfo.TryGetInt32(ExifDirectoryBase.TagOrientation, out var orientation) |
| | 161 | 57 | | && orientation is |
| | 161 | 58 | | // Stryker disable once Equality |
| | 161 | 59 | | >= ExifOrientation.Upright |
| | 161 | 60 | | and <= ExifOrientation.Max |
| | 161 | 61 | | ? orientation |
| | 161 | 62 | | : ExifOrientation.Upright; |
| | 186 | 63 | | } |
| | | 64 | | |
| | | 65 | | private static double RotationOf(Directory directory, int rotationTag) |
| | 25 | 66 | | => directory.TryGetDouble(rotationTag, out var degrees) ? degrees : 0; |
| | | 67 | | |
| | | 68 | | private readonly record struct SizeSource(Directory? Directory, int WidthTag, int HeightTag) |
| | | 69 | | { |
| | | 70 | | public static SizeSource First<T>(IEnumerable<Directory> directories, int widthTag, int heightTag) |
| | | 71 | | where T : Directory |
| | 1116 | 72 | | => new(directories.OfType<T>().FirstOrDefault(), widthTag, heightTag); |
| | | 73 | | |
| | | 74 | | public (int Width, int Height)? Read() |
| | 1561 | 75 | | => Directory is not null |
| | 1561 | 76 | | && Directory.TryGetInt32(WidthTag, out var width) && width > 0 |
| | 1561 | 77 | | && Directory.TryGetInt32(HeightTag, out var height) && height > 0 |
| | 1561 | 78 | | ? (width, height) |
| | 1561 | 79 | | : null; |
| | | 80 | | } |
| | | 81 | | } |