Skip to content

Commit e682888

Browse files
Tiff: Tiff format should not load thumbnail into collection
1 parent a988a3a commit e682888

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

Binary file not shown.

IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/AnyBitmapFunctionality.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,5 +853,18 @@ public void ExtractAlphaData_WithUnsupportedBppImage_ThrowsException()
853853
Assert.Equal($"Extracting alpha data is not supported for {bitmap.BitsPerPixel} bpp images.", exception.Message);
854854
}
855855

856+
[FactWithAutomaticDisplayName]
857+
public void LoadImage_TiffImage_ShouldLoadWithoutThumbnail()
858+
{
859+
// Arrange
860+
string imagePath = GetRelativeFilePath("example.tif");
861+
862+
// Act
863+
var bitmap = new AnyBitmap(imagePath);
864+
865+
// Assert
866+
bitmap.FrameCount.Should().Be(1);
867+
}
868+
856869
}
857870
}

IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2208,13 +2208,18 @@ private void OpenTiffToImageSharp(ReadOnlySpan<byte> bytes)
22082208
using MemoryStream tiffStream = new(bytes.ToArray());
22092209

22102210
// open a TIFF stored in the stream
2211-
using (var tif = Tiff.ClientOpen("in-memory", "r", tiffStream, new TiffStream()))
2211+
using (Tiff tif = Tiff.ClientOpen("in-memory", "r", tiffStream, new TiffStream()))
22122212
{
22132213
short num = tif.NumberOfDirectories();
22142214
for (short i = 0; i < num; i++)
22152215
{
22162216
_ = tif.SetDirectory(i);
22172217

2218+
if (IsThumbnail(tif))
2219+
{
2220+
continue;
2221+
}
2222+
22182223
var (width, height) = SetWidthHeight(tif, i, ref imageWidth, ref imageHeight);
22192224

22202225
// Read the image into the memory buffer
@@ -2281,6 +2286,22 @@ private void OpenTiffToImageSharp(ReadOnlySpan<byte> bytes)
22812286
}
22822287
}
22832288

2289+
/// <summary>
2290+
/// Determines if a TIFF frame contains a thumbnail.
2291+
/// </summary>
2292+
/// <param name="tif">The <see cref="Tiff"/> which set number of directory to analyze.</param>
2293+
/// <returns>True if the frame contains a thumbnail, otherwise false.</returns>
2294+
private bool IsThumbnail(Tiff tif)
2295+
{
2296+
FieldValue[] subFileTypeFieldValue = tif.GetField(TiffTag.SUBFILETYPE);
2297+
2298+
// Current thumbnail identification relies on the SUBFILETYPE tag with a value of FileType.REDUCEDIMAGE.
2299+
// This may need refinement in the future to include additional checks
2300+
// (e.g., FileType.COMPRESSION is NONE, Image Dimensions).
2301+
return subFileTypeFieldValue != null && subFileTypeFieldValue.Length > 0
2302+
&& (FileType)subFileTypeFieldValue[0].Value == FileType.REDUCEDIMAGE;
2303+
}
2304+
22842305
private ReadOnlySpan<byte> PrepareByteArray(Image<Rgba32> bmp, int[] raster, int width, int height)
22852306
{
22862307
byte[] bits = new byte[GetStride(bmp) * height];

0 commit comments

Comments
 (0)