Browse Source

introduce IImageFrameCollectino to hide disposable

pull/326/head
Scott Williams 9 years ago
parent
commit
db80a7ff61
  1. 84
      src/ImageSharp/Image/IImageFrameCollection.cs
  2. 2
      src/ImageSharp/Image/ImageFrameCollection.cs
  3. 23
      src/ImageSharp/Image/Image{TPixel}.cs

84
src/ImageSharp/Image/IImageFrameCollection.cs

@ -0,0 +1,84 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections;
using System.Collections.Generic;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp
{
/// <summary>
/// Encapsulates an imaged collection of frames.
/// </summary>
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
public interface IImageFrameCollection<TPixel> : IEnumerable<ImageFrame<TPixel>>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
/// Gets the count.
/// </summary>
int Count { get; }
/// <summary>
/// Gets the root frame.
/// </summary>
ImageFrame<TPixel> RootFrame { get; }
/// <summary>
/// Gets or sets the <see cref="ImageFrame{TPixel}"/> at the specified index.
/// </summary>
/// <value>
/// The <see cref="ImageFrame{TPixel}"/>.
/// </value>
/// <param name="index">The index.</param>
/// <returns>The <see cref="ImageFrame{TPixel}"/> at the specified index.</returns>
ImageFrame<TPixel> this[int index] { get; set; }
/// <summary>
/// Determines the index of a specific <paramref name="frame"/> in the <seealso cref="Image{TPixel}"/>.
/// </summary>
/// <param name="frame">The <seealso cref="ImageFrame{TPixel}"/> to locate in the <seealso cref="Image{TPixel}"/>.</param>
/// <returns>The index of item if found in the list; otherwise, -1.</returns>
int IndexOf(ImageFrame<TPixel> frame);
/// <summary>
/// Inserts the <paramref name="frame"/> to the <seealso cref="Image{TPixel}"/> at the specified <paramref name="index"/>.
/// </summary>
/// <param name="index"> The zero-based index at which item should be inserted..</param>
/// <param name="frame">The <seealso cref="ImageFrame{TPixel}"/> to insert into the <seealso cref="Image{TPixel}"/>.</param>
void Insert(int index, ImageFrame<TPixel> frame);
/// <summary>
/// Removes the <seealso cref="ImageFrame{TPixel}"/> from the <seealso cref="Image{TPixel}"/> at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the item to remove.</param>
/// <exception cref="InvalidOperationException">Cannot remove last frame.</exception>
void RemoveAt(int index);
/// <summary>
/// Adds the specified frame.
/// </summary>
/// <param name="frame">The frame.</param>
/// <exception cref="ArgumentException">Frame must have the same dimensions as the image - frame</exception>
void Add(ImageFrame<TPixel> frame);
/// <summary>
/// Determines whether the <seealso cref="Image{TPixel}"/> contains the <paramref name="frame"/>.
/// </summary>
/// <param name="frame">The frame.</param>
/// <returns>
/// <c>true</c> if the <seealso cref="Image{TPixel}"/> the specified frame; otherwise, <c>false</c>.
/// </returns>
bool Contains(ImageFrame<TPixel> frame);
/// <summary>
/// Removes the specified frame.
/// </summary>
/// <param name="frame">The frame.</param>
/// <returns>true if item is found in the <seealso cref="Image{TPixel}"/>; otherwise,</returns>
/// <exception cref="InvalidOperationException">Cannot remove last frame</exception>
bool Remove(ImageFrame<TPixel> frame);
}
}

2
src/ImageSharp/Image/ImageFrameCollection.cs

