Browse Source

Merge pull request #1647 from SixLabors/bp/tiff12bit

Add support for decoding 6, 12, 30, 42 bits per pixel tiff's
pull/1655/head
Brian Popow 5 years ago
committed by GitHub
parent
commit
4d7e8298df
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs
  2. 60
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb444TiffColor{TPixel}.cs
  3. 1
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs
  4. 44
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs
  5. 20
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs
  6. 28
      src/ImageSharp/Formats/Tiff/TiffBitsPerPixel.cs
  7. 20
      src/ImageSharp/Formats/Tiff/TiffBitsPerSample.cs
  8. 40
      src/ImageSharp/Formats/Tiff/TiffBitsPerSampleExtensions.cs
  9. 2
      src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs
  10. 31
      src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs
  11. 9
      src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
  12. 11
      src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs
  13. 32
      tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
  14. 32
      tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs
  15. 5
      tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs
  16. 8
      tests/ImageSharp.Tests/TestImages.cs
  17. 9
      tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs
  18. 3
      tests/Images/Input/Tiff/flower-rgb-contig-02.tiff
  19. 3
      tests/Images/Input/Tiff/flower-rgb-contig-04.tiff
  20. 3
      tests/Images/Input/Tiff/flower-rgb-contig-10.tiff
  21. 3
      tests/Images/Input/Tiff/flower-rgb-contig-14.tiff
  22. 3
      tests/Images/Input/Tiff/flower-rgb-planar-02.tiff
  23. 3
      tests/Images/Input/Tiff/flower-rgb-planar-04.tiff
  24. 3
      tests/Images/Input/Tiff/flower-rgb-planar-10.tiff
  25. 3
      tests/Images/Input/Tiff/flower-rgb-planar-14.tiff

22
src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs

@ -96,10 +96,30 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Constants
public static readonly ushort[] BitsPerSample8Bit = { 8 }; public static readonly ushort[] BitsPerSample8Bit = { 8 };
/// <summary> /// <summary>
/// The bits per sample for images with 8 bits for each color channel. /// The bits per sample for color images with 2 bits for each color channel.
/// </summary>
public static readonly ushort[] BitsPerSampleRgb2Bit = { 2, 2, 2 };
/// <summary>
/// The bits per sample for color images with 4 bits for each color channel.
/// </summary>
public static readonly ushort[] BitsPerSampleRgb4Bit = { 4, 4, 4 };
/// <summary>
/// The bits per sample for color images with 8 bits for each color channel.
/// </summary> /// </summary>
public static readonly ushort[] BitsPerSampleRgb8Bit = { 8, 8, 8 }; public static readonly ushort[] BitsPerSampleRgb8Bit = { 8, 8, 8 };
/// <summary>
/// The bits per sample for color images with 10 bits for each color channel.
/// </summary>
public static readonly ushort[] BitsPerSampleRgb10Bit = { 10, 10, 10 };
/// <summary>
/// The bits per sample for color images with 14 bits for each color channel.
/// </summary>
public static readonly ushort[] BitsPerSampleRgb14Bit = { 14, 14, 14 };
/// <summary> /// <summary>
/// The list of mimetypes that equate to a tiff. /// The list of mimetypes that equate to a tiff.
/// </summary> /// </summary>

60
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb444TiffColor{TPixel}.cs

@ -0,0 +1,60 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
{
/// <summary>
/// Implements the 'RGB' photometric interpretation for 4 bits per color channel images.
/// </summary>
internal class Rgb444TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
/// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{
var color = default(TPixel);
int offset = 0;
var bgra = default(Bgra4444);
for (int y = top; y < top + height; y++)
{
Span<TPixel> pixelRow = pixels.GetRowSpan(y);
for (int x = left; x < left + width; x += 2)
{
byte r = (byte)((data[offset] & 0xF0) >> 4);
byte g = (byte)(data[offset] & 0xF);
offset++;
byte b = (byte)((data[offset] & 0xF0) >> 4);
bgra.PackedValue = ToBgraPackedValue(b, g, r);
color.FromScaledVector4(bgra.ToScaledVector4());
pixelRow[x] = color;
if (x + 1 >= pixelRow.Length)
{
offset++;
break;
}
r = (byte)(data[offset] & 0xF);
offset++;
g = (byte)((data[offset] & 0xF0) >> 4);
b = (byte)(data[offset] & 0xF);
offset++;
bgra.PackedValue = ToBgraPackedValue(b, g, r);
color.FromScaledVector4(bgra.ToScaledVector4());
pixelRow[x + 1] = color;
}
}
}
private static ushort ToBgraPackedValue(byte b, byte g, byte r) => (ushort)(b | (g << 4) | (r << 8) | (0xF << 12));
}
}

