From a6082f95d1abb965868bcd15773da743a32d4ef3 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 9 Jan 2020 11:26:28 +1100 Subject: [PATCH] Enumerate once on clone. Fix #1073 --- src/ImageSharp/ImageFrameCollection.cs | 4 ++-- src/ImageSharp/Image{TPixel}.cs | 18 +++++++++++++----- .../CloningImageProcessor{TPixel}.cs | 15 +++++++++------ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/ImageFrameCollection.cs b/src/ImageSharp/ImageFrameCollection.cs index c5bd02c79..c584d2d19 100644 --- a/src/ImageSharp/ImageFrameCollection.cs +++ b/src/ImageSharp/ImageFrameCollection.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; @@ -122,7 +122,7 @@ namespace SixLabors.ImageSharp public IEnumerator GetEnumerator() => this.NonGenericGetEnumerator(); /// - IEnumerator IEnumerable.GetEnumerator() => this.NonGenericGetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); /// /// Implements . diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index 3f733479d..29184522f 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/src/ImageSharp/Image{TPixel}.cs @@ -130,7 +130,7 @@ namespace SixLabors.ImageSharp protected override ImageFrameCollection NonGenericFrameCollection => this.Frames; /// - /// Gets the frames. + /// Gets the collection of image frames. /// public new ImageFrameCollection Frames { get; } @@ -166,8 +166,12 @@ namespace SixLabors.ImageSharp { this.EnsureNotDisposed(); - IEnumerable> clonedFrames = - this.Frames.Select, ImageFrame>(x => x.Clone(configuration)); + var clonedFrames = new ImageFrame[this.Frames.Count]; + for (int i = 0; i < clonedFrames.Length; i++) + { + clonedFrames[i] = this.Frames[i].Clone(configuration); + } + return new Image(configuration, this.Metadata.DeepClone(), clonedFrames); } @@ -181,8 +185,12 @@ namespace SixLabors.ImageSharp { this.EnsureNotDisposed(); - IEnumerable> clonedFrames = - this.Frames.Select, ImageFrame>(x => x.CloneAs(configuration)); + var clonedFrames = new ImageFrame[this.Frames.Count]; + for (int i = 0; i < clonedFrames.Length; i++) + { + clonedFrames[i] = this.Frames[i].CloneAs(configuration); + } + return new Image(configuration, this.Metadata.DeepClone(), clonedFrames); } diff --git a/src/ImageSharp/Processing/Processors/CloningImageProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/CloningImageProcessor{TPixel}.cs index 8aef51c03..780bec22c 100644 --- a/src/ImageSharp/Processing/Processors/CloningImageProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/CloningImageProcessor{TPixel}.cs @@ -173,16 +173,19 @@ namespace SixLabors.ImageSharp.Processing.Processors Image source = this.Source; Size targetSize = this.GetTargetSize(); - // We will always be creating the clone even for mutate because we may need to resize the canvas - IEnumerable> frames = source.Frames.Select, ImageFrame>( - x => new ImageFrame( + // We will always be creating the clone even for mutate because we may need to resize the canvas. + var targetFrames = new ImageFrame[source.Frames.Count]; + for (int i = 0; i < targetFrames.Length; i++) + { + targetFrames[i] = new ImageFrame( this.Configuration, targetSize.Width, targetSize.Height, - x.Metadata.DeepClone())); + source.Frames[i].Metadata.DeepClone()); + } - // Use the overload to prevent an extra frame being added - return new Image(this.Configuration, source.Metadata.DeepClone(), frames); + // Use the overload to prevent an extra frame being added. + return new Image(this.Configuration, source.Metadata.DeepClone(), targetFrames); } private void CheckFrameCount(Image a, Image b)