Skip to content

Commit a9e2b4c

Browse files
Merge pull request #1420 from SixLabors/js/pixel-type-info
Add new PixelAlphaRepresentation property and implement for TPixel types
2 parents f77801e + 8a065ac commit a9e2b4c

File tree

91 files changed

+2304
-671
lines changed

Some content is hidden

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

91 files changed

+2304
-671
lines changed

src/ImageSharp/Formats/PixelTypeInfo.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System.Runtime.CompilerServices;
5-
65
using SixLabors.ImageSharp.PixelFormats;
76

87
namespace SixLabors.ImageSharp.Formats
@@ -16,18 +15,32 @@ public class PixelTypeInfo
1615
/// Initializes a new instance of the <see cref="PixelTypeInfo"/> class.
1716
/// </summary>
1817
/// <param name="bitsPerPixel">Color depth, in number of bits per pixel.</param>
19-
internal PixelTypeInfo(int bitsPerPixel)
18+
/// <param name="alpha">Tthe pixel alpha transparency behavior.</param>
19+
internal PixelTypeInfo(int bitsPerPixel, PixelAlphaRepresentation? alpha = null)
2020
{
2121
this.BitsPerPixel = bitsPerPixel;
22+
this.AlphaRepresentation = alpha;
2223
}
2324

2425
/// <summary>
2526
/// Gets color depth, in number of bits per pixel.
2627
/// </summary>
2728
public int BitsPerPixel { get; }
2829

30+
/// <summary>
31+
/// Gets the pixel alpha transparency behavior.
32+
/// <see langword="null"/> means unknown, unspecified.
33+
/// </summary>
34+
public PixelAlphaRepresentation? AlphaRepresentation { get; }
35+
2936
internal static PixelTypeInfo Create<TPixel>()
3037
where TPixel : unmanaged, IPixel<TPixel> =>
3138
new PixelTypeInfo(Unsafe.SizeOf<TPixel>() * 8);
39+
40+
internal static PixelTypeInfo Create<TPixel>(PixelAlphaRepresentation alpha)
41+
where TPixel : unmanaged, IPixel<TPixel>
42+
{
43+
return new PixelTypeInfo(Unsafe.SizeOf<TPixel>() * 8, alpha);
44+
}
3245
}
3346
}

