Browse Source

cache Image dimensions into a field + re-enable all target frameworks

pull/904/head
Anton Firszov 7 years ago
parent
commit
cf3d4b50f5
  1. 4
      src/ImageSharp.Drawing/ImageSharp.Drawing.csproj
  2. 39
      src/ImageSharp/Image.cs
  3. 3
      src/ImageSharp/ImageSharp.csproj
  4. 45
      src/ImageSharp/Image{TPixel}.cs

4
src/ImageSharp.Drawing/ImageSharp.Drawing.csproj

@ -10,8 +10,8 @@
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix> <VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix>
<VersionPrefix Condition="$(packageversion) == ''">0.0.1</VersionPrefix> <VersionPrefix Condition="$(packageversion) == ''">0.0.1</VersionPrefix>
<!--<TargetFrameworks>netcoreapp2.1;netstandard1.3;netstandard2.0</TargetFrameworks>--> <TargetFrameworks>netcoreapp2.1;netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>7.3</LangVersion> <LangVersion>7.3</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>

39
src/ImageSharp/Image.cs

@ -7,6 +7,7 @@ using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp namespace SixLabors.ImageSharp
{ {
@ -17,19 +18,36 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
public abstract partial class Image : IImage, IConfigurable public abstract partial class Image : IImage, IConfigurable
{ {
private Size size;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Image"/> class. /// Initializes a new instance of the <see cref="Image"/> class.
/// </summary> /// </summary>
/// <param name="configuration">The <see cref="Configuration"/>.</param> /// <param name="configuration">The <see cref="Configuration"/>.</param>
/// <param name="pixelType">The <see cref="PixelTypeInfo"/>.</param> /// <param name="pixelType">The <see cref="PixelTypeInfo"/>.</param>
/// <param name="metadata">The <see cref="ImageMetadata"/>.</param> /// <param name="metadata">The <see cref="ImageMetadata"/>.</param>
protected Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetadata metadata) /// <param name="size">The <see cref="size"/>.</param>
protected Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetadata metadata, Size size)
{ {
this.Configuration = configuration ?? Configuration.Default; this.Configuration = configuration ?? Configuration.Default;
this.PixelType = pixelType; this.PixelType = pixelType;
this.size = size;
this.Metadata = metadata ?? new ImageMetadata(); this.Metadata = metadata ?? new ImageMetadata();
} }
/// <summary>
/// Initializes a new instance of the <see cref="Image"/> class.
/// </summary>
internal Image(
Configuration configuration,
PixelTypeInfo pixelType,
ImageMetadata metadata,
int width,
int height)
: this(configuration, pixelType, metadata, new Size(width, height))
{
}
/// <summary> /// <summary>
/// Gets the <see cref="Configuration"/>. /// Gets the <see cref="Configuration"/>.
/// </summary> /// </summary>
@ -39,10 +57,10 @@ namespace SixLabors.ImageSharp
public PixelTypeInfo PixelType { get; } public PixelTypeInfo PixelType { get; }
/// <inheritdoc /> /// <inheritdoc />
public abstract int Width { get; } public int Width => this.size.Width;
/// <inheritdoc /> /// <inheritdoc />
public abstract int Height { get; } public int Height => this.size.Height;
/// <inheritdoc/> /// <inheritdoc/>
public ImageMetadata Metadata { get; } public ImageMetadata Metadata { get; }
@ -55,8 +73,6 @@ namespace SixLabors.ImageSharp
/// <inheritdoc /> /// <inheritdoc />
public abstract void Dispose(); public abstract void Dispose();
internal abstract void AcceptVisitor(IImageVisitor visitor);
/// <summary> /// <summary>
/// Saves the image to the given stream using the given image encoder. /// Saves the image to the given stream using the given image encoder.
/// </summary> /// </summary>
@ -72,6 +88,19 @@ namespace SixLabors.ImageSharp
this.AcceptVisitor(visitor); this.AcceptVisitor(visitor);
} }
/// <summary>
/// Accept a <see cref="IImageVisitor"/>.
/// Implemented by <see cref="Image{TPixel}"/> invoking <see cref="IImageVisitor.Visit{TPixel}"/>
/// with the pixel type of the image.
/// </summary>
internal abstract void AcceptVisitor(IImageVisitor visitor);
/// <summary>
/// Update the size of the image after mutation.
/// </summary>
/// <param name="size">The <see cref="Size"/>.</param>
protected void UpdateSize(Size size) => this.size = size;
private class EncodeVisitor : IImageVisitor private class EncodeVisitor : IImageVisitor
{ {
private readonly IImageEncoder encoder; private readonly IImageEncoder encoder;

3
src/ImageSharp/ImageSharp.csproj

@ -11,8 +11,7 @@
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix> <VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix>
<VersionPrefix Condition="$(packageversion) == ''">0.0.1</VersionPrefix> <VersionPrefix Condition="$(packageversion) == ''">0.0.1</VersionPrefix>
<!--<TargetFrameworks>netcoreapp2.1;netstandard1.3;netstandard2.0;net472</TargetFrameworks>--> <TargetFrameworks>netcoreapp2.1;netstandard1.3;netstandard2.0;net472</TargetFrameworks>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>

45
src/ImageSharp/Image{TPixel}.cs

@ -3,14 +3,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp namespace SixLabors.ImageSharp
{ {
@ -67,14 +66,14 @@ namespace SixLabors.ImageSharp
/// <param name="height">The height of the image in pixels.</param> /// <param name="height">The height of the image in pixels.</param>
/// <param name="metadata">The images metadata.</param> /// <param name="metadata">The images metadata.</param>
internal Image(Configuration configuration, int width, int height, ImageMetadata metadata) internal Image(Configuration configuration, int width, int height, ImageMetadata metadata)
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata) : base(configuration, PixelTypeInfo.Create<TPixel>(), metadata, width, height)
{ {
this.Frames = new ImageFrameCollection<TPixel>(this, width, height, default(TPixel)); this.Frames = new ImageFrameCollection<TPixel>(this, width, height, default(TPixel));
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Image{TPixel}"/> class /// Initializes a new instance of the <see cref="Image{TPixel}"/> class
/// wrapping an external <see cref="MemorySource{T}"/> /// wrapping an external <see cref="MemorySource{T}"/>.
/// </summary> /// </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="memorySource">The memory source.</param> /// <param name="memorySource">The memory source.</param>
@ -82,7 +81,7 @@ namespace SixLabors.ImageSharp
/// <param name="height">The height of the image in pixels.</param> /// <param name="height">The height of the image in pixels.</param>
/// <param name="metadata">The images metadata.</param> /// <param name="metadata">The images metadata.</param>
internal Image(Configuration configuration, MemorySource<TPixel> memorySource, int width, int height, ImageMetadata metadata) internal Image(Configuration configuration, MemorySource<TPixel> memorySource, int width, int height, ImageMetadata metadata)
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata) : base(configuration, PixelTypeInfo.Create<TPixel>(), metadata, width, height)
{ {
this.Frames = new ImageFrameCollection<TPixel>(this, width, height, memorySource); this.Frames = new ImageFrameCollection<TPixel>(this, width, height, memorySource);
} }
@ -97,7 +96,7 @@ namespace SixLabors.ImageSharp
/// <param name="backgroundColor">The color to initialize the pixels with.</param> /// <param name="backgroundColor">The color to initialize the pixels with.</param>
/// <param name="metadata">The images metadata.</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)
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata) : base(configuration, PixelTypeInfo.Create<TPixel>(), metadata, width, height)
{ {
this.Frames = new ImageFrameCollection<TPixel>(this, width, height, backgroundColor); this.Frames = new ImageFrameCollection<TPixel>(this, width, height, backgroundColor);
} }
@ -110,17 +109,11 @@ namespace SixLabors.ImageSharp
/// <param name="metadata">The images metadata.</param> /// <param name="metadata">The images metadata.</param>
/// <param name="frames">The frames that will be owned by this image instance.</param> /// <param name="frames">The frames that will be owned by this image instance.</param>
internal Image(Configuration configuration, ImageMetadata metadata, IEnumerable<ImageFrame<TPixel>> frames) internal Image(Configuration configuration, ImageMetadata metadata, IEnumerable<ImageFrame<TPixel>> frames)
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata) : base(configuration, PixelTypeInfo.Create<TPixel>(), metadata, ValidateFramesAndGetSize(frames))
{ {
this.Frames = new ImageFrameCollection<TPixel>(this, frames); this.Frames = new ImageFrameCollection<TPixel>(this, frames);
} }
/// <inheritdoc/>
public override int Width => this.Frames.RootFrame.Width;
/// <inheritdoc/>
public override int Height => this.Frames.RootFrame.Height;
/// <summary> /// <summary>
/// Gets the frames. /// Gets the frames.
/// </summary> /// </summary>
@ -173,7 +166,7 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <typeparam name="TPixel2">The pixel format.</typeparam> /// <typeparam name="TPixel2">The pixel format.</typeparam>
/// <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>
/// <returns>The <see cref="Image{TPixel2}"/></returns> /// <returns>The <see cref="Image{TPixel2}"/>.</returns>
public Image<TPixel2> CloneAs<TPixel2>(Configuration configuration) public Image<TPixel2> CloneAs<TPixel2>(Configuration configuration)
where TPixel2 : struct, IPixel<TPixel2> where TPixel2 : struct, IPixel<TPixel2>
{ {
@ -184,6 +177,7 @@ namespace SixLabors.ImageSharp
/// <inheritdoc/> /// <inheritdoc/>
public override void Dispose() => this.Frames.Dispose(); public override void Dispose() => this.Frames.Dispose();
/// <inheritdoc />
internal override void AcceptVisitor(IImageVisitor visitor) internal override void AcceptVisitor(IImageVisitor visitor)
{ {
visitor.Visit(this); visitor.Visit(this);
@ -204,6 +198,29 @@ namespace SixLabors.ImageSharp
{ {
this.Frames[i].SwapOrCopyPixelsBufferFrom(pixelSource.Frames[i]); this.Frames[i].SwapOrCopyPixelsBufferFrom(pixelSource.Frames[i]);
} }
this.UpdateSize(pixelSource.Size());
}
private static Size ValidateFramesAndGetSize(IEnumerable<ImageFrame<TPixel>> frames)
{
Guard.NotNull(frames, nameof(frames));
ImageFrame<TPixel> rootFrame = frames.FirstOrDefault();
if (rootFrame == null)
{
throw new ArgumentException("Must not be empty.", nameof(frames));
}
Size rootSize = rootFrame.Size();
if (frames.Any(f => f.Size() != rootSize))
{
throw new ArgumentException("The provided frames must be of the same size.", nameof(frames));
}
return rootSize;
} }
} }
} }
Loading…
Cancel
Save