1
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs

@ -27,7 +27,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
private readonly ushort bitsPerSampleB; private readonly ushort bitsPerSampleB;
public RgbPlanarTiffColor(ushort[] bitsPerSample) public RgbPlanarTiffColor(ushort[] bitsPerSample)
/* : base(bitsPerSample, null) */
{ {
this.bitsPerSampleR = bitsPerSample[0]; this.bitsPerSampleR = bitsPerSample[0];
this.bitsPerSampleG = bitsPerSample[1]; this.bitsPerSampleG = bitsPerSample[1];

44
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs

@ -57,16 +57,56 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
DebugGuard.IsTrue(colorMap == null, "colorMap"); DebugGuard.IsTrue(colorMap == null, "colorMap");
return new RgbTiffColor<TPixel>(bitsPerSample); return new RgbTiffColor<TPixel>(bitsPerSample);
case TiffColorType.Rgb222:
DebugGuard.IsTrue(
bitsPerSample.Length == 3
&& bitsPerSample[2] == 2
&& bitsPerSample[1] == 2
&& bitsPerSample[0] == 2,
"bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new RgbTiffColor<TPixel>(bitsPerSample);
case TiffColorType.Rgb444:
DebugGuard.IsTrue(
bitsPerSample.Length == 3
&& bitsPerSample[2] == 4
&& bitsPerSample[1] == 4
&& bitsPerSample[0] == 4,
"bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new Rgb444TiffColor<TPixel>();
case TiffColorType.Rgb888: case TiffColorType.Rgb888:
DebugGuard.IsTrue( DebugGuard.IsTrue(
bitsPerSample.Length == 3 bitsPerSample.Length == 3
&& bitsPerSample[0] == 8 && bitsPerSample[2] == 8
&& bitsPerSample[1] == 8 && bitsPerSample[1] == 8
&& bitsPerSample[2] == 8, && bitsPerSample[0] == 8,
"bitsPerSample"); "bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap"); DebugGuard.IsTrue(colorMap == null, "colorMap");
return new Rgb888TiffColor<TPixel>(); return new Rgb888TiffColor<TPixel>();
case TiffColorType.Rgb101010:
DebugGuard.IsTrue(
bitsPerSample.Length == 3
&& bitsPerSample[2] == 10
&& bitsPerSample[1] == 10
&& bitsPerSample[0] == 10,
"bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new RgbTiffColor<TPixel>(bitsPerSample);
case TiffColorType.Rgb141414:
DebugGuard.IsTrue(
bitsPerSample.Length == 3
&& bitsPerSample[2] == 14
&& bitsPerSample[1] == 14
&& bitsPerSample[0] == 14,
"bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new RgbTiffColor<TPixel>(bitsPerSample);
case TiffColorType.PaletteColor: case TiffColorType.PaletteColor:
DebugGuard.NotNull(bitsPerSample, "bitsPerSample"); DebugGuard.NotNull(bitsPerSample, "bitsPerSample");
DebugGuard.NotNull(colorMap, "colorMap"); DebugGuard.NotNull(colorMap, "colorMap");

20
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs

@ -58,11 +58,31 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
/// </summary> /// </summary>
Rgb, Rgb,
/// <summary>
/// RGB color image with 2 bits for each channel.
/// </summary>
Rgb222,
/// <summary>
/// RGB color image with 4 bits for each channel.
/// </summary>
Rgb444,
/// <summary> /// <summary>
/// RGB Full Color. Optimized implementation for 8-bit images. /// RGB Full Color. Optimized implementation for 8-bit images.
/// </summary> /// </summary>
Rgb888, Rgb888,
/// <summary>
/// RGB color image with 10 bits for each channel.
/// </summary>
Rgb101010,
/// <summary>
/// RGB color image with 14 bits for each channel.
/// </summary>
Rgb141414,
/// <summary> /// <summary>
/// RGB Full Color. Planar configuration of data. /// RGB Full Color. Planar configuration of data.
/// </summary> /// </summary>

28
src/ImageSharp/Formats/Tiff/TiffBitsPerPixel.cs

@ -18,14 +18,42 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// </summary> /// </summary>
Bit4 = 4, Bit4 = 4,
/// <summary>
/// 6 bits per pixel. 2 bit for each color channel.
///
/// Note: The TiffEncoder does not yet support 2 bits per color channel and will default to 24 bits per pixel instead.
/// </summary>
Bit6 = 6,
/// <summary> /// <summary>
/// 8 bits per pixel, grayscale or color palette images. /// 8 bits per pixel, grayscale or color palette images.
/// </summary> /// </summary>
Bit8 = 8, Bit8 = 8,
/// <summary>
/// 12 bits per pixel. 4 bit for each color channel.
///
/// Note: The TiffEncoder does not yet support 4 bits per color channel and will default to 24 bits per pixel instead.
/// </summary>
Bit12 = 12,
/// <summary> /// <summary>
/// 24 bits per pixel. One byte for each color channel. /// 24 bits per pixel. One byte for each color channel.
/// </summary> /// </summary>
Bit24 = 24, Bit24 = 24,
/// <summary>
/// 30 bits per pixel. 10 bit for each color channel.
///
/// Note: The TiffEncoder does not yet support 10 bits per color channel and will default to 24 bits per pixel instead.
/// </summary>
Bit30 = 30,
/// <summary>
/// 42 bits per pixel. 14 bit for each color channel.
///
/// Note: The TiffEncoder does not yet support 14 bits per color channel and will default to 24 bits per pixel instead.
/// </summary>
Bit42 = 42,
} }
} }

20
src/ImageSharp/Formats/Tiff/TiffBitsPerSample.cs

@ -28,9 +28,29 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// </summary> /// </summary>
Bit8 = 8, Bit8 = 8,
/// <summary>
/// Six bits per sample, each channel has 2 bits.
/// </summary>
Bit6 = 6,
/// <summary>
/// Twelve bits per sample, each channel has 4 bits.
/// </summary>
Bit12 = 12,
/// <summary> /// <summary>
/// 24 bits per sample, each color channel has 8 Bits. /// 24 bits per sample, each color channel has 8 Bits.
/// </summary> /// </summary>
Bit24 = 24, Bit24 = 24,
/// <summary>
/// Thirty bits per sample, each channel has 10 bits.
/// </summary>
Bit30 = 30,
/// <summary>
/// Forty two bits per sample, each channel has 14 bits.
/// </summary>
Bit42 = 42,
} }
} }

