@ -11,7 +11,6 @@ using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Memory ;
using SixLabors.ImageSharp.MetaData ;
using SixLabors.ImageSharp.PixelFormats ;
using SixLabors.Memory ;
namespace SixLabors.ImageSharp
{
@ -23,15 +22,12 @@ namespace SixLabors.ImageSharp
where TPixel : struct , IPixel < TPixel >
{
private readonly Configuration configuration ;
private readonly ImageFrameCollection < TPixel > 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="configuration">The configuration providing initialization code which allows extending the library.</param>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
public Image ( Configuration configuration , int width , int height )
@ -43,9 +39,7 @@ namespace SixLabors.ImageSharp
/// 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="configuration">The configuration providing initialization code which allows extending the library.</param>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
/// <param name="backgroundColor">The color to initialize the pixels with.</param>
@ -69,9 +63,7 @@ namespace SixLabors.ImageSharp
/// 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="configuration">The configuration providing initialization code which allows extending the library.</param>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
/// <param name="metadata">The images metadata.</param>
@ -80,37 +72,41 @@ namespace SixLabors.ImageSharp
this . configuration = configuration ? ? Configuration . Default ;
this . PixelType = new PixelTypeInfo ( Unsafe . SizeOf < TPixel > ( ) * 8 ) ;
this . MetaData = metadata ? ? new ImageMetaData ( ) ;
this . f rames = new ImageFrameCollection < TPixel > ( this , width , height , default ( TPixel ) ) ;
this . F rames = new ImageFrameCollection < TPixel > ( this , width , height , default ( TPixel ) ) ;
}
/// <summary>
/// Initializes a new instance of the <see cref="Image{TPixel}"/> class
/// wrapping an external <see cref="MemorySource{T}"/>
/// </summary>
/// <param name="configuration">The configuration providing initialization code which allows extending the library.</param>
/// <param name="memorySource">The memory source.</param>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
/// <param name="metadata">The images metadata.</param>
internal Image ( Configuration configuration , MemorySource < TPixel > memorySource , int width , int height , ImageMetaData metadata )
{
this . configuration = configuration ;
this . PixelType = new PixelTypeInfo ( Unsafe . SizeOf < TPixel > ( ) * 8 ) ;
this . MetaData = metadata ;
this . f rames = new ImageFrameCollection < TPixel > ( this , width , height , memorySource ) ;
this . F rames = new ImageFrameCollection < TPixel > ( this , width , height , memorySource ) ;
}
/// <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="configuration">The configuration providing initialization code which allows extending the library.</param>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
/// <param name="backgroundColor">The color to initialize the pixels with.</param>
/// <param name="metadata">The images metadata.</param>
internal Image ( Configuration configuration , int width , int height , TPixel backgroundColor , ImageMetaData metadata ) {
internal Image ( Configuration configuration , int width , int height , TPixel backgroundColor , ImageMetaData metadata )
{
this . configuration = configuration ? ? Configuration . Default ;
this . PixelType = new PixelTypeInfo ( Unsafe . SizeOf < TPixel > ( ) * 8 ) ;
this . MetaData = metadata ? ? new ImageMetaData ( ) ;
this . f rames = new ImageFrameCollection < TPixel > ( this , width , height , backgroundColor ) ;
this . F rames = new ImageFrameCollection < TPixel > ( this , width , height , backgroundColor ) ;
}
/// <summary>
@ -126,7 +122,7 @@ namespace SixLabors.ImageSharp
this . PixelType = new PixelTypeInfo ( Unsafe . SizeOf < TPixel > ( ) * 8 ) ;
this . MetaData = metadata ? ? new ImageMetaData ( ) ;
this . f rames = new ImageFrameCollection < TPixel > ( this , frames ) ;
this . F rames = new ImageFrameCollection < TPixel > ( this , frames ) ;
}
/// <summary>
@ -138,10 +134,10 @@ namespace SixLabors.ImageSharp
public PixelTypeInfo PixelType { get ; }
/// <inheritdoc/>
public int Width = > this . f rames. RootFrame . Width ;
public int Width = > this . F rames. RootFrame . Width ;
/// <inheritdoc/>
public int Height = > this . f rames. RootFrame . Height ;
public int Height = > this . F rames. RootFrame . Height ;
/// <inheritdoc/>
public ImageMetaData MetaData { get ; }
@ -149,12 +145,12 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Gets the frames.
/// </summary>
public ImageFrameCollection < TPixel > Frames = > this . frames ;
public ImageFrameCollection < TPixel > Frames { get ; }
/// <summary>
/// Gets the root frame.
/// </summary>
private IPixelSource < TPixel > PixelSource = > this . f rames? . RootFrame ? ? throw new ObjectDisposedException ( nameof ( Image < TPixel > ) ) ;
private IPixelSource < TPixel > PixelSource = > this . F rames? . RootFrame ? ? throw new ObjectDisposedException ( nameof ( Image < TPixel > ) ) ;
/// <summary>
/// Gets or sets the pixel at the specified position.
@ -187,16 +183,17 @@ namespace SixLabors.ImageSharp
/// Clones the current image
/// </summary>
/// <returns>Returns a new image with all the same metadata as the original.</returns>
public Image < TPixel > Clone ( )
{
IEnumerable < ImageFrame < TPixel > > clonedFrames = this . frames . Select ( x = > x . Clone ( ) ) ;
return new Image < TPixel > ( this . configuration , this . MetaData . Clone ( ) , clonedFrames ) ;
}
public Image < TPixel > Clone ( ) = > this . Clone ( this . configuration ) ;
/// <inheritdoc/>
public override string ToString ( )
/// <summary>
/// Clones the current image with the given configueation.
/// </summary>
/// <param name="configuration">The configuration providing initialization code which allows extending the library.</param>
/// <returns>Returns a new <see cref="Image{TPixel}"/> with all the same pixel data as the original.</returns>
public Image < TPixel > Clone ( Configuration configuration )
{
return $"Image<{typeof(TPixel).Name}>: {this.Width}x{this.Height}" ;
IEnumerable < ImageFrame < TPixel > > clonedFrames = this . Frames . Select ( x = > x . Clone ( configuration ) ) ;
return new Image < TPixel > ( configuration , this . MetaData . Clone ( ) , clonedFrames ) ;
}
/// <summary>
@ -205,22 +202,27 @@ namespace SixLabors.ImageSharp
/// <typeparam name="TPixel2">The pixel format.</typeparam>
/// <returns>The <see cref="Image{TPixel2}"/></returns>
public Image < TPixel2 > CloneAs < TPixel2 > ( )
where TPixel2 : struct , IPixel < TPixel2 >
{
IEnumerable < ImageFrame < TPixel2 > > clonedFrames = this . frames . Select ( x = > x . CloneAs < TPixel2 > ( ) ) ;
var target = new Image < TPixel2 > ( this . configuration , this . MetaData . Clone ( ) , clonedFrames ) ;
return target ;
}
where TPixel2 : struct , IPixel < TPixel2 > = > this . CloneAs < TPixel2 > ( this . configuration ) ;
/// <summary>
/// Releases managed resources .
/// Returns a copy of the image in the given pixel format.
/// </summary>
public void Dispose ( )
/// <typeparam name="TPixel2">The pixel format.</typeparam>
/// <param name="configuration">The configuration providing initialization code which allows extending the library.</param>
/// <returns>The <see cref="Image{TPixel2}"/></returns>
public Image < TPixel2 > CloneAs < TPixel2 > ( Configuration configuration )
where TPixel2 : struct , IPixel < TPixel2 >
{
this . frames . Dispose ( ) ;
IEnumerable < ImageFrame < TPixel2 > > clonedFrames = this . Frames . Select ( x = > x . CloneAs < TPixel2 > ( configuration ) ) ;
return new Image < TPixel2 > ( configuration , this . MetaData . Clone ( ) , clonedFrames ) ;
}
/// <inheritdoc/>
public void Dispose ( ) = > this . Frames . Dispose ( ) ;
/// <inheritdoc/>
public override string ToString ( ) = > $"Image<{typeof(TPixel).Name}>: {this.Width}x{this.Height}" ;
/// <summary>
/// Switches the buffers used by the image and the pixelSource meaning that the Image will "own" the buffer from the pixelSource and the pixelSource will now own the Images buffer.
/// </summary>
@ -229,9 +231,9 @@ namespace SixLabors.ImageSharp
{
Guard . NotNull ( pixelSource , nameof ( pixelSource ) ) ;
for ( int i = 0 ; i < this . f rames. Count ; i + + )
for ( int i = 0 ; i < this . F rames. Count ; i + + )
{
this . f rames[ i ] . SwapOrCopyPixelsBufferFrom ( pixelSource . f rames[ i ] ) ;
this . F rames[ i ] . SwapOrCopyPixelsBufferFrom ( pixelSource . F rames[ i ] ) ;
}
}
}