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

Added Image<TPixel> constructor from single ImageFrame #1680

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public TiffBiColorWriter(ImageFrame<TPixel> image, MemoryAllocator memoryAllocat
: base(image, memoryAllocator, configuration, entriesCollector)
{
// Convert image to black and white.
this.imageBlackWhite = new Image<TPixel>(configuration, new ImageMetadata(), new[] { image.Clone() });
this.imageBlackWhite = new Image<TPixel>(configuration, new ImageMetadata(), image.Clone());
this.imageBlackWhite.Mutate(img => img.BinaryDither(KnownDitherings.FloydSteinberg));
}

Expand Down
16 changes: 14 additions & 2 deletions src/ImageSharp/ImageFrameCollection{TPixel}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ internal ImageFrameCollection(Image<TPixel> parent, int width, int height, Memor
this.frames.Add(new ImageFrame<TPixel>(parent.GetConfiguration(), width, height, memorySource));
}

internal ImageFrameCollection(Image<TPixel> parent, ImageFrame<TPixel> frame)
{
Guard.NotNull(parent, nameof(parent));
Guard.NotNull(frame, nameof(frame));

this.parent = parent;

// Frame is already cloned by the caller
// this.ValidateFrame call can be omitted as it's the only frame
this.frames.Add(frame);
}

internal ImageFrameCollection(Image<TPixel> parent, IEnumerable<ImageFrame<TPixel>> frames)
{
Guard.NotNull(parent, nameof(parent));
Expand Down Expand Up @@ -273,7 +285,7 @@ public override void MoveFrame(int sourceIndex, int destinationIndex)

this.frames.Remove(frame);

return new Image<TPixel>(this.parent.GetConfiguration(), this.parent.Metadata.DeepClone(), new[] { frame });
return new Image<TPixel>(this.parent.GetConfiguration(), this.parent.Metadata.DeepClone(), frame);
}

/// <summary>
Expand All @@ -288,7 +300,7 @@ public override void MoveFrame(int sourceIndex, int destinationIndex)

ImageFrame<TPixel> frame = this[index];
ImageFrame<TPixel> clonedFrame = frame.Clone();
return new Image<TPixel>(this.parent.GetConfiguration(), this.parent.Metadata.DeepClone(), new[] { clonedFrame });
return new Image<TPixel>(this.parent.GetConfiguration(), this.parent.Metadata.DeepClone(), clonedFrame);
}

/// <summary>
Expand Down
13 changes: 13 additions & 0 deletions src/ImageSharp/Image{TPixel}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ internal Image(Configuration configuration, ImageMetadata metadata, IEnumerable<
this.frames = new ImageFrameCollection<TPixel>(this, frames);
}

/// <summary>
/// Initializes a new instance of the <see cref="Image{TPixel}" /> class
/// with the height and the width of the image.
/// </summary>
/// <param name="configuration">The configuration providing initialization code which allows extending the library.</param>
/// <param name="metadata">The images metadata.</param>
/// <param name="frame">The frame that will be owned by this image instance.</param>
internal Image(Configuration configuration, ImageMetadata metadata, ImageFrame<TPixel> frame)
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata, frame.Size())
{
this.frames = new ImageFrameCollection<TPixel>(this, frame);
}

/// <inheritdoc />
protected override ImageFrameCollection NonGenericFrameCollection => this.Frames;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected override void BeforeImageApply()

// TODO: This is clunky. We should add behavior enum to ExtractFrame.
// All frames have be the same size so we only need to calculate the correct dimensions for the first frame
using (var temp = new Image<TPixel>(this.Configuration, this.Source.Metadata.DeepClone(), new[] { this.Source.Frames.RootFrame.Clone() }))
using (var temp = new Image<TPixel>(this.Configuration, this.Source.Metadata.DeepClone(), this.Source.Frames.RootFrame.Clone()))
{
Configuration configuration = this.Source.GetConfiguration();

Expand Down