diff --git a/src/ImageSharp/Formats/Tiff/Constants/TiffCompression.cs b/src/ImageSharp/Formats/Tiff/Constants/TiffCompression.cs
index 6a6bd7911..40cc76845 100644
--- a/src/ImageSharp/Formats/Tiff/Constants/TiffCompression.cs
+++ b/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.
///
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.
///
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.
///
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.
///
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.
///
ItuTRecT43 = 10
}
diff --git a/src/ImageSharp/Formats/Tiff/Constants/TiffPhotometricInterpretation.cs b/src/ImageSharp/Formats/Tiff/Constants/TiffPhotometricInterpretation.cs
index d5f234c3f..42d5c4140 100644
--- a/src/ImageSharp/Formats/Tiff/Constants/TiffPhotometricInterpretation.cs
+++ b/src/ImageSharp/Formats/Tiff/Constants/TiffPhotometricInterpretation.cs
@@ -19,12 +19,12 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants
BlackIsZero = 1,
///
- /// RGB
+ /// RGB image.
///
Rgb = 2,
///
- /// Palette Color
+ /// Palette Color.
///
PaletteColor = 3,
diff --git a/src/ImageSharp/Formats/Tiff/ITiffEncoderOptions.cs b/src/ImageSharp/Formats/Tiff/ITiffEncoderOptions.cs
index c823b50c2..75bea696f 100644
--- a/src/ImageSharp/Formats/Tiff/ITiffEncoderOptions.cs
+++ b/src/ImageSharp/Formats/Tiff/ITiffEncoderOptions.cs
@@ -13,9 +13,9 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
internal interface ITiffEncoderOptions
{
///
- /// Gets or sets the number of bits per pixel.
+ /// Gets the number of bits per pixel.
///
- TiffBitsPerPixel? BitsPerPixel { get; set; }
+ TiffBitsPerPixel? BitsPerPixel { get; }
///
/// Gets the compression type to use.
diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
index ce55ecd1f..1bff4aecd 100644
--- a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
+++ b/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;
diff --git a/src/ImageSharp/Formats/Tiff/TiffMetadata.cs b/src/ImageSharp/Formats/Tiff/TiffMetadata.cs
index e6b0bf868..6b5d4e1ca 100644
--- a/src/ImageSharp/Formats/Tiff/TiffMetadata.cs
+++ b/src/ImageSharp/Formats/Tiff/TiffMetadata.cs
@@ -33,24 +33,25 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
}
///
- /// Gets the byte order.
+ /// Gets or sets the byte order.
///
- public ByteOrder ByteOrder { get; internal set; }
+ public ByteOrder ByteOrder { get; set; }
///
- /// Gets the number of bits per pixel.
+ /// Gets or sets the number of bits per pixel.
///
- public TiffBitsPerPixel BitsPerPixel { get; internal set; } = TiffBitsPerPixel.Bit24;
+ public TiffBitsPerPixel? BitsPerPixel { get; set; }
///
- /// Gets the compression used to create the TIFF file.
+ /// Gets or sets the compression used to create the TIFF file.
+ /// Defaults to None.
///
- public TiffCompression Compression { get; internal set; } = TiffCompression.None;
+ public TiffCompression Compression { get; set; } = TiffCompression.None;
///
- /// 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.
///
- public TiffPhotometricInterpretation PhotometricInterpretation { get; internal set; }
+ public TiffPhotometricInterpretation PhotometricInterpretation { get; set; }
///
/// Gets or sets the XMP profile.
diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs
index 2a1f800a2..b611241f2 100644
--- a/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs
+++ b/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(10, 10);
+ using var memStream = new MemoryStream();
+
+ // act
+ input.Save(memStream, tiffEncoder);
+
+ // assert
+ memStream.Position = 0;
+ using var output = Image.Load(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(10, 10);
+ using var memStream = new MemoryStream();
+
+ // act
+ input.Save(memStream, tiffEncoder);
+
+ // assert
+ memStream.Position = 0;
+ using var output = Image.Load(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(TestImageProvider provider, TiffBitsPerPixel expectedBitsPerPixel)
+ public void TiffEncoder_PreservesBitsPerPixel(TestImageProvider provider, TiffBitsPerPixel expectedBitsPerPixel)
where TPixel : unmanaged, IPixel
{
// arrange
diff --git a/tests/ImageSharp.Tests/Formats/Tiff/Utils/TiffWriterTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/Utils/TiffWriterTests.cs
index b1389cec5..df84c51c8 100644
--- a/tests/ImageSharp.Tests/Formats/Tiff/Utils/TiffWriterTests.cs
+++ b/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());
}