Skip to content

Commit

Permalink
Merge pull request #1074 from SixLabors/js/fix-1073
Browse files Browse the repository at this point in the history
Enumerate once on clone. Fix #1073
  • Loading branch information
JimBobSquarePants authored Jan 9, 2020
2 parents dadc274 + 5b7052c commit 06bf7ac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/ImageSharp/ImageFrameCollection.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -122,7 +122,7 @@ public abstract class ImageFrameCollection : IEnumerable<ImageFrame>
public IEnumerator<ImageFrame> GetEnumerator() => this.NonGenericGetEnumerator();

/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator() => this.NonGenericGetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();

/// <summary>
/// Implements <see cref="GetEnumerator"/>.
Expand Down
18 changes: 13 additions & 5 deletions src/ImageSharp/Image{TPixel}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal Image(Configuration configuration, ImageMetadata metadata, IEnumerable<
protected override ImageFrameCollection NonGenericFrameCollection => this.Frames;

/// <summary>
/// Gets the frames.
/// Gets the collection of image frames.
/// </summary>
public new ImageFrameCollection<TPixel> Frames { get; }

Expand Down Expand Up @@ -166,8 +166,12 @@ public Image<TPixel> Clone(Configuration configuration)
{
this.EnsureNotDisposed();

IEnumerable<ImageFrame<TPixel>> clonedFrames =
this.Frames.Select<ImageFrame<TPixel>, ImageFrame<TPixel>>(x => x.Clone(configuration));
var clonedFrames = new ImageFrame<TPixel>[this.Frames.Count];
for (int i = 0; i < clonedFrames.Length; i++)
{
clonedFrames[i] = this.Frames[i].Clone(configuration);
}

return new Image<TPixel>(configuration, this.Metadata.DeepClone(), clonedFrames);
}

Expand All @@ -181,8 +185,12 @@ public override Image<TPixel2> CloneAs<TPixel2>(Configuration configuration)
{
this.EnsureNotDisposed();

IEnumerable<ImageFrame<TPixel2>> clonedFrames =
this.Frames.Select<ImageFrame<TPixel>, ImageFrame<TPixel2>>(x => x.CloneAs<TPixel2>(configuration));
var clonedFrames = new ImageFrame<TPixel2>[this.Frames.Count];
for (int i = 0; i < clonedFrames.Length; i++)
{
clonedFrames[i] = this.Frames[i].CloneAs<TPixel2>(configuration);
}

return new Image<TPixel2>(configuration, this.Metadata.DeepClone(), clonedFrames);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,19 @@ private Image<TPixel> CreateTarget()
Image<TPixel> 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<ImageFrame<TPixel>> frames = source.Frames.Select<ImageFrame<TPixel>, ImageFrame<TPixel>>(
x => new ImageFrame<TPixel>(
// We will always be creating the clone even for mutate because we may need to resize the canvas.
var targetFrames = new ImageFrame<TPixel>[source.Frames.Count];
for (int i = 0; i < targetFrames.Length; i++)
{
targetFrames[i] = new ImageFrame<TPixel>(
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<TPixel>(this.Configuration, source.Metadata.DeepClone(), frames);
// Use the overload to prevent an extra frame being added.
return new Image<TPixel>(this.Configuration, source.Metadata.DeepClone(), targetFrames);
}

private void CheckFrameCount(Image<TPixel> a, Image<TPixel> b)
Expand Down

0 comments on commit 06bf7ac

Please sign in to comment.