Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/ImageSharp/Metadata/Profiles/IPTC/IptcProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ public void SetDateTimeValue(IptcTag tag, DateTimeOffset dateTimeOffset)
/// <summary>
/// Updates the data of the profile.
/// </summary>
/// <remarks>
/// When any value uses UTF-8 encoding, which is the default encoding of a value, the data starts with the
/// 1:90 Coded Character Set dataset declaring UTF-8, also when the text is ASCII only.
/// </remarks>
public void UpdateData()
{
int length = 0;
Expand Down Expand Up @@ -337,7 +341,9 @@ private bool HasValuesInUtf8()
{
foreach (IptcValue value in this.values)
{
if (value.Encoding == Encoding.UTF8)
// Compare the code page rather than the instance. Cloned values hold a copy of their encoding,
// and callers can pass their own UTF8Encoding, but both still write UTF-8 bytes.
if (value.Encoding.CodePage == Encoding.UTF8.CodePage)
{
return true;
}
Expand Down
51 changes: 51 additions & 0 deletions tests/ImageSharp.Tests/Metadata/Profiles/IPTC/IptcProfileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,57 @@ public void IptcProfile_WithUtf8Data_WritesEnvelopeRecord_Works()
Assert.True(profileBytes.AsSpan(0, 8).SequenceEqual(expectedEnvelopeData));
}

[Fact]
public void IptcProfile_DeepClone_WithUtf8Data_WritesEnvelopeRecord_Works()
{
// arrange
IptcProfile profile = new();
profile.SetValue(IptcTag.City, "ESPA脩A");
IptcProfile clone = profile.DeepClone();
byte[] expectedEnvelopeData = [28, 1, 90, 0, 3, 27, 37, 71];

// act
clone.UpdateData();

// assert
Assert.True(clone.Data.AsSpan(0, 8).SequenceEqual(expectedEnvelopeData));
}

[Fact]
public void IptcProfile_WithUtf8EncodingInstance_WritesEnvelopeRecord_Works()
{
// arrange
IptcProfile profile = new();
profile.SetValue(IptcTag.City, new System.Text.UTF8Encoding(false), "ESPA脩A");
byte[] expectedEnvelopeData = [28, 1, 90, 0, 3, 27, 37, 71];

// act
profile.UpdateData();

// assert
Assert.True(profile.Data.AsSpan(0, 8).SequenceEqual(expectedEnvelopeData));
}

[Fact]
public void WritingClonedImage_WithUtf8Data_WritesEnvelopeRecordOnce()
{
// arrange
using Image<Rgba32> image = new(1, 1);
image.Metadata.IptcProfile = new IptcProfile();
image.Metadata.IptcProfile.SetValue(IptcTag.Caption, "ESPA脩A");
byte[] expectedData = [28, 1, 90, 0, 3, 27, 37, 71, 28, 2, 120, 0, 7, 69, 83, 80, 65, 195, 145, 65];

// act
using Image<Rgba32> once = WriteAndReadJpeg(image.Clone());
using Image<Rgba32> twice = WriteAndReadJpeg(once.Clone());

// assert
IptcProfile actual = twice.Metadata.IptcProfile;
Assert.Equal(expectedData, actual.Data);
IptcValue value = Assert.Single(actual.Values);
Assert.Equal("ESPA脩A", value.Value);
}

[Theory]
[MemberData(nameof(AllIptcTags))]
public void IptcProfile_SetValue_WithStrictEnabled_Works(IptcTag tag)
Expand Down