From 383aa223222ac9a2034d990382d2605f17deb82a Mon Sep 17 00:00:00 2001 From: LuisAlfredo92 <92luisalfredo@protonmail.com> Date: Fri, 30 Jun 2023 11:42:05 -0600 Subject: [PATCH] Fixing and optimizing decoder and encoder --- .gitattributes | 2 +- shared-infrastructure | 2 +- src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs | 17 +++++++---------- src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs | 2 +- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/.gitattributes b/.gitattributes index 2f27bf2032..3647a7063d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -118,6 +118,7 @@ *.bmp filter=lfs diff=lfs merge=lfs -text *.gif filter=lfs diff=lfs merge=lfs -text *.png filter=lfs diff=lfs merge=lfs -text +*.qoi filter=lfs diff=lfs merge=lfs -text *.tif filter=lfs diff=lfs merge=lfs -text *.tiff filter=lfs diff=lfs merge=lfs -text *.tga filter=lfs diff=lfs merge=lfs -text @@ -132,4 +133,3 @@ *.pnm filter=lfs diff=lfs merge=lfs -text *.wbmp filter=lfs diff=lfs merge=lfs -text *.exr filter=lfs diff=lfs merge=lfs -text -*.qoi filter=lfs diff=lfs merge=lfs -text diff --git a/shared-infrastructure b/shared-infrastructure index 9a6cf00d9a..353b9afe32 160000 --- a/shared-infrastructure +++ b/shared-infrastructure @@ -1 +1 @@ -Subproject commit 9a6cf00d9a3d482bb08211dd8309f4724a2735cb +Subproject commit 353b9afe32a8000410312d17263407cd7bb82d19 diff --git a/src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs b/src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs index a3f8171b3d..564710f30b 100644 --- a/src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs +++ b/src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Buffers; using System.Buffers.Binary; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; @@ -24,11 +25,6 @@ internal class QoiDecoderCore : IImageDecoderInternals /// private readonly MemoryAllocator memoryAllocator; - /// - /// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded. - /// - private readonly bool skipMetadata; - /// /// The QOI header. /// @@ -38,7 +34,6 @@ internal class QoiDecoderCore : IImageDecoderInternals { this.Options = options; this.configuration = options.Configuration; - this.skipMetadata = options.SkipMetadata; this.memoryAllocator = this.configuration.MemoryAllocator; } @@ -149,7 +144,9 @@ internal class QoiDecoderCore : IImageDecoderInternals private void ProcessPixels(BufferedReadStream stream, Buffer2D pixels) where TPixel : unmanaged, IPixel { - Rgba32[] previouslySeenPixels = new Rgba32[64]; + using IMemoryOwner previouslySeenPixelsBuffer = this.memoryAllocator.Allocate(64, AllocationOptions.Clean); + Span previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan(); + //Rgba32[] previouslySeenPixels = new Rgba32[64]; Rgba32 previousPixel = new(0, 0, 0, 255); // We save the pixel to avoid loosing the fully opaque black pixel @@ -163,9 +160,9 @@ internal class QoiDecoderCore : IImageDecoderInternals for (int i = 0; i < this.header.Height; i++) { - for (int j = 0; j < this.header.Width; j++) + Span row = pixels.DangerousGetRowSpan(i); + for (int j = 0; j < row.Length; j++) { - Span row = pixels.DangerousGetRowSpan(i); operationByte = (byte)stream.ReadByte(); switch ((QoiChunk)operationByte) { @@ -247,7 +244,7 @@ internal class QoiDecoderCore : IImageDecoderInternals pixel.FromRgba32(readPixel); for (int k = -1; k < repetitions; k++, j++) { - if (j == this.header.Width) + if (j == row.Length) { j = 0; i++; diff --git a/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs b/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs index dd0aab1f7c..ae4de86e47 100644 --- a/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs +++ b/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs @@ -61,7 +61,7 @@ public class QoiEncoderCore : IImageEncoderInternals where TPixel : unmanaged, IPixel { // Start image encoding - using IMemoryOwner previouslySeenPixelsBuffer = this.memoryAllocator.Allocate(64); + using IMemoryOwner previouslySeenPixelsBuffer = this.memoryAllocator.Allocate(64, AllocationOptions.Clean); Span previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan(); Rgba32 previousPixel = new(0, 0, 0, 255); Rgba32 currentRgba32 = default;