mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
44 changed files with 1008 additions and 252 deletions
@ -0,0 +1,21 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Common.Helpers |
||||
|
{ |
||||
|
internal readonly struct ExifResolutionValues |
||||
|
{ |
||||
|
public ExifResolutionValues(ushort resolutionUnit, double? horizontalResolution, double? verticalResolution) |
||||
|
{ |
||||
|
this.ResolutionUnit = resolutionUnit; |
||||
|
this.HorizontalResolution = horizontalResolution; |
||||
|
this.VerticalResolution = verticalResolution; |
||||
|
} |
||||
|
|
||||
|
public ushort ResolutionUnit { get; } |
||||
|
|
||||
|
public double? HorizontalResolution { get; } |
||||
|
|
||||
|
public double? VerticalResolution { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'BlackIsZero' photometric interpretation for 16-bit grayscale images.
|
||||
|
/// </summary>
|
||||
|
internal class BlackIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="BlackIsZero16TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public BlackIsZero16TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
L16 l16 = TiffUtils.L16Default; |
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ushort intensity = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2)); |
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ushort intensity = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2)); |
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,71 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'RGB' photometric interpretation with 16 bits for each channel.
|
||||
|
/// </summary>
|
||||
|
internal class Rgb161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Rgb161616TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public Rgb161616TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
Rgba64 rgba = TiffUtils.Rgba64Default; |
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
|
||||
|
int offset = 0; |
||||
|
|
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
|
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong r = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2)); |
||||
|
offset += 2; |
||||
|
ulong g = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2)); |
||||
|
offset += 2; |
||||
|
ulong b = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2)); |
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong r = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2)); |
||||
|
offset += 2; |
||||
|
ulong g = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2)); |
||||
|
offset += 2; |
||||
|
ulong b = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2)); |
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,72 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Buffers; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'RGB' photometric interpretation with 'Planar' layout for all 16 bit.
|
||||
|
/// </summary>
|
||||
|
internal class Rgb16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Rgb16PlanarTiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public Rgb16PlanarTiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
Rgba64 rgba = TiffUtils.Rgba64Default; |
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
|
||||
|
Span<byte> redData = data[0].GetSpan(); |
||||
|
Span<byte> greenData = data[1].GetSpan(); |
||||
|
Span<byte> blueData = data[2].GetSpan(); |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong r = TiffUtils.ConvertToShortBigEndian(redData.Slice(offset, 2)); |
||||
|
ulong g = TiffUtils.ConvertToShortBigEndian(greenData.Slice(offset, 2)); |
||||
|
ulong b = TiffUtils.ConvertToShortBigEndian(blueData.Slice(offset, 2)); |
||||
|
|
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong r = TiffUtils.ConvertToShortLittleEndian(redData.Slice(offset, 2)); |
||||
|
ulong g = TiffUtils.ConvertToShortLittleEndian(greenData.Slice(offset, 2)); |
||||
|
ulong b = TiffUtils.ConvertToShortLittleEndian(blueData.Slice(offset, 2)); |
||||
|
|
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Buffers; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The base class for planar color decoders.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
||||
|
internal abstract class TiffBasePlanarColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Decodes source raw pixel data using the current photometric interpretation.
|
||||
|
/// </summary>
|
||||
|
/// <param name="data">The buffers to read image data from.</param>
|
||||
|
/// <param name="pixels">The image buffer to write pixels to.</param>
|
||||
|
/// <param name="left">The x-coordinate of the left-hand side of the image block.</param>
|
||||
|
/// <param name="top">The y-coordinate of the top of the image block.</param>
|
||||
|
/// <param name="width">The width of the image block.</param>
|
||||
|
/// <param name="height">The height of the image block.</param>
|
||||
|
public abstract void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'WhiteIsZero' photometric interpretation for 16-bit grayscale images.
|
||||
|
/// </summary>
|
||||
|
internal class WhiteIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="WhiteIsZero16TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public WhiteIsZero16TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
L16 l16 = TiffUtils.L16Default; |
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2))); |
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2))); |
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,58 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Buffers.Binary; |
||||
|
using System.Numerics; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.Utils |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Helper methods for TIFF decoding.
|
||||
|
/// </summary>
|
||||
|
internal static class TiffUtils |
||||
|
{ |
||||
|
public static Vector4 Vector4Default { get; } = new Vector4(0.0f, 0.0f, 0.0f, 0.0f); |
||||
|
|
||||
|
public static Rgba64 Rgba64Default { get; } = new Rgba64(0, 0, 0, 0); |
||||
|
|
||||
|
public static L16 L16Default { get; } = new L16(0); |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static ushort ConvertToShortBigEndian(ReadOnlySpan<byte> buffer) => |
||||
|
BinaryPrimitives.ReadUInt16BigEndian(buffer); |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static ushort ConvertToShortLittleEndian(ReadOnlySpan<byte> buffer) => |
||||
|
BinaryPrimitives.ReadUInt16LittleEndian(buffer); |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel ColorFromL8<TPixel>(L8 l8, byte intensity, TPixel color) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
l8.PackedValue = intensity; |
||||
|
color.FromL8(l8); |
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel ColorFromL16<TPixel>(L16 l16, ushort intensity, TPixel color) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
l16.PackedValue = intensity; |
||||
|
color.FromL16(l16); |
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel ColorFromRgba64<TPixel>(Rgba64 rgba, ulong r, ulong g, ulong b, TPixel color) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48); |
||||
|
color.FromRgba64(rgba); |
||||
|
return color; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,114 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.IO; |
||||
|
|
||||
|
using SixLabors.ImageSharp.Formats; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Constants; |
||||
|
using SixLabors.ImageSharp.Metadata.Profiles.Exif; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; |
||||
|
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; |
||||
|
|
||||
|
using Xunit; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.Formats.Tiff |
||||
|
{ |
||||
|
[Trait("Format", "Tiff")] |
||||
|
public abstract class TiffEncoderBaseTester |
||||
|
{ |
||||
|
protected static readonly IImageDecoder ReferenceDecoder = new MagickReferenceDecoder(); |
||||
|
|
||||
|
protected static void TestStripLength<TPixel>( |
||||
|
TestImageProvider<TPixel> provider, |
||||
|
TiffPhotometricInterpretation photometricInterpretation, |
||||
|
TiffCompression compression, |
||||
|
bool useExactComparer = true, |
||||
|
float compareTolerance = 0.01f) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
// arrange
|
||||
|
var tiffEncoder = new TiffEncoder() { PhotometricInterpretation = photometricInterpretation, Compression = compression }; |
||||
|
using Image<TPixel> input = provider.GetImage(); |
||||
|
using var memStream = new MemoryStream(); |
||||
|
TiffFrameMetadata inputMeta = input.Frames.RootFrame.Metadata.GetTiffMetadata(); |
||||
|
TiffCompression inputCompression = inputMeta.Compression ?? TiffCompression.None; |
||||
|
|
||||
|
// act
|
||||
|
input.Save(memStream, tiffEncoder); |
||||
|
|
||||
|
// assert
|
||||
|
memStream.Position = 0; |
||||
|
using var output = Image.Load<Rgba32>(memStream); |
||||
|
ExifProfile exifProfileOutput = output.Frames.RootFrame.Metadata.ExifProfile; |
||||
|
TiffFrameMetadata outputMeta = output.Frames.RootFrame.Metadata.GetTiffMetadata(); |
||||
|
ImageFrame<Rgba32> rootFrame = output.Frames.RootFrame; |
||||
|
|
||||
|
Number rowsPerStrip = exifProfileOutput.GetValue(ExifTag.RowsPerStrip) != null ? exifProfileOutput.GetValue(ExifTag.RowsPerStrip).Value : TiffConstants.RowsPerStripInfinity; |
||||
|
Assert.True(output.Height > (int)rowsPerStrip); |
||||
|
Assert.True(exifProfileOutput.GetValue(ExifTag.StripOffsets)?.Value.Length > 1); |
||||
|
Number[] stripByteCounts = exifProfileOutput.GetValue(ExifTag.StripByteCounts)?.Value; |
||||
|
Assert.NotNull(stripByteCounts); |
||||
|
Assert.True(stripByteCounts.Length > 1); |
||||
|
Assert.NotNull(outputMeta.BitsPerPixel); |
||||
|
|
||||
|
foreach (Number sz in stripByteCounts) |
||||
|
{ |
||||
|
Assert.True((uint)sz <= TiffConstants.DefaultStripSize); |
||||
|
} |
||||
|
|
||||
|
// For uncompressed more accurate test.
|
||||
|
if (compression == TiffCompression.None) |
||||
|
{ |
||||
|
for (int i = 0; i < stripByteCounts.Length - 1; i++) |
||||
|
{ |
||||
|
// The difference must be less than one row.
|
||||
|
int stripBytes = (int)stripByteCounts[i]; |
||||
|
int widthBytes = ((int)outputMeta.BitsPerPixel + 7) / 8 * rootFrame.Width; |
||||
|
|
||||
|
Assert.True((TiffConstants.DefaultStripSize - stripBytes) < widthBytes); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Compare with reference.
|
||||
|
TestTiffEncoderCore( |
||||
|
provider, |
||||
|
inputMeta.BitsPerPixel, |
||||
|
photometricInterpretation, |
||||
|
inputCompression, |
||||
|
useExactComparer: useExactComparer, |
||||
|
compareTolerance: compareTolerance); |
||||
|
} |
||||
|
|
||||
|
protected static void TestTiffEncoderCore<TPixel>( |
||||
|
TestImageProvider<TPixel> provider, |
||||
|
TiffBitsPerPixel? bitsPerPixel, |
||||
|
TiffPhotometricInterpretation photometricInterpretation, |
||||
|
TiffCompression compression = TiffCompression.None, |
||||
|
TiffPredictor predictor = TiffPredictor.None, |
||||
|
bool useExactComparer = true, |
||||
|
float compareTolerance = 0.001f, |
||||
|
IImageDecoder imageDecoder = null) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
using Image<TPixel> image = provider.GetImage(); |
||||
|
var encoder = new TiffEncoder |
||||
|
{ |
||||
|
PhotometricInterpretation = photometricInterpretation, |
||||
|
BitsPerPixel = bitsPerPixel, |
||||
|
Compression = compression, |
||||
|
HorizontalPredictor = predictor |
||||
|
}; |
||||
|
|
||||
|
// Does DebugSave & load reference CompareToReferenceInput():
|
||||
|
image.VerifyEncoder( |
||||
|
provider, |
||||
|
"tiff", |
||||
|
bitsPerPixel, |
||||
|
encoder, |
||||
|
useExactComparer ? ImageComparer.Exact : ImageComparer.Tolerant(compareTolerance), |
||||
|
referenceDecoder: imageDecoder ?? ReferenceDecoder); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,179 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Constants; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; |
||||
|
using Xunit; |
||||
|
|
||||
|
using static SixLabors.ImageSharp.Tests.TestImages.Tiff; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.Formats.Tiff |
||||
|
{ |
||||
|
[Trait("Format", "Tiff")] |
||||
|
public class TiffEncoderMultiframeTests : TiffEncoderBaseTester |
||||
|
{ |
||||
|
[Theory] |
||||
|
[WithFile(MultiframeLzwPredictor, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_Works<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> => TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit24, TiffPhotometricInterpretation.Rgb); |
||||
|
|
||||
|
[Theory] |
||||
|
[WithFile(MultiframeDifferentSize, PixelTypes.Rgba32)] |
||||
|
[WithFile(MultiframeDifferentVariants, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_NotSupport<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> => Assert.Throws<NotSupportedException>(() => TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit24, TiffPhotometricInterpretation.Rgb)); |
||||
|
|
||||
|
[Theory] |
||||
|
[WithFile(MultiframeDeflateWithPreview, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_WithPreview<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> => TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit24, TiffPhotometricInterpretation.Rgb); |
||||
|
|
||||
|
[Theory] |
||||
|
[WithFile(TestImages.Gif.Receipt, PixelTypes.Rgb24)] |
||||
|
[WithFile(TestImages.Gif.Issues.BadDescriptorWidth, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_Convert<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> => TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit48, TiffPhotometricInterpretation.Rgb); |
||||
|
|
||||
|
[Theory] |
||||
|
[WithFile(MultiframeLzwPredictor, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_RemoveFrames<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
using Image<TPixel> image = provider.GetImage(); |
||||
|
Assert.True(image.Frames.Count > 1); |
||||
|
|
||||
|
image.Frames.RemoveFrame(0); |
||||
|
|
||||
|
TiffBitsPerPixel bitsPerPixel = TiffBitsPerPixel.Bit24; |
||||
|
var encoder = new TiffEncoder |
||||
|
{ |
||||
|
PhotometricInterpretation = TiffPhotometricInterpretation.Rgb, |
||||
|
BitsPerPixel = bitsPerPixel, |
||||
|
Compression = TiffCompression.Deflate |
||||
|
}; |
||||
|
|
||||
|
image.VerifyEncoder( |
||||
|
provider, |
||||
|
"tiff", |
||||
|
bitsPerPixel, |
||||
|
encoder, |
||||
|
ImageComparer.Exact); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[WithFile(TestImages.Png.Bike, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_AddFrames<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
using Image<TPixel> image = provider.GetImage(); |
||||
|
Assert.Equal(1, image.Frames.Count); |
||||
|
|
||||
|
using var image1 = new Image<Rgba32>(image.Width, image.Height, Color.Green.ToRgba32()); |
||||
|
|
||||
|
using var image2 = new Image<Rgba32>(image.Width, image.Height, Color.Yellow.ToRgba32()); |
||||
|
|
||||
|
image.Frames.AddFrame(image1.Frames.RootFrame); |
||||
|
image.Frames.AddFrame(image2.Frames.RootFrame); |
||||
|
|
||||
|
TiffBitsPerPixel bitsPerPixel = TiffBitsPerPixel.Bit24; |
||||
|
var encoder = new TiffEncoder |
||||
|
{ |
||||
|
PhotometricInterpretation = TiffPhotometricInterpretation.Rgb, |
||||
|
BitsPerPixel = bitsPerPixel, |
||||
|
Compression = TiffCompression.Deflate |
||||
|
}; |
||||
|
|
||||
|
using (var ms = new System.IO.MemoryStream()) |
||||
|
{ |
||||
|
image.Save(ms, encoder); |
||||
|
|
||||
|
ms.Position = 0; |
||||
|
using var output = Image.Load<Rgba32>(ms); |
||||
|
|
||||
|
Assert.Equal(3, output.Frames.Count); |
||||
|
|
||||
|
ImageFrame<Rgba32> frame1 = output.Frames[1]; |
||||
|
ImageFrame<Rgba32> frame2 = output.Frames[2]; |
||||
|
|
||||
|
Assert.Equal(Color.Green.ToRgba32(), frame1[10, 10]); |
||||
|
Assert.Equal(Color.Yellow.ToRgba32(), frame2[10, 10]); |
||||
|
|
||||
|
Assert.Equal(TiffCompression.Deflate, frame1.Metadata.GetTiffMetadata().Compression); |
||||
|
Assert.Equal(TiffCompression.Deflate, frame1.Metadata.GetTiffMetadata().Compression); |
||||
|
|
||||
|
Assert.Equal(TiffPhotometricInterpretation.Rgb, frame1.Metadata.GetTiffMetadata().PhotometricInterpretation); |
||||
|
Assert.Equal(TiffPhotometricInterpretation.Rgb, frame2.Metadata.GetTiffMetadata().PhotometricInterpretation); |
||||
|
} |
||||
|
|
||||
|
image.VerifyEncoder( |
||||
|
provider, |
||||
|
"tiff", |
||||
|
bitsPerPixel, |
||||
|
encoder, |
||||
|
ImageComparer.Exact); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[WithBlankImages(100, 100, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_Create<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
using Image<TPixel> image = provider.GetImage(); |
||||
|
|
||||
|
using var image0 = new Image<Rgba32>(image.Width, image.Height, Color.Red.ToRgba32()); |
||||
|
|
||||
|
using var image1 = new Image<Rgba32>(image.Width, image.Height, Color.Green.ToRgba32()); |
||||
|
|
||||
|
using var image2 = new Image<Rgba32>(image.Width, image.Height, Color.Yellow.ToRgba32()); |
||||
|
|
||||
|
image.Frames.AddFrame(image0.Frames.RootFrame); |
||||
|
image.Frames.AddFrame(image1.Frames.RootFrame); |
||||
|
image.Frames.AddFrame(image2.Frames.RootFrame); |
||||
|
image.Frames.RemoveFrame(0); |
||||
|
|
||||
|
TiffBitsPerPixel bitsPerPixel = TiffBitsPerPixel.Bit8; |
||||
|
var encoder = new TiffEncoder |
||||
|
{ |
||||
|
PhotometricInterpretation = TiffPhotometricInterpretation.PaletteColor, |
||||
|
BitsPerPixel = bitsPerPixel, |
||||
|
Compression = TiffCompression.Lzw |
||||
|
}; |
||||
|
|
||||
|
using (var ms = new System.IO.MemoryStream()) |
||||
|
{ |
||||
|
image.Save(ms, encoder); |
||||
|
|
||||
|
ms.Position = 0; |
||||
|
using var output = Image.Load<Rgba32>(ms); |
||||
|
|
||||
|
Assert.Equal(3, output.Frames.Count); |
||||
|
|
||||
|
ImageFrame<Rgba32> frame0 = output.Frames[0]; |
||||
|
ImageFrame<Rgba32> frame1 = output.Frames[1]; |
||||
|
ImageFrame<Rgba32> frame2 = output.Frames[2]; |
||||
|
|
||||
|
Assert.Equal(Color.Red.ToRgba32(), frame0[10, 10]); |
||||
|
Assert.Equal(Color.Green.ToRgba32(), frame1[10, 10]); |
||||
|
Assert.Equal(Color.Yellow.ToRgba32(), frame2[10, 10]); |
||||
|
|
||||
|
Assert.Equal(TiffCompression.Lzw, frame0.Metadata.GetTiffMetadata().Compression); |
||||
|
Assert.Equal(TiffCompression.Lzw, frame1.Metadata.GetTiffMetadata().Compression); |
||||
|
Assert.Equal(TiffCompression.Lzw, frame1.Metadata.GetTiffMetadata().Compression); |
||||
|
|
||||
|
Assert.Equal(TiffPhotometricInterpretation.PaletteColor, frame0.Metadata.GetTiffMetadata().PhotometricInterpretation); |
||||
|
Assert.Equal(TiffPhotometricInterpretation.PaletteColor, frame1.Metadata.GetTiffMetadata().PhotometricInterpretation); |
||||
|
Assert.Equal(TiffPhotometricInterpretation.PaletteColor, frame2.Metadata.GetTiffMetadata().PhotometricInterpretation); |
||||
|
} |
||||
|
|
||||
|
image.VerifyEncoder( |
||||
|
provider, |
||||
|
"tiff", |
||||
|
bitsPerPixel, |
||||
|
encoder, |
||||
|
ImageComparer.Exact); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:c734dd489c65fb77bd7a35cd663aa16ce986df2c2ab8c7ca43d8b65db9d47c03 |
||||
|
size 6666162 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:c3806304a5453a6ec8a6795bc77b967b9aa8593288af36bbf9802f22ee27869e |
||||
|
size 6588 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:d8f2c2afd8f1645717087bd2edbc3e8a46b88a54a4996c0e9350fdd652b5c382 |
||||
|
size 6588 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:435c92b453587e1943940111b66afabf70307beb0e1d65e9701fd9bb753eead2 |
||||
|
size 6588 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:0951a9c2207eb6864b6a19ec8513a28a874adddb37c3c06b9fd07831372924e3 |
||||
|
size 19150 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:46a60552a7ff37f2c16c43e030e7180872af712f5d9c9c7673e2547049af3da9 |
||||
|
size 19168 |
||||
Loading…
Reference in new issue