Skip to content

Commit db70581

Browse files
Add PixelComponentPrecision
1 parent 9e89cc8 commit db70581

Some content is hidden

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

68 files changed

+971
-569
lines changed

src/ImageSharp/Color/Color.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private Color(IPixel pixel)
107107
public static Color FromPixel<TPixel>(TPixel pixel)
108108
where TPixel : unmanaged, IPixel<TPixel>
109109
{
110-
// Avoid boxing in case we can convert to Rgba64 safely and efficently
110+
// Avoid boxing in case we can convert to Rgba64 safely and efficiently
111111
if (typeof(TPixel) == typeof(Rgba64))
112112
{
113113
return new((Rgba64)(object)pixel);

src/ImageSharp/Formats/PixelTypeInfo.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,51 @@
44
using System.Runtime.CompilerServices;
55
using SixLabors.ImageSharp.PixelFormats;
66

7-
// TODO: Review this class as it's used to represent 2 different things.
7+
// TODO: Review this type as it's used to represent 2 different things.
88
// 1.The encoded image pixel format.
99
// 2. The pixel format of the decoded image.
1010
namespace SixLabors.ImageSharp.Formats;
1111

1212
/// <summary>
1313
/// Contains information about the pixels that make up an images visual data.
1414
/// </summary>
15-
public readonly struct PixelTypeInfo
15+
/// <remarks>
16+
/// Initializes a new instance of the <see cref="PixelTypeInfo"/> struct.
17+
/// </remarks>
18+
/// <param name="bitsPerPixel">Color depth, in number of bits per pixel.</param>
19+
public readonly struct PixelTypeInfo(int bitsPerPixel)
1620
{
17-
/// <summary>
18-
/// Initializes a new instance of the <see cref="PixelTypeInfo"/> struct.
19-
/// </summary>
20-
/// <param name="bitsPerPixel">Color depth, in number of bits per pixel.</param>
21-
public PixelTypeInfo(int bitsPerPixel)
22-
=> this.BitsPerPixel = bitsPerPixel;
23-
2421
/// <summary>
2522
/// Gets color depth, in number of bits per pixel.
2623
/// </summary>
27-
public int BitsPerPixel { get; init; }
24+
public int BitsPerPixel { get; init; } = bitsPerPixel;
2825

2926
/// <summary>
3027
/// Gets the count of the color components
3128
/// </summary>
3229
public byte ComponentCount { get; init; }
3330

31+
/// <summary>
32+
/// Gets the pixel component precision.
33+
/// </summary>
34+
public PixelComponentPrecision? ComponentPrecision { get; init; }
35+
3436
/// <summary>
3537
/// Gets the pixel alpha transparency behavior.
3638
/// <see langword="null"/> means unknown, unspecified.
3739
/// </summary>
3840
public PixelAlphaRepresentation? AlphaRepresentation { get; init; }
3941

40-
internal static PixelTypeInfo Create<TPixel>(byte componentCount, PixelAlphaRepresentation pixelAlphaRepresentation)
42+
internal static PixelTypeInfo Create<TPixel>(
43+
byte componentCount,
44+
PixelComponentPrecision componentPrecision,
45+
PixelAlphaRepresentation pixelAlphaRepresentation)
4146
where TPixel : unmanaged, IPixel<TPixel>
4247
=> new()
4348
{
4449
BitsPerPixel = Unsafe.SizeOf<TPixel>() * 8,
4550
ComponentCount = componentCount,
51+
ComponentPrecision = componentPrecision,
4652
AlphaRepresentation = pixelAlphaRepresentation
4753
};
4854
}

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLabPlanarTiffColor{TPixel}.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels,
2727
Span<byte> a = data[1].GetSpan();
2828
Span<byte> b = data[2].GetSpan();
2929

30-
var color = default(TPixel);
30+
TPixel color = default;
3131
int offset = 0;
3232
for (int y = top; y < top + height; y++)
3333
{
3434
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width);
3535
for (int x = 0; x < pixelRow.Length; x++)
3636
{
37-
var lab = new CieLab((l[offset] & 0xFF) * 100f * Inv255, (sbyte)a[offset], (sbyte)b[offset]);
38-
var rgb = ColorSpaceConverter.ToRgb(lab);
37+
CieLab lab = new((l[offset] & 0xFF) * 100f * Inv255, (sbyte)a[offset], (sbyte)b[offset]);
38+
Rgb rgb = ColorSpaceConverter.ToRgb(lab);
3939

40-
color.FromVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f));
40+
color.FromScaledVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f));
4141
pixelRow[x] = color;
4242

