Skip to content

Commit

Permalink
Merge pull request #2339 from ptasev/pt/tiff-encoder-l16
Browse files Browse the repository at this point in the history
Added L16 support to tiff encoder
  • Loading branch information
JimBobSquarePants committed Feb 3, 2023
2 parents 52efdbf + 9ed23d9 commit 8cf4991
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 21 deletions.
31 changes: 31 additions & 0 deletions src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public static void ApplyHorizontalPrediction(Span<byte> rows, int width, int bit
{
ApplyHorizontalPrediction8Bit(rows, width);
}
else if (bitsPerPixel == 16)
{
// Assume rows are L16 grayscale since that's currently the only way 16 bits is supported by encoder
ApplyHorizontalPrediction16Bit(rows, width);
}
else if (bitsPerPixel == 24)
{
ApplyHorizontalPrediction24Bit(rows, width);
Expand Down Expand Up @@ -102,6 +107,32 @@ private static void ApplyHorizontalPrediction24Bit(Span<byte> rows, int width)
}
}

/// <summary>
/// Applies a horizontal predictor to the L16 row.
/// Make use of the fact that many continuous-tone images rarely vary much in pixel value from one pixel to the next.
/// In such images, if we replace the pixel values by differences between consecutive pixels, many of the differences should be 0, plus
/// or minus 1, and so on.This reduces the apparent information content and allows LZW to encode the data more compactly.
/// </summary>
/// <param name="rows">The L16 pixel rows.</param>
/// <param name="width">The width.</param>
[MethodImpl(InliningOptions.ShortMethod)]
private static void ApplyHorizontalPrediction16Bit(Span<byte> rows, int width)
{
DebugGuard.IsTrue(rows.Length % width == 0, "Values must be equals");
int height = rows.Length / width;
for (int y = 0; y < height; y++)
{
Span<byte> rowSpan = rows.Slice(y * width, width);
Span<L16> rowL16 = MemoryMarshal.Cast<byte, L16>(rowSpan);

for (int x = rowL16.Length - 1; x >= 1; x--)
{
ushort val = (ushort)(rowL16[x].PackedValue - rowL16[x - 1].PackedValue);
rowL16[x].PackedValue = val;
}
}
}

/// <summary>
/// Applies a horizontal predictor to a gray pixel row.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ internal static class TiffConstants
/// </summary>
public static readonly TiffBitsPerSample BitsPerSample8Bit = new TiffBitsPerSample(8, 0, 0);

/// <summary>
/// The bits per sample for 16-bit grayscale images.
/// </summary>
public static readonly TiffBitsPerSample BitsPerSample16Bit = new TiffBitsPerSample(16, 0, 0);

/// <summary>
/// The bits per sample for color images with 8 bits for each color channel.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Tiff/TiffBitsPerPixel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public enum TiffBitsPerPixel
/// <summary>
/// 16 bits per pixel, for gray images.
///
/// Note: The TiffEncoder does not yet support 16 bits per color channel and will default to 24 bits per pixel instead.
/// Note: The TiffEncoder does not yet support 16 bits per color channel and will default to 16 bits grayscale instead.
/// </summary>
Bit16 = 16,

Expand Down
20 changes: 18 additions & 2 deletions src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,14 @@ private long WriteIfd(TiffStreamWriter writer, List<IExifValue> entries)
case TiffBitsPerPixel.Bit8:
this.SetEncoderOptions(bitsPerPixel, photometricInterpretation ?? TiffPhotometricInterpretation.BlackIsZero, compression, predictor);
break;
case TiffBitsPerPixel.Bit16:
// Assume desire to encode as L16 grayscale
this.SetEncoderOptions(bitsPerPixel, TiffPhotometricInterpretation.BlackIsZero, compression, predictor);
break;
case TiffBitsPerPixel.Bit6:
case TiffBitsPerPixel.Bit10:
case TiffBitsPerPixel.Bit12:
case TiffBitsPerPixel.Bit14:
case TiffBitsPerPixel.Bit16:
case TiffBitsPerPixel.Bit30:
case TiffBitsPerPixel.Bit36:
case TiffBitsPerPixel.Bit42:
Expand Down Expand Up @@ -413,13 +416,20 @@ private long WriteIfd(TiffStreamWriter writer, List<IExifValue> entries)
return;
}

