diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsJpegPixelArea.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsJpegPixelArea.cs index f16fb9a2c2..9bbac6129b 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsJpegPixelArea.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsJpegPixelArea.cs @@ -5,6 +5,7 @@ using System; using System.Diagnostics; using System.Numerics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components @@ -16,14 +17,25 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { private readonly MemoryManager memoryManager; - private readonly int imageWidth; - - private readonly int imageHeight; - private IBuffer componentData; private int rowStride; + /// + /// Gets the number of components + /// + public int NumberOfComponents; + + /// + /// Gets the width + /// + public int Width; + + /// + /// Gets the height + /// + public int Height; + /// /// Initializes a new instance of the struct. /// @@ -34,77 +46,52 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components public PdfJsJpegPixelArea(MemoryManager memoryManager, int imageWidth, int imageHeight, int numberOfComponents) { this.memoryManager = memoryManager; - this.imageWidth = imageWidth; - this.imageHeight = imageHeight; - this.Width = 0; - this.Height = 0; + this.Width = imageWidth; + this.Height = imageHeight; this.NumberOfComponents = numberOfComponents; this.componentData = null; - this.rowStride = 0; + this.rowStride = this.Width * this.NumberOfComponents; + this.componentData = this.memoryManager.Allocate(this.Width * this.Height * this.NumberOfComponents); } - /// - /// Gets the number of components - /// - public int NumberOfComponents { get; } - - /// - /// Gets the width - /// - public int Width { get; private set; } - - /// - /// Gets the height - /// - public int Height { get; private set; } - /// /// Organsizes the decoded jpeg components into a linear array ordered by component. /// This must be called before attempting to retrieve the data. /// /// The jpeg component blocks - /// The pixel area width - /// The pixel area height - public void LinearizeBlockData(PdfJsComponentBlocks components, int width, int height) + public void LinearizeBlockData(PdfJsComponentBlocks components) { - this.Width = width; - this.Height = height; - int numberOfComponents = this.NumberOfComponents; - this.rowStride = width * numberOfComponents; - 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; + ref byte componentDataRef = ref MemoryMarshal.GetReference(this.componentData.Span); const uint Mask3Lsb = 0xFFFFFFF8; // Used to clear the 3 LSBs - using (IBuffer xScaleBlockOffset = this.memoryManager.Allocate(width)) + using (IBuffer xScaleBlockOffset = this.memoryManager.Allocate(this.Width)) { - Span xScaleBlockOffsetSpan = xScaleBlockOffset.Span; - for (int i = 0; i < numberOfComponents; i++) + ref int xScaleBlockOffsetRef = ref MemoryMarshal.GetReference(xScaleBlockOffset.Span); + for (int i = 0; i < this.NumberOfComponents; i++) { ref PdfJsComponent component = ref components.Components[i]; - Vector2 componentScale = component.Scale * scale; - int offset = i; - Span output = component.Output.Span; + ref short outputRef = ref MemoryMarshal.GetReference(component.Output.Span); + Vector2 componentScale = component.Scale; int blocksPerScanline = (component.BlocksPerLine + 1) << 3; // Precalculate the xScaleBlockOffset int j; - for (int x = 0; x < width; x++) + for (int x = 0; x < this.Width; x++) { j = (int)(x * componentScale.X); - xScaleBlockOffsetSpan[x] = (int)((j & Mask3Lsb) << 3) | (j & 7); + Unsafe.Add(ref xScaleBlockOffsetRef, x) = (int)((j & Mask3Lsb) << 3) | (j & 7); } // Linearize the blocks of the component - for (int y = 0; y < height; y++) + int offset = i; + for (int y = 0; y < this.Height; y++) { j = (int)(y * componentScale.Y); int index = blocksPerScanline * (int)(j & Mask3Lsb) | ((j & 7) << 3); - for (int x = 0; x < width; x++) + for (int x = 0; x < this.Width; x++) { - componentDataSpan[offset] = (byte)output[index + xScaleBlockOffsetSpan[x]]; - offset += numberOfComponents; + Unsafe.Add(ref componentDataRef, offset) = (byte)Unsafe.Add(ref outputRef, index + Unsafe.Add(ref xScaleBlockOffsetRef, x)); + offset += this.NumberOfComponents; } } } diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index aa9a9a6b0b..b50d726ec5 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -5,6 +5,7 @@ using System; using System.Buffers.Binary; using System.IO; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats.Jpeg.Common.Decoder; using SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components; @@ -336,7 +337,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort } this.pixelArea = new PdfJsJpegPixelArea(this.configuration.MemoryManager, image.Width, image.Height, this.NumberOfComponents); - this.pixelArea.LinearizeBlockData(this.components, image.Width, image.Height); + this.pixelArea.LinearizeBlockData(this.components); if (this.NumberOfComponents == 1) { @@ -838,13 +839,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort { for (int y = 0; y < image.Height; y++) { - Span imageRowSpan = image.GetPixelRowSpan(y); - Span areaRowSpan = this.pixelArea.GetRowSpan(y); + ref TPixel imageRowRef = ref MemoryMarshal.GetReference(image.GetPixelRowSpan(y)); + ref byte areaRowRef = ref MemoryMarshal.GetReference(this.pixelArea.GetRowSpan(y)); for (int x = 0; x < image.Width; x++) { - ref byte luminance = ref areaRowSpan[x]; - ref TPixel pixel = ref imageRowSpan[x]; + ref byte luminance = ref Unsafe.Add(ref areaRowRef, x); + ref TPixel pixel = ref Unsafe.Add(ref imageRowRef, x); var rgba = new Rgba32(luminance, luminance, luminance); pixel.PackFromRgba32(rgba); } @@ -857,14 +858,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort { for (int y = 0; y < image.Height; y++) { - Span imageRowSpan = image.GetPixelRowSpan(y); - Span areaRowSpan = this.pixelArea.GetRowSpan(y); + ref TPixel imageRowRef = ref MemoryMarshal.GetReference(image.GetPixelRowSpan(y)); + ref byte areaRowRef = ref MemoryMarshal.GetReference(this.pixelArea.GetRowSpan(y)); + for (int x = 0, o = 0; x < image.Width; x++, o += 3) { - ref byte yy = ref areaRowSpan[o]; - ref byte cb = ref areaRowSpan[o + 1]; - ref byte cr = ref areaRowSpan[o + 2]; - ref TPixel pixel = ref imageRowSpan[x]; + ref byte yy = ref Unsafe.Add(ref areaRowRef, o); + ref byte cb = ref Unsafe.Add(ref areaRowRef, o + 1); + ref byte cr = ref Unsafe.Add(ref areaRowRef, o + 2); + ref TPixel pixel = ref Unsafe.Add(ref imageRowRef, x); PdfJsYCbCrToRgbTables.PackYCbCr(ref pixel, yy, cb, cr); } } @@ -876,16 +878,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort { for (int y = 0; y < image.Height; y++) { - Span imageRowSpan = image.GetPixelRowSpan(y); - Span areaRowSpan = this.pixelArea.GetRowSpan(y); + ref TPixel imageRowRef = ref MemoryMarshal.GetReference(image.GetPixelRowSpan(y)); + ref byte areaRowRef = ref MemoryMarshal.GetReference(this.pixelArea.GetRowSpan(y)); + for (int x = 0, o = 0; x < image.Width; x++, o += 4) { - ref byte yy = ref areaRowSpan[o]; - ref byte cb = ref areaRowSpan[o + 1]; - ref byte cr = ref areaRowSpan[o + 2]; - ref byte k = ref areaRowSpan[o + 3]; + ref byte yy = ref Unsafe.Add(ref areaRowRef, o); + ref byte cb = ref Unsafe.Add(ref areaRowRef, o + 1); + ref byte cr = ref Unsafe.Add(ref areaRowRef, o + 2); + ref byte k = ref Unsafe.Add(ref areaRowRef, o + 3); - ref TPixel pixel = ref imageRowSpan[x]; + ref TPixel pixel = ref Unsafe.Add(ref imageRowRef, x); PdfJsYCbCrToRgbTables.PackYccK(ref pixel, yy, cb, cr, k); } } @@ -897,20 +900,21 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort { for (int y = 0; y < image.Height; y++) { - Span imageRowSpan = image.GetPixelRowSpan(y); - Span areaRowSpan = this.pixelArea.GetRowSpan(y); + ref TPixel imageRowRef = ref MemoryMarshal.GetReference(image.GetPixelRowSpan(y)); + ref byte areaRowRef = ref MemoryMarshal.GetReference(this.pixelArea.GetRowSpan(y)); + for (int x = 0, o = 0; x < image.Width; x++, o += 4) { - ref byte c = ref areaRowSpan[o]; - ref byte m = ref areaRowSpan[o + 1]; - ref byte cy = ref areaRowSpan[o + 2]; - ref byte k = ref areaRowSpan[o + 3]; + ref byte c = ref Unsafe.Add(ref areaRowRef, o); + ref byte m = ref Unsafe.Add(ref areaRowRef, o + 1); + ref byte cy = ref Unsafe.Add(ref areaRowRef, o + 2); + ref byte k = ref Unsafe.Add(ref areaRowRef, o + 3); byte r = (byte)((c * k) / 255); byte g = (byte)((m * k) / 255); byte b = (byte)((cy * k) / 255); - ref TPixel pixel = ref imageRowSpan[x]; + ref TPixel pixel = ref Unsafe.Add(ref imageRowRef, x); var rgba = new Rgba32(r, g, b); pixel.PackFromRgba32(rgba); }