Skip to content

Commit cc4c0b1

Browse files
Merge pull request #2590 from SixLabors/bp/Issue2587
Explicitly set BitsPerPixel and BitsPerSample to 1 for bicolor compressed images
2 parents c5cc7a7 + b46ab5a commit cc4c0b1

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public static bool VerifyAndParse(this TiffDecoderCore options, ExifProfile exif
139139
options.OldJpegCompressionStartOfImageMarker = jpegInterchangeFormatValue.Value;
140140
}
141141

142-
options.ParseColorType(exifProfile);
143142
options.ParseCompression(frameMetadata.Compression, exifProfile);
143+
options.ParseColorType(exifProfile);
144144

145145
bool isTiled = VerifyRequiredFieldsArePresent(exifProfile, frameMetadata, options.PlanarConfiguration);
146146

@@ -194,7 +194,9 @@ private static bool VerifyRequiredFieldsArePresent(ExifProfile exifProfile, Tiff
194194
}
195195
}
196196

197-
if (frameMetadata.BitsPerPixel == null)
197+
// For BiColor compressed images, the BitsPerPixel value will be set explicitly to 1, so we don't throw in those cases.
198+
// See: https://github.com/SixLabors/ImageSharp/issues/2587
199+
if (frameMetadata.BitsPerPixel == null && !IsBiColorCompression(frameMetadata.Compression))
198200
{
199201
TiffThrowHelper.ThrowNotSupported("The TIFF BitsPerSample entry is missing which is required to decode the image!");
200202
}
@@ -570,6 +572,11 @@ private static void ParseCompression(this TiffDecoderCore options, TiffCompressi
570572
options.FaxCompressionOptions = FaxCompressionOptions.None;
571573
}
572574

575+
// Some encoders do not set the BitsPerSample correctly, so we set those values here to the required values:
576+
// https://github.com/SixLabors/ImageSharp/issues/2587
577+
options.BitsPerSample = new TiffBitsPerSample(1, 0, 0);
578+
options.BitsPerPixel = 1;
579+
573580
break;
574581
}
575582

@@ -585,12 +592,18 @@ private static void ParseCompression(this TiffDecoderCore options, TiffCompressi
585592
options.FaxCompressionOptions = FaxCompressionOptions.None;
586593
}
587594

595+
options.BitsPerSample = new TiffBitsPerSample(1, 0, 0);
596+
options.BitsPerPixel = 1;
597+
588598
break;
589599
}
590600

591601
case TiffCompression.Ccitt1D:
592602
{
593603
options.CompressionType = TiffDecoderCompressionType.HuffmanRle;
604+
options.BitsPerSample = new TiffBitsPerSample(1, 0, 0);
605+
options.BitsPerPixel = 1;
606+
594607
break;
595608
}
596609

@@ -645,4 +658,15 @@ private static void ParseCompression(this TiffDecoderCore options, TiffCompressi
645658
}
646659
}
647660
}
661+
662+
private static bool IsBiColorCompression(TiffCompression? compression)
663+
{
664+
if (compression is TiffCompression.Ccitt1D or TiffCompression.CcittGroup3Fax or
665+
TiffCompression.CcittGroup4Fax)
666+
{
667+
return true;
668+
}
669+
670+
return false;
671+
}
648672
}

tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,12 @@ public void TiffDecoder_CanDecode_Fax4CompressedWithStrips<TPixel>(TestImageProv
665665
public void TiffDecoder_CanDecode_TiledWithNonEqualWidthAndHeight<TPixel>(TestImageProvider<TPixel> provider)
666666
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
667667

668+
// https://github.com/SixLabors/ImageSharp/issues/2587
669+
[Theory]
670+
[WithFile(Issues2587, PixelTypes.Rgba32)]
671+
public void TiffDecoder_CanDecode_BiColorWithMissingBitsPerSample<TPixel>(TestImageProvider<TPixel> provider)
672+
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
673+
668674
[Theory]
669675
[WithFile(JpegCompressedGray0000539558, PixelTypes.Rgba32)]
670676
public void TiffDecoder_ThrowsException_WithCircular_IFD_Offsets<TPixel>(TestImageProvider<TPixel> provider)

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,7 @@ public static class Tiff
992992
public const string Issues2149 = "Tiff/Issues/Group4CompressionWithStrips.tiff";
993993
public const string Issues2255 = "Tiff/Issues/Issue2255.png";
994994
public const string Issues2435 = "Tiff/Issues/Issue2435.tiff";
995+
public const string Issues2587 = "Tiff/Issues/Issue2587.tiff";
995996
public const string JpegCompressedGray0000539558 = "Tiff/Issues/JpegCompressedGray-0000539558.tiff";
996997
public const string Tiled0000023664 = "Tiff/Issues/tiled-0000023664.tiff";
997998

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:b5a245e4313bcf942d9a8ab7108946ddcadcf45d898b8a8986fc42a6e8c64dc2
3+
size 87746

0 commit comments

Comments
 (0)