From 7881fdd341ad1da9e7a10812b690593e820507d6 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 20 Feb 2018 02:07:27 +0100 Subject: [PATCH] Hide Buffer indexer + !! WuQuantizer review in comments !! --- .../Brushes/ImageBrush{TPixel}.cs | 13 +- .../Brushes/PatternBrush{TPixel}.cs | 13 +- .../Brushes/Processors/BrushApplicator.cs | 13 +- .../Brushes/RecolorBrush{TPixel}.cs | 13 +- .../Brushes/SolidBrush{TPixel}.cs | 15 +- .../Processors/DrawImageProcessor.cs | 9 +- .../Processors/FillProcessor.cs | 24 +-- .../Processors/FillRegionProcessor.cs | 4 +- src/ImageSharp/Formats/Gif/LzwDecoder.cs | 26 +-- src/ImageSharp/Formats/Gif/LzwEncoder.cs | 39 ++-- .../Common/Decoder/JpegImagePostProcessor.cs | 4 +- .../GolangPort/Components/Decoder/Bytes.cs | 22 ++- .../PdfJsPort/Components/PdfJsHuffmanTable.cs | 10 +- .../Components/PdfJsJpegPixelArea.cs | 8 +- .../PdfJsPort/Components/PdfJsScanDecoder.cs | 14 +- .../Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 4 +- .../{FakeBuffer.cs => BasicArrayBuffer.cs} | 10 +- src/ImageSharp/Memory/Buffer{T}.cs | 69 +------ src/ImageSharp/Memory/MemoryManager.cs | 4 +- .../Effects/BackgroundColorProcessor.cs | 14 +- .../Processors/Overlays/GlowProcessor.cs | 16 +- .../Processors/Overlays/VignetteProcessor.cs | 18 +- .../Processors/Transforms/ResizeProcessor.cs | 8 +- .../Quantizers/QuantizerBase{TPixel}.cs | 1 + .../Quantizers/WuQuantizer{TPixel}.cs | 173 +++++++++++------- .../Color/Bulk/PackFromVector4.cs | 4 +- .../Color/Bulk/PackFromXyzw.cs | 4 +- .../Color/Bulk/ToVector4.cs | 4 +- .../ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs | 4 +- .../Color/Bulk/ToXyzw.cs | 4 +- .../PixelBlenders/PorterDuffBulkVsPixel.cs | 42 ++--- tests/ImageSharp.Benchmarks/Samplers/Glow.cs | 5 +- .../Formats/Jpg/JpegColorConverterTests.cs | 2 +- .../PixelFormats/PixelOperationsTests.cs | 31 ++-- .../ReferenceCodecs/SystemDrawingBridge.cs | 21 ++- 35 files changed, 334 insertions(+), 331 deletions(-) rename src/ImageSharp/Memory/{FakeBuffer.cs => BasicArrayBuffer.cs} (82%) diff --git a/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs index ff69d65ee6..5866d9feae 100644 --- a/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs @@ -122,24 +122,27 @@ namespace SixLabors.ImageSharp.Drawing.Brushes internal override void Apply(Span scanline, int x, int y) { // Create a span for colors - using (var amountBuffer = this.Target.MemoryManager.Allocate(scanline.Length)) - using (var overlay = this.Target.MemoryManager.Allocate(scanline.Length)) + using (Buffer amountBuffer = this.Target.MemoryManager.Allocate(scanline.Length)) + using (Buffer overlay = this.Target.MemoryManager.Allocate(scanline.Length)) { + Span amountSpan = amountBuffer.Span; + Span overlaySpan = overlay.Span; + int sourceY = (y - this.offsetY) % this.yLength; int offsetX = x - this.offsetX; Span sourceRow = this.source.GetPixelRowSpan(sourceY); for (int i = 0; i < scanline.Length; i++) { - amountBuffer[i] = scanline[i] * this.Options.BlendPercentage; + amountSpan[i] = scanline[i] * this.Options.BlendPercentage; int sourceX = (i + offsetX) % this.xLength; TPixel pixel = sourceRow[sourceX]; - overlay[i] = pixel; + overlaySpan[i] = pixel; } Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length); - this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); + this.Blender.Blend(destinationRow, destinationRow, overlaySpan, amountSpan); } } } diff --git a/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs index 2a25979873..ac8ffa7941 100644 --- a/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs @@ -152,19 +152,22 @@ namespace SixLabors.ImageSharp.Drawing.Brushes internal override void Apply(Span scanline, int x, int y) { int patternY = y % this.pattern.Height; - using (var amountBuffer = this.Target.MemoryManager.Allocate(scanline.Length)) - using (var overlay = this.Target.MemoryManager.Allocate(scanline.Length)) + using (Buffer amountBuffer = this.Target.MemoryManager.Allocate(scanline.Length)) + using (Buffer overlay = this.Target.MemoryManager.Allocate(scanline.Length)) { + Span amountSpan = amountBuffer.Span; + Span overlaySpan = overlay.Span; + for (int i = 0; i < scanline.Length; i++) { - amountBuffer[i] = (scanline[i] * this.Options.BlendPercentage).Clamp(0, 1); + amountSpan[i] = (scanline[i] * this.Options.BlendPercentage).Clamp(0, 1); int patternX = (x + i) % this.pattern.Width; - overlay[i] = this.pattern[patternY, patternX]; + overlaySpan[i] = this.pattern[patternY, patternX]; } Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length); - this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); + this.Blender.Blend(destinationRow, destinationRow, overlaySpan, amountSpan); } } } diff --git a/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs b/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs index 08bbb571ad..dadd546e93 100644 --- a/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs +++ b/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs @@ -65,21 +65,24 @@ namespace SixLabors.ImageSharp.Drawing.Brushes.Processors /// scanlineBuffer will be > scanlineWidth but provide and offset in case we want to share a larger buffer across runs. internal virtual void Apply(Span scanline, int x, int y) { - using (var amountBuffer = this.Target.MemoryManager.Allocate(scanline.Length)) - using (var overlay = this.Target.MemoryManager.Allocate(scanline.Length)) + using (Buffer amountBuffer = this.Target.MemoryManager.Allocate(scanline.Length)) + using (Buffer overlay = this.Target.MemoryManager.Allocate(scanline.Length)) { + Span amountSpan = amountBuffer.Span; + Span overlaySpan = overlay.Span; + for (int i = 0; i < scanline.Length; i++) { if (this.Options.BlendPercentage < 1) { - amountBuffer[i] = scanline[i] * this.Options.BlendPercentage; + amountSpan[i] = scanline[i] * this.Options.BlendPercentage; } - overlay[i] = this[x + i, y]; + overlaySpan[i] = this[x + i, y]; } Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length); - this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); + this.Blender.Blend(destinationRow, destinationRow, overlaySpan, amountSpan); } } } diff --git a/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs index d480457113..d1fda7ebe6 100644 --- a/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs @@ -144,22 +144,25 @@ namespace SixLabors.ImageSharp.Drawing.Brushes /// internal override void Apply(Span scanline, int x, int y) { - using (var amountBuffer = this.Target.MemoryManager.Allocate(scanline.Length)) - using (var overlay = this.Target.MemoryManager.Allocate(scanline.Length)) + using (Buffer amountBuffer = this.Target.MemoryManager.Allocate(scanline.Length)) + using (Buffer overlay = this.Target.MemoryManager.Allocate(scanline.Length)) { + Span amountSpan = amountBuffer.Span; + Span overlaySpan = overlay.Span; + for (int i = 0; i < scanline.Length; i++) { - amountBuffer[i] = scanline[i] * this.Options.BlendPercentage; + amountSpan[i] = scanline[i] * this.Options.BlendPercentage; int offsetX = x + i; // no doubt this one can be optermised further but I can't imagine its // actually being used and can probably be removed/interalised for now - overlay[i] = this[offsetX, y]; + overlaySpan[i] = this[offsetX, y]; } Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length); - this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); + this.Blender.Blend(destinationRow, destinationRow, overlaySpan, amountSpan); } } } diff --git a/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs index 2d460603bb..510299f263 100644 --- a/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs @@ -62,10 +62,7 @@ namespace SixLabors.ImageSharp.Drawing.Brushes : base(source, options) { this.Colors = source.MemoryManager.Allocate(source.Width); - for (int i = 0; i < this.Colors.Length; i++) - { - this.Colors[i] = color; - } + this.Colors.Span.Fill(color); } /// @@ -81,7 +78,7 @@ namespace SixLabors.ImageSharp.Drawing.Brushes /// /// The color /// - internal override TPixel this[int x, int y] => this.Colors[x]; + internal override TPixel this[int x, int y] => this.Colors.Span[x]; /// public override void Dispose() @@ -96,14 +93,16 @@ namespace SixLabors.ImageSharp.Drawing.Brushes { Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length); - using (var amountBuffer = this.Target.MemoryManager.Allocate(scanline.Length)) + using (Buffer amountBuffer = this.Target.MemoryManager.Allocate(scanline.Length)) { + Span amountSpan = amountBuffer.Span; + for (int i = 0; i < scanline.Length; i++) { - amountBuffer[i] = scanline[i] * this.Options.BlendPercentage; + amountSpan[i] = scanline[i] * this.Options.BlendPercentage; } - this.Blender.Blend(destinationRow, destinationRow, this.Colors, amountBuffer); + this.Blender.Blend(destinationRow, destinationRow, this.Colors.Span, amountSpan); } } catch (Exception) diff --git a/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs b/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs index 201adfecc0..eb3949b007 100644 --- a/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs @@ -84,12 +84,9 @@ namespace SixLabors.ImageSharp.Drawing.Processors maxY = Math.Min(this.Location.Y + this.Size.Height, maxY); int width = maxX - minX; - using (var amount = this.Image.GetConfiguration().MemoryManager.Allocate(width)) + using (Buffer amount = this.Image.GetConfiguration().MemoryManager.Allocate(width)) { - for (int i = 0; i < width; i++) - { - amount[i] = this.Alpha; - } + amount.Span.Fill(this.Alpha); Parallel.For( minY, @@ -99,7 +96,7 @@ namespace SixLabors.ImageSharp.Drawing.Processors { Span background = source.GetPixelRowSpan(y).Slice(minX, width); Span foreground = targetImage.GetPixelRowSpan(y - this.Location.Y).Slice(targetX, width); - this.blender.Blend(background, background, foreground, amount); + this.blender.Blend(background, background, foreground, amount.Span); }); } } diff --git a/src/ImageSharp.Drawing/Processors/FillProcessor.cs b/src/ImageSharp.Drawing/Processors/FillProcessor.cs index 0174a63880..75ea1f2033 100644 --- a/src/ImageSharp.Drawing/Processors/FillProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/FillProcessor.cs @@ -66,25 +66,25 @@ namespace SixLabors.ImageSharp.Drawing.Processors int width = maxX - minX; - using (var amount = source.MemoryManager.Allocate(width)) - using (BrushApplicator applicator = this.brush.CreateApplicator(source, sourceRectangle, this.options)) + using (Buffer amount = source.MemoryManager.Allocate(width)) + using (BrushApplicator applicator = this.brush.CreateApplicator( + source, + sourceRectangle, + this.options)) { - for (int i = 0; i < width; i++) - { - amount[i] = this.options.BlendPercentage; - } + amount.Span.Fill(this.options.BlendPercentage); - Parallel.For( + Parallel.For( minY, maxY, configuration.ParallelOptions, y => - { - int offsetY = y - startY; - int offsetX = minX - startX; + { + int offsetY = y - startY; + int offsetX = minX - startX; - applicator.Apply(amount, offsetX, offsetY); - }); + applicator.Apply(amount.Span, offsetX, offsetY); + }); } } } diff --git a/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs b/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs index fc3f289abf..ae5ee2d9f6 100644 --- a/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs @@ -99,8 +99,8 @@ namespace SixLabors.ImageSharp.Drawing.Processors using (BrushApplicator applicator = this.Brush.CreateApplicator(source, rect, this.Options)) { int scanlineWidth = maxX - minX; - using (FakeBuffer buffer = source.MemoryManager.AllocateFake(maxIntersections)) - using (FakeBuffer scanline = source.MemoryManager.AllocateFake(scanlineWidth)) + using (BasicArrayBuffer buffer = source.MemoryManager.AllocateFake(maxIntersections)) + using (BasicArrayBuffer scanline = source.MemoryManager.AllocateFake(scanlineWidth)) { bool scanlineDirty = true; for (int y = minY; y < maxY; y++) diff --git a/src/ImageSharp/Formats/Gif/LzwDecoder.cs b/src/ImageSharp/Formats/Gif/LzwDecoder.cs index b28857e573..27ca275db1 100644 --- a/src/ImageSharp/Formats/Gif/LzwDecoder.cs +++ b/src/ImageSharp/Formats/Gif/LzwDecoder.cs @@ -115,10 +115,14 @@ namespace SixLabors.ImageSharp.Formats.Gif int data = 0; int first = 0; + Span prefixSpan = this.prefix.Span; + Span suffixSpan = this.suffix.Span; + Span pixelStackSpan = this.pixelStack.Span; + for (code = 0; code < clearCode; code++) { - this.prefix[code] = 0; - this.suffix[code] = (byte)code; + prefixSpan[code] = 0; + suffixSpan[code] = (byte)code; } byte[] buffer = new byte[255]; @@ -172,7 +176,7 @@ namespace SixLabors.ImageSharp.Formats.Gif if (oldCode == NullCode) { - this.pixelStack[top++] = this.suffix[code]; + pixelStackSpan[top++] = suffixSpan[code]; oldCode = code; first = code; continue; @@ -181,27 +185,27 @@ namespace SixLabors.ImageSharp.Formats.Gif int inCode = code; if (code == availableCode) { - this.pixelStack[top++] = (byte)first; + pixelStackSpan[top++] = (byte)first; code = oldCode; } while (code > clearCode) { - this.pixelStack[top++] = this.suffix[code]; - code = this.prefix[code]; + pixelStackSpan[top++] = suffixSpan[code]; + code = prefixSpan[code]; } - first = this.suffix[code]; + first = suffixSpan[code]; - this.pixelStack[top++] = this.suffix[code]; + pixelStackSpan[top++] = suffixSpan[code]; // Fix for Gifs that have "deferred clear code" as per here : // https://bugzilla.mozilla.org/show_bug.cgi?id=55918 if (availableCode < MaxStackSize) { - this.prefix[availableCode] = oldCode; - this.suffix[availableCode] = first; + prefixSpan[availableCode] = oldCode; + suffixSpan[availableCode] = first; availableCode++; if (availableCode == codeMask + 1 && availableCode < MaxStackSize) { @@ -217,7 +221,7 @@ namespace SixLabors.ImageSharp.Formats.Gif top--; // Clear missing pixels - pixels[xyz++] = (byte)this.pixelStack[top]; + pixels[xyz++] = (byte)pixelStackSpan[top]; } } diff --git a/src/ImageSharp/Formats/Gif/LzwEncoder.cs b/src/ImageSharp/Formats/Gif/LzwEncoder.cs index 2ecd229b5f..115ecf6fbe 100644 --- a/src/ImageSharp/Formats/Gif/LzwEncoder.cs +++ b/src/ImageSharp/Formats/Gif/LzwEncoder.cs @@ -259,7 +259,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// The output stream. private void ClearBlock(Stream stream) { - this.ResetCodeTable(this.hsize); + this.ResetCodeTable(); this.freeEntry = this.clearCode + 2; this.clearFlag = true; @@ -269,13 +269,15 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// Reset the code table. /// - /// The hash size. - private void ResetCodeTable(int size) + private void ResetCodeTable() { - for (int i = 0; i < size; ++i) - { - this.hashTable[i] = -1; - } + this.hashTable.Span.Fill(-1); + + // Original code: + // for (int i = 0; i < size; ++i) + // { + // this.hashTable[i] = -1; + // } } /// @@ -317,23 +319,26 @@ namespace SixLabors.ImageSharp.Formats.Gif hsizeReg = this.hsize; - this.ResetCodeTable(hsizeReg); // clear hash table + this.ResetCodeTable(); // clear hash table this.Output(this.clearCode, stream); + Span hashTableSpan = this.hashTable.Span; + Span codeTableSpan = this.codeTable.Span; + while ((c = this.NextPixel()) != Eof) { fcode = (c << this.maxbits) + ent; int i = (c << hshift) ^ ent /* = 0 */; - if (this.hashTable[i] == fcode) + if (hashTableSpan[i] == fcode) { - ent = this.codeTable[i]; + ent = codeTableSpan[i]; continue; } // Non-empty slot - if (this.hashTable[i] >= 0) + if (hashTableSpan[i] >= 0) { int disp = hsizeReg - i; if (i == 0) @@ -348,15 +353,15 @@ namespace SixLabors.ImageSharp.Formats.Gif i += hsizeReg; } - if (this.hashTable[i] == fcode) + if (hashTableSpan[i] == fcode) { - ent = this.codeTable[i]; + ent = codeTableSpan[i]; break; } } - while (this.hashTable[i] >= 0); + while (hashTableSpan[i] >= 0); - if (this.hashTable[i] == fcode) + if (hashTableSpan[i] == fcode) { continue; } @@ -366,8 +371,8 @@ namespace SixLabors.ImageSharp.Formats.Gif ent = c; if (this.freeEntry < this.maxmaxcode) { - this.codeTable[i] = this.freeEntry++; // code -> hashtable - this.hashTable[i] = fcode; + codeTableSpan[i] = this.freeEntry++; // code -> hashtable + hashTableSpan[i] = fcode; } else { diff --git a/src/ImageSharp/Formats/Jpeg/Common/Decoder/JpegImagePostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Common/Decoder/JpegImagePostProcessor.cs index 1b83f62eb4..3258fd32cc 100644 --- a/src/ImageSharp/Formats/Jpeg/Common/Decoder/JpegImagePostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Common/Decoder/JpegImagePostProcessor.cs @@ -150,11 +150,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Common.Decoder int y = yy - this.PixelRowCounter; var values = new ColorConverters.JpegColorConverter.ComponentValues(buffers, y); - this.colorConverter.ConvertToRGBA(values, this.rgbaBuffer); + this.colorConverter.ConvertToRGBA(values, this.rgbaBuffer.Span); Span destRow = destination.GetPixelRowSpan(yy); - PixelOperations.Instance.PackFromVector4(this.rgbaBuffer, destRow, destination.Width); + PixelOperations.Instance.PackFromVector4(this.rgbaBuffer.Span, destRow, destination.Width); } } } diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs index 56a85bc9df..7a22b043b8 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs @@ -14,6 +14,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// Bytes is a byte buffer, similar to a stream, except that it /// has to be able to unread more than 1 byte, due to byte stuffing. /// Byte stuffing is specified in section F.1.2.3. + /// TODO: Optimize buffer management inside this class! /// internal struct Bytes : IDisposable { @@ -26,7 +27,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// Gets or sets the buffer. /// buffer[i:j] are the buffered bytes read from the underlying /// stream that haven't yet been passed further on. - /// TODO: Do we really need buffer here? Might be an optimiziation opportunity. /// public IManagedByteBuffer Buffer; @@ -88,7 +88,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder // Take the fast path if bytes.buf contains at least two bytes. if (this.I + 2 <= this.J) { - x = this.BufferAsInt[this.I]; + Span bufferSpan = this.BufferAsInt.Span; + x = bufferSpan[this.I]; this.I++; this.UnreadableBytes = 1; if (x != OrigJpegConstants.Markers.XFFInt) @@ -96,7 +97,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder return OrigDecoderErrorCode.NoError; } - if (this.BufferAsInt[this.I] != 0x00) + if (bufferSpan[this.I] != 0x00) { return OrigDecoderErrorCode.MissingFF00; } @@ -196,7 +197,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder } } - result = this.BufferAsInt[this.I]; + result = this.BufferAsInt.Span[this.I]; this.I++; this.UnreadableBytes = 0; return errorCode; @@ -230,20 +231,20 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder DecoderThrowHelper.ThrowImageFormatException.FillCalledWhenUnreadBytesExist(); } - Span bufferSpan = this.Buffer.Span; + Span byteSpan = this.Buffer.Span; // Move the last 2 bytes to the start of the buffer, in case we need // to call UnreadByteStuffedByte. if (this.J > 2) { - bufferSpan[0] = bufferSpan[this.J - 2]; - bufferSpan[1] = bufferSpan[this.J - 1]; + byteSpan[0] = byteSpan[this.J - 2]; + byteSpan[1] = byteSpan[this.J - 1]; this.I = 2; this.J = 2; } // Fill in the rest of the buffer. - int n = inputStream.Read(this.Buffer.Array, this.J, bufferSpan.Length - this.J); + int n = inputStream.Read(this.Buffer.Array, this.J, byteSpan.Length - this.J); if (n == 0) { return OrigDecoderErrorCode.UnexpectedEndOfStream; @@ -251,9 +252,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder this.J += n; - for (int i = 0; i < bufferSpan.Length; i++) + Span intSpan = this.BufferAsInt.Span; + for (int i = 0; i < byteSpan.Length; i++) { - this.BufferAsInt[i] = bufferSpan[i]; + intSpan[i] = byteSpan[i]; } return OrigDecoderErrorCode.NoError; diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs index 95631a7e66..b8694c538e 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs @@ -12,9 +12,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// internal struct PdfJsHuffmanTable : IDisposable { - private FakeBuffer lookahead; - private FakeBuffer valOffset; - private FakeBuffer maxcode; + private BasicArrayBuffer lookahead; + private BasicArrayBuffer valOffset; + private BasicArrayBuffer maxcode; private IManagedByteBuffer huffval; /// @@ -30,8 +30,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.valOffset = memoryManager.AllocateFake(18); this.maxcode = memoryManager.AllocateFake(18); - using (FakeBuffer huffsize = memoryManager.AllocateFake(257)) - using (FakeBuffer huffcode = memoryManager.AllocateFake(257)) + using (BasicArrayBuffer huffsize = memoryManager.AllocateFake(257)) + using (BasicArrayBuffer huffcode = memoryManager.AllocateFake(257)) { GenerateSizeTable(lengths, huffsize); GenerateCodeTable(huffsize, huffcode); diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsJpegPixelArea.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsJpegPixelArea.cs index ac26d892c1..a6f8780177 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsJpegPixelArea.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsJpegPixelArea.cs @@ -74,18 +74,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components var scale = new Vector2(this.imageWidth / (float)width, this.imageHeight / (float)height); this.componentData = this.memoryManager.Allocate(width * height * numberOfComponents); - Span componentDataSpan = this.componentData; + Span componentDataSpan = this.componentData.Span; const uint Mask3Lsb = 0xFFFFFFF8; // Used to clear the 3 LSBs - using (var xScaleBlockOffset = this.memoryManager.Allocate(width)) + using (IBuffer xScaleBlockOffset = this.memoryManager.Allocate(width)) { - Span xScaleBlockOffsetSpan = xScaleBlockOffset; + Span xScaleBlockOffsetSpan = xScaleBlockOffset.Span; for (int i = 0; i < numberOfComponents; i++) { ref PdfJsComponent component = ref components.Components[i]; Vector2 componentScale = component.Scale * scale; int offset = i; - Span output = component.Output; + Span output = component.Output.Span; int blocksPerScanline = (component.BlocksPerLine + 1) << 3; // Precalculate the xScaleBlockOffset diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs index e2e5d985e6..9e245ea2c6 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs @@ -707,6 +707,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] private void DecodeBaseline(PdfJsFrameComponent component, int offset, ref PdfJsHuffmanTable dcHuffmanTable, ref PdfJsHuffmanTable acHuffmanTable, Stream stream) { + Span blockDataSpan = component.BlockData.Span; + int t = this.DecodeHuffman(ref dcHuffmanTable, stream); if (this.endOfStreamReached || this.unexpectedMarkerReached) { @@ -714,7 +716,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } int diff = t == 0 ? 0 : this.ReceiveAndExtend(t, stream); - component.BlockData[offset] = (short)(component.Pred += diff); + blockDataSpan[offset] = (short)(component.Pred += diff); int k = 1; while (k < 64) @@ -748,7 +750,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components byte z = PdfJsQuantizationTables.DctZigZag[k]; short re = (short)this.ReceiveAndExtend(s, stream); - component.BlockData[offset + z] = re; + blockDataSpan[offset + z] = re; k++; } } @@ -756,6 +758,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] private void DecodeDCFirst(PdfJsFrameComponent component, int offset, ref PdfJsHuffmanTable dcHuffmanTable, Stream stream) { + Span blockDataSpan = component.BlockData.Span; + int t = this.DecodeHuffman(ref dcHuffmanTable, stream); if (this.endOfStreamReached || this.unexpectedMarkerReached) { @@ -763,19 +767,21 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } int diff = t == 0 ? 0 : this.ReceiveAndExtend(t, stream) << this.successiveState; - component.BlockData[offset] = (short)(component.Pred += diff); + blockDataSpan[offset] = (short)(component.Pred += diff); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void DecodeDCSuccessive(PdfJsFrameComponent component, int offset, Stream stream) { + Span blockDataSpan = component.BlockData.Span; + int bit = this.ReadBit(stream); if (this.endOfStreamReached || this.unexpectedMarkerReached) { return; } - component.BlockData[offset] |= (short)(bit << this.successiveState); + blockDataSpan[offset] |= (short)(bit << this.successiveState); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index f05a8a136d..6bcde2f487 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -790,14 +790,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort using (Buffer multiplicationBuffer = this.configuration.MemoryManager.Allocate(64, true)) { Span quantizationTable = this.quantizationTables.Tables.GetRowSpan(frameComponent.QuantizationTableIndex); - Span computationBufferSpan = computationBuffer; + Span computationBufferSpan = computationBuffer.Span; // For AA&N IDCT method, multiplier are equal to quantization // coefficients scaled by scalefactor[row]*scalefactor[col], where // scalefactor[0] = 1 // scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 // For integer operation, the multiplier table is to be scaled by 12. - Span multiplierSpan = multiplicationBuffer; + Span multiplierSpan = multiplicationBuffer.Span; // for (int i = 0; i < 64; i++) // { diff --git a/src/ImageSharp/Memory/FakeBuffer.cs b/src/ImageSharp/Memory/BasicArrayBuffer.cs similarity index 82% rename from src/ImageSharp/Memory/FakeBuffer.cs rename to src/ImageSharp/Memory/BasicArrayBuffer.cs index e4bc4e463c..d9eb5a19a1 100644 --- a/src/ImageSharp/Memory/FakeBuffer.cs +++ b/src/ImageSharp/Memory/BasicArrayBuffer.cs @@ -4,12 +4,12 @@ using System.Runtime.CompilerServices; namespace SixLabors.ImageSharp.Memory { /// - /// Temporal workaround providing a "Buffer" based on a generic array without the 'Unsafe.As()' hackery. + /// Exposes an array through the interface. /// - internal class FakeBuffer : IBuffer + internal class BasicArrayBuffer : IBuffer where T : struct { - public FakeBuffer(T[] array) + public BasicArrayBuffer(T[] array) { this.Array = array; } @@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.Memory /// /// The to convert. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator ReadOnlySpan(FakeBuffer buffer) + public static implicit operator ReadOnlySpan(BasicArrayBuffer buffer) { return new ReadOnlySpan(buffer.Array, 0, buffer.Length); } @@ -52,7 +52,7 @@ namespace SixLabors.ImageSharp.Memory /// /// The to convert. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Span(FakeBuffer buffer) + public static implicit operator Span(BasicArrayBuffer buffer) { return new Span(buffer.Array, 0, buffer.Length); } diff --git a/src/ImageSharp/Memory/Buffer{T}.cs b/src/ImageSharp/Memory/Buffer{T}.cs index 55eb44820a..309cca1f41 100644 --- a/src/ImageSharp/Memory/Buffer{T}.cs +++ b/src/ImageSharp/Memory/Buffer{T}.cs @@ -38,11 +38,6 @@ namespace SixLabors.ImageSharp.Memory this.memoryManager = memoryManager; } - /// - /// Gets a value indicating whether this instance is disposed, or has lost ownership of . - /// - public bool IsDisposedOrLostArrayOwnership { get; private set; } - /// /// Gets the count of "relevant" elements. It's usually smaller than 'Array.Length' when is pooled. /// @@ -51,44 +46,7 @@ namespace SixLabors.ImageSharp.Memory /// /// Gets a to the backing buffer. /// - public Span Span => this; - - /// - /// Returns a reference to specified element of the buffer. - /// - /// The index - /// The reference to the specified element - public ref T this[int index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - DebugGuard.MustBeLessThan(index, this.Length, nameof(index)); - - Span span = this.Span; - return ref span[index]; - } - } - - /// - /// Converts to an . - /// - /// The to convert. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator ReadOnlySpan(Buffer buffer) - { - return new ReadOnlySpan(buffer.array, 0, buffer.Length); - } - - /// - /// Converts to an . - /// - /// The to convert. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Span(Buffer buffer) - { - return new Span(buffer.array, 0, buffer.Length); - } + public Span Span => new Span(this.array, 0, this.Length); /// /// Disposes the instance by unpinning the array, and returning the pooled buffer when necessary. @@ -96,13 +54,11 @@ namespace SixLabors.ImageSharp.Memory [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() { - if (this.IsDisposedOrLostArrayOwnership) + if (this.array == null) { return; } - this.IsDisposedOrLostArrayOwnership = true; - this.memoryManager?.Release(this); this.memoryManager = null; @@ -112,27 +68,6 @@ namespace SixLabors.ImageSharp.Memory GC.SuppressFinalize(this); } - /// - /// Unpins and makes the object "quasi-disposed" so the array is no longer owned by this object. - /// If is rented, it's the callers responsibility to return it to it's pool. - /// - /// The unpinned - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T[] TakeArrayOwnership() - { - if (this.IsDisposedOrLostArrayOwnership) - { - throw new InvalidOperationException( - "TakeArrayOwnership() is invalid: either Buffer is disposed or TakeArrayOwnership() has been called multiple times!"); - } - - this.IsDisposedOrLostArrayOwnership = true; - T[] a = this.array; - this.array = null; - this.memoryManager = null; - return a; - } - /// /// TODO: Refactor this /// diff --git a/src/ImageSharp/Memory/MemoryManager.cs b/src/ImageSharp/Memory/MemoryManager.cs index 58f2458193..540c045de3 100644 --- a/src/ImageSharp/Memory/MemoryManager.cs +++ b/src/ImageSharp/Memory/MemoryManager.cs @@ -37,10 +37,10 @@ namespace SixLabors.ImageSharp.Memory /// Temporal workaround. A method providing a "Buffer" based on a generic array without the 'Unsafe.As()' hackery. /// Should be replaced with 'Allocate()' as soon as SixLabors.Shapes has Span-based API-s! /// - internal FakeBuffer AllocateFake(int length, bool dummy = false) + internal BasicArrayBuffer AllocateFake(int length, bool dummy = false) where T : struct { - return new FakeBuffer(new T[length]); + return new BasicArrayBuffer(new T[length]); } } } diff --git a/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs b/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs index 296ae1bb37..a42a2056c4 100644 --- a/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs @@ -71,13 +71,17 @@ namespace SixLabors.ImageSharp.Processing.Processors int width = maxX - minX; - using (var colors = this.memoryManager.Allocate(width)) - using (var amount = this.memoryManager.Allocate(width)) + using (Buffer colors = this.memoryManager.Allocate(width)) + using (Buffer amount = this.memoryManager.Allocate(width)) { + // Be careful! Do not capture colorSpan & amountSpan in the lambda below! + Span colorSpan = colors.Span; + Span amountSpan = amount.Span; + for (int i = 0; i < width; i++) { - colors[i] = this.Value; - amount[i] = this.options.BlendPercentage; + colorSpan[i] = this.Value; + amountSpan[i] = this.options.BlendPercentage; } PixelBlender blender = PixelOperations.Instance.GetPixelBlender(this.options.BlenderMode); @@ -90,7 +94,7 @@ namespace SixLabors.ImageSharp.Processing.Processors Span destination = source.GetPixelRowSpan(y - startY).Slice(minX - startX, width); // This switched color & destination in the 2nd and 3rd places because we are applying the target colour under the current one - blender.Blend(destination, colors, destination, amount); + blender.Blend(destination, colors.Span, destination, amount.Span); }); } } diff --git a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs index 6114c64387..85c592ceaa 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs @@ -65,7 +65,7 @@ namespace SixLabors.ImageSharp.Processing.Processors TPixel glowColor = this.GlowColor; Vector2 centre = Rectangle.Center(sourceRectangle); - var finalRadius = this.Radius.Calculate(source.Size()); + float finalRadius = this.Radius.Calculate(source.Size()); float maxDistance = finalRadius > 0 ? MathF.Min(finalRadius, sourceRectangle.Width * .5F) : sourceRectangle.Width * .5F; @@ -87,11 +87,14 @@ namespace SixLabors.ImageSharp.Processing.Processors } int width = maxX - minX; - using (var rowColors = this.memoryManager.Allocate(width)) + using (IBuffer rowColors = this.memoryManager.Allocate(width)) { + // Be careful! Do not capture rowColorsSpan in the lambda below! + Span rowColorsSpan = rowColors.Span; + for (int i = 0; i < width; i++) { - rowColors[i] = glowColor; + rowColorsSpan[i] = glowColor; } Parallel.For( @@ -100,19 +103,20 @@ namespace SixLabors.ImageSharp.Processing.Processors configuration.ParallelOptions, y => { - using (var amounts = this.memoryManager.Allocate(width)) + using (IBuffer amounts = this.memoryManager.Allocate(width)) { + Span amountsSpan = amounts.Span; int offsetY = y - startY; int offsetX = minX - startX; for (int i = 0; i < width; i++) { float distance = Vector2.Distance(centre, new Vector2(i + offsetX, offsetY)); - amounts[i] = (this.options.BlendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1); + amountsSpan[i] = (this.options.BlendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1); } Span destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width); - this.blender.Blend(destination, destination, rowColors, amounts); + this.blender.Blend(destination, destination, rowColors.Span, amountsSpan); } }); } diff --git a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs index 9877f4cc9d..d0943b27bb 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs @@ -86,8 +86,8 @@ namespace SixLabors.ImageSharp.Processing.Processors TPixel vignetteColor = this.VignetteColor; Vector2 centre = Rectangle.Center(sourceRectangle); - var finalradiusX = this.RadiusX.Calculate(source.Size()); - var finalradiusY = this.RadiusY.Calculate(source.Size()); + float finalradiusX = this.RadiusX.Calculate(source.Size()); + float finalradiusY = this.RadiusY.Calculate(source.Size()); float rX = finalradiusX > 0 ? MathF.Min(finalradiusX, sourceRectangle.Width * .5F) : sourceRectangle.Width * .5F; float rY = finalradiusY > 0 ? MathF.Min(finalradiusY, sourceRectangle.Height * .5F) : sourceRectangle.Height * .5F; float maxDistance = MathF.Sqrt((rX * rX) + (rY * rY)); @@ -110,11 +110,14 @@ namespace SixLabors.ImageSharp.Processing.Processors } int width = maxX - minX; - using (var rowColors = this.memoryManager.Allocate(width)) + using (IBuffer rowColors = this.memoryManager.Allocate(width)) { + // Be careful! Do not capture rowColorsSpan in the lambda below! + Span rowColorsSpan = rowColors.Span; + for (int i = 0; i < width; i++) { - rowColors[i] = vignetteColor; + rowColorsSpan[i] = vignetteColor; } Parallel.For( @@ -123,19 +126,20 @@ namespace SixLabors.ImageSharp.Processing.Processors configuration.ParallelOptions, y => { - using (var amounts = this.memoryManager.Allocate(width)) + using (IBuffer amounts = this.memoryManager.Allocate(width)) { + Span amountsSpan = amounts.Span; int offsetY = y - startY; int offsetX = minX - startX; for (int i = 0; i < width; i++) { float distance = Vector2.Distance(centre, new Vector2(i + offsetX, offsetY)); - amounts[i] = (this.options.BlendPercentage * (.9F * (distance / maxDistance))).Clamp(0, 1); + amountsSpan[i] = (this.options.BlendPercentage * (.9F * (distance / maxDistance))).Clamp(0, 1); } Span destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width); - this.blender.Blend(destination, destination, rowColors, amounts); + this.blender.Blend(destination, destination, rowColors.Span, amountsSpan); } }); } diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index 1fa388da48..1e76422508 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -135,18 +135,18 @@ namespace SixLabors.ImageSharp.Processing.Processors y => { // TODO: Without Parallel.For() this buffer object could be reused: - using (var tempRowBuffer = this.MemoryManager.Allocate(source.Width)) + using (IBuffer tempRowBuffer = this.MemoryManager.Allocate(source.Width)) { Span firstPassRow = firstPassPixels.GetRowSpan(y); Span sourceRow = source.GetPixelRowSpan(y); - PixelOperations.Instance.ToVector4(sourceRow, tempRowBuffer, sourceRow.Length); + PixelOperations.Instance.ToVector4(sourceRow, tempRowBuffer.Span, sourceRow.Length); if (this.Compand) { for (int x = minX; x < maxX; x++) { WeightsWindow window = this.HorizontalWeights.Weights[x - startX]; - firstPassRow[x] = window.ComputeExpandedWeightedRowSum(tempRowBuffer, sourceX); + firstPassRow[x] = window.ComputeExpandedWeightedRowSum(tempRowBuffer.Span, sourceX); } } else @@ -154,7 +154,7 @@ namespace SixLabors.ImageSharp.Processing.Processors for (int x = minX; x < maxX; x++) { WeightsWindow window = this.HorizontalWeights.Weights[x - startX]; - firstPassRow[x] = window.ComputeWeightedRowSum(tempRowBuffer, sourceX); + firstPassRow[x] = window.ComputeWeightedRowSum(tempRowBuffer.Span, sourceX); } } } diff --git a/src/ImageSharp/Quantizers/QuantizerBase{TPixel}.cs b/src/ImageSharp/Quantizers/QuantizerBase{TPixel}.cs index 31e424060b..0b41edf98d 100644 --- a/src/ImageSharp/Quantizers/QuantizerBase{TPixel}.cs +++ b/src/ImageSharp/Quantizers/QuantizerBase{TPixel}.cs @@ -115,6 +115,7 @@ namespace SixLabors.ImageSharp.Quantizers.Base /// /// Override this to process the pixel in the first pass of the algorithm + /// TODO: We really should do this on a per-row basis! Shouldn't we internalize this method? /// /// The pixel to quantize /// diff --git a/src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs b/src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs index b5d31014b2..d9f188bf30 100644 --- a/src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs +++ b/src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs @@ -36,6 +36,14 @@ namespace SixLabors.ImageSharp.Quantizers public class WuQuantizer : QuantizerBase where TPixel : struct, IPixel { + // TODO: The WuQuantizer code is rising several questions: + // - Do we really need to ALWAYS allocate the whole table of size TableLength? (~ 2471625 * sizeof(long) * 5 bytes ) + // - Isn't an AOS ("array of structures") layout more efficient & more readable than SOA ("structure of arrays") for this particular use case? + // (T, R, G, B, A, M2) could be grouped together! + // - There are per-pixel virtual calls in InitialQuantizePixel, why not do it on a per-row basis? + // - It's a frequently used class, we need tests! (So we can optimize safely.) There are tests in the original!!! We should just adopt them! + // https://github.com/JeremyAnsel/JeremyAnsel.ColorQuant/blob/master/JeremyAnsel.ColorQuant/JeremyAnsel.ColorQuant.Tests/WuColorQuantizerTests.cs + /// /// The index bits. /// @@ -69,37 +77,37 @@ namespace SixLabors.ImageSharp.Quantizers /// /// Moment of P(c). /// - private Buffer vwt; + private IBuffer vwt; /// /// Moment of r*P(c). /// - private Buffer vmr; + private IBuffer vmr; /// /// Moment of g*P(c). /// - private Buffer vmg; + private IBuffer vmg; /// /// Moment of b*P(c). /// - private Buffer vmb; + private IBuffer vmb; /// /// Moment of a*P(c). /// - private Buffer vma; + private IBuffer vma; /// /// Moment of c^2*P(c). /// - private Buffer m2; + private IBuffer m2; /// /// Color space tag. /// - private Buffer tag; + private IBuffer tag; /// /// Maximum allowed color depth @@ -153,23 +161,16 @@ namespace SixLabors.ImageSharp.Quantizers } finally { - this.DisposeBuffer(ref this.vwt); - this.DisposeBuffer(ref this.vmr); - this.DisposeBuffer(ref this.vmg); - this.DisposeBuffer(ref this.vmb); - this.DisposeBuffer(ref this.vma); - this.DisposeBuffer(ref this.m2); - this.DisposeBuffer(ref this.tag); + this.vwt.Dispose(); + this.vmr.Dispose(); + this.vmg.Dispose(); + this.vmb.Dispose(); + this.vma.Dispose(); + this.m2.Dispose(); + this.tag.Dispose(); } } - private void DisposeBuffer(ref Buffer buffer) - where T : struct - { - buffer?.Dispose(); - buffer = null; - } - /// protected override TPixel[] GetPalette() { @@ -213,14 +214,21 @@ namespace SixLabors.ImageSharp.Quantizers int index = GetPaletteIndex(r + 1, g + 1, b + 1, a + 1); - this.vwt[index]++; - this.vmr[index] += rgba.R; - this.vmg[index] += rgba.G; - this.vmb[index] += rgba.B; - this.vma[index] += rgba.A; + Span vwtSpan = this.vwt.Span; + Span vmrSpan = this.vmr.Span; + Span vmgSpan = this.vmg.Span; + Span vmbSpan = this.vmb.Span; + Span vmaSpan = this.vma.Span; + Span m2Span = this.m2.Span; + + vwtSpan[index]++; + vmrSpan[index] += rgba.R; + vmgSpan[index] += rgba.G; + vmbSpan[index] += rgba.B; + vmaSpan[index] += rgba.A; var vector = new Vector4(rgba.R, rgba.G, rgba.B, rgba.A); - this.m2[index] += Vector4.Dot(vector, vector); + m2Span[index] += Vector4.Dot(vector, vector); } /// @@ -458,6 +466,13 @@ namespace SixLabors.ImageSharp.Quantizers /// private void Get3DMoments(MemoryManager memoryManager) { + Span vwtSpan = this.vwt.Span; + Span vmrSpan = this.vmr.Span; + Span vmgSpan = this.vmg.Span; + Span vmbSpan = this.vmb.Span; + Span vmaSpan = this.vma.Span; + Span m2Span = this.m2.Span; + using (Buffer volume = memoryManager.Allocate(IndexCount * IndexAlphaCount)) using (Buffer volumeR = memoryManager.Allocate(IndexCount * IndexAlphaCount)) using (Buffer volumeG = memoryManager.Allocate(IndexCount * IndexAlphaCount)) @@ -472,6 +487,20 @@ namespace SixLabors.ImageSharp.Quantizers using (Buffer areaA = memoryManager.Allocate(IndexAlphaCount)) using (Buffer area2 = memoryManager.Allocate(IndexAlphaCount)) { + Span volumeSpan = volume.Span; + Span volumeRSpan = volumeR.Span; + Span volumeGSpan = volumeG.Span; + Span volumeBSpan = volumeB.Span; + Span volumeASpan = volumeA.Span; + Span volume2Span = volume2.Span; + + Span areaSpan = area.Span; + Span areaRSpan = areaR.Span; + Span areaGSpan = areaG.Span; + Span areaBSpan = areaB.Span; + Span areaASpan = areaA.Span; + Span area2Span = area2.Span; + for (int r = 1; r < IndexCount; r++) { volume.Clear(); @@ -503,37 +532,37 @@ namespace SixLabors.ImageSharp.Quantizers { int ind1 = GetPaletteIndex(r, g, b, a); - line += this.vwt[ind1]; - lineR += this.vmr[ind1]; - lineG += this.vmg[ind1]; - lineB += this.vmb[ind1]; - lineA += this.vma[ind1]; - line2 += this.m2[ind1]; + line += vwtSpan[ind1]; + lineR += vmrSpan[ind1]; + lineG += vmgSpan[ind1]; + lineB += vmbSpan[ind1]; + lineA += vmaSpan[ind1]; + line2 += m2Span[ind1]; - area[a] += line; - areaR[a] += lineR; - areaG[a] += lineG; - areaB[a] += lineB; - areaA[a] += lineA; - area2[a] += line2; + areaSpan[a] += line; + areaRSpan[a] += lineR; + areaGSpan[a] += lineG; + areaBSpan[a] += lineB; + areaASpan[a] += lineA; + area2Span[a] += line2; int inv = (b * IndexAlphaCount) + a; - volume[inv] += area[a]; - volumeR[inv] += areaR[a]; - volumeG[inv] += areaG[a]; - volumeB[inv] += areaB[a]; - volumeA[inv] += areaA[a]; - volume2[inv] += area2[a]; + volumeSpan[inv] += areaSpan[a]; + volumeRSpan[inv] += areaRSpan[a]; + volumeGSpan[inv] += areaGSpan[a]; + volumeBSpan[inv] += areaBSpan[a]; + volumeASpan[inv] += areaASpan[a]; + volume2Span[inv] += area2Span[a]; int ind2 = ind1 - GetPaletteIndex(1, 0, 0, 0); - this.vwt[ind1] = this.vwt[ind2] + volume[inv]; - this.vmr[ind1] = this.vmr[ind2] + volumeR[inv]; - this.vmg[ind1] = this.vmg[ind2] + volumeG[inv]; - this.vmb[ind1] = this.vmb[ind2] + volumeB[inv]; - this.vma[ind1] = this.vma[ind2] + volumeA[inv]; - this.m2[ind1] = this.m2[ind2] + volume2[inv]; + vwtSpan[ind1] = vwtSpan[ind2] + volumeSpan[inv]; + vmrSpan[ind1] = vmrSpan[ind2] + volumeRSpan[inv]; + vmgSpan[ind1] = vmgSpan[ind2] + volumeGSpan[inv]; + vmbSpan[ind1] = vmbSpan[ind2] + volumeBSpan[inv]; + vmaSpan[ind1] = vmaSpan[ind2] + volumeASpan[inv]; + m2Span[ind1] = m2Span[ind2] + volume2Span[inv]; } } } @@ -553,23 +582,25 @@ namespace SixLabors.ImageSharp.Quantizers float db = Volume(ref cube, this.vmb.Span); float da = Volume(ref cube, this.vma.Span); + Span m2Span = this.m2.Span; + float xx = - this.m2[GetPaletteIndex(cube.R1, cube.G1, cube.B1, cube.A1)] - - this.m2[GetPaletteIndex(cube.R1, cube.G1, cube.B1, cube.A0)] - - this.m2[GetPaletteIndex(cube.R1, cube.G1, cube.B0, cube.A1)] - + this.m2[GetPaletteIndex(cube.R1, cube.G1, cube.B0, cube.A0)] - - this.m2[GetPaletteIndex(cube.R1, cube.G0, cube.B1, cube.A1)] - + this.m2[GetPaletteIndex(cube.R1, cube.G0, cube.B1, cube.A0)] - + this.m2[GetPaletteIndex(cube.R1, cube.G0, cube.B0, cube.A1)] - - this.m2[GetPaletteIndex(cube.R1, cube.G0, cube.B0, cube.A0)] - - this.m2[GetPaletteIndex(cube.R0, cube.G1, cube.B1, cube.A1)] - + this.m2[GetPaletteIndex(cube.R0, cube.G1, cube.B1, cube.A0)] - + this.m2[GetPaletteIndex(cube.R0, cube.G1, cube.B0, cube.A1)] - - this.m2[GetPaletteIndex(cube.R0, cube.G1, cube.B0, cube.A0)] - + this.m2[GetPaletteIndex(cube.R0, cube.G0, cube.B1, cube.A1)] - - this.m2[GetPaletteIndex(cube.R0, cube.G0, cube.B1, cube.A0)] - - this.m2[GetPaletteIndex(cube.R0, cube.G0, cube.B0, cube.A1)] - + this.m2[GetPaletteIndex(cube.R0, cube.G0, cube.B0, cube.A0)]; + m2Span[GetPaletteIndex(cube.R1, cube.G1, cube.B1, cube.A1)] + - m2Span[GetPaletteIndex(cube.R1, cube.G1, cube.B1, cube.A0)] + - m2Span[GetPaletteIndex(cube.R1, cube.G1, cube.B0, cube.A1)] + + m2Span[GetPaletteIndex(cube.R1, cube.G1, cube.B0, cube.A0)] + - m2Span[GetPaletteIndex(cube.R1, cube.G0, cube.B1, cube.A1)] + + m2Span[GetPaletteIndex(cube.R1, cube.G0, cube.B1, cube.A0)] + + m2Span[GetPaletteIndex(cube.R1, cube.G0, cube.B0, cube.A1)] + - m2Span[GetPaletteIndex(cube.R1, cube.G0, cube.B0, cube.A0)] + - m2Span[GetPaletteIndex(cube.R0, cube.G1, cube.B1, cube.A1)] + + m2Span[GetPaletteIndex(cube.R0, cube.G1, cube.B1, cube.A0)] + + m2Span[GetPaletteIndex(cube.R0, cube.G1, cube.B0, cube.A1)] + - m2Span[GetPaletteIndex(cube.R0, cube.G1, cube.B0, cube.A0)] + + m2Span[GetPaletteIndex(cube.R0, cube.G0, cube.B1, cube.A1)] + - m2Span[GetPaletteIndex(cube.R0, cube.G0, cube.B1, cube.A0)] + - m2Span[GetPaletteIndex(cube.R0, cube.G0, cube.B0, cube.A1)] + + m2Span[GetPaletteIndex(cube.R0, cube.G0, cube.B0, cube.A0)]; var vector = new Vector4(dr, dg, db, da); return xx - (Vector4.Dot(vector, vector) / Volume(ref cube, this.vwt.Span)); @@ -742,6 +773,8 @@ namespace SixLabors.ImageSharp.Quantizers /// A label. private void Mark(ref Box cube, byte label) { + Span tagSpan = this.tag.Span; + for (int r = cube.R0 + 1; r <= cube.R1; r++) { for (int g = cube.G0 + 1; g <= cube.G1; g++) @@ -750,7 +783,7 @@ namespace SixLabors.ImageSharp.Quantizers { for (int a = cube.A0 + 1; a <= cube.A1; a++) { - this.tag[GetPaletteIndex(r, g, b, a)] = label; + tagSpan[GetPaletteIndex(r, g, b, a)] = label; } } } @@ -833,7 +866,9 @@ namespace SixLabors.ImageSharp.Quantizers int b = rgba.B >> (8 - IndexBits); int a = rgba.A >> (8 - IndexAlphaBits); - return this.tag[GetPaletteIndex(r + 1, g + 1, b + 1, a + 1)]; + Span tagSpan = this.tag.Span; + + return tagSpan[GetPaletteIndex(r + 1, g + 1, b + 1, a + 1)]; } } } \ No newline at end of file diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4.cs index 53a55e06e3..1b49c48ec5 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4.cs @@ -49,13 +49,13 @@ namespace SixLabors.ImageSharp.Benchmarks.Color.Bulk [Benchmark] public void CommonBulk() { - new PixelOperations().PackFromVector4(this.source, this.destination, this.Count); + new PixelOperations().PackFromVector4(this.source.Span, this.destination.Span, this.Count); } [Benchmark] public void OptimizedBulk() { - PixelOperations.Instance.PackFromVector4(this.source, this.destination, this.Count); + PixelOperations.Instance.PackFromVector4(this.source.Span, this.destination.Span, this.Count); } } diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs index fb2f03d743..33dbcd24ca 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs @@ -50,13 +50,13 @@ namespace SixLabors.ImageSharp.Benchmarks.Color.Bulk [Benchmark] public void CommonBulk() { - new PixelOperations().PackFromRgba32Bytes(this.source, this.destination, this.Count); + new PixelOperations().PackFromRgba32Bytes(this.source.Span, this.destination.Span, this.Count); } [Benchmark] public void OptimizedBulk() { - PixelOperations.Instance.PackFromRgba32Bytes(this.source, this.destination, this.Count); + PixelOperations.Instance.PackFromRgba32Bytes(this.source.Span, this.destination.Span, this.Count); } } diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs index cddf0f9a86..8a7fc52afd 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs @@ -49,13 +49,13 @@ namespace SixLabors.ImageSharp.Benchmarks.Color.Bulk [Benchmark] public void CommonBulk() { - new PixelOperations().ToVector4(this.source, this.destination, this.Count); + new PixelOperations().ToVector4(this.source.Span, this.destination.Span, this.Count); } [Benchmark] public void OptimizedBulk() { - PixelOperations.Instance.ToVector4(this.source, this.destination, this.Count); + PixelOperations.Instance.ToVector4(this.source.Span, this.destination.Span, this.Count); } } diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs index 6593a28ae3..45ccaa8299 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs @@ -55,13 +55,13 @@ namespace SixLabors.ImageSharp.Benchmarks.Color.Bulk [Benchmark] public void CommonBulk() { - new PixelOperations().ToRgb24Bytes(this.source, this.destination, this.Count); + new PixelOperations().ToRgb24Bytes(this.source.Span, this.destination.Span, this.Count); } [Benchmark] public void OptimizedBulk() { - PixelOperations.Instance.ToRgb24Bytes(this.source, this.destination, this.Count); + PixelOperations.Instance.ToRgb24Bytes(this.source.Span, this.destination.Span, this.Count); } } diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs index 58b80d5504..9912e987cf 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs @@ -58,13 +58,13 @@ namespace SixLabors.ImageSharp.Benchmarks.Color.Bulk [Benchmark] public void CommonBulk() { - new PixelOperations().ToRgba32Bytes(this.source, this.destination, this.Count); + new PixelOperations().ToRgba32Bytes(this.source.Span, this.destination.Span, this.Count); } [Benchmark] public void OptimizedBulk() { - PixelOperations.Instance.ToRgba32Bytes(this.source, this.destination, this.Count); + PixelOperations.Instance.ToRgba32Bytes(this.source.Span, this.destination.Span, this.Count); } } diff --git a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs index 4524d757cf..07734c6f55 100644 --- a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs +++ b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs @@ -59,21 +59,20 @@ namespace SixLabors.ImageSharp.Benchmarks { using (Image image = new Image(800, 800)) { - Buffer amounts = Configuration.Default.MemoryManager.Allocate(image.Width); - - for (int x = 0; x < image.Width; x++) - { - amounts[x] = 1; - } - using (PixelAccessor pixels = image.Lock()) + using (Buffer amounts = Configuration.Default.MemoryManager.Allocate(image.Width)) { - for (int y = 0; y < image.Height; y++) + amounts.Span.Fill(1); + + using (PixelAccessor pixels = image.Lock()) { - Span span = pixels.GetRowSpan(y); - BulkVectorConvert(span, span, span, amounts); + for (int y = 0; y < image.Height; y++) + { + Span span = pixels.GetRowSpan(y); + this.BulkVectorConvert(span, span, span, amounts.Span); + } } + return new CoreSize(image.Width, image.Height); } - return new CoreSize(image.Width, image.Height); } } @@ -82,21 +81,20 @@ namespace SixLabors.ImageSharp.Benchmarks { using (Image image = new Image(800, 800)) { - Buffer amounts = Configuration.Default.MemoryManager.Allocate(image.Width); - - for (int x = 0; x < image.Width; x++) + using (Buffer amounts = Configuration.Default.MemoryManager.Allocate(image.Width)) { - amounts[x] = 1; - } - using (PixelAccessor pixels = image.Lock()) - { - for (int y = 0; y < image.Height; y++) + amounts.Span.Fill(1); + using (PixelAccessor pixels = image.Lock()) { - Span span = pixels.GetRowSpan(y); - BulkPixelConvert(span, span, span, amounts); + for (int y = 0; y < image.Height; y++) + { + Span span = pixels.GetRowSpan(y); + this.BulkPixelConvert(span, span, span, amounts.Span); + } } + + return new CoreSize(image.Width, image.Height); } - return new CoreSize(image.Width, image.Height); } } } diff --git a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs index 2743924f24..b36b4841f2 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs @@ -106,10 +106,7 @@ namespace SixLabors.ImageSharp.Benchmarks using (Buffer rowColors = Configuration.Default.MemoryManager.Allocate(width)) using (PixelAccessor sourcePixels = source.Lock()) { - for (int i = 0; i < width; i++) - { - rowColors[i] = glowColor; - } + rowColors.Span.Fill(glowColor); Parallel.For( minY, diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index b5d4aaebe4..bf6b1f4ab2 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -289,7 +289,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg } // no need to dispose when buffer is not array owner - buffers[i] = new Buffer2D(new FakeBuffer(values), values.Length, 1); + buffers[i] = new Buffer2D(new BasicArrayBuffer(values), values.Length, 1); } return new JpegColorConverter.ComponentValues(buffers, 0); } diff --git a/tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs b/tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs index 2ea06d7248..6e01bf182d 100644 --- a/tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs +++ b/tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs @@ -37,7 +37,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats TestOperation( source, expected, - (s, d) => ImageSharp.Rgba32.PixelOperations.ToVector4SimdAligned(s, d, 64) + (s, d) => ImageSharp.Rgba32.PixelOperations.ToVector4SimdAligned(s, d.Span, 64) ); } @@ -57,7 +57,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats times, () => { - PixelOperations.Instance.ToVector4(source, dest, count); + PixelOperations.Instance.ToVector4(source.Span, dest.Span, count); }); } } @@ -116,7 +116,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats TestOperation( source, expected, - (s, d) => Operations.PackFromVector4(s, d, count) + (s, d) => Operations.PackFromVector4(s, d.Span, count) ); } @@ -141,7 +141,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats TestOperation( source, expected, - (s, d) => Operations.ToVector4(s, d, count) + (s, d) => Operations.ToVector4(s, d.Span, count) ); } @@ -163,7 +163,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats TestOperation( source, expected, - (s, d) => Operations.PackFromRgb24Bytes(s, d, count) + (s, d) => Operations.PackFromRgb24Bytes(s, d.Span, count) ); } @@ -187,7 +187,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats TestOperation( source, expected, - (s, d) => Operations.ToRgb24Bytes(s, d, count) + (s, d) => Operations.ToRgb24Bytes(s, d.Span, count) ); } @@ -208,7 +208,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats TestOperation( source, expected, - (s, d) => Operations.PackFromRgba32Bytes(s, d, count) + (s, d) => Operations.PackFromRgba32Bytes(s, d.Span, count) ); } @@ -233,7 +233,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats TestOperation( source, expected, - (s, d) => Operations.ToRgba32Bytes(s, d, count) + (s, d) => Operations.ToRgba32Bytes(s, d.Span, count) ); } @@ -254,7 +254,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats TestOperation( source, expected, - (s, d) => Operations.PackFromBgr24Bytes(s, d, count) + (s, d) => Operations.PackFromBgr24Bytes(s, d.Span, count) ); } @@ -278,7 +278,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats TestOperation( source, expected, - (s, d) => Operations.ToBgr24Bytes(s, d, count) + (s, d) => Operations.ToBgr24Bytes(s, d.Span, count) ); } @@ -299,7 +299,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats TestOperation( source, expected, - (s, d) => Operations.PackFromBgra32Bytes(s, d, count) + (s, d) => Operations.PackFromBgra32Bytes(s, d.Span, count) ); } @@ -324,7 +324,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats TestOperation( source, expected, - (s, d) => Operations.ToBgra32Bytes(s, d, count) + (s, d) => Operations.ToBgra32Bytes(s, d.Span, count) ); } @@ -336,10 +336,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats public TSource[] SourceBuffer { get; } public Buffer ActualDestBuffer { get; } public TDest[] ExpectedDestBuffer { get; } - - public Span Source => this.SourceBuffer; - public Span ActualDest => this.ActualDestBuffer; - + public TestBuffers(TSource[] source, TDest[] expectedDest) { this.SourceBuffer = source; @@ -386,7 +383,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats internal static void TestOperation( TSource[] source, TDest[] expected, - Action> action) + Action> action) where TSource : struct where TDest : struct { diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs index b8907e81e3..9dbfeaca91 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs @@ -19,13 +19,14 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs int length = source.Length; Guard.MustBeSizedAtLeast(dest, length, nameof(dest)); - using (var rgbaBuffer = Configuration.Default.MemoryManager.Allocate(length)) + using (Buffer rgbaBuffer = Configuration.Default.MemoryManager.Allocate(length)) { - PixelOperations.Instance.ToRgba32(source, rgbaBuffer, length); + Span rgbaSpan = rgbaBuffer.Span; + PixelOperations.Instance.ToRgba32(source, rgbaSpan, length); for (int i = 0; i < length; i++) { - ref Rgba32 s = ref rgbaBuffer[i]; + ref Rgba32 s = ref rgbaSpan[i]; ref Argb32 d = ref dest[i]; d.PackFromRgba32(s); @@ -39,13 +40,14 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs int length = source.Length; Guard.MustBeSizedAtLeast(dest, length, nameof(dest)); - using (var rgbaBuffer = Configuration.Default.MemoryManager.Allocate(length)) + using (Buffer rgbaBuffer = Configuration.Default.MemoryManager.Allocate(length)) { - PixelOperations.Instance.ToRgba32(source, rgbaBuffer, length); + Span rgbaSpan = rgbaBuffer.Span; + PixelOperations.Instance.ToRgba32(source, rgbaSpan, length); for (int i = 0; i < length; i++) { - ref Rgba32 s = ref rgbaBuffer[i]; + ref Rgba32 s = ref rgbaSpan[i]; ref TPixel d = ref dest[i]; d.PackFromRgba32(s); @@ -59,13 +61,14 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs int length = source.Length; Guard.MustBeSizedAtLeast(dest, length, nameof(dest)); - using (var rgbaBuffer = Configuration.Default.MemoryManager.Allocate(length)) + using (Buffer rgbBuffer = Configuration.Default.MemoryManager.Allocate(length)) { - PixelOperations.Instance.ToRgb24(source, rgbaBuffer, length); + Span rgbSpan = rgbBuffer.Span; + PixelOperations.Instance.ToRgb24(source, rgbSpan, length); for (int i = 0; i < length; i++) { - ref Rgb24 s = ref rgbaBuffer[i]; + ref Rgb24 s = ref rgbSpan[i]; ref TPixel d = ref dest[i]; var rgba = default(Rgba32); s.ToRgba32(ref rgba);