Browse Source

Port Tiff, fix constructors

pull/2180/head
James Jackson-South 4 years ago
parent
commit
3917b123d1
  1. 2
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  2. 7
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs
  3. 8
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  4. 2
      src/ImageSharp/Formats/Tga/TgaDecoderOptions.cs
  5. 33
      src/ImageSharp/Formats/Tiff/TiffDecoder.cs
  6. 50
      src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs
  7. 14
      src/ImageSharp/Formats/Tiff/TiffDecoderOptions.cs

2
src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

@ -105,9 +105,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// <param name="options">The options.</param>
public BmpDecoderCore(BmpDecoderOptions options)
{
this.Options = options;
this.configuration = options.GeneralOptions.Configuration;
this.memoryAllocator = this.configuration.MemoryAllocator;
this.Options = options;
}
/// <inheritdoc />

7
src/ImageSharp/Formats/Gif/GifDecoderCore.cs

@ -67,7 +67,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
private readonly MemoryAllocator memoryAllocator;
/// <summary>
/// The maximum number of frames to decode.
/// The maximum number of frames to decode. Inclusive.
/// </summary>
private readonly int maxFrames;
@ -92,10 +92,11 @@ namespace SixLabors.ImageSharp.Formats.Gif
/// <param name="options">The decoder options.</param>
public GifDecoderCore(GifDecoderOptions options)
{
this.skipMetadata = options.GeneralOptions.SkipMetadata;
this.Options = options;
this.configuration = options.GeneralOptions.Configuration;
this.memoryAllocator = this.configuration.MemoryAllocator;
this.skipMetadata = options.GeneralOptions.SkipMetadata;
this.maxFrames = options.GeneralOptions.MaxFrames;
this.memoryAllocator = this.configuration.MemoryAllocator;
}
/// <inheritdoc />

8
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -125,17 +125,19 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <param name="options">The decoder options.</param>
public PngDecoderCore(PngDecoderOptions options)
{
this.Options = options;
this.configuration = options.GeneralOptions.Configuration;
this.memoryAllocator = this.configuration.MemoryAllocator;
this.skipMetadata = options.GeneralOptions.SkipMetadata;
this.memoryAllocator = this.configuration.MemoryAllocator;
}
internal PngDecoderCore(PngDecoderOptions options, bool colorMetadataOnly)
{
this.configuration = options.GeneralOptions.Configuration;
this.memoryAllocator = this.configuration.MemoryAllocator;
this.Options = options;
this.colorMetadataOnly = colorMetadataOnly;
this.skipMetadata = true;
this.configuration = options.GeneralOptions.Configuration;
this.memoryAllocator = this.configuration.MemoryAllocator;
}
/// <inheritdoc/>

2
src/ImageSharp/Formats/Tga/TgaDecoderOptions.cs

