Skip to content

Commit bf261f0

Browse files
committed
Stylecop extra blank line issue fixed. Refactored code to extract common logic.
1 parent 1e53adf commit bf261f0

File tree

2 files changed

+39
-79
lines changed

2 files changed

+39
-79
lines changed

src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs

Lines changed: 37 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
using SixLabors.ImageSharp.Formats.Tiff.Constants;
77
using SixLabors.ImageSharp.Metadata;
88
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
9+
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
10+
using SixLabors.ImageSharp.Metadata.Profiles.Iptc;
911
using SixLabors.ImageSharp.Metadata.Profiles.Xmp;
10-
using SixLabors.ImageSharp.PixelFormats;
1112

1213
namespace SixLabors.ImageSharp.Formats.Tiff;
1314

@@ -76,7 +77,6 @@ public void Process(Image image, bool skipMetadata)
7677
}
7778
}
7879

79-
8080
public void Process(ImageFrame frame, bool skipMetadata)
8181
{
8282
this.ProcessProfiles(frame.Metadata, skipMetadata);
@@ -171,79 +171,30 @@ private void ProcessMetadata(ExifProfile exifProfile)
171171

172172
private void ProcessProfiles(ImageMetadata imageMetadata, bool skipMetadata)
173173
{
174-
if (!skipMetadata && (imageMetadata.ExifProfile != null && imageMetadata.ExifProfile.Parts != ExifParts.None))
175-
{
176-
foreach (IExifValue entry in imageMetadata.ExifProfile.Values)
177-
{
178-
if (!this.Collector.Entries.Exists(t => t.Tag == entry.Tag) && entry.GetValue() != null)
179-
{
180-
ExifParts entryPart = ExifTags.GetPart(entry.Tag);
181-
if (entryPart != ExifParts.None && imageMetadata.ExifProfile.Parts.HasFlag(entryPart))
182-
{
183-
this.Collector.AddOrReplace(entry.DeepClone());
184-
}
185-
}
186-
}
187-
}
188-
else
189-
{
190-
imageMetadata.ExifProfile?.RemoveValue(ExifTag.SubIFDOffset);
191-
}
192-
193-
if (!skipMetadata && imageMetadata.IptcProfile != null)
194-
{
195-
imageMetadata.IptcProfile.UpdateData();
196-
ExifByteArray iptc = new(ExifTagValue.IPTC, ExifDataType.Byte)
197-
{
198-
Value = imageMetadata.IptcProfile.Data
199-
};
200-
201-
this.Collector.AddOrReplace(iptc);
202-
}
203-
else
204-
{
205-
imageMetadata.ExifProfile?.RemoveValue(ExifTag.IPTC);
206-
}
207-
208-
if (imageMetadata.IccProfile != null)
209-
{
210-
ExifByteArray icc = new(ExifTagValue.IccProfile, ExifDataType.Undefined)
211-
{
212-
Value = imageMetadata.IccProfile.ToByteArray()
213-
};
214-
215-
this.Collector.AddOrReplace(icc);
216-
}
217-
else
218-
{
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-
}
174+
this.ProcessExifProfile(skipMetadata, imageMetadata.ExifProfile);
175+
this.ProcessIptcProfile(skipMetadata, imageMetadata.IptcProfile, imageMetadata.ExifProfile);
176+
this.ProcessIccProfile(imageMetadata.IccProfile, imageMetadata.ExifProfile);
177+
this.ProcessXmpProfile(skipMetadata, imageMetadata.XmpProfile, imageMetadata.ExifProfile);
235178
}
236179

237180
private void ProcessProfiles(ImageFrameMetadata frameMetadata, bool skipMetadata)
238181
{
239-
if (!skipMetadata && (frameMetadata.ExifProfile != null && frameMetadata.ExifProfile.Parts != ExifParts.None))
182+
this.ProcessExifProfile(skipMetadata, frameMetadata.ExifProfile);
183+
this.ProcessIptcProfile(skipMetadata, frameMetadata.IptcProfile, frameMetadata.ExifProfile);
184+
this.ProcessIccProfile(frameMetadata.IccProfile, frameMetadata.ExifProfile);
185+
this.ProcessXmpProfile(skipMetadata, frameMetadata.XmpProfile, frameMetadata.ExifProfile);
186+
}
187+
188+
private void ProcessExifProfile(bool skipMetadata, ExifProfile exifProfile)
189+
{
190+
if (!skipMetadata && (exifProfile != null && exifProfile.Parts != ExifParts.None))
240191
{
241-
foreach (IExifValue entry in frameMetadata.ExifProfile.Values)
192+
foreach (IExifValue entry in exifProfile.Values)
242193
{
243194
if (!this.Collector.Entries.Exists(t => t.Tag == entry.Tag) && entry.GetValue() != null)
244195
{
245196
ExifParts entryPart = ExifTags.GetPart(entry.Tag);
246-
if (entryPart != ExifParts.None && frameMetadata.ExifProfile.Parts.HasFlag(entryPart))
197+
if (entryPart != ExifParts.None && exifProfile.Parts.HasFlag(entryPart))
247198
{
248199
this.Collector.AddOrReplace(entry.DeepClone());
249200
}
@@ -252,50 +203,59 @@ private void ProcessProfiles(ImageFrameMetadata frameMetadata, bool skipMetadata
252203
}
253204
else
254205
{
255-
frameMetadata.ExifProfile?.RemoveValue(ExifTag.SubIFDOffset);
206+
exifProfile?.RemoveValue(ExifTag.SubIFDOffset);
256207
}
208+
}
257209

258-
if (!skipMetadata && frameMetadata.IptcProfile != null)
210+
private void ProcessIptcProfile(bool skipMetadata, IptcProfile iptcProfile, ExifProfile exifProfile)
211+
{
212+
if (!skipMetadata && iptcProfile != null)
259213
{
260-
frameMetadata.IptcProfile.UpdateData();
214+
iptcProfile.UpdateData();
261215
ExifByteArray iptc = new(ExifTagValue.IPTC, ExifDataType.Byte)
262216
{
263-
Value = frameMetadata.IptcProfile.Data
217+
Value = iptcProfile.Data
264218
};
265219

266220
this.Collector.AddOrReplace(iptc);
267221
}
268222
else
269223
{
270-
frameMetadata.ExifProfile?.RemoveValue(ExifTag.IPTC);
224+
exifProfile?.RemoveValue(ExifTag.IPTC);
271225
}
226+
}
272227

273-
if (frameMetadata.IccProfile != null)
228+
private void ProcessIccProfile(IccProfile iccProfile, ExifProfile exifProfile)
229+
{
230+
if (iccProfile != null)
274231
{
275232
ExifByteArray icc = new(ExifTagValue.IccProfile, ExifDataType.Undefined)
276233
{
277-
Value = frameMetadata.IccProfile.ToByteArray()
234+
Value = iccProfile.ToByteArray()
278235
};
279236

280237
this.Collector.AddOrReplace(icc);
281238
}
282239
else
283240
{
284-
frameMetadata.ExifProfile?.RemoveValue(ExifTag.IccProfile);
241+
exifProfile?.RemoveValue(ExifTag.IccProfile);
285242
}
243+
}
286244

287-
if (!skipMetadata && frameMetadata.XmpProfile != null)
245+
private void ProcessXmpProfile(bool skipMetadata, XmpProfile xmpProfile, ExifProfile exifProfile)
246+
{
247+
if (!skipMetadata && xmpProfile != null)
288248
{
289249
ExifByteArray xmp = new(ExifTagValue.XMP, ExifDataType.Byte)
290250
{
291-
Value = frameMetadata.XmpProfile.Data
251+
Value = xmpProfile.Data
292252
};
293253

294254
this.Collector.AddOrReplace(xmp);
295255
}
296256
else
297257
{
298-
frameMetadata.ExifProfile?.RemoveValue(ExifTag.XMP);
258+
exifProfile?.RemoveValue(ExifTag.XMP);
299259
}
300260
}
301261
}

tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ public void Encode_PreservesMetadata_IptcAndIcc<TPixel>(TestImageProvider<TPixel
336336
iptcProfile.SetValue(IptcTag.Name, "Test name");
337337
rootFrameInput.Metadata.IptcProfile = iptcProfile;
338338

339-
IccProfileHeader iccProfileHeader = new IccProfileHeader();
340-
iccProfileHeader.Class = IccProfileClass.ColorSpace;
339+
IccProfileHeader iccProfileHeader = new() { Class = IccProfileClass.ColorSpace };
341340
IccProfile iccProfile = new();
342341
rootFrameInput.Metadata.IccProfile = iccProfile;
343342

@@ -406,6 +405,7 @@ public void Encode_PreservesMetadata_IptcAndIcc<TPixel>(TestImageProvider<TPixel
406405
Assert.Equal(exifProfileInput.GetValue(ExifTag.Model).Value, encodedImageExifProfile.GetValue(ExifTag.Model).Value);
407406

408407
Assert.Equal((ushort)TiffPlanarConfiguration.Chunky, encodedImageExifProfile.GetValue(ExifTag.PlanarConfiguration)?.Value);
408+
409409
// Adding the IPTC and ICC profiles dynamically increments the number of values in the original EXIF profile by 2
410410
Assert.Equal(exifProfileInput.Values.Count + 2, encodedImageExifProfile.Values.Count);
411411
}

0 commit comments

Comments
 (0)