Skip to content

Commit 53dc3e3

Browse files
Merge branch 'main' into better_handle_corrupt_png
2 parents 583a9c9 + 6cf09dc commit 53dc3e3

File tree

10 files changed

+72
-6
lines changed

10 files changed

+72
-6
lines changed

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -753,10 +753,7 @@ private void ProcessApplicationHeaderMarker(BufferedReadStream stream, int remai
753753
Span<byte> temp = stackalloc byte[2 * 16 * 4];
754754

755755
stream.Read(temp, 0, JFifMarker.Length);
756-
if (!JFifMarker.TryParse(temp, out this.jFif))
757-
{
758-
JpegThrowHelper.ThrowNotSupportedException("Unknown App0 Marker - Expected JFIF.");
759-
}
756+
_ = JFifMarker.TryParse(temp, out this.jFif);
760757

761758
remaining -= JFifMarker.Length;
762759

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using SixLabors.ImageSharp.Formats.Png.Filters;
1616
using SixLabors.ImageSharp.IO;
1717
using SixLabors.ImageSharp.Memory;
18+
using SixLabors.ImageSharp.Memory.Internals;
1819
using SixLabors.ImageSharp.Metadata;
1920
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
2021
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
@@ -1873,6 +1874,11 @@ private void SkipChunkDataAndCrc(in PngChunk chunk)
18731874
[MethodImpl(InliningOptions.ShortMethod)]
18741875
private IMemoryOwner<byte> ReadChunkData(int length)
18751876
{
1877+
if (length == 0)
1878+
{
1879+
return new BasicArrayBuffer<byte>(Array.Empty<byte>());
1880+
}
1881+
18761882
// We rent the buffer here to return it afterwards in Decode()
18771883
IMemoryOwner<byte> buffer = this.configuration.MemoryAllocator.Allocate<byte>(length, AllocationOptions.Clean);
18781884

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/Jpg/JpegDecoderTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,17 @@ public void Issue2478_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
326326
image.CompareToOriginal(provider);
327327
}
328328

329+
// https://github.com/SixLabors/ImageSharp/discussions/2564
330+
[Theory]
331+
[WithFile(TestImages.Jpeg.Issues.Issue2564, PixelTypes.Rgba32)]
332+
public void Issue2564_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
333+
where TPixel : unmanaged, IPixel<TPixel>
334+
{
335+
using Image<TPixel> image = provider.GetImage(JpegDecoder.Instance);
336+
image.DebugSave(provider);
337+
image.CompareToOriginal(provider);
338+
}
339+
329340
[Theory]
330341
[WithFile(TestImages.Jpeg.Issues.HangBadScan, PixelTypes.L8)]
331342
public void DecodeHang<TPixel>(TestImageProvider<TPixel> provider)

tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,4 +647,13 @@ static void RunTest(string providerDump, string nonContiguousBuffersStr)
647647
"Disco")
648648
.Dispose();
649649
}
650+
651+
[Fact]
652+
public void Binary_PrematureEof()
653+
{
654+
using EofHitCounter eofHitCounter = EofHitCounter.RunDecoder(TestImages.Png.Bad.FlagOfGermany0000016446);
655+
656+
Assert.True(eofHitCounter.EofHitCount <= 2);
657+
Assert.Equal(new Size(200, 120), eofHitCounter.Image.Size);
658+
}
650659
}

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ public static class Bad
180180
// Invalid color type.
181181
public const string ColorTypeOne = "Png/xc1n0g08.png";
182182
public const string ColorTypeNine = "Png/xc9n2c08.png";
183+
184+
public const string FlagOfGermany0000016446 = "Png/issues/flag_of_germany-0000016446.png";
183185
}
184186
}
185187

@@ -302,6 +304,7 @@ public static class Issues
302304
public const string Issue2334_NotEnoughBytesA = "Jpg/issues/issue-2334-a.jpg";
303305
public const string Issue2334_NotEnoughBytesB = "Jpg/issues/issue-2334-b.jpg";
304306
public const string Issue2478_JFXX = "Jpg/issues/issue-2478-jfxx.jpg";
307+
public const string Issue2564 = "Jpg/issues/issue-2564.jpg";
305308
public const string HangBadScan = "Jpg/issues/Hang_C438A851.jpg";
306309

307310
public static class Fuzz
@@ -992,6 +995,7 @@ public static class Tiff
992995
public const string Issues2149 = "Tiff/Issues/Group4CompressionWithStrips.tiff";
993996
public const string Issues2255 = "Tiff/Issues/Issue2255.png";
994997
public const string Issues2435 = "Tiff/Issues/Issue2435.tiff";
998+
public const string Issues2587 = "Tiff/Issues/Issue2587.tiff";
995999
public const string JpegCompressedGray0000539558 = "Tiff/Issues/JpegCompressedGray-0000539558.tiff";
9961000
public const string Tiled0000023664 = "Tiff/Issues/tiled-0000023664.tiff";
9971001

Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
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)