Skip to content

Commit 85de14f

Browse files
committed
Added readonly modifiers to pixel formats
1 parent 72a820e commit 85de14f

28 files changed

Lines changed: 228 additions & 224 deletions

src/ImageSharp/PixelFormats/PixelImplementations/A8.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,23 @@ public struct A8 : IPixel<A8>, IPackedVector<byte>
5757
public static bool operator !=(A8 left, A8 right) => !left.Equals(right);
5858

5959
/// <inheritdoc />
60-
public PixelOperations<A8> CreatePixelOperations() => new PixelOperations<A8>();
60+
public readonly PixelOperations<A8> CreatePixelOperations() => new PixelOperations<A8>();
6161

6262
/// <inheritdoc/>
6363
[MethodImpl(InliningOptions.ShortMethod)]
6464
public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector);
6565

6666
/// <inheritdoc/>
6767
[MethodImpl(InliningOptions.ShortMethod)]
68-
public Vector4 ToScaledVector4() => this.ToVector4();
68+
public readonly Vector4 ToScaledVector4() => this.ToVector4();
6969

7070
/// <inheritdoc />
7171
[MethodImpl(InliningOptions.ShortMethod)]
7272
public void FromVector4(Vector4 vector) => this.PackedValue = Pack(vector.W);
7373

7474
/// <inheritdoc />
7575
[MethodImpl(InliningOptions.ShortMethod)]
76-
public Vector4 ToVector4() => new Vector4(0, 0, 0, this.PackedValue / 255F);
76+
public readonly Vector4 ToVector4() => new Vector4(0, 0, 0, this.PackedValue / 255F);
7777

7878
/// <inheritdoc/>
7979
[MethodImpl(InliningOptions.ShortMethod)]
@@ -136,25 +136,25 @@ public void ToRgba32(ref Rgba32 dest)
136136
/// </summary>
137137
/// <param name="obj">The object to compare.</param>
138138
/// <returns>True if the object is equal to the packed vector.</returns>
139-
public override bool Equals(object obj) => obj is A8 other && this.Equals(other);
139+
public override readonly bool Equals(object obj) => obj is A8 other && this.Equals(other);
140140

141141
/// <summary>
142142
/// Compares another A8 packed vector with the packed vector.
143143
/// </summary>
144144
/// <param name="other">The A8 packed vector to compare.</param>
145145
/// <returns>True if the packed vectors are equal.</returns>
146146
[MethodImpl(InliningOptions.ShortMethod)]
147-
public bool Equals(A8 other) => this.PackedValue.Equals(other.PackedValue);
147+
public readonly bool Equals(A8 other) => this.PackedValue.Equals(other.PackedValue);
148148

149149
/// <summary>
150150
/// Gets a string representation of the packed vector.
151151
/// </summary>
152152
/// <returns>A string representation of the packed vector.</returns>
153-
public override string ToString() => $"A8({this.PackedValue})";
153+
public override readonly string ToString() => $"A8({this.PackedValue})";
154154

155155
/// <inheritdoc />
156156
[MethodImpl(InliningOptions.ShortMethod)]
157-
public override int GetHashCode() => this.PackedValue.GetHashCode();
157+
public override readonly int GetHashCode() => this.PackedValue.GetHashCode();
158158

159159
/// <summary>
160160
/// Packs a <see cref="float"/> into a byte.

