From c6f5a8aaa01369794eac5c4958718f5d6f018595 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 1 Jun 2021 23:48:48 +0200 Subject: [PATCH] Add support for decoding 12 bits per pixel tiff's --- .../Formats/Tiff/Constants/TiffConstants.cs | 7 ++- .../Rgb444TiffColor{TPixel}.cs | 60 +++++++++++++++++++ .../RgbPlanarTiffColor{TPixel}.cs | 1 - .../TiffColorDecoderFactory{TPixel}.cs | 10 ++++ .../TiffColorType.cs | 5 ++ .../Formats/Tiff/TiffBitsPerPixel.cs | 7 +++ .../Formats/Tiff/TiffBitsPerSample.cs | 5 ++ .../Tiff/TiffBitsPerSampleExtensions.cs | 9 +++ .../Formats/Tiff/TiffDecoderCore.cs | 2 +- .../Formats/Tiff/TiffDecoderOptionsParser.cs | 17 +++++- .../Formats/Tiff/TiffEncoderCore.cs | 6 +- .../Tiff/TiffEncoderEntriesCollector.cs | 11 +++- .../Formats/Tiff/TiffDecoderTests.cs | 12 ++++ .../Formats/Tiff/TiffMetadataTests.cs | 5 +- tests/ImageSharp.Tests/TestImages.cs | 2 + .../Input/Tiff/flower-rgb-contig-04.tiff | 3 + .../Input/Tiff/flower-rgb-planar-04.tiff | 3 + 17 files changed, 155 insertions(+), 10 deletions(-) create mode 100644 src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb444TiffColor{TPixel}.cs create mode 100644 tests/Images/Input/Tiff/flower-rgb-contig-04.tiff create mode 100644 tests/Images/Input/Tiff/flower-rgb-planar-04.tiff diff --git a/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs b/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs index a30890a69e..988b1242ae 100644 --- a/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs +++ b/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs @@ -96,10 +96,15 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Constants public static readonly ushort[] BitsPerSample8Bit = { 8 }; /// - /// 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. /// public static readonly ushort[] BitsPerSampleRgb8Bit = { 8, 8, 8 }; + /// + /// The bits per sample for color images with 4 bits for each color channel. + /// + public static readonly ushort[] BitsPerSampleRgb4Bit = { 4, 4, 4 }; + /// /// The list of mimetypes that equate to a tiff. /// diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb444TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb444TiffColor{TPixel}.cs new file mode 100644 index 0000000000..d8c48942f8 --- /dev/null +++ b/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 +{ + /// + /// Implements the 'RGB' photometric interpretation for 4 bits per color channel images. + /// + internal class Rgb444TiffColor : TiffBaseColorDecoder + where TPixel : unmanaged, IPixel + { + /// + public override void Decode(ReadOnlySpan data, Buffer2D 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 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)); + } +} diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs index e45dd44bdc..b40158fcee 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs @@ -27,7 +27,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation private readonly ushort bitsPerSampleB; public RgbPlanarTiffColor(ushort[] bitsPerSample) - /* : base(bitsPerSample, null) */ { this.bitsPerSampleR = bitsPerSample[0]; this.bitsPerSampleG = bitsPerSample[1]; diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs index 0a7941dfbc..d78b06aa7f 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs +++ b/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(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(); + case TiffColorType.Rgb888: DebugGuard.IsTrue( bitsPerSample.Length == 3 diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs index 484d231633..089dc31ade 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs @@ -63,6 +63,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation /// Rgb888, + /// + /// RGB color image with 4 bits for each channel. + /// + Rgb444, + /// /// RGB Full Color. Planar configuration of data. /// diff --git a/src/ImageSharp/Formats/Tiff/TiffBitsPerPixel.cs b/src/ImageSharp/Formats/Tiff/TiffBitsPerPixel.cs index 35a9a590bb..289637fc32 100644 --- a/src/ImageSharp/Formats/Tiff/TiffBitsPerPixel.cs +++ b/src/ImageSharp/Formats/Tiff/TiffBitsPerPixel.cs @@ -23,6 +23,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff /// Bit8 = 8, + /// + /// 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. + /// + Bit12 = 12, + /// /// 24 bits per pixel. One byte for each color channel. /// diff --git a/src/ImageSharp/Formats/Tiff/TiffBitsPerSample.cs b/src/ImageSharp/Formats/Tiff/TiffBitsPerSample.cs index bc74cbc5fb..992a5ad6eb 100644 --- a/src/ImageSharp/Formats/Tiff/TiffBitsPerSample.cs +++ b/src/ImageSharp/Formats/Tiff/TiffBitsPerSample.cs @@ -28,6 +28,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff /// Bit8 = 8, + /// + /// Twelve bits per sample, each channel has 4 bits. + /// + Bit12 = 12, + /// /// 24 bits per sample, each color channel has 8 Bits. /// diff --git a/src/ImageSharp/Formats/Tiff/TiffBitsPerSampleExtensions.cs b/src/ImageSharp/Formats/Tiff/TiffBitsPerSampleExtensions.cs index 5c4c374bef..32ef547ba4 100644 --- a/src/ImageSharp/Formats/Tiff/TiffBitsPerSampleExtensions.cs +++ b/src/ImageSharp/Formats/Tiff/TiffBitsPerSampleExtensions.cs @@ -23,6 +23,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff return TiffConstants.BitsPerSample4Bit; case TiffBitsPerSample.Bit8: return TiffConstants.BitsPerSample8Bit; + case TiffBitsPerSample.Bit12: + return TiffConstants.BitsPerSampleRgb4Bit; case TiffBitsPerSample.Bit24: return TiffConstants.BitsPerSampleRgb8Bit; @@ -48,6 +50,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff return TiffBitsPerSample.Bit24; } + if (bitsPerSample[0] == TiffConstants.BitsPerSampleRgb4Bit[0] && + bitsPerSample[1] == TiffConstants.BitsPerSampleRgb4Bit[1] && + bitsPerSample[2] == TiffConstants.BitsPerSampleRgb4Bit[2]) + { + return TiffBitsPerSample.Bit12; + } + break; case 1: diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs index 50882c0072..fadb4f7c2e 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs @@ -294,7 +294,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff int top = rowsPerStrip * stripIndex; 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; } diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs index b5f3e7cf1e..1b48cd08f6 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System; using System.Linq; using SixLabors.ImageSharp.Formats.Tiff.Compression; using SixLabors.ImageSharp.Formats.Tiff.Constants; @@ -179,7 +180,18 @@ namespace SixLabors.ImageSharp.Formats.Tiff 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 { @@ -274,8 +286,9 @@ namespace SixLabors.ImageSharp.Formats.Tiff TiffBitsPerPixel.Bit1 => TiffBitsPerSample.Bit1, TiffBitsPerPixel.Bit4 => TiffBitsPerSample.Bit4, TiffBitsPerPixel.Bit8 => TiffBitsPerSample.Bit8, + TiffBitsPerPixel.Bit12 => TiffBitsPerSample.Bit12, TiffBitsPerPixel.Bit24 => TiffBitsPerSample.Bit24, - _ => TiffBitsPerSample.Bit24, + _ => throw new NotSupportedException("The bits per pixel are not supported"), }; } } diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs index 74c516f63b..6b5ca0086f 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs @@ -306,7 +306,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff case TiffBitsPerPixel.Bit1: 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); break; } @@ -319,6 +319,10 @@ namespace SixLabors.ImageSharp.Formats.Tiff case TiffBitsPerPixel.Bit8: this.SetEncoderOptions(bitsPerPixel, photometricInterpretation ?? TiffPhotometricInterpretation.BlackIsZero, compression, predictor); 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: this.SetEncoderOptions(bitsPerPixel, TiffPhotometricInterpretation.Rgb, compression, predictor); break; diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs index 09605bc690..9bc0792c40 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs @@ -66,8 +66,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff Value = SoftwareValue }; - this.collector.Add(width); - this.collector.Add(height); + this.collector.AddOrReplace(width); + this.collector.AddOrReplace(height); this.ProcessResolution(image.Metadata, rootFrameExifProfile); this.ProcessProfiles(image.Metadata, rootFrameExifProfile, rootFrameXmpBytes); @@ -227,7 +227,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff exifProfile.RemoveValue(ExifTag.IccProfile); } - TiffMetadata tiffMetadata = imageMetadata.GetTiffMetadata(); if (xmpProfile != null) { var xmp = new ExifByteArray(ExifTagValue.XMP, ExifDataType.Byte) @@ -252,6 +251,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff public void Process(TiffEncoderCore encoder) { + var planarConfig = new ExifShort(ExifTagValue.PlanarConfiguration) + { + Value = (ushort)TiffPlanarConfiguration.Chunky + }; + var samplesPerPixel = new ExifLong(ExifTagValue.SamplesPerPixel) { Value = GetSamplesPerPixel(encoder) @@ -274,6 +278,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff Value = (ushort)encoder.PhotometricInterpretation }; + this.collector.AddOrReplace(planarConfig); this.collector.AddOrReplace(samplesPerPixel); this.collector.AddOrReplace(bitPerSample); this.collector.AddOrReplace(compression); diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs index 1a72046fb3..2144ddfdb3 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs +++ b/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(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + if (TestEnvironment.IsWindows) + { + TestTiffDecoder(provider, new SystemDrawingReferenceDecoder()); + } + } + [Theory] [WithFile(GrayscaleDeflateMultistrip, PixelTypes.Rgba32)] [WithFile(RgbDeflateMultistrip, PixelTypes.Rgba32)] diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs index 3aded7b0e3..68244b3b12 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs +++ b/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.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.Make).Value, encodedImageExifProfile.GetValue(ExifTag.Make).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); } } } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 09394d4ea0..d1c29489fb 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/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 Flower4BitPalette = "Tiff/flower-palette-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 SmallRgbLzw = "Tiff/rgb_small_lzw.tiff"; diff --git a/tests/Images/Input/Tiff/flower-rgb-contig-04.tiff b/tests/Images/Input/Tiff/flower-rgb-contig-04.tiff new file mode 100644 index 0000000000..d9a141f29a --- /dev/null +++ b/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 diff --git a/tests/Images/Input/Tiff/flower-rgb-planar-04.tiff b/tests/Images/Input/Tiff/flower-rgb-planar-04.tiff new file mode 100644 index 0000000000..7a2270e486 --- /dev/null +++ b/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