Browse Source

Add support for decoding 6 bit per pixel tiff's

pull/1647/head
Brian Popow 5 years ago
parent
commit
42d5d9ee91
  1. 9
      src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs
  2. 10
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs
  3. 9
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs
  4. 9
      src/ImageSharp/Formats/Tiff/TiffBitsPerPixel.cs
  5. 5
      src/ImageSharp/Formats/Tiff/TiffBitsPerSample.cs
  6. 9
      src/ImageSharp/Formats/Tiff/TiffBitsPerSampleExtensions.cs
  7. 4
      src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs
  8. 3
      src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
  9. 6
      tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
  10. 1
      tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs
  11. 2
      tests/ImageSharp.Tests/TestImages.cs
  12. 3
      tests/Images/Input/Tiff/flower-rgb-contig-02.tiff
  13. 3
      tests/Images/Input/Tiff/flower-rgb-planar-02.tiff

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

@ -96,15 +96,20 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Constants
public static readonly ushort[] BitsPerSample8Bit = { 8 };
/// <summary>
/// The bits per sample for color 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[] BitsPerSampleRgb8Bit = { 8, 8, 8 };
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>
public static readonly ushort[] BitsPerSampleRgb8Bit = { 8, 8, 8 };
/// <summary>
/// The list of mimetypes that equate to a tiff.
/// </summary>

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

@ -57,6 +57,16 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new RgbTiffColor<TPixel>(bitsPerSample);
case TiffColorType.Rgb222:
DebugGuard.IsTrue(
bitsPerSample.Length == 3
&& bitsPerSample[0] == 2
&& bitsPerSample[1] == 2
&& bitsPerSample[2] == 2,
"bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new RgbTiffColor<TPixel>(bitsPerSample);
case TiffColorType.Rgb444:
DebugGuard.IsTrue(
bitsPerSample.Length == 3

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

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

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

@ -18,6 +18,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// </summary>
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>
/// 8 bits per pixel, grayscale or color palette images.
/// </summary>
@ -26,7 +33,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// <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.
/// Note: The TiffEncoder does not yet support 4 bits per color channel and will default to 24 bits per pixel instead.
/// </summary>
Bit12 = 12,

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

@ -28,6 +28,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// </summary>
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>

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

@ -21,6 +21,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff
return TiffConstants.BitsPerSample1Bit;
case TiffBitsPerSample.Bit4:
return TiffConstants.BitsPerSample4Bit;
case TiffBitsPerSample.Bit6:
return TiffConstants.BitsPerSampleRgb2Bit;
case TiffBitsPerSample.Bit8:
return TiffConstants.BitsPerSample8Bit;
case TiffBitsPerSample.Bit12:
@ -57,6 +59,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff
return TiffBitsPerSample.Bit12;
}
if (bitsPerSample[2] == TiffConstants.BitsPerSampleRgb2Bit[2] &&
bitsPerSample[1] == TiffConstants.BitsPerSampleRgb2Bit[1] &&
bitsPerSample[0] == TiffConstants.BitsPerSampleRgb2Bit[0])
{
return TiffBitsPerSample.Bit6;
}
break;
case 1:

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

@ -188,6 +188,9 @@ namespace SixLabors.ImageSharp.Formats.Tiff
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;
@ -285,6 +288,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
{
TiffBitsPerPixel.Bit1 => TiffBitsPerSample.Bit1,
TiffBitsPerPixel.Bit4 => TiffBitsPerSample.Bit4,
TiffBitsPerPixel.Bit6 => TiffBitsPerSample.Bit6,
TiffBitsPerPixel.Bit8 => TiffBitsPerSample.Bit8,
TiffBitsPerPixel.Bit12 => TiffBitsPerSample.Bit12,
TiffBitsPerPixel.Bit24 => TiffBitsPerSample.Bit24,

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

@ -319,8 +319,9 @@ namespace SixLabors.ImageSharp.Formats.Tiff
case TiffBitsPerPixel.Bit8:
this.SetEncoderOptions(bitsPerPixel, photometricInterpretation ?? TiffPhotometricInterpretation.BlackIsZero, compression, predictor);
break;
case TiffBitsPerPixel.Bit6:
case TiffBitsPerPixel.Bit12:
// Encoding 12 bits per pixel is not yet supported. Default to 24 bits.
// Encoding 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:

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

@ -99,6 +99,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
public void TiffDecoder_CanDecode_4Bit_WithPalette<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider, ReferenceDecoder, useExactComparer: false, 0.01f);
[Theory]
[WithFile(FlowerRgb222Contiguous, PixelTypes.Rgba32)]
[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)]

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

@ -79,6 +79,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
[Theory]
[InlineData(TiffBitsPerPixel.Bit12)]
[InlineData(TiffBitsPerPixel.Bit6)]
public void EncoderOptions_UnsupportedBitPerPixel_DefaultTo24Bits(TiffBitsPerPixel bitsPerPixel)
{
// arrange

2
tests/ImageSharp.Tests/TestImages.cs

@ -562,6 +562,8 @@ namespace SixLabors.ImageSharp.Tests
public const string Flower4BitPaletteGray = "Tiff/flower-minisblack-04.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 SmallRgbLzw = "Tiff/rgb_small_lzw.tiff";

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-planar-02.tiff

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