src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public Argb32(uint packed)
129129
public uint Argb
130130
{
131131
[MethodImpl(InliningOptions.ShortMethod)]
132-
get => Unsafe.As<Argb32, uint>(ref this);
132+
readonly get => Unsafe.As<Argb32, uint>(ref Unsafe.AsRef(this));
133133

134134
[MethodImpl(InliningOptions.ShortMethod)]
135135
set => Unsafe.As<Argb32, uint>(ref this) = value;
@@ -138,7 +138,10 @@ public uint Argb
138138
/// <inheritdoc/>
139139
public uint PackedValue
140140
{
141-
get => this.Argb;
141+
[MethodImpl(InliningOptions.ShortMethod)]
142+
readonly get => this.Argb;
143+
144+
[MethodImpl(InliningOptions.ShortMethod)]
142145
set => this.Argb = value;
143146
}
144147

@@ -181,23 +184,23 @@ public uint PackedValue
181184
public static bool operator !=(Argb32 left, Argb32 right) => !left.Equals(right);
182185

183186
/// <inheritdoc />
184-
public PixelOperations<Argb32> CreatePixelOperations() => new PixelOperations();
187+
public readonly PixelOperations<Argb32> CreatePixelOperations() => new PixelOperations();
185188

186189
/// <inheritdoc/>
187190
[MethodImpl(InliningOptions.ShortMethod)]
188191
public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector);
189192

190193
/// <inheritdoc/>
191194
[MethodImpl(InliningOptions.ShortMethod)]
192-
public Vector4 ToScaledVector4() => this.ToVector4();
195+
public readonly Vector4 ToScaledVector4() => this.ToVector4();
193196

194197
/// <inheritdoc/>
195198
[MethodImpl(InliningOptions.ShortMethod)]
196199
public void FromVector4(Vector4 vector) => this.Pack(ref vector);
197200

198201
/// <inheritdoc/>
199202
[MethodImpl(InliningOptions.ShortMethod)]
200-
public Vector4 ToVector4() => new Vector4(this.R, this.G, this.B, this.A) / MaxBytes;
203+
public readonly Vector4 ToVector4() => new Vector4(this.R, this.G, this.B, this.A) / MaxBytes;
201204

202205
/// <inheritdoc/>
203206
[MethodImpl(InliningOptions.ShortMethod)]
@@ -320,21 +323,21 @@ public void FromRgba64(Rgba64 source)
320323
}
321324

322325
/// <inheritdoc/>
323-
public override bool Equals(object obj) => obj is Argb32 argb32 && this.Equals(argb32);
326+
public override readonly bool Equals(object obj) => obj is Argb32 argb32 && this.Equals(argb32);
324327

325328
/// <inheritdoc/>
326329
[MethodImpl(InliningOptions.ShortMethod)]
327-
public bool Equals(Argb32 other) => this.Argb == other.Argb;
330+
public readonly bool Equals(Argb32 other) => this.Argb == other.Argb;
328331

329332
/// <summary>
330333
/// Gets a string representation of the packed vector.
331334
/// </summary>
332335
/// <returns>A string representation of the packed vector.</returns>
333-
public override string ToString() => $"Argb({this.A}, {this.R}, {this.G}, {this.B})";
336+
public override readonly string ToString() => $"Argb({this.A}, {this.R}, {this.G}, {this.B})";
334337

335338
/// <inheritdoc/>
336339
[MethodImpl(InliningOptions.ShortMethod)]
337-
public override int GetHashCode() => this.Argb.GetHashCode();
340+
public override readonly int GetHashCode() => this.Argb.GetHashCode();
338341

339342
/// <summary>
340343
/// Packs the four floats into a color.

src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ public Bgr24(byte r, byte g, byte b)
8989
public static bool operator !=(Bgr24 left, Bgr24 right) => !left.Equals(right);
9090

9191
/// <inheritdoc/>
92-
public PixelOperations<Bgr24> CreatePixelOperations() => new PixelOperations();
92+
public readonly PixelOperations<Bgr24> CreatePixelOperations() => new PixelOperations();
9393

9494
/// <inheritdoc/>
9595
[MethodImpl(InliningOptions.ShortMethod)]
9696
public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector);
9797

9898
/// <inheritdoc/>
9999
[MethodImpl(InliningOptions.ShortMethod)]
100-
public Vector4 ToScaledVector4() => this.ToVector4();
100+
public readonly Vector4 ToScaledVector4() => this.ToVector4();
101101

