Skip to content

Commit b8fe22c

Browse files
Merge branch 'main' into stefannikolei/nullable/exifprofile
2 parents 49d332e + 84dcd18 commit b8fe22c

31 files changed

+121
-106
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace SixLabors.ImageSharp.Compression.Zlib;
57

68
internal static class DeflateThrowHelper
79
{
10+
[DoesNotReturn]
811
public static void ThrowAlreadyFinished() => throw new InvalidOperationException("Finish() already called.");
912

13+
[DoesNotReturn]
1014
public static void ThrowAlreadyClosed() => throw new InvalidOperationException("Deflator already closed.");
1115

16+
[DoesNotReturn]
1217
public static void ThrowUnknownCompression() => throw new InvalidOperationException("Unknown compression function.");
1318

19+
[DoesNotReturn]
1420
public static void ThrowNotProcessed() => throw new InvalidOperationException("Old input was not completely processed.");
1521

22+
[DoesNotReturn]
1623
public static void ThrowNull(string name) => throw new ArgumentNullException(name);
1724

25+
[DoesNotReturn]
1826
public static void ThrowOutOfRange(string name) => throw new ArgumentOutOfRangeException(name);
1927

28+
[DoesNotReturn]
2029
public static void ThrowHeapViolated() => throw new InvalidOperationException("Huffman heap invariant violated.");
2130

31+
[DoesNotReturn]
2232
public static void ThrowNoDeflate() => throw new ImageFormatException("Cannot deflate all input.");
2333
}

src/ImageSharp/Compression/Zlib/Deflater.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Runtime.CompilerServices;
65
using SixLabors.ImageSharp.Memory;
@@ -285,7 +284,6 @@ public void Dispose()
285284
if (!this.isDisposed)
286285
{
287286
this.engine.Dispose();
288-
this.engine = null;
289287
this.isDisposed = true;
290288
}
291289
}

src/ImageSharp/Compression/Zlib/DeflaterEngine.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers;
65
using System.Runtime.CompilerServices;
@@ -87,7 +86,7 @@ internal sealed unsafe class DeflaterEngine : IDisposable
8786
/// <summary>
8887
/// The input data for compression.
8988
/// </summary>
90-
private byte[] inputBuf;
89+
private byte[]? inputBuf;
9190

9291
/// <summary>
9392
/// The offset into inputBuf, where input data starts.
@@ -222,7 +221,7 @@ public bool Deflate(bool flush, bool finish)
222221
/// <param name="buffer">The buffer containing input data.</param>
223222
/// <param name="offset">The offset of the first byte of data.</param>
224223
/// <param name="count">The number of bytes of data to use as input.</param>
225-
public void SetInput(byte[] buffer, int offset, int count)
224+
public void SetInput(byte[]? buffer, int offset, int count)
226225
{
227226
if (buffer is null)
228227
{
@@ -362,6 +361,8 @@ public void FillWindow()
362361
more = this.inputEnd - this.inputOff;
363362
}
364363

364+
ArgumentNullException.ThrowIfNull(this.inputBuf);
365+
365366
Unsafe.CopyBlockUnaligned(
366367
ref this.window.Span[this.strstart + this.lookahead],
367368
ref this.inputBuf[this.inputOff],
@@ -393,11 +394,6 @@ public void Dispose()
393394
this.prevMemoryHandle.Dispose();
394395
this.prevMemoryOwner.Dispose();
395396

396-
this.windowMemoryOwner = null;
397-
this.headMemoryOwner = null;
398-
this.prevMemoryOwner = null;
399-
this.huffman = null;
400-
401397
this.isDisposed = true;
402398
}
403399
}

src/ImageSharp/Compression/Zlib/DeflaterHuffman.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers;
65
using System.Runtime.CompilerServices;
@@ -427,10 +426,6 @@ public void Dispose()
427426
this.blTree.Dispose();
428427
this.distTree.Dispose();
429428

430-
this.Pending = null;
431-
this.literalTree = null;
432-
this.blTree = null;
433-
this.distTree = null;
434429
this.isDisposed = true;
435430
}
436431
}
@@ -977,10 +972,6 @@ public void Dispose()
977972
this.codesMemoryHandle.Dispose();
978973
this.codesMemoryOwner.Dispose();
979974

980-
this.frequenciesMemoryOwner = null;
981-
this.lengthsMemoryOwner = null;
982-
this.codesMemoryOwner = null;
983-
984975
this.isDisposed = true;
985976
}
986977
}

src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers;
65
using SixLabors.ImageSharp.Memory;
@@ -137,8 +136,6 @@ protected override void Dispose(bool disposing)
137136
this.memoryOwner.Dispose();
138137
}
139138

140-
this.deflater = null;
141-
this.memoryOwner = null;
142139
this.isDisposed = true;
143140
base.Dispose(disposing);
144141
}

src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers;
65
using System.Runtime.CompilerServices;
@@ -180,7 +179,6 @@ public void Dispose()
180179
{
181180
this.bufferMemoryHandle.Dispose();
182181
this.bufferMemoryOwner.Dispose();
183-
this.bufferMemoryOwner = null;
184182
this.isDisposed = true;
185183
}
186184
}

