Browse Source

Move rowsPerBlock to base compression class

pull/3124/head
Brian Popow 2 months ago
parent
commit
2bf2e95638
  1. 5
      src/ImageSharp/Formats/Exr/Compression/Compressors/NoneExrCompressor.cs
  2. 5
      src/ImageSharp/Formats/Exr/Compression/Compressors/ZipExrCompressor.cs
  3. 19
      src/ImageSharp/Formats/Exr/Compression/Decompressors/B44ExrCompression.cs
  4. 5
      src/ImageSharp/Formats/Exr/Compression/Decompressors/NoneExrCompression.cs
  5. 9
      src/ImageSharp/Formats/Exr/Compression/Decompressors/Pxr24Compression.cs
  6. 4
      src/ImageSharp/Formats/Exr/Compression/Decompressors/RunLengthExrCompression.cs
  7. 7
      src/ImageSharp/Formats/Exr/Compression/Decompressors/ZipExrCompression.cs
  8. 9
      src/ImageSharp/Formats/Exr/Compression/ExrBaseCompression.cs
  9. 5
      src/ImageSharp/Formats/Exr/Compression/ExrBaseDecompressor.cs
  10. 8
      src/ImageSharp/Formats/Exr/Compression/ExrCompressorFactory.cs
  11. 9
      src/ImageSharp/Formats/Exr/Compression/ExrDecompressorFactory.cs
  12. 5
      src/ImageSharp/Formats/Exr/ExrBaseCompressor.cs
  13. 4
      src/ImageSharp/Formats/Exr/ExrEncoderCore.cs

5
src/ImageSharp/Formats/Exr/Compression/Compressors/NoneExrCompressor.cs

@ -17,9 +17,10 @@ internal class NoneExrCompressor : ExrBaseCompressor
/// <param name="allocator">The memory allocator.</param>
/// <param name="bytesPerBlock">Bytes per row block.</param>
/// <param name="bytesPerRow">Bytes per pixel row.</param>
/// <param name="rowsPerBlock">The pixel rows per block.</param>
/// <param name="width">The witdh of one row in pixels.</param>
public NoneExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, int width)
: base(output, allocator, bytesPerBlock, bytesPerRow, width)
public NoneExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width)
: base(output, allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width)
{
}

5
src/ImageSharp/Formats/Exr/Compression/Compressors/ZipExrCompressor.cs

