Browse Source

Add setters for tiff metadata properties

pull/1553/head
Brian Popow 5 years ago
parent
commit
13fbde9213
  1. 10
      src/ImageSharp/Formats/Tiff/Constants/TiffCompression.cs
  2. 4
      src/ImageSharp/Formats/Tiff/Constants/TiffPhotometricInterpretation.cs
  3. 4
      src/ImageSharp/Formats/Tiff/ITiffEncoderOptions.cs
  4. 6
      src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
  5. 17
      src/ImageSharp/Formats/Tiff/TiffMetadata.cs
  6. 56
      tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs
  7. 8
      tests/ImageSharp.Tests/Formats/Tiff/Utils/TiffWriterTests.cs

10
src/ImageSharp/Formats/Tiff/Constants/TiffCompression.cs

@ -45,7 +45,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants
/// JPEG compression - obsolete (see Section 22 of the TIFF 6.0 specification).
///
/// Note: The TIFF encoder does not support this compression and will default to use no compression instead,
/// if this is choosen.
/// if this is chosen.
/// </summary>
OldJpeg = 6,
@ -53,7 +53,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants
/// JPEG compression (see TIFF Specification, supplement 2).
///
/// Note: The TIFF encoder does not yet support this compression and will default to use no compression instead,
/// if this is choosen.
/// if this is chosen.
/// </summary>
Jpeg = 7,
@ -66,7 +66,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants
/// Deflate compression - old.
///
/// Note: The TIFF encoder does not support this compression and will default to use no compression instead,
/// if this is choosen.
/// if this is chosen.
/// </summary>
OldDeflate = 32946,
@ -74,7 +74,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants
/// ITU-T Rec. T.82 coding, applying ITU-T Rec. T.85 (JBIG) (see RFC2301).
///
/// Note: The TIFF encoder does not yet support this compression and will default to use no compression instead,
/// if this is choosen.
/// if this is chosen.
/// </summary>
ItuTRecT82 = 9,
@ -82,7 +82,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants
/// ITU-T Rec. T.43 representation, using ITU-T Rec. T.82 (JBIG) (see RFC2301).
///
/// Note: The TIFF encoder does not yet support this compression and will default to use no compression instead,
/// if this is choosen.
/// if this is chosen.
/// </summary>
ItuTRecT43 = 10
}

4
src/ImageSharp/Formats/Tiff/Constants/TiffPhotometricInterpretation.cs

@ -19,12 +19,12 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants
BlackIsZero = 1,
/// <summary>
/// RGB
/// RGB image.
/// </summary>
Rgb = 2,
/// <summary>
/// Palette Color
/// Palette Color.
/// </summary>
PaletteColor = 3,

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

