Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumerate once on clone. Fix #1073 #1074

Merged
merged 1 commit into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best to call the method above to ensure no diversion is possible in future refactoring.


/// <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