Browse Source

Add DCT memory, make JxlImage3<T> implement IDisposable

pull/3153/head
winscripter 2 months ago
parent
commit
7223d98e66
  1. 12
      src/ImageSharp/Formats/Jxl/Memory/JxlImage3{T}.cs
  2. 68
      src/ImageSharp/Formats/Jxl/Processing/IJxlDctAcImage.cs
  3. 63
      src/ImageSharp/Formats/Jxl/Processing/JxlDctAcImage{T}.cs
  4. 20
      src/ImageSharp/Formats/Jxl/Processing/JxlDctAcPointer.cs
  5. 20
      src/ImageSharp/Formats/Jxl/Processing/JxlDctAcType.cs
  6. 24
      src/ImageSharp/Formats/Jxl/Processing/JxlDctReadOnlyAcPointer.cs

12
src/ImageSharp/Formats/Jxl/Memory/JxlImage3{T}.cs

@ -7,7 +7,7 @@ using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.Formats.Jxl.Memory;
// NOTE: Do not seal this class.
internal class JxlImage3<T>
internal class JxlImage3<T> : IDisposable
where T : unmanaged
{
private const int PlaneCount = 3;
@ -82,4 +82,14 @@ internal class JxlImage3<T>
[Conditional("DEBUG")]
private void PlaneRowBoundsCheck(int c, int y) =>
Debug.Assert(c < PlaneCount && y < this.YSize, "The bounds check has failed");
public void Dispose()
{
foreach (JxlPlane<T> plane in this.planes)
{
plane.Dispose();
}
GC.SuppressFinalize(this);
}
}

68
src/ImageSharp/Formats/Jxl/Processing/IJxlDctAcImage.cs

@ -0,0 +1,68 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp.Formats.Jxl.Processing;
/// <summary>
/// Base DCT AC coefficient image
/// </summary>
internal interface IJxlDctAcImage
{
/// <summary>
/// Gets the bit width of AC coefficients
/// </summary>
public JxlDctAcType Type { get; }
/// <summary>
/// Gets the number of pixels per row.
/// </summary>
public int PixelsPerRow { get; }
/// <summary>
/// Gets a value indicating whether the image is empty and doesn't
/// have anything within.
/// </summary>
public bool IsEmpty { get; }
/// <summary>
/// Returns a reference to the coefficients at the specified row.
/// </summary>
/// <param name="channel">The plane index (which channel).</param>
/// <param name="y">The row index within plane specified by <paramref name="channel"/>.</param>
/// <param name="xBase">The X offset at the specified row.</param>
/// <returns>
/// A reference to the coefficients inside the channel
/// specified by index <paramref name="channel"/>, at index of
/// the row specified by <paramref name="y"/>, with X offset
/// specified by <paramref name="xBase"/>.
/// </returns>
public JxlDctAcPointer GetPlaneRow(int channel, int y, int xBase = 0);
/// <summary>
/// Returns a reference to the coefficients at the specified row.
/// </summary>
/// <param name="channel">The plane index (which channel).</param>
/// <param name="y">The row index within plane specified by <paramref name="channel"/>.</param>
/// <param name="xBase">The X offset at the specified row.</param>
/// <returns>
/// A reference to the coefficients inside the channel
/// specified by index <paramref name="channel"/>, at index of
/// the row specified by <paramref name="y"/>, with X offset
/// specified by <paramref name="xBase"/>.
/// </returns>
/// <remarks>
/// This is a read-only kind of <see cref="GetPlaneRow(int, int, int)"/>.
/// </remarks>
public JxlDctReadOnlyAcPointer GetReadOnlyPlaneRow(int channel, int y, int xBase = 0);
/// <summary>
/// Fills all planes with zero.
/// </summary>
public void Clear();
/// <summary>
/// Fills everything within the specified plane with zero.
/// </summary>
/// <param name="plane">Desired index of the plane.</param>
public void Clear(int plane = 0);
}

63
src/ImageSharp/Formats/Jxl/Processing/JxlDctAcImage{T}.cs

