Browse Source

Cleanup Compressor/Decompressors: Remove not needed fields

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

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

@ -8,8 +8,8 @@ namespace SixLabors.ImageSharp.Formats.Exr.Compression.Compressors;
internal class NoneExrCompressor : ExrBaseCompressor
{
public NoneExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock)
: base(output, allocator, bytesPerBlock)
public NoneExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(output, allocator, bytesPerBlock, bytesPerRow)
{
}
@ -17,12 +17,7 @@ internal class NoneExrCompressor : ExrBaseCompressor
public override ExrCompression Method => ExrCompression.Zip;
/// <inheritdoc/>
public override void Initialize(int rowsPerBlock)
{
}
/// <inheritdoc/>
public override uint CompressRowBlock(Span<byte> rows, int height)
public override uint CompressRowBlock(Span<byte> rows, int rowCount)
{
this.Output.Write(rows);
return (uint)rows.Length;

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

@ -15,8 +15,8 @@ internal class ZipExrCompressor : ExrBaseCompressor
private readonly System.Buffers.IMemoryOwner<byte> buffer;
public ZipExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, DeflateCompressionLevel compressionLevel)
: base(output, allocator, bytesPerBlock)
public ZipExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, DeflateCompressionLevel compressionLevel)
: base(output, allocator, bytesPerBlock, bytesPerRow)
{
this.compressionLevel = compressionLevel;
this.buffer = allocator.Allocate<byte>((int)bytesPerBlock);
@ -26,12 +26,7 @@ internal class ZipExrCompressor : ExrBaseCompressor
public override ExrCompression Method => ExrCompression.Zip;
/// <inheritdoc/>
public override void Initialize(int rowsPerBlock)
{
}
/// <inheritdoc/>
public override uint CompressRowBlock(Span<byte> rows, int height)
public override uint CompressRowBlock(Span<byte> rows, int rowCount)
{
// Re-oder pixel values.
int n = rows.Length;

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

@ -12,28 +12,26 @@ internal class B44ExrCompression : ExrBaseDecompressor
{
private readonly int width;
private readonly int height;
private readonly uint rowsPerBlock;
private readonly int channelCount;
private byte[] scratch = new byte[14];
private readonly byte[] scratch = new byte[14];
private ushort[] s = new ushort[16];
private IMemoryOwner<ushort> tmpBuffer;
private readonly IMemoryOwner<ushort> tmpBuffer;
public B44ExrCompression(MemoryAllocator allocator, uint bytesPerBlock, int width, int height, uint rowsPerBlock, int channelCount)
: base(allocator, bytesPerBlock)
public B44ExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width, int channelCount)
: base(allocator, bytesPerBlock, bytesPerRow)
{
this.width = width;
this.height = height;
this.rowsPerBlock = rowsPerBlock;
this.channelCount = channelCount;
this.tmpBuffer = allocator.Allocate<ushort>((int)(width * rowsPerBlock * channelCount));
}
/// <inheritdoc/>
public override void Decompress(BufferedReadStream stream, uint compressedBytes, Span<byte> buffer)
{
Span<ushort> outputBuffer = MemoryMarshal.Cast<byte, ushort>(buffer);
@ -187,5 +185,6 @@ internal class B44ExrCompression : ExrBaseDecompressor
}
}
/// <inheritdoc/>
protected override void Dispose(bool disposing) => this.tmpBuffer.Dispose();
}

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

@ -8,14 +8,16 @@ namespace SixLabors.ImageSharp.Formats.Exr.Compression.Decompressors;
internal class NoneExrCompression : ExrBaseDecompressor
{
public NoneExrCompression(MemoryAllocator allocator, uint bytesPerBlock)
: base(allocator, bytesPerBlock)
public NoneExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(allocator, bytesPerBlock, bytesPerRow)
{
}
/// <inheritdoc/>
public override void Decompress(BufferedReadStream stream, uint compressedBytes, Span<byte> buffer)
=> stream.Read(buffer, 0, Math.Min(buffer.Length, (int)this.BytesPerBlock));
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}

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