// At the moment only 8 and 32 bits per pixel can be preserved by the tiff encoder.
// At the moment only 8, 16 and 32 bits per pixel can be preserved by the tiff encoder.
if (inputBitsPerPixel == 8)
{
this.SetEncoderOptions(TiffBitsPerPixel.Bit8, TiffPhotometricInterpretation.BlackIsZero, compression, predictor);
return;
}

if (inputBitsPerPixel == 16)
{
// Assume desire to encode as L16 grayscale
this.SetEncoderOptions(TiffBitsPerPixel.Bit16, TiffPhotometricInterpretation.BlackIsZero, compression, predictor);
return;
}

this.SetEncoderOptions(TiffBitsPerPixel.Bit24, TiffPhotometricInterpretation.Rgb, compression, predictor);
return;
}
Expand All @@ -434,6 +444,12 @@ private long WriteIfd(TiffStreamWriter writer, List<IExifValue> entries)
return;
}

if (inputBitsPerPixel == 16)
{
this.SetEncoderOptions(TiffBitsPerPixel.Bit16, photometricInterpretation, compression, predictor);
return;
}

this.SetEncoderOptions(TiffBitsPerPixel.Bit8, photometricInterpretation, compression, predictor);
return;

Expand Down
20 changes: 10 additions & 10 deletions src/ImageSharp/Formats/Tiff/TiffEncoderEntriesCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,20 +343,20 @@ private static ushort[] GetBitsPerSampleValue(TiffEncoderCore encoder)
return TiffConstants.BitsPerSampleRgb8Bit.ToArray();

case TiffPhotometricInterpretation.WhiteIsZero:
if (encoder.BitsPerPixel == TiffBitsPerPixel.Bit1)
return encoder.BitsPerPixel switch
{
return TiffConstants.BitsPerSample1Bit.ToArray();
}

return TiffConstants.BitsPerSample8Bit.ToArray();
TiffBitsPerPixel.Bit1 => TiffConstants.BitsPerSample1Bit.ToArray(),
TiffBitsPerPixel.Bit16 => TiffConstants.BitsPerSample16Bit.ToArray(),
_ => TiffConstants.BitsPerSample8Bit.ToArray()
};

case TiffPhotometricInterpretation.BlackIsZero:
if (encoder.BitsPerPixel == TiffBitsPerPixel.Bit1)
return encoder.BitsPerPixel switch
{
return TiffConstants.BitsPerSample1Bit.ToArray();
}

return TiffConstants.BitsPerSample8Bit.ToArray();
TiffBitsPerPixel.Bit1 => TiffConstants.BitsPerSample1Bit.ToArray(),
TiffBitsPerPixel.Bit16 => TiffConstants.BitsPerSample16Bit.ToArray(),
_ => TiffConstants.BitsPerSample8Bit.ToArray()
};

default:
return TiffConstants.BitsPerSampleRgb8Bit.ToArray();
Expand Down
9 changes: 5 additions & 4 deletions src/ImageSharp/Formats/Tiff/Writers/TiffColorWriterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ internal static class TiffColorWriterFactory
return new TiffPaletteWriter<TPixel>(image, quantizer, pixelSamplingStrategy, memoryAllocator, configuration, entriesCollector, bitsPerPixel);
case TiffPhotometricInterpretation.BlackIsZero:
case TiffPhotometricInterpretation.WhiteIsZero:
if (bitsPerPixel == 1)
return bitsPerPixel switch
{
return new TiffBiColorWriter<TPixel>(image, memoryAllocator, configuration, entriesCollector);
}
1 => new TiffBiColorWriter<TPixel>(image, memoryAllocator, configuration, entriesCollector),
16 => new TiffGrayL16Writer<TPixel>(image, memoryAllocator, configuration, entriesCollector),
_ => new TiffGrayWriter<TPixel>(image, memoryAllocator, configuration, entriesCollector)
};

return new TiffGrayWriter<TPixel>(image, memoryAllocator, configuration, entriesCollector);
default:
return new TiffRgbWriter<TPixel>(image, memoryAllocator, configuration, entriesCollector);
}
Expand Down
22 changes: 22 additions & 0 deletions src/ImageSharp/Formats/Tiff/Writers/TiffGrayL16Writer{TPixel}.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;

namespace SixLabors.ImageSharp.Formats.Tiff.Writers;

