Skip to content

Commit 383aa22

Browse files
committed
Fixing and optimizing decoder and encoder
1 parent 88de0a6 commit 383aa22

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
*.bmp filter=lfs diff=lfs merge=lfs -text
119119
*.gif filter=lfs diff=lfs merge=lfs -text
120120
*.png filter=lfs diff=lfs merge=lfs -text
121+
*.qoi filter=lfs diff=lfs merge=lfs -text
121122
*.tif filter=lfs diff=lfs merge=lfs -text
122123
*.tiff filter=lfs diff=lfs merge=lfs -text
123124
*.tga filter=lfs diff=lfs merge=lfs -text
@@ -132,4 +133,3 @@
132133
*.pnm filter=lfs diff=lfs merge=lfs -text
133134
*.wbmp filter=lfs diff=lfs merge=lfs -text
134135
*.exr filter=lfs diff=lfs merge=lfs -text
135-
*.qoi filter=lfs diff=lfs merge=lfs -text

src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Buffers;
45
using System.Buffers.Binary;
56
using System.Diagnostics.CodeAnalysis;
67
using System.Runtime.CompilerServices;
@@ -24,11 +25,6 @@ internal class QoiDecoderCore : IImageDecoderInternals
2425
/// </summary>
2526
private readonly MemoryAllocator memoryAllocator;
2627

27-
/// <summary>
28-
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded.
29-
/// </summary>
30-
private readonly bool skipMetadata;
31-
3228
/// <summary>
3329
/// The QOI header.
3430
/// </summary>
@@ -38,7 +34,6 @@ public QoiDecoderCore(DecoderOptions options)
3834
{
3935
this.Options = options;
4036
this.configuration = options.Configuration;
41-
this.skipMetadata = options.SkipMetadata;
4237
this.memoryAllocator = this.configuration.MemoryAllocator;
4338
}
4439

@@ -149,7 +144,9 @@ private static void ThrowInvalidImageContentException()
149144
private void ProcessPixels<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels)
150145
where TPixel : unmanaged, IPixel<TPixel>
151146
{
152-
Rgba32[] previouslySeenPixels = new Rgba32[64];
147+
using IMemoryOwner<Rgba32> previouslySeenPixelsBuffer = this.memoryAllocator.Allocate<Rgba32>(64, AllocationOptions.Clean);
148+
Span<Rgba32> previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan();
149+
//Rgba32[] previouslySeenPixels = new Rgba32[64];
153150
Rgba32 previousPixel = new(0, 0, 0, 255);
154151

155152
// We save the pixel to avoid loosing the fully opaque black pixel
@@ -163,9 +160,9 @@ private void ProcessPixels<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> p
163160

164161
for (int i = 0; i < this.header.Height; i++)
165162
{
166-
for (int j = 0; j < this.header.Width; j++)
163+
Span<TPixel> row = pixels.DangerousGetRowSpan(i);
164+
for (int j = 0; j < row.Length; j++)
167165
{
168-
Span<TPixel> row = pixels.DangerousGetRowSpan(i);
169166
operationByte = (byte)stream.ReadByte();
170167
switch ((QoiChunk)operationByte)
171168
{
@@ -247,7 +244,7 @@ private void ProcessPixels<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> p
247244
pixel.FromRgba32(readPixel);
248245
for (int k = -1; k < repetitions; k++, j++)
249246
{
250-
if (j == this.header.Width)
247+
if (j == row.Length)
251248
{
252249
j = 0;
253250
i++;

src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private void WritePixels<TPixel>(Image<TPixel> image, Stream stream)
6161
where TPixel : unmanaged, IPixel<TPixel>
6262
{
6363
// Start image encoding
64-
using IMemoryOwner<Rgba32> previouslySeenPixelsBuffer = this.memoryAllocator.Allocate<Rgba32>(64);
64+
using IMemoryOwner<Rgba32> previouslySeenPixelsBuffer = this.memoryAllocator.Allocate<Rgba32>(64, AllocationOptions.Clean);
6565
Span<Rgba32> previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan();
6666
Rgba32 previousPixel = new(0, 0, 0, 255);
6767
Rgba32 currentRgba32 = default;

0 commit comments

Comments
 (0)