102102
/// <inheritdoc/>
103103
[MethodImpl(InliningOptions.ShortMethod)]
@@ -110,7 +110,7 @@ public void FromVector4(Vector4 vector)
110110

111111
/// <inheritdoc/>
112112
[MethodImpl(InliningOptions.ShortMethod)]
113-
public Vector4 ToVector4() => new Rgba32(this.R, this.G, this.B, byte.MaxValue).ToVector4();
113+
public readonly Vector4 ToVector4() => new Rgba32(this.R, this.G, this.B, byte.MaxValue).ToVector4();
114114

115115
/// <inheritdoc/>
116116
[MethodImpl(InliningOptions.ShortMethod)]
@@ -219,16 +219,16 @@ public void FromRgba64(Rgba64 source)
219219

220220
/// <inheritdoc/>
221221
[MethodImpl(InliningOptions.ShortMethod)]
222-
public bool Equals(Bgr24 other) => this.R.Equals(other.R) && this.G.Equals(other.G) && this.B.Equals(other.B);
222+
public readonly bool Equals(Bgr24 other) => this.R.Equals(other.R) && this.G.Equals(other.G) && this.B.Equals(other.B);
223223

224224
/// <inheritdoc/>
225-
public override bool Equals(object obj) => obj is Bgr24 other && this.Equals(other);
225+
public override readonly bool Equals(object obj) => obj is Bgr24 other && this.Equals(other);
226226

227227
/// <inheritdoc />
228-
public override string ToString() => $"Bgra({this.B}, {this.G}, {this.R})";
228+
public override readonly string ToString() => $"Bgra({this.B}, {this.G}, {this.R})";
229229

230230
/// <inheritdoc/>
231231
[MethodImpl(InliningOptions.ShortMethod)]
232-
public override int GetHashCode() => HashCode.Combine(this.R, this.B, this.G);
232+
public override readonly int GetHashCode() => HashCode.Combine(this.R, this.B, this.G);
233233
}
234234
}

src/ImageSharp/PixelFormats/PixelImplementations/Bgr565.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public Bgr565(float x, float y, float z)
6161
public static bool operator !=(Bgr565 left, Bgr565 right) => !left.Equals(right);
6262

6363
/// <inheritdoc />
64-
public PixelOperations<Bgr565> CreatePixelOperations() => new PixelOperations<Bgr565>();
64+
public readonly PixelOperations<Bgr565> CreatePixelOperations() => new PixelOperations<Bgr565>();
6565

6666
/// <inheritdoc/>
6767
[MethodImpl(InliningOptions.ShortMethod)]
6868
public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector);
6969

7070
/// <inheritdoc/>
7171
[MethodImpl(InliningOptions.ShortMethod)]
72-
public Vector4 ToScaledVector4() => this.ToVector4();
72+
public readonly Vector4 ToScaledVector4() => this.ToVector4();
7373

7474
/// <inheritdoc />
7575
[MethodImpl(InliningOptions.ShortMethod)]
@@ -81,7 +81,7 @@ public void FromVector4(Vector4 vector)
8181

8282
/// <inheritdoc />
8383
[MethodImpl(InliningOptions.ShortMethod)]
84-
public Vector4 ToVector4() => new Vector4(this.ToVector3(), 1F);
84+
public readonly Vector4 ToVector4() => new Vector4(this.ToVector3(), 1F);
8585

