Browse Source

Remove BitsPerPixel from TiffMetaData, its already present in TiffFrameMetadata

pull/1553/head
Brian Popow 5 years ago
parent
commit
b0ecabbbd7
  1. 2
      src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs
  2. 2
      src/ImageSharp/Formats/Tiff/TiffDecoderMetadataCreator.cs
  3. 26
      src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
  4. 6
      src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs
  5. 11
      src/ImageSharp/Formats/Tiff/TiffMetadata.cs
  6. 25
      tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs
  7. 21
      tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs

2
src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs

@ -157,7 +157,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
int width = GetImageWidth(rootFrameExifProfile);
int height = GetImageHeight(rootFrameExifProfile);
return new ImageInfo(new PixelTypeInfo(root.BitsPerPixel), width, height, metadata);
return new ImageInfo(new PixelTypeInfo((int)root.BitsPerPixel), width, height, metadata);
}
/// <summary>

2
src/ImageSharp/Formats/Tiff/TiffDecoderMetadataCreator.cs

@ -33,7 +33,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff
TiffMetadata tiffMetadata = imageMetaData.GetTiffMetadata();
tiffMetadata.ByteOrder = byteOrder;
tiffMetadata.BitsPerPixel = GetBitsPerPixel(rootFrameMetadata);
if (!ignoreMetadata)
{
@ -82,7 +81,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff
TiffMetadata tiffMetadata = imageMetaData.GetTiffMetadata();
tiffMetadata.ByteOrder = byteOrder;
tiffMetadata.BitsPerPixel = GetBitsPerPixel(rootFrameMetadata);
return imageMetaData;
}

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

@ -110,21 +110,25 @@ namespace SixLabors.ImageSharp.Formats.Tiff
Guard.NotNull(stream, nameof(stream));
this.configuration = image.GetConfiguration();
ImageMetadata metadata = image.Metadata;
TiffMetadata tiffMetadata = metadata.GetTiffMetadata();
ExifProfile rootFrameExifProfile = image.Frames.RootFrame.Metadata.ExifProfile;
TiffPhotometricInterpretation rootFramePhotometricInterpretation = GetRootFramePhotometricInterpretation(image);
TiffPhotometricInterpretation photometricInterpretation = this.Mode == TiffEncodingMode.ColorPalette
? TiffPhotometricInterpretation.PaletteColor : rootFramePhotometricInterpretation;
TiffBitsPerPixel? rootFrameBitsPerPixel = null;
if (rootFrameExifProfile != null)
{
rootFrameBitsPerPixel = new TiffFrameMetadata(rootFrameExifProfile).BitsPerPixel;
}
this.SetMode(tiffMetadata, photometricInterpretation);
this.SetBitsPerPixel(tiffMetadata);
this.SetMode(rootFrameBitsPerPixel, photometricInterpretation);
this.SetBitsPerPixel(rootFrameBitsPerPixel);
this.SetPhotometricInterpretation();
using (var writer = new TiffStreamWriter(stream))
{
long firstIfdMarker = this.WriteHeader(writer);
// TODO: multiframing is not support
// TODO: multiframing is not supported
this.WriteImage(writer, image, firstIfdMarker);
}
}
@ -264,7 +268,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
return nextIfdMarker;
}
private void SetMode(TiffMetadata tiffMetadata, TiffPhotometricInterpretation photometricInterpretation)
private void SetMode(TiffBitsPerPixel? rootFrameBitsPerPixel, TiffPhotometricInterpretation photometricInterpretation)
{
// Make sure, that the fax compressions are only used together with the BiColor mode.
if (this.CompressionType == TiffCompression.CcittGroup3Fax || this.CompressionType == TiffCompression.Ccitt1D)
@ -282,10 +286,10 @@ namespace SixLabors.ImageSharp.Formats.Tiff
}
}
if (this.Mode == TiffEncodingMode.Default && tiffMetadata.BitsPerPixel != null)
if (this.Mode == TiffEncodingMode.Default && rootFrameBitsPerPixel.HasValue)
{
// Preserve input bits per pixel, if no encoding mode was specified.
this.SetModeWithBitsPerPixel(tiffMetadata.BitsPerPixel, photometricInterpretation);
// Preserve input bits per pixel, if no encoding mode was specified and the input image has a bits per pixel set.
this.SetModeWithBitsPerPixel(rootFrameBitsPerPixel, photometricInterpretation);
return;
}
@ -319,9 +323,9 @@ namespace SixLabors.ImageSharp.Formats.Tiff
}
}
private void SetBitsPerPixel(TiffMetadata tiffMetadata)
private void SetBitsPerPixel(TiffBitsPerPixel? rootFrameBitsPerPixel)
{
this.BitsPerPixel ??= tiffMetadata.BitsPerPixel;
this.BitsPerPixel ??= rootFrameBitsPerPixel;
switch (this.Mode)
{
case TiffEncodingMode.BiColor:

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

@ -31,7 +31,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// Initializes a new instance of the <see cref="TiffFrameMetadata"/> class.
/// </summary>
/// <param name="frameTags">The Tiff frame directory tags.</param>
public TiffFrameMetadata(ExifProfile frameTags) => this.Initialize(frameTags);
public TiffFrameMetadata(ExifProfile frameTags) => this.Initialize(frameTags ?? new ExifProfile());
/// <summary>
/// Gets or sets a general indication of the kind of data contained in this subfile.
@ -51,7 +51,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// <summary>
/// Gets or sets the bits per pixel.
/// </summary>
public int BitsPerPixel { get; set; }
public TiffBitsPerPixel BitsPerPixel { get; set; }
/// <summary>
/// Gets or sets the compression scheme used on the image data.
@ -189,7 +189,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
this.BitsPerSample = bits.GetBitsPerSample();
}
this.BitsPerPixel = this.BitsPerSample.GetValueOrDefault().BitsPerPixel();
this.BitsPerPixel = (TiffBitsPerPixel)this.BitsPerSample.GetValueOrDefault().BitsPerPixel();
}
/// <summary>

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