src/ImageSharp/ImageSharp.csproj

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,62 +62,62 @@
6262
<AutoGen>True</AutoGen>
6363
<DependentUpon>PixelOperations{TPixel}.Generated.tt</DependentUpon>
6464
</Compile>
65-
<Compile Update="PixelFormats\PixelImplementations\Generated\Argb32.PixelOperations.Generated.cs">
65+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Argb32.PixelOperations.Generated.cs">
6666
<DesignTime>True</DesignTime>
6767
<AutoGen>True</AutoGen>
6868
<DependentUpon>Argb32.PixelOperations.Generated.tt</DependentUpon>
6969
</Compile>
70-
<Compile Update="PixelFormats\PixelImplementations\Generated\Bgr24.PixelOperations.Generated.cs">
70+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Bgr24.PixelOperations.Generated.cs">
7171
<DesignTime>True</DesignTime>
7272
<AutoGen>True</AutoGen>
7373
<DependentUpon>Bgr24.PixelOperations.Generated.tt</DependentUpon>
7474
</Compile>
75-
<Compile Update="PixelFormats\PixelImplementations\Generated\Bgra32.PixelOperations.Generated.cs">
75+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Bgra32.PixelOperations.Generated.cs">
7676
<DesignTime>True</DesignTime>
7777
<AutoGen>True</AutoGen>
7878
<DependentUpon>Bgra32.PixelOperations.Generated.tt</DependentUpon>
7979
</Compile>
80-
<Compile Update="PixelFormats\PixelImplementations\Generated\Bgra5551.PixelOperations.Generated.cs">
80+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Bgra5551.PixelOperations.Generated.cs">
8181
<DesignTime>True</DesignTime>
8282
<AutoGen>True</AutoGen>
8383
<DependentUpon>Bgra5551.PixelOperations.Generated.tt</DependentUpon>
8484
</Compile>
85-
<Compile Update="PixelFormats\PixelImplementations\Generated\L8.PixelOperations.Generated.cs">
85+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\L16.PixelOperations.Generated.cs">
8686
<DesignTime>True</DesignTime>
8787
<AutoGen>True</AutoGen>
88-
<DependentUpon>L8.PixelOperations.Generated.tt</DependentUpon>
88+
<DependentUpon>L16.PixelOperations.Generated.tt</DependentUpon>
8989
</Compile>
90-
<Compile Update="PixelFormats\PixelImplementations\Generated\L16.PixelOperations.Generated.cs">
90+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\L8.PixelOperations.Generated.cs">
9191
<DesignTime>True</DesignTime>
9292
<AutoGen>True</AutoGen>
93-
<DependentUpon>L16.PixelOperations.Generated.tt</DependentUpon>
93+
<DependentUpon>L8.PixelOperations.Generated.tt</DependentUpon>
9494
</Compile>
95-
<Compile Update="PixelFormats\PixelImplementations\Generated\La16.PixelOperations.Generated.cs">
95+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\La16.PixelOperations.Generated.cs">
9696
<DesignTime>True</DesignTime>
9797
<AutoGen>True</AutoGen>
9898
<DependentUpon>La16.PixelOperations.Generated.tt</DependentUpon>
9999
</Compile>
100-
<Compile Update="PixelFormats\PixelImplementations\Generated\La32.PixelOperations.Generated.cs">
100+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\La32.PixelOperations.Generated.cs">
101101
<DesignTime>True</DesignTime>
102102
<AutoGen>True</AutoGen>
103103
<DependentUpon>La32.PixelOperations.Generated.tt</DependentUpon>
104104
</Compile>
105-
<Compile Update="PixelFormats\PixelImplementations\Generated\Rgb24.PixelOperations.Generated.cs">
105+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Rgb24.PixelOperations.Generated.cs">
106106
<DesignTime>True</DesignTime>
107107
<AutoGen>True</AutoGen>
108108
<DependentUpon>Rgb24.PixelOperations.Generated.tt</DependentUpon>
109109
</Compile>
110-
<Compile Update="PixelFormats\PixelImplementations\Generated\Rgba32.PixelOperations.Generated.cs">
110+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Rgb48.PixelOperations.Generated.cs">
111111
<DesignTime>True</DesignTime>
112112
<AutoGen>True</AutoGen>
113-
<DependentUpon>Rgba32.PixelOperations.Generated.tt</DependentUpon>
113+
<DependentUpon>Rgb48.PixelOperations.Generated.tt</DependentUpon>
114114
</Compile>
115-
<Compile Update="PixelFormats\PixelImplementations\Generated\Rgb48.PixelOperations.Generated.cs">
115+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Rgba32.PixelOperations.Generated.cs">
116116
<DesignTime>True</DesignTime>
117117
<AutoGen>True</AutoGen>
118-
<DependentUpon>Rgb48.PixelOperations.Generated.tt</DependentUpon>
118+
<DependentUpon>Rgba32.PixelOperations.Generated.tt</DependentUpon>
119119
</Compile>
120-
<Compile Update="PixelFormats\PixelImplementations\Generated\Rgba64.PixelOperations.Generated.cs">
120+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Rgba64.PixelOperations.Generated.cs">
121121
<DesignTime>True</DesignTime>
122122
<AutoGen>True</AutoGen>
123123
<DependentUpon>Rgba64.PixelOperations.Generated.tt</DependentUpon>
@@ -156,51 +156,51 @@
156156
<Generator>TextTemplatingFileGenerator</Generator>
157157
<LastGenOutput>PixelOperations{TPixel}.Generated.cs</LastGenOutput>
158158
</None>
159-
<None Update="PixelFormats\PixelImplementations\Generated\Argb32.PixelOperations.Generated.tt">
159+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Argb32.PixelOperations.Generated.tt">
160160
<Generator>TextTemplatingFileGenerator</Generator>
161161
<LastGenOutput>Argb32.PixelOperations.Generated.cs</LastGenOutput>
162162
</None>
163-
<None Update="PixelFormats\PixelImplementations\Generated\Bgr24.PixelOperations.Generated.tt">
163+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Bgr24.PixelOperations.Generated.tt">
164164
<Generator>TextTemplatingFileGenerator</Generator>
165165
<LastGenOutput>Bgr24.PixelOperations.Generated.cs</LastGenOutput>
166166
</None>
167-
<None Update="PixelFormats\PixelImplementations\Generated\Bgra32.PixelOperations.Generated.tt">
167+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Bgra32.PixelOperations.Generated.tt">
168168
<Generator>TextTemplatingFileGenerator</Generator>
169169
<LastGenOutput>Bgra32.PixelOperations.Generated.cs</LastGenOutput>
170170
</None>
171-
<None Update="PixelFormats\PixelImplementations\Generated\Bgra5551.PixelOperations.Generated.tt">
171+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Bgra5551.PixelOperations.Generated.tt">
172172
<Generator>TextTemplatingFileGenerator</Generator>
173173
<LastGenOutput>Bgra5551.PixelOperations.Generated.cs</LastGenOutput>
174174
</None>
175-
<None Update="PixelFormats\PixelImplementations\Generated\L8.PixelOperations.Generated.tt">
175+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\L8.PixelOperations.Generated.tt">
176176
<Generator>TextTemplatingFileGenerator</Generator>
177177
<LastGenOutput>L8.PixelOperations.Generated.cs</LastGenOutput>
178178
</None>
179-
<None Update="PixelFormats\PixelImplementations\Generated\L16.PixelOperations.Generated.tt">
179+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\L16.PixelOperations.Generated.tt">
180180
<Generator>TextTemplatingFileGenerator</Generator>
181181
<LastGenOutput>L16.PixelOperations.Generated.cs</LastGenOutput>
182182
</None>
183-
<None Update="PixelFormats\PixelImplementations\Generated\La16.PixelOperations.Generated.tt">
183+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\La16.PixelOperations.Generated.tt">
184184
<Generator>TextTemplatingFileGenerator</Generator>
185185
<LastGenOutput>La16.PixelOperations.Generated.cs</LastGenOutput>
186186
</None>
187-
<None Update="PixelFormats\PixelImplementations\Generated\La32.PixelOperations.Generated.tt">
187+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\La32.PixelOperations.Generated.tt">
188188
<Generator>TextTemplatingFileGenerator</Generator>
189189
<LastGenOutput>La32.PixelOperations.Generated.cs</LastGenOutput>
190190
</None>
191-
<None Update="PixelFormats\PixelImplementations\Generated\Rgb24.PixelOperations.Generated.tt">
191+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Rgb24.PixelOperations.Generated.tt">
192192
<Generator>TextTemplatingFileGenerator</Generator>
193193
<LastGenOutput>Rgb24.PixelOperations.Generated.cs</LastGenOutput>
194194
</None>
195-
<None Update="PixelFormats\PixelImplementations\Generated\Rgba32.PixelOperations.Generated.tt">
195+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Rgba32.PixelOperations.Generated.tt">
196196
<Generator>TextTemplatingFileGenerator</Generator>
197197
<LastGenOutput>Rgba32.PixelOperations.Generated.cs</LastGenOutput>
198198
</None>
199-
<None Update="PixelFormats\PixelImplementations\Generated\Rgb48.PixelOperations.Generated.tt">
199+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Rgb48.PixelOperations.Generated.tt">
200200
<Generator>TextTemplatingFileGenerator</Generator>
201201
<LastGenOutput>Rgb48.PixelOperations.Generated.cs</LastGenOutput>
202202
</None>
203-
<None Update="PixelFormats\PixelImplementations\Generated\Rgba64.PixelOperations.Generated.tt">
203+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Rgba64.PixelOperations.Generated.tt">
204204
<Generator>TextTemplatingFileGenerator</Generator>
205205
<LastGenOutput>Rgba64.PixelOperations.Generated.cs</LastGenOutput>
206206
</None>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
namespace SixLabors.ImageSharp.PixelFormats
5+
{
6+
/// <summary>
7+
/// Provides enumeration of the alpha value transparency behavior of a pixel format.
8+
/// </summary>
9+
public enum PixelAlphaRepresentation
10+
{
11+
/// <summary>
12+
/// Indicates that the pixel format does not contain an alpha channel.
13+
/// </summary>
14+
None,
15+
16+
/// <summary>
17+
/// Indicates that the transparency behavior is premultiplied.
18+
/// Each color is first scaled by the alpha value. The alpha value itself is the same
19+
/// in both straight and premultiplied alpha. Typically, no color channel value is
20+
/// greater than the alpha channel value.
21+
/// If a color channel value in a premultiplied format is greater than the alpha
22+
/// channel, the standard source-over blending math results in an additive blend.
23+
/// </summary>
24+
Associated,
25+
26+
/// <summary>
27+
/// Indicates that the transparency behavior is not premultiplied.
28+
/// The alpha channel indicates the transparency of the color.
29+
/// </summary>
30+
Unassociated
31+
}
32+
}

