77using SixLabors . ImageSharp . Metadata ;
88using SixLabors . ImageSharp . Metadata . Profiles . Exif ;
99using SixLabors . ImageSharp . Metadata . Profiles . Xmp ;
10+ using SixLabors . ImageSharp . PixelFormats ;
1011
1112namespace SixLabors . ImageSharp . Formats . Tiff ;
1213
@@ -19,6 +20,9 @@ internal class TiffEncoderEntriesCollector
1920 public void ProcessMetadata ( Image image , bool skipMetadata )
2021 => new MetadataProcessor ( this ) . Process ( image , skipMetadata ) ;
2122
23+ public void ProcessMetadata ( ImageFrame frame , bool skipMetadata )
24+ => new MetadataProcessor ( this ) . Process ( frame , skipMetadata ) ;
25+
2226 public void ProcessFrameInfo ( ImageFrame frame , ImageMetadata imageMetadata )
2327 => new FrameInfoProcessor ( this ) . Process ( frame , imageMetadata ) ;
2428
@@ -56,15 +60,30 @@ public MetadataProcessor(TiffEncoderEntriesCollector collector)
5660
5761 public void Process ( Image image , bool skipMetadata )
5862 {
59- ImageFrame rootFrame = image . Frames . RootFrame ;
60- ExifProfile rootFrameExifProfile = rootFrame . Metadata . ExifProfile ;
61- XmpProfile rootFrameXmpProfile = rootFrame . Metadata . XmpProfile ;
63+ this . ProcessProfiles ( image . Metadata , skipMetadata ) ;
6264
63- this . ProcessProfiles ( image . Metadata , skipMetadata , rootFrameExifProfile , rootFrameXmpProfile ) ;
65+ if ( ! skipMetadata )
66+ {
67+ this . ProcessMetadata ( image . Metadata . ExifProfile ?? new ExifProfile ( ) ) ;
68+ }
69+
70+ if ( ! this . Collector . Entries . Exists ( t => t . Tag == ExifTag . Software ) )
71+ {
72+ this . Collector . Add ( new ExifString ( ExifTagValue . Software )
73+ {
74+ Value = SoftwareValue
75+ } ) ;
76+ }
77+ }
78+
79+
80+ public void Process ( ImageFrame frame , bool skipMetadata )
81+ {
82+ this . ProcessProfiles ( frame . Metadata , skipMetadata ) ;
6483
6584 if ( ! skipMetadata )
6685 {
67- this . ProcessMetadata ( rootFrameExifProfile ?? new ExifProfile ( ) ) ;
86+ this . ProcessMetadata ( frame . Metadata . ExifProfile ?? new ExifProfile ( ) ) ;
6887 }
6988
7089 if ( ! this . Collector . Entries . Exists ( t => t . Tag == ExifTag . Software ) )
@@ -150,16 +169,16 @@ private void ProcessMetadata(ExifProfile exifProfile)
150169 }
151170 }
152171
153- private void ProcessProfiles ( ImageMetadata imageMetadata , bool skipMetadata , ExifProfile exifProfile , XmpProfile xmpProfile )
172+ private void ProcessProfiles ( ImageMetadata imageMetadata , bool skipMetadata )
154173 {
155- if ( ! skipMetadata && ( exifProfile != null && exifProfile . Parts != ExifParts . None ) )
174+ if ( ! skipMetadata && ( imageMetadata . ExifProfile != null && imageMetadata . ExifProfile . Parts != ExifParts . None ) )
156175 {
157- foreach ( IExifValue entry in exifProfile . Values )
176+ foreach ( IExifValue entry in imageMetadata . ExifProfile . Values )
158177 {
159178 if ( ! this . Collector . Entries . Exists ( t => t . Tag == entry . Tag ) && entry . GetValue ( ) != null )
160179 {
161180 ExifParts entryPart = ExifTags . GetPart ( entry . Tag ) ;
162- if ( entryPart != ExifParts . None && exifProfile . Parts . HasFlag ( entryPart ) )
181+ if ( entryPart != ExifParts . None && imageMetadata . ExifProfile . Parts . HasFlag ( entryPart ) )
163182 {
164183 this . Collector . AddOrReplace ( entry . DeepClone ( ) ) ;
165184 }
@@ -168,7 +187,7 @@ private void ProcessProfiles(ImageMetadata imageMetadata, bool skipMetadata, Exi
168187 }
169188 else
170189 {
171- exifProfile ? . RemoveValue ( ExifTag . SubIFDOffset ) ;
190+ imageMetadata . ExifProfile ? . RemoveValue ( ExifTag . SubIFDOffset ) ;
172191 }
173192
174193 if ( ! skipMetadata && imageMetadata . IptcProfile != null )
@@ -183,7 +202,7 @@ private void ProcessProfiles(ImageMetadata imageMetadata, bool skipMetadata, Exi
183202 }
184203 else
185204 {
186- exifProfile ? . RemoveValue ( ExifTag . IPTC ) ;
205+ imageMetadata . ExifProfile ? . RemoveValue ( ExifTag . IPTC ) ;
187206 }
188207
189208 if ( imageMetadata . IccProfile != null )
@@ -197,21 +216,86 @@ private void ProcessProfiles(ImageMetadata imageMetadata, bool skipMetadata, Exi
197216 }
198217 else
199218 {
200- exifProfile ? . RemoveValue ( ExifTag . IccProfile ) ;
219+ imageMetadata . ExifProfile ? . RemoveValue ( ExifTag . IccProfile ) ;
220+ }
221+
222+ if ( ! skipMetadata && imageMetadata . XmpProfile != null )
223+ {
224+ ExifByteArray xmp = new ( ExifTagValue . XMP , ExifDataType . Byte )
225+ {
226+ Value = imageMetadata . XmpProfile . Data
227+ } ;
228+
229+ this . Collector . AddOrReplace ( xmp ) ;
230+ }
231+ else
232+ {
233+ imageMetadata . ExifProfile ? . RemoveValue ( ExifTag . XMP ) ;
234+ }
235+ }
236+
237+ private void ProcessProfiles ( ImageFrameMetadata frameMetadata , bool skipMetadata )
238+ {
239+ if ( ! skipMetadata && ( frameMetadata . ExifProfile != null && frameMetadata . ExifProfile . Parts != ExifParts . None ) )
240+ {
241+ foreach ( IExifValue entry in frameMetadata . ExifProfile . Values )
242+ {
243+ if ( ! this . Collector . Entries . Exists ( t => t . Tag == entry . Tag ) && entry . GetValue ( ) != null )
244+ {
245+ ExifParts entryPart = ExifTags . GetPart ( entry . Tag ) ;
246+ if ( entryPart != ExifParts . None && frameMetadata . ExifProfile . Parts . HasFlag ( entryPart ) )
247+ {
248+ this . Collector . AddOrReplace ( entry . DeepClone ( ) ) ;
249+ }
250+ }
251+ }
252+ }
253+ else
254+ {
255+ frameMetadata . ExifProfile ? . RemoveValue ( ExifTag . SubIFDOffset ) ;
256+ }
257+
258+ if ( ! skipMetadata && frameMetadata . IptcProfile != null )
259+ {
260+ frameMetadata . IptcProfile . UpdateData ( ) ;
261+ ExifByteArray iptc = new ( ExifTagValue . IPTC , ExifDataType . Byte )
262+ {
263+ Value = frameMetadata . IptcProfile . Data
264+ } ;
265+
266+ this . Collector . AddOrReplace ( iptc ) ;
267+ }
268+ else
269+ {
270+ frameMetadata . ExifProfile ? . RemoveValue ( ExifTag . IPTC ) ;
271+ }
272+
273+ if ( frameMetadata . IccProfile != null )
274+ {
275+ ExifByteArray icc = new ( ExifTagValue . IccProfile , ExifDataType . Undefined )
276+ {
277+ Value = frameMetadata . IccProfile . ToByteArray ( )
278+ } ;
279+
280+ this . Collector . AddOrReplace ( icc ) ;
281+ }
282+ else
283+ {
284+ frameMetadata . ExifProfile ? . RemoveValue ( ExifTag . IccProfile ) ;
201285 }
202286
203- if ( ! skipMetadata && xmpProfile != null )
287+ if ( ! skipMetadata && frameMetadata . XmpProfile != null )
204288 {
205289 ExifByteArray xmp = new ( ExifTagValue . XMP , ExifDataType . Byte )
206290 {
207- Value = xmpProfile . Data
291+ Value = frameMetadata . XmpProfile . Data
208292 } ;
209293
210294 this . Collector . AddOrReplace ( xmp ) ;
211295 }
212296 else
213297 {
214- exifProfile ? . RemoveValue ( ExifTag . XMP ) ;
298+ frameMetadata . ExifProfile ? . RemoveValue ( ExifTag . XMP ) ;
215299 }
216300 }
217301 }
0 commit comments