From cab2e05cc4136a91ae8a3add2617a33474aba1dd Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 31 Jan 2021 11:41:05 +0100 Subject: [PATCH] A little cleanup --- .../Tiff/Compression/TiffBaseCompression.cs | 32 ++++------- .../Formats/Tiff/Ifd/DirectoryReader.cs | 10 ++-- .../BlackIsZero1TiffColor.cs | 2 +- .../BlackIsZero4TiffColor.cs | 2 +- .../BlackIsZero8TiffColor.cs | 2 +- .../Rgb888TiffColor.cs | 2 +- .../TiffColorType.cs | 14 ++--- .../WhiteIsZero1TiffColor.cs | 2 +- .../WhiteIsZero4TiffColor.cs | 2 +- .../WhiteIsZero8TiffColor.cs | 2 +- src/ImageSharp/Formats/Tiff/README.md | 5 -- .../Tiff/TiffDecoderMetadataCreator.cs | 6 +-- .../Formats/Tiff/TiffDecoderOptionsParser.cs | 6 +-- .../Formats/Tiff/TiffEncoderCore.cs | 20 ++++--- .../Tiff/TiffEncoderEntriesCollector.cs | 6 +-- .../Formats/Tiff/TiffFrameMetadata.cs | 54 +++++++++++-------- .../TiffFrameMetadataResolutionExtensions.cs | 40 +++++++------- .../Formats/Tiff/TiffThrowHelper.cs | 33 ++---------- .../Formats/Tiff/Utils/TiffWriter.cs | 19 ++----- 19 files changed, 109 insertions(+), 150 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/Compression/TiffBaseCompression.cs b/src/ImageSharp/Formats/Tiff/Compression/TiffBaseCompression.cs index 323e77783..7046f0fe1 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/TiffBaseCompression.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/TiffBaseCompression.cs @@ -15,42 +15,32 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Compression /// internal abstract class TiffBaseCompression { - private readonly MemoryAllocator allocator; - - private readonly TiffPhotometricInterpretation photometricInterpretation; - - private readonly int width; - - private readonly int bitsPerPixel; - - private readonly TiffPredictor predictor; - - protected TiffBaseCompression(MemoryAllocator allocator) => this.allocator = allocator; + protected TiffBaseCompression(MemoryAllocator allocator) => this.Allocator = allocator; protected TiffBaseCompression(MemoryAllocator allocator, TiffPhotometricInterpretation photometricInterpretation, int width) : this(allocator) { - this.photometricInterpretation = photometricInterpretation; - this.width = width; + this.PhotometricInterpretation = photometricInterpretation; + this.Width = width; } protected TiffBaseCompression(MemoryAllocator allocator, int width, int bitsPerPixel, TiffPredictor predictor) : this(allocator) { - this.width = width; - this.bitsPerPixel = bitsPerPixel; - this.predictor = predictor; + this.Width = width; + this.BitsPerPixel = bitsPerPixel; + this.Predictor = predictor; } - protected MemoryAllocator Allocator => this.allocator; + protected MemoryAllocator Allocator { get; } - protected TiffPhotometricInterpretation PhotometricInterpretation => this.photometricInterpretation; + protected TiffPhotometricInterpretation PhotometricInterpretation { get; } - protected int Width => this.width; + protected int Width { get; } - protected int BitsPerPixel => this.bitsPerPixel; + protected int BitsPerPixel { get; } - protected TiffPredictor Predictor => this.predictor; + protected TiffPredictor Predictor { get; } /// /// Decompresses image data into the supplied buffer. diff --git a/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs b/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs index 3e8f138b7..2343eca5e 100644 --- a/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs +++ b/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs @@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff return ByteOrder.BigEndian; } - throw TiffThrowHelper.InvalidHeader(); + throw TiffThrowHelper.ThrowInvalidHeader(); } private IEnumerable ReadIfds() @@ -62,7 +62,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff readers.Add(reader); } - // sequential reading big values + // Sequential reading big values. foreach (Action loader in this.lazyLoaders.Values) { loader(); @@ -78,7 +78,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff return list; } - /// used for possiblity add a duplicate offsets (but tags don't duplicate). + /// + /// used for possibility add a duplicate offsets (but tags don't duplicate). + /// /// The type of the key. private class DuplicateKeyComparer : IComparer where TKey : IComparable @@ -87,7 +89,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { int result = x.CompareTo(y); - // Handle equality as beeing greater + // Handle equality as being greater. return (result == 0) ? 1 : result; } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs index ecfae2c9b..542d675d7 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs @@ -9,7 +9,7 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { /// - /// Implements the 'BlackIsZero' photometric interpretation (optimised for bilevel images). + /// Implements the 'BlackIsZero' photometric interpretation (optimized for bilevel images). /// /// The pixel format. internal class BlackIsZero1TiffColor : TiffBaseColorDecoder diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs index 096e473e5..4d6ffa6a9 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs @@ -9,7 +9,7 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { /// - /// Implements the 'BlackIsZero' photometric interpretation (optimised for 4-bit grayscale images). + /// Implements the 'BlackIsZero' photometric interpretation (optimized for 4-bit grayscale images). /// internal class BlackIsZero4TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs index 3ed7718c4..7bffd4a92 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs @@ -9,7 +9,7 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { /// - /// Implements the 'BlackIsZero' photometric interpretation (optimised for 8-bit grayscale images). + /// Implements the 'BlackIsZero' photometric interpretation (optimized for 8-bit grayscale images). /// internal class BlackIsZero8TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs index 6bb34eb19..8fd82d98e 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs @@ -9,7 +9,7 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { /// - /// Implements the 'RGB' photometric interpretation (optimised for 8-bit full color images). + /// Implements the 'RGB' photometric interpretation (optimized for 8-bit full color images). /// internal class Rgb888TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs index 7d17ff0b7..24c8f6da5 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs @@ -14,17 +14,17 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff BlackIsZero, /// - /// Grayscale: 0 is imaged as black. The maximum value is imaged as white. Optimised implementation for bilevel images. + /// Grayscale: 0 is imaged as black. The maximum value is imaged as white. Optimized implementation for bilevel images. /// BlackIsZero1, /// - /// Grayscale: 0 is imaged as black. The maximum value is imaged as white. Optimised implementation for 4-bit images. + /// Grayscale: 0 is imaged as black. The maximum value is imaged as white. Optimized implementation for 4-bit images. /// BlackIsZero4, /// - /// Grayscale: 0 is imaged as black. The maximum value is imaged as white. Optimised implementation for 8-bit images. + /// Grayscale: 0 is imaged as black. The maximum value is imaged as white. Optimized implementation for 8-bit images. /// BlackIsZero8, @@ -34,17 +34,17 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff WhiteIsZero, /// - /// Grayscale: 0 is imaged as white. The maximum value is imaged as black. Optimised implementation for bilevel images. + /// Grayscale: 0 is imaged as white. The maximum value is imaged as black. Optimized implementation for bilevel images. /// WhiteIsZero1, /// - /// Grayscale: 0 is imaged as white. The maximum value is imaged as black. Optimised implementation for 4-bit images. + /// Grayscale: 0 is imaged as white. The maximum value is imaged as black. Optimized implementation for 4-bit images. /// WhiteIsZero4, /// - /// Grayscale: 0 is imaged as white. The maximum value is imaged as black. Optimised implementation for 8-bit images. + /// Grayscale: 0 is imaged as white. The maximum value is imaged as black. Optimized implementation for 8-bit images. /// WhiteIsZero8, @@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff Rgb, /// - /// RGB Full Color. Optimised implementation for 8-bit images. + /// RGB Full Color. Optimized implementation for 8-bit images. /// Rgb888, diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs index 2a7010f5f..4bef2dba8 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs @@ -9,7 +9,7 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { /// - /// Implements the 'WhiteIsZero' photometric interpretation (optimised for bilevel images). + /// Implements the 'WhiteIsZero' photometric interpretation (optimized for bilevel images). /// internal class WhiteIsZero1TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs index bb2a921c7..7eca9b966 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs @@ -9,7 +9,7 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { /// - /// Implements the 'WhiteIsZero' photometric interpretation (optimised for 4-bit grayscale images). + /// Implements the 'WhiteIsZero' photometric interpretation (optimized for 4-bit grayscale images). /// internal class WhiteIsZero4TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs index b9cc28543..68b74f60a 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs @@ -9,7 +9,7 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { /// - /// Implements the 'WhiteIsZero' photometric interpretation (optimised for 8-bit grayscale images). + /// Implements the 'WhiteIsZero' photometric interpretation (optimized for 8-bit grayscale images). /// internal class WhiteIsZero8TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel diff --git a/src/ImageSharp/Formats/Tiff/README.md b/src/ImageSharp/Formats/Tiff/README.md index 8f06bbb59..fd88e8eb0 100644 --- a/src/ImageSharp/Formats/Tiff/README.md +++ b/src/ImageSharp/Formats/Tiff/README.md @@ -25,11 +25,6 @@ ## Implementation Status -### Know issue which need to be fixed - -Decoder: -- Decoding compressed images with HorizontalPrediction: Works for deflate, but not for lzw (maybe an issue with lzw itself?). - ### Deviations from the TIFF spec (to be fixed) - Decoder diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderMetadataCreator.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderMetadataCreator.cs index 865c1eeba..db0b04bd0 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderMetadataCreator.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderMetadataCreator.cs @@ -47,7 +47,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { if (tiffMetadata.XmpProfile == null) { - IExifValue val = frame.ExifProfile.GetValue(ExifTag.XMP); + IExifValue val = frame.ExifProfile.GetValue(ExifTag.XMP); if (val != null) { tiffMetadata.XmpProfile = val.Value; @@ -56,7 +56,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff if (coreMetadata.IptcProfile == null) { - IExifValue val = frame.ExifProfile.GetValue(ExifTag.IPTC); + IExifValue val = frame.ExifProfile.GetValue(ExifTag.IPTC); if (val != null) { coreMetadata.IptcProfile = new IptcProfile(val.Value); @@ -65,7 +65,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff if (coreMetadata.IccProfile == null) { - IExifValue val = frame.ExifProfile.GetValue(ExifTag.IccProfile); + IExifValue val = frame.ExifProfile.GetValue(ExifTag.IccProfile); if (val != null) { coreMetadata.IccProfile = new IccProfile(val.Value); diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs index e4c653209..2c632b36e 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs @@ -29,14 +29,14 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff TiffThrowHelper.ThrowNotSupported("The lower-order bits of the byte FillOrder is not supported."); } - if (entries.ExifProfile.GetValue(ExifTag.TileOffsets) != null) + if (entries.ExifProfile.GetValue(ExifTag.TileOffsets) != null) { - TiffThrowHelper.ThrowNotSupported("The Tile images is not supported."); + TiffThrowHelper.ThrowNotSupported("Tiled images are not supported."); } if (entries.Predictor == TiffPredictor.FloatingPoint) { - TiffThrowHelper.ThrowNotSupported("ImageSharp does not support FloatingPoint Predictor images."); + TiffThrowHelper.ThrowNotSupported("TIFF images with FloatingPoint horizontal predictor are not supported."); } if (entries.SampleFormat != null) diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs index 9a692a160..d206a2d23 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs @@ -50,11 +50,6 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// private readonly IQuantizer quantizer; - /// - /// Indicating whether to use horizontal prediction. This can improve the compression ratio with deflate compression. - /// - private readonly bool useHorizontalPredictor; - /// /// Sets the deflate compression level. /// @@ -71,7 +66,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff this.CompressionType = options.Compression; this.Mode = options.Mode; this.quantizer = options.Quantizer ?? KnownQuantizers.Octree; - this.useHorizontalPredictor = options.UseHorizontalPredictor; + this.UseHorizontalPredictor = options.UseHorizontalPredictor; this.compressionLevel = options.CompressionLevel; } @@ -90,7 +85,10 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// internal TiffEncodingMode Mode { get; private set; } - internal bool UseHorizontalPredictor => this.useHorizontalPredictor; + /// + /// Gets a value indicating whether to use horizontal prediction. This can improve the compression ratio with deflate compression. + /// + internal bool UseHorizontalPredictor { get; } /// /// Encodes the image to the specified stream from the . @@ -166,16 +164,16 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff switch (this.Mode) { case TiffEncodingMode.ColorPalette: - imageDataBytes = writer.WritePalettedRgb(image, this.quantizer, this.CompressionType, this.compressionLevel, this.useHorizontalPredictor, entriesCollector); + imageDataBytes = writer.WritePalettedRgb(image, this.quantizer, this.CompressionType, this.compressionLevel, this.UseHorizontalPredictor, entriesCollector); break; case TiffEncodingMode.Gray: - imageDataBytes = writer.WriteGray(image, this.CompressionType, this.compressionLevel, this.useHorizontalPredictor); + imageDataBytes = writer.WriteGray(image, this.CompressionType, this.compressionLevel, this.UseHorizontalPredictor); break; case TiffEncodingMode.BiColor: imageDataBytes = writer.WriteBiColor(image, this.CompressionType, this.compressionLevel); break; default: - imageDataBytes = writer.WriteRgb(image, this.CompressionType, this.compressionLevel, this.useHorizontalPredictor); + imageDataBytes = writer.WriteRgb(image, this.CompressionType, this.compressionLevel, this.UseHorizontalPredictor); break; } @@ -209,7 +207,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff writer.Write((ushort)entries.Count); - foreach (ExifValue entry in entries) + foreach (IExifValue entry in entries) { writer.Write((ushort)entry.Tag); writer.Write((ushort)entry.DataType); diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs index 9cdce21e2..f2e3d9faf 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.ExifProfile.GetValue(ExifTag.XResolution).Value + Value = frameMetadata.ExifProfile.GetValue(ExifTag.XResolution).Value }; var yResolution = new ExifRational(ExifTagValue.YResolution) { - Value = frameMetadata.ExifProfile.GetValue(ExifTag.YResolution).Value + Value = frameMetadata.ExifProfile.GetValue(ExifTag.YResolution).Value }; var resolutionUnit = new ExifShort(ExifTagValue.ResolutionUnit) { - Value = frameMetadata.ExifProfile.GetValue(ExifTag.ResolutionUnit).Value + Value = frameMetadata.ExifProfile.GetValue(ExifTag.ResolutionUnit).Value }; this.collector.AddInternal(xResolution); diff --git a/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs b/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs index e89e337b7..78b42905e 100644 --- a/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs +++ b/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs @@ -39,23 +39,27 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff internal set => this.frameTags = value; } - /// Gets a general indication of the kind of data contained in this subfile. + /// + /// 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 => (TiffNewSubfileType?)this.ExifProfile.GetValue(ExifTag.SubfileType)?.Value ?? 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. + /// + /// 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 => (TiffSubfileType?)this.ExifProfile.GetValue(ExifTag.OldSubfileType)?.Value; + 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.ExifProfile.GetValue(ExifTag.ImageWidth).Value; + public Number Width => this.ExifProfile.GetValue(ExifTag.ImageWidth).Value; /// /// Gets the number of rows of pixels in the image. /// - public Number Height => this.ExifProfile.GetValue(ExifTag.ImageLength).Value; + public Number Height => this.ExifProfile.GetValue(ExifTag.ImageLength).Value; /// /// Gets the number of bits per component. @@ -64,7 +68,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff { get { - var bits = this.ExifProfile.GetValue(ExifTag.BitsPerSample)?.Value; + var bits = this.ExifProfile.GetValue(ExifTag.BitsPerSample)?.Value; if (bits == null) { if (this.PhotometricInterpretation == TiffPhotometricInterpretation.WhiteIsZero @@ -96,19 +100,21 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff } } - /// Gets the compression scheme used on the image data. + /// + /// Gets the compression scheme used on the image data. + /// /// The compression scheme used on the image data. - public TiffCompression Compression => (TiffCompression)this.ExifProfile.GetValue(ExifTag.Compression).Value; + public TiffCompression Compression => (TiffCompression)this.ExifProfile.GetValue(ExifTag.Compression).Value; /// /// Gets the color space of the image data. /// - public TiffPhotometricInterpretation PhotometricInterpretation => (TiffPhotometricInterpretation)this.ExifProfile.GetValue(ExifTag.PhotometricInterpretation).Value; + public TiffPhotometricInterpretation PhotometricInterpretation => (TiffPhotometricInterpretation)this.ExifProfile.GetValue(ExifTag.PhotometricInterpretation).Value; /// /// Gets the logical order of bits within a byte. /// - internal TiffFillOrder FillOrder => (TiffFillOrder?)this.ExifProfile.GetValue(ExifTag.FillOrder)?.Value ?? 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. @@ -137,25 +143,29 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff set => this.ExifProfile.SetValue(ExifTag.Model, value); } - /// Gets for each strip, the byte offset of that strip.. - public Number[] StripOffsets => this.ExifProfile.GetValue(ExifTag.StripOffsets).Value; + /// + /// Gets for each strip, the byte offset of that strip. + /// + public Number[] StripOffsets => this.ExifProfile.GetValue(ExifTag.StripOffsets).Value; /// /// Gets the number of components per pixel. /// - public ushort SamplesPerPixel => this.ExifProfile.GetValue(ExifTag.SamplesPerPixel).Value; + public ushort SamplesPerPixel => this.ExifProfile.GetValue(ExifTag.SamplesPerPixel).Value; /// /// Gets the number of rows per strip. /// - public Number RowsPerStrip => this.ExifProfile.GetValue(ExifTag.RowsPerStrip).Value; + 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.ExifProfile.GetValue(ExifTag.StripByteCounts).Value; + public Number[] StripByteCounts => this.ExifProfile.GetValue(ExifTag.StripByteCounts).Value; - /// Gets the resolution of the image in x- direction. + /// + /// Gets the resolution of the image in x- direction. + /// /// The density of the image in x- direction. public double? HorizontalResolution => this.GetResolution(ExifTag.XResolution); @@ -168,7 +178,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// /// Gets how the components of each pixel are stored. /// - public TiffPlanarConfiguration PlanarConfiguration => (TiffPlanarConfiguration?)this.ExifProfile.GetValue(ExifTag.PlanarConfiguration)?.Value ?? DefaultPlanarConfiguration; + public TiffPlanarConfiguration PlanarConfiguration => (TiffPlanarConfiguration?)this.ExifProfile.GetValue(ExifTag.PlanarConfiguration)?.Value ?? DefaultPlanarConfiguration; /// /// Gets the unit of measurement for XResolution and YResolution. @@ -214,12 +224,12 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// /// Gets a color map for palette color images. /// - public ushort[] ColorMap => this.ExifProfile.GetValue(ExifTag.ColorMap)?.Value; + public ushort[] ColorMap => this.ExifProfile.GetValue(ExifTag.ColorMap)?.Value; /// /// Gets the description of extra components. /// - public ushort[] ExtraSamples => this.ExifProfile.GetValue(ExifTag.ExtraSamples)?.Value; + public ushort[] ExtraSamples => this.ExifProfile.GetValue(ExifTag.ExtraSamples)?.Value; /// /// Gets or sets the copyright notice. @@ -233,13 +243,13 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// /// Gets a mathematical operator that is applied to the image data before an encoding scheme is applied. /// - public TiffPredictor Predictor => (TiffPredictor?)this.ExifProfile.GetValue(ExifTag.Predictor)?.Value ?? 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.ExifProfile.GetValue(ExifTag.SampleFormat)?.Value?.Select(a => (TiffSampleFormat)a).ToArray(); + public TiffSampleFormat[] SampleFormat => this.ExifProfile.GetValue(ExifTag.SampleFormat)?.Value?.Select(a => (TiffSampleFormat)a).ToArray(); /// /// Clears the metadata. diff --git a/src/ImageSharp/Formats/Tiff/TiffFrameMetadataResolutionExtensions.cs b/src/ImageSharp/Formats/Tiff/TiffFrameMetadataResolutionExtensions.cs index 5a7ad1ac9..86a128ca3 100644 --- a/src/ImageSharp/Formats/Tiff/TiffFrameMetadataResolutionExtensions.cs +++ b/src/ImageSharp/Formats/Tiff/TiffFrameMetadataResolutionExtensions.cs @@ -37,14 +37,14 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff public static PixelResolutionUnit GetResolutionUnit(this TiffFrameMetadata meta) { - ushort res = meta.ExifProfile.GetValue(ExifTag.ResolutionUnit)?.Value ?? 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) { - IExifValue resolution = meta.ExifProfile.GetValue(tag); + IExifValue resolution = meta.ExifProfile.GetValue(tag); if (resolution == null) { return null; @@ -73,27 +73,25 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff meta.ExifProfile.RemoveValue(tag); return; } - else - { - double res = value.Value; - switch (meta.ResolutionUnit) - { - case PixelResolutionUnit.AspectRatio: - res = 0; - break; - case PixelResolutionUnit.PixelsPerCentimeter: - res = UnitConverter.InchToCm(res); - break; - case PixelResolutionUnit.PixelsPerMeter: - res = UnitConverter.InchToMeter(res); - break; - case PixelResolutionUnit.PixelsPerInch: - default: - break; - } - meta.ExifProfile.SetValue(tag, new Rational(res)); + double res = value.Value; + switch (meta.ResolutionUnit) + { + case PixelResolutionUnit.AspectRatio: + res = 0; + break; + case PixelResolutionUnit.PixelsPerCentimeter: + res = UnitConverter.InchToCm(res); + break; + case PixelResolutionUnit.PixelsPerMeter: + res = UnitConverter.InchToMeter(res); + break; + case PixelResolutionUnit.PixelsPerInch: + default: + break; } + + meta.ExifProfile.SetValue(tag, new Rational(res)); } } } diff --git a/src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs b/src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs index 3709bca04..d853836b5 100644 --- a/src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs +++ b/src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. using System; -using System.IO; using System.Runtime.CompilerServices; namespace SixLabors.ImageSharp.Formats.Experimental.Tiff @@ -17,40 +16,16 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// /// The error message for the exception. [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowImageFormatException(string errorMessage) - => throw new ImageFormatException(errorMessage); + public static void ThrowImageFormatException(string errorMessage) => throw new ImageFormatException(errorMessage); [MethodImpl(InliningOptions.ColdPath)] - public static Exception TagNotFound(string tagName) - => new ArgumentException("Required tag is not found.", tagName); + public static Exception NotSupportedCompression(string compressionType) => throw new NotSupportedException($"Not supported compression: {compressionType}"); [MethodImpl(InliningOptions.ColdPath)] - public static void ThrowTagNotFound(string tagName) - => throw TagNotFound(tagName); + public static Exception InvalidColorType(string colorType) => throw new NotSupportedException($"Invalid color type: {colorType}"); [MethodImpl(InliningOptions.ColdPath)] - public static void ThrowBadZlibHeader(int cmf) => throw new ImageFormatException($"Bad compression method for ZLIB header: cmf={cmf}"); - - [MethodImpl(InliningOptions.ColdPath)] - public static Exception NotSupportedCompression(string compressionType) => new NotSupportedException("Not supported compression: " + compressionType); - - [MethodImpl(InliningOptions.ColdPath)] - public static void ThrowNotSupportedCompression(string compressionType) => throw NotSupportedCompression(compressionType); - - [MethodImpl(InliningOptions.ColdPath)] - public static Exception InvalidColorType(string colorType) => new NotSupportedException("Invalid color type: " + colorType); - - [MethodImpl(InliningOptions.ColdPath)] - public static Exception InvalidHeader() => new ImageFormatException("Invalid TIFF file header."); - - [MethodImpl(InliningOptions.ColdPath)] - public static void ThrowInvalidHeader() => throw InvalidHeader(); - - [MethodImpl(InliningOptions.ColdPath)] - public static void ThrowOutOfRange(string structure) => throw new InvalidDataException($"Out of range of {structure} structure."); - - [MethodImpl(InliningOptions.ColdPath)] - public static void ThrowBadStringEntry() => throw new ImageFormatException("The retrieved string is not null terminated."); + public static Exception ThrowInvalidHeader() => throw new ImageFormatException("Invalid TIFF file header."); [MethodImpl(InliningOptions.ColdPath)] public static void ThrowNotSupported(string message) => throw new NotSupportedException(message); diff --git a/src/ImageSharp/Formats/Tiff/Utils/TiffWriter.cs b/src/ImageSharp/Formats/Tiff/Utils/TiffWriter.cs index f8f5f8ab8..ebfd119f4 100644 --- a/src/ImageSharp/Formats/Tiff/Utils/TiffWriter.cs +++ b/src/ImageSharp/Formats/Tiff/Utils/TiffWriter.cs @@ -69,19 +69,13 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Utils /// Writes an array of bytes to the current stream. /// /// The bytes to write. - public void Write(byte[] value) - { - this.output.Write(value, 0, value.Length); - } + public void Write(byte[] value) => this.output.Write(value, 0, value.Length); /// /// Writes a byte to the current stream. /// /// The byte to write. - public void Write(byte value) - { - this.output.Write(new byte[] { value }, 0, 1); - } + public void Write(byte value) => this.output.Write(new[] { value }, 0, 1); /// /// Writes a two-byte unsigned integer to the current stream. @@ -258,7 +252,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Utils where TPixel : unmanaged, IPixel { // Worst case is that the actual compressed data is larger then the input data. In this case we need 1 additional byte per 127 bytes. - int additionalBytes = ((image.Width * 3) / 127) + 1; + int additionalBytes = (image.Width * 3 / 127) + 1; using IManagedByteBuffer compressedRow = this.memoryAllocator.AllocateManagedByteBuffer((image.Width * 3) + additionalBytes, AllocationOptions.Clean); Span compressedRowSpan = compressedRow.GetSpan(); int bytesWritten = 0; @@ -464,7 +458,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Utils where TPixel : unmanaged, IPixel { // Worst case is that the actual compressed data is larger then the input data. In this case we need 1 additional byte per 127 bytes. - int additionalBytes = ((image.Width * 3) / 127) + 1; + int additionalBytes = (image.Width * 3 / 127) + 1; using IManagedByteBuffer compressedRow = this.memoryAllocator.AllocateManagedByteBuffer((image.Width * 3) + additionalBytes, AllocationOptions.Clean); Span compressedRowSpan = compressedRow.GetSpan(); @@ -806,9 +800,6 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Utils /// /// Disposes instance, ensuring any unwritten data is flushed. /// - public void Dispose() - { - this.output.Flush(); - } + public void Dispose() => this.output.Flush(); } }