Skip to content

Commit 30a2dfa

Browse files
committed
Remove Nullable Disable from ICC Code
Removed the nullable disable from the icc Code (And one Tiff). Most parts could be solved relative easily. There are also some parts were the bang operator had to be used.
1 parent 0b34bca commit 30a2dfa

19 files changed

+208
-195
lines changed

src/ImageSharp/ColorProfiles/Icc/Calculators/CurveCalculator.cs

Lines changed: 2 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 SixLabors.ImageSharp.ColorProfiles.Icc.Calculators;
65
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
@@ -9,7 +8,7 @@ namespace SixLabors.ImageSharp.ColorProfiles.Conversion.Icc;
98

109
internal partial class CurveCalculator : ISingleCalculator
1110
{
12-
private readonly LutCalculator lutCalculator;
11+
private readonly LutCalculator? lutCalculator;
1312
private readonly float gamma;
1413
private readonly CalculationType type;
1514

@@ -41,7 +40,7 @@ public float Calculate(float value)
4140
{
4241
CalculationType.Identity => value,
4342
CalculationType.Gamma => MathF.Pow(value, this.gamma), // TODO: This could be optimized using a LUT. See SrgbCompanding
44-
CalculationType.Lut => this.lutCalculator.Calculate(value),
43+
CalculationType.Lut => this.lutCalculator!.Calculate(value),
4544
_ => throw new InvalidOperationException("Invalid calculation type"),
4645
};
4746
}

