From ae34c3ceca3987bfc5042c515c84a601b2c26278 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Wed, 25 Nov 2020 10:32:40 +0100 Subject: [PATCH] Add Tiff EncodeAsync --- src/ImageSharp/Formats/Tiff/README.md | 26 +++++++++---------- src/ImageSharp/Formats/Tiff/TiffEncoder.cs | 3 ++- .../Formats/Tiff/TiffEncoderCore.cs | 17 ++++++++---- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/README.md b/src/ImageSharp/Formats/Tiff/README.md index 636e08a32e..0343d0a466 100644 --- a/src/ImageSharp/Formats/Tiff/README.md +++ b/src/ImageSharp/Formats/Tiff/README.md @@ -40,7 +40,7 @@ | |Encoder|Decoder|Comments | |---------------------------|:-----:|:-----:|--------------------------| -|None | | Y | | +|None | Y | Y | encoding only rgb so far | |Ccitt1D | | Y | | |PackBits | | Y | | |CcittGroup3Fax | | Y | | @@ -58,7 +58,7 @@ |WhiteIsZero | | Y | General + 1/4/8-bit optimised implementations | |BlackIsZero | | Y | General + 1/4/8-bit optimised implementations | |Rgb (Chunky) | | Y | General + Rgb888 optimised implementation | -|Rgb (Planar) | | Y | General implementation only | +|Rgb (Planar) | Y | Y | General implementation only | |PaletteColor | | Y | General implementation only | |TransparencyMask | | | | |Separated (TIFF Extension) | | | | @@ -72,34 +72,34 @@ |---------------------------|:-----:|:-----:|--------------------------| |NewSubfileType | | | | |SubfileType | | | | -|ImageWidth | | Y | | -|ImageLength | | Y | | -|BitsPerSample | | Y | | +|ImageWidth | Y | Y | | +|ImageLength | Y | Y | | +|BitsPerSample | Y | Y | | |Compression | | Y | | -|PhotometricInterpretation | | Y | | -|Threshholding | | | | +|PhotometricInterpretation | Y | Y | | +|Thresholding | | | | |CellWidth | | | | |CellLength | | | | |FillOrder | | - | Ignore. In practice is very uncommon, and is not recommended. | |ImageDescription | | Y | | |Make | | Y | | |Model | | Y | | -|StripOffsets | | Y | | +|StripOffsets | Y | Y | | |Orientation | | - | Ignore. Many readers ignore this tag. | -|SamplesPerPixel | | - | Currently ignored, as can be inferred from count of BitsPerSample | +|SamplesPerPixel | Y | - | Currently ignored, as can be inferred from count of BitsPerSample | |RowsPerStrip | | Y | | -|StripByteCounts | | Y | | +|StripByteCounts | Y | Y | | |MinSampleValue | | | | |MaxSampleValue | | | | -|XResolution | | Y | | -|YResolution | | Y | | +|XResolution | Y | Y | | +|YResolution | Y | Y | | |PlanarConfiguration | | Y | | |FreeOffsets | | | | |FreeByteCounts | | | | |GrayResponseUnit | | | | |GrayResponseCurve | | | | |ResolutionUnit | | Y | | -|Software | | Y | | +|Software | Y | Y | | |DateTime | | Y | | |Artist | | Y | | |HostComputer | | Y | | diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoder.cs b/src/ImageSharp/Formats/Tiff/TiffEncoder.cs index 18c0d12a0f..17ed52182a 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoder.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoder.cs @@ -26,7 +26,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff public Task EncodeAsync(Image image, Stream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - throw new System.NotImplementedException(); + var encoder = new TiffEncoderCore(this, image.GetMemoryAllocator()); + return encoder.EncodeAsync(image, stream, cancellationToken); } } } diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs index 0350f42a47..250fb23895 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Threading; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Metadata.Profiles.Exif; @@ -14,7 +15,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff /// /// Performs the TIFF encoding operation. /// - internal sealed class TiffEncoderCore + internal sealed class TiffEncoderCore : IImageEncoderInternals { /// /// The amount to pad each row by in bytes. @@ -39,7 +40,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff public TiffEncoderCore(ITiffEncoderOptions options, MemoryAllocator memoryAllocator) { this.memoryAllocator = memoryAllocator; - options = options ?? new TiffEncoder(); } /// @@ -58,7 +58,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff /// The pixel format. /// The to encode from. /// The to encode the image data to. - public void Encode(Image image, Stream stream) + /// The token to request cancellation. + public void Encode(Image image, Stream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { Guard.NotNull(image, nameof(image)); @@ -221,7 +222,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff var stripOffsets = new ExifLongArray(ExifTagValue.StripOffsets) { // TODO: we only write one image strip for the start. - Value = new uint[] { imageDataStartOffset } + Value = new[] { imageDataStartOffset } }; var samplesPerPixel = new ExifLong(ExifTagValue.SamplesPerPixel) @@ -237,7 +238,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff var stripByteCounts = new ExifLongArray(ExifTagValue.StripByteCounts) { - Value = new[] { (uint)(imageDataBytes) } + Value = new[] { (uint)imageDataBytes } }; var xResolution = new ExifRational(ExifTagValue.XResolution) @@ -258,6 +259,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff Value = 0 }; + var software = new ExifString(ExifTagValue.Software) + { + Value = "ImageSharp" + }; + ifdEntries.Add(width); ifdEntries.Add(height); ifdEntries.Add(bitPerSample); @@ -270,6 +276,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff ifdEntries.Add(xResolution); ifdEntries.Add(yResolution); ifdEntries.Add(resolutionUnit); + ifdEntries.Add(software); } } }