Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promote PixelTypeInfo to Pixel #2601

Merged
merged 20 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ImageSharp.ruleset
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="ImageSharp" ToolsVersion="17.0">
<Include Path="..\shared-infrastructure\sixlabors.ruleset" Action="Default" />
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.NetAnalyzers" RuleNamespace="Microsoft.CodeAnalysis.CSharp.NetAnalyzers">
<Rule Id="CA1000" Action="None"/>
</Rules>
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
<Rule Id="SA1011" Action="None" />
</Rules>
Expand Down
33 changes: 14 additions & 19 deletions src/ImageSharp/Formats/PixelTypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,37 @@ namespace SixLabors.ImageSharp.Formats;
/// <summary>
/// Contains information about the pixels that make up an images visual data.
/// </summary>
public class PixelTypeInfo
public readonly struct PixelTypeInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="PixelTypeInfo"/> class.
/// Initializes a new instance of the <see cref="PixelTypeInfo"/> struct.
/// </summary>
/// <param name="bitsPerPixel">Color depth, in number of bits per pixel.</param>
public PixelTypeInfo(int bitsPerPixel)
=> this.BitsPerPixel = bitsPerPixel;

/// <summary>
/// Initializes a new instance of the <see cref="PixelTypeInfo"/> class.
/// Gets color depth, in number of bits per pixel.
/// </summary>
/// <param name="bitsPerPixel">Color depth, in number of bits per pixel.</param>
/// <param name="alpha">The pixel alpha transparency behavior.</param>
public PixelTypeInfo(int bitsPerPixel, PixelAlphaRepresentation alpha)
{
this.BitsPerPixel = bitsPerPixel;
this.AlphaRepresentation = alpha;
}
public int BitsPerPixel { get; init; }

/// <summary>
/// Gets color depth, in number of bits per pixel.
/// Gets the count of the color components
/// </summary>
public int BitsPerPixel { get; }
public byte ComponentCount { get; init; }

/// <summary>
/// Gets the pixel alpha transparency behavior.
/// <see langword="null"/> means unknown, unspecified.
/// </summary>
public PixelAlphaRepresentation? AlphaRepresentation { get; }

internal static PixelTypeInfo Create<TPixel>()
where TPixel : unmanaged, IPixel<TPixel>
=> new(Unsafe.SizeOf<TPixel>() * 8);
public PixelAlphaRepresentation? AlphaRepresentation { get; init; }

internal static PixelTypeInfo Create<TPixel>(PixelAlphaRepresentation alpha)
internal static PixelTypeInfo Create<TPixel>(byte componentCount, PixelAlphaRepresentation pixelAlphaRepresentation)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering whether we can remove this? I want to add the additional enum #2534 (comment) which means adding an additional property to the constructor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just add the additional parameter as having to duplicate Unsafe.SizeOf<TPixel>() * 8 is worse.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stefannikolei I'm going to add this for you today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. To be honest.. totally forgot about that comment

where TPixel : unmanaged, IPixel<TPixel>
=> new(Unsafe.SizeOf<TPixel>() * 8, alpha);
=> new()
{
BitsPerPixel = Unsafe.SizeOf<TPixel>() * 8,
ComponentCount = componentCount,
AlphaRepresentation = pixelAlphaRepresentation
};
}
8 changes: 4 additions & 4 deletions src/ImageSharp/Image{TPixel}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Image(int width, int height)
/// <param name="height">The height of the image in pixels.</param>
/// <param name="metadata">The images metadata.</param>
internal Image(Configuration configuration, int width, int height, ImageMetadata? metadata)
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata ?? new(), width, height)
: base(configuration, TPixel.GetPixelTypeInfo(), metadata ?? new(), width, height)
=> this.frames = new ImageFrameCollection<TPixel>(this, width, height, default(TPixel));

/// <summary>
Expand Down Expand Up @@ -111,7 +111,7 @@ internal Image(
int width,
int height,
ImageMetadata metadata)
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata, width, height)
: base(configuration, TPixel.GetPixelTypeInfo(), metadata, width, height)
=> this.frames = new ImageFrameCollection<TPixel>(this, width, height, memoryGroup);

/// <summary>
Expand All @@ -129,7 +129,7 @@ internal Image(
int height,
TPixel backgroundColor,
ImageMetadata? metadata)
: base(configuration, PixelTypeInfo.Create<TPixel>(), metadata ?? new(), width, height)
: base(configuration, TPixel.GetPixelTypeInfo(), metadata ?? new(), width, height)
=> this.frames = new ImageFrameCollection<TPixel>(this, width, height, backgroundColor);

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

/// <inheritdoc />
Expand Down
7 changes: 7 additions & 0 deletions src/ImageSharp/PixelFormats/IPixel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Six Labors Split License.