4343
offset++;

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLabTiffColor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3434
CieLab lab = new(l, (sbyte)data[offset + 1], (sbyte)data[offset + 2]);
3535
Rgb rgb = ColorSpaceConverter.ToRgb(lab);
3636

37-
color.FromVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f));
37+
color.FromScaledVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f));
3838
pixelRow[x] = color;
3939

4040
offset += 3;

src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CmykTiffColor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
2727
Cmyk cmyk = new(data[offset] * Inv255, data[offset + 1] * Inv255, data[offset + 2] * Inv255, data[offset + 3] * Inv255);
2828
Rgb rgb = ColorSpaceConverter.ToRgb(in cmyk);
2929

30-
color.FromVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f));
30+
color.FromScaledVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f));
3131
pixelRow[x] = color;
3232

3333
offset += 4;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
namespace SixLabors.ImageSharp.PixelFormats;
5+
6+
/// <summary>
7+
/// Provides enumeration of the maximum precision of individual components within a pixel format.
8+
/// </summary>
9+
public enum PixelComponentPrecision
10+
{
11+
/// <summary>
12+
/// 8-bit signed integer.
13+
/// </summary>
14+
SByte,
15+
16+
/// <summary>
17+
/// 8-bit unsigned integer.
18+
/// </summary>
19+
Byte,
20+
21+
/// <summary>
22+
/// 16-bit signed integer.
23+
/// </summary>
24+
Short,
25+
26+
/// <summary>
27+
/// 16-bit unsigned integer.
28+
/// </summary>
29+
UShort,
30+
31+
/// <summary>
32+
/// 32-bit signed integer.
33+
/// </summary>
34+
Int,
35+
36+
/// <summary>
37+
/// 32-bit unsigned integer.
38+
/// </summary>
39+
UInt,
40+
41+
/// <summary>
42+
/// 16-bit floating point.
43+
/// </summary>
44+
Half,
45+
46+
/// <summary>
47+
/// 32-bit floating point.
48+
/// </summary>
49+
Float
50+
}

src/ImageSharp/PixelFormats/PixelImplementations/A8.cs

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

5959
/// <inheritdoc/>
60-
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<A8>(1, PixelAlphaRepresentation.Unassociated);
60+
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<A8>(1, PixelComponentPrecision.Byte, PixelAlphaRepresentation.Unassociated);
6161

6262
/// <inheritdoc />
6363
public readonly PixelOperations<A8> CreatePixelOperations() => new PixelOperations();

src/ImageSharp/PixelFormats/PixelImplementations/Abgr32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public uint PackedValue
185185
public static bool operator !=(Abgr32 left, Abgr32 right) => !left.Equals(right);
186186

187187
/// <inheritdoc />
188-
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Abgr32>(4, PixelAlphaRepresentation.Unassociated);
188+
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Abgr32>(4, PixelComponentPrecision.Byte, PixelAlphaRepresentation.Unassociated);
189189

190190
/// <inheritdoc />
191191
public readonly PixelOperations<Abgr32> CreatePixelOperations() => new PixelOperations();

src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public uint PackedValue
185185
public static bool operator !=(Argb32 left, Argb32 right) => !left.Equals(right);
186186

187187
/// <inheritdoc />
188-
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Argb32>(4, PixelAlphaRepresentation.Unassociated);
188+
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Argb32>(4, PixelComponentPrecision.Byte, PixelAlphaRepresentation.Unassociated);
189189

190190
/// <inheritdoc />
191191
public readonly PixelOperations<Argb32> CreatePixelOperations() => new PixelOperations();

src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ 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 static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Bgr24>(3, PixelAlphaRepresentation.None);
92+
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Bgr24>(3, PixelComponentPrecision.Byte, PixelAlphaRepresentation.None);
9393

9494
/// <inheritdoc/>
9595
public readonly PixelOperations<Bgr24> CreatePixelOperations() => new PixelOperations();

0 commit comments

Comments
 (0)