@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp
/// Encapsulates an imaged collection of frames.
/// </summary>
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
public sealed class ImageFrameCollection<TPixel> : IEnumerable<ImageFrame<TPixel>>, IDisposable
internal sealed class ImageFrameCollection<TPixel> : IImageFrameCollection<TPixel>, IDisposable
where TPixel : struct, IPixel<TPixel>
{
private readonly IList<ImageFrame<TPixel>> frames = new List<ImageFrame<TPixel>>();

23
src/ImageSharp/Image/Image{TPixel}.cs

@ -21,6 +21,7 @@ namespace SixLabors.ImageSharp
where TPixel : struct, IPixel<TPixel>
{
private Configuration configuration;
private ImageFrameCollection<TPixel> frames;
/// <summary>
/// Initializes a new instance of the <see cref="Image{TPixel}"/> class
@ -61,7 +62,7 @@ namespace SixLabors.ImageSharp
{
this.configuration = configuration ?? Configuration.Default;
this.MetaData = metadata ?? new ImageMetaData();
this.Frames = new ImageFrameCollection<TPixel>(width, height);
this.frames = new ImageFrameCollection<TPixel>(width, height);
}
/// <summary>
@ -76,7 +77,7 @@ namespace SixLabors.ImageSharp
this.configuration = configuration ?? Configuration.Default;
this.MetaData = metadata ?? new ImageMetaData();
this.Frames = new ImageFrameCollection<TPixel>(frames);
this.frames = new ImageFrameCollection<TPixel>(frames);
}
/// <summary>
@ -87,12 +88,12 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Gets the width.
/// </summary>
public int Width => this.Frames.RootFrame.Width;
public int Width => this.frames.RootFrame.Width;
/// <summary>
/// Gets the height.
/// </summary>
public int Height => this.Frames.RootFrame.Height;
public int Height => this.frames.RootFrame.Height;
/// <summary>
/// Gets the meta data of the image.
@ -102,12 +103,12 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Gets the frames.
/// </summary>
public ImageFrameCollection<TPixel> Frames { get; private set; }
public IImageFrameCollection<TPixel> Frames => this.frames;
/// <summary>
/// Gets the root frame.
/// </summary>
private IPixelSource<TPixel> PixelSource => this.Frames?.RootFrame;
private IPixelSource<TPixel> PixelSource => this.frames?.RootFrame ?? throw new ObjectDisposedException(nameof(Image<TPixel>));
/// <summary>
/// Gets or sets the pixel at the specified position.
@ -142,7 +143,7 @@ namespace SixLabors.ImageSharp
/// <returns>Returns a new image with all the same metadata as the original.</returns>
public Image<TPixel> Clone()
{
IEnumerable<ImageFrame<TPixel>> frames = this.Frames.Select(x => x.Clone()).ToArray();
IEnumerable<ImageFrame<TPixel>> frames = this.frames.Select(x => x.Clone()).ToArray();
return new Image<TPixel>(this.configuration, this.MetaData.Clone(), frames);
}
@ -161,7 +162,7 @@ namespace SixLabors.ImageSharp
public Image<TPixel2> CloneAs<TPixel2>()
where TPixel2 : struct, IPixel<TPixel2>
{
IEnumerable<ImageFrame<TPixel2>> frames = this.Frames.Select(x => x.CloneAs<TPixel2>()).ToArray();
IEnumerable<ImageFrame<TPixel2>> frames = this.frames.Select(x => x.CloneAs<TPixel2>()).ToArray();
var target = new Image<TPixel2>(this.configuration, this.MetaData, frames);
return target;
@ -172,7 +173,7 @@ namespace SixLabors.ImageSharp
/// </summary>
public void Dispose()
{
this.Frames.Dispose();
this.frames.Dispose();
}
/// <summary>
@ -183,9 +184,9 @@ namespace SixLabors.ImageSharp
{
Guard.NotNull(pixelSource, nameof(pixelSource));
for (int i = 0; i < this.Frames.Count; i++)
for (int i = 0; i < this.frames.Count; i++)
{
this.Frames[i].SwapPixelsBuffers(pixelSource.Frames[i]);
this.frames[i].SwapPixelsBuffers(pixelSource.frames[i]);
}
}
}

Loading…
Cancel
Save