@ -24,10 +24,11 @@ internal class ZipExrCompressor : ExrBaseCompressor
/// <param name="allocator">The memory allocator.</param>
/// <param name="bytesPerBlock">The bytes per block.</param>
/// <param name="bytesPerRow">The bytes per row.</param>
/// <param name="rowsPerBlock">The pixel rows per block.</param>
/// <param name="width">The witdh of one row in pixels.</param>
/// <param name="compressionLevel">The compression level for deflate compression.</param>
public ZipExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, int width, DeflateCompressionLevel compressionLevel)
: base(output, allocator, bytesPerBlock, bytesPerRow, width)
public ZipExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width, DeflateCompressionLevel compressionLevel)
: base(output, allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width)
{
this.compressionLevel = compressionLevel;
this.buffer = allocator.Allocate<byte>((int)bytesPerBlock);

19
src/ImageSharp/Formats/Exr/Compression/Decompressors/B44ExrCompression.cs

@ -13,8 +13,6 @@ namespace SixLabors.ImageSharp.Formats.Exr.Compression.Decompressors;
/// </summary>
internal class B44ExrCompression : ExrBaseDecompressor
{
private readonly uint rowsPerBlock;
private readonly int channelCount;
private readonly byte[] scratch = new byte[14];
@ -29,13 +27,12 @@ internal class B44ExrCompression : ExrBaseDecompressor
/// <param name="allocator">The memory allocator.</param>
/// <param name="bytesPerBlock">The bytes per pixel row block.</param>
/// <param name="bytesPerRow">The bytes per row.</param>
/// <param name="rowsPerBlock">The rows per block.</param>
/// <param name="rowsPerBlock">The pixel rows per block.</param>
/// <param name="width">The width of a pixel row in pixels.</param>
/// <param name="channelCount">The number of channels of the image.</param>
public B44ExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width, int channelCount)
: base(allocator, bytesPerBlock, bytesPerRow, width)
: base(allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width)
{
this.rowsPerBlock = rowsPerBlock;
this.channelCount = channelCount;
this.tmpBuffer = allocator.Allocate<ushort>((int)(width * rowsPerBlock * channelCount));
}
@ -49,7 +46,7 @@ internal class B44ExrCompression : ExrBaseDecompressor
int bytesLeft = (int)compressedBytes;
for (int i = 0; i < this.channelCount && bytesLeft > 0; i++)
{
for (int y = 0; y < this.rowsPerBlock; y += 4)
for (int y = 0; y < this.RowsPerBlock; y += 4)
{
Span<ushort> row0 = decompressed.Slice(outputOffset, this.Width);
outputOffset += this.Width;
@ -88,7 +85,7 @@ internal class B44ExrCompression : ExrBaseDecompressor
}
int n = x + 3 < this.Width ? 4 : this.Width - x;
if (y + 3 < this.rowsPerBlock)
if (y + 3 < this.RowsPerBlock)
{
this.s.AsSpan(0, n).CopyTo(row0[rowOffset..]);
this.s.AsSpan(4, n).CopyTo(row1[rowOffset..]);
@ -98,12 +95,12 @@ internal class B44ExrCompression : ExrBaseDecompressor
else
{
this.s.AsSpan(0, n).CopyTo(row0[rowOffset..]);
if (y + 1 < this.rowsPerBlock)
if (y + 1 < this.RowsPerBlock)
{
this.s.AsSpan(4, n).CopyTo(row1[rowOffset..]);
}
if (y + 2 < this.rowsPerBlock)
if (y + 2 < this.RowsPerBlock)
{
this.s.AsSpan(8, n).CopyTo(row2[rowOffset..]);
}
@ -122,8 +119,8 @@ internal class B44ExrCompression : ExrBaseDecompressor
// Rearrange the decompressed data such that the data for each scan line form a contiguous block.
int offsetDecompressed = 0;
int offsetOutput = 0;
int blockSize = (int)(this.Width * this.rowsPerBlock);
for (int y = 0; y < this.rowsPerBlock; y++)
int blockSize = (int)(this.Width * this.RowsPerBlock);
for (int y = 0; y < this.RowsPerBlock; y++)
{
for (int i = 0; i < this.channelCount; i++)
{

5
src/ImageSharp/Formats/Exr/Compression/Decompressors/NoneExrCompression.cs

@ -17,9 +17,10 @@ internal class NoneExrCompression : ExrBaseDecompressor
/// <param name="allocator">The memory allocator.</param>
/// <param name="bytesPerBlock">The bytes per pixel row block.</param>
/// <param name="bytesPerRow">The bytes per pixel row.</param>
/// <param name="rowsPerBlock">The pixel rows per block.</param>
/// <param name="width">The number of pixels per row.</param>
public NoneExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, int width)
: base(allocator, bytesPerBlock, bytesPerRow, width)
public NoneExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width)
: base(allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width)
{
}

9
src/ImageSharp/Formats/Exr/Compression/Decompressors/Pxr24Compression.cs

@ -2,9 +2,7 @@
// Licensed under the Six Labors Split License.
using System.Buffers;
using System.IO.Compression;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Compression.Zlib;
using SixLabors.ImageSharp.Formats.Exr.Constants;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory;
@ -18,8 +16,6 @@ internal class Pxr24Compression : ExrBaseDecompressor
{
private readonly IMemoryOwner<byte> tmpBuffer;
private readonly uint rowsPerBlock;
private readonly int channelCount;
private readonly ExrPixelType pixelType;
@ -35,10 +31,9 @@ internal class Pxr24Compression : ExrBaseDecompressor
/// <param name="channelCount">The number of channels for a pixel.</param>
/// <param name="pixelType">The pixel type.</param>
public Pxr24Compression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width, int channelCount, ExrPixelType pixelType)
: base(allocator, bytesPerBlock, bytesPerRow, width)
: base(allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width)
{
this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
this.rowsPerBlock = rowsPerBlock;
this.channelCount = channelCount;
this.pixelType = pixelType;
}
@ -56,7 +51,7 @@ internal class Pxr24Compression : ExrBaseDecompressor
int lastIn = 0;
int outputOffset = 0;
for (int y = 0; y < this.rowsPerBlock; y++)
for (int y = 0; y < this.RowsPerBlock; y++)
{
for (int c = 0; c < this.channelCount; c++)
{

4
src/ImageSharp/Formats/Exr/Compression/Decompressors/RunLengthExrCompression.cs

@ -23,8 +23,8 @@ internal class RunLengthExrCompression : ExrBaseDecompressor
/// <param name="bytesPerBlock">The bytes per pixel row block.</param>
/// <param name="bytesPerRow">The bytes per row.</param>
/// <param name="width">The witdh of one row in pixels.</param>
public RunLengthExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, int width)
: base(allocator, bytesPerBlock, bytesPerRow, width) => this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
public RunLengthExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width)
: base(allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width) => this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
/// <inheritdoc/>
public override void Decompress(BufferedReadStream stream, uint compressedBytes, Span<byte> buffer)

7
src/ImageSharp/Formats/Exr/Compression/Decompressors/ZipExrCompression.cs

@ -2,8 +2,6 @@
// Licensed under the Six Labors Split License.
using System.Buffers;
using System.IO.Compression;
using SixLabors.ImageSharp.Compression.Zlib;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory;
@ -22,9 +20,10 @@ internal class ZipExrCompression : ExrBaseDecompressor
/// <param name="allocator">The memory allocator.</param>
/// <param name="bytesPerBlock">The bytes per pixel row block.</param>
/// <param name="bytesPerRow">The bytes per pixel row.</param>
/// <param name="rowsPerBlock">The pixel rows per block.</param>
/// <param name="width">The witdh of one row in pixels.</param>
public ZipExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, int width)
: base(allocator, bytesPerBlock, bytesPerRow, width) => this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
public ZipExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width)
: base(allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width) => this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
/// <inheritdoc/>
public override void Decompress(BufferedReadStream stream, uint compressedBytes, Span<byte> buffer)

9
src/ImageSharp/Formats/Exr/Compression/ExrBaseCompression.cs

@ -18,12 +18,14 @@ internal abstract class ExrBaseCompression : IDisposable
/// <param name="allocator">The memory allocator.</param>
/// <param name="bytesPerBlock">The bytes per block.</param>
/// <param name="bytesPerRow">The bytes per row.</param>
/// <param name="rowsPerBlock">The number of pixel rows per block.</param>
/// <param name="width">The number of pixels of a row.</param>
protected ExrBaseCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, int width)
protected ExrBaseCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width)
{
this.Allocator = allocator;
this.BytesPerBlock = bytesPerBlock;
this.BytesPerRow = bytesPerRow;
this.RowsPerBlock = rowsPerBlock;
this.Width = width;
}
@ -47,6 +49,11 @@ internal abstract class ExrBaseCompression : IDisposable
/// </summary>
public uint BytesPerBlock { get; }
/// <summary>
/// Gets the number of pixel rows per block.
/// </summary>
public uint RowsPerBlock { get; }
/// <summary>
/// Gets the image width.
/// </summary>