src/ImageSharp/PixelFormats/PixelImplementations/A8.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.PixelFormats
1313
/// Ranges from [0, 0, 0, 0] to [0, 0, 0, 1] in vector form.
1414
/// </para>
1515
/// </summary>
16-
public struct A8 : IPixel<A8>, IPackedVector<byte>
16+
public partial struct A8 : IPixel<A8>, IPackedVector<byte>
1717
{
1818
/// <summary>
1919
/// Initializes a new instance of the <see cref="A8"/> struct.
@@ -57,7 +57,7 @@ public struct A8 : IPixel<A8>, IPackedVector<byte>
5757
public static bool operator !=(A8 left, A8 right) => !left.Equals(right);
5858

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

6262
/// <inheritdoc/>
6363
[MethodImpl(InliningOptions.ShortMethod)]

src/ImageSharp/PixelFormats/PixelImplementations/Bgr565.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.PixelFormats
1414
/// Ranges from [0, 0, 0, 1] to [1, 1, 1, 1] in vector form.
1515
/// </para>
1616
/// </summary>
17-
public struct Bgr565 : IPixel<Bgr565>, IPackedVector<ushort>
17+
public partial struct Bgr565 : IPixel<Bgr565>, IPackedVector<ushort>
1818
{
1919
/// <summary>
2020
/// Initializes a new instance of the <see cref="Bgr565"/> struct.
@@ -61,7 +61,7 @@ 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 readonly PixelOperations<Bgr565> CreatePixelOperations() => new PixelOperations<Bgr565>();
64+
public readonly PixelOperations<Bgr565> CreatePixelOperations() => new PixelOperations();
6565

6666
/// <inheritdoc/>
6767
[MethodImpl(InliningOptions.ShortMethod)]

src/ImageSharp/PixelFormats/PixelImplementations/Bgra4444.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.PixelFormats
1313
/// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form.
1414
/// </para>
1515
/// </summary>
16-
public struct Bgra4444 : IPixel<Bgra4444>, IPackedVector<ushort>
16+
public partial struct Bgra4444 : IPixel<Bgra4444>, IPackedVector<ushort>
1717
{
1818
/// <summary>
1919
/// Initializes a new instance of the <see cref="Bgra4444"/> struct.
@@ -59,7 +59,7 @@ 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 readonly PixelOperations<Bgra4444> CreatePixelOperations() => new PixelOperations<Bgra4444>();
62+
public readonly PixelOperations<Bgra4444> CreatePixelOperations() => new PixelOperations();
6363

6464
/// <inheritdoc/>
6565
[MethodImpl(InliningOptions.ShortMethod)]

src/ImageSharp/PixelFormats/PixelImplementations/Byte4.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.PixelFormats
1313
/// Ranges from [0, 0, 0, 0] to [255, 255, 255, 255] in vector form.
1414
/// </para>
1515
/// </summary>
16-
public struct Byte4 : IPixel<Byte4>, IPackedVector<uint>
16+
public partial struct Byte4 : IPixel<Byte4>, IPackedVector<uint>
1717
{
1818
/// <summary>
1919
/// Initializes a new instance of the <see cref="Byte4"/> struct.
@@ -62,7 +62,7 @@ public Byte4(float x, float y, float z, float w)
6262
public static bool operator !=(Byte4 left, Byte4 right) => !left.Equals(right);
6363

6464
/// <inheritdoc />
65-
public readonly PixelOperations<Byte4> CreatePixelOperations() => new PixelOperations<Byte4>();
65+
public readonly PixelOperations<Byte4> CreatePixelOperations() => new PixelOperations();
6666

6767
/// <inheritdoc/>
6868
[MethodImpl(InliningOptions.ShortMethod)]

src/ImageSharp/PixelFormats/PixelImplementations/HalfSingle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.PixelFormats
1313
/// Ranges from [-1, 0, 0, 1] to [1, 0, 0, 1] in vector form.
1414
/// </para>
1515
/// </summary>
16-
public struct HalfSingle : IPixel<HalfSingle>, IPackedVector<ushort>
16+
public partial struct HalfSingle : IPixel<HalfSingle>, IPackedVector<ushort>
1717
{
1818
/// <summary>
1919
/// Initializes a new instance of the <see cref="HalfSingle"/> struct.
@@ -47,7 +47,7 @@ public struct HalfSingle : IPixel<HalfSingle>, IPackedVector<ushort>
4747
public static bool operator !=(HalfSingle left, HalfSingle right) => !left.Equals(right);
4848

4949
/// <inheritdoc />
50-
public PixelOperations<HalfSingle> CreatePixelOperations() => new PixelOperations<HalfSingle>();
50+
public PixelOperations<HalfSingle> CreatePixelOperations() => new PixelOperations();
5151

5252
/// <inheritdoc/>
5353
[MethodImpl(InliningOptions.ShortMethod)]

src/ImageSharp/PixelFormats/PixelImplementations/HalfVector2.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.PixelFormats
1313
/// Ranges from [-1, -1, 0, 1] to [1, 1, 0, 1] in vector form.
1414
/// </para>
1515
/// </summary>
16-
public struct HalfVector2 : IPixel<HalfVector2>, IPackedVector<uint>
16+
public partial struct HalfVector2 : IPixel<HalfVector2>, IPackedVector<uint>
1717
{
1818
/// <summary>
1919
/// Initializes a new instance of the <see cref="HalfVector2"/> struct.
@@ -54,13 +54,13 @@ public struct HalfVector2 : IPixel<HalfVector2>, IPackedVector<uint>
5454
public static bool operator !=(HalfVector2 left, HalfVector2 right) => !left.Equals(right);
5555

5656
/// <inheritdoc />
57-
public readonly PixelOperations<HalfVector2> CreatePixelOperations() => new PixelOperations<HalfVector2>();
57+
public readonly PixelOperations<HalfVector2> CreatePixelOperations() => new PixelOperations();
5858

5959
/// <inheritdoc/>
6060
[MethodImpl(InliningOptions.ShortMethod)]
6161
public void FromScaledVector4(Vector4 vector)
6262
{
63-
var scaled = new Vector2(vector.X, vector.Y) * 2F;
63+
Vector2 scaled = new Vector2(vector.X, vector.Y) * 2F;
6464
scaled -= Vector2.One;
6565
this.PackedValue = Pack(scaled.X, scaled.Y);
6666
}

src/ImageSharp/PixelFormats/PixelImplementations/HalfVector4.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.PixelFormats
1313
/// Ranges from [-1, -1, -1, -1] to [1, 1, 1, 1] in vector form.
1414
/// </para>
1515
/// </summary>
16-
public struct HalfVector4 : IPixel<HalfVector4>, IPackedVector<ulong>
16+
public partial struct HalfVector4 : IPixel<HalfVector4>, IPackedVector<ulong>
1717
{
1818
/// <summary>
1919
/// Initializes a new instance of the <see cref="HalfVector4"/> struct.
@@ -59,7 +59,7 @@ public HalfVector4(float x, float y, float z, float w)
5959
public static bool operator !=(HalfVector4 left, HalfVector4 right) => !left.Equals(right);
6060

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

6464
/// <inheritdoc/>
6565
[MethodImpl(InliningOptions.ShortMethod)]

0 commit comments

Comments
 (0)