@ -19,22 +19,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// Initializes a new instance of the <see cref="TiffMetadata"/> class.
/// </summary>
/// <param name="other">The metadata to create an instance from.</param>
private TiffMetadata(TiffMetadata other)
{
this.ByteOrder = other.ByteOrder;
this.BitsPerPixel = other.BitsPerPixel;
}
private TiffMetadata(TiffMetadata other) => this.ByteOrder = other.ByteOrder;
/// <summary>
/// Gets or sets the byte order.
/// </summary>
public ByteOrder ByteOrder { get; set; }
/// <summary>
/// Gets or sets the number of bits per pixel for the root frame.
/// </summary>
public TiffBitsPerPixel? BitsPerPixel { get; set; }
/// <inheritdoc/>
public IDeepCloneable DeepClone() => new TiffMetadata(this);
}

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

@ -49,9 +49,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
// assert
memStream.Position = 0;
using var output = Image.Load<Rgba32>(Configuration, memStream);
TiffMetadata meta = output.Metadata.GetTiffMetadata();
ExifProfile exifProfile = output.Frames.RootFrame.Metadata.ExifProfile;
Assert.Equal(expectedBitsPerPixel, meta.BitsPerPixel);
var frameMetaData = new TiffFrameMetadata(exifProfile);
Assert.Equal(expectedBitsPerPixel, frameMetaData.BitsPerPixel);
Assert.Equal(TiffCompression.None, (TiffCompression)exifProfile.GetValue(ExifTag.Compression).Value);
}
@ -73,9 +73,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
// assert
memStream.Position = 0;
using var output = Image.Load<Rgba32>(Configuration, memStream);
TiffMetadata meta = output.Metadata.GetTiffMetadata();
ExifProfile exifProfile = output.Frames.RootFrame.Metadata.ExifProfile;
Assert.Equal(bitsPerPixel, meta.BitsPerPixel);
var frameMetaData = new TiffFrameMetadata(exifProfile);
Assert.Equal(bitsPerPixel, frameMetaData.BitsPerPixel);
Assert.Equal(TiffCompression.None, (TiffCompression)exifProfile.GetValue(ExifTag.Compression).Value);
}
@ -113,9 +113,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
// assert
memStream.Position = 0;
using var output = Image.Load<Rgba32>(Configuration, memStream);
TiffMetadata meta = output.Metadata.GetTiffMetadata();
ExifProfile exifProfile = output.Frames.RootFrame.Metadata.ExifProfile;
Assert.Equal(expectedBitsPerPixel, meta.BitsPerPixel);
var frameMetaData = new TiffFrameMetadata(exifProfile);
Assert.Equal(expectedBitsPerPixel, frameMetaData.BitsPerPixel);
Assert.Equal(expectedCompression, (TiffCompression)exifProfile.GetValue(ExifTag.Compression).Value);
}
@ -140,8 +140,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
// assert
memStream.Position = 0;
using var output = Image.Load<Rgba32>(Configuration, memStream);
TiffMetadata meta = output.Metadata.GetTiffMetadata();
Assert.Equal(expectedBitsPerPixel, meta.BitsPerPixel);
ExifProfile exifProfile = output.Frames.RootFrame.Metadata.ExifProfile;
var frameMetaData = new TiffFrameMetadata(exifProfile);
Assert.Equal(expectedBitsPerPixel, frameMetaData.BitsPerPixel);
}
[Theory]
@ -163,9 +164,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
// assert
memStream.Position = 0;
using var output = Image.Load<Rgba32>(Configuration, memStream);
TiffMetadata meta = output.Metadata.GetTiffMetadata();
ExifProfile exifProfile = output.Frames.RootFrame.Metadata.ExifProfile;
Assert.Equal(TiffBitsPerPixel.Bit1, meta.BitsPerPixel);
var frameMetaData = new TiffFrameMetadata(exifProfile);
Assert.Equal(TiffBitsPerPixel.Bit1, frameMetaData.BitsPerPixel);
Assert.Equal(expectedCompression, (TiffCompression)exifProfile.GetValue(ExifTag.Compression).Value);
}
@ -367,7 +368,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
{
// The difference must be less than one row.
int stripBytes = (int)outputMeta.StripByteCounts[i];
int widthBytes = (outputMeta.BitsPerPixel + 7) / 8 * rootFrame.Width;
int widthBytes = ((int)outputMeta.BitsPerPixel + 7) / 8 * rootFrame.Width;
Assert.True((TiffConstants.DefaultStripSize - stripBytes) < widthBytes);
}
@ -376,7 +377,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
// Compare with reference.
TestTiffEncoderCore(
provider,
(TiffBitsPerPixel)inputMeta.BitsPerPixel,
inputMeta.BitsPerPixel,
mode,
inputMeta.Compression);
}

