From 508844ad60c3f7fff94116bcc10463070fbd830b Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Wed, 19 May 2021 20:35:55 +0200 Subject: [PATCH] Fix failing tests --- .../Formats/Tiff/TiffEncoderCore.cs | 21 +++++++++++++++++-- .../Formats/Tiff/TiffMetadataTests.cs | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs index 055def11d5..878bd99a9a 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs @@ -120,7 +120,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff // TODO: This isn't correct. // We're overwriting explicit BPP based upon the Mode. It should be the other way around. // BPP should also be nullable and based upon the current TPixel if not set. - this.SetBitsPerPixel(rootFrameBitsPerPixel); + this.SetBitsPerPixel(rootFrameBitsPerPixel, photometricInterpretation); this.SetMode(photometricInterpretation); this.SetPhotometricInterpretation(); @@ -286,6 +286,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff if (this.Mode == TiffEncodingMode.Default) { this.Mode = TiffEncodingMode.BiColor; + this.BitsPerPixel = TiffBitsPerPixel.Bit1; return; } @@ -321,9 +322,25 @@ namespace SixLabors.ImageSharp.Formats.Tiff } } - private void SetBitsPerPixel(TiffBitsPerPixel? rootFrameBitsPerPixel) + private void SetBitsPerPixel(TiffBitsPerPixel? rootFrameBitsPerPixel, TiffPhotometricInterpretation photometricInterpretation) { this.BitsPerPixel ??= rootFrameBitsPerPixel; + + if (photometricInterpretation == TiffPhotometricInterpretation.PaletteColor) + { + if (this.BitsPerPixel != TiffBitsPerPixel.Bit8 && this.BitsPerPixel != TiffBitsPerPixel.Bit4) + { + this.BitsPerPixel = TiffBitsPerPixel.Bit8; + } + + return; + } + + if (this.BitsPerPixel.HasValue) + { + return; + } + switch (this.Mode) { case TiffEncodingMode.BiColor: diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs index 1e96e64fdd..228eec078e 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs @@ -236,7 +236,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff ExifProfile encodedImageExifProfile = rootFrameEncodedImage.Metadata.ExifProfile; byte[] encodedImageXmpProfile = rootFrameEncodedImage.Metadata.XmpProfile; - Assert.Equal(TiffBitsPerPixel.Bit24, tiffMetaDataEncodedRootFrame.BitsPerPixel); + Assert.Equal(TiffBitsPerPixel.Bit4, tiffMetaDataEncodedRootFrame.BitsPerPixel); Assert.Equal(TiffCompression.None, (TiffCompression)encodedImageExifProfile.GetValue(ExifTag.Compression).Value); Assert.Equal(inputMetaData.HorizontalResolution, encodedImageMetaData.HorizontalResolution);