@ -13,9 +13,10 @@ internal class RunLengthExrCompression : ExrBaseDecompressor
private readonly ushort[] s = new ushort[16];
public RunLengthExrCompression(MemoryAllocator allocator, uint uncompressedBytes)
: base(allocator, uncompressedBytes) => this.tmpBuffer = allocator.Allocate<byte>((int)uncompressedBytes);
public RunLengthExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(allocator, bytesPerBlock, bytesPerRow) => this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
/// <inheritdoc/>
public override void Decompress(BufferedReadStream stream, uint compressedBytes, Span<byte> buffer)
{
Span<byte> uncompressed = this.tmpBuffer.GetSpan();
@ -78,5 +79,6 @@ internal class RunLengthExrCompression : ExrBaseDecompressor
return (byte)nextByte;
}
/// <inheritdoc/>
protected override void Dispose(bool disposing) => this.tmpBuffer.Dispose();
}

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

@ -13,9 +13,10 @@ internal class ZipExrCompression : ExrBaseDecompressor
{
private readonly IMemoryOwner<byte> tmpBuffer;
public ZipExrCompression(MemoryAllocator allocator, uint bytesPerBlock)
: base(allocator, bytesPerBlock) => this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
public ZipExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(allocator, bytesPerBlock, bytesPerRow) => this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
/// <inheritdoc/>
public override void Decompress(BufferedReadStream stream, uint compressedBytes, Span<byte> buffer)
{
Span<byte> uncompressed = this.tmpBuffer.GetSpan();
@ -52,5 +53,6 @@ internal class ZipExrCompression : ExrBaseDecompressor
Interleave(uncompressed, (uint)totalRead, buffer);
}
/// <inheritdoc/>
protected override void Dispose(bool disposing) => this.tmpBuffer.Dispose();
}

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

@ -9,10 +9,11 @@ internal abstract class ExrBaseCompression : IDisposable
{
private bool isDisposed;
protected ExrBaseCompression(MemoryAllocator allocator, uint bytesPerBlock)
protected ExrBaseCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
{
this.Allocator = allocator;
this.BytesPerBlock = bytesPerBlock;
this.BytesPerRow = bytesPerRow;
}
/// <summary>
@ -28,7 +29,7 @@ internal abstract class ExrBaseCompression : IDisposable
/// <summary>
/// Gets the bytes per row.
/// </summary>
public int BytesPerRow { get; }
public uint BytesPerRow { get; }
/// <summary>
/// Gets the uncompressed bytes per block.

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

@ -8,8 +8,8 @@ namespace SixLabors.ImageSharp.Formats.Exr.Compression;
internal abstract class ExrBaseDecompressor : ExrBaseCompression
{
protected ExrBaseDecompressor(MemoryAllocator allocator, uint bytesPerBlock)
: base(allocator, bytesPerBlock)
protected ExrBaseDecompressor(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(allocator, bytesPerBlock, bytesPerRow)
{
}

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

@ -14,19 +14,18 @@ internal static class ExrCompressorFactory
ExrCompression method,
MemoryAllocator allocator,
Stream output,
int width,
int height,
uint bytesPerBlock,
uint rowsPerBlock,
int channelCount,
uint bytesPerRow,
DeflateCompressionLevel compressionLevel = DeflateCompressionLevel.DefaultCompression)
{
switch (method)
{
case ExrCompression.None:
return new NoneExrCompressor(output, allocator, bytesPerBlock);
return new NoneExrCompressor(output, allocator, bytesPerBlock, bytesPerRow);
case ExrCompression.Zips:
return new ZipExrCompressor(output, allocator, bytesPerBlock, compressionLevel);
return new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, compressionLevel);
case ExrCompression.Zip:
return new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, compressionLevel);
default:
throw ExrThrowHelper.NotSupportedCompressor(method.ToString());

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

@ -13,23 +13,23 @@ internal static class ExrDecompressorFactory
ExrCompression method,
MemoryAllocator memoryAllocator,
int width,
int height,
uint bytesPerBlock,
uint bytesPerRow,
uint rowsPerBlock,
int channelCount)
{
switch (method)
{
case ExrCompression.None:
return new NoneExrCompression(memoryAllocator, bytesPerBlock);
return new NoneExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow);
case ExrCompression.Zips:
return new ZipExrCompression(memoryAllocator, bytesPerBlock);
return new ZipExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow);
case ExrCompression.Zip:
return new ZipExrCompression(memoryAllocator, bytesPerBlock);
return new ZipExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow);
case ExrCompression.RunLengthEncoded:
return new RunLengthExrCompression(memoryAllocator, bytesPerBlock);
return new RunLengthExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow);
case ExrCompression.B44:
return new B44ExrCompression(memoryAllocator, bytesPerBlock, width, height, rowsPerBlock, channelCount);
return new B44ExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width, channelCount);
default:
throw ExrThrowHelper.NotSupportedDecompressor(nameof(method));
}

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

