From 7152238573ffe3ecd712fe00231551cd05d0bb5e Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 31 Jan 2021 13:06:57 +0100 Subject: [PATCH] - Return default values, if compression, photometricInterpretation or rowsPerStrip is not present. - Throw Exception, if width or height is not set --- src/ImageSharp/Formats/{ => Tga}/README.md | 2 +- .../Formats/Tiff/Constants/TiffConstants.cs | 5 ++ src/ImageSharp/Formats/Tiff/README.md | 1 - .../Formats/Tiff/TiffDecoderCore.cs | 7 +- .../Formats/Tiff/TiffFrameMetadata.cs | 70 +++++++++++++++++-- 5 files changed, 72 insertions(+), 13 deletions(-) rename src/ImageSharp/Formats/{ => Tga}/README.md (78%) diff --git a/src/ImageSharp/Formats/README.md b/src/ImageSharp/Formats/Tga/README.md similarity index 78% rename from src/ImageSharp/Formats/README.md rename to src/ImageSharp/Formats/Tga/README.md index 4a2b401b1..219f111b9 100644 --- a/src/ImageSharp/Formats/README.md +++ b/src/ImageSharp/Formats/Tga/README.md @@ -1,4 +1,4 @@ -# Encoder/Decoder for true vision targa files +# Encoder/Decoder for true vision targa files Useful links for reference: diff --git a/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs b/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs index 68f121fc3..8a591fc83 100644 --- a/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs +++ b/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs @@ -35,6 +35,11 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants /// public const ushort HeaderMagicNumber = 42; + /// + /// RowsPerStrip default value, which is effectively infinity. + /// + public const int RowsPerStripInfinity = 2147483647; + /// /// Size (in bytes) of the TIFF file header. /// diff --git a/src/ImageSharp/Formats/Tiff/README.md b/src/ImageSharp/Formats/Tiff/README.md index fd88e8eb0..861cd9d9a 100644 --- a/src/ImageSharp/Formats/Tiff/README.md +++ b/src/ImageSharp/Formats/Tiff/README.md @@ -32,7 +32,6 @@ - NB: Need to handle this for both planar and chunky data - If the SampleFormat field is present and not 1 - fail gracefully if you cannot handle this - Compression=None should treat 16/32-BitsPerSample for all samples as SHORT/LONG (for byte order and padding rows) - - RowsPerStrip should default to 2^32-1 (effectively infinity) to store the image as a single strip - Check Planar format data - is this encoded as strips in order RGBRGBRGB or RRRGGGBBB? - Make sure we ignore any strips that are not needed for the image (if too many are present) diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs index 022f04e1c..9b7afe005 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs @@ -34,11 +34,6 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// private BufferedReadStream inputStream; - /// - /// RowsPerStrip default value, which is effectively infinity. - /// - private const int RowsPerStripInfinity = 2147483647; - /// /// Initializes a new instance of the class. /// @@ -287,7 +282,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff where TPixel : unmanaged, IPixel { // If the rowsPerStrip has the default value, which is effectively infinity. That is, the entire image is one strip. - if (rowsPerStrip == RowsPerStripInfinity) + if (rowsPerStrip == TiffConstants.RowsPerStripInfinity) { rowsPerStrip = frame.Height; } diff --git a/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs b/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs index 78b42905e..7d79f7a85 100644 --- a/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs +++ b/src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs @@ -54,12 +54,36 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// /// 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 + { + get + { + IExifValue width = this.ExifProfile.GetValue(ExifTag.ImageWidth); + if (width == null) + { + TiffThrowHelper.ThrowImageFormatException("The TIFF image is missing the ImageWidth"); + } + + return width.Value; + } + } /// /// Gets the number of rows of pixels in the image. /// - public Number Height => this.ExifProfile.GetValue(ExifTag.ImageLength).Value; + public Number Height + { + get + { + IExifValue height = this.ExifProfile.GetValue(ExifTag.ImageLength); + if (height == null) + { + TiffThrowHelper.ThrowImageFormatException("The TIFF image is missing the ImageLength"); + } + + return height.Value; + } + } /// /// Gets the number of bits per component. @@ -104,12 +128,36 @@ 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 => (TiffCompression)this.ExifProfile.GetValue(ExifTag.Compression).Value; + public TiffCompression Compression + { + get + { + IExifValue compression = this.ExifProfile.GetValue(ExifTag.Compression); + if (compression == null) + { + return TiffCompression.None; + } + + return (TiffCompression)compression.Value; + } + } /// /// Gets the color space of the image data. /// - public TiffPhotometricInterpretation PhotometricInterpretation => (TiffPhotometricInterpretation)this.ExifProfile.GetValue(ExifTag.PhotometricInterpretation).Value; + public TiffPhotometricInterpretation PhotometricInterpretation + { + get + { + IExifValue photometricInterpretation = this.ExifProfile.GetValue(ExifTag.PhotometricInterpretation); + if (photometricInterpretation == null) + { + return TiffPhotometricInterpretation.WhiteIsZero; + } + + return (TiffPhotometricInterpretation)photometricInterpretation.Value; + } + } /// /// Gets the logical order of bits within a byte. @@ -156,7 +204,19 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff /// /// Gets the number of rows per strip. /// - public Number RowsPerStrip => this.ExifProfile.GetValue(ExifTag.RowsPerStrip).Value; + public Number RowsPerStrip + { + get + { + IExifValue rowsPerStrip = this.ExifProfile.GetValue(ExifTag.RowsPerStrip); + if (rowsPerStrip == null) + { + return TiffConstants.RowsPerStripInfinity; + } + + return rowsPerStrip.Value; + } + } /// /// Gets for each strip, the number of bytes in the strip after compression.