@ -0,0 +1,63 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats.Jxl.Memory;
namespace SixLabors.ImageSharp.Formats.Jxl.Processing;
internal sealed class JxlDctAcImage<T> : IJxlDctAcImage, IDisposable
where T : unmanaged
{
private readonly JxlImage3<T> image;
public unsafe JxlDctAcImage(Configuration configuration, int width, int height)
{
DebugGuard.IsTrue(sizeof(T) is 2 or 4, "The type must be 2 or 4 bytes");
this.image = JxlImage3<T>.Create(configuration, width, height);
}
public unsafe JxlDctAcType Type => sizeof(T) == 4 ? JxlDctAcType.Ac32 : JxlDctAcType.Ac16;
public int PixelsPerRow => this.image.PixelsPerRow;
public bool IsEmpty => this.image.XSize == 0 || this.image.YSize == 0;
public void Clear() => JxlImageOperations.ClearImage(this.image);
public void Clear(int plane = 0) => JxlImageOperations.ClearImage(this.image);
public unsafe JxlDctAcPointer GetPlaneRow(int channel, int y, int xBase = 0)
{
if (sizeof(T) == 4)
{
Span<int> span = (this.image as JxlImage3<int>)!.PlaneRow(channel, y)[xBase..];
return new JxlDctAcPointer() { Pointer32 = span };
}
else
{
Span<short> span = (this.image as JxlImage3<short>)!.PlaneRow(channel, y)[xBase..];
return new JxlDctAcPointer() { Pointer16 = span };
}
}
public unsafe JxlDctReadOnlyAcPointer GetReadOnlyPlaneRow(int channel, int y, int xBase = 0)
{
if (sizeof(T) == 4)
{
ReadOnlySpan<int> span = (this.image as JxlImage3<int>)!.PlaneRow(channel, y)[xBase..];
return new JxlDctReadOnlyAcPointer(span);
}
else
{
ReadOnlySpan<short> span = (this.image as JxlImage3<short>)!.PlaneRow(channel, y)[xBase..];
return new JxlDctReadOnlyAcPointer(span);
}
}
public void Dispose()
{
this.image.Dispose();
GC.SuppressFinalize(this);
}
}

20
src/ImageSharp/Formats/Jxl/Processing/JxlDctAcPointer.cs

@ -0,0 +1,20 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp.Formats.Jxl.Processing;
/// <summary>
/// Pointer to DCT AC coefficients
/// </summary>
internal ref struct JxlDctAcPointer()
{
/// <summary>
/// 16-bit pointer (if any)
/// </summary>
public Span<short> Pointer16 = [];
/// <summary>
/// 32-bit pointer (if any)
/// </summary>
public Span<int> Pointer32 = [];
}

20
src/ImageSharp/Formats/Jxl/Processing/JxlDctAcType.cs

@ -0,0 +1,20 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp.Formats.Jxl.Processing;
/// <summary>
/// Bit size for DCT AC coefficient
/// </summary>
internal enum JxlDctAcType : byte
{
/// <summary>
/// 16-bit coefficient
/// </summary>
Ac16,
/// <summary>
/// 32-bit coefficient
/// </summary>
Ac32
}

24
src/ImageSharp/Formats/Jxl/Processing/JxlDctReadOnlyAcPointer.cs

@ -0,0 +1,24 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp.Formats.Jxl.Processing;
/// <summary>
/// Pointer to DCT AC coefficients. (Read-only)
/// </summary>
internal readonly ref struct JxlDctReadOnlyAcPointer
{
/// <summary>
/// 16-bit pointer (if any)
/// </summary>
public readonly ReadOnlySpan<short> Pointer16;
/// <summary>
/// 32-bit pointer (if any)
/// </summary>
public readonly ReadOnlySpan<int> Pointer32;
internal JxlDctReadOnlyAcPointer(ReadOnlySpan<short> pointer16) => this.Pointer16 = pointer16;
internal JxlDctReadOnlyAcPointer(ReadOnlySpan<int> pointer32) => this.Pointer32 = pointer32;
}
Loading…
Cancel
Save