Browse Source

Fixing and optimizing decoder and encoder

qoi
LuisAlfredo92 3 years ago
parent
commit
383aa22322
No known key found for this signature in database GPG Key ID: 13A8436905993B8F
  1. 2
      .gitattributes
  2. 2
      shared-infrastructure
  3. 17
      src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs
  4. 2
      src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs

2
.gitattributes

@ -118,6 +118,7 @@
*.bmp filter=lfs diff=lfs merge=lfs -text *.bmp filter=lfs diff=lfs merge=lfs -text
*.gif filter=lfs diff=lfs merge=lfs -text *.gif filter=lfs diff=lfs merge=lfs -text
*.png 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 *.tif filter=lfs diff=lfs merge=lfs -text
*.tiff filter=lfs diff=lfs merge=lfs -text *.tiff filter=lfs diff=lfs merge=lfs -text
*.tga 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 *.pnm filter=lfs diff=lfs merge=lfs -text
*.wbmp filter=lfs diff=lfs merge=lfs -text *.wbmp filter=lfs diff=lfs merge=lfs -text
*.exr filter=lfs diff=lfs merge=lfs -text *.exr filter=lfs diff=lfs merge=lfs -text
*.qoi filter=lfs diff=lfs merge=lfs -text

2
shared-infrastructure

@ -1 +1 @@
Subproject commit 9a6cf00d9a3d482bb08211dd8309f4724a2735cb Subproject commit 353b9afe32a8000410312d17263407cd7bb82d19

17
src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Buffers;
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -24,11 +25,6 @@ internal class QoiDecoderCore : IImageDecoderInternals
/// </summary> /// </summary>
private readonly MemoryAllocator memoryAllocator; private readonly MemoryAllocator memoryAllocator;
/// <summary>
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded.
/// </summary>
private readonly bool skipMetadata;
/// <summary> /// <summary>
/// The QOI header. /// The QOI header.
/// </summary> /// </summary>
@ -38,7 +34,6 @@ internal class QoiDecoderCore : IImageDecoderInternals
{ {
this.Options = options; this.Options = options;
this.configuration = options.Configuration; this.configuration = options.Configuration;
this.skipMetadata = options.SkipMetadata;
this.memoryAllocator = this.configuration.MemoryAllocator; this.memoryAllocator = this.configuration.MemoryAllocator;
} }
@ -149,7 +144,9 @@ internal class QoiDecoderCore : IImageDecoderInternals
private void ProcessPixels<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels) private void ProcessPixels<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
Rgba32[] previouslySeenPixels = new Rgba32[64]; using IMemoryOwner<Rgba32> previouslySeenPixelsBuffer = this.memoryAllocator.Allocate<Rgba32>(64, AllocationOptions.Clean);
Span<Rgba32> previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan();
//Rgba32[] previouslySeenPixels = new Rgba32[64];
Rgba32 previousPixel = new(0, 0, 0, 255); Rgba32 previousPixel = new(0, 0, 0, 255);
// We save the pixel to avoid loosing the fully opaque black pixel // 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 i = 0; i < this.header.Height; i++)
{ {
for (int j = 0; j < this.header.Width; j++) Span<TPixel> row = pixels.DangerousGetRowSpan(i);
for (int j = 0; j < row.Length; j++)
{ {
Span<TPixel> row = pixels.DangerousGetRowSpan(i);
operationByte = (byte)stream.ReadByte(); operationByte = (byte)stream.ReadByte();
switch ((QoiChunk)operationByte) switch ((QoiChunk)operationByte)
{ {
@ -247,7 +244,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
pixel.FromRgba32(readPixel); pixel.FromRgba32(readPixel);
for (int k = -1; k < repetitions; k++, j++) for (int k = -1; k < repetitions; k++, j++)
{ {
if (j == this.header.Width) if (j == row.Length)
{ {
j = 0; j = 0;
i++; i++;

2
src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs

@ -61,7 +61,7 @@ public class QoiEncoderCore : IImageEncoderInternals
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
// Start image encoding // Start image encoding
using IMemoryOwner<Rgba32> previouslySeenPixelsBuffer = this.memoryAllocator.Allocate<Rgba32>(64); using IMemoryOwner<Rgba32> previouslySeenPixelsBuffer = this.memoryAllocator.Allocate<Rgba32>(64, AllocationOptions.Clean);
Span<Rgba32> previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan(); Span<Rgba32> previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan();
Rgba32 previousPixel = new(0, 0, 0, 255); Rgba32 previousPixel = new(0, 0, 0, 255);
Rgba32 currentRgba32 = default; Rgba32 currentRgba32 = default;

Loading…
Cancel
Save