Browse Source

Add support for decoding tiff with webp compressed data

pull/2128/head
Brian Popow 4 years ago
parent
commit
b6e09b5bb4
  1. 55
      src/ImageSharp/Formats/Tiff/Compression/Decompressors/WebpTiffCompression.cs
  2. 5
      src/ImageSharp/Formats/Tiff/Compression/TiffDecoderCompressionType.cs
  3. 4
      src/ImageSharp/Formats/Tiff/Compression/TiffDecompressorsFactory.cs
  4. 8
      src/ImageSharp/Formats/Tiff/Constants/TiffCompression.cs
  5. 6
      src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs

55
src/ImageSharp/Formats/Tiff/Compression/Decompressors/WebpTiffCompression.cs

@ -0,0 +1,55 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Formats.Tiff.Constants;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors
{
/// <summary>
/// Class to handle cases where TIFF image data is compressed as a webp stream.
/// </summary>
internal class WebpTiffCompression : TiffBaseDecompressor
{
/// <summary>
/// Initializes a new instance of the <see cref="WebpTiffCompression"/> class.
/// </summary>
/// <param name="memoryAllocator">The memory allocator.</param>
/// <param name="width">The width of the image.</param>
/// <param name="bitsPerPixel">The bits per pixel.</param>
/// <param name="predictor">The predictor.</param>
public WebpTiffCompression(MemoryAllocator memoryAllocator, int width, int bitsPerPixel, TiffPredictor predictor = TiffPredictor.None)
: base(memoryAllocator, width, bitsPerPixel, predictor)
{
}
/// <inheritdoc/>
protected override void Decompress(BufferedReadStream stream, int byteCount, int stripHeight, Span<byte> buffer)
{
using var image = Image.Load<Rgb24>(stream, new WebpDecoder());
CopyImageBytesToBuffer(buffer, image.Frames.RootFrame.PixelBuffer);
}
private static void CopyImageBytesToBuffer(Span<byte> buffer, Buffer2D<Rgb24> pixelBuffer)
{
int offset = 0;
for (int y = 0; y < pixelBuffer.Height; y++)
{
Span<Rgb24> pixelRowSpan = pixelBuffer.DangerousGetRowSpan(y);
Span<byte> rgbBytes = MemoryMarshal.AsBytes(pixelRowSpan);
rgbBytes.CopyTo(buffer.Slice(offset));
offset += rgbBytes.Length;
}
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
}
}

5
src/ImageSharp/Formats/Tiff/Compression/TiffDecoderCompressionType.cs

@ -47,5 +47,10 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression
/// The image data is compressed as a JPEG stream.
/// </summary>
Jpeg = 7,
/// <summary>
/// The image data is compressed as a WEBP stream.
/// </summary>
Webp = 8,
}
}

4
src/ImageSharp/Formats/Tiff/Compression/TiffDecompressorsFactory.cs

@ -60,6 +60,10 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Predictor should only be used with lzw or deflate compression");
return new JpegTiffCompression(configuration, allocator, width, bitsPerPixel, jpegTables, photometricInterpretation);
case TiffDecoderCompressionType.Webp:
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Predictor should only be used with lzw or deflate compression");
return new WebpTiffCompression(allocator, width, bitsPerPixel);
default:
throw TiffThrowHelper.NotSupportedDecompressor(nameof(method));
}

8
src/ImageSharp/Formats/Tiff/Constants/TiffCompression.cs

@ -103,5 +103,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Constants
/// if this is chosen.
/// </summary>
OldDeflate = 32946,
/// <summary>
/// Pixel data is compressed with webp encoder.
///
/// Note: The TIFF encoder does not support this compression and will default to use no compression instead,
/// if this is chosen.
/// </summary>
Webp = 50001,
}
}

6
src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs

@ -470,6 +470,12 @@ namespace SixLabors.ImageSharp.Formats.Tiff
break;
}
case TiffCompression.Webp:
{
options.CompressionType = TiffDecoderCompressionType.Webp;
break;
}
default:
{
TiffThrowHelper.ThrowNotSupported($"The specified TIFF compression format '{compression}' is not supported");

Loading…
Cancel
Save