From 8664ff9e6ca203a12c62e15b6c2471b3bf1b6fae Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 13 Oct 2022 10:03:59 +1000 Subject: [PATCH] Use PixelOperations for buffer copying. --- .../Decoder/SpectralConverter{TPixel}.cs | 19 +++++++++---------- .../Decompressors/JpegCompressionUtils.cs | 16 +++++++--------- .../Decompressors/JpegTiffCompression.cs | 9 +++++---- .../Decompressors/OldJpegTiffCompression.cs | 4 ++-- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 8240a74587..b5780bf531 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -21,11 +21,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; internal class SpectralConverter : SpectralConverter, IDisposable where TPixel : unmanaged, IPixel { - /// - /// instance associated with current - /// decoding routine. - /// - private readonly Configuration configuration; private JpegFrame frame; @@ -81,11 +76,15 @@ internal class SpectralConverter : SpectralConverter, IDisposable /// Optional target size for decoded image. public SpectralConverter(Configuration configuration, Size? targetSize = null) { - this.configuration = configuration; - + this.Configuration = configuration; this.targetSize = targetSize; } + /// + /// Gets the configuration instance associated with current decoding routine. + /// + public Configuration Configuration { get; } + /// /// Gets converted pixel buffer. /// @@ -177,7 +176,7 @@ internal class SpectralConverter : SpectralConverter, IDisposable { DebugGuard.IsTrue(this.colorConverter == null, "SpectralConverter.PrepareForDecoding() must be called once."); - MemoryAllocator allocator = this.configuration.MemoryAllocator; + MemoryAllocator allocator = this.Configuration.MemoryAllocator; // color converter from RGB to TPixel JpegColorConverterBase converter = this.GetColorConverter(this.frame, this.jpegData); @@ -196,7 +195,7 @@ internal class SpectralConverter : SpectralConverter, IDisposable this.pixelBuffer = allocator.Allocate2D( pixelSize.Width, pixelSize.Height, - this.configuration.PreferContiguousImageBuffers); + this.Configuration.PreferContiguousImageBuffers); this.paddedProxyPixelRow = allocator.Allocate(pixelSize.Width + 3); // component processors from spectral to RGB @@ -225,7 +224,7 @@ internal class SpectralConverter : SpectralConverter, IDisposable protected ComponentProcessor[] CreateComponentProcessors(JpegFrame frame, IRawJpegData jpegData, int blockPixelSize, Size processorBufferSize) { - MemoryAllocator allocator = this.configuration.MemoryAllocator; + MemoryAllocator allocator = this.Configuration.MemoryAllocator; var componentProcessors = new ComponentProcessor[frame.Components.Length]; for (int i = 0; i < componentProcessors.Length; i++) { diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegCompressionUtils.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegCompressionUtils.cs index a52c7e5293..4577a2492a 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegCompressionUtils.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegCompressionUtils.cs @@ -1,7 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -9,27 +9,25 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors; internal static class JpegCompressionUtils { - public static void CopyImageBytesToBuffer(Span buffer, Buffer2D pixelBuffer) + public static void CopyImageBytesToBuffer(Configuration configuration, Span buffer, Buffer2D pixelBuffer) { int offset = 0; for (int y = 0; y < pixelBuffer.Height; y++) { Span pixelRowSpan = pixelBuffer.DangerousGetRowSpan(y); - Span rgbBytes = MemoryMarshal.AsBytes(pixelRowSpan); - rgbBytes.CopyTo(buffer[offset..]); - offset += rgbBytes.Length; + PixelOperations.Instance.ToRgb24Bytes(configuration, pixelRowSpan, buffer[offset..], pixelRowSpan.Length); + offset += Unsafe.SizeOf() * pixelRowSpan.Length; } } - public static void CopyImageBytesToBuffer(Span buffer, Buffer2D pixelBuffer) + public static void CopyImageBytesToBuffer(Configuration configuration, Span buffer, Buffer2D pixelBuffer) { int offset = 0; for (int y = 0; y < pixelBuffer.Height; y++) { Span pixelRowSpan = pixelBuffer.DangerousGetRowSpan(y); - Span rgbBytes = MemoryMarshal.AsBytes(pixelRowSpan); - rgbBytes.CopyTo(buffer[offset..]); - offset += rgbBytes.Length; + PixelOperations.Instance.ToL8Bytes(configuration, pixelRowSpan, buffer[offset..], pixelRowSpan.Length); + offset += Unsafe.SizeOf() * pixelRowSpan.Length; } } } diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs index 818bb3d6dc..2cbe3eaec9 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; using SixLabors.ImageSharp.Formats.Tiff.Constants; @@ -73,8 +74,8 @@ internal sealed class JpegTiffCompression : TiffBaseDecompressor } else { - using Image image = Image.Load(stream); - JpegCompressionUtils.CopyImageBytesToBuffer(buffer, image.Frames.RootFrame.PixelBuffer); + using Image image = Image.Load(this.options.GeneralOptions, stream); + JpegCompressionUtils.CopyImageBytesToBuffer(this.options.GeneralOptions.Configuration, buffer, image.Frames.RootFrame.PixelBuffer); } } @@ -94,7 +95,7 @@ internal sealed class JpegTiffCompression : TiffBaseDecompressor jpegDecoder.ParseStream(stream, spectralConverterGray, cancellationToken); using Buffer2D decompressedBuffer = spectralConverterGray.GetPixelBuffer(cancellationToken); - JpegCompressionUtils.CopyImageBytesToBuffer(buffer, decompressedBuffer); + JpegCompressionUtils.CopyImageBytesToBuffer(spectralConverterGray.Configuration, buffer, decompressedBuffer); break; } @@ -108,7 +109,7 @@ internal sealed class JpegTiffCompression : TiffBaseDecompressor jpegDecoder.ParseStream(stream, spectralConverter, cancellationToken); using Buffer2D decompressedBuffer = spectralConverter.GetPixelBuffer(cancellationToken); - JpegCompressionUtils.CopyImageBytesToBuffer(buffer, decompressedBuffer); + JpegCompressionUtils.CopyImageBytesToBuffer(spectralConverter.Configuration, buffer, decompressedBuffer); break; } diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/OldJpegTiffCompression.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/OldJpegTiffCompression.cs index e57035a5a6..978c93cb05 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/OldJpegTiffCompression.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/OldJpegTiffCompression.cs @@ -58,7 +58,7 @@ internal sealed class OldJpegTiffCompression : TiffBaseDecompressor jpegDecoder.ParseStream(stream, spectralConverterGray, cancellationToken); using Buffer2D decompressedBuffer = spectralConverterGray.GetPixelBuffer(cancellationToken); - JpegCompressionUtils.CopyImageBytesToBuffer(buffer, decompressedBuffer); + JpegCompressionUtils.CopyImageBytesToBuffer(spectralConverterGray.Configuration, buffer, decompressedBuffer); break; } @@ -70,7 +70,7 @@ internal sealed class OldJpegTiffCompression : TiffBaseDecompressor jpegDecoder.ParseStream(stream, spectralConverter, cancellationToken); using Buffer2D decompressedBuffer = spectralConverter.GetPixelBuffer(cancellationToken); - JpegCompressionUtils.CopyImageBytesToBuffer(buffer, decompressedBuffer); + JpegCompressionUtils.CopyImageBytesToBuffer(spectralConverter.Configuration, buffer, decompressedBuffer); break; }