From b03165cd2c588a2d636fd77ebacfe12a0fb7ab4f Mon Sep 17 00:00:00 2001 From: Andrew Wilkinson Date: Thu, 25 May 2017 11:09:17 +0100 Subject: [PATCH] Add helper function when reading entries with defaults --- .../Formats/Tiff/TiffDecoderCore.cs | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs index a2d1f37c8..f3a55412b 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs @@ -208,11 +208,7 @@ namespace ImageSharp.Formats Image image = new Image(this.configuration, width, height); - TiffResolutionUnit resolutionUnit = TiffResolutionUnit.Inch; - if (ifd.TryGetIfdEntry(TiffTags.ResolutionUnit, out TiffIfdEntry resolutionUnitEntry)) - { - resolutionUnit = (TiffResolutionUnit)this.ReadUnsignedInteger(ref resolutionUnitEntry); - } + TiffResolutionUnit resolutionUnit = (TiffResolutionUnit)this.ReadUnsignedInteger(ifd, TiffTags.ResolutionUnit, (uint)TiffResolutionUnit.Inch); if (resolutionUnit != TiffResolutionUnit.None) { @@ -252,12 +248,7 @@ namespace ImageSharp.Formats /// The IFD to read the image format information for. public void ReadImageFormat(TiffIfd ifd) { - TiffCompression compression = TiffCompression.None; - - if (ifd.TryGetIfdEntry(TiffTags.Compression, out TiffIfdEntry compressionEntry)) - { - compression = (TiffCompression)this.ReadUnsignedInteger(ref compressionEntry); - } + TiffCompression compression = (TiffCompression)this.ReadUnsignedInteger(ifd, TiffTags.Compression, (uint)TiffCompression.None); switch (compression) { @@ -286,14 +277,7 @@ namespace ImageSharp.Formats } } - if (ifd.TryGetIfdEntry(TiffTags.PlanarConfiguration, out TiffIfdEntry planarConfigurationEntry)) - { - this.PlanarConfiguration = (TiffPlanarConfiguration)this.ReadUnsignedInteger(ref planarConfigurationEntry); - } - else - { - this.PlanarConfiguration = TiffPlanarConfiguration.Chunky; - } + this.PlanarConfiguration = (TiffPlanarConfiguration)this.ReadUnsignedInteger(ifd, TiffTags.PlanarConfiguration, (uint)TiffPlanarConfiguration.Chunky); TiffPhotometricInterpretation photometricInterpretation; @@ -653,6 +637,29 @@ namespace ImageSharp.Formats } } + /// + /// Reads the data for a specified tag of a as an unsigned integer value. + /// + /// The to read from. + /// The tag ID to search for. + /// The default value if the entry is missing + /// The data. + /// + /// Thrown if the data-type specified by the file cannot be converted to a , or if + /// there is an array of items. + /// + public uint ReadUnsignedInteger(TiffIfd ifd, ushort tag, uint defaultValue) + { + if (ifd.TryGetIfdEntry(tag, out TiffIfdEntry entry)) + { + return this.ReadUnsignedInteger(ref entry); + } + else + { + return defaultValue; + } + } + /// /// Reads the data from a as a signed integer value. /// @@ -1181,7 +1188,7 @@ namespace ImageSharp.Formats for (int planeIndex = 0; planeIndex < stripsPerPixel; planeIndex++) { - int stripIndex = i * stripsPerPixel + planeIndex; + int stripIndex = (i * stripsPerPixel) + planeIndex; this.DecompressImageBlock(stripOffsets[stripIndex], stripByteCounts[stripIndex], stripBytes[planeIndex]); }