diff --git a/src/ImageSharp/Common/Helpers/UnitConverter.cs b/src/ImageSharp/Common/Helpers/UnitConverter.cs
index efc0e0e152..03d50c025c 100644
--- a/src/ImageSharp/Common/Helpers/UnitConverter.cs
+++ b/src/ImageSharp/Common/Helpers/UnitConverter.cs
@@ -100,12 +100,11 @@ namespace SixLabors.ImageSharp.Common.Helpers
///
/// Sets the exif profile resolution values.
///
- /// The exif profile.
/// The resolution unit.
/// The horizontal resolution value.
/// The vertical resolution value.
[MethodImpl(InliningOptions.ShortMethod)]
- public static void SetResolutionValues(ExifProfile exifProfile, PixelResolutionUnit unit, double horizontal, double vertical)
+ public static (ushort, double?, double?) AdjustToExif(PixelResolutionUnit unit, double horizontal, double vertical)
{
switch (unit)
{
@@ -114,30 +113,25 @@ namespace SixLabors.ImageSharp.Common.Helpers
case PixelResolutionUnit.PixelsPerCentimeter:
break;
case PixelResolutionUnit.PixelsPerMeter:
- {
- unit = PixelResolutionUnit.PixelsPerCentimeter;
- horizontal = UnitConverter.MeterToCm(horizontal);
- vertical = UnitConverter.MeterToCm(vertical);
- }
+ {
+ unit = PixelResolutionUnit.PixelsPerCentimeter;
+ horizontal = MeterToCm(horizontal);
+ vertical = MeterToCm(vertical);
+ }
- break;
+ break;
default:
unit = PixelResolutionUnit.PixelsPerInch;
break;
}
- exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)(unit + 1));
-
+ ushort exifUnit = (ushort)(unit + 1);
if (unit == PixelResolutionUnit.AspectRatio)
{
- exifProfile.RemoveValue(ExifTag.XResolution);
- exifProfile.RemoveValue(ExifTag.YResolution);
- }
- else
- {
- exifProfile.SetValue(ExifTag.XResolution, new Rational(horizontal));
- exifProfile.SetValue(ExifTag.YResolution, new Rational(vertical));
+ return (exifUnit, null, null);
}
+
+ return (exifUnit, horizontal, vertical);
}
}
}
diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
index 2273d759f0..9e7284aca8 100644
--- a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
+++ b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
@@ -74,6 +74,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff
///
private const TiffPhotometricInterpretation DefaultPhotometricInterpretation = TiffPhotometricInterpretation.Rgb;
+ private readonly List<(long, uint)> frameMarkers = new List<(long, uint)>();
+
///
/// Initializes a new instance of the class.
///
@@ -147,13 +149,30 @@ namespace SixLabors.ImageSharp.Formats.Tiff
// Make sure, the Encoder options makes sense in combination with each other.
this.SanitizeAndSetEncoderOptions(bitsPerPixel, image.PixelType.BitsPerPixel, photometricInterpretation, compression, predictor);
- using (var writer = new TiffStreamWriter(stream))
+ using var writer = new TiffStreamWriter(stream);
+ long ifdMarker = this.WriteHeader(writer);
+
+ Image metadataImage = image;
+ foreach (ImageFrame frame in image.Frames)
{
- long firstIfdMarker = this.WriteHeader(writer);
+ var subfileType = (TiffNewSubfileType)(frame.Metadata.ExifProfile.GetValue(ExifTag.SubfileType)?.Value ?? 0);
+
+ if (subfileType != TiffNewSubfileType.FullImage)
+ {
+ continue;
+ }
- // TODO: multiframing is not supported
- this.WriteImage(writer, image, firstIfdMarker);
+ ifdMarker = this.WriteFrame(writer, frame, image.Metadata, metadataImage, ifdMarker);
+ metadataImage = null;
}
+
+ long currentOffset = writer.BaseStream.Position;
+ foreach ((long, uint) marker in this.frameMarkers)
+ {
+ writer.WriteMarker(marker.Item1, marker.Item2);
+ }
+
+ writer.BaseStream.Seek(currentOffset, SeekOrigin.Begin);
}
///
@@ -174,41 +193,56 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// Writes all data required to define an image.
///
/// The pixel format.
- /// The to write data to.
- /// The to encode from.
+ /// The to write data to.
+ /// The tiff frame.
+ /// The image metadata (resolution values for each frame).
+ /// The image (common metadata for root frame).
/// The marker to write this IFD offset.
- private void WriteImage(TiffStreamWriter writer, Image image, long ifdOffset)
+ ///
+ /// The next IFD offset value.
+ ///
+ private long WriteFrame(
+ TiffStreamWriter writer,
+ ImageFrame frame,
+ ImageMetadata imageMetadata,
+ Image image,
+ long ifdOffset)
where TPixel : unmanaged, IPixel
{
- var entriesCollector = new TiffEncoderEntriesCollector();
-
using TiffBaseCompressor compressor = TiffCompressorFactory.Create(
this.CompressionType ?? TiffCompression.None,
writer.BaseStream,
this.memoryAllocator,
- image.Width,
+ frame.Width,
(int)this.BitsPerPixel,
this.compressionLevel,
this.HorizontalPredictor == TiffPredictor.Horizontal ? this.HorizontalPredictor.Value : TiffPredictor.None);
+ var entriesCollector = new TiffEncoderEntriesCollector();
using TiffBaseColorWriter colorWriter = TiffColorWriterFactory.Create(
this.PhotometricInterpretation,
- image.Frames.RootFrame,
+ frame,
this.quantizer,
this.memoryAllocator,
this.configuration,
entriesCollector,
(int)this.BitsPerPixel);
- int rowsPerStrip = this.CalcRowsPerStrip(image.Frames.RootFrame.Height, colorWriter.BytesPerRow);
+ int rowsPerStrip = this.CalcRowsPerStrip(frame.Height, colorWriter.BytesPerRow);
colorWriter.Write(compressor, rowsPerStrip);
+ if (image != null)
+ {
+ entriesCollector.ProcessMetadata(image);
+ }
+
+ entriesCollector.ProcessFrameInfo(frame, imageMetadata);
entriesCollector.ProcessImageFormat(this);
- entriesCollector.ProcessGeneral(image);
- writer.WriteMarker(ifdOffset, (uint)writer.Position);
- long nextIfdMarker = this.WriteIfd(writer, entriesCollector.Entries);
+ this.frameMarkers.Add((ifdOffset, (uint)writer.Position));
+
+ return this.WriteIfd(writer, entriesCollector.Entries);
}
///
@@ -272,7 +306,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
}
else
{
- var raw = new byte[length];
+ byte[] raw = new byte[length];
int sz = ExifWriter.WriteValue(entry, raw, 0);
DebugGuard.IsTrue(sz == raw.Length, "Incorrect number of bytes written");
largeDataBlocks.Add(raw);
diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs
index 43a0868496..b00dcc9673 100644
--- a/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs
+++ b/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs
@@ -6,7 +6,6 @@ using SixLabors.ImageSharp.Common.Helpers;
using SixLabors.ImageSharp.Formats.Tiff.Constants;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
-using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Tiff
{
@@ -16,9 +15,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff
public List Entries { get; } = new List();
- public void ProcessGeneral(Image image)
- where TPixel : unmanaged, IPixel
- => new GeneralProcessor(this).Process(image);
+ public void ProcessMetadata(Image image)
+ => new MetadataProcessor(this).Process(image);
+
+ public void ProcessFrameInfo(ImageFrame frame, ImageMetadata imageMetadata)
+ => new FrameInfoProcessor(this).Process(frame, imageMetadata);
public void ProcessImageFormat(TiffEncoderCore encoder)
=> new ImageFormatProcessor(this).Process(encoder);
@@ -38,44 +39,35 @@ namespace SixLabors.ImageSharp.Formats.Tiff
private void Add(IExifValue entry) => this.Entries.Add(entry);
- private class GeneralProcessor
+ private abstract class BaseProcessor
{
- private readonly TiffEncoderEntriesCollector collector;
+ public BaseProcessor(TiffEncoderEntriesCollector collector) => this.Collector = collector;
- public GeneralProcessor(TiffEncoderEntriesCollector collector) => this.collector = collector;
+ protected TiffEncoderEntriesCollector Collector { get; }
+ }
- public void Process(Image image)
- where TPixel : unmanaged, IPixel
+ private class MetadataProcessor : BaseProcessor
+ {
+ public MetadataProcessor(TiffEncoderEntriesCollector collector)
+ : base(collector)
{
- ImageFrame rootFrame = image.Frames.RootFrame;
- ExifProfile rootFrameExifProfile = rootFrame.Metadata.ExifProfile ?? new ExifProfile();
- byte[] rootFrameXmpBytes = rootFrame.Metadata.XmpProfile;
-
- var width = new ExifLong(ExifTagValue.ImageWidth)
- {
- Value = (uint)image.Width
- };
-
- var height = new ExifLong(ExifTagValue.ImageLength)
- {
- Value = (uint)image.Height
- };
-
- var software = new ExifString(ExifTagValue.Software)
- {
- Value = SoftwareValue
- };
+ }
- this.collector.AddOrReplace(width);
- this.collector.AddOrReplace(height);
+ public void Process(Image image)
+ {
+ ImageFrame rootFrame = image.Frames.RootFrame;
+ ExifProfile rootFrameExifProfile = rootFrame.Metadata.ExifProfile ?? new ExifProfile();
+ byte[] foorFrameXmpBytes = rootFrame.Metadata.XmpProfile;
- this.ProcessResolution(image.Metadata, rootFrameExifProfile);
- this.ProcessProfiles(image.Metadata, rootFrameExifProfile, rootFrameXmpBytes);
+ this.ProcessProfiles(image.Metadata, rootFrameExifProfile, foorFrameXmpBytes);
this.ProcessMetadata(rootFrameExifProfile);
- if (!this.collector.Entries.Exists(t => t.Tag == ExifTag.Software))
+ if (!this.Collector.Entries.Exists(t => t.Tag == ExifTag.Software))
{
- this.collector.Add(software);
+ this.Collector.Add(new ExifString(ExifTagValue.Software)
+ {
+ Value = SoftwareValue
+ });
}
}
@@ -114,26 +106,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff
}
}
- private void ProcessResolution(ImageMetadata imageMetadata, ExifProfile exifProfile)
- {
- UnitConverter.SetResolutionValues(
- exifProfile,
- imageMetadata.ResolutionUnits,
- imageMetadata.HorizontalResolution,
- imageMetadata.VerticalResolution);
-
- this.collector.Add(exifProfile.GetValue(ExifTag.ResolutionUnit).DeepClone());
-
- IExifValue xResolution = exifProfile.GetValue(ExifTag.XResolution)?.DeepClone();
- IExifValue yResolution = exifProfile.GetValue(ExifTag.YResolution)?.DeepClone();
-
- if (xResolution != null && yResolution != null)
- {
- this.collector.Add(xResolution);
- this.collector.Add(yResolution);
- }
- }
-
private void ProcessMetadata(ExifProfile exifProfile)
{
foreach (IExifValue entry in exifProfile.Values)
@@ -170,9 +142,9 @@ namespace SixLabors.ImageSharp.Formats.Tiff
break;
}
- if (!this.collector.Entries.Exists(t => t.Tag == entry.Tag))
+ if (!this.Collector.Entries.Exists(t => t.Tag == entry.Tag))
{
- this.collector.AddOrReplace(entry.DeepClone());
+ this.Collector.AddOrReplace(entry.DeepClone());
}
}
}
@@ -183,12 +155,12 @@ namespace SixLabors.ImageSharp.Formats.Tiff
{
foreach (IExifValue entry in exifProfile.Values)
{
- if (!this.collector.Entries.Exists(t => t.Tag == entry.Tag) && entry.GetValue() != null)
+ if (!this.Collector.Entries.Exists(t => t.Tag == entry.Tag) && entry.GetValue() != null)
{
ExifParts entryPart = ExifTags.GetPart(entry.Tag);
if (entryPart != ExifParts.None && exifProfile.Parts.HasFlag(entryPart))
{
- this.collector.AddOrReplace(entry.DeepClone());
+ this.Collector.AddOrReplace(entry.DeepClone());
}
}
}
@@ -206,7 +178,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
Value = imageMetadata.IptcProfile.Data
};
- this.collector.Add(iptc);
+ this.Collector.Add(iptc);
}
else
{
@@ -220,7 +192,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
Value = imageMetadata.IccProfile.ToByteArray()
};
- this.collector.Add(icc);
+ this.Collector.Add(icc);
}
else
{
@@ -234,7 +206,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
Value = xmpProfile
};
- this.collector.Add(xmp);
+ this.Collector.Add(xmp);
}
else
{
@@ -243,11 +215,61 @@ namespace SixLabors.ImageSharp.Formats.Tiff
}
}
- private class ImageFormatProcessor
+ private class FrameInfoProcessor : BaseProcessor
{
- private readonly TiffEncoderEntriesCollector collector;
+ public FrameInfoProcessor(TiffEncoderEntriesCollector collector)
+ : base(collector)
+ {
+ }
+
+ public void Process(ImageFrame frame, ImageMetadata imageMetadata)
+ {
+ this.Collector.AddOrReplace(new ExifLong(ExifTagValue.ImageWidth)
+ {
+ Value = (uint)frame.Width
+ });
+
+ this.Collector.AddOrReplace(new ExifLong(ExifTagValue.ImageLength)
+ {
+ Value = (uint)frame.Height
+ });
+
+ this.ProcessResolution(imageMetadata);
+ }
+
+ private void ProcessResolution(ImageMetadata imageMetadata)
+ {
+ (ushort, double?, double?) exifValues = UnitConverter.AdjustToExif(
+ imageMetadata.ResolutionUnits,
+ imageMetadata.HorizontalResolution,
+ imageMetadata.VerticalResolution);
- public ImageFormatProcessor(TiffEncoderEntriesCollector collector) => this.collector = collector;
+ this.Collector.Add(new ExifShort(ExifTagValue.ResolutionUnit)
+ {
+ Value = exifValues.Item1
+ });
+
+ if (exifValues.Item2 != null && exifValues.Item3 != null)
+ {
+ this.Collector.Add(new ExifRational(ExifTagValue.XResolution)
+ {
+ Value = new Rational(exifValues.Item2.Value)
+ });
+
+ this.Collector.Add(new ExifRational(ExifTagValue.YResolution)
+ {
+ Value = new Rational(exifValues.Item3.Value)
+ });
+ }
+ }
+ }
+
+ private class ImageFormatProcessor : BaseProcessor
+ {
+ public ImageFormatProcessor(TiffEncoderEntriesCollector collector)
+ : base(collector)
+ {
+ }
public void Process(TiffEncoderCore encoder)
{
@@ -278,11 +300,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff
Value = (ushort)encoder.PhotometricInterpretation
};
- this.collector.AddOrReplace(planarConfig);
- this.collector.AddOrReplace(samplesPerPixel);
- this.collector.AddOrReplace(bitPerSample);
- this.collector.AddOrReplace(compression);
- this.collector.AddOrReplace(photometricInterpretation);
+ this.Collector.AddOrReplace(planarConfig);
+ this.Collector.AddOrReplace(samplesPerPixel);
+ this.Collector.AddOrReplace(bitPerSample);
+ this.Collector.AddOrReplace(compression);
+ this.Collector.AddOrReplace(photometricInterpretation);
if (encoder.HorizontalPredictor == TiffPredictor.Horizontal)
{
@@ -292,7 +314,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
{
var predictor = new ExifShort(ExifTagValue.Predictor) { Value = (ushort)TiffPredictor.Horizontal };
- this.collector.AddOrReplace(predictor);
+ this.Collector.AddOrReplace(predictor);
}
}
}
diff --git a/src/ImageSharp/Formats/Tiff/Writers/TiffStreamWriter.cs b/src/ImageSharp/Formats/Tiff/Writers/TiffStreamWriter.cs
index 05a1ca7a24..8c83f41ccf 100644
--- a/src/ImageSharp/Formats/Tiff/Writers/TiffStreamWriter.cs
+++ b/src/ImageSharp/Formats/Tiff/Writers/TiffStreamWriter.cs
@@ -126,10 +126,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers
/// The four-byte unsigned integer to write.
public void WriteMarker(long offset, uint value)
{
- long currentOffset = this.BaseStream.Position;
this.BaseStream.Seek(offset, SeekOrigin.Begin);
this.Write(value);
- this.BaseStream.Seek(currentOffset, SeekOrigin.Begin);
}
///