Skip to content

Commit d48a6dd

Browse files
fix for loading multiple sub ifds
Fixes #2857
1 parent e08651b commit d48a6dd

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,19 @@ protected void ReadSubIfd(List<IExifValue> values)
189189
{
190190
if (this.subIfds is not null)
191191
{
192-
foreach (ulong subIfdOffset in this.subIfds)
192+
do
193193
{
194-
this.ReadValues(values, (uint)subIfdOffset);
194+
int sz = this.subIfds.Count;
195+
Span<ulong> buf = sz <= 256 ? stackalloc ulong[sz] : new ulong[sz];
196+
197+
this.subIfds.CopyTo(buf);
198+
this.subIfds.Clear();
199+
foreach (ulong subIfdOffset in buf)
200+
{
201+
this.ReadValues(values, (uint)subIfdOffset);
202+
}
195203
}
204+
while (this.subIfds.Count > 0);
196205
}
197206
}
198207

tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using SixLabors.ImageSharp.PixelFormats;
1111
using SixLabors.ImageSharp.Processing;
1212
using SixLabors.ImageSharp.Tests.TestUtilities;
13+
using static SixLabors.ImageSharp.Metadata.Profiles.Exif.EncodedString;
1314

1415
// ReSharper disable InconsistentNaming
1516
namespace SixLabors.ImageSharp.Tests.Formats.Jpg;
@@ -439,9 +440,8 @@ public void JpegDecoder_DecodeMetadataComment<TPixel>(TestImageProvider<TPixel>
439440
Assert.Equal(expectedComment, metadata.Comments.ElementAtOrDefault(0).ToString());
440441
image.DebugSave(provider);
441442
image.CompareToOriginal(provider);
442-
443443
}
444-
444+
445445
// https://github.com/SixLabors/ImageSharp/issues/2758
446446
[Theory]
447447
[WithFile(TestImages.Jpeg.Issues.Issue2758, PixelTypes.L8)]
@@ -468,6 +468,47 @@ public void Issue2758_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
468468
image.Save(ms, new JpegEncoder());
469469
}
470470

471+
// https://github.com/SixLabors/ImageSharp/issues/2857
472+
[Theory]
473+
[WithFile(TestImages.Jpeg.Issues.Issue2857, PixelTypes.Rgb24)]
474+
public void Issue2857_SubSubIfds<TPixel>(TestImageProvider<TPixel> provider)
475+
where TPixel : unmanaged, IPixel<TPixel>
476+
{
477+
using Image<TPixel> image = provider.GetImage(JpegDecoder.Instance);
478+
479+
Assert.Equal(5616, image.Width);
480+
Assert.Equal(3744, image.Height);
481+
482+
JpegMetadata meta = image.Metadata.GetJpegMetadata();
483+
Assert.Equal(92, meta.LuminanceQuality);
484+
Assert.Equal(93, meta.ChrominanceQuality);
485+
486+
ExifProfile exifProfile = image.Metadata.ExifProfile;
487+
Assert.NotNull(exifProfile);
488+
489+
using MemoryStream ms = new();
490+
bool hasThumbnail = exifProfile.TryCreateThumbnail(out _);
491+
Assert.False(hasThumbnail);
492+
493+
Assert.Equal("BilderBox - Erwin Wodicka / [email protected]", exifProfile.GetValue(ExifTag.Copyright).Value);
494+
Assert.Equal("Adobe Photoshop CS3 Windows", exifProfile.GetValue(ExifTag.Software).Value);
495+
496+
Assert.Equal("Carers; seniors; caregiver; senior care; retirement home; hands; old; elderly; elderly caregiver; elder care; elderly care; geriatric care; nursing home; age; old age care; outpatient; needy; health care; home nurse; home care; sick; retirement; medical; mobile; the elderly; nursing department; nursing treatment; nursing; care services; nursing services; nursing care; nursing allowance; nursing homes; home nursing; care category; nursing class; care; nursing shortage; nursing patient care staff\0", exifProfile.GetValue(ExifTag.XPKeywords).Value);
497+
498+
Assert.Equal(
499+
new EncodedString(CharacterCode.ASCII, "StockSubmitter|Miscellaneous||Miscellaneous$|00|0000330000000110000000000000000|22$@NA_1005010.460@[email protected]$$@$@26$$@$@$@$@205$@$@$@$@$@$@$@$@$@43$@$@$@[email protected]$$@90$$@22$@$@$@$@$@$@$|||"),
500+
exifProfile.GetValue(ExifTag.UserComment).Value);
501+
502+
image.Mutate(x => x.Crop(new(0, 0, 100, 100)));
503+
504+
image.Save(ms, new JpegEncoder());
505+
506+
foreach (IExifValue val in image.Metadata.ExifProfile.Values)
507+
{
508+
this.Output.WriteLine($"{val.Tag}={val.GetValue()}");
509+
}
510+
}
511+
471512
private static void VerifyEncodedStrings(ExifProfile exif)
472513
{
473514
Assert.NotNull(exif);

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ public static class Issues
325325
public const string Issue2067_CommentMarker = "Jpg/issues/issue-2067-comment.jpg";
326326
public const string Issue2638 = "Jpg/issues/Issue2638.jpg";
327327
public const string Issue2758 = "Jpg/issues/issue-2758.jpg";
328+
public const string Issue2857 = "Jpg/issues/issue-2857-subsub-ifds.jpg";
328329

329330
public static class Fuzz
330331
{
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)