21
tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs

@ -35,16 +35,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
{
var meta = new TiffMetadata
{
BitsPerPixel = TiffBitsPerPixel.Bit8,
ByteOrder = ByteOrder.BigEndian,
};
var clone = (TiffMetadata)meta.DeepClone();
clone.BitsPerPixel = TiffBitsPerPixel.Bit24;
clone.ByteOrder = ByteOrder.LittleEndian;
Assert.False(meta.BitsPerPixel == clone.BitsPerPixel);
Assert.False(meta.ByteOrder == clone.ByteOrder);
}
@ -71,10 +68,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
}
[Theory]
[InlineData(Calliphora_BiColorUncompressed, TiffBitsPerPixel.Bit1)]
[InlineData(GrayscaleUncompressed, TiffBitsPerPixel.Bit8)]
[InlineData(RgbUncompressed, TiffBitsPerPixel.Bit24)]
public void Identify_DetectsCorrectBitPerPixel(string imagePath, TiffBitsPerPixel expectedBitsPerPixel)
[InlineData(Calliphora_BiColorUncompressed, 1)]
[InlineData(GrayscaleUncompressed, 8)]
[InlineData(RgbUncompressed, 24)]
public void Identify_DetectsCorrectBitPerPixel(string imagePath, int expectedBitsPerPixel)
{
var testFile = TestFile.Create(imagePath);
using var stream = new MemoryStream(testFile.Bytes, false);
@ -84,7 +81,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
Assert.NotNull(imageInfo);
TiffMetadata tiffMetadata = imageInfo.Metadata.GetTiffMetadata();
Assert.NotNull(tiffMetadata);
Assert.Equal(expectedBitsPerPixel, tiffMetadata.BitsPerPixel);
Assert.Equal(expectedBitsPerPixel, imageInfo.PixelType.BitsPerPixel);
}
[Theory]
@ -178,7 +175,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
TiffMetadata tiffMetaData = image.Metadata.GetTiffMetadata();
Assert.NotNull(tiffMetaData);
Assert.Equal(ByteOrder.LittleEndian, tiffMetaData.ByteOrder);
Assert.Equal(TiffBitsPerPixel.Bit4, tiffMetaData.BitsPerPixel);
var frameMetaData = new TiffFrameMetadata(exifProfile);
Assert.Equal(TiffBitsPerPixel.Bit4, frameMetaData.BitsPerPixel);
var tiffFrameMetadata = new TiffFrameMetadata(exifProfile);
VerifyExpectedTiffFrameMetaDataIsPresent(tiffFrameMetadata);
@ -250,7 +249,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
ExifProfile rootFrameExifProfileInput = rootFrameInput.Metadata.ExifProfile;
Assert.Equal(TiffCompression.Lzw, frameMetaInput.Compression);
Assert.Equal(TiffBitsPerPixel.Bit4, tiffMetaInput.BitsPerPixel);
Assert.Equal(TiffBitsPerPixel.Bit4, frameMetaInput.BitsPerPixel);
// Save to Tiff
var tiffEncoder = new TiffEncoder() { Mode = TiffEncodingMode.Rgb };
@ -268,7 +267,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
ExifProfile encodedImageExifProfile = rootFrameEncodedImage.Metadata.ExifProfile;
byte[] encodedImageXmpProfile = rootFrameEncodedImage.Metadata.XmpProfile;
Assert.Equal(TiffBitsPerPixel.Bit24, tiffMetaDataEncodedImage.BitsPerPixel);
Assert.Equal(TiffBitsPerPixel.Bit24, tiffMetaDataEncodedRootFrame.BitsPerPixel);
Assert.Equal(TiffCompression.None, tiffMetaDataEncodedRootFrame.Compression);
Assert.Equal(inputMetaData.HorizontalResolution, encodedImageMetaData.HorizontalResolution);

Loading…
Cancel
Save