Skip to content

Commit a87f5ef

Browse files
Merge pull request #2941 from SixLabors/js/v4-tiff-extra-samples
V4 TIFF : Allow additional and undefined extra samples
2 parents 9badd81 + 07cc9f7 commit a87f5ef

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,22 @@ internal static class TiffDecoderOptionsParser
2525
/// <returns>True, if the image uses tiles. Otherwise the images has strip's.</returns>
2626
public static bool VerifyAndParse(this TiffDecoderCore options, ExifProfile exifProfile, TiffFrameMetadata frameMetadata)
2727
{
28-
IExifValue extraSamplesExifValue = exifProfile.GetValueInternal(ExifTag.ExtraSamples);
29-
if (extraSamplesExifValue is not null)
28+
if (exifProfile.TryGetValue(ExifTag.ExtraSamples, out IExifValue<ushort[]> samples))
3029
{
31-
short[] extraSamples = (short[])extraSamplesExifValue.GetValue();
32-
if (extraSamples.Length != 1)
30+
// We only support a single sample pertaining to alpha data.
31+
// Other information is discarded.
32+
TiffExtraSampleType sampleType = (TiffExtraSampleType)samples.Value[0];
33+
if (sampleType is TiffExtraSampleType.CorelDrawUnassociatedAlphaData)
3334
{
34-
TiffThrowHelper.ThrowNotSupported("ExtraSamples is only supported with one extra sample for alpha data.");
35+
// According to libtiff, this CorelDRAW-specific value indicates unassociated alpha.
36+
// Patch required for compatibility with malformed CorelDRAW-generated TIFFs.
37+
// https://libtiff.gitlab.io/libtiff/releases/v3.9.0beta.html
38+
sampleType = TiffExtraSampleType.UnassociatedAlphaData;
3539
}
3640

37-
TiffExtraSampleType extraSamplesType = (TiffExtraSampleType)extraSamples[0];
38-
options.ExtraSamplesType = extraSamplesType;
39-
if (extraSamplesType is not (TiffExtraSampleType.UnassociatedAlphaData or TiffExtraSampleType.AssociatedAlphaData))
41+
if (sampleType is (TiffExtraSampleType.UnassociatedAlphaData or TiffExtraSampleType.AssociatedAlphaData))
4042
{
41-
TiffThrowHelper.ThrowNotSupported("Decoding Tiff images with ExtraSamples is not supported with UnspecifiedData.");
43+
options.ExtraSamplesType = sampleType;
4244
}
4345
}
4446

src/ImageSharp/Formats/Tiff/TiffExtraSampleType.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@ internal enum TiffExtraSampleType
2222
/// The extra data is unassociated alpha data is transparency information that logically exists independent of an image;
2323
/// it is commonly called a soft matte.
2424
/// </summary>
25-
UnassociatedAlphaData = 2
25+
UnassociatedAlphaData = 2,
26+
27+
/// <summary>
28+
/// A CorelDRAW-specific value observed in damaged files, indicating unassociated alpha.
29+
/// Not part of the official TIFF specification; patched in ImageSharp for compatibility.
30+
/// </summary>
31+
CorelDrawUnassociatedAlphaData = 999,
2632
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,4 +828,9 @@ public void TiffDecoder_Decode_Resize<TPixel>(TestImageProvider<TPixel> provider
828828
testOutputDetails: details,
829829
appendPixelTypeToFileName: false);
830830
}
831+
832+
[Theory]
833+
[WithFile(ExtraSamplesUnspecified, PixelTypes.Rgba32)]
834+
public void TiffDecoder_CanDecode_ExtraSamplesUnspecified<TPixel>(TestImageProvider<TPixel> provider)
835+
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
831836
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@ public static class Tiff
11361136
public const string Issues2679 = "Tiff/Issues/Issue2679.tiff";
11371137
public const string JpegCompressedGray0000539558 = "Tiff/Issues/JpegCompressedGray-0000539558.tiff";
11381138
public const string Tiled0000023664 = "Tiff/Issues/tiled-0000023664.tiff";
1139+
public const string ExtraSamplesUnspecified = "Tiff/Issues/ExtraSamplesUnspecified.tif";
11391140

11401141
public const string SmallRgbDeflate = "Tiff/rgb_small_deflate.tiff";
11411142
public const string SmallRgbLzw = "Tiff/rgb_small_lzw.tiff";
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:71d28f17d2d56481faa4d241fae882eae4f8303c70f85bc3759f6a0c2074979e
3+
size 1426558

0 commit comments

Comments
 (0)