40
src/ImageSharp/Formats/Tiff/TiffBitsPerSampleExtensions.cs

@ -21,10 +21,18 @@ namespace SixLabors.ImageSharp.Formats.Tiff
return TiffConstants.BitsPerSample1Bit; return TiffConstants.BitsPerSample1Bit;
case TiffBitsPerSample.Bit4: case TiffBitsPerSample.Bit4:
return TiffConstants.BitsPerSample4Bit; return TiffConstants.BitsPerSample4Bit;
case TiffBitsPerSample.Bit6:
return TiffConstants.BitsPerSampleRgb2Bit;
case TiffBitsPerSample.Bit8: case TiffBitsPerSample.Bit8:
return TiffConstants.BitsPerSample8Bit; return TiffConstants.BitsPerSample8Bit;
case TiffBitsPerSample.Bit12:
return TiffConstants.BitsPerSampleRgb4Bit;
case TiffBitsPerSample.Bit24: case TiffBitsPerSample.Bit24:
return TiffConstants.BitsPerSampleRgb8Bit; return TiffConstants.BitsPerSampleRgb8Bit;
case TiffBitsPerSample.Bit30:
return TiffConstants.BitsPerSampleRgb10Bit;
case TiffBitsPerSample.Bit42:
return TiffConstants.BitsPerSampleRgb14Bit;
default: default:
return Array.Empty<ushort>(); return Array.Empty<ushort>();
@ -41,13 +49,41 @@ namespace SixLabors.ImageSharp.Formats.Tiff
switch (bitsPerSample.Length) switch (bitsPerSample.Length)
{ {
case 3: case 3:
if (bitsPerSample[0] == TiffConstants.BitsPerSampleRgb8Bit[0] && if (bitsPerSample[2] == TiffConstants.BitsPerSampleRgb14Bit[2] &&
bitsPerSample[1] == TiffConstants.BitsPerSampleRgb14Bit[1] &&
bitsPerSample[0] == TiffConstants.BitsPerSampleRgb14Bit[0])
{
return TiffBitsPerSample.Bit42;
}
if (bitsPerSample[2] == TiffConstants.BitsPerSampleRgb10Bit[2] &&
bitsPerSample[1] == TiffConstants.BitsPerSampleRgb10Bit[1] &&
bitsPerSample[0] == TiffConstants.BitsPerSampleRgb10Bit[0])
{
return TiffBitsPerSample.Bit30;
}
if (bitsPerSample[2] == TiffConstants.BitsPerSampleRgb8Bit[2] &&
bitsPerSample[1] == TiffConstants.BitsPerSampleRgb8Bit[1] && bitsPerSample[1] == TiffConstants.BitsPerSampleRgb8Bit[1] &&
bitsPerSample[2] == TiffConstants.BitsPerSampleRgb8Bit[2]) bitsPerSample[0] == TiffConstants.BitsPerSampleRgb8Bit[0])
{ {
return TiffBitsPerSample.Bit24; return TiffBitsPerSample.Bit24;
} }
if (bitsPerSample[2] == TiffConstants.BitsPerSampleRgb4Bit[2] &&
bitsPerSample[1] == TiffConstants.BitsPerSampleRgb4Bit[1] &&
bitsPerSample[0] == TiffConstants.BitsPerSampleRgb4Bit[0])
{
return TiffBitsPerSample.Bit12;
}
if (bitsPerSample[2] == TiffConstants.BitsPerSampleRgb2Bit[2] &&
bitsPerSample[1] == TiffConstants.BitsPerSampleRgb2Bit[1] &&
bitsPerSample[0] == TiffConstants.BitsPerSampleRgb2Bit[0])
{
return TiffBitsPerSample.Bit6;
}
break; break;
case 1: case 1:

2
src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs

@ -294,7 +294,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
int top = rowsPerStrip * stripIndex; int top = rowsPerStrip * stripIndex;
if (top + stripHeight > frame.Height) if (top + stripHeight > frame.Height)
{ {
// Make sure we ignore any strips that are not needed for the image (if too many are present) // Make sure we ignore any strips that are not needed for the image (if too many are present).
break; break;
} }

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

@ -1,6 +1,7 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System;
using System.Linq; using System.Linq;
using SixLabors.ImageSharp.Formats.Tiff.Compression; using SixLabors.ImageSharp.Formats.Tiff.Compression;
using SixLabors.ImageSharp.Formats.Tiff.Constants; using SixLabors.ImageSharp.Formats.Tiff.Constants;
@ -179,7 +180,29 @@ namespace SixLabors.ImageSharp.Formats.Tiff
if (options.PlanarConfiguration == TiffPlanarConfiguration.Chunky) if (options.PlanarConfiguration == TiffPlanarConfiguration.Chunky)
{ {
options.ColorType = options.BitsPerSample == TiffBitsPerSample.Bit24 ? TiffColorType.Rgb888 : TiffColorType.Rgb; switch (options.BitsPerSample)
{
case TiffBitsPerSample.Bit42:
options.ColorType = TiffColorType.Rgb141414;
break;
case TiffBitsPerSample.Bit30:
options.ColorType = TiffColorType.Rgb101010;
break;
case TiffBitsPerSample.Bit24:
options.ColorType = TiffColorType.Rgb888;
break;
case TiffBitsPerSample.Bit12:
options.ColorType = TiffColorType.Rgb444;
break;
case TiffBitsPerSample.Bit6:
options.ColorType = TiffColorType.Rgb222;
break;
default:
TiffThrowHelper.ThrowNotSupported("Bits per sample is not supported.");
break;
}
} }
else else
{ {
@ -273,9 +296,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff
{ {
TiffBitsPerPixel.Bit1 => TiffBitsPerSample.Bit1, TiffBitsPerPixel.Bit1 => TiffBitsPerSample.Bit1,
TiffBitsPerPixel.Bit4 => TiffBitsPerSample.Bit4, TiffBitsPerPixel.Bit4 => TiffBitsPerSample.Bit4,
TiffBitsPerPixel.Bit6 => TiffBitsPerSample.Bit6,
TiffBitsPerPixel.Bit8 => TiffBitsPerSample.Bit8, TiffBitsPerPixel.Bit8 => TiffBitsPerSample.Bit8,
TiffBitsPerPixel.Bit12 => TiffBitsPerSample.Bit12,
TiffBitsPerPixel.Bit24 => TiffBitsPerSample.Bit24, TiffBitsPerPixel.Bit24 => TiffBitsPerSample.Bit24,
_ => TiffBitsPerSample.Bit24, TiffBitsPerPixel.Bit30 => TiffBitsPerSample.Bit30,
TiffBitsPerPixel.Bit42 => TiffBitsPerSample.Bit42,
_ => throw new NotSupportedException("The bits per pixel are not supported"),
}; };
} }
} }

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