using System.Numerics;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand All @@ -14,6 +15,12 @@ namespace SixLabors.ImageSharp.PixelFormats;
public interface IPixel<TSelf> : IPixel, IEquatable<TSelf>
where TSelf : unmanaged, IPixel<TSelf>
{
/// <summary>
/// Gets the The pixel type information.
stefannikolei marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <returns>PixelTypeInfo</returns>
stefannikolei marked this conversation as resolved.
Show resolved Hide resolved
static abstract PixelTypeInfo GetPixelTypeInfo();

/// <summary>
/// Creates a <see cref="PixelOperations{TPixel}"/> instance for this pixel type.
/// This method is not intended to be consumed directly. Use <see cref="PixelOperations{TPixel}.Instance"/> instead.
Expand Down
7 changes: 7 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/A8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

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

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
stefannikolei marked this conversation as resolved.
Show resolved Hide resolved
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<A8>(1, PixelAlphaRepresentation.Unassociated);

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

Expand Down
7 changes: 7 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/Abgr32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -183,6 +184,12 @@ public uint PackedValue
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(Abgr32 left, Abgr32 right) => !left.Equals(right);

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Abgr32>(4, PixelAlphaRepresentation.Unassociated);

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

Expand Down
7 changes: 7 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -183,6 +184,12 @@ public uint PackedValue
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(Argb32 left, Argb32 right) => !left.Equals(right);

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Argb32>(4, PixelAlphaRepresentation.Unassociated);

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

Expand Down
7 changes: 7 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -87,6 +88,12 @@ public Bgr24(byte r, byte g, byte b)
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(Bgr24 left, Bgr24 right) => !left.Equals(right);

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Bgr24>(3, PixelAlphaRepresentation.None);

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

Expand Down
7 changes: 7 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgr565.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -59,6 +60,12 @@ public Bgr565(float x, float y, float z)
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(Bgr565 left, Bgr565 right) => !left.Equals(right);

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Bgr565>(3, PixelAlphaRepresentation.None);

/// <inheritdoc />
public readonly PixelOperations<Bgr565> CreatePixelOperations() => new PixelOperations();

Expand Down
7 changes: 7 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -136,6 +137,12 @@ public uint PackedValue
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(Bgra32 left, Bgra32 right) => !left.Equals(right);

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Bgra32>(4, PixelAlphaRepresentation.Unassociated);

/// <inheritdoc/>
public readonly PixelOperations<Bgra32> CreatePixelOperations() => new PixelOperations();

Expand Down
7 changes: 7 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgra4444.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

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

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Bgra4444>(4, PixelAlphaRepresentation.Unassociated);

/// <inheritdoc />
public readonly PixelOperations<Bgra4444> CreatePixelOperations() => new PixelOperations();

Expand Down
7 changes: 7 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgra5551.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -60,6 +61,12 @@ public Bgra5551(float x, float y, float z, float w)
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(Bgra5551 left, Bgra5551 right) => !left.Equals(right);

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Bgra5551>(4, PixelAlphaRepresentation.Unassociated);

/// <inheritdoc />
public readonly PixelOperations<Bgra5551> CreatePixelOperations() => new PixelOperations();

Expand Down
7 changes: 7 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/Byte4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -60,6 +61,12 @@ public Byte4(float x, float y, float z, float w)
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(Byte4 left, Byte4 right) => !left.Equals(right);

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<Byte4>(4, PixelAlphaRepresentation.Unassociated);

/// <inheritdoc />
public readonly PixelOperations<Byte4> CreatePixelOperations() => new PixelOperations();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -45,6 +46,12 @@ public partial struct HalfSingle : IPixel<HalfSingle>, IPackedVector<ushort>
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(HalfSingle left, HalfSingle right) => !left.Equals(right);

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<HalfSingle>(1, PixelAlphaRepresentation.None);

/// <inheritdoc />
public PixelOperations<HalfSingle> CreatePixelOperations() => new PixelOperations();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -52,6 +53,12 @@ public partial struct HalfVector2 : IPixel<HalfVector2>, IPackedVector<uint>
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(HalfVector2 left, HalfVector2 right) => !left.Equals(right);

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<HalfVector2>(2, PixelAlphaRepresentation.None);

/// <inheritdoc />
public readonly PixelOperations<HalfVector2> CreatePixelOperations() => new PixelOperations();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -57,6 +58,12 @@ public HalfVector4(float x, float y, float z, float w)
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(HalfVector4 left, HalfVector4 right) => !left.Equals(right);

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<HalfVector4>(4, PixelAlphaRepresentation.Unassociated);

/// <inheritdoc />
public readonly PixelOperations<HalfVector4> CreatePixelOperations() => new PixelOperations();

Expand Down
7 changes: 7 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/L16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats;

namespace SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -47,6 +48,12 @@ public partial struct L16 : IPixel<L16>, IPackedVector<ushort>
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(L16 left, L16 right) => !left.Equals(right);

/// <summary>
/// Gets the The pixel type information.
/// </summary>
/// <returns>PixelTypeInfo</returns>
public static PixelTypeInfo GetPixelTypeInfo() => PixelTypeInfo.Create<L16>(1, PixelAlphaRepresentation.None);

/// <inheritdoc />
public readonly PixelOperations<L16> CreatePixelOperations() => new PixelOperations();

Expand Down
Loading
Loading