diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs index 8cd88b963..91c5db1aa 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs @@ -19,11 +19,6 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// internal class TiffDecoderCore : IImageDecoderInternals { - /// - /// The global configuration - /// - private readonly Configuration configuration; - /// /// Used for allocating memory during processing operations. /// @@ -48,9 +43,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { options ??= new TiffDecoder(); - this.configuration = configuration ?? Configuration.Default; + this.Configuration = configuration ?? Configuration.Default; this.ignoreMetadata = options.IgnoreMetadata; - this.memoryAllocator = this.configuration.MemoryAllocator; + this.memoryAllocator = this.Configuration.MemoryAllocator; } /// @@ -91,7 +86,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff public TiffPredictor Predictor { get; set; } /// - public Configuration Configuration => this.configuration; + public Configuration Configuration { get; } /// public Size Dimensions { get; private set; } @@ -129,7 +124,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff } } - var image = new Image(this.configuration, metadata, frames); + var image = new Image(this.Configuration, metadata, frames); return image; } @@ -175,7 +170,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff int width = (int)frameMetaData.Width; int height = (int)frameMetaData.Height; - var frame = new ImageFrame(this.configuration, width, height, coreMetadata); + var frame = new ImageFrame(this.Configuration, width, height, coreMetadata); int rowsPerStrip = (int)frameMetaData.RowsPerStrip; Number[] stripOffsets = frameMetaData.StripOffsets; diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderMetadataCreator.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderMetadataCreator.cs index ff093777a..865c1eeba 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderMetadataCreator.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderMetadataCreator.cs @@ -47,28 +47,28 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { if (tiffMetadata.XmpProfile == null) { - byte[] buf = frame.GetArray(ExifTag.XMP, true); - if (buf != null) + IExifValue val = frame.ExifProfile.GetValue(ExifTag.XMP); + if (val != null) { - tiffMetadata.XmpProfile = buf; + tiffMetadata.XmpProfile = val.Value; } } if (coreMetadata.IptcProfile == null) { - byte[] buf = frame.GetArray(ExifTag.IPTC, true); - if (buf != null) + IExifValue val = frame.ExifProfile.GetValue(ExifTag.IPTC); + if (val != null) { - coreMetadata.IptcProfile = new IptcProfile(buf); + coreMetadata.IptcProfile = new IptcProfile(val.Value); } } if (coreMetadata.IccProfile == null) { - byte[] buf = frame.GetArray(ExifTag.IccProfile, true); - if (buf != null) + IExifValue val = frame.ExifProfile.GetValue(ExifTag.IccProfile); + if (val != null) { - coreMetadata.IccProfile = new IccProfile(buf); + coreMetadata.IccProfile = new IccProfile(val.Value); } } } diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs index 713c85c06..439a6bbe2 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs @@ -29,7 +29,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff TiffThrowHelper.ThrowNotSupported("The lower-order bits of the byte FillOrder is not supported."); } - if (entries.GetArray(ExifTag.TileOffsets, true) != null) + if (entries.ExifProfile.GetValue(ExifTag.TileOffsets) != null) { TiffThrowHelper.ThrowNotSupported("The Tile images is not supported."); } diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs index f0e41c63b..9cdce21e2 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs @@ -114,17 +114,17 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff var xResolution = new ExifRational(ExifTagValue.XResolution) { - Value = frameMetadata.GetSingle(ExifTag.XResolution) + Value = frameMetadata.ExifProfile.GetValue(ExifTag.XResolution).Value }; var yResolution = new ExifRational(ExifTagValue.YResolution) { - Value = frameMetadata.GetSingle(ExifTag.YResolution) + Value = frameMetadata.ExifProfile.GetValue(ExifTag.YResolution).Value }; var resolutionUnit = new ExifShort(ExifTagValue.ResolutionUnit) { - Value = frameMetadata.GetSingle(ExifTag.ResolutionUnit) + Value = frameMetadata.ExifProfile.GetValue(ExifTag.ResolutionUnit).Value }; this.collector.AddInternal(xResolution); @@ -183,7 +183,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff } else { - tiffFrameMetadata.Remove(ExifTag.SubIFDOffset); + tiffFrameMetadata.ExifProfile.RemoveValue(ExifTag.SubIFDOffset); } if (imageMetadata.IptcProfile != null) @@ -198,7 +198,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff } else { - tiffFrameMetadata.Remove(ExifTag.IPTC); + tiffFrameMetadata.ExifProfile.RemoveValue(ExifTag.IPTC); } if (imageMetadata.IccProfile != null) @@ -212,7 +212,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff } else { - tiffFrameMetadata.Remove(ExifTag.IccProfile); + tiffFrameMetadata.ExifProfile.RemoveValue(ExifTag.IccProfile); } TiffMetadata tiffMetadata = imageMetadata.GetTiffMetadata(); @@ -227,7 +227,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff } else { - tiffFrameMetadata.Remove(ExifTag.XMP); + tiffFrameMetadata.ExifProfile.RemoveValue(ExifTag.XMP); } } } diff --git a/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs b/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs index a099e1e19..e89e337b7 100644 --- a/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs +++ b/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System.Collections.Generic; - +using System.Linq; using SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata.Profiles.Exif; @@ -41,21 +41,21 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// Gets a general indication of the kind of data contained in this subfile. /// A general indication of the kind of data contained in this subfile. - public TiffNewSubfileType SubfileType => this.GetSingleEnum(ExifTag.SubfileType, TiffNewSubfileType.FullImage); + public TiffNewSubfileType SubfileType => (TiffNewSubfileType?)this.ExifProfile.GetValue(ExifTag.SubfileType)?.Value ?? TiffNewSubfileType.FullImage; /// Gets a general indication of the kind of data contained in this subfile. /// A general indication of the kind of data contained in this subfile. - public TiffSubfileType? OldSubfileType => this.GetSingleEnumNullable(ExifTag.OldSubfileType); + public TiffSubfileType? OldSubfileType => (TiffSubfileType?)this.ExifProfile.GetValue(ExifTag.OldSubfileType)?.Value; /// /// Gets the number of columns in the image, i.e., the number of pixels per row. /// - public Number Width => this.GetSingle(ExifTag.ImageWidth); + public Number Width => this.ExifProfile.GetValue(ExifTag.ImageWidth).Value; /// /// Gets the number of rows of pixels in the image. /// - public Number Height => this.GetSingle(ExifTag.ImageLength); + public Number Height => this.ExifProfile.GetValue(ExifTag.ImageLength).Value; /// /// Gets the number of bits per component. @@ -64,7 +64,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { get { - var bits = this.GetArray(ExifTag.BitsPerSample, true); + var bits = this.ExifProfile.GetValue(ExifTag.BitsPerSample)?.Value; if (bits == null) { if (this.PhotometricInterpretation == TiffPhotometricInterpretation.WhiteIsZero @@ -98,25 +98,25 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// Gets the compression scheme used on the image data. /// The compression scheme used on the image data. - public TiffCompression Compression => this.GetSingleEnum(ExifTag.Compression); + public TiffCompression Compression => (TiffCompression)this.ExifProfile.GetValue(ExifTag.Compression).Value; /// /// Gets the color space of the image data. /// - public TiffPhotometricInterpretation PhotometricInterpretation => this.GetSingleEnum(ExifTag.PhotometricInterpretation); + public TiffPhotometricInterpretation PhotometricInterpretation => (TiffPhotometricInterpretation)this.ExifProfile.GetValue(ExifTag.PhotometricInterpretation).Value; /// /// Gets the logical order of bits within a byte. /// - internal TiffFillOrder FillOrder => this.GetSingleEnum(ExifTag.FillOrder, TiffFillOrder.MostSignificantBitFirst); + internal TiffFillOrder FillOrder => (TiffFillOrder?)this.ExifProfile.GetValue(ExifTag.FillOrder)?.Value ?? TiffFillOrder.MostSignificantBitFirst; /// /// Gets or sets the a string that describes the subject of the image. /// public string ImageDescription { - get => this.GetString(ExifTag.ImageDescription); - set => this.Set(ExifTag.ImageDescription, value); + get => this.ExifProfile.GetValue(ExifTag.ImageDescription)?.Value; + set => this.ExifProfile.SetValue(ExifTag.ImageDescription, value); } /// @@ -124,8 +124,8 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// public string Make { - get => this.GetString(ExifTag.Make); - set => this.Set(ExifTag.Make, value); + get => this.ExifProfile.GetValue(ExifTag.Make)?.Value; + set => this.ExifProfile.SetValue(ExifTag.Make, value); } /// @@ -133,27 +133,27 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// public string Model { - get => this.GetString(ExifTag.Model); - set => this.Set(ExifTag.Model, value); + get => this.ExifProfile.GetValue(ExifTag.Model)?.Value; + set => this.ExifProfile.SetValue(ExifTag.Model, value); } /// Gets for each strip, the byte offset of that strip.. - public Number[] StripOffsets => this.GetArray(ExifTag.StripOffsets); + public Number[] StripOffsets => this.ExifProfile.GetValue(ExifTag.StripOffsets).Value; /// /// Gets the number of components per pixel. /// - public ushort SamplesPerPixel => this.GetSingle(ExifTag.SamplesPerPixel); + public ushort SamplesPerPixel => this.ExifProfile.GetValue(ExifTag.SamplesPerPixel).Value; /// /// Gets the number of rows per strip. /// - public Number RowsPerStrip => this.GetSingle(ExifTag.RowsPerStrip); + public Number RowsPerStrip => this.ExifProfile.GetValue(ExifTag.RowsPerStrip).Value; /// /// Gets for each strip, the number of bytes in the strip after compression. /// - public Number[] StripByteCounts => this.GetArray(ExifTag.StripByteCounts); + public Number[] StripByteCounts => this.ExifProfile.GetValue(ExifTag.StripByteCounts).Value; /// Gets the resolution of the image in x- direction. /// The density of the image in x- direction. @@ -168,7 +168,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// /// Gets how the components of each pixel are stored. /// - public TiffPlanarConfiguration PlanarConfiguration => this.GetSingleEnum(ExifTag.PlanarConfiguration, DefaultPlanarConfiguration); + public TiffPlanarConfiguration PlanarConfiguration => (TiffPlanarConfiguration?)this.ExifProfile.GetValue(ExifTag.PlanarConfiguration)?.Value ?? DefaultPlanarConfiguration; /// /// Gets the unit of measurement for XResolution and YResolution. @@ -180,8 +180,8 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// public string Software { - get => this.GetString(ExifTag.Software); - set => this.Set(ExifTag.Software, value); + get => this.ExifProfile.GetValue(ExifTag.Software)?.Value; + set => this.ExifProfile.SetValue(ExifTag.Software, value); } /// @@ -189,8 +189,8 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// public string DateTime { - get => this.GetString(ExifTag.DateTime); - set => this.Set(ExifTag.DateTime, value); + get => this.ExifProfile.GetValue(ExifTag.DateTime)?.Value; + set => this.ExifProfile.SetValue(ExifTag.DateTime, value); } /// @@ -198,8 +198,8 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// public string Artist { - get => this.GetString(ExifTag.Artist); - set => this.Set(ExifTag.Artist, value); + get => this.ExifProfile.GetValue(ExifTag.Artist)?.Value; + set => this.ExifProfile.SetValue(ExifTag.Artist, value); } /// @@ -207,39 +207,39 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// public string HostComputer { - get => this.GetString(ExifTag.HostComputer); - set => this.Set(ExifTag.HostComputer, value); + get => this.ExifProfile.GetValue(ExifTag.HostComputer)?.Value; + set => this.ExifProfile.SetValue(ExifTag.HostComputer, value); } /// /// Gets a color map for palette color images. /// - public ushort[] ColorMap => this.GetArray(ExifTag.ColorMap, true); + public ushort[] ColorMap => this.ExifProfile.GetValue(ExifTag.ColorMap)?.Value; /// /// Gets the description of extra components. /// - public ushort[] ExtraSamples => this.GetArray(ExifTag.ExtraSamples, true); + public ushort[] ExtraSamples => this.ExifProfile.GetValue(ExifTag.ExtraSamples)?.Value; /// /// Gets or sets the copyright notice. /// public string Copyright { - get => this.GetString(ExifTag.Copyright); - set => this.Set(ExifTag.Copyright, value); + get => this.ExifProfile.GetValue(ExifTag.Copyright)?.Value; + set => this.ExifProfile.SetValue(ExifTag.Copyright, value); } /// /// Gets a mathematical operator that is applied to the image data before an encoding scheme is applied. /// - public TiffPredictor Predictor => this.GetSingleEnum(ExifTag.Predictor, DefaultPredictor); + public TiffPredictor Predictor => (TiffPredictor?)this.ExifProfile.GetValue(ExifTag.Predictor)?.Value ?? DefaultPredictor; /// /// Gets the specifies how to interpret each data sample in a pixel. /// /// - public TiffSampleFormat[] SampleFormat => this.GetEnumArray(ExifTag.SampleFormat, true); + public TiffSampleFormat[] SampleFormat => this.ExifProfile.GetValue(ExifTag.SampleFormat)?.Value?.Select(a => (TiffSampleFormat)a).ToArray(); /// /// Clears the metadata. diff --git a/src/ImageSharp/Formats/Tiff/TiffFrameMetadataExtensions.cs b/src/ImageSharp/Formats/Tiff/TiffFrameMetadataExtensions.cs deleted file mode 100644 index 7521e9334..000000000 --- a/src/ImageSharp/Formats/Tiff/TiffFrameMetadataExtensions.cs +++ /dev/null @@ -1,136 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -using System.Linq; - -using SixLabors.ImageSharp.Metadata.Profiles.Exif; - -namespace SixLabors.ImageSharp.Formats.Experimental.Tiff -{ - /// - /// The tiff metadata extensions - /// - internal static class TiffFrameMetadataExtensions - { - public static T[] GetArray(this TiffFrameMetadata meta, ExifTag tag, bool optional = false) - where T : struct - { - if (meta.TryGetArray(tag, out T[] result)) - { - return result; - } - - if (!optional) - { - TiffThrowHelper.ThrowTagNotFound(nameof(tag)); - } - - return null; - } - - public static bool TryGetArray(this TiffFrameMetadata meta, ExifTag tag, out T[] result) - where T : struct - { - IExifValue obj = meta.ExifProfile.GetValueInternal(tag); - if (obj != null) - { - DebugGuard.IsTrue(obj.IsArray, "Expected array entry"); - object value = obj.GetValue(); - result = (T[])value; - return true; - } - - result = null; - return false; - } - - public static TEnum[] GetEnumArray(this TiffFrameMetadata meta, ExifTag tag, bool optional = false) - where TEnum : struct - where TTagValue : struct - { - if (meta.TryGetArray(tag, out TTagValue[] result)) - { - return result.Select(a => (TEnum)(object)a).ToArray(); - } - - if (!optional) - { - TiffThrowHelper.ThrowTagNotFound(nameof(tag)); - } - - return null; - } - - public static string GetString(this TiffFrameMetadata meta, ExifTag tag) - { - IExifValue obj = meta.ExifProfile.GetValueInternal(tag); - if (obj != null) - { - DebugGuard.IsTrue(obj.DataType == ExifDataType.Ascii, "Expected string entry"); - object value = obj.GetValue(); - DebugGuard.IsTrue(value is string, "Expected string entry"); - return (string)value; - } - - return null; - } - - public static void Set(this TiffFrameMetadata meta, ExifTag tag, object value) => - meta.ExifProfile.SetValueInternal(tag, value); - - public static TEnum? GetSingleEnumNullable(this TiffFrameMetadata meta, ExifTag tag) - where TEnum : struct - where TTagValue : struct - { - if (!meta.TryGetSingle(tag, out TTagValue value)) - { - return null; - } - - return (TEnum)(object)value; - } - - public static TEnum GetSingleEnum(this TiffFrameMetadata meta, ExifTag tag, TEnum? defaultValue = null) - where TEnum : struct - where TTagValue : struct - => meta.GetSingleEnumNullable(tag) ?? (defaultValue != null ? defaultValue.Value : throw TiffThrowHelper.TagNotFound(nameof(tag))); - - public static T GetSingle(this TiffFrameMetadata meta, ExifTag tag) - where T : struct - { - if (meta.TryGetSingle(tag, out T result)) - { - return result; - } - - throw TiffThrowHelper.TagNotFound(nameof(tag)); - } - - public static bool TryGetSingle(this TiffFrameMetadata meta, ExifTag tag, out T result) - where T : struct - { - IExifValue obj = meta.ExifProfile.GetValueInternal(tag); - if (obj != null) - { - DebugGuard.IsTrue(!obj.IsArray, "Expected non array entry"); - object value = obj.GetValue(); - result = (T)value; - return true; - } - - result = default; - return false; - } - - public static bool Remove(this TiffFrameMetadata meta, ExifTag tag) - { - IExifValue obj = meta.ExifProfile.GetValueInternal(tag); - if (obj != null) - { - return meta.ExifProfile.RemoveValue(obj.Tag); - } - - return false; - } - } -} diff --git a/src/ImageSharp/Formats/Tiff/TiffFrameMetadataResolutionExtensions.cs b/src/ImageSharp/Formats/Tiff/TiffFrameMetadataResolutionExtensions.cs index e45420c91..5a7ad1ac9 100644 --- a/src/ImageSharp/Formats/Tiff/TiffFrameMetadataResolutionExtensions.cs +++ b/src/ImageSharp/Formats/Tiff/TiffFrameMetadataResolutionExtensions.cs @@ -19,9 +19,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff break; case PixelResolutionUnit.PixelsPerMeter: { - unit = PixelResolutionUnit.PixelsPerCentimeter; - horizontal = UnitConverter.MeterToCm(horizontal); - vertical = UnitConverter.MeterToCm(vertical); + unit = PixelResolutionUnit.PixelsPerCentimeter; + horizontal = UnitConverter.MeterToCm(horizontal); + vertical = UnitConverter.MeterToCm(vertical); } break; @@ -30,29 +30,27 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff break; } - meta.Set(ExifTag.ResolutionUnit, (ushort)unit + 1); + meta.ExifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)(unit + 1)); meta.SetResolution(ExifTag.XResolution, horizontal); meta.SetResolution(ExifTag.YResolution, vertical); } public static PixelResolutionUnit GetResolutionUnit(this TiffFrameMetadata meta) { - if (!meta.TryGetSingle(ExifTag.ResolutionUnit, out ushort res)) - { - res = TiffFrameMetadata.DefaultResolutionUnit; - } + ushort res = meta.ExifProfile.GetValue(ExifTag.ResolutionUnit)?.Value ?? TiffFrameMetadata.DefaultResolutionUnit; return (PixelResolutionUnit)(res - 1); } - public static double? GetResolution(this TiffFrameMetadata meta, ExifTag tag) + public static double? GetResolution(this TiffFrameMetadata meta, ExifTag tag) { - if (!meta.TryGetSingle(tag, out Rational resolution)) + IExifValue resolution = meta.ExifProfile.GetValue(tag); + if (resolution == null) { return null; } - double res = resolution.ToDouble(); + double res = resolution.Value.ToDouble(); switch (meta.ResolutionUnit) { case PixelResolutionUnit.AspectRatio: @@ -68,11 +66,11 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff } } - private static void SetResolution(this TiffFrameMetadata meta, ExifTag tag, double? value) + private static void SetResolution(this TiffFrameMetadata meta, ExifTag tag, double? value) { if (value == null) { - meta.Remove(tag); + meta.ExifProfile.RemoveValue(tag); return; } else @@ -94,7 +92,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff break; } - meta.Set(tag, new Rational(res)); + meta.ExifProfile.SetValue(tag, new Rational(res)); } } }