@ -306,7 +306,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
case TiffBitsPerPixel.Bit1: case TiffBitsPerPixel.Bit1:
if (compression == TiffCompression.Ccitt1D || compression == TiffCompression.CcittGroup3Fax || compression == TiffCompression.CcittGroup4Fax) if (compression == TiffCompression.Ccitt1D || compression == TiffCompression.CcittGroup3Fax || compression == TiffCompression.CcittGroup4Fax)
{ {
// The “normal” PhotometricInterpretation for bilevel CCITT compressed data is WhiteIsZero. // The “normal” PhotometricInterpretation for bilevel CCITT compressed data is WhiteIsZero.
this.SetEncoderOptions(bitsPerPixel, TiffPhotometricInterpretation.WhiteIsZero, compression, TiffPredictor.None); this.SetEncoderOptions(bitsPerPixel, TiffPhotometricInterpretation.WhiteIsZero, compression, TiffPredictor.None);
break; break;
} }
@ -319,6 +319,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff
case TiffBitsPerPixel.Bit8: case TiffBitsPerPixel.Bit8:
this.SetEncoderOptions(bitsPerPixel, photometricInterpretation ?? TiffPhotometricInterpretation.BlackIsZero, compression, predictor); this.SetEncoderOptions(bitsPerPixel, photometricInterpretation ?? TiffPhotometricInterpretation.BlackIsZero, compression, predictor);
break; break;
case TiffBitsPerPixel.Bit6:
case TiffBitsPerPixel.Bit12:
case TiffBitsPerPixel.Bit30:
case TiffBitsPerPixel.Bit42:
// Encoding 42, 30, 12 and 6 bits per pixel is not yet supported. Default to 24 bits.
this.SetEncoderOptions(TiffBitsPerPixel.Bit24, TiffPhotometricInterpretation.Rgb, compression, TiffPredictor.None);
break;
default: default:
this.SetEncoderOptions(bitsPerPixel, TiffPhotometricInterpretation.Rgb, compression, predictor); this.SetEncoderOptions(bitsPerPixel, TiffPhotometricInterpretation.Rgb, compression, predictor);
break; break;

