Skip to content

Commit d0a09ee

Browse files
Merge branch 'main' into bp/Issue2877
2 parents 85c7ed2 + daa10b5 commit d0a09ee

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,8 +1597,8 @@ private int ReadImageHeaders(BufferedReadStream stream, out bool inverted, out b
15971597

15981598
if (palette.Length > 0)
15991599
{
1600-
Color[] colorTable = new Color[palette.Length / Unsafe.SizeOf<Bgr24>()];
1601-
ReadOnlySpan<Bgr24> rgbTable = MemoryMarshal.Cast<byte, Bgr24>(palette);
1600+
Color[] colorTable = new Color[palette.Length / Unsafe.SizeOf<Bgra32>()];
1601+
ReadOnlySpan<Bgra32> rgbTable = MemoryMarshal.Cast<byte, Bgra32>(palette);
16021602
Color.FromPixel(rgbTable, colorTable);
16031603
this.bmpMetadata.ColorTable = colorTable;
16041604
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Diagnostics;
88
using System.Globalization;
99
using System.Runtime.CompilerServices;
10+
using System.Runtime.InteropServices;
1011
using System.Text;
1112
using SixLabors.ImageSharp.Memory;
1213

@@ -187,11 +188,21 @@ protected void ReadValues(List<IExifValue> values, uint offset)
187188

188189
protected void ReadSubIfd(List<IExifValue> values)
189190
{
190-
if (this.subIfds is not null)
191+
if (this.subIfds != null)
191192
{
192-
foreach (ulong subIfdOffset in this.subIfds)
193+
const int maxSubIfds = 8;
194+
const int maxNestingLevel = 8;
195+
Span<ulong> buf = stackalloc ulong[maxSubIfds];
196+
for (int i = 0; i < maxNestingLevel && this.subIfds.Count > 0; i++)
193197
{
194-
this.ReadValues(values, (uint)subIfdOffset);
198+
int sz = Math.Min(this.subIfds.Count, maxSubIfds);
199+
CollectionsMarshal.AsSpan(this.subIfds)[..sz].CopyTo(buf);
200+
201+
this.subIfds.Clear();
202+
foreach (ulong subIfdOffset in buf[..sz])
203+
{
204+
this.ReadValues(values, (uint)subIfdOffset);
205+
}
195206
}
196207
}
197208
}
@@ -447,6 +458,7 @@ private void ReadValue64(List<IExifValue> values, Span<byte> offsetBuffer)
447458
ExifTagValue.TileByteCounts => new ExifLong8Array(ExifTagValue.TileByteCounts),
448459
_ => ExifValues.Create(tag) ?? ExifValues.Create(tag, dataType, numberOfComponents),
449460
};
461+
450462
if (exifValue is null)
451463
{
452464
this.AddInvalidTag(new UnkownExifTag(tag));
@@ -481,8 +493,9 @@ private void Add(IList<IExifValue> values, ExifValue exif, object? value)
481493

482494
foreach (IExifValue val in values)
483495
{
484-
// Sometimes duplicates appear, can compare val.Tag == exif.Tag
485-
if (val == exif)
496+
// to skip duplicates must be used Equals method,
497+
// == operator not defined for ExifValue and IExifValue
498+
if (exif.Equals(val))
486499
{
487500
Debug.WriteLine($"Duplicate Exif tag: tag={exif.Tag}, dataType={exif.DataType}");
488501
return;

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,8 @@ public void JpegDecoder_DecodeMetadataComment<TPixel>(TestImageProvider<TPixel>
439439
Assert.Equal(expectedComment, metadata.Comments.ElementAtOrDefault(0).ToString());
440440
image.DebugSave(provider);
441441
image.CompareToOriginal(provider);
442-
443442
}
444-
443+
445444
// https://github.com/SixLabors/ImageSharp/issues/2758
446445
[Theory]
447446
[WithFile(TestImages.Jpeg.Issues.Issue2758, PixelTypes.L8)]
@@ -468,6 +467,45 @@ public void Issue2758_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
468467
image.Save(ms, new JpegEncoder());
469468
}
470469

470+
// https://github.com/SixLabors/ImageSharp/issues/2857
471+
[Theory]
472+
[WithFile(TestImages.Jpeg.Issues.Issue2857, PixelTypes.Rgb24)]
473+
public void Issue2857_SubSubIfds<TPixel>(TestImageProvider<TPixel> provider)
474+
where TPixel : unmanaged, IPixel<TPixel>
475+
{
476+
using Image<TPixel> image = provider.GetImage(JpegDecoder.Instance);
477+
478+
Assert.Equal(5616, image.Width);
479+
Assert.Equal(3744, image.Height);
480+
481+
JpegMetadata meta = image.Metadata.GetJpegMetadata();
482+
Assert.Equal(92, meta.LuminanceQuality);
483+
Assert.Equal(93, meta.ChrominanceQuality);
484+
485+
ExifProfile exifProfile = image.Metadata.ExifProfile;
486+
Assert.NotNull(exifProfile);
487+
488+
using MemoryStream ms = new();
489+
bool hasThumbnail = exifProfile.TryCreateThumbnail(out _);
490+
Assert.False(hasThumbnail);
491+
492+
Assert.Equal("BilderBox - Erwin Wodicka / [email protected]", exifProfile.GetValue(ExifTag.Copyright).Value);
493+
Assert.Equal("Adobe Photoshop CS3 Windows", exifProfile.GetValue(ExifTag.Software).Value);
494+
495+
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);
496+
497+
Assert.Equal(
498+
new EncodedString(EncodedString.CharacterCode.ASCII, "StockSubmitter|Miscellaneous||Miscellaneous$|00|0000330000000110000000000000000|22$@NA_1005010.460@[email protected]$$@$@26$$@$@$@$@205$@$@$@$@$@$@$@$@$@43$@$@$@[email protected]$$@90$$@22$@$@$@$@$@$@$|||"),
499+
exifProfile.GetValue(ExifTag.UserComment).Value);
500+
501+
// the profile contains 4 duplicated UserComment
502+
Assert.Equal(1, exifProfile.Values.Count(t => t.Tag == ExifTag.UserComment));
503+
504+
image.Mutate(x => x.Crop(new(0, 0, 100, 100)));
505+
506+
image.Save(ms, new JpegEncoder());
507+
}
508+
471509
private static void VerifyEncodedStrings(ExifProfile exif)
472510
{
473511
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)