Skip to content

Commit b52ef56

Browse files
committed
Promote PixelTypeInfo to TPixel
Fixes #2534
1 parent 290906a commit b52ef56

File tree

70 files changed

+207
-468
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+207
-468
lines changed

src/ImageSharp/Formats/PixelTypeInfo.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace SixLabors.ImageSharp.Formats;
1212
/// <summary>
1313
/// Contains information about the pixels that make up an images visual data.
1414
/// </summary>
15-
public class PixelTypeInfo
15+
public readonly struct PixelTypeInfo
1616
{
1717
/// <summary>
1818
/// Initializes a new instance of the <see cref="PixelTypeInfo"/> class.
@@ -21,33 +21,25 @@ public class PixelTypeInfo
2121
public PixelTypeInfo(int bitsPerPixel)
2222
=> this.BitsPerPixel = bitsPerPixel;
2323

24-
/// <summary>
25-
/// Initializes a new instance of the <see cref="PixelTypeInfo"/> class.
26-
/// </summary>
27-
/// <param name="bitsPerPixel">Color depth, in number of bits per pixel.</param>
28-
/// <param name="alpha">The pixel alpha transparency behavior.</param>
29-
public PixelTypeInfo(int bitsPerPixel, PixelAlphaRepresentation alpha)
30-
{
31-
this.BitsPerPixel = bitsPerPixel;
32-
this.AlphaRepresentation = alpha;
33-
}
34-
3524
/// <summary>
3625
/// Gets color depth, in number of bits per pixel.
3726
/// </summary>
38-
public int BitsPerPixel { get; }
27+
public int BitsPerPixel { get; init; }
28+
29+
public byte ComponentCount { get; init; }
3930

4031
/// <summary>
4132
/// Gets the pixel alpha transparency behavior.
4233
/// <see langword="null"/> means unknown, unspecified.
4334
/// </summary>
44-
public PixelAlphaRepresentation? AlphaRepresentation { get; }
45-
46-
internal static PixelTypeInfo Create<TPixel>()
47-
where TPixel : unmanaged, IPixel<TPixel>
48-
=> new(Unsafe.SizeOf<TPixel>() * 8);
35+
public PixelAlphaRepresentation? AlphaRepresentation { get; init; }
4936

50-
internal static PixelTypeInfo Create<TPixel>(PixelAlphaRepresentation alpha)
37+
internal static PixelTypeInfo Create<TPixel>(byte componentCount, PixelAlphaRepresentation pixelAlphaRepresentation)
5138
where TPixel : unmanaged, IPixel<TPixel>
52-
=> new(Unsafe.SizeOf<TPixel>() * 8, alpha);
39+
=> new()
40+
{
41+
BitsPerPixel = Unsafe.SizeOf<TPixel>() * 8,
42+
ComponentCount = componentCount,
43+
AlphaRepresentation = pixelAlphaRepresentation
44+
};
5345
}

src/ImageSharp/Image{TPixel}.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public Image(int width, int height)
7878
/// <param name="height">The height of the image in pixels.</param>
7979
/// <param name="metadata">The images metadata.</param>
8080
internal Image(Configuration configuration, int width, int height, ImageMetadata? metadata)
81-
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata ?? new(), width, height)
81+
: base(configuration, TPixel.GetPixelTypeInfo(), metadata ?? new(), width, height)
8282
=> this.frames = new ImageFrameCollection<TPixel>(this, width, height, default(TPixel));
8383

8484
/// <summary>
@@ -111,7 +111,7 @@ internal Image(
111111
int width,
112112
int height,
113113
ImageMetadata metadata)
114-
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata, width, height)
114+
: base(configuration, TPixel.GetPixelTypeInfo(), metadata, width, height)
115115
=> this.frames = new ImageFrameCollection<TPixel>(this, width, height, memoryGroup);
116116

117117
/// <summary>
@@ -129,7 +129,7 @@ internal Image(
129129
int height,
130130
TPixel backgroundColor,
131131
ImageMetadata? metadata)
132-
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata ?? new(), width, height)
132+
: base(configuration, TPixel.GetPixelTypeInfo(), metadata ?? new(), width, height)
133133
=> this.frames = new ImageFrameCollection<TPixel>(this, width, height, backgroundColor);
134134

135135
/// <summary>
@@ -140,7 +140,7 @@ internal Image(
140140
/// <param name="metadata">The images metadata.</param>
141141
/// <param name="frames">The frames that will be owned by this image instance.</param>
142142
internal Image(Configuration configuration, ImageMetadata metadata, IEnumerable<ImageFrame<TPixel>> frames)
143-
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata, ValidateFramesAndGetSize(frames))
143+
: base(configuration, TPixel.GetPixelTypeInfo(), metadata, ValidateFramesAndGetSize(frames))
144144
=> this.frames = new ImageFrameCollection<TPixel>(this, frames);
145145

146146
/// <inheritdoc />

src/ImageSharp/PixelFormats/IPixel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Six Labors Split License.
33

44
using System.Numerics;
5+
using SixLabors.ImageSharp.Formats;
56

67
namespace SixLabors.ImageSharp.PixelFormats;
78