11
src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs

@ -66,8 +66,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff
Value = SoftwareValue Value = SoftwareValue
}; };
this.collector.Add(width); this.collector.AddOrReplace(width);
this.collector.Add(height); this.collector.AddOrReplace(height);
this.ProcessResolution(image.Metadata, rootFrameExifProfile); this.ProcessResolution(image.Metadata, rootFrameExifProfile);
this.ProcessProfiles(image.Metadata, rootFrameExifProfile, rootFrameXmpBytes); this.ProcessProfiles(image.Metadata, rootFrameExifProfile, rootFrameXmpBytes);
@ -227,7 +227,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff
exifProfile.RemoveValue(ExifTag.IccProfile); exifProfile.RemoveValue(ExifTag.IccProfile);
} }
TiffMetadata tiffMetadata = imageMetadata.GetTiffMetadata();
if (xmpProfile != null) if (xmpProfile != null)
{ {
var xmp = new ExifByteArray(ExifTagValue.XMP, ExifDataType.Byte) var xmp = new ExifByteArray(ExifTagValue.XMP, ExifDataType.Byte)
@ -252,6 +251,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff
public void Process(TiffEncoderCore encoder) public void Process(TiffEncoderCore encoder)
{ {
var planarConfig = new ExifShort(ExifTagValue.PlanarConfiguration)
{
Value = (ushort)TiffPlanarConfiguration.Chunky
};
var samplesPerPixel = new ExifLong(ExifTagValue.SamplesPerPixel) var samplesPerPixel = new ExifLong(ExifTagValue.SamplesPerPixel)
{ {
Value = GetSamplesPerPixel(encoder) Value = GetSamplesPerPixel(encoder)
@ -274,6 +278,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
Value = (ushort)encoder.PhotometricInterpretation Value = (ushort)encoder.PhotometricInterpretation
}; };
this.collector.AddOrReplace(planarConfig);
this.collector.AddOrReplace(samplesPerPixel); this.collector.AddOrReplace(samplesPerPixel);
this.collector.AddOrReplace(bitPerSample); this.collector.AddOrReplace(bitPerSample);
this.collector.AddOrReplace(compression); this.collector.AddOrReplace(compression);

32
tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

@ -97,13 +97,31 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
[WithFile(Flower4BitPalette, PixelTypes.Rgba32)] [WithFile(Flower4BitPalette, PixelTypes.Rgba32)]
[WithFile(Flower4BitPaletteGray, PixelTypes.Rgba32)] [WithFile(Flower4BitPaletteGray, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_4Bit_WithPalette<TPixel>(TestImageProvider<TPixel> provider) public void TiffDecoder_CanDecode_4Bit_WithPalette<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider, ReferenceDecoder, useExactComparer: false, 0.01f);
{
if (TestEnvironment.IsWindows) [Theory]
{ [WithFile(FlowerRgb222Contiguous, PixelTypes.Rgba32)]
TestTiffDecoder(provider, new SystemDrawingReferenceDecoder(), useExactComparer: false, 0.01f); [WithFile(FlowerRgb222Planar, PixelTypes.Rgba32)]
} public void TiffDecoder_CanDecode_6Bit<TPixel>(TestImageProvider<TPixel> provider)
} where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
[Theory]
[WithFile(FlowerRgb444Contiguous, PixelTypes.Rgba32)]
[WithFile(FlowerRgb444Planar, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_12Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
[Theory]
[WithFile(FlowerRgb101010Contiguous, PixelTypes.Rgba32)]
[WithFile(FlowerRgb101010Planar, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_30Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
[Theory]
[WithFile(FlowerRgb141414Contiguous, PixelTypes.Rgba32)]
[WithFile(FlowerRgb141414Planar, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_42Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
[Theory] [Theory]
[WithFile(GrayscaleDeflateMultistrip, PixelTypes.Rgba32)] [WithFile(GrayscaleDeflateMultistrip, PixelTypes.Rgba32)]

32
tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs

@ -77,6 +77,29 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
Assert.Equal(TiffCompression.None, frameMetaData.Compression); Assert.Equal(TiffCompression.None, frameMetaData.Compression);
} }
[Theory]
[InlineData(TiffBitsPerPixel.Bit42)]
[InlineData(TiffBitsPerPixel.Bit30)]
[InlineData(TiffBitsPerPixel.Bit12)]
[InlineData(TiffBitsPerPixel.Bit6)]
public void EncoderOptions_UnsupportedBitPerPixel_DefaultTo24Bits(TiffBitsPerPixel bitsPerPixel)
{
// arrange
var tiffEncoder = new TiffEncoder { BitsPerPixel = bitsPerPixel };
using Image input = new Image<Rgb24>(10, 10);
using var memStream = new MemoryStream();
// act
input.Save(memStream, tiffEncoder);
// assert
memStream.Position = 0;
using var output = Image.Load<Rgba32>(memStream);
TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata();
Assert.Equal(TiffBitsPerPixel.Bit24, frameMetaData.BitsPerPixel);
}
[Theory] [Theory]
[InlineData(null, TiffCompression.Deflate, TiffBitsPerPixel.Bit24, TiffCompression.Deflate)] [InlineData(null, TiffCompression.Deflate, TiffBitsPerPixel.Bit24, TiffCompression.Deflate)]
[InlineData(TiffPhotometricInterpretation.Rgb, TiffCompression.Deflate, TiffBitsPerPixel.Bit24, TiffCompression.Deflate)] [InlineData(TiffPhotometricInterpretation.Rgb, TiffCompression.Deflate, TiffBitsPerPixel.Bit24, TiffCompression.Deflate)]
@ -228,6 +251,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
Assert.Equal(expectedCompression, frameMetaData.Compression); Assert.Equal(expectedCompression, frameMetaData.Compression);
} }
// This makes sure, that when decoding a planar tiff, the planar configuration is not carried over to the encoded image.
[Theory]
[WithFile(FlowerRgb444Planar, PixelTypes.Rgba32)]
public void TiffEncoder_EncodePlanar_AndReload_Works<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit24, TiffPhotometricInterpretation.Rgb, imageDecoder: new TiffDecoder());
[Theory] [Theory]
[WithFile(Calliphora_RgbUncompressed, PixelTypes.Rgba32)] [WithFile(Calliphora_RgbUncompressed, PixelTypes.Rgba32)]
public void TiffEncoder_EncodeRgb_Works<TPixel>(TestImageProvider<TPixel> provider) public void TiffEncoder_EncodeRgb_Works<TPixel>(TestImageProvider<TPixel> provider)
@ -300,8 +329,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
[WithFile(Flower4BitPaletteGray, PixelTypes.Rgba32)] [WithFile(Flower4BitPaletteGray, PixelTypes.Rgba32)]
public void TiffEncoder_EncodeColorPalette_With4Bit_Works<TPixel>(TestImageProvider<TPixel> provider) public void TiffEncoder_EncodeColorPalette_With4Bit_Works<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => where TPixel : unmanaged, IPixel<TPixel> =>
//// Note: The magick reference decoder does not support 4 bit tiff's, so we use our TIFF decoder instead. TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit4, TiffPhotometricInterpretation.PaletteColor, useExactComparer: false, compareTolerance: 0.003f);
TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit4, TiffPhotometricInterpretation.PaletteColor, useExactComparer: false, compareTolerance: 0.003f, imageDecoder: new TiffDecoder());
[Theory] [Theory]
[WithFile(Calliphora_PaletteUncompressed, PixelTypes.Rgba32)] [WithFile(Calliphora_PaletteUncompressed, PixelTypes.Rgba32)]

5
tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs

@ -288,10 +288,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
Assert.Equal("This is Изготовитель камеры", exifProfileInput.GetValue(ExifTag.Make).Value); Assert.Equal("This is Изготовитель камеры", exifProfileInput.GetValue(ExifTag.Make).Value);
Assert.Equal("This is Авторские права", exifProfileInput.GetValue(ExifTag.Copyright).Value); Assert.Equal("This is Авторские права", exifProfileInput.GetValue(ExifTag.Copyright).Value);
Assert.Equal(exifProfileInput.Values.Count, encodedImageExifProfile.Values.Count);
Assert.Equal(exifProfileInput.GetValue(ExifTag.ImageDescription).Value, encodedImageExifProfile.GetValue(ExifTag.ImageDescription).Value); Assert.Equal(exifProfileInput.GetValue(ExifTag.ImageDescription).Value, encodedImageExifProfile.GetValue(ExifTag.ImageDescription).Value);
Assert.Equal(exifProfileInput.GetValue(ExifTag.Make).Value, encodedImageExifProfile.GetValue(ExifTag.Make).Value); Assert.Equal(exifProfileInput.GetValue(ExifTag.Make).Value, encodedImageExifProfile.GetValue(ExifTag.Make).Value);
Assert.Equal(exifProfileInput.GetValue(ExifTag.Copyright).Value, encodedImageExifProfile.GetValue(ExifTag.Copyright).Value); Assert.Equal(exifProfileInput.GetValue(ExifTag.Copyright).Value, encodedImageExifProfile.GetValue(ExifTag.Copyright).Value);
// Note that the encoded profile has PlanarConfiguration explicitly set, which is missing in the original image profile.
Assert.Equal((ushort)TiffPlanarConfiguration.Chunky, encodedImageExifProfile.GetValue(ExifTag.PlanarConfiguration)?.Value);
Assert.Equal(exifProfileInput.Values.Count + 1, encodedImageExifProfile.Values.Count);
} }
} }
} }

