From fa1e0baced645949989929db33a59683855c509e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 5 May 2019 17:43:06 +0200 Subject: [PATCH] cache Image dimensions into a field + re-enable all target frameworks --- .../ImageSharp.Drawing.csproj | 4 +- src/ImageSharp/Image.cs | 39 +++++++++++++--- src/ImageSharp/ImageSharp.csproj | 3 +- src/ImageSharp/Image{TPixel}.cs | 45 +++++++++++++------ 4 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj index 1734c48de2..ac5d7fdea6 100644 --- a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj +++ b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj @@ -10,8 +10,8 @@ $(packageversion) 0.0.1 - - netcoreapp2.1 + netcoreapp2.1;netstandard1.3;netstandard2.0 + netstandard2.0 7.3 true diff --git a/src/ImageSharp/Image.cs b/src/ImageSharp/Image.cs index d0a1c4319d..1566fd0eed 100644 --- a/src/ImageSharp/Image.cs +++ b/src/ImageSharp/Image.cs @@ -7,6 +7,7 @@ using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; namespace SixLabors.ImageSharp { @@ -17,19 +18,36 @@ namespace SixLabors.ImageSharp /// public abstract partial class Image : IImage, IConfigurable { + private Size size; + /// /// Initializes a new instance of the class. /// /// The . /// The . /// The . - protected Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetadata metadata) + /// The . + protected Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetadata metadata, Size size) { this.Configuration = configuration ?? Configuration.Default; this.PixelType = pixelType; + this.size = size; this.Metadata = metadata ?? new ImageMetadata(); } + /// + /// Initializes a new instance of the class. + /// + internal Image( + Configuration configuration, + PixelTypeInfo pixelType, + ImageMetadata metadata, + int width, + int height) + : this(configuration, pixelType, metadata, new Size(width, height)) + { + } + /// /// Gets the . /// @@ -39,10 +57,10 @@ namespace SixLabors.ImageSharp public PixelTypeInfo PixelType { get; } /// - public abstract int Width { get; } + public int Width => this.size.Width; /// - public abstract int Height { get; } + public int Height => this.size.Height; /// public ImageMetadata Metadata { get; } @@ -55,8 +73,6 @@ namespace SixLabors.ImageSharp /// public abstract void Dispose(); - internal abstract void AcceptVisitor(IImageVisitor visitor); - /// /// Saves the image to the given stream using the given image encoder. /// @@ -72,6 +88,19 @@ namespace SixLabors.ImageSharp this.AcceptVisitor(visitor); } + /// + /// Accept a . + /// Implemented by invoking + /// with the pixel type of the image. + /// + internal abstract void AcceptVisitor(IImageVisitor visitor); + + /// + /// Update the size of the image after mutation. + /// + /// The . + protected void UpdateSize(Size size) => this.size = size; + private class EncodeVisitor : IImageVisitor { private readonly IImageEncoder encoder; diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index f780ca666a..e763b8ef1c 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -11,8 +11,7 @@ $(packageversion) 0.0.1 - - netcoreapp2.1 + netcoreapp2.1;netstandard1.3;netstandard2.0;net472 true true diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index 243e2912be..27e2bc593c 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/src/ImageSharp/Image{TPixel}.cs @@ -3,14 +3,13 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; namespace SixLabors.ImageSharp { @@ -67,14 +66,14 @@ namespace SixLabors.ImageSharp /// The height of the image in pixels. /// The images metadata. internal Image(Configuration configuration, int width, int height, ImageMetadata metadata) - : base(configuration, PixelTypeInfo.Create(), metadata) + : base(configuration, PixelTypeInfo.Create(), metadata, width, height) { this.Frames = new ImageFrameCollection(this, width, height, default(TPixel)); } /// /// Initializes a new instance of the class - /// wrapping an external + /// wrapping an external . /// /// The configuration providing initialization code which allows extending the library. /// The memory source. @@ -82,7 +81,7 @@ namespace SixLabors.ImageSharp /// The height of the image in pixels. /// The images metadata. internal Image(Configuration configuration, MemorySource memorySource, int width, int height, ImageMetadata metadata) - : base(configuration, PixelTypeInfo.Create(), metadata) + : base(configuration, PixelTypeInfo.Create(), metadata, width, height) { this.Frames = new ImageFrameCollection(this, width, height, memorySource); } @@ -97,7 +96,7 @@ namespace SixLabors.ImageSharp /// The color to initialize the pixels with. /// The images metadata. internal Image(Configuration configuration, int width, int height, TPixel backgroundColor, ImageMetadata metadata) - : base(configuration, PixelTypeInfo.Create(), metadata) + : base(configuration, PixelTypeInfo.Create(), metadata, width, height) { this.Frames = new ImageFrameCollection(this, width, height, backgroundColor); } @@ -110,17 +109,11 @@ namespace SixLabors.ImageSharp /// The images metadata. /// The frames that will be owned by this image instance. internal Image(Configuration configuration, ImageMetadata metadata, IEnumerable> frames) - : base(configuration, PixelTypeInfo.Create(), metadata) + : base(configuration, PixelTypeInfo.Create(), metadata, ValidateFramesAndGetSize(frames)) { this.Frames = new ImageFrameCollection(this, frames); } - /// - public override int Width => this.Frames.RootFrame.Width; - - /// - public override int Height => this.Frames.RootFrame.Height; - /// /// Gets the frames. /// @@ -173,7 +166,7 @@ namespace SixLabors.ImageSharp /// /// The pixel format. /// The configuration providing initialization code which allows extending the library. - /// The + /// The . public Image CloneAs(Configuration configuration) where TPixel2 : struct, IPixel { @@ -184,6 +177,7 @@ namespace SixLabors.ImageSharp /// public override void Dispose() => this.Frames.Dispose(); + /// internal override void AcceptVisitor(IImageVisitor visitor) { visitor.Visit(this); @@ -204,6 +198,29 @@ namespace SixLabors.ImageSharp { this.Frames[i].SwapOrCopyPixelsBufferFrom(pixelSource.Frames[i]); } + + this.UpdateSize(pixelSource.Size()); + } + + private static Size ValidateFramesAndGetSize(IEnumerable> frames) + { + Guard.NotNull(frames, nameof(frames)); + + ImageFrame 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; } } } \ No newline at end of file