Skip to content

Commit 1e69aa4

Browse files
committed
Add additional doc strings
1 parent af14463 commit 1e69aa4

18 files changed

+606
-117
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@
55

66
namespace SixLabors.ImageSharp.Formats.Exr.Compression.Compressors;
77

8+
/// <summary>
9+
/// Compressor for EXR image data which does not use any compression method.
10+
/// </summary>
811
internal class NoneExrCompressor : ExrBaseCompressor
912
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="NoneExrCompressor"/> class.
15+
/// </summary>
16+
/// <param name="output">The output stream to write the compressed image data to.</param>
17+
/// <param name="allocator">The memory allocator.</param>
18+
/// <param name="bytesPerBlock">Bytes per row block.</param>
19+
/// <param name="bytesPerRow">Bytes per pixel row.</param>
1020
public NoneExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
1121
: base(output, allocator, bytesPerBlock, bytesPerRow)
1222
{

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
namespace SixLabors.ImageSharp.Formats.Exr.Compression.Compressors;
88

9+
/// <summary>
10+
/// Compressor for EXR image data using the ZIP compression.
11+
/// </summary>
912
internal class ZipExrCompressor : ExrBaseCompressor
1013
{
1114
private readonly DeflateCompressionLevel compressionLevel;
@@ -14,6 +17,14 @@ internal class ZipExrCompressor : ExrBaseCompressor
1417

1518
private readonly System.Buffers.IMemoryOwner<byte> buffer;
1619

20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="ZipExrCompressor"/> class.
22+
/// </summary>
23+
/// <param name="output">The stream to write the compressed data to.</param>
24+
/// <param name="allocator">The memory allocator.</param>
25+
/// <param name="bytesPerBlock">The bytes per block.</param>
26+
/// <param name="bytesPerRow">The bytes per row.</param>
27+
/// <param name="compressionLevel">The compression level for deflate compression.</param>
1728
public ZipExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, DeflateCompressionLevel compressionLevel)
1829
: base(output, allocator, bytesPerBlock, bytesPerRow)
1930
{

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
namespace SixLabors.ImageSharp.Formats.Exr.Compression.Decompressors;
1010

11+
/// <summary>
12+
/// Implementation of B44 decompressor for EXR image data.
13+
/// </summary>
1114
internal class B44ExrCompression : ExrBaseDecompressor
1215
{
1316
private readonly int width;
@@ -22,6 +25,15 @@ internal class B44ExrCompression : ExrBaseDecompressor
2225

2326
private readonly IMemoryOwner<ushort> tmpBuffer;
2427

28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="B44ExrCompression" /> class.
30+
/// </summary>
31+
/// <param name="allocator">The memory allocator.</param>
32+
/// <param name="bytesPerBlock">The bytes per pixel row block.</param>
33+
/// <param name="bytesPerRow">The bytes per row.</param>
34+
/// <param name="rowsPerBlock">The rows per block.</param>
35+
/// <param name="width">The width of a pixel row in pixels.</param>
36+
/// <param name="channelCount">The number of channels of the image.</param>
2537
public B44ExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, uint rowsPerBlock, int width, int channelCount)
2638
: base(allocator, bytesPerBlock, bytesPerRow)
2739
{
@@ -57,7 +69,7 @@ public override void Decompress(BufferedReadStream stream, uint compressedBytes,
5769
int bytesRead = stream.Read(this.scratch, 0, 3);
5870
if (bytesRead == 0)
5971
{
60-
ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough data from the stream");
72+
ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough data from the stream!");
6173
}
6274

6375
if (this.scratch[2] >= 13 << 2)
@@ -70,7 +82,7 @@ public override void Decompress(BufferedReadStream stream, uint compressedBytes,
7082
bytesRead = stream.Read(this.scratch, 3, 11);
7183
if (bytesRead == 0)
7284
{
73-
ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough data from the stream");
85+
ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough data from the stream!");
7486
}
7587

7688
Unpack14(this.scratch, this.s);
@@ -80,22 +92,22 @@ public override void Decompress(BufferedReadStream stream, uint compressedBytes,
8092
int n = x + 3 < this.width ? 4 : this.width - x;
8193
if (y + 3 < this.rowsPerBlock)
8294
{
83-
this.s.AsSpan(0, n).CopyTo(row0.Slice(rowOffset));
84-
this.s.AsSpan(4, n).CopyTo(row1.Slice(rowOffset));
85-
this.s.AsSpan(8, n).CopyTo(row2.Slice(rowOffset));
86-
this.s.AsSpan(12, n).CopyTo(row3.Slice(rowOffset));
95+
this.s.AsSpan(0, n).CopyTo(row0[rowOffset..]);
96+
this.s.AsSpan(4, n).CopyTo(row1[rowOffset..]);
97+
this.s.AsSpan(8, n).CopyTo(row2[rowOffset..]);
98+
this.s.AsSpan(12, n).CopyTo(row3[rowOffset..]);
8799
}
88100
else
89101
{
90-
this.s.AsSpan(0, n).CopyTo(row0.Slice(rowOffset));
102+
this.s.AsSpan(0, n).CopyTo(row0[rowOffset..]);
91103
if (y + 1 < this.rowsPerBlock)
92104
{
93-
this.s.AsSpan(4, n).CopyTo(row1.Slice(rowOffset));
105+
this.s.AsSpan(4, n).CopyTo(row1[rowOffset..]);
94106
}
95107

96108
if (y + 2 < this.rowsPerBlock)
97109
{
98-
this.s.AsSpan(8, n).CopyTo(row2.Slice(rowOffset));
110+
this.s.AsSpan(8, n).CopyTo(row2[rowOffset..]);
99111
}
100112
}
101113

@@ -117,15 +129,19 @@ public override void Decompress(BufferedReadStream stream, uint compressedBytes,
117129
{
118130
for (int i = 0; i < this.channelCount; i++)
119131
{
120-
decompressed.Slice(offsetDecompressed + (i * blockSize), this.width).CopyTo(outputBuffer.Slice(offsetOutput));
132+
decompressed.Slice(offsetDecompressed + (i * blockSize), this.width).CopyTo(outputBuffer[offsetOutput..]);
121133
offsetOutput += this.width;
122134
}
123135

124136
offsetDecompressed += this.width;
125137
}
126138
}
127139

128-
// Unpack a 14-byte block into 4 by 4 16-bit pixels.
140+
/// <summary>
141+
/// Unpack a 14-byte block into 4 by 4 16-bit pixels.
142+
/// </summary>
143+
/// <param name="b">The source byte data to unpack.</param>
144+
/// <param name="s">Destintation buffer.</param>
129145
private static void Unpack14(Span<byte> b, Span<ushort> s)
130146
{
131147
s[0] = (ushort)((b[0] << 8) | b[1]);
@@ -165,7 +181,11 @@ private static void Unpack14(Span<byte> b, Span<ushort> s)
165181
}
166182
}
167183

168-
// Unpack a 3-byte block into 4 by 4 identical 16-bit pixels.
184+
/// <summary>
185+
/// // Unpack a 3-byte block into 4 by 4 identical 16-bit pixels.
186+
/// </summary>
187+
/// <param name="b">The source byte data to unpack.</param>
188+
/// <param name="s">The destination buffer.</param>
169189
private static void Unpack3(Span<byte> b, Span<ushort> s)
170190
{
171191
s[0] = (ushort)((b[0] << 8) | b[1]);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66

77
namespace SixLabors.ImageSharp.Formats.Exr.Compression.Decompressors;
88

9+
/// <summary>
10+
/// Decompressor for EXR image data which do not use any compression.
11+
/// </summary>
912
internal class NoneExrCompression : ExrBaseDecompressor
1013
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="NoneExrCompression" /> class.
16+
/// </summary>
17+
/// <param name="allocator">The memory allocator.</param>
18+
/// <param name="bytesPerBlock">The bytes per pixel row block.</param>
19+
/// <param name="bytesPerRow">The bytes per pixel row.</param>
1120
public NoneExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
1221
: base(allocator, bytesPerBlock, bytesPerRow)
1322
{
@@ -19,7 +28,7 @@ public override void Decompress(BufferedReadStream stream, uint compressedBytes,
1928
int bytesRead = stream.Read(buffer, 0, Math.Min(buffer.Length, (int)this.BytesPerBlock));
2029
if (bytesRead != (int)this.BytesPerBlock)
2130
{
22-
ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough pixel data!");
31+
ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough pixel data from the stream!");
2332
}
2433
}
2534

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77

88
namespace SixLabors.ImageSharp.Formats.Exr.Compression.Decompressors;
99

10+
/// <summary>
11+
/// Implementation of RLE decompressor for EXR images.
12+
/// </summary>
1013
internal class RunLengthExrCompression : ExrBaseDecompressor
1114
{
1215
private readonly IMemoryOwner<byte> tmpBuffer;
1316

1417
private readonly ushort[] s = new ushort[16];
1518

19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="RunLengthExrCompression" /> class.
21+
/// </summary>
22+
/// <param name="allocator">The memory allocator.</param>
23+
/// <param name="bytesPerBlock">The bytes per pixel row block.</param>
24+
/// <param name="bytesPerRow">The bytes per row.</param>
1625
public RunLengthExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
1726
: base(allocator, bytesPerBlock, bytesPerRow) => this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
1827

@@ -68,12 +77,17 @@ public override void Decompress(BufferedReadStream stream, uint compressedBytes,
6877
Interleave(uncompressed, this.BytesPerBlock, buffer);
6978
}
7079

80+
/// <summary>
81+
/// Reads the next byte from the stream.
82+
/// </summary>
83+
/// <param name="stream">The stream.</param>
84+
/// <returns>The next byte.</returns>
7185
private static byte ReadNextByte(BufferedReadStream stream)
7286
{
7387
int nextByte = stream.ReadByte();
7488
if (nextByte == -1)
7589
{
76-
ExrThrowHelper.ThrowInvalidImageContentException("Not enough data to decompress RLE image!");
90+
ExrThrowHelper.ThrowInvalidImageContentException("Not enough data to decompress RLE encoded EXR image!");
7791
}
7892

7993
return (byte)nextByte;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@
99

1010
namespace SixLabors.ImageSharp.Formats.Exr.Compression.Decompressors;
1111

12+
/// <summary>
13+
/// Implementation of zhe Zip decompressor for EXR image data.
14+
/// </summary>
1215
internal class ZipExrCompression : ExrBaseDecompressor
1316
{
1417
private readonly IMemoryOwner<byte> tmpBuffer;
1518

19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="ZipExrCompression" /> class.
21+
/// </summary>
22+
/// <param name="allocator">The memory allocator.</param>
23+
/// <param name="bytesPerBlock">The bytes per pixel row block.</param>
24+
/// <param name="bytesPerRow">The bytes per pixel row.</param>
1625
public ZipExrCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
1726
: base(allocator, bytesPerBlock, bytesPerRow) => this.tmpBuffer = allocator.Allocate<byte>((int)bytesPerBlock);
1827

@@ -46,7 +55,7 @@ public override void Decompress(BufferedReadStream stream, uint compressedBytes,
4655

4756
if (totalRead == 0)
4857
{
49-
ExrThrowHelper.ThrowInvalidImageContentException("Could not read zip compressed image data!");
58+
ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough data for zip compressed image data!");
5059
}
5160

5261
Reconstruct(uncompressed, (uint)totalRead);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55

66
namespace SixLabors.ImageSharp.Formats.Exr.Compression;
77

8+
/// <summary>
9+
/// Base class for EXR compression.
10+
/// </summary>
811
internal abstract class ExrBaseCompression : IDisposable
912
{
1013
private bool isDisposed;
1114

15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ExrBaseCompression" /> class.
17+
/// </summary>
18+
/// <param name="allocator">The memory allocator.</param>
19+
/// <param name="bytesPerBlock">The bytes per block.</param>
20+
/// <param name="bytesPerRow">The bytes per row.</param>
1221
protected ExrBaseCompression(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
1322
{
1423
this.Allocator = allocator;

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,36 @@
66

77
namespace SixLabors.ImageSharp.Formats.Exr.Compression;
88

9+
/// <summary>
10+
/// The base EXR decompressor class.
11+
/// </summary>
912
internal abstract class ExrBaseDecompressor : ExrBaseCompression
1013
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="ExrBaseDecompressor" /> class.
16+
/// </summary>
17+
/// <param name="allocator">The memory allocator.</param>
18+
/// <param name="bytesPerBlock">The bytes per row block.</param>
19+
/// <param name="bytesPerRow">The bytes per row.</param>
1120
protected ExrBaseDecompressor(MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow)
1221
: base(allocator, bytesPerBlock, bytesPerRow)
1322
{
1423
}
1524

25+
/// <summary>
26+
/// Decompresses the specified stream.
27+
/// </summary>
28+
/// <param name="stream">The buffered stream to decompress.</param>
29+
/// <param name="compressedBytes">The compressed bytes.</param>
30+
/// <param name="buffer">The buffer to write the decompressed data to.</param>
1631
public abstract void Decompress(BufferedReadStream stream, uint compressedBytes, Span<byte> buffer);
1732

33+
/// <summary>
34+
/// Integrate over all differences to the previous value in order to
35+
/// reconstruct sample values.
36+
/// </summary>
37+
/// <param name="buffer">The buffer with the data.</param>
38+
/// <param name="unCompressedBytes">The un compressed bytes.</param>
1839
protected static void Reconstruct(Span<byte> buffer, uint unCompressedBytes)
1940
{
2041
int offset = 0;
@@ -26,6 +47,12 @@ protected static void Reconstruct(Span<byte> buffer, uint unCompressedBytes)
2647
}
2748
}
2849

50+
/// <summary>
51+
/// Interleaves the input data.
52+
/// </summary>
53+
/// <param name="source">The source data.</param>
54+
/// <param name="unCompressedBytes">The uncompressed bytes.</param>
55+
/// <param name="output">The output to write to.</param>
2956
protected static void Interleave(Span<byte> source, uint unCompressedBytes, Span<byte> output)
3057
{
3158
int sourceOffset = 0;

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,32 @@
88

99
namespace SixLabors.ImageSharp.Formats.Exr.Compression;
1010

11+
/// <summary>
12+
/// Factory class for creating a compressor for EXR image data.
13+
/// </summary>
1114
internal static class ExrCompressorFactory
1215
{
16+
/// <summary>
17+
/// Creates the specified exr data compressor.
18+
/// </summary>
19+
/// <param name="method">The compression method.</param>
20+
/// <param name="allocator">The memory allocator.</param>
21+
/// <param name="output">The output stream.</param>
22+
/// <param name="bytesPerBlock">The bytes per block.</param>
23+
/// <param name="bytesPerRow">The bytes per row.</param>
24+
/// <param name="compressionLevel">The deflate compression level.</param>
25+
/// <returns>A compressor for EXR image data.</returns>
1326
public static ExrBaseCompressor Create(
1427
ExrCompression method,
1528
MemoryAllocator allocator,
1629
Stream output,
1730
uint bytesPerBlock,
1831
uint bytesPerRow,
19-
DeflateCompressionLevel compressionLevel = DeflateCompressionLevel.DefaultCompression)
20-
{
21-
switch (method)
32+
DeflateCompressionLevel compressionLevel = DeflateCompressionLevel.DefaultCompression) => method switch
2233
{
23-
case ExrCompression.None:
24-
return new NoneExrCompressor(output, allocator, bytesPerBlock, bytesPerRow);
25-
case ExrCompression.Zips:
26-
return new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, compressionLevel);
27-
case ExrCompression.Zip:
28-
return new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, compressionLevel);
29-
30-
default:
31-
throw ExrThrowHelper.NotSupportedCompressor(method.ToString());
32-
}
33-
}
34+
ExrCompression.None => new NoneExrCompressor(output, allocator, bytesPerBlock, bytesPerRow),
35+
ExrCompression.Zips => new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, compressionLevel),
36+
ExrCompression.Zip => new ZipExrCompressor(output, allocator, bytesPerBlock, bytesPerRow, compressionLevel),
37+
_ => throw ExrThrowHelper.NotSupportedCompressor(method.ToString()),
38+
};
3439
}

0 commit comments

Comments
 (0)