Skip to content

Exif metadata for "User Comment" maybe encoded incorrectly? #1919

Closed

Description

@danpetitt

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 DEBUG and RELEASE mode
  • 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:
metadata

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

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

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

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

  1. changed the title [-]Exif metadata for "User Comment" is encoded incorrectly?[/-] [+]Exif metadata for "User Comment" maybe encoded incorrectly?[/+] on Jan 2, 2022
  2. added this to the milestone on Jan 3, 2022
  3. JimBobSquarePants commented on Jan 3, 2022

    @JimBobSquarePants
    Member

    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.

  4. IldarKhayrutdinov commented on Jan 4, 2022

    @IldarKhayrutdinov
    Contributor

    @danpetitt You are create tags of a byte type:
    exif.SetValue<byte[]>
    So the dump viewer displays those bytes as ascii text.

    By tiff specification (page 15):

    2 = ASCII 8-bit byte that contains a 7-bit ASCII code; the last byte
    must be NUL (binary zero).

    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 UTF8 encoding (probably this is better that 7-bit ACSII), it's required to be corrected to use specified encoding:

    case ExifDataType.Ascii:
    offset = Write(Encoding.UTF8.GetBytes((string)value), destination, offset);

  5. danpetitt commented on Jan 4, 2022

    @danpetitt
    Author

    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 UserComment tag (and GPSProcessingMethod, GPSAreaInformation tags 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.

    image

    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 string generic 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.

  6. IldarKhayrutdinov commented on Jan 4, 2022

    @IldarKhayrutdinov
    Contributor

    @danpetitt thanks for the explanation. I understand, it turns out we need to implement writing strings as in these tags
    UserComment
    GPSProcessingMethod
    GPSAreaInformation

  7. danpetitt commented on Jan 4, 2022

    @danpetitt
    Author

    Yeah, I think thats my understanding of the spec. Thanks for your patience image

  8. IldarKhayrutdinov commented on Jan 10, 2022

    @IldarKhayrutdinov
    Contributor

    Draft PR: #1935

  9. antonfirsov commented on Feb 6, 2022

    @antonfirsov
    Member

    #1935 is merged, so I assume this is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions