Skip to content

Commit d9f7278

Browse files
committed
#2231 First steps for removing nullable disable in webp
1 parent d9192c6 commit d9f7278

17 files changed

+178
-184
lines changed

src/ImageSharp/Formats/Webp/AlphaDecoder.cs

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

54
using System.Buffers;
5+
using System.Diagnostics.CodeAnalysis;
66
using System.Runtime.CompilerServices;
77
using System.Runtime.InteropServices;
88
using System.Runtime.Intrinsics;
@@ -110,6 +110,7 @@ public AlphaDecoder(int width, int height, IMemoryOwner<byte> data, byte alphaCh
110110
/// <summary>
111111
/// Gets a value indicating whether the alpha channel uses compression.
112112
/// </summary>
113+
[MemberNotNullWhen(true, nameof(LosslessDecoder))]
113114
private bool Compressed { get; }
114115

115116
/// <summary>
@@ -120,7 +121,7 @@ public AlphaDecoder(int width, int height, IMemoryOwner<byte> data, byte alphaCh
120121
/// <summary>
121122
/// Gets the Vp8L decoder which is used to de compress the alpha channel, if needed.
122123
/// </summary>
123-
private WebpLosslessDecoder LosslessDecoder { get; }
124+
private WebpLosslessDecoder? LosslessDecoder { get; }
124125

125126
/// <summary>
126127
/// Gets a value indicating whether the decoding needs 1 byte per pixel for decoding.

src/ImageSharp/Formats/Webp/AlphaEncoder.cs

Lines changed: 1 addition & 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 SixLabors.ImageSharp.Advanced;
@@ -15,7 +14,7 @@ namespace SixLabors.ImageSharp.Formats.Webp;
1514
/// </summary>
1615
internal class AlphaEncoder : IDisposable
1716
{
18-
private IMemoryOwner<byte> alphaData;
17+
private IMemoryOwner<byte>? alphaData;
1918

2019
/// <summary>
2120
/// Encodes the alpha channel data.

src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs

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

54
using System.Buffers;
5+
using System.Diagnostics.CodeAnalysis;
66
using SixLabors.ImageSharp.Memory;
77

88
namespace SixLabors.ImageSharp.Formats.Webp.BitReader;
@@ -17,14 +17,15 @@ internal abstract class BitReaderBase : IDisposable
1717
/// <summary>
1818
/// Gets or sets the raw encoded image data.
1919
/// </summary>
20-
public IMemoryOwner<byte> Data { get; set; }
20+
public IMemoryOwner<byte>? Data { get; set; }
2121

2222
/// <summary>
2323
/// Copies the raw encoded image data from the stream into a byte array.
2424
/// </summary>
2525
/// <param name="input">The input stream.</param>
2626
/// <param name="bytesToRead">Number of bytes to read as indicated from the chunk size.</param>
2727
/// <param name="memoryAllocator">Used for allocating memory during reading data from the stream.</param>
28+
[MemberNotNull(nameof(Data))]
2829
protected void ReadImageDataFromStream(Stream input, int bytesToRead, MemoryAllocator memoryAllocator)
2930
{
3031
this.Data = memoryAllocator.Allocate<byte>(bytesToRead);

src/ImageSharp/Formats/Webp/BitReader/Vp8BitReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private void LoadNewBytes()
186186
{
187187
if (this.pos < this.bufferMax)
188188
{
189-
ulong inBits = BinaryPrimitives.ReadUInt64LittleEndian(this.Data.Memory.Span.Slice((int)this.pos, 8));
189+
ulong inBits = BinaryPrimitives.ReadUInt64LittleEndian(this.Data!.Memory.Span.Slice((int)this.pos, 8));
190190
this.pos += BitsCount >> 3;
191191
ulong bits = ByteSwap64(inBits);
192192
bits >>= 64 - BitsCount;
@@ -205,7 +205,7 @@ private void LoadFinalBytes()
205205
if (this.pos < this.bufferEnd)
206206
{
207207
this.bits += 8;
208-
this.value = this.Data.Memory.Span[(int)this.pos++] | (this.value << 8);
208+
this.value = this.Data!.Memory.Span[(int)this.pos++] | (this.value << 8);
209209
}
210210
else if (!this.eof)
211211
{

src/ImageSharp/Formats/Webp/BitReader/Vp8LBitReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public void FillBitWindow()
193193
[MethodImpl(InliningOptions.ShortMethod)]
194194
private void ShiftBytes()
195195
{
196-
System.Span<byte> dataSpan = this.Data.Memory.Span;
196+
System.Span<byte> dataSpan = this.Data!.Memory.Span;
197197
while (this.bitPos >= 8 && this.pos < this.len)
198198
{
199199
this.value >>= 8;

src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected static uint AlphaChunkSize(Span<byte> alphaBytes)
123123
/// <param name="stream">The stream to write to.</param>
124124
/// <param name="metadataBytes">The metadata profile's bytes.</param>
125125
/// <param name="chunkType">The chuck type to write.</param>
126-
protected void WriteMetadataProfile(Stream stream, byte[] metadataBytes, WebpChunkType chunkType)
126+
protected void WriteMetadataProfile(Stream stream, byte[]? metadataBytes, WebpChunkType chunkType)
127127
{
128128
DebugGuard.NotNull(metadataBytes, nameof(metadataBytes));
129129

@@ -207,7 +207,7 @@ protected void WriteColorProfile(Stream stream, byte[] iccProfileBytes)
207207
/// <param name="width">The width of the image.</param>
208208
/// <param name="height">The height of the image.</param>
209209
/// <param name="hasAlpha">Flag indicating, if a alpha channel is present.</param>
210-
protected void WriteVp8XHeader(Stream stream, ExifProfile exifProfile, XmpProfile xmpProfile, byte[] iccProfileBytes, uint width, uint height, bool hasAlpha)
210+
protected void WriteVp8XHeader(Stream stream, ExifProfile? exifProfile, XmpProfile? xmpProfile, byte[]? iccProfileBytes, uint width, uint height, bool hasAlpha)
211211
{
212212
if (width > MaxDimension || height > MaxDimension)
213213
{

src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs

Lines changed: 16 additions & 22 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.Binary;
65
using SixLabors.ImageSharp.Formats.Webp.Lossy;
@@ -58,7 +57,8 @@ internal class Vp8BitWriter : BitWriterBase
5857
/// Initializes a new instance of the <see cref="Vp8BitWriter"/> class.
5958
/// </summary>
6059
/// <param name="expectedSize">The expected size in bytes.</param>
61-
public Vp8BitWriter(int expectedSize)
60+
/// <param name="enc">The Vp8Encoder.</param>
61+
public Vp8BitWriter(int expectedSize, Vp8Encoder enc)
6262
: base(expectedSize)
6363
{
6464
this.range = 255 - 1;
@@ -67,15 +67,9 @@ public Vp8BitWriter(int expectedSize)
6767
this.nbBits = -8;
6868
this.pos = 0;
6969
this.maxPos = 0;
70-
}
7170

72-
/// <summary>
73-
/// Initializes a new instance of the <see cref="Vp8BitWriter"/> class.
74-
/// </summary>
75-
/// <param name="expectedSize">The expected size in bytes.</param>
76-
/// <param name="enc">The Vp8Encoder.</param>
77-
public Vp8BitWriter(int expectedSize, Vp8Encoder enc)
78-
: this(expectedSize) => this.enc = enc;
71+
this.enc = enc;
72+
}
7973

8074
/// <inheritdoc/>
8175
public override int NumBytes() => (int)this.pos;
@@ -414,32 +408,32 @@ private void Flush()
414408
/// <param name="alphaDataIsCompressed">Indicates, if the alpha data is compressed.</param>
415409
public void WriteEncodedImageToStream(
416410
Stream stream,
417-
ExifProfile exifProfile,
418-
XmpProfile xmpProfile,
419-
IccProfile iccProfile,
411+
ExifProfile? exifProfile,
412+
XmpProfile? xmpProfile,
413+
IccProfile? iccProfile,
420414
uint width,
421415
uint height,
422416
bool hasAlpha,
423417
Span<byte> alphaData,
424418
bool alphaDataIsCompressed)
425419
{
426420
bool isVp8X = false;
427-
byte[] exifBytes = null;
428-
byte[] xmpBytes = null;
429-
byte[] iccProfileBytes = null;
421+
byte[]? exifBytes = null;
422+
byte[]? xmpBytes = null;
423+
byte[]? iccProfileBytes = null;
430424
uint riffSize = 0;
431425
if (exifProfile != null)
432426
{
433427
isVp8X = true;
434428
exifBytes = exifProfile.ToByteArray();
435-
riffSize += MetadataChunkSize(exifBytes);
429+
riffSize += MetadataChunkSize(exifBytes!);
436430
}
437431

438432
if (xmpProfile != null)
439433
{
440434
isVp8X = true;
441435
xmpBytes = xmpProfile.Data;
442-
riffSize += MetadataChunkSize(xmpBytes);
436+
riffSize += MetadataChunkSize(xmpBytes!);
443437
}
444438

445439
if (iccProfile != null)
@@ -465,7 +459,7 @@ public void WriteEncodedImageToStream(
465459
int mbSize = this.enc.Mbw * this.enc.Mbh;
466460
int expectedSize = mbSize * 7 / 8;
467461

468-
var bitWriterPartZero = new Vp8BitWriter(expectedSize);
462+
Vp8BitWriter bitWriterPartZero = new(expectedSize, this.enc);
469463

470464
// Partition #0 with header and partition sizes.
471465
uint size0 = this.GeneratePartition0(bitWriterPartZero);
@@ -676,9 +670,9 @@ private void WriteWebpHeaders(
676670
bool isVp8X,
677671
uint width,
678672
uint height,
679-
ExifProfile exifProfile,
680-
XmpProfile xmpProfile,
681-
byte[] iccProfileBytes,
673+
ExifProfile? exifProfile,
674+
XmpProfile? xmpProfile,
675+
byte[]? iccProfileBytes,
682676
bool hasAlpha,
683677
Span<byte> alphaData,
684678
bool alphaDataIsCompressed)

src/ImageSharp/Formats/Webp/BitWriter/Vp8LBitWriter.cs

Lines changed: 6 additions & 7 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.Binary;
65
using SixLabors.ImageSharp.Formats.Webp.Lossless;
@@ -138,25 +137,25 @@ public override void Finish()
138137
/// <param name="width">The width of the image.</param>
139138
/// <param name="height">The height of the image.</param>
140139
/// <param name="hasAlpha">Flag indicating, if a alpha channel is present.</param>
141-
public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, XmpProfile xmpProfile, IccProfile iccProfile, uint width, uint height, bool hasAlpha)
140+
public void WriteEncodedImageToStream(Stream stream, ExifProfile? exifProfile, XmpProfile? xmpProfile, IccProfile? iccProfile, uint width, uint height, bool hasAlpha)
142141
{
143142
bool isVp8X = false;
144-
byte[] exifBytes = null;
145-
byte[] xmpBytes = null;
146-
byte[] iccBytes = null;
143+
byte[]? exifBytes = null;
144+
byte[]? xmpBytes = null;
145+
byte[]? iccBytes = null;
147146
uint riffSize = 0;
148147
if (exifProfile != null)
149148
{
150149
isVp8X = true;
151150
exifBytes = exifProfile.ToByteArray();
152-
riffSize += MetadataChunkSize(exifBytes);
151+
riffSize += MetadataChunkSize(exifBytes!);
153152
}
154153

155154
if (xmpProfile != null)
156155
{
157156
isVp8X = true;
158157
xmpBytes = xmpProfile.Data;
159-
riffSize += MetadataChunkSize(xmpBytes);
158+
riffSize += MetadataChunkSize(xmpBytes!);
160159
}
161160

162161
if (iccProfile != null)

0 commit comments

Comments
 (0)