8
tests/ImageSharp.Tests/TestImages.cs

@ -560,6 +560,14 @@ namespace SixLabors.ImageSharp.Tests
public const string RgbPaletteDeflate = "Tiff/rgb_palette_deflate.tiff"; public const string RgbPaletteDeflate = "Tiff/rgb_palette_deflate.tiff";
public const string Flower4BitPalette = "Tiff/flower-palette-04.tiff"; public const string Flower4BitPalette = "Tiff/flower-palette-04.tiff";
public const string Flower4BitPaletteGray = "Tiff/flower-minisblack-04.tiff"; public const string Flower4BitPaletteGray = "Tiff/flower-minisblack-04.tiff";
public const string FlowerRgb141414Contiguous = "Tiff/flower-rgb-contig-14.tiff";
public const string FlowerRgb141414Planar = "Tiff/flower-rgb-planar-14.tiff";
public const string FlowerRgb101010Contiguous = "Tiff/flower-rgb-contig-10.tiff";
public const string FlowerRgb101010Planar = "Tiff/flower-rgb-planar-10.tiff";
public const string FlowerRgb444Contiguous = "Tiff/flower-rgb-contig-04.tiff";
public const string FlowerRgb444Planar = "Tiff/flower-rgb-planar-04.tiff";
public const string FlowerRgb222Contiguous = "Tiff/flower-rgb-contig-02.tiff";
public const string FlowerRgb222Planar = "Tiff/flower-rgb-planar-02.tiff";
public const string SmallRgbDeflate = "Tiff/rgb_small_deflate.tiff"; public const string SmallRgbDeflate = "Tiff/rgb_small_deflate.tiff";
public const string SmallRgbLzw = "Tiff/rgb_small_lzw.tiff"; public const string SmallRgbLzw = "Tiff/rgb_small_lzw.tiff";