8686
/// <inheritdoc/>
8787
[MethodImpl(InliningOptions.ShortMethod)]
@@ -144,7 +144,7 @@ public void ToRgba32(ref Rgba32 dest)
144144
/// </summary>
145145
/// <returns>The <see cref="Vector3"/>.</returns>
146146
[MethodImpl(InliningOptions.ShortMethod)]
147-
public Vector3 ToVector3()
147+
public readonly Vector3 ToVector3()
148148
{
149149
return new Vector3(
150150
((this.PackedValue >> 11) & 0x1F) * (1F / 31F),
@@ -153,22 +153,22 @@ public Vector3 ToVector3()
153153
}
154154

155155
/// <inheritdoc />
156-
public override bool Equals(object obj) => obj is Bgr565 other && this.Equals(other);
156+
public override readonly bool Equals(object obj) => obj is Bgr565 other && this.Equals(other);
157157

158158
/// <inheritdoc />
159159
[MethodImpl(InliningOptions.ShortMethod)]
160-
public bool Equals(Bgr565 other) => this.PackedValue.Equals(other.PackedValue);
160+
public readonly bool Equals(Bgr565 other) => this.PackedValue.Equals(other.PackedValue);
161161

162162
/// <inheritdoc />
163-
public override string ToString()
163+
public override readonly string ToString()
164164
{
165165
var vector = this.ToVector3();
166166
return FormattableString.Invariant($"Bgr565({vector.Z:#0.##}, {vector.Y:#0.##}, {vector.X:#0.##})");
167167
}
168168

169169
/// <inheritdoc />
170170
[MethodImpl(InliningOptions.ShortMethod)]
171-
public override int GetHashCode() => this.PackedValue.GetHashCode();
171+
public override readonly int GetHashCode() => this.PackedValue.GetHashCode();
172172

173173
[MethodImpl(InliningOptions.ShortMethod)]
174174
private static ushort Pack(ref Vector3 vector)

src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public Bgra32(byte r, byte g, byte b, byte a)
8585
public uint Bgra
8686
{
8787
[MethodImpl(InliningOptions.ShortMethod)]
88-
get => Unsafe.As<Bgra32, uint>(ref this);
88+
readonly get => Unsafe.As<Bgra32, uint>(ref Unsafe.AsRef(this));
8989

9090
[MethodImpl(InliningOptions.ShortMethod)]
9191
set => Unsafe.As<Bgra32, uint>(ref this) = value;
@@ -94,7 +94,7 @@ public uint Bgra
9494
/// <inheritdoc/>
9595
public uint PackedValue
9696
{
97-
get => this.Bgra;
97+
readonly get => this.Bgra;
9898
set => this.Bgra = value;
9999
}
100100

@@ -137,23 +137,23 @@ public uint PackedValue
137137
public static bool operator !=(Bgra32 left, Bgra32 right) => !left.Equals(right);
138138

139139
/// <inheritdoc/>
140-
public PixelOperations<Bgra32> CreatePixelOperations() => new PixelOperations();
140+
public readonly PixelOperations<Bgra32> CreatePixelOperations() => new PixelOperations();
141141

142142
/// <inheritdoc/>
143143
[MethodImpl(InliningOptions.ShortMethod)]
144144
public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector);
145145

146146
/// <inheritdoc/>
147147
[MethodImpl(InliningOptions.ShortMethod)]
148-
public Vector4 ToScaledVector4() => this.ToVector4();
148+
public readonly Vector4 ToScaledVector4() => this.ToVector4();
149149

150150
/// <inheritdoc/>
151151
[MethodImpl(InliningOptions.ShortMethod)]
152152
public void FromVector4(Vector4 vector) => this.Pack(ref vector);
153153

154154
/// <inheritdoc/>
155155
[MethodImpl(InliningOptions.ShortMethod)]
156-
public Vector4 ToVector4() => new Vector4(this.R, this.G, this.B, this.A) / MaxBytes;
156+
public readonly Vector4 ToVector4() => new Vector4(this.R, this.G, this.B, this.A) / MaxBytes;
157157

158158
/// <inheritdoc/>
159159
[MethodImpl(InliningOptions.ShortMethod)]
@@ -276,16 +276,16 @@ public void FromRgba64(Rgba64 source)
276276
}
277277