@ -13,9 +13,10 @@ internal abstract class ExrBaseCompressor : ExrBaseCompression
/// </summary>
/// <param name="output">The output stream to write the compressed image to.</param>
/// <param name="allocator">The memory allocator.</param>
/// <param name="bytesPerBlock">Bytes per block.</param>
protected ExrBaseCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock)
: base(allocator, bytesPerBlock)
/// <param name="bytesPerBlock">Bytes per row block.</param>
/// <param name="bytesPerRow">Bytes per pixel row.</param>
protected ExrBaseCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(allocator, bytesPerBlock, bytesPerRow)
=> this.Output = output;
/// <summary>
@ -28,17 +29,11 @@ internal abstract class ExrBaseCompressor : ExrBaseCompression
/// </summary>
public Stream Output { get; }
/// <summary>
/// Does any initialization required for the compression.
/// </summary>
/// <param name="rowsPerBlock">The number of rows per block.</param>
public abstract void Initialize(int rowsPerBlock);
/// <summary>
/// Compresses a block of rows of the image.
/// </summary>
/// <param name="rows">Image rows to compress.</param>
/// <param name="height">Image height.</param>
/// <param name="rowCount">The number of rows to compress.</param>
/// <returns>Number of bytes of of the compressed data.</returns>
public abstract uint CompressRowBlock(Span<byte> rows, int height);
public abstract uint CompressRowBlock(Span<byte> rows, int rowCount);
}

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

