Skip to content

Commit 88de0a6

Browse files
committed
Fixing encoder optimizations
1 parent 02212bd commit 88de0a6

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,15 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
7272
/// <inheritdoc />
7373
public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
7474
{
75-
ImageMetadata metadata = new();
76-
7775
this.ProcessHeader(stream);
7876
PixelTypeInfo pixelType = new(8 * (int)this.header.Channels);
7977
Size size = new((int)this.header.Width, (int)this.header.Height);
8078

79+
ImageMetadata metadata = new();
80+
QoiMetadata qoiMetadata = metadata.GetQoiMetadata();
81+
qoiMetadata.Channels = this.header.Channels;
82+
qoiMetadata.ColorSpace = this.header.ColorSpace;
83+
8184
return new ImageInfo(pixelType, size, metadata);
8285
}
8386

src/ImageSharp/Formats/Qoi/QoiEncoder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Six Labors Split License.
33

44
using System.Text;
5+
using SixLabors.ImageSharp.Advanced;
56

67
namespace SixLabors.ImageSharp.Formats.Qoi;
78

@@ -27,7 +28,7 @@ public class QoiEncoder : ImageEncoder
2728
/// <inheritdoc />
2829
protected override void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
2930
{
30-
QoiEncoderCore encoder = new(this);
31+
QoiEncoderCore encoder = new(this, image.GetMemoryAllocator());
3132
encoder.Encode(image, stream, cancellationToken);
3233
}
3334
}

src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs

Lines changed: 13 additions & 5 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.Runtime.CompilerServices;
67
using SixLabors.ImageSharp.Memory;
@@ -14,10 +15,16 @@ namespace SixLabors.ImageSharp.Formats.Qoi;
1415
public class QoiEncoderCore : IImageEncoderInternals
1516
{
1617
private readonly QoiEncoder encoder;
18+
private readonly MemoryAllocator memoryAllocator;
19+
1720
/// <summary>
1821
/// Initializes a new instance of the <see cref="QoiEncoderCore"/> class.
1922
/// </summary>
20-
public QoiEncoderCore(QoiEncoder encoder) => this.encoder = encoder;
23+
public QoiEncoderCore(QoiEncoder encoder, MemoryAllocator memoryAllocator)
24+
{
25+
this.encoder = encoder;
26+
this.memoryAllocator = memoryAllocator;
27+
}
2128

2229
/// <inheritdoc />
2330
public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
@@ -27,7 +34,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
2734
Guard.NotNull(stream, nameof(stream));
2835

2936
this.WriteHeader(image, stream);
30-
WritePixels(image, stream);
37+
this.WritePixels(image, stream);
3138
WriteEndOfStream(stream);
3239
stream.Flush();
3340
}
@@ -50,21 +57,22 @@ private void WriteHeader(Image image, Stream stream)
5057
stream.WriteByte((byte)qoiColorSpace);
5158
}
5259

53-
private static void WritePixels<TPixel>(Image<TPixel> image, Stream stream)
60+
private void WritePixels<TPixel>(Image<TPixel> image, Stream stream)
5461
where TPixel : unmanaged, IPixel<TPixel>
5562
{
5663
// Start image encoding
57-
Rgba32[] previouslySeenPixels = new Rgba32[64];
64+
using IMemoryOwner<Rgba32> previouslySeenPixelsBuffer = this.memoryAllocator.Allocate<Rgba32>(64);
65+
Span<Rgba32> previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan();
5866
Rgba32 previousPixel = new(0, 0, 0, 255);
5967
Rgba32 currentRgba32 = default;
6068
Buffer2D<TPixel> pixels = image.Frames[0].PixelBuffer;
6169

6270
for (int i = 0; i < pixels.Height; i++)
6371
{
72+
Span<TPixel> row = pixels.DangerousGetRowSpan(i);
6473
for (int j = 0; j < pixels.Width && i < pixels.Height; j++)
6574
{
6675
// We get the RGBA value from pixels
67-
Span<TPixel> row = pixels.DangerousGetRowSpan(i);
6876
TPixel currentPixel = pixels[j, i];
6977
currentPixel.ToRgba32(ref currentRgba32);
7078

0 commit comments

Comments
 (0)