From db80a7ff61056aa49e6842b6bb39ac8b82624ea3 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Tue, 12 Sep 2017 08:08:40 +0100 Subject: [PATCH] introduce IImageFrameCollectino to hide disposable --- src/ImageSharp/Image/IImageFrameCollection.cs | 84 +++++++++++++++++++ src/ImageSharp/Image/ImageFrameCollection.cs | 2 +- src/ImageSharp/Image/Image{TPixel}.cs | 23 ++--- 3 files changed, 97 insertions(+), 12 deletions(-) create mode 100644 src/ImageSharp/Image/IImageFrameCollection.cs diff --git a/src/ImageSharp/Image/IImageFrameCollection.cs b/src/ImageSharp/Image/IImageFrameCollection.cs new file mode 100644 index 0000000000..ee325bc632 --- /dev/null +++ b/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 +{ + /// + /// Encapsulates an imaged collection of frames. + /// + /// The type of the pixel. + public interface IImageFrameCollection : IEnumerable> + where TPixel : struct, IPixel + { + /// + /// Gets the count. + /// + int Count { get; } + + /// + /// Gets the root frame. + /// + ImageFrame RootFrame { get; } + + /// + /// Gets or sets the at the specified index. + /// + /// + /// The . + /// + /// The index. + /// The at the specified index. + ImageFrame this[int index] { get; set; } + + /// + /// Determines the index of a specific in the . + /// + /// The to locate in the . + /// The index of item if found in the list; otherwise, -1. + int IndexOf(ImageFrame frame); + + /// + /// Inserts the to the at the specified . + /// + /// The zero-based index at which item should be inserted.. + /// The to insert into the . + void Insert(int index, ImageFrame frame); + + /// + /// Removes the from the at the specified index. + /// + /// The zero-based index of the item to remove. + /// Cannot remove last frame. + void RemoveAt(int index); + + /// + /// Adds the specified frame. + /// + /// The frame. + /// Frame must have the same dimensions as the image - frame + void Add(ImageFrame frame); + + /// + /// Determines whether the contains the . + /// + /// The frame. + /// + /// true if the the specified frame; otherwise, false. + /// + bool Contains(ImageFrame frame); + + /// + /// Removes the specified frame. + /// + /// The frame. + /// true if item is found in the ; otherwise, + /// Cannot remove last frame + bool Remove(ImageFrame frame); + } +} \ No newline at end of file diff --git a/src/ImageSharp/Image/ImageFrameCollection.cs b/src/ImageSharp/Image/ImageFrameCollection.cs index b8b46c88b2..25c0d0c449 100644 --- a/src/ImageSharp/Image/ImageFrameCollection.cs +++ b/src/ImageSharp/Image/ImageFrameCollection.cs @@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp /// Encapsulates an imaged collection of frames. /// /// The type of the pixel. - public sealed class ImageFrameCollection : IEnumerable>, IDisposable + internal sealed class ImageFrameCollection : IImageFrameCollection, IDisposable where TPixel : struct, IPixel { private readonly IList> frames = new List>(); diff --git a/src/ImageSharp/Image/Image{TPixel}.cs b/src/ImageSharp/Image/Image{TPixel}.cs index 8da19469e5..5c35d854a3 100644 --- a/src/ImageSharp/Image/Image{TPixel}.cs +++ b/src/ImageSharp/Image/Image{TPixel}.cs @@ -21,6 +21,7 @@ namespace SixLabors.ImageSharp where TPixel : struct, IPixel { private Configuration configuration; + private ImageFrameCollection frames; /// /// Initializes a new instance of the class @@ -61,7 +62,7 @@ namespace SixLabors.ImageSharp { this.configuration = configuration ?? Configuration.Default; this.MetaData = metadata ?? new ImageMetaData(); - this.Frames = new ImageFrameCollection(width, height); + this.frames = new ImageFrameCollection(width, height); } /// @@ -76,7 +77,7 @@ namespace SixLabors.ImageSharp this.configuration = configuration ?? Configuration.Default; this.MetaData = metadata ?? new ImageMetaData(); - this.Frames = new ImageFrameCollection(frames); + this.frames = new ImageFrameCollection(frames); } /// @@ -87,12 +88,12 @@ namespace SixLabors.ImageSharp /// /// Gets the width. /// - public int Width => this.Frames.RootFrame.Width; + public int Width => this.frames.RootFrame.Width; /// /// Gets the height. /// - public int Height => this.Frames.RootFrame.Height; + public int Height => this.frames.RootFrame.Height; /// /// Gets the meta data of the image. @@ -102,12 +103,12 @@ namespace SixLabors.ImageSharp /// /// Gets the frames. /// - public ImageFrameCollection Frames { get; private set; } + public IImageFrameCollection Frames => this.frames; /// /// Gets the root frame. /// - private IPixelSource PixelSource => this.Frames?.RootFrame; + private IPixelSource PixelSource => this.frames?.RootFrame ?? throw new ObjectDisposedException(nameof(Image)); /// /// Gets or sets the pixel at the specified position. @@ -142,7 +143,7 @@ namespace SixLabors.ImageSharp /// Returns a new image with all the same metadata as the original. public Image Clone() { - IEnumerable> frames = this.Frames.Select(x => x.Clone()).ToArray(); + IEnumerable> frames = this.frames.Select(x => x.Clone()).ToArray(); return new Image(this.configuration, this.MetaData.Clone(), frames); } @@ -161,7 +162,7 @@ namespace SixLabors.ImageSharp public Image CloneAs() where TPixel2 : struct, IPixel { - IEnumerable> frames = this.Frames.Select(x => x.CloneAs()).ToArray(); + IEnumerable> frames = this.frames.Select(x => x.CloneAs()).ToArray(); var target = new Image(this.configuration, this.MetaData, frames); return target; @@ -172,7 +173,7 @@ namespace SixLabors.ImageSharp /// public void Dispose() { - this.Frames.Dispose(); + this.frames.Dispose(); } /// @@ -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]); } } }