Browse Source

Add support for decoding 12 bits per pixel tiff's

pull/1647/head
Brian Popow 5 years ago
parent
commit
c6f5a8aaa0
  1. 7
      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. 10
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs
  5. 5
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs
  6. 7
      src/ImageSharp/Formats/Tiff/TiffBitsPerPixel.cs
  7. 5
      src/ImageSharp/Formats/Tiff/TiffBitsPerSample.cs
  8. 9
      src/ImageSharp/Formats/Tiff/TiffBitsPerSampleExtensions.cs
  9. 2
      src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs
  10. 17
      src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs
  11. 6
      src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
  12. 11
      src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs
  13. 12
      tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
  14. 5
      tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs
  15. 2
      tests/ImageSharp.Tests/TestImages.cs
  16. 3
      tests/Images/Input/Tiff/flower-rgb-contig-04.tiff
  17. 3
      tests/Images/Input/Tiff/flower-rgb-planar-04.tiff

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

@ -96,10 +96,15 @@ 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 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 4 bits for each color channel.
/// </summary>
public static readonly ushort[] BitsPerSampleRgb4Bit = { 4, 4, 4 };
/// <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];

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

@ -57,6 +57,16 @@ 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.Rgb444:
DebugGuard.IsTrue(
bitsPerSample.Length == 3
&& bitsPerSample[0] == 4
&& bitsPerSample[1] == 4
&& bitsPerSample[2] == 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

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

@ -63,6 +63,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
/// </summary> /// </summary>
Rgb888, Rgb888,
/// <summary>
/// RGB color image with 4 bits for each channel.
/// </summary>
Rgb444,
/// <summary> /// <summary>
/// RGB Full Color. Planar configuration of data. /// RGB Full Color. Planar configuration of data.
/// </summary> /// </summary>

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

@ -23,6 +23,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// </summary> /// </summary>
Bit8 = 8, Bit8 = 8,
/// <summary>
/// 14 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.
/// </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>

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

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

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

@ -23,6 +23,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff
return TiffConstants.BitsPerSample4Bit; return TiffConstants.BitsPerSample4Bit;
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;
@ -48,6 +50,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff
return TiffBitsPerSample.Bit24; return TiffBitsPerSample.Bit24;
} }
if (bitsPerSample[0] == TiffConstants.BitsPerSampleRgb4Bit[0] &&
bitsPerSample[1] == TiffConstants.BitsPerSampleRgb4Bit[1] &&
bitsPerSample[2] == TiffConstants.BitsPerSampleRgb4Bit[2])
{
return TiffBitsPerSample.Bit12;
}
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;
} }

17
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,18 @@ 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.Bit24:
options.ColorType = TiffColorType.Rgb888;
break;
case TiffBitsPerSample.Bit12:
options.ColorType = TiffColorType.Rgb444;
break;
default:
TiffThrowHelper.ThrowNotSupported("Bits per sample is nut supported.");
break;
}
} }
else else
{ {
@ -274,8 +286,9 @@ namespace SixLabors.ImageSharp.Formats.Tiff
TiffBitsPerPixel.Bit1 => TiffBitsPerSample.Bit1, TiffBitsPerPixel.Bit1 => TiffBitsPerSample.Bit1,
TiffBitsPerPixel.Bit4 => TiffBitsPerSample.Bit4, TiffBitsPerPixel.Bit4 => TiffBitsPerSample.Bit4,
TiffBitsPerPixel.Bit8 => TiffBitsPerSample.Bit8, TiffBitsPerPixel.Bit8 => TiffBitsPerSample.Bit8,
TiffBitsPerPixel.Bit12 => TiffBitsPerSample.Bit12,
TiffBitsPerPixel.Bit24 => TiffBitsPerSample.Bit24, TiffBitsPerPixel.Bit24 => TiffBitsPerSample.Bit24,
_ => TiffBitsPerSample.Bit24, _ => throw new NotSupportedException("The bits per pixel are not supported"),
}; };
} }
} }

6
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,10 @@ 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.Bit12:
// Encoding 12 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);

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

@ -105,6 +105,18 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
} }
} }
[Theory]
[WithFile(FlowerRgb444Contiguous, PixelTypes.Rgba32)]
[WithFile(FlowerRgb444Planar, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_12Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
if (TestEnvironment.IsWindows)
{
TestTiffDecoder(provider, new SystemDrawingReferenceDecoder());
}
}
[Theory] [Theory]
[WithFile(GrayscaleDeflateMultistrip, PixelTypes.Rgba32)] [WithFile(GrayscaleDeflateMultistrip, PixelTypes.Rgba32)]
[WithFile(RgbDeflateMultistrip, PixelTypes.Rgba32)] [WithFile(RgbDeflateMultistrip, 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);
} }
} }
} }

2
tests/ImageSharp.Tests/TestImages.cs

@ -560,6 +560,8 @@ 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 FlowerRgb444Contiguous = "Tiff/flower-rgb-contig-04.tiff";
public const string FlowerRgb444Planar = "Tiff/flower-rgb-planar-04.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";

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

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