dlemstra / Magick.NET

The .NET library for ImageMagick
Apache License 2.0
3.49k stars 415 forks source link

Exif data not persisted after loading #1764

Open etihwddot opened 1 day ago

etihwddot commented 1 day ago

Magick.NET version

14.2

Environment (Operating system, version and so on)

Windows

Description

When loading some images with MagickImage and writing them to a stream or saving ToByteArray the loaded exif metadata is no longer written to the output. This causes some things like orientation information to be lost.

This appears to be a regression from earlier versions of Magick.NET. Specifically, we were previously using 13.4 before upgrading and seeing this new behavior. It seems likely that the image is corrupt in some way (at the very least looks like it doesn't have the EOI marker) but it was great that MagickImage handled these corrupt images from our users better in the previous version.

We have a workaround for orientation by calling AutoOrient before persisting to a byte array but I don't know if there is other important metadata that could be lost in this process.

Steps to Reproduce

The following output samples are from using the image attached to this as input and the provided script.

14.2 output

Before Has Exif Profie: True Has Color Profie: True

After Has Profie: False Has Color Profie: True

13.4 output

Before Has Exif Profie: True Has Color Profie: True

After Has Profie: True Has Color Profie: True

Test code

using var image = new MagickImage(inputFile);

Console.WriteLine("Before");
Console.WriteLine($"Has Exif Profie: {(image.GetExifProfile() != null)}");
Console.WriteLine($"Has Color Profie: {(image.GetColorProfile() != null)}");

var bytes = image.ToByteArray();

using var reloadedImage = new MagickImage(bytes);   
Console.WriteLine();
Console.WriteLine($"After");
Console.WriteLine($"Has Profie: {(reloadedImage.GetExifProfile() != null)}");
Console.WriteLine($"Has Color Profie: {(reloadedImage.GetColorProfile() != null)}");

reloadedImage.Write(outputFile);

rome

dlemstra commented 1 day ago

It looks like you found an edge case situation. The length of your profile is the maximum length an exif profile can have in a jpef file. But we had an incorrect check in the code of ImageMagick. I just pushed a patch to fix that and this issue will be resolved in the next release. I will add a unit test at a later point to make sure this stays fixed.