internal sealed class TiffGrayL16Writer<TPixel> : TiffCompositeColorWriter<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
public TiffGrayL16Writer(ImageFrame<TPixel> image, MemoryAllocator memoryAllocator, Configuration configuration, TiffEncoderEntriesCollector entriesCollector)
: base(image, memoryAllocator, configuration, entriesCollector)
{
}

/// <inheritdoc />
public override int BitsPerPixel => 16;

/// <inheritdoc />
protected override void EncodePixels(Span<TPixel> pixels, Span<byte> buffer) => PixelOperations<TPixel>.Instance.ToL16Bytes(this.Configuration, pixels, buffer, pixels.Length);
}
7 changes: 6 additions & 1 deletion tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ public void ThrowsNotSupported<TPixel>(TestImageProvider<TPixel> provider)
[InlineData(RgbUncompressed, 24, 256, 256, 300, 300, PixelResolutionUnit.PixelsPerInch)]
[InlineData(SmallRgbDeflate, 24, 32, 32, 96, 96, PixelResolutionUnit.PixelsPerInch)]
[InlineData(Calliphora_GrayscaleUncompressed, 8, 200, 298, 96, 96, PixelResolutionUnit.PixelsPerInch)]
[InlineData(Calliphora_GrayscaleUncompressed16Bit, 16, 200, 298, 96, 96, PixelResolutionUnit.PixelsPerInch)]
[InlineData(Flower4BitPalette, 4, 73, 43, 72, 72, PixelResolutionUnit.PixelsPerInch)]
public void Identify(string imagePath, int expectedPixelSize, int expectedWidth, int expectedHeight, double expectedHResolution, double expectedVResolution, PixelResolutionUnit expectedResolutionUnit)
{
TestFile testFile = TestFile.Create(imagePath);
using MemoryStream stream = new(testFile.Bytes, false);
ImageInfo info = Image.Identify(stream);

Assert.Equal(expectedPixelSize, info.PixelType?.BitsPerPixel);
Assert.Equal(expectedPixelSize, info.PixelType.BitsPerPixel);
Assert.Equal(expectedWidth, info.Width);
Assert.Equal(expectedHeight, info.Height);
Assert.NotNull(info.Metadata);
Expand Down Expand Up @@ -64,6 +65,7 @@ public void ByteOrder(string imagePath, ByteOrder expectedByteOrder)
[Theory]
[WithFile(RgbUncompressed, PixelTypes.Rgba32)]
[WithFile(Calliphora_GrayscaleUncompressed, PixelTypes.Rgba32)]
[WithFile(Calliphora_GrayscaleUncompressed16Bit, PixelTypes.Rgba32)]
[WithFile(Calliphora_RgbUncompressed, PixelTypes.Rgba32)]
[WithFile(Calliphora_BiColorUncompressed, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_Uncompressed<TPixel>(TestImageProvider<TPixel> provider)
Expand Down Expand Up @@ -599,6 +601,8 @@ public void TiffDecoder_CanDecode_128Bit_WithUnassociatedAlpha<TPixel>(TestImage
[WithFile(RgbDeflateMultistrip, PixelTypes.Rgba32)]
[WithFile(Calliphora_GrayscaleDeflate, PixelTypes.Rgba32)]
[WithFile(Calliphora_GrayscaleDeflate_Predictor, PixelTypes.Rgba32)]
[WithFile(Calliphora_GrayscaleDeflate16Bit, PixelTypes.Rgba32)]
[WithFile(Calliphora_GrayscaleDeflate_Predictor16Bit, PixelTypes.Rgba32)]
[WithFile(Calliphora_RgbDeflate_Predictor, PixelTypes.Rgba32)]
[WithFile(RgbDeflate, PixelTypes.Rgba32)]
[WithFile(RgbDeflatePredictor, PixelTypes.Rgba32)]
Expand All @@ -615,6 +619,7 @@ public void TiffDecoder_CanDecode_DeflateCompressed<TPixel>(TestImageProvider<TP
[WithFile(Calliphora_RgbPaletteLzw_Predictor, PixelTypes.Rgba32)]
[WithFile(Calliphora_RgbLzwPredictor, PixelTypes.Rgba32)]
[WithFile(Calliphora_GrayscaleLzw_Predictor, PixelTypes.Rgba32)]
[WithFile(Calliphora_GrayscaleLzw_Predictor16Bit, PixelTypes.Rgba32)]
[WithFile(SmallRgbLzw, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_LzwCompressed<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
Expand Down
Loading

0 comments on commit 8cf4991

Please sign in to comment.