src/ImageSharp/ColorProfiles/Icc/Calculators/GrayTrcCalculator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ internal class GrayTrcCalculator : IVector4Calculator
1111
{
1212
private readonly TrcCalculator calculator;
1313

14-
public GrayTrcCalculator(IccTagDataEntry grayTrc, bool toPcs)
15-
=> this.calculator = new TrcCalculator(new IccTagDataEntry[] { grayTrc }, !toPcs);
14+
public GrayTrcCalculator(IccTagDataEntry? grayTrc, bool toPcs)
15+
=> this.calculator = new TrcCalculator([grayTrc], !toPcs);
1616

1717
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1818
public Vector4 Calculate(Vector4 value) => this.calculator.Calculate(value);

src/ImageSharp/ColorProfiles/Icc/Calculators/LutABCalculator.CalculationType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace SixLabors.ImageSharp.ColorProfiles.Conversion.Icc;
55

66
internal partial class LutABCalculator
77
{
8+
[Flags]
89
private enum CalculationType
910
{
1011
AtoB = 1 << 3,
Lines changed: 36 additions & 37 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.Numerics;
65
using SixLabors.ImageSharp.ColorProfiles.Icc.Calculators;
@@ -11,22 +10,22 @@ namespace SixLabors.ImageSharp.ColorProfiles.Conversion.Icc;
1110
internal partial class LutABCalculator : IVector4Calculator
1211
{
1312
private CalculationType type;
14-
private TrcCalculator curveACalculator;
15-
private TrcCalculator curveBCalculator;
16-
private TrcCalculator curveMCalculator;
17-
private MatrixCalculator matrixCalculator;
18-
private ClutCalculator clutCalculator;
13+
private TrcCalculator? curveACalculator;
14+
private TrcCalculator? curveBCalculator;
15+
private TrcCalculator? curveMCalculator;
16+
private MatrixCalculator? matrixCalculator;
17+
private ClutCalculator? clutCalculator;
1918

2019
public LutABCalculator(IccLutAToBTagDataEntry entry)
2120
{
22-
Guard.NotNull(entry, nameof(entry));
21+
Guard.NotNull(entry);
2322
this.Init(entry.CurveA, entry.CurveB, entry.CurveM, entry.Matrix3x1, entry.Matrix3x3, entry.ClutValues);
2423
this.type |= CalculationType.AtoB;
2524
}
2625

2726
public LutABCalculator(IccLutBToATagDataEntry entry)
2827
{
29-
Guard.NotNull(entry, nameof(entry));
28+
Guard.NotNull(entry);
3029
this.Init(entry.CurveA, entry.CurveB, entry.CurveM, entry.Matrix3x1, entry.Matrix3x3, entry.ClutValues);
3130
this.type |= CalculationType.BtoA;
3231
}
@@ -36,49 +35,49 @@ public Vector4 Calculate(Vector4 value)
3635
switch (this.type)
3736
{
3837
case CalculationType.Full | CalculationType.AtoB:
39-
value = this.curveACalculator.Calculate(value);
40-
value = this.clutCalculator.Calculate(value);
41-
value = this.curveMCalculator.Calculate(value);
42-
value = this.matrixCalculator.Calculate(value);
43-
return this.curveBCalculator.Calculate(value);
38+
value = this.curveACalculator!.Calculate(value);
39+
value = this.clutCalculator!.Calculate(value);
40+
value = this.curveMCalculator!.Calculate(value);
41+
value = this.matrixCalculator!.Calculate(value);
42+
return this.curveBCalculator!.Calculate(value);
4443

4544
case CalculationType.Full | CalculationType.BtoA:
46-
value = this.curveBCalculator.Calculate(value);
47-
value = this.matrixCalculator.Calculate(value);
48-
value = this.curveMCalculator.Calculate(value);
49-
value = this.clutCalculator.Calculate(value);
50-
return this.curveACalculator.Calculate(value);
45+
value = this.curveBCalculator!.Calculate(value);
46+
value = this.matrixCalculator!.Calculate(value);
47+
value = this.curveMCalculator!.Calculate(value);
48+
value = this.clutCalculator!.Calculate(value);
49+
return this.curveACalculator!.Calculate(value);
5150

5251
case CalculationType.CurveClut | CalculationType.AtoB:
53-
value = this.curveACalculator.Calculate(value);
54-
value = this.clutCalculator.Calculate(value);
55-
return this.curveBCalculator.Calculate(value);
52+
value = this.curveACalculator!.Calculate(value);
53+
value = this.clutCalculator!.Calculate(value);
54+
return this.curveBCalculator!.Calculate(value);
5655

5756
case CalculationType.CurveClut | CalculationType.BtoA:
58-
value = this.curveBCalculator.Calculate(value);
59-
value = this.clutCalculator.Calculate(value);
60-
return this.curveACalculator.Calculate(value);
57+
value = this.curveBCalculator!.Calculate(value);
58+
value = this.clutCalculator!.Calculate(value);
59+
return this.curveACalculator!.Calculate(value);
6160

6261
case CalculationType.CurveMatrix | CalculationType.AtoB:
63-
value = this.curveMCalculator.Calculate(value);
64-
value = this.matrixCalculator.Calculate(value);
65-
return this.curveBCalculator.Calculate(value);
62+
value = this.curveMCalculator!.Calculate(value);
63+
value = this.matrixCalculator!.Calculate(value);
64+
return this.curveBCalculator!.Calculate(value);
6665

6766
case CalculationType.CurveMatrix | CalculationType.BtoA:
68-
value = this.curveBCalculator.Calculate(value);
69-
value = this.matrixCalculator.Calculate(value);
70-
return this.curveMCalculator.Calculate(value);
67+
value = this.curveBCalculator!.Calculate(value);
68+
value = this.matrixCalculator!.Calculate(value);
69+
return this.curveMCalculator!.Calculate(value);
7170

7271
case CalculationType.SingleCurve | CalculationType.AtoB:
7372
case CalculationType.SingleCurve | CalculationType.BtoA:
74-
return this.curveBCalculator.Calculate(value);
73+
return this.curveBCalculator!.Calculate(value);
7574

7675
default:
7776
throw new InvalidOperationException("Invalid calculation type");
7877
}
7978
}
8079

81-
private void Init(IccTagDataEntry[] curveA, IccTagDataEntry[] curveB, IccTagDataEntry[] curveM, Vector3? matrix3x1, Matrix4x4? matrix3x3, IccClut clut)
80+
private void Init(IccTagDataEntry[]? curveA, IccTagDataEntry[]? curveB, IccTagDataEntry[]? curveM, Vector3? matrix3x1, Matrix4x4? matrix3x3, IccClut? clut)
8281
{
8382
bool hasACurve = curveA != null;
8483
bool hasBCurve = curveB != null;
@@ -109,27 +108,27 @@ private void Init(IccTagDataEntry[] curveA, IccTagDataEntry[] curveB, IccTagData
109108

110109
if (hasACurve)
111110
{
112-
this.curveACalculator = new TrcCalculator(curveA, false);
111+
this.curveACalculator = new TrcCalculator(curveA!, false);
113112
}
114113

115114
if (hasBCurve)
116115
{
117-
this.curveBCalculator = new TrcCalculator(curveB, false);
116+
this.curveBCalculator = new TrcCalculator(curveB!, false);
118117
}
119118

120119
if (hasMCurve)
121120
{
122-
this.curveMCalculator = new TrcCalculator(curveM, false);
121+
this.curveMCalculator = new TrcCalculator(curveM!, false);
123122
}
124123

125124
if (hasMatrix)
126125
{
127-
this.matrixCalculator = new MatrixCalculator(matrix3x3.Value, matrix3x1.Value);
126+
this.matrixCalculator = new MatrixCalculator(matrix3x3!.Value, matrix3x1!.Value);
128127
}
129128

130129
if (hasClut)
131130
{
132-
this.clutCalculator = new ClutCalculator(clut);
131+
this.clutCalculator = new ClutCalculator(clut!);
133132
}
134133
}
135134
}

src/ImageSharp/ColorProfiles/Icc/Calculators/LutEntryCalculator.cs

Lines changed: 2 additions & 1 deletion
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.Numerics;
66
using System.Runtime.CompilerServices;
77
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
@@ -57,6 +57,7 @@ private static Vector4 CalculateLut(LutCalculator[] lut, Vector4 value)
5757
return value;
5858
}
5959

60+
[MemberNotNull(nameof(this.inputCurve), nameof(this.outputCurve), nameof(this.clutCalculator), nameof(this.matrix))]
6061
private void Init(IccLut[] inputCurve, IccLut[] outputCurve, IccClut clut, Matrix4x4 matrix)
6162
{
6263
this.inputCurve = InitLut(inputCurve);

src/ImageSharp/ColorProfiles/Icc/Calculators/TrcCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal class TrcCalculator : IVector4Calculator
1212
{
1313
private readonly ISingleCalculator[] calculators;
1414

15-
public TrcCalculator(IccTagDataEntry[] entries, bool inverted)
15+
public TrcCalculator(IccTagDataEntry?[] entries, bool inverted)
1616
{
1717
Guard.NotNull(entries, nameof(entries));
1818

src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs

Lines changed: 2 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 SixLabors.ImageSharp.Metadata.Profiles.Icc;
65

@@ -146,10 +145,10 @@ private static ConversionMethod CheckMethod2(IccProfile profile)
146145
private static bool HasTag(IccProfile profile, IccProfileTag tag)
147146
=> profile.Entries.Any(t => t.TagSignature == tag);
148147

149-
private static IccTagDataEntry GetTag(IccProfile profile, IccProfileTag tag)
148+
private static IccTagDataEntry? GetTag(IccProfile profile, IccProfileTag tag)
150149
=> Array.Find(profile.Entries, t => t.TagSignature == tag);
151150

152-
private static T GetTag<T>(IccProfile profile, IccProfileTag tag)
151+
private static T? GetTag<T>(IccProfile profile, IccProfileTag tag)
153152
where T : IccTagDataEntry
154153
=> profile.Entries.OfType<T>().FirstOrDefault(t => t.TagSignature == tag);
155154
}

src/ImageSharp/ColorProfiles/Icc/IccConverterbase.Conversions.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using SixLabors.ImageSharp.ColorProfiles.Icc.Calculators;
56
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
67

@@ -24,6 +25,7 @@ internal abstract partial class IccConverterBase
2425
/// <param name="toPcs">True if the conversion is to the Profile Connection Space.</param>
2526
/// <param name="renderingIntent">The wanted rendering intent. Can be ignored if not available.</param>
2627
/// <exception cref="InvalidIccProfileException">Invalid conversion method.</exception>
28+
[MemberNotNull(nameof(this.calculator))]
2729
protected void Init(IccProfile profile, bool toPcs, IccRenderingIntent renderingIntent)
2830
=> this.calculator = GetConversionMethod(profile, renderingIntent) switch
2931
{
@@ -73,13 +75,13 @@ private static IVector4Calculator InitD(IccProfile profile, IccProfileTag tag)
7375

7476
private static ColorTrcCalculator InitColorTrc(IccProfile profile, bool toPcs)
7577
{
76-
IccXyzTagDataEntry redMatrixColumn = GetTag<IccXyzTagDataEntry>(profile, IccProfileTag.RedMatrixColumn);
77-
IccXyzTagDataEntry greenMatrixColumn = GetTag<IccXyzTagDataEntry>(profile, IccProfileTag.GreenMatrixColumn);
78-
IccXyzTagDataEntry blueMatrixColumn = GetTag<IccXyzTagDataEntry>(profile, IccProfileTag.BlueMatrixColumn);
78+
IccXyzTagDataEntry? redMatrixColumn = GetTag<IccXyzTagDataEntry>(profile, IccProfileTag.RedMatrixColumn);
79+
IccXyzTagDataEntry? greenMatrixColumn = GetTag<IccXyzTagDataEntry>(profile, IccProfileTag.GreenMatrixColumn);
80+
IccXyzTagDataEntry? blueMatrixColumn = GetTag<IccXyzTagDataEntry>(profile, IccProfileTag.BlueMatrixColumn);
7981

80-
IccTagDataEntry redTrc = GetTag(profile, IccProfileTag.RedTrc);
81-
IccTagDataEntry greenTrc = GetTag(profile, IccProfileTag.GreenTrc);
82-
IccTagDataEntry blueTrc = GetTag(profile, IccProfileTag.BlueTrc);
82+
IccTagDataEntry? redTrc = GetTag(profile, IccProfileTag.RedTrc);
83+
IccTagDataEntry? greenTrc = GetTag(profile, IccProfileTag.GreenTrc);
84+
IccTagDataEntry? blueTrc = GetTag(profile, IccProfileTag.BlueTrc);
8385

8486
if (redMatrixColumn == null ||
8587
greenMatrixColumn == null ||
@@ -103,7 +105,7 @@ private static ColorTrcCalculator InitColorTrc(IccProfile profile, bool toPcs)
103105

104106
private static GrayTrcCalculator InitGrayTrc(IccProfile profile, bool toPcs)
105107
{
106-
IccTagDataEntry entry = GetTag(profile, IccProfileTag.GrayTrc);
108+
IccTagDataEntry? entry = GetTag(profile, IccProfileTag.GrayTrc);
107109
return new GrayTrcCalculator(entry, toPcs);
108110
}
109111
}

src/ImageSharp/ColorProfiles/Icc/IccConverterbase.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.Numerics;
65
using System.Runtime.CompilerServices;
@@ -18,7 +17,7 @@ internal abstract partial class IccConverterBase
1817
/// </summary>
1918
/// <param name="profile">The ICC profile to use for the conversions</param>
2019
/// <param name="toPcs">True if the conversion is to the profile connection space (PCS); False if the conversion is to the data space</param>
21-
protected IccConverterBase(IccProfile profile, bool toPcs)
20+
protected IccConverterBase(IccProfile? profile, bool toPcs)
2221
{
2322
Guard.NotNull(profile, nameof(profile));
2423
this.Init(profile, toPcs, profile.Header.RenderingIntent);

src/ImageSharp/Formats/Tiff/TiffDecoderMetadataCreator.cs

Lines changed: 11 additions & 10 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 SixLabors.ImageSharp.Common.Helpers;
65
using SixLabors.ImageSharp.Metadata;
@@ -31,12 +30,14 @@ public static ImageMetadata Create(List<ImageFrameMetadata> frames, bool ignoreM
3130
// ICC profile data has already been resolved in the frame metadata,
3231
// as it is required for color conversion.
3332
ImageFrameMetadata frameMetaData = frames[i];
34-
if (TryGetIptc(frameMetaData.ExifProfile.Values, out byte[] iptcBytes))
33+
DebugGuard.NotNull(frameMetaData.ExifProfile);
34+
35+
if (TryGetIptc(frameMetaData.ExifProfile.Values, out byte[]? iptcBytes))
3536
{
3637
frameMetaData.IptcProfile = new IptcProfile(iptcBytes);
3738
}
3839

39-
if (frameMetaData.ExifProfile.TryGetValue(ExifTag.XMP, out IExifValue<byte[]> xmpProfileBytes))
40+
if (frameMetaData.ExifProfile.TryGetValue(ExifTag.XMP, out IExifValue<byte[]>? xmpProfileBytes))
4041
{
4142
frameMetaData.XmpProfile = new XmpProfile(xmpProfileBytes.Value);
4243
}
@@ -65,7 +66,7 @@ private static ImageMetadata Create(ByteOrder byteOrder, bool isBigTiff, ImageFr
6566
return imageMetaData;
6667
}
6768

68-
private static void SetResolution(ImageMetadata imageMetaData, ExifProfile exifProfile)
69+
private static void SetResolution(ImageMetadata imageMetaData, ExifProfile? exifProfile)
6970
{
7071
imageMetaData.ResolutionUnits = exifProfile != null ? UnitConverter.ExifProfileToResolutionUnit(exifProfile) : PixelResolutionUnit.PixelsPerInch;
7172

@@ -74,34 +75,34 @@ private static void SetResolution(ImageMetadata imageMetaData, ExifProfile exifP
7475
return;
7576
}
7677

77-
if (exifProfile.TryGetValue(ExifTag.XResolution, out IExifValue<Rational> horizontalResolution))
78+
if (exifProfile.TryGetValue(ExifTag.XResolution, out IExifValue<Rational>? horizontalResolution))
7879
{
7980
imageMetaData.HorizontalResolution = horizontalResolution.Value.ToDouble();
8081
}
8182

82-
if (exifProfile.TryGetValue(ExifTag.YResolution, out IExifValue<Rational> verticalResolution))
83+
if (exifProfile.TryGetValue(ExifTag.YResolution, out IExifValue<Rational>? verticalResolution))
8384
{
8485
imageMetaData.VerticalResolution = verticalResolution.Value.ToDouble();
8586
}
8687
}
8788

88-
private static bool TryGetIptc(IReadOnlyList<IExifValue> exifValues, out byte[] iptcBytes)
89+
private static bool TryGetIptc(IReadOnlyList<IExifValue> exifValues, out byte[]? iptcBytes)
8990
{
9091
iptcBytes = null;
91-
IExifValue iptc = exifValues.FirstOrDefault(f => f.Tag == ExifTag.IPTC);
92+
IExifValue? iptc = exifValues.FirstOrDefault(f => f.Tag == ExifTag.IPTC);
9293

9394
if (iptc != null)
9495
{
9596
if (iptc.DataType is ExifDataType.Byte or ExifDataType.Undefined)
9697
{
97-
iptcBytes = (byte[])iptc.GetValue();
98+
iptcBytes = (byte[]?)iptc.GetValue();
9899
return true;
99100
}
100101

101102
// Some Encoders write the data type of IPTC as long.
102103
if (iptc.DataType == ExifDataType.Long)
103104
{
104-
uint[] iptcValues = (uint[])iptc.GetValue();
105+
uint[] iptcValues = (uint[])iptc.GetValue()!;
105106
iptcBytes = new byte[iptcValues.Length * 4];
106107
Buffer.BlockCopy(iptcValues, 0, iptcBytes, 0, iptcValues.Length * 4);
107108
if (iptcBytes[0] == 0x1c)

0 commit comments

Comments
 (0)