Browse Source

Clean up formats.

Former-commit-id: 531da397b90ec92c74fff08bf070c9647c4498d3
Former-commit-id: 8fe224b456b8fd940f5eae1944df9e641a0c2a25
Former-commit-id: 7b75d81db72c566c483964701a4da6274b63e9b6
af/merge-core
James Jackson-South 10 years ago
parent
commit
10c1a00c98
  1. 52
      src/ImageProcessorCore/Image.cs
  2. 6
      src/ImageProcessorCore/ImageExtensions.cs

52
src/ImageProcessorCore/Image.cs

@ -7,7 +7,6 @@ namespace ImageProcessorCore
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
@ -77,27 +76,9 @@ namespace ImageProcessorCore
this.RepeatCount = other.RepeatCount;
this.HorizontalResolution = other.HorizontalResolution;
this.VerticalResolution = other.VerticalResolution;
this.Formats = other.Formats;
this.CurrentImageFormat = other.CurrentImageFormat;
}
/// <summary>
/// Initializes a new instance of the <see cref="Image"/> class.
/// </summary>
/// <param name="other">
/// The other <see cref="ImageFrame"/> to create this instance from.
/// </param>
/// <exception cref="ArgumentNullException">
/// Thrown if the given <see cref="ImageFrame"/> is null.
/// </exception>
public Image(ImageFrame other)
: base(other)
{
// Most likely a gif
// TODO: Should this be aproperty on ImageFrame?
this.CurrentImageFormat = Bootstrapper.Instance.ImageFormats.First(f => f.GetType() == typeof(GifFormat));
}
/// <summary>
/// Initializes a new instance of the <see cref="Image"/> class.
/// </summary>
@ -108,29 +89,13 @@ namespace ImageProcessorCore
public Image(Stream stream)
{
Guard.NotNull(stream, nameof(stream));
this.Load(stream, Bootstrapper.Instance.ImageFormats.ToList());
}
/// <summary>
/// Initializes a new instance of the <see cref="Image"/> class.
/// </summary>
/// <param name="stream">
/// The stream containing image information.
/// </param>
/// <param name="formats">
/// The collection of <see cref="IImageFormat"/>.
/// </param>
/// <exception cref="ArgumentNullException">Thrown if the stream is null.</exception>
public Image(Stream stream, params IImageFormat[] formats)
{
Guard.NotNull(stream, nameof(stream));
this.Load(stream, formats.ToList());
this.Load(stream);
}
/// <summary>
/// Gets a list of supported image formats.
/// </summary>
public IReadOnlyCollection<IImageFormat> Formats { get; internal set; } = Bootstrapper.Instance.ImageFormats;
public IReadOnlyCollection<IImageFormat> Formats { get; } = Bootstrapper.Instance.ImageFormats;
/// <inheritdoc/>
public double HorizontalResolution { get; set; } = DefaultHorizontalResolution;
@ -246,15 +211,12 @@ namespace ImageProcessorCore
/// Loads the image from the given stream.
/// </summary>
/// <param name="stream">The stream containing image information.</param>
/// <param name="formats">The collection of <see cref="IImageFormat"/>.</param>
/// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable.
/// </exception>
private void Load(Stream stream, IList<IImageFormat> formats)
private void Load(Stream stream)
{
if (!formats.Any()) { return; }
this.Formats = new ReadOnlyCollection<IImageFormat>(formats);
if (!this.Formats.Any()) { return; }
if (!stream.CanRead)
{
@ -266,7 +228,7 @@ namespace ImageProcessorCore
throw new NotSupportedException("The stream does not support seeking.");
}
int maxHeaderSize = formats.Max(x => x.Decoder.HeaderSize);
int maxHeaderSize = this.Formats.Max(x => x.Decoder.HeaderSize);
if (maxHeaderSize > 0)
{
byte[] header = new byte[maxHeaderSize];
@ -275,7 +237,7 @@ namespace ImageProcessorCore
stream.Read(header, 0, maxHeaderSize);
stream.Position = 0;
IImageFormat format = formats.FirstOrDefault(x => x.Decoder.IsSupportedFileFormat(header));
IImageFormat format = this.Formats.FirstOrDefault(x => x.Decoder.IsSupportedFileFormat(header));
if (format != null)
{
format.Decoder.Decode(this, stream);
@ -287,7 +249,7 @@ namespace ImageProcessorCore
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Image cannot be loaded. Available formats:");
foreach (IImageFormat format in formats)
foreach (IImageFormat format in this.Formats)
{
stringBuilder.AppendLine("-" + format);
}

6
src/ImageProcessorCore/ImageExtensions.cs

@ -127,6 +127,7 @@ namespace ImageProcessorCore
/// <param name="clone">Whether to clone the image.</param>
/// <param name="action">The <see cref="Action"/> to perform against the image.</param>
/// <returns>The <see cref="Image"/>.</returns>
/// <exception cref="ObjectDisposedException">Thrown if the <paramref name="source"/> has been disposed.</exception>
private static Image PerformAction(Image source, bool clone, Action<ImageBase, ImageBase> action)
{
if (source.IsDisposed)
@ -142,7 +143,6 @@ namespace ImageProcessorCore
// TODO: Check why we need to set these?
HorizontalResolution = source.HorizontalResolution,
VerticalResolution = source.VerticalResolution,
Formats = source.Formats,
CurrentImageFormat = source.CurrentImageFormat,
RepeatCount = source.RepeatCount
};
@ -165,9 +165,9 @@ namespace ImageProcessorCore
}
}
// According to http://stackoverflow.com/questions/37921815/idisposable-unmanaged-fields-reference-types-and-assignments/37922955#37922955
// According to http://stackoverflow.com/questions/37921815/idisposable-unmanaged-fields-reference-types-and-assignments
// There's no need to dispose of the original image as the GC will get around to cleaning it up now there are no references to the original data.
// TODO: Investigate how log this is held onto and try to keep that to a minimum.
// TODO: Investigate how long this is held onto and try to keep that to a minimum.
source = transformedImage;
return source;
}

Loading…
Cancel
Save