Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 22 additions & 4 deletions src/GameUtils/Types/Bitmap.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
using System;
using System.Numerics;

namespace GameUtils.Types;

/// <summary>
/// Bitmap image class
/// </summary>
public class Bitmap(int width, int height)
public class Bitmap
{
/// <summary>
/// Width of the image in pixels
/// </summary>
public int Width { get; } = width;
public int Width { get; }

/// <summary>
/// Height of the image in pixels
/// </summary>
public int Height { get; } = height;
public int Height { get; }

/// <summary>
/// Pixel data
/// </summary>
public Vector3[] Data { get; } = new Vector3[width * height];
public Vector3[] Data { get; }

/// <summary>
/// Initializes a new instance of the <see cref="Bitmap"/> class with the specified width and height.
/// </summary>
public Bitmap(int width, int height)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(width);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(height);
if ((long)width * height > int.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(height), "Width and height product exceeds maximum array size.");
}

Width = width;
Height = height;
Data = new Vector3[width * height];
}

/// <summary>
/// Gets or sets a pixel at the specified coordinates. If the coordinates are out of bounds, no operation is performed and Vector3.Zero is returned.
Expand Down
11 changes: 11 additions & 0 deletions tests/GameUtils.Tests/Types/BitmapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ public void Write_WithTraversalPath_ThrowsUnauthorizedAccessException()
Assert.ThrowsExactly<UnauthorizedAccessException>(() => bitmap.Write(invalidPath));
}

[TestMethod]
[DataRow(-1, 10)]
[DataRow(0, 10)]
[DataRow(10, -1)]
[DataRow(10, 0)]
[DataRow(100000, 100000)]
public void Constructor_InvalidDimensions_ThrowsArgumentOutOfRangeException(int width, int height)
{
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new Bitmap(width, height));
}

[TestMethod]
public void Write_WithValidPath_DoesNotThrow()
{
Expand Down
Loading