@ -4,7 +4,7 @@
namespace SixLabors.ImageSharp.Formats.Tga
{
/// <summary>
/// Configuration options for decoding Png images.
/// Configuration options for decoding Tga images.
/// </summary>
public class TgaDecoderOptions : ISpecializedDecoderOptions
{

33
src/ImageSharp/Formats/Tiff/TiffDecoder.cs

@ -3,7 +3,6 @@
using System.IO;
using System.Threading;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Tiff
@ -11,39 +10,29 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// <summary>
/// Image decoder for generating an image out of a TIFF stream.
/// </summary>
public class TiffDecoder : IImageDecoder, ITiffDecoderOptions, IImageInfoDetector
public class TiffDecoder : ImageDecoder<TiffDecoderOptions>
{
/// <summary>
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded.
/// </summary>
public bool IgnoreMetadata { get; set; }
/// <summary>
/// Gets or sets the decoding mode for multi-frame images.
/// </summary>
public FrameDecodingMode DecodingMode { get; set; }
/// <inheritdoc/>
public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
public override Image<TPixel> DecodeSpecialized<TPixel>(TiffDecoderOptions options, Stream stream, CancellationToken cancellationToken)
{
Guard.NotNull(stream, nameof(stream));
TiffDecoderCore decoder = new(options);
Image<TPixel> image = decoder.Decode<TiffDecoderOptions, TPixel>(options.GeneralOptions.Configuration, stream, cancellationToken);
Resize(options.GeneralOptions, image);
var decoder = new TiffDecoderCore(configuration, this);
return decoder.Decode<TPixel>(configuration, stream, cancellationToken);
return image;
}
/// <inheritdoc/>
public Image Decode(Configuration configuration, Stream stream, CancellationToken cancellationToken)
=> this.Decode<Rgba32>(configuration, stream, cancellationToken);
public override Image DecodeSpecialized(TiffDecoderOptions options, Stream stream, CancellationToken cancellationToken)
=> this.DecodeSpecialized<Rgba32>(options, stream, cancellationToken);
/// <inheritdoc/>
public IImageInfo Identify(Configuration configuration, Stream stream, CancellationToken cancellationToken)
public override IImageInfo IdentifySpecialized(TiffDecoderOptions options, Stream stream, CancellationToken cancellationToken)
{
Guard.NotNull(stream, nameof(stream));
var decoder = new TiffDecoderCore(configuration, this);
return decoder.Identify(configuration, stream, cancellationToken);
return new TiffDecoderCore(options).Identify(options.GeneralOptions.Configuration, stream, cancellationToken);
}
}
}

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

@ -20,8 +20,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// <summary>
/// Performs the tiff decoding operation.
/// </summary>
internal class TiffDecoderCore : IImageDecoderInternals
internal class TiffDecoderCore : IImageDecoderInternals<TiffDecoderOptions>
{
/// <summary>
/// General configuration options.
/// </summary>
private readonly Configuration configuration;
/// <summary>
/// Used for allocating memory during processing operations.
/// </summary>
@ -30,12 +35,12 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// <summary>
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded.
/// </summary>
private readonly bool ignoreMetadata;
private readonly bool skipMetadata;
/// <summary>
/// Gets the decoding mode for multi-frame images
/// The maximum number of frames to decode. Inclusive.
/// </summary>
private readonly FrameDecodingMode decodingMode;
private readonly int maxFrames;
/// <summary>
/// The stream to decode from.
@ -55,16 +60,14 @@ namespace SixLabors.ImageSharp.Formats.Tiff
/// <summary>
/// Initializes a new instance of the <see cref="TiffDecoderCore" /> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="options">The decoder options.</param>
public TiffDecoderCore(Configuration configuration, ITiffDecoderOptions options)
public TiffDecoderCore(TiffDecoderOptions options)
{
options ??= new TiffDecoder();
this.Configuration = configuration ?? Configuration.Default;
this.ignoreMetadata = options.IgnoreMetadata;
this.decodingMode = options.DecodingMode;
this.memoryAllocator = this.Configuration.MemoryAllocator;
this.Options = options;
this.configuration = options.GeneralOptions.Configuration;
this.skipMetadata = options.GeneralOptions.SkipMetadata;
this.maxFrames = options.GeneralOptions.MaxFrames;
this.memoryAllocator = this.configuration.MemoryAllocator;
}
/// <summary>
@ -148,7 +151,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
public TiffPredictor Predictor { get; set; }
/// <inheritdoc/>
public Configuration Configuration { get; }
public TiffDecoderOptions Options { get; }
/// <inheritdoc/>
public Size Dimensions { get; private set; }
@ -161,25 +164,26 @@ namespace SixLabors.ImageSharp.Formats.Tiff
try
{
this.inputStream = stream;
var reader = new DirectoryReader(stream, this.Configuration.MemoryAllocator);
var reader = new DirectoryReader(stream, this.configuration.MemoryAllocator);
IEnumerable<ExifProfile> directories = reader.Read();
this.byteOrder = reader.ByteOrder;
this.isBigTiff = reader.IsBigTiff;
int frameCount = 0;
foreach (ExifProfile ifd in directories)
{
cancellationToken.ThrowIfCancellationRequested();
ImageFrame<TPixel> frame = this.DecodeFrame<TPixel>(ifd, cancellationToken);
frames.Add(frame);
if (this.decodingMode is FrameDecodingMode.First)
if (frameCount++ <= this.maxFrames)
{
break;
}
}
ImageMetadata metadata = TiffDecoderMetadataCreator.Create(frames, this.ignoreMetadata, reader.ByteOrder, reader.IsBigTiff);
ImageMetadata metadata = TiffDecoderMetadataCreator.Create(frames, this.skipMetadata, reader.ByteOrder, reader.IsBigTiff);
// TODO: Tiff frames can have different sizes.
ImageFrame<TPixel> root = frames[0];
@ -192,7 +196,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
}
}
return new Image<TPixel>(this.Configuration, metadata, frames);
return new Image<TPixel>(this.configuration, metadata, frames);
}
catch
{
@ -209,7 +213,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
public IImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
{
this.inputStream = stream;
var reader = new DirectoryReader(stream, this.Configuration.MemoryAllocator);
var reader = new DirectoryReader(stream, this.configuration.MemoryAllocator);
IEnumerable<ExifProfile> directories = reader.Read();
ExifProfile rootFrameExifProfile = directories.First();
@ -233,7 +237,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
where TPixel : unmanaged, IPixel<TPixel>
{
var imageFrameMetaData = new ImageFrameMetadata();
if (!this.ignoreMetadata)
if (!this.skipMetadata)
{
imageFrameMetaData.ExifProfile = tags;
}
@ -245,7 +249,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
int width = GetImageWidth(tags);
int height = GetImageHeight(tags);
var frame = new ImageFrame<TPixel>(this.Configuration, width, height, imageFrameMetaData);
var frame = new ImageFrame<TPixel>(this.configuration, width, height, imageFrameMetaData);
int rowsPerStrip = tags.GetValue(ExifTag.RowsPerStrip) != null ? (int)tags.GetValue(ExifTag.RowsPerStrip).Value : TiffConstants.RowsPerStripInfinity;
@ -369,7 +373,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
}
using TiffBaseDecompressor decompressor = TiffDecompressorsFactory.Create(
this.Configuration,
this.configuration,
this.CompressionType,
this.memoryAllocator,
this.PhotometricInterpretation,
@ -449,7 +453,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
Buffer2D<TPixel> pixels = frame.PixelBuffer;
using TiffBaseDecompressor decompressor = TiffDecompressorsFactory.Create(
this.Configuration,
this.configuration,
this.CompressionType,
this.memoryAllocator,
this.PhotometricInterpretation,
@ -463,7 +467,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff
this.byteOrder);
TiffBaseColorDecoder<TPixel> colorDecoder = TiffColorDecoderFactory<TPixel>.Create(
this.Configuration,
this.configuration,
this.memoryAllocator,
this.ColorType,
this.BitsPerSample,

14
src/ImageSharp/Formats/Tiff/TiffDecoderOptions.cs

@ -0,0 +1,14 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
namespace SixLabors.ImageSharp.Formats.Tiff
{
/// <summary>
/// Configuration options for decoding Tiff images.
/// </summary>
public class TiffDecoderOptions : ISpecializedDecoderOptions
{
/// <inheritdoc/>
public DecoderOptions GeneralOptions { get; set; } = new();
}
}
Loading…
Cancel
Save