7
7
using SixLabors . ImageSharp . Metadata ;
8
8
using SixLabors . ImageSharp . Metadata . Profiles . Exif ;
9
9
using SixLabors . ImageSharp . Metadata . Profiles . Xmp ;
10
+ using SixLabors . ImageSharp . PixelFormats ;
10
11
11
12
namespace SixLabors . ImageSharp . Formats . Tiff ;
12
13
@@ -19,6 +20,9 @@ internal class TiffEncoderEntriesCollector
19
20
public void ProcessMetadata ( Image image , bool skipMetadata )
20
21
=> new MetadataProcessor ( this ) . Process ( image , skipMetadata ) ;
21
22
23
+ public void ProcessMetadata ( ImageFrame frame , bool skipMetadata )
24
+ => new MetadataProcessor ( this ) . Process ( frame , skipMetadata ) ;
25
+
22
26
public void ProcessFrameInfo ( ImageFrame frame , ImageMetadata imageMetadata )
23
27
=> new FrameInfoProcessor ( this ) . Process ( frame , imageMetadata ) ;
24
28
@@ -56,15 +60,30 @@ public MetadataProcessor(TiffEncoderEntriesCollector collector)
56
60
57
61
public void Process ( Image image , bool skipMetadata )
58
62
{
59
- ImageFrame rootFrame = image . Frames . RootFrame ;
60
- ExifProfile rootFrameExifProfile = rootFrame . Metadata . ExifProfile ;
61
- XmpProfile rootFrameXmpProfile = rootFrame . Metadata . XmpProfile ;
63
+ this . ProcessProfiles ( image . Metadata , skipMetadata ) ;
62
64
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 ) ;
64
83
65
84
if ( ! skipMetadata )
66
85
{
67
- this . ProcessMetadata ( rootFrameExifProfile ?? new ExifProfile ( ) ) ;
86
+ this . ProcessMetadata ( frame . Metadata . ExifProfile ?? new ExifProfile ( ) ) ;
68
87
}
69
88
70
89
if ( ! this . Collector . Entries . Exists ( t => t . Tag == ExifTag . Software ) )
@@ -150,16 +169,16 @@ private void ProcessMetadata(ExifProfile exifProfile)
150
169
}
151
170
}
152
171
153
- private void ProcessProfiles ( ImageMetadata imageMetadata , bool skipMetadata , ExifProfile exifProfile , XmpProfile xmpProfile )
172
+ private void ProcessProfiles ( ImageMetadata imageMetadata , bool skipMetadata )
154
173
{
155
- if ( ! skipMetadata && ( exifProfile != null && exifProfile . Parts != ExifParts . None ) )
174
+ if ( ! skipMetadata && ( imageMetadata . ExifProfile != null && imageMetadata . ExifProfile . Parts != ExifParts . None ) )
156
175
{
157
- foreach ( IExifValue entry in exifProfile . Values )
176
+ foreach ( IExifValue entry in imageMetadata . ExifProfile . Values )
158
177
{
159
178
if ( ! this . Collector . Entries . Exists ( t => t . Tag == entry . Tag ) && entry . GetValue ( ) != null )
160
179
{
161
180
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 ) )
163
182
{
164
183
this . Collector . AddOrReplace ( entry . DeepClone ( ) ) ;
165
184
}
@@ -168,7 +187,7 @@ private void ProcessProfiles(ImageMetadata imageMetadata, bool skipMetadata, Exi
168
187
}
169
188
else
170
189
{
171
- exifProfile ? . RemoveValue ( ExifTag . SubIFDOffset ) ;
190
+ imageMetadata . ExifProfile ? . RemoveValue ( ExifTag . SubIFDOffset ) ;
172
191
}
173
192
174
193
if ( ! skipMetadata && imageMetadata . IptcProfile != null )
@@ -183,7 +202,7 @@ private void ProcessProfiles(ImageMetadata imageMetadata, bool skipMetadata, Exi
183
202
}
184
203
else
185
204
{
186
- exifProfile ? . RemoveValue ( ExifTag . IPTC ) ;
205
+ imageMetadata . ExifProfile ? . RemoveValue ( ExifTag . IPTC ) ;
187
206
}
188
207
189
208
if ( imageMetadata . IccProfile != null )
@@ -197,21 +216,86 @@ private void ProcessProfiles(ImageMetadata imageMetadata, bool skipMetadata, Exi
197
216
}
198
217
else
199
218
{
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 ) ;
201
285
}
202
286
203
- if ( ! skipMetadata && xmpProfile != null )
287
+ if ( ! skipMetadata && frameMetadata . XmpProfile != null )
204
288
{
205
289
ExifByteArray xmp = new ( ExifTagValue . XMP , ExifDataType . Byte )
206
290
{
207
- Value = xmpProfile . Data
291
+ Value = frameMetadata . XmpProfile . Data
208
292
} ;
209
293
210
294
this . Collector . AddOrReplace ( xmp ) ;
211
295
}
212
296
else
213
297
{
214
- exifProfile ? . RemoveValue ( ExifTag . XMP ) ;
298
+ frameMetadata . ExifProfile ? . RemoveValue ( ExifTag . XMP ) ;
215
299
}
216
300
}
217
301
}
0 commit comments