Repository navigation
Description
Prerequisites
- I have written a descriptive issue title
- I have verified that I am running the latest version of ImageSharp
- I have verified if the problem exist in both
DEBUGandRELEASEmode - I have searched open and closed issues to ensure it has not already been reported
Description
I believe, when writing out a "User Comment" Exif tag, the tag is an 'undefined' type which means, according to the spec, that the encoding type should be written out in the first 8 bytes of the value data.
This is seen if you set the metadata and use https://www.metadata2go.com/ to verify the data, it will produce a warning for the "User Comment" tag saying "Invalid EXIF text encoding for UserComment".
If you then edit the metadata on the site and download you will get the additional 8 bytes written before the tag text area as defined in the spec and it then validates okay.
Refer to the specification at https://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf on page 46 for user comment.
I couldnt see anywhere where it states this is optional; but I may be misreading things here so apologies.
On another site (https://www.exiv2.org/tags.html) it states that this may be applicable to other tags which are also of the 'undefined' or 'comment' type:
The string encoding is defined using the same scheme as UserComment
- GPSProcessingMethod
- GPSAreaInformation
Steps to Reproduce
using( var image = Image.Load( "metadata.jpg" ) )
{
var exif = new ExifProfile();
exif.SetValue<byte[]>( ExifTag.XPAuthor, Encoding.GetEncoding( "UCS-2" ).GetBytes( "Dan Petitt" ) );
exif.SetValue<byte[]>( ExifTag.XPTitle, Encoding.GetEncoding( "UCS-2" ).GetBytes( "A bit of test metadata for image title" ) );
exif.SetValue<byte[]>( ExifTag.UserComment, Encoding.ASCII.GetBytes( "A bit of normal comment text" ) ); // Possibly needs to a parameter to specify what encoding is being used to write the bytes out perhaps?
image.Metadata.ExifProfile = exif;
image.Save( "metadata-exif.jpg" );
}metadata.jpg shows the file written out by ImageSharp:

The binary dump of the file showing no 8 byte prefix to the value data...

metadata-fixed.jpg shows the file written out by metadata2go:

The binary dump of the fixed file showing the new 8 byte ASCII prefix:

System Configuration
- ImageSharp version: v1.0.4
- Environment (Operating system, version and so on): Windows 11, Visual Studio 2022
- .NET Framework version: .NET 6
- Additional information:
Activity
[-]Exif metadata for "User Comment" is encoded incorrectly?[/-][+]Exif metadata for "User Comment" maybe encoded incorrectly?[/+]JimBobSquarePants commentedon Jan 3, 2022
Thanks @danpetitt for such a detailed issue report. Much appreciated!
Yep, that certainly looks like an issue. I had a quick read of the relevant parts of the specification you highlighted and your findings looks correct to me. We should prioritize getting this fixed for V2.
@dlemstra this will interest you also I'm sure.
IldarKhayrutdinov commentedon Jan 4, 2022
@danpetitt You are create tags of a
bytetype:exif.SetValue<byte[]>So the dump viewer displays those bytes as ascii text.
By tiff specification (page 15):
There are encodings problems on practice: https://stackoverflow.com/a/19284524/3189289
We need to consider the ability to create text type tags with a specified encoding.
At the moment the strings are converted to bytes with
UTF8encoding (probably this is better that 7-bit ACSII), it's required to be corrected to use specified encoding:ImageSharp/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs
Lines 369 to 370 in e20410c
danpetitt commentedon Jan 4, 2022
Thanks for looking into this @IldarKhayrutdinov. However, forgive me, but I do not think, if I follow you correctly, that this is the problem.
The problem I report is specifically to do with the
UserCommenttag (andGPSProcessingMethod,GPSAreaInformationtags which follow the same scheme) which states that the first 8 bytes must be the encoding signature that has been used to encode the data.Without these first 8 bytes, its invalid, but also cannot be decoded reliably by any readers ... as a decoder, someone can probably assume or try UTF8 if these 8 bytes do not match any of the signatures in Table 9 I guess, but its still invalid without.
As a suggestion you could possibly receive a
stringgeneric and encode as Unicode then write the unicode signature for these fields. This should cover most cases and avoids having to create a new interface for the API user to specify the encoding of the byte array passed to the ImageSharp method.IldarKhayrutdinov commentedon Jan 4, 2022
@danpetitt thanks for the explanation. I understand, it turns out we need to implement writing strings as in these tags
UserComment
GPSProcessingMethod
GPSAreaInformation
danpetitt commentedon Jan 4, 2022
Yeah, I think thats my understanding of the spec. Thanks for your patience
IldarKhayrutdinov commentedon Jan 10, 2022
Draft PR: #1935
antonfirsov commentedon Feb 6, 2022
#1935 is merged, so I assume this is fixed.