Browse Source

Use Width Property from ExrBaseDecompressor

pull/3124/head
Brian Popow 3 weeks ago
parent
commit
344d49d86f
  1. 5
      src/ImageSharp/Formats/Exr/Compression/Compressors/NoneExrCompressor.cs
  2. 5
      src/ImageSharp/Formats/Exr/Compression/Compressors/ZipExrCompressor.cs
  3. 33
      src/ImageSharp/Formats/Exr/Compression/Decompressors/B44ExrCompression.cs
  4. 5
      src/ImageSharp/Formats/Exr/Compression/Decompressors/NoneExrCompression.cs
  5. 17
      src/ImageSharp/Formats/Exr/Compression/Decompressors/Pxr24Compression.cs
  6. 5
      src/ImageSharp/Formats/Exr/Compression/Decompressors/RunLengthExrCompression.cs
  7. 5
      src/ImageSharp/Formats/Exr/Compression/Decompressors/ZipExrCompression.cs
  8. 4
      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. 8
      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,8 +17,9 @@ 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>
public NoneExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(output, allocator, bytesPerBlock, bytesPerRow)
/// <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)
{
}

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

@ -24,9 +24,10 @@ 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="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, DeflateCompressionLevel compressionLevel)
: base(output, allocator, bytesPerBlock, bytesPerRow)
public ZipExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, int width, DeflateCompressionLevel compressionLevel)
: base(output, allocator, bytesPerBlock, bytesPerRow, width)
{
this.compressionLevel = compressionLevel;
this.buffer = allocator.Allocate<byte>((int)bytesPerBlock);

33
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 int width;
private readonly uint rowsPerBlock;
private readonly int channelCount;
@ -35,9 +33,8 @@ internal class B44ExrCompression : ExrBaseDecompressor
/// <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)
: base(allocator, bytesPerBlock, bytesPerRow, width)
{
this.width = width;
this.rowsPerBlock = rowsPerBlock;
this.channelCount = channelCount;
this.tmpBuffer = allocator.Allocate<ushort>((int)(width * rowsPerBlock * channelCount));
@ -54,17 +51,17 @@ internal class B44ExrCompression : ExrBaseDecompressor
{
for (int y = 0; y < this.rowsPerBlock; y += 4)
{
Span<ushort> row0 = decompressed.Slice(outputOffset, this.width);
outputOffset += this.width;
Span<ushort> row1 = decompressed.Slice(outputOffset, this.width);
outputOffset += this.width;
Span<ushort> row2 = decompressed.Slice(outputOffset, this.width);
outputOffset += this.width;
Span<ushort> row3 = decompressed.Slice(outputOffset, this.width);
outputOffset += this.width;
Span<ushort> row0 = decompressed.Slice(outputOffset, this.Width);
outputOffset += this.Width;
Span<ushort> row1 = decompressed.Slice(outputOffset, this.Width);
outputOffset += this.Width;
Span<ushort> row2 = decompressed.Slice(outputOffset, this.Width);
outputOffset += this.Width;
Span<ushort> row3 = decompressed.Slice(outputOffset, this.Width);
outputOffset += this.Width;
int rowOffset = 0;
for (int x = 0; x < this.width && bytesLeft > 0; x += 4)
for (int x = 0; x < this.Width && bytesLeft > 0; x += 4)
{
int bytesRead = stream.Read(this.scratch, 0, 3);
if (bytesRead == 0)
@ -90,7 +87,7 @@ internal class B44ExrCompression : ExrBaseDecompressor
bytesLeft -= 14;
}
int n = x + 3 < this.width ? 4 : this.width - x;
int n = x + 3 < this.Width ? 4 : this.Width - x;
if (y + 3 < this.rowsPerBlock)
{
this.s.AsSpan(0, n).CopyTo(row0[rowOffset..]);
@ -125,16 +122,16 @@ 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);
int blockSize = (int)(this.Width * this.rowsPerBlock);
for (int y = 0; y < this.rowsPerBlock; y++)
{
for (int i = 0; i < this.channelCount; i++)
{
decompressed.Slice(offsetDecompressed + (i * blockSize), this.width).CopyTo(outputBuffer[offsetOutput..]);
offsetOutput += this.width;
decompressed.Slice(offsetDecompressed + (i * blockSize), this.Width).CopyTo(outputBuffer[offsetOutput..]);
offsetOutput += this.Width;
}
offsetDecompressed += this.width;
offsetDecompressed += this.Width;
}
}

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

@ -17,8 +17,9 @@ 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>
public NoneExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(allocator, bytesPerBlock, bytesPerRow)
/// <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)
{
}

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

@ -10,6 +10,9 @@ using SixLabors.ImageSharp.Memory;
namespace SixLabors.ImageSharp.Formats.Exr.Compression.Decompressors;
/// <summary>
/// Implementation of PXR24 decompressor for EXR image data.
/// </summary>
internal class Pxr24Compression : ExrBaseDecompressor
{
private readonly IMemoryOwner<byte> tmpBuffer;
@ -18,20 +21,20 @@ internal class Pxr24Compression : ExrBaseDecompressor
private readonly int channelCount;
private readonly int width;
/// <summary>
/// Initializes a new instance of the <see cref="Pxr24Compression" /> class.
/// </summary>
/// <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>
/// <param name="channelCount">The number of channels for a pixel.</param>
public Pxr24Compression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width, int channelCount)
: base(allocator, bytesPerBlock, bytesPerRow)
: base(allocator, bytesPerBlock, bytesPerRow, width)
{
this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
this.rowsPerBlock = rowsPerBlock;
this.width = width;
this.channelCount = channelCount;
}
@ -76,11 +79,11 @@ internal class Pxr24Compression : ExrBaseDecompressor
for (int c = 0; c < this.channelCount; c++)
{
int offsetT1 = lastIn;
lastIn += this.width;
lastIn += this.Width;
int offsetT2 = lastIn;
lastIn += this.width;
lastIn += this.Width;
uint pixel = 0;
for (int x = 0; x < this.width; x++)
for (int x = 0; x < this.Width; x++)
{
uint t1 = uncompressed[offsetT1];
uint t2 = uncompressed[offsetT2];

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

@ -22,8 +22,9 @@ internal class RunLengthExrCompression : 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>
public RunLengthExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(allocator, bytesPerBlock, bytesPerRow) => this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
/// <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);
/// <inheritdoc/>
public override void Decompress(BufferedReadStream stream, uint compressedBytes, Span<byte> buffer)

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

@ -22,8 +22,9 @@ 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>
public ZipExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(allocator, bytesPerBlock, bytesPerRow) => this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
/// <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);
/// <inheritdoc/>
public override void Decompress(BufferedReadStream stream, uint compressedBytes, Span<byte> buffer)

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

@ -18,11 +18,13 @@ 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>
protected ExrBaseCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
/// <param name="width">The number of pixels of a row.</param>
protected ExrBaseCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, int width)
{
this.Allocator = allocator;
this.BytesPerBlock = bytesPerBlock;
this.BytesPerRow = bytesPerRow;
this.Width = width;
}
/// <summary>

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

@ -17,8 +17,9 @@ 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>
protected ExrBaseDecompressor(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(allocator, bytesPerBlock, bytesPerRow)
/// <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)
{
}

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="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>
public static ExrBaseCompressor Create(
@ -29,11 +30,12 @@ internal static class ExrCompressorFactory
Stream output,
uint bytesPerBlock,
uint bytesPerRow,
int width,
DeflateCompressionLevel compressionLevel = DeflateCompressionLevel.DefaultCompression) => method switch
{
ExrCompression.None => new NoneExrCompressor(output, allocator, bytesPerBlock, bytesPerRow),
ExrCompression.Zips => new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, compressionLevel),
ExrCompression.Zip => new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, compressionLevel),
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),
_ => throw ExrThrowHelper.NotSupportedCompressor(method.ToString()),
};
}

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

@ -32,10 +32,10 @@ internal static class ExrDecompressorFactory
uint rowsPerBlock,
int channelCount) => method switch
{
ExrCompression.None => new NoneExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow),
ExrCompression.Zips => new ZipExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow),
ExrCompression.Zip => new ZipExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow),
ExrCompression.RunLengthEncoded => new RunLengthExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow),
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.B44 => new B44ExrCompression(memoryAllocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width, channelCount),
ExrCompression.Pxr24 => new Pxr24Compression(memoryAllocator, bytesPerBlock, bytesPerRow, rowsPerBlock, width, channelCount),
_ => throw ExrThrowHelper.NotSupportedDecompressor(nameof(method)),

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

@ -14,8 +14,9 @@ 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>
protected ExrBaseCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
: base(allocator, bytesPerBlock, bytesPerRow)
/// <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)
=> 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);
using ExrBaseCompressor compressor = ExrCompressorFactory.Create(compression, this.memoryAllocator, stream, bytesPerBlock, bytesPerRow, 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);
using ExrBaseCompressor compressor = ExrCompressorFactory.Create(compression, this.memoryAllocator, stream, bytesPerBlock, bytesPerRow, width);
Rgba128 rgb = default;
ulong[] rowOffsets = new ulong[height];

Loading…
Cancel
Save