From c08b155d2cc6916e11f9ddeeca60f7a39fe9e328 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 17 Feb 2018 19:32:17 +0100 Subject: [PATCH] fix build after merge --- src/ImageSharp/Advanced/AdvancedImageExtensions.cs | 8 ++++++++ src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 10 +++++++--- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 13 ++++++++----- src/ImageSharp/Image/ImageFrame{TPixel}.cs | 6 ++++-- .../Transforms/AffineTransformProcessor.cs | 8 +++++--- .../Transforms/ProjectiveTransformProcessor.cs | 13 +++++++++---- .../Processors/Transforms/WeightsBuffer.cs | 5 +++-- .../Processors/Transforms/WeightsWindow.cs | 2 +- .../ImageSharp.Tests/Formats/GeneralFormatTests.cs | 9 +++++---- .../Transforms/ResizeProfilingBenchmarks.cs | 3 ++- .../Quantization/QuantizedImageTests.cs | 2 +- 11 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs index 0acb846c5..612ced5d8 100644 --- a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs +++ b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs @@ -88,6 +88,14 @@ namespace SixLabors.ImageSharp.Advanced where TPixel : struct, IPixel => source.Frames.RootFrame.GetPixelRowSpan(row); + /// + /// Gets the assigned to 'source'. + /// + /// The source image + /// Returns the configuration. + internal static MemoryManager GetMemoryManager(this IConfigurable source) + => GetConfiguration(source).MemoryManager; + /// /// Gets the span to the backing buffer. /// diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 0c1d8b21a..c120c9e11 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -92,6 +92,8 @@ namespace SixLabors.ImageSharp.Formats.Gif /// public FrameDecodingMode DecodingMode { get; } + private MemoryManager MemoryManager => this.configuration.MemoryManager; + /// /// Decodes the stream to the image. /// @@ -148,7 +150,8 @@ namespace SixLabors.ImageSharp.Formats.Gif { break; } - this.globalColorTable = this.configuration.MemoryManager.Allocate(this.globalColorTableLength, true); + + this.globalColorTable = this.MemoryManager.Allocate(this.globalColorTableLength, true); nextFlag = stream.ReadByte(); if (nextFlag == -1) @@ -334,7 +337,7 @@ namespace SixLabors.ImageSharp.Formats.Gif continue; } - using (Buffer commentsBuffer = this.configuration.MemoryManager.Allocate(length)) + using (Buffer commentsBuffer = this.MemoryManager.Allocate(length)) { this.currentStream.Read(commentsBuffer.Array, 0, length); string comments = this.TextEncoding.GetString(commentsBuffer.Array, 0, length); @@ -601,7 +604,8 @@ namespace SixLabors.ImageSharp.Formats.Gif if (this.logicalScreenDescriptor.GlobalColorTableFlag) { this.globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3; - this.globalColorTable = Buffer.CreateClean(this.globalColorTableLength); + + this.globalColorTable = this.MemoryManager.Allocate(this.globalColorTableLength, true); // Read the global color table from the stream stream.Read(this.globalColorTable.Array, 0, this.globalColorTableLength); diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 6a04c77b9..0546c5ee3 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -190,6 +190,8 @@ namespace SixLabors.ImageSharp.Formats.Png this.textEncoding = options.TextEncoding ?? PngConstants.DefaultEncoding; this.ignoreMetadata = options.IgnoreMetadata; } + + private MemoryManager MemoryManager => this.configuration.MemoryManager; /// /// Decodes the stream to the image. @@ -297,17 +299,17 @@ namespace SixLabors.ImageSharp.Formats.Png switch (currentChunk.Type) { case PngChunkTypes.Header: - this.ReadHeaderChunk(currentChunk.Data); + this.ReadHeaderChunk(currentChunk.Data.Array); this.ValidateHeader(); break; case PngChunkTypes.Physical: - this.ReadPhysicalChunk(metadata, currentChunk.Data); + this.ReadPhysicalChunk(metadata, currentChunk.Data.Array); break; case PngChunkTypes.Data: this.SkipChunkDataAndCrc(currentChunk); break; case PngChunkTypes.Text: - this.ReadTextChunk(metadata, currentChunk.Data, currentChunk.Length); + this.ReadTextChunk(metadata, currentChunk.Data.Array, currentChunk.Length); break; case PngChunkTypes.End: this.isEndChunkReached = true; @@ -319,7 +321,7 @@ namespace SixLabors.ImageSharp.Formats.Png // Data is rented in ReadChunkData() if (currentChunk.Data != null) { - ArrayPool.Shared.Return(currentChunk.Data); + ArrayPool.Shared.Return(currentChunk.Data.Array); } } } @@ -435,10 +437,11 @@ namespace SixLabors.ImageSharp.Formats.Png this.bytesPerSample = this.header.BitDepth / 8; } - this.previousScanline = this.configuration.MemoryManager.Allocate(this.bytesPerScanline, true); + this.previousScanline = this.MemoryManager.Allocate(this.bytesPerScanline, true); this.scanline = this.configuration.MemoryManager.Allocate(this.bytesPerScanline, true); } + /// /// Calculates the correct number of bits per pixel for the given color type. /// diff --git a/src/ImageSharp/Image/ImageFrame{TPixel}.cs b/src/ImageSharp/Image/ImageFrame{TPixel}.cs index e96275a9f..eb34c2673 100644 --- a/src/ImageSharp/Image/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/Image/ImageFrame{TPixel}.cs @@ -61,16 +61,18 @@ namespace SixLabors.ImageSharp /// /// Initializes a new instance of the class. /// + /// The to use for buffer allocations. /// The of the frame. /// The meta data. - internal ImageFrame(Size size, ImageFrameMetaData metaData) - : this(size.Width, size.Height, metaData) + internal ImageFrame(MemoryManager memoryManager, Size size, ImageFrameMetaData metaData) + : this(memoryManager, size.Width, size.Height, metaData) { } /// /// Initializes a new instance of the class. /// + /// The to use for buffer allocations. /// The source. internal ImageFrame(MemoryManager memoryManager, ImageFrame source) { diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs index 8595e8692..9a5b000de 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs @@ -73,7 +73,7 @@ namespace SixLabors.ImageSharp.Processing.Processors // We will always be creating the clone even for mutate because we may need to resize the canvas IEnumerable> frames = - source.Frames.Select(x => new ImageFrame(this.targetDimensions, x.MetaData.Clone())); + source.Frames.Select(x => new ImageFrame(source.GetMemoryManager(), this.targetDimensions, x.MetaData.Clone())); // Use the overload to prevent an extra frame being added return new Image(source.GetConfiguration(), source.MetaData.Clone(), frames); @@ -130,8 +130,10 @@ namespace SixLabors.ImageSharp.Processing.Processors int xLength = (int)MathF.Ceiling((radius.X * 2) + 2); int yLength = (int)MathF.Ceiling((radius.Y * 2) + 2); - using (var yBuffer = new Buffer2D(yLength, height)) - using (var xBuffer = new Buffer2D(xLength, height)) + MemoryManager memoryManager = configuration.MemoryManager; + + using (Buffer2D yBuffer = memoryManager.Allocate2D(yLength, height)) + using (Buffer2D xBuffer = memoryManager.Allocate2D(xLength, height)) { Parallel.For( 0, diff --git a/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs index 7e547727e..3c04c2722 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs @@ -73,8 +73,11 @@ namespace SixLabors.ImageSharp.Processing.Processors } // We will always be creating the clone even for mutate because we may need to resize the canvas - IEnumerable> frames = - source.Frames.Select(x => new ImageFrame(this.targetRectangle.Width, this.targetRectangle.Height, x.MetaData.Clone())); + IEnumerable> frames = source.Frames.Select( + x => new ImageFrame( + source.GetMemoryManager(), + this.targetRectangle.Size, + x.MetaData.Clone())); // Use the overload to prevent an extra frame being added return new Image(source.GetConfiguration(), source.MetaData.Clone(), frames); @@ -125,8 +128,10 @@ namespace SixLabors.ImageSharp.Processing.Processors int xLength = (int)MathF.Ceiling((radius.X * 2) + 2); int yLength = (int)MathF.Ceiling((radius.Y * 2) + 2); - using (var yBuffer = new Buffer2D(yLength, height)) - using (var xBuffer = new Buffer2D(xLength, height)) + MemoryManager memoryManager = configuration.MemoryManager; + + using (Buffer2D yBuffer = memoryManager.Allocate2D(yLength, height)) + using (Buffer2D xBuffer = memoryManager.Allocate2D(xLength, height)) { Parallel.For( 0, diff --git a/src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs b/src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs index 0e91087f9..d633a3869 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs @@ -17,11 +17,12 @@ namespace SixLabors.ImageSharp.Processing.Processors /// /// Initializes a new instance of the class. /// + /// The MemoryManager to use for allocations. /// The size of the source window /// The size of the destination window - public WeightsBuffer(int sourceSize, int destinationSize) + public WeightsBuffer(MemoryManager memoryManager, int sourceSize, int destinationSize) { - this.dataBuffer = Buffer2D.CreateClean(sourceSize, destinationSize); + this.dataBuffer = memoryManager.Allocate2D(sourceSize, destinationSize, true); this.Weights = new WeightsWindow[destinationSize]; } diff --git a/src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs b/src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs index b0a530514..1ee61a9a6 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs @@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp.Processing.Processors { this.flatStartIndex = (index * buffer.Width) + left; this.Left = left; - this.buffer = buffer; + this.buffer = buffer.Buffer; this.Length = length; } diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index 97128e2c9..a6a883d42 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -14,7 +14,8 @@ namespace SixLabors.ImageSharp.Tests { using System; - + using SixLabors.ImageSharp.Advanced; + using SixLabors.ImageSharp.Memory; public class GeneralFormatTests : FileTestBase { @@ -178,7 +179,7 @@ namespace SixLabors.ImageSharp.Tests { using (var memoryStream = new MemoryStream()) { - image.Save(memoryStream, GetEncoder(format)); + image.Save(memoryStream, GetEncoder(image.GetMemoryManager(), format)); memoryStream.Position = 0; var imageInfo = Image.Identify(memoryStream); @@ -189,12 +190,12 @@ namespace SixLabors.ImageSharp.Tests } } - private static IImageEncoder GetEncoder(string format) + private static IImageEncoder GetEncoder(MemoryManager memoryManager, string format) { switch (format) { case "png": - return new PngEncoder(); + return new PngEncoder(memoryManager); case "gif": return new GifEncoder(); case "bmp": diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs index c87dcb648..54a3b7418 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs @@ -11,6 +11,8 @@ using Xunit.Abstractions; namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms { + using SixLabors.ImageSharp.Memory; + public class ResizeProfilingBenchmarks : MeasureFixture { public ResizeProfilingBenchmarks(ITestOutputHelper output) @@ -38,7 +40,6 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms // [Fact] public void PrintWeightsData() { - var proc = new ResizeProcessor(KnownResamplers.Bicubic, 200, 200); var proc = new ResizeProcessor(Configuration.Default.MemoryManager, new BicubicResampler(), 200, 200); WeightsBuffer weights = proc.PrecomputeWeights(200, 500); diff --git a/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs b/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs index a9e5b440d..5d569aa8f 100644 --- a/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs +++ b/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs @@ -12,7 +12,7 @@ { var palette = new PaletteQuantizer(); var octree = new OctreeQuantizer(); - var wu = new WuQuantizer(); + var wu = new WuQuantizer(Configuration.Default.MemoryManager); Assert.True(palette.Dither); Assert.True(octree.Dither);