Skip to content

Commit

Permalink
Slice and Fill fixes.
Browse files Browse the repository at this point in the history
GetPlaneData, GetPixelDataAt
  • Loading branch information
juliusfriedman committed Oct 19, 2024
1 parent a8167b5 commit 16218ae
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
102 changes: 100 additions & 2 deletions Codecs/Image/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ public int PlaneLength(int plane)
return plane < 0 || plane >= MediaFormat.Components.Length ? -1 : Binary.BitsToBytes(PlaneSize(plane));
}

public Common.MemorySegment GetPlaneData(int plane)
{
int offset = 0;
for (int i = 0; i < plane; i++)
offset += PlaneLength(i);
return Data.Slice(offset, PlaneLength(plane));
}

public Common.MemorySegment GetPixelDataAt(int x, int y, int plane)
{
return GetPlaneData(plane).Slice(y * PlaneWidth(plane) + x, ImageFormat.Length);
}

/// <summary>
/// Calculates the byte offset to component/channel data
/// </summary>
Expand Down Expand Up @@ -537,7 +550,7 @@ public static void Test_Get_Set_Indexer()

public static void TestSave()
{
string currentPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
var currentPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

var outputDirectory = Directory.CreateDirectory(Path.Combine(currentPath, "Media", "BmpTest", "output"));

Expand Down Expand Up @@ -814,7 +827,7 @@ public static void TestConversionRGB()
//Create the source image
using (Media.Codecs.Image.Image rgbImage = new(Media.Codecs.Image.ImageFormat.RGB(8), testWidth, testHeight))
{
if (rgbImage.ImageFormat.HasAlphaComponent) throw new System.Exception("HasAlphaComponent should be false");
if (rgbImage.ImageFormat.HasAlphaComponent) throw new System.Exception("HasAlphaComponent should be false");

//Create the ImageFormat based on YUV packed but in Planar format with a full height luma plane and half hight chroma planes
var Yuv420P = new Codecs.Image.ImageFormat(Media.Codecs.Image.ImageFormat.YUV(8, Common.Binary.ByteOrder.Little, Codec.DataLayout.Planar), new int[] { 0, 1, 1 });
Expand Down Expand Up @@ -1316,5 +1329,90 @@ public void CalculateStride_Monochrome1_ReturnsCorrectStride()
int expectedStride = (rowSize + 3) & ~3; // Align to 4 bytes
System.Diagnostics.Debug.Assert(expectedStride == stride);
}

public void Test_GetPlaneData_ValidComponentIndex_ReturnsCorrectData()
{
// Arrange
int width = 1920;
int height = 1080;
var imageFormat = ImageFormat.RGB(24, Common.Binary.ByteOrder.Little, DataLayout.Planar);
var image = new Image(imageFormat, width, height);

// Act
var planeData = image.GetPlaneData(0);

// Assert
System.Diagnostics.Debug.Assert(planeData != null && planeData.Count > 0);
System.Diagnostics.Debug.Assert(image.PlaneLength(0) == planeData.Count);
}

public void Test_GetPlaneData_InvalidComponentIndex_ThrowsException()
{
// Arrange
int width = 1920;
int height = 1080;
var imageFormat = ImageFormat.RGB(24, Common.Binary.ByteOrder.Little, DataLayout.Planar);
var image = new Image(imageFormat, width, height);

// Act & Assert
try { image.GetPlaneData(-1); }
catch (ArgumentOutOfRangeException) { }
try { image.GetPlaneData(3); }
catch (ArgumentOutOfRangeException) { }
}

public void Test_PixAt_ValidCoordinates_ReturnsCorrectData()
{
// Arrange
int width = 1920;
int height = 1080;
var imageFormat = ImageFormat.RGB(8, Common.Binary.ByteOrder.Little, DataLayout.Packed);
var image = new Image(imageFormat, width, height);

// Act
var pixelData = image.GetPixelDataAt(100, 100, 0);
pixelData.Fill(byte.MaxValue);

//Lower left
pixelData = image.GetPixelDataAt(0, 0, 0);
pixelData.Fill(byte.MaxValue);

//Upper right
pixelData = image.GetPixelDataAt(width - 1, height -1, 0);
pixelData.Fill(byte.MaxValue);

// Assert
System.Diagnostics.Debug.Assert(pixelData != null);
System.Diagnostics.Debug.Assert(imageFormat.Length == pixelData.Count);

// Save
var currentPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

var outputDirectory = Directory.CreateDirectory(Path.Combine(currentPath, "Media", "BmpTest", "output"));

using (var outputBmpStream = new System.IO.FileStream(Path.Combine(outputDirectory.FullName, $"rgb24_{imageFormat.DataLayout}.bmp"), FileMode.OpenOrCreate))
{
image.SaveBitmap(outputBmpStream);
}
}

public void Test_PixAt_InvalidCoordinates_ThrowsException()
{
// Arrange
int width = 1920;
int height = 1080;
var imageFormat = ImageFormat.RGB(24, Common.Binary.ByteOrder.Little, DataLayout.Packed);
var image = new Image(imageFormat, width, height);

// Act & Assert
try { image.PixAt(-1, 100, 0); }

Check failure on line 1408 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1408 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1408 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1408 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)
catch (ArgumentOutOfRangeException) { }
try { image.PixAt(100, -1, 0); }

Check failure on line 1410 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1410 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1410 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1410 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)
catch (ArgumentOutOfRangeException) { }
try { image.PixAt(1920, 100, 0); }

Check failure on line 1412 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1412 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1412 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1412 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)
catch (ArgumentOutOfRangeException) { }
try { image.PixAt(100, 1080, 0); }

Check failure on line 1414 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1414 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1414 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1414 in Codecs/Image/Image.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0.x)

'Image' does not contain a definition for 'PixAt' and no accessible extension method 'PixAt' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)
catch (ArgumentOutOfRangeException) { }
}
}
}
4 changes: 2 additions & 2 deletions Common/Classes/MemorySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,9 @@ public static class MemorySegmentExtensions
{
public static void Clear(this MemorySegment segment) => Array.Clear(segment.Array, segment.Offset, segment.Count);

public static void Fill(this MemorySegment segment, byte value) => Array.Fill(segment.Array, value);
public static void Fill(this MemorySegment segment, byte value) => Array.Fill(segment.Array, value, segment.Offset, segment.Count);

public static MemorySegment Slice(this MemorySegment segment, int offset) => Slice(segment, offset, segment.Count - offset);
public static MemorySegment Slice(this MemorySegment segment, int offset) => Slice(segment, offset, Math.Max(0, segment.Count - offset));

public static MemorySegment Slice(this MemorySegment segment, int offset, int count) => new(segment.Array, segment.Offset + offset, count);

Expand Down

0 comments on commit 16218ae

Please sign in to comment.