9
tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs

@ -25,10 +25,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
{ {
} }
public MagickReferenceDecoder(bool validate) public MagickReferenceDecoder(bool validate) => this.validate = validate;
{
this.validate = validate;
}
public static MagickReferenceDecoder Instance { get; } = new MagickReferenceDecoder(); public static MagickReferenceDecoder Instance { get; } = new MagickReferenceDecoder();
@ -87,13 +84,13 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
MemoryGroup<TPixel> framePixels = frame.PixelBuffer.FastMemoryGroup; MemoryGroup<TPixel> framePixels = frame.PixelBuffer.FastMemoryGroup;
using IUnsafePixelCollection<ushort> pixels = magicFrame.GetPixelsUnsafe(); using IUnsafePixelCollection<ushort> pixels = magicFrame.GetPixelsUnsafe();
if (magicFrame.Depth == 8 || magicFrame.Depth == 1) if (magicFrame.Depth == 8 || magicFrame.Depth == 4 || magicFrame.Depth == 2 || magicFrame.Depth == 1 || magicFrame.Depth == 10)
{ {
byte[] data = pixels.ToByteArray(PixelMapping.RGBA); byte[] data = pixels.ToByteArray(PixelMapping.RGBA);
FromRgba32Bytes(configuration, data, framePixels); FromRgba32Bytes(configuration, data, framePixels);
} }
else if (magicFrame.Depth == 16) else if (magicFrame.Depth == 16 || magicFrame.Depth == 14)
{ {
ushort[] data = pixels.ToShortArray(PixelMapping.RGBA); ushort[] data = pixels.ToShortArray(PixelMapping.RGBA);
Span<byte> bytes = MemoryMarshal.Cast<ushort, byte>(data.AsSpan()); Span<byte> bytes = MemoryMarshal.Cast<ushort, byte>(data.AsSpan());

3
tests/Images/Input/Tiff/flower-rgb-contig-02.tiff

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:fbcd225c0db343f0cc984c35609b81f6413ebc1ba2ce2494d3607db375e969ff
size 2685

3
tests/Images/Input/Tiff/flower-rgb-contig-04.tiff

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:96c4c1dfc23a0d9e5c6189717647fa117b08aac9a40c63e3945d3e674df4c3c6
size 5049

3
tests/Images/Input/Tiff/flower-rgb-contig-10.tiff

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:68168ea1c2e50e674a7c5c41e5b055c881adf8cb940d0fd033a927a7ebdd7b6f
size 12117

3
tests/Images/Input/Tiff/flower-rgb-contig-14.tiff

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:a419a8e2f89321501ca8ad70d2a19d37a7bf3a8c2f45c809acc30be59139ae29
size 16855

3
tests/Images/Input/Tiff/flower-rgb-planar-02.tiff

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:21c4ede6382d8c72cb8e6f7939203d5111b362646a9727d95a2f63310ec8e5b3
size 2795

3
tests/Images/Input/Tiff/flower-rgb-planar-04.tiff

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ca4434aa1a8c52654b20596c7c428c9016e089de75c29dc6ddcd32708874005c
size 5117

3
tests/Images/Input/Tiff/flower-rgb-planar-10.tiff

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7f53948d4a36c80f45d70a315d2e76514ec41cabe982c06dbbd0d47e671120e2
size 12211

3
tests/Images/Input/Tiff/flower-rgb-planar-14.tiff

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d28f021d40f53a011053f9644400fee2d29c02f97b4101fec899251125dbb18e
size 16855
Loading…
Cancel
Save