@ -13,9 +13,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
internal interface ITiffEncoderOptions
{
/// <summary>
/// Gets or sets the number of bits per pixel.
/// Gets the number of bits per pixel.
/// </summary>
TiffBitsPerPixel? BitsPerPixel { get; set; }
TiffBitsPerPixel? BitsPerPixel { get; }
/// <summary>
/// Gets the compression type to use.

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

@ -293,10 +293,10 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
}
}
if (this.Mode == TiffEncodingMode.Default)
if (this.Mode == TiffEncodingMode.Default && this.BitsPerPixel != null)
{
// Preserve input bits per pixel, if no mode was specified.
switch (tiffMetadata.BitsPerPixel)
// Preserve input bits per pixel, if no encoding mode was specified.
switch (this.BitsPerPixel)
{
case TiffBitsPerPixel.Bit1:
this.Mode = TiffEncodingMode.BiColor;

17
src/ImageSharp/Formats/Tiff/TiffMetadata.cs

@ -33,24 +33,25 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
}
/// <summary>
/// Gets the byte order.
/// Gets or sets the byte order.
/// </summary>
public ByteOrder ByteOrder { get; internal set; }
public ByteOrder ByteOrder { get; set; }
/// <summary>
/// Gets the number of bits per pixel.
/// Gets or sets the number of bits per pixel.
/// </summary>
public TiffBitsPerPixel BitsPerPixel { get; internal set; } = TiffBitsPerPixel.Bit24;
public TiffBitsPerPixel? BitsPerPixel { get; set; }
/// <summary>
/// Gets the compression used to create the TIFF file.
/// Gets or sets the compression used to create the TIFF file.
/// Defaults to None.
/// </summary>
public TiffCompression Compression { get; internal set; } = TiffCompression.None;
public TiffCompression Compression { get; set; } = TiffCompression.None;
/// <summary>
/// Gets the photometric interpretation which indicates how the pixels are to be interpreted, e.g. if the image is bicolor, RGB, color paletted etc.
/// Gets or sets the photometric interpretation which indicates how the pixels are to be interpreted, e.g. if the image is bicolor, RGB, color paletted etc.
/// </summary>
public TiffPhotometricInterpretation PhotometricInterpretation { get; internal set; }
public TiffPhotometricInterpretation PhotometricInterpretation { get; set; }
/// <summary>
/// Gets or sets the XMP profile.

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

@ -30,11 +30,53 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
}
[Theory]
[InlineData(TiffEncodingMode.Default, TiffCompression.None, TiffBitsPerPixel.Bit24, TiffCompression.None)]
[InlineData(TiffEncodingMode.Rgb, TiffCompression.None, TiffBitsPerPixel.Bit24, TiffCompression.None)]
[InlineData(TiffEncodingMode.ColorPalette, TiffCompression.None, TiffBitsPerPixel.Bit8, TiffCompression.None)]
[InlineData(TiffEncodingMode.Gray, TiffCompression.None, TiffBitsPerPixel.Bit8, TiffCompression.None)]
[InlineData(TiffEncodingMode.BiColor, TiffCompression.None, TiffBitsPerPixel.Bit1, TiffCompression.None)]
[InlineData(TiffEncodingMode.Default, TiffBitsPerPixel.Bit24)]
[InlineData(TiffEncodingMode.Rgb, TiffBitsPerPixel.Bit24)]
[InlineData(TiffEncodingMode.ColorPalette, TiffBitsPerPixel.Bit8)]
[InlineData(TiffEncodingMode.Gray, TiffBitsPerPixel.Bit8)]
[InlineData(TiffEncodingMode.BiColor, TiffBitsPerPixel.Bit1)]
public void EncoderOptions_SetEncodingMode_Works(TiffEncodingMode mode, TiffBitsPerPixel expectedBitsPerPixel)
{
// arrange
var tiffEncoder = new TiffEncoder { Mode = mode };
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>(Configuration, memStream);
TiffMetadata meta = output.Metadata.GetTiffMetadata();
Assert.Equal(expectedBitsPerPixel, meta.BitsPerPixel);
Assert.Equal(TiffCompression.None, meta.Compression);
}
[Theory]
[InlineData(TiffBitsPerPixel.Bit24)]
[InlineData(TiffBitsPerPixel.Bit8)]
[InlineData(TiffBitsPerPixel.Bit4)]
[InlineData(TiffBitsPerPixel.Bit1)]
public void EncoderOptions_SetBitPerPixel_Works(TiffBitsPerPixel bitsPerPixel)
{
// arrange
var tiffEncoder = new TiffEncoder { BitsPerPixel = bitsPerPixel };
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>(Configuration, memStream);
TiffMetadata meta = output.Metadata.GetTiffMetadata();
Assert.Equal(bitsPerPixel, meta.BitsPerPixel);
Assert.Equal(TiffCompression.None, meta.Compression);
}
[Theory]
[InlineData(TiffEncodingMode.Default, TiffCompression.Deflate, TiffBitsPerPixel.Bit24, TiffCompression.Deflate)]
[InlineData(TiffEncodingMode.Rgb, TiffCompression.Deflate, TiffBitsPerPixel.Bit24, TiffCompression.Deflate)]
[InlineData(TiffEncodingMode.Gray, TiffCompression.Deflate, TiffBitsPerPixel.Bit8, TiffCompression.Deflate)]
@ -55,7 +97,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
[InlineData(TiffEncodingMode.Rgb, TiffCompression.Jpeg, TiffBitsPerPixel.Bit24, TiffCompression.None)]
[InlineData(TiffEncodingMode.Rgb, TiffCompression.OldDeflate, TiffBitsPerPixel.Bit24, TiffCompression.None)]
[InlineData(TiffEncodingMode.Rgb, TiffCompression.OldJpeg, TiffBitsPerPixel.Bit24, TiffCompression.None)]
public void EncoderOptions_Work(TiffEncodingMode mode, TiffCompression compression, TiffBitsPerPixel expectedBitsPerPixel, TiffCompression expectedCompression)
public void EncoderOptions_SetEncodingModeAndCompression_Works(TiffEncodingMode mode, TiffCompression compression, TiffBitsPerPixel expectedBitsPerPixel, TiffCompression expectedCompression)
{
// arrange
var tiffEncoder = new TiffEncoder { Mode = mode, Compression = compression };
@ -80,7 +122,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
[WithFile(Rgb4BitPalette, PixelTypes.Rgba32, TiffBitsPerPixel.Bit4)]
[WithFile(RgbPalette, PixelTypes.Rgba32, TiffBitsPerPixel.Bit8)]
[WithFile(Calliphora_PaletteUncompressed, PixelTypes.Rgba32, TiffBitsPerPixel.Bit8)]
public void TiffEncoder_PreserveBitsPerPixel<TPixel>(TestImageProvider<TPixel> provider, TiffBitsPerPixel expectedBitsPerPixel)
public void TiffEncoder_PreservesBitsPerPixel<TPixel>(TestImageProvider<TPixel> provider, TiffBitsPerPixel expectedBitsPerPixel)
where TPixel : unmanaged, IPixel<TPixel>
{
// arrange

8
tests/ImageSharp.Tests/Formats/Tiff/Utils/TiffWriterTests.cs

@ -3,7 +3,6 @@
using System.IO;
using SixLabors.ImageSharp.Formats.Experimental.Tiff.Writers;
using SixLabors.ImageSharp.Memory;
using Xunit;
@ -12,9 +11,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff.Utils
[Trait("Format", "Tiff")]
public class TiffWriterTests
{
private static readonly MemoryAllocator MemoryAllocator = new ArrayPoolMemoryAllocator();
private static readonly Configuration Configuration = Configuration.Default;
[Fact]
public void IsLittleEndian_IsTrueOnWindows()
{
@ -40,7 +36,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff.Utils
{
using var stream = new MemoryStream();
using var writer = new TiffStreamWriter(stream);
writer.Write((byte)42);
writer.Write(42);
Assert.Equal(new byte[] { 42 }, stream.ToArray());
}
@ -60,7 +56,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff.Utils
{
using var stream = new MemoryStream();
using var writer = new TiffStreamWriter(stream);
writer.Write((ushort)1234);
writer.Write(1234);
Assert.Equal(new byte[] { 0xD2, 0x04 }, stream.ToArray());
}

Loading…
Cancel
Save