From 91ef4e6898c03c65f15ee1c28f8e5257b403727e Mon Sep 17 00:00:00 2001 From: Richard Fox Date: Wed, 8 May 2024 05:14:15 -0700 Subject: [PATCH] feat(exif): Enable EXIF Writing using FIMD_EXIF_RAW FreeImage keeps two separate copies of Exif Metadata in memory: - One is used/found in MDM_EXIF_MAIN and its extensions(e.g. MDM_EXIF_GPS). - Another one is used when files are actually written that support the EXIF Format. - In some cases you want to access, the raw data. - A good example of this is when using MetadataModel.DestroyModel to wipe EXIF data on an image that you wanted persisted if that file is exported again from FreeImage. - DestoryModel was also corrected to DestroyModel, this will result in a breaking change version but that's worth it! With this you can use the following to clear and persist that "clearing" of the exif data on a FreeImage image: ```cs ImageMetadata imageMetadata = new ImageMetadata(dib, true, true); var model = imageMetadata[FREE_IMAGE_MDMODEL.FIMD_EXIF_RAW]; model.DestroyModel(); ``` When you then write that subsequent image to a file, the file will not contain any EXIF data! This is based on: - https://sourceforge.net/p/freeimage/discussion/36111/thread/2d087f91/ where i discovered FIMD_EXIF_RAW - https://sourceforge.net/p/freeimage/discussion/36111/thread/8233f5bc/ where I learnt how you might use it. As this change doesn't touch the native library, you won't need to compile that again. This is a purely .NET change. - --- .../cs/Library/Classes/ImageMetadata.cs | 19 ++++++++----- .../cs/Library/Classes/MetadataModel.cs | 2 +- .../cs/Library/Classes/MetadataModels.cs | 28 +++++++++++++++++++ .../Enumerations/FREE_IMAGE_MDMODEL.cs | 8 +++++- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/Wrapper/FreeImage.NET/cs/Library/Classes/ImageMetadata.cs b/Wrapper/FreeImage.NET/cs/Library/Classes/ImageMetadata.cs index e49f7e4..e342e95 100644 --- a/Wrapper/FreeImage.NET/cs/Library/Classes/ImageMetadata.cs +++ b/Wrapper/FreeImage.NET/cs/Library/Classes/ImageMetadata.cs @@ -60,14 +60,15 @@ public class ImageMetadata : IEnumerable, IComparable, IComparableHandle to a FreeImage bitmap. public ImageMetadata(FIBITMAP dib) : this(dib, false) { } - /// - /// Initializes a new instance based on the specified , - /// showing or hiding empry models. - /// - /// Handle to a FreeImage bitmap. - /// When true, empty metadata models + /// + /// Initializes a new instance based on the specified , + /// showing or hiding empry models. + /// + /// Handle to a FreeImage bitmap. + /// When true, empty metadata models /// will be hidden until a tag to this model is added. - public ImageMetadata(FIBITMAP dib, bool hideEmptyModels) + /// When true, include raw byte access to EXIF, . + public ImageMetadata(FIBITMAP dib, bool hideEmptyModels, bool includeRaw = false) { if (dib.IsNull) throw new ArgumentNullException("dib"); data = new List(FreeImage.FREE_IMAGE_MDMODELS.Length); @@ -86,6 +87,10 @@ public ImageMetadata(FIBITMAP dib, bool hideEmptyModels) data.Add(new MDM_IPTC(dib)); data.Add(new MDM_NODATA(dib)); data.Add(new MDM_XMP(dib)); + + // This is based on: https://sourceforge.net/p/freeimage/discussion/36111/thread/2d087f91/ + if (includeRaw) + data.Add(new MDM_EXIF_RAW(dib)); } /// diff --git a/Wrapper/FreeImage.NET/cs/Library/Classes/MetadataModel.cs b/Wrapper/FreeImage.NET/cs/Library/Classes/MetadataModel.cs index 5857db8..d5fa23f 100644 --- a/Wrapper/FreeImage.NET/cs/Library/Classes/MetadataModel.cs +++ b/Wrapper/FreeImage.NET/cs/Library/Classes/MetadataModel.cs @@ -148,7 +148,7 @@ public bool RemoveTag(string key) /// which will remove all tags of this model from the bitmap. /// /// Returns true on success, false on failure. - public bool DestoryModel() + public bool DestroyModel() { return FreeImage.SetMetadata(Model, dib, null, FITAG.Zero); } diff --git a/Wrapper/FreeImage.NET/cs/Library/Classes/MetadataModels.cs b/Wrapper/FreeImage.NET/cs/Library/Classes/MetadataModels.cs index dfa9f90..d3b29d1 100644 --- a/Wrapper/FreeImage.NET/cs/Library/Classes/MetadataModels.cs +++ b/Wrapper/FreeImage.NET/cs/Library/Classes/MetadataModels.cs @@ -384,6 +384,34 @@ public override FREE_IMAGE_MDMODEL Model } } + /// + /// Represents a collection of all tags contained in the metadata model + /// . + /// + /// + /// FreeImage keeps two separate copies of Exif Metadata in memory. + /// One is used/found in and its extensions(e.g. ). + /// Another one is used when files are actually written that support the EXIF Format. In some cases you want to access, the raw data. + /// A good example of this is when using to wipe EXIF data on an image, + /// that you want persisted if that file is exported again from FreeImage. + /// + public class MDM_EXIF_RAW : MetadataModel + { + /// + /// Initializes a new instance of this class. + /// + /// Handle to a FreeImage bitmap. + public MDM_EXIF_RAW(FIBITMAP dib) : base(dib) { } + + /// + /// Retrieves the datamodel that this instance represents. + /// + public override FREE_IMAGE_MDMODEL Model + { + get { return FREE_IMAGE_MDMODEL.FIMD_EXIF_RAW; } + } + } + /// /// Represents a collection of all tags contained in the metadata model /// . diff --git a/Wrapper/FreeImage.NET/cs/Library/Enumerations/FREE_IMAGE_MDMODEL.cs b/Wrapper/FreeImage.NET/cs/Library/Enumerations/FREE_IMAGE_MDMODEL.cs index 99907aa..9571d1d 100644 --- a/Wrapper/FreeImage.NET/cs/Library/Enumerations/FREE_IMAGE_MDMODEL.cs +++ b/Wrapper/FreeImage.NET/cs/Library/Enumerations/FREE_IMAGE_MDMODEL.cs @@ -87,6 +87,12 @@ public enum FREE_IMAGE_MDMODEL /// /// Used to attach other metadata types to a dib /// - FIMD_CUSTOM = 10 + FIMD_CUSTOM = 10, + + /// + /// Raw access to EXIF. + /// + /// This is based on: https://sourceforge.net/p/freeimage/discussion/36111/thread/2d087f91/ + FIMD_EXIF_RAW = 11 } } \ No newline at end of file