Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly set BitsPerPixel and BitsPerSample to 1 for bicolor compressed images #2590

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public static bool VerifyAndParse(this TiffDecoderCore options, ExifProfile exif
options.OldJpegCompressionStartOfImageMarker = jpegInterchangeFormatValue.Value;
}

options.ParseColorType(exifProfile);
options.ParseCompression(frameMetadata.Compression, exifProfile);
options.ParseColorType(exifProfile);

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

Expand Down Expand Up @@ -194,7 +194,9 @@ private static bool VerifyRequiredFieldsArePresent(ExifProfile exifProfile, Tiff
}
}

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

// Some encoders do not set the BitsPerSample correctly, so we set those values here to the required values:
// https://github.com/SixLabors/ImageSharp/issues/2587
options.BitsPerSample = new TiffBitsPerSample(1, 0, 0);
options.BitsPerPixel = 1;

break;
}

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

options.BitsPerSample = new TiffBitsPerSample(1, 0, 0);
options.BitsPerPixel = 1;

break;
}

case TiffCompression.Ccitt1D:
{
options.CompressionType = TiffDecoderCompressionType.HuffmanRle;
options.BitsPerSample = new TiffBitsPerSample(1, 0, 0);
options.BitsPerPixel = 1;

break;
}

Expand Down Expand Up @@ -645,4 +658,15 @@ private static void ParseCompression(this TiffDecoderCore options, TiffCompressi
}
}
}

private static bool IsBiColorCompression(TiffCompression? compression)
{
if (compression is TiffCompression.Ccitt1D or TiffCompression.CcittGroup3Fax or
TiffCompression.CcittGroup4Fax)
{
return true;
}

return false;
}
}
6 changes: 6 additions & 0 deletions tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,12 @@ public void TiffDecoder_CanDecode_Fax4CompressedWithStrips<TPixel>(TestImageProv
public void TiffDecoder_CanDecode_TiledWithNonEqualWidthAndHeight<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);

// https://github.com/SixLabors/ImageSharp/issues/2587
[Theory]
[WithFile(Issues2587, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_BiColorWithMissingBitsPerSample<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);

[Theory]
[WithFile(JpegCompressedGray0000539558, PixelTypes.Rgba32)]
public void TiffDecoder_ThrowsException_WithCircular_IFD_Offsets<TPixel>(TestImageProvider<TPixel> provider)
Expand Down
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ public static class Tiff
public const string Issues2149 = "Tiff/Issues/Group4CompressionWithStrips.tiff";
public const string Issues2255 = "Tiff/Issues/Issue2255.png";
public const string Issues2435 = "Tiff/Issues/Issue2435.tiff";
public const string Issues2587 = "Tiff/Issues/Issue2587.tiff";
public const string JpegCompressedGray0000539558 = "Tiff/Issues/JpegCompressedGray-0000539558.tiff";
public const string Tiled0000023664 = "Tiff/Issues/tiled-0000023664.tiff";

Expand Down
3 changes: 3 additions & 0 deletions tests/Images/Input/Tiff/Issues/Issue2587.tiff
Git LFS file not shown
Loading