@ -147,7 +147,7 @@ internal sealed class ExrDecoderCore : ImageDecoderCore
Span<float> bluePixelData = rowBuffer.GetSpan().Slice(width * 2, width);
Span<float> alphaPixelData = rowBuffer.GetSpan().Slice(width * 3, width);
using ExrBaseDecompressor decompressor = ExrDecompressorFactory.Create(this.Compression, this.memoryAllocator, width, height, bytesPerBlock, rowsPerBlock, channelCount);
using ExrBaseDecompressor decompressor = ExrDecompressorFactory.Create(this.Compression, this.memoryAllocator, width, bytesPerBlock, bytesPerRow, rowsPerBlock, channelCount);
for (uint y = 0; y < height; y += rowsPerBlock)
{
@ -200,7 +200,7 @@ internal sealed class ExrDecoderCore : ImageDecoderCore
Span<uint> bluePixelData = rowBuffer.GetSpan().Slice(width * 2, width);
Span<uint> alphaPixelData = rowBuffer.GetSpan().Slice(width * 3, width);
using ExrBaseDecompressor decompressor = ExrDecompressorFactory.Create(this.Compression, this.memoryAllocator, width, height, bytesPerBlock, rowsPerBlock, channelCount);
using ExrBaseDecompressor decompressor = ExrDecompressorFactory.Create(this.Compression, this.memoryAllocator, width, bytesPerBlock, bytesPerRow, rowsPerBlock, channelCount);
for (uint y = 0; y < height; y += rowsPerBlock)
{

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

@ -5,7 +5,6 @@ using System.Buffers;
using System.Buffers.Binary;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Threading.Channels;
using SixLabors.ImageSharp.Formats.Exr.Compression;
using SixLabors.ImageSharp.Formats.Exr.Constants;
using SixLabors.ImageSharp.Memory;
@ -32,7 +31,7 @@ internal sealed class ExrEncoderCore
/// <summary>
/// The global configuration.
/// </summary>
private Configuration configuration;
private readonly Configuration configuration;
/// <summary>
/// The encoder with options.
@ -170,7 +169,7 @@ internal sealed class ExrEncoderCore
Span<float> greenBuffer = rgbBuffer.GetSpan().Slice(width, width);
Span<float> blueBuffer = rgbBuffer.GetSpan().Slice(width * 2, width);
using ExrBaseCompressor compressor = ExrCompressorFactory.Create(compression, this.memoryAllocator, stream, width, height, bytesPerBlock, rowsPerBlock, channelCount);
using ExrBaseCompressor compressor = ExrCompressorFactory.Create(compression, this.memoryAllocator, stream, bytesPerBlock, bytesPerRow);
ulong[] rowOffsets = new ulong[height];
for (int y = 0; y < height; y++)
@ -181,7 +180,7 @@ internal sealed class ExrEncoderCore
BinaryPrimitives.WriteUInt32LittleEndian(this.buffer, (uint)y);
stream.Write(this.buffer.AsSpan(0, 4));
// At this point, it is not yet known how mcuh bytes the compressed data will take up, keep stream position.
// At this point, it is not yet known how much bytes the compressed data will take up, keep stream position.
long pixelDataSizePos = stream.Position;
Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan(y);
stream.Position = pixelDataSizePos + 4;
@ -195,18 +194,19 @@ internal sealed class ExrEncoderCore
}
// Write pixel data to buffer.
Span<byte> rowBlockSpan = rowBlockBuffer.GetSpan();
switch (this.pixelType)
{
case ExrPixelType.Float:
this.WriteSingleRow(rowBlockBuffer.GetSpan(), width, blueBuffer, greenBuffer, redBuffer);
this.WriteSingleRow(rowBlockSpan, width, blueBuffer, greenBuffer, redBuffer);
break;
case ExrPixelType.Half:
this.WriteHalfSingleRow(rowBlockBuffer.GetSpan(), width, blueBuffer, greenBuffer, redBuffer);
this.WriteHalfSingleRow(rowBlockSpan, width, blueBuffer, greenBuffer, redBuffer);
break;
}
// Write compressed pixel row data to stream.
uint compressedBytes = compressor.CompressRowBlock(rowBlockBuffer.GetSpan(), 1);
// Write compressed pixel row data to the stream.
uint compressedBytes = compressor.CompressRowBlock(rowBlockSpan, (int)rowsPerBlock);
long positionAfterPixelData = stream.Position;
// Write pixel row data size.
@ -239,7 +239,7 @@ internal sealed class ExrEncoderCore
Span<uint> greenBuffer = rgbBuffer.GetSpan().Slice(width, width);
Span<uint> blueBuffer = rgbBuffer.GetSpan().Slice(width * 2, width);
using ExrBaseCompressor compressor = ExrCompressorFactory.Create(compression, this.memoryAllocator, stream, width, height, bytesPerBlock, rowsPerBlock, channelCount);
using ExrBaseCompressor compressor = ExrCompressorFactory.Create(compression, this.memoryAllocator, stream, bytesPerBlock, bytesPerRow);
Rgb96 rgb = default;
ulong[] rowOffsets = new ulong[height];
@ -251,7 +251,7 @@ internal sealed class ExrEncoderCore
BinaryPrimitives.WriteUInt32LittleEndian(this.buffer, (uint)y);
stream.Write(this.buffer.AsSpan(0, 4));
// At this point, it is not yet known how mcuh bytes the compressed data will take up, keep stream position.
// At this point, it is not yet known how much bytes the compressed data will take up, keep stream position.
long pixelDataSizePos = stream.Position;
Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan(y);
stream.Position = pixelDataSizePos + 4;

Loading…
Cancel
Save