src/ImageSharp/Compression/Zlib/ZlibDeflateStream.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Runtime.CompilerServices;
65
using SixLabors.ImageSharp.Formats.Png;
@@ -172,8 +171,6 @@ protected override void Dispose(bool disposing)
172171
this.rawStream.WriteByte((byte)(crc & 0xFF));
173172
}
174173

175-
this.deflateStream = null;
176-
177174
base.Dispose(disposing);
178175
this.isDisposed = true;
179176
}

src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

4+
using System.Diagnostics.CodeAnalysis;
55
using System.IO.Compression;
66
using SixLabors.ImageSharp.IO;
77

@@ -90,14 +90,15 @@ public ZlibInflateStream(BufferedReadStream innerStream, Func<int> getData)
9090
/// <summary>
9191
/// Gets the compressed stream over the deframed inner stream.
9292
/// </summary>
93-
public DeflateStream CompressedStream { get; private set; }
93+
public DeflateStream? CompressedStream { get; private set; }
9494

9595
/// <summary>
9696
/// Adds new bytes from a frame found in the original stream.
9797
/// </summary>
9898
/// <param name="bytes">The current remaining data according to the chunk length.</param>
9999
/// <param name="isCriticalChunk">Whether the chunk to be inflated is a critical chunk.</param>
100100
/// <returns>The <see cref="bool"/>.</returns>
101+
[MemberNotNullWhen(true, nameof(CompressedStream))]
101102
public bool AllocateNewBytes(int bytes, bool isCriticalChunk)
102103
{
103104
this.currentDataRemaining = bytes;
@@ -210,6 +211,7 @@ protected override void Dispose(bool disposing)
210211
this.isDisposed = true;
211212
}
212213

214+
[MemberNotNullWhen(true, nameof(CompressedStream))]
213215
private bool InitializeInflateStream(bool isCriticalChunk)
214216
{
215217
// Read the zlib header : http://tools.ietf.org/html/rfc1950

src/ImageSharp/Diagnostics/MemoryDiagnostics.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
namespace SixLabors.ImageSharp.Diagnostics;
65

@@ -17,7 +16,7 @@ public static class MemoryDiagnostics
1716
{
1817
private static int totalUndisposedAllocationCount;
1918

20-
private static UndisposedAllocationDelegate undisposedAllocation;
19+
private static UndisposedAllocationDelegate? undisposedAllocation;
2120
private static int undisposedAllocationSubscriptionCounter;
2221
private static readonly object SyncRoot = new();
2322

@@ -50,12 +49,12 @@ public static event UndisposedAllocationDelegate UndisposedAllocation
5049
/// <summary>
5150
/// Fires when ImageSharp allocates memory from a MemoryAllocator
5251
/// </summary>
53-
internal static event Action MemoryAllocated;
52+
internal static event Action? MemoryAllocated;
5453

5554
/// <summary>
5655
/// Fires when ImageSharp releases memory allocated from a MemoryAllocator
5756
/// </summary>
58-
internal static event Action MemoryReleased;
57+
internal static event Action? MemoryReleased;
5958

6059
/// <summary>
6160
/// Gets a value indicating the total number of memory resource objects leaked to the finalizer.

src/ImageSharp/Formats/Jpeg/Components/Decoder/JFifMarker.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder;
2626
/// <param name="yDensity">The vertical pixel density.</param>
2727
private JFifMarker(byte majorVersion, byte minorVersion, byte densityUnits, short xDensity, short yDensity)
2828
{
29-
if (xDensity <= 0)
30-
{
31-
JpegThrowHelper.ThrowInvalidImageContentException($"X-Density {xDensity} must be greater than 0.");
32-
}
33-
34-
if (yDensity <= 0)
35-
{
36-
JpegThrowHelper.ThrowInvalidImageContentException($"Y-Density {yDensity} must be greater than 0.");
37-
}
38-
3929
this.MajorVersion = majorVersion;
4030
this.MinorVersion = minorVersion;
4131

@@ -64,12 +54,12 @@ private JFifMarker(byte majorVersion, byte minorVersion, byte densityUnits, shor
6454
public PixelResolutionUnit DensityUnits { get; }
6555

6656
/// <summary>
67-
/// Gets the horizontal pixel density. Must not be zero.
57+
/// Gets the horizontal pixel density.
6858
/// </summary>
6959
public short XDensity { get; }
7060

7161
/// <summary>
72-
/// Gets the vertical pixel density. Must not be zero.
62+
/// Gets the vertical pixel density.
7363
/// </summary>
7464
public short YDensity { get; }
7565

@@ -88,12 +78,8 @@ public static bool TryParse(byte[] bytes, out JFifMarker marker)
8878
byte densityUnits = bytes[7];
8979
short xDensity = (short)((bytes[8] << 8) | bytes[9]);
9080
short yDensity = (short)((bytes[10] << 8) | bytes[11]);
91-
92-
if (xDensity > 0 && yDensity > 0)
93-
{
94-
marker = new JFifMarker(majorVersion, minorVersion, densityUnits, xDensity, yDensity);
95-
return true;
96-
}
81+
marker = new JFifMarker(majorVersion, minorVersion, densityUnits, xDensity, yDensity);
82+
return true;
9783
}
9884

9985
marker = default;

0 commit comments

Comments
 (0)