Browse Source

Implement encoding multi-frame tiff images

111
pull/1686/head
Ildar Khayrutdinov 5 years ago
parent
commit
f1d9188253
  1. 28
      src/ImageSharp/Common/Helpers/UnitConverter.cs
  2. 66
      src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
  3. 160
      src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs
  4. 2
      src/ImageSharp/Formats/Tiff/Writers/TiffStreamWriter.cs

28
src/ImageSharp/Common/Helpers/UnitConverter.cs

@ -100,12 +100,11 @@ namespace SixLabors.ImageSharp.Common.Helpers
/// <summary>
/// Sets the exif profile resolution values.
/// </summary>
/// <param name="exifProfile">The exif profile.</param>
/// <param name="unit">The resolution unit.</param>
/// <param name="horizontal">The horizontal resolution value.</param>
/// <param name="vertical">The vertical resolution value.</param>
[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);
}
}
}

66
src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs

@ -74,6 +74,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// </summary>
private const TiffPhotometricInterpretation DefaultPhotometricInterpretation = TiffPhotometricInterpretation.Rgb;
private readonly List<(long, uint)> frameMarkers = new List<(long, uint)>();
/// <summary>
/// Initializes a new instance of the <see cref="TiffEncoderCore"/> class.
/// </summary>
@ -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<TPixel> metadataImage = image;
foreach (ImageFrame<TPixel> 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);
}
/// <summary>
@ -174,41 +193,56 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// Writes all data required to define an image.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="writer">The <see cref="BinaryWriter"/> to write data to.</param>
/// <param name="image">The <see cref="Image{TPixel}"/> to encode from.</param>
/// <param name="writer">The <see cref="BinaryWriter" /> to write data to.</param>
/// <param name="frame">The tiff frame.</param>
/// <param name="imageMetadata">The image metadata (resolution values for each frame).</param>
/// <param name="image">The image (common metadata for root frame).</param>
/// <param name="ifdOffset">The marker to write this IFD offset.</param>
private void WriteImage<TPixel>(TiffStreamWriter writer, Image<TPixel> image, long ifdOffset)
/// <returns>
/// The next IFD offset value.
/// </returns>
private long WriteFrame<TPixel>(
TiffStreamWriter writer,
ImageFrame<TPixel> frame,
ImageMetadata imageMetadata,
Image<TPixel> image,
long ifdOffset)
where TPixel : unmanaged, IPixel<TPixel>
{
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<TPixel> 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);
}
/// <summary>
@ -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);

160
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<IExifValue> Entries { get; } = new List<IExifValue>();
public void ProcessGeneral<TPixel>(Image<TPixel> image)
where TPixel : unmanaged, IPixel<TPixel>
=> 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<TPixel>(Image<TPixel> image)
where TPixel : unmanaged, IPixel<TPixel>
private class MetadataProcessor : BaseProcessor
{
public MetadataProcessor(TiffEncoderEntriesCollector collector)
: base(collector)
{
ImageFrame<TPixel> 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);
}
}
}

2
src/ImageSharp/Formats/Tiff/Writers/TiffStreamWriter.cs

@ -126,10 +126,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers
/// <param name="value">The four-byte unsigned integer to write.</param>
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);
}
/// <summary>

Loading…
Cancel
Save