@@ -14,6 +15,8 @@ namespace SixLabors.ImageSharp.PixelFormats;
1415
public interface IPixel<TSelf> : IPixel, IEquatable<TSelf>
1516
where TSelf : unmanaged, IPixel<TSelf>
1617
{
18+
static abstract PixelTypeInfo GetPixelTypeInfo();
19+
1720
/// <summary>
1821
/// Creates a <see cref="PixelOperations{TPixel}"/> instance for this pixel type.
1922
/// This method is not intended to be consumed directly. Use <see cref="PixelOperations{TPixel}.Instance"/> instead.

src/ImageSharp/PixelFormats/PixelImplementations/A8.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Numerics;
55
using System.Runtime.CompilerServices;
6+
using SixLabors.ImageSharp.Formats;
67

78
namespace SixLabors.ImageSharp.PixelFormats;
89

@@ -55,6 +56,8 @@ public partial struct A8 : IPixel<A8>, IPackedVector<byte>
5556
[MethodImpl(InliningOptions.ShortMethod)]
5657
public static bool operator !=(A8 left, A8 right) => !left.Equals(right);
5758

59+
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<A8>(1, PixelAlphaRepresentation.Unassociated);
60+
5861
/// <inheritdoc />
5962
public readonly PixelOperations<A8> CreatePixelOperations() => new PixelOperations();
6063

src/ImageSharp/PixelFormats/PixelImplementations/Abgr32.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Numerics;
55
using System.Runtime.CompilerServices;
66
using System.Runtime.InteropServices;
7+
using SixLabors.ImageSharp.Formats;
78

89
namespace SixLabors.ImageSharp.PixelFormats;
910

@@ -183,6 +184,8 @@ public uint PackedValue
183184
[MethodImpl(InliningOptions.ShortMethod)]
184185
public static bool operator !=(Abgr32 left, Abgr32 right) => !left.Equals(right);
185186

187+
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Abgr32>(4, PixelAlphaRepresentation.Unassociated);
188+
186189
/// <inheritdoc />
187190
public readonly PixelOperations<Abgr32> CreatePixelOperations() => new PixelOperations();
188191

src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Numerics;
55
using System.Runtime.CompilerServices;
66
using System.Runtime.InteropServices;
7+
using SixLabors.ImageSharp.Formats;
78

89
namespace SixLabors.ImageSharp.PixelFormats;
910

@@ -183,6 +184,8 @@ public uint PackedValue
183184
[MethodImpl(InliningOptions.ShortMethod)]
184185
public static bool operator !=(Argb32 left, Argb32 right) => !left.Equals(right);
185186

187+
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Argb32>(4, PixelAlphaRepresentation.Unassociated);
188+
186189
/// <inheritdoc />
187190
public readonly PixelOperations<Argb32> CreatePixelOperations() => new PixelOperations();
188191

src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Numerics;
55
using System.Runtime.CompilerServices;
66
using System.Runtime.InteropServices;
7+
using SixLabors.ImageSharp.Formats;
78

89
namespace SixLabors.ImageSharp.PixelFormats;
910

@@ -87,6 +88,8 @@ public Bgr24(byte r, byte g, byte b)
8788
[MethodImpl(InliningOptions.ShortMethod)]
8889
public static bool operator !=(Bgr24 left, Bgr24 right) => !left.Equals(right);
8990

91+
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Bgr24>(3, PixelAlphaRepresentation.None);
92+
9093
/// <inheritdoc/>
9194
public readonly PixelOperations<Bgr24> CreatePixelOperations() => new PixelOperations();
9295

src/ImageSharp/PixelFormats/PixelImplementations/Bgr565.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Numerics;
55
using System.Runtime.CompilerServices;
6+
using SixLabors.ImageSharp.Formats;
67

78
namespace SixLabors.ImageSharp.PixelFormats;
89

@@ -59,6 +60,8 @@ public Bgr565(float x, float y, float z)
5960
[MethodImpl(InliningOptions.ShortMethod)]
6061
public static bool operator !=(Bgr565 left, Bgr565 right) => !left.Equals(right);
6162

63+
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Bgr565>(3, PixelAlphaRepresentation.None);
64+
6265
/// <inheritdoc />
6366
public readonly PixelOperations<Bgr565> CreatePixelOperations() => new PixelOperations();
6467

src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Numerics;
55
using System.Runtime.CompilerServices;
66
using System.Runtime.InteropServices;
7+
using SixLabors.ImageSharp.Formats;
78

89
namespace SixLabors.ImageSharp.PixelFormats;
910

@@ -136,6 +137,8 @@ public uint PackedValue
136137
[MethodImpl(InliningOptions.ShortMethod)]
137138
public static bool operator !=(Bgra32 left, Bgra32 right) => !left.Equals(right);
138139

140+
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Bgra32>(4, PixelAlphaRepresentation.Unassociated);
141+
139142
/// <inheritdoc/>
140143
public readonly PixelOperations<Bgra32> CreatePixelOperations() => new PixelOperations();
141144

src/ImageSharp/PixelFormats/PixelImplementations/Bgra4444.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Numerics;
55
using System.Runtime.CompilerServices;
6+
using SixLabors.ImageSharp.Formats;
67

78
namespace SixLabors.ImageSharp.PixelFormats;
89

@@ -57,6 +58,8 @@ public Bgra4444(float x, float y, float z, float w)
5758
[MethodImpl(InliningOptions.ShortMethod)]
5859
public static bool operator !=(Bgra4444 left, Bgra4444 right) => !left.Equals(right);
5960

61+
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Bgra4444>(4, PixelAlphaRepresentation.Unassociated);
62+
6063
/// <inheritdoc />
6164
public readonly PixelOperations<Bgra4444> CreatePixelOperations() => new PixelOperations();
6265

0 commit comments

Comments
 (0)