5
src/ImageSharp/Formats/Exr/Compression/ExrBaseDecompressor.cs

@ -19,9 +19,10 @@ internal abstract class ExrBaseDecompressor : ExrBaseCompression
/// <param name="allocator">The memory allocator.</param>
/// <param name="bytesPerBlock">The bytes per row block.</param>
/// <param name="bytesPerRow">The bytes per row.</param>
/// <param name="rowsPerBlock">The pixel rows per block.</param>
/// <param name="width">The number of pixels per row.</param>
protected ExrBaseDecompressor(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, int width)
: base(allocator, bytesPerBlock, bytesPerRow, width)
protected ExrBaseDecompressor(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width)
: base(allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width)
{
}

8
src/ImageSharp/Formats/Exr/Compression/ExrCompressorFactory.cs

@ -21,6 +21,7 @@ internal static class ExrCompressorFactory
/// <param name="output">The output stream.</param>
/// <param name="bytesPerBlock">The bytes per block.</param>
/// <param name="bytesPerRow">The bytes per row.</param>
/// <param name="rowsPerBlock">The pixel rows per block.</param>
/// <param name="width">The witdh of one row in pixels.</param>
/// <param name="compressionLevel">The deflate compression level.</param>
/// <returns>A compressor for EXR image data.</returns>
@ -30,12 +31,13 @@ internal static class ExrCompressorFactory
Stream output,
uint bytesPerBlock,
uint bytesPerRow,
uint rowsPerBlock,
int width,
DeflateCompressionLevel compressionLevel = DeflateCompressionLevel.DefaultCompression) => method switch
{
ExrCompression.None => new NoneExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, width),
ExrCompression.Zips => new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, width, compressionLevel),
ExrCompression.Zip => new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, width, compressionLevel),
ExrCompression.None => new NoneExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width),
ExrCompression.Zips => new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width, compressionLevel),
ExrCompression.Zip => new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width, compressionLevel),
_ => throw ExrThrowHelper.NotSupportedCompressor(method.ToString()),
};
}

