Wrong application markers order after changing one of them

Jul 27, 2015 at 11:58 AM
Hello

I have the image with several profiles(exif, iptc, and so on).
I change several profiles: add or remove some values and save them with the new image.
using (MagickImage image = new MagickImage(SampleFiles.FujiFilmFinePixS1ProJpg))
{
    ExifProfile exifProfile = image.GetExifProfile();
    IptcProfile iptcProfile = image.GetIptcProfile();

    exifProfile.SetValue(ExifTag.ExposureTime, 0.125);
    iptcProfile.SetValue(IptcTag.CopyrightNotice, "TEST");

    image.AddProfile(iptcProfile);
    image.AddProfile(exifProfile);

    image.Write(SampleFiles.OutputDirectory + "FujiFilmFinePixS1Pro.updatedExifAndIptc.jpg");
}
After that I open the generated image using hex editor and I see that order of application markers in header section has changed.
For example section for exif data(application marker APP0: FF E1) moved to another place in a file. See images below:
Image
Image

There is any way to save original order(from FF E0 down to FF EF) for all application markers?
Coordinator
Jul 27, 2015 at 12:29 PM
The profiles are saved to the file in an unspecified order and you cannot force a specific order. Why would that be important?
Jul 27, 2015 at 12:52 PM
Edited Jul 27, 2015 at 12:52 PM
I need to load file into stream and cut off some application markers from it.
I have a code which works on images not processed by MagickNET. This code expects that all blocks were saved in a correct order in a file.

See specification: http://www.media.mit.edu/pia/Research/deepview/exif.html
It starts from SOI(0xFFD8) Marker, so it's a JPEG file. Then APP1 Marker follows immediately.

I always believe that the APP1 marker should be located in the beginning of the file.

Can I remove all application markers from file and add them in a correct order?
Will they save in a correct order in this case?
Coordinator
Jul 27, 2015 at 8:01 PM
I read the page you linked and searched Google to see if the application segments should be written in a particular order but I could not find any evidence for that. The code of ImageMagick 'for eaches' the profile lists and writes them to the file. They are internally stored in a splay tree (https://en.wikipedia.org/wiki/Splay_tree) so I don't think the order can be forced.

Why don't you use Magick.NET to remove some of the application markers?
Jul 28, 2015 at 6:45 AM
Why don't you use Magick.NET to remove some of the application markers?
As I wrote before: I have to process the stream. I can't create a MagickImage because of performance issue.

I fixed that problem in a following way:
before saving image I use Strip() method to remove all application markers and after that add only needed profiles in the correct order.
So I don't need to change code that processing the stream.

Looks like it works correctly.