278278
/// <inheritdoc/>
279-
public override bool Equals(object obj) => obj is Bgra32 other && this.Equals(other);
279+
public override readonly bool Equals(object obj) => obj is Bgra32 other && this.Equals(other);
280280

281281
/// <inheritdoc/>
282-
public bool Equals(Bgra32 other) => this.Bgra.Equals(other.Bgra);
282+
public readonly bool Equals(Bgra32 other) => this.Bgra.Equals(other.Bgra);
283283

284284
/// <inheritdoc/>
285-
public override int GetHashCode() => this.Bgra.GetHashCode();
285+
public override readonly int GetHashCode() => this.Bgra.GetHashCode();
286286

287287
/// <inheritdoc />
288-
public override string ToString() => $"Bgra32({this.B}, {this.G}, {this.R}, {this.A})";
288+
public override readonly string ToString() => $"Bgra32({this.B}, {this.G}, {this.R}, {this.A})";
289289

290290
/// <summary>
291291
/// Packs a <see cref="Vector4"/> into a color.

src/ImageSharp/PixelFormats/PixelImplementations/Bgra4444.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,23 @@ public Bgra4444(float x, float y, float z, float w)
5959
public static bool operator !=(Bgra4444 left, Bgra4444 right) => !left.Equals(right);
6060

6161
/// <inheritdoc />
62-
public PixelOperations<Bgra4444> CreatePixelOperations() => new PixelOperations<Bgra4444>();
62+
public readonly PixelOperations<Bgra4444> CreatePixelOperations() => new PixelOperations<Bgra4444>();
6363

6464
/// <inheritdoc/>
6565
[MethodImpl(InliningOptions.ShortMethod)]
6666
public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector);
6767

6868
/// <inheritdoc/>
6969
[MethodImpl(InliningOptions.ShortMethod)]
70-
public Vector4 ToScaledVector4() => this.ToVector4();
70+
public readonly Vector4 ToScaledVector4() => this.ToVector4();
7171

7272
/// <inheritdoc />
7373
[MethodImpl(InliningOptions.ShortMethod)]
7474
public void FromVector4(Vector4 vector) => this.PackedValue = Pack(ref vector);
7575

7676
/// <inheritdoc />
7777
[MethodImpl(InliningOptions.ShortMethod)]
78-
public Vector4 ToVector4()
78+
public readonly Vector4 ToVector4()
7979
{
8080
const float Max = 1 / 15F;
8181

@@ -142,22 +142,22 @@ public void ToRgba32(ref Rgba32 dest)
142142
public void FromRgba64(Rgba64 source) => this.FromScaledVector4(source.ToScaledVector4());
143143

144144
/// <inheritdoc />
145-
public override bool Equals(object obj) => obj is Bgra4444 other && this.Equals(other);
145+
public override readonly bool Equals(object obj) => obj is Bgra4444 other && this.Equals(other);
146146

147147
/// <inheritdoc />
148148
[MethodImpl(InliningOptions.ShortMethod)]
149-
public bool Equals(Bgra4444 other) => this.PackedValue.Equals(other.PackedValue);
149+
public readonly bool Equals(Bgra4444 other) => this.PackedValue.Equals(other.PackedValue);
150150

151151
/// <inheritdoc />
152-
public override string ToString()
152+
public override readonly string ToString()
153153
{
154154
var vector = this.ToVector4();
155155
return FormattableString.Invariant($"Bgra4444({vector.Z:#0.##}, {vector.Y:#0.##}, {vector.X:#0.##}, {vector.W:#0.##})");
156156
}
157157

158158
/// <inheritdoc />
159159
[MethodImpl(InliningOptions.ShortMethod)]
160-
public override int GetHashCode() => this.PackedValue.GetHashCode();
160+
public override readonly int GetHashCode() => this.PackedValue.GetHashCode();
161161

162162
[MethodImpl(InliningOptions.ShortMethod)]
163163
private static ushort Pack(ref Vector4 vector)

0 commit comments

Comments
 (0)