9
src/ImageSharp/Formats/Exr/Compression/ExrDecompressorFactory.cs

@ -22,6 +22,7 @@ internal static class ExrDecompressorFactory
/// <param name="bytesPerRow">The bytes per row.</param>
/// <param name="rowsPerBlock">The rows per block.</param>
/// <param name="channelCount">The number of image channels.</param>
/// <param name="pixelType">The pixel type.</param>
/// <returns>Decompressor for EXR image data.</returns>
public static ExrBaseDecompressor Create(
ExrCompression method,
@ -33,10 +34,10 @@ internal static class ExrDecompressorFactory
int channelCount,
ExrPixelType pixelType) => method switch
{
ExrCompression.None => new NoneExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow, width),
ExrCompression.Zips => new ZipExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow, width),
ExrCompression.Zip => new ZipExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow, width),
ExrCompression.RunLengthEncoded => new RunLengthExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow, width),
ExrCompression.None => new NoneExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width),
ExrCompression.Zips => new ZipExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width),
ExrCompression.Zip => new ZipExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width),
ExrCompression.RunLengthEncoded => new RunLengthExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width),
ExrCompression.B44 => new B44ExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width, channelCount),
ExrCompression.Pxr24 => new Pxr24Compression(memoryAllocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width, channelCount, pixelType),
_ => throw ExrThrowHelper.NotSupportedDecompressor(nameof(method)),

5
src/ImageSharp/Formats/Exr/ExrBaseCompressor.cs

@ -14,9 +14,10 @@ internal abstract class ExrBaseCompressor : ExrBaseCompression
/// <param name="allocator">The memory allocator.</param>
/// <param name="bytesPerBlock">Bytes per row block.</param>
/// <param name="bytesPerRow">Bytes per pixel row.</param>
/// <param name="rowsPerBlock">The pixel rows per block.</param>
/// <param name="width">The number of pixels per row.</param>
protected ExrBaseCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, int width)
: base(allocator, bytesPerBlock, bytesPerRow, width)
protected ExrBaseCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width)
: base(allocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width)
=> this.Output = output;
/// <summary>

4
src/ImageSharp/Formats/Exr/ExrEncoderCore.cs

@ -181,7 +181,7 @@ internal sealed class ExrEncoderCore
Span<float> blueBuffer = rgbBuffer.GetSpan().Slice(width * 2, width);
Span<float> alphaBuffer = rgbBuffer.GetSpan().Slice(width * 3, width);
using ExrBaseCompressor compressor = ExrCompressorFactory.Create(compression, this.memoryAllocator, stream, bytesPerBlock, bytesPerRow, width);
using ExrBaseCompressor compressor = ExrCompressorFactory.Create(compression, this.memoryAllocator, stream, bytesPerBlock, bytesPerRow, rowsPerBlock, width);
ulong[] rowOffsets = new ulong[height];
for (uint y = 0; y < height; y += rowsPerBlock)
@ -273,7 +273,7 @@ internal sealed class ExrEncoderCore
Span<uint> blueBuffer = rgbBuffer.GetSpan().Slice(width * 2, width);
Span<uint> alphaBuffer = rgbBuffer.GetSpan().Slice(width * 3, width);
using ExrBaseCompressor compressor = ExrCompressorFactory.Create(compression, this.memoryAllocator, stream, bytesPerBlock, bytesPerRow, width);
using ExrBaseCompressor compressor = ExrCompressorFactory.Create(compression, this.memoryAllocator, stream, bytesPerBlock, bytesPerRow, rowsPerBlock, width);
Rgba128 rgb = default;
ulong[] rowOffsets = new ulong[height];

Loading…
Cancel
Save