Skip to content

Commit 37933af

Browse files
Merge branch 'master' into Release
2 parents adc5993 + 6165783 commit 37933af

File tree

5 files changed

+309
-110
lines changed

5 files changed

+309
-110
lines changed
Binary file not shown.

IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/AnyBitmapFunctionality.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,73 @@ public void Redact_ShouldRedactRegionWithColor()
655655
}
656656
}
657657

658+
[FactWithAutomaticDisplayName]
659+
public void TestGetRGBBuffer()
660+
{
661+
string imagePath = GetRelativeFilePath("checkmark.jpg");
662+
using var bitmap = new AnyBitmap(imagePath);
663+
var expectedSize = bitmap.Width * bitmap.Height * 3; // 3 bytes per pixel (RGB)
664+
665+
byte[] buffer = bitmap.GetRGBBuffer();
666+
667+
Assert.Equal(expectedSize, buffer.Length);
668+
669+
// Verify the first pixel's RGB values
670+
var firstPixel = bitmap.GetPixel(0, 0);
671+
Assert.Equal(firstPixel.R, buffer[0]);
672+
Assert.Equal(firstPixel.G, buffer[1]);
673+
Assert.Equal(firstPixel.B, buffer[2]);
674+
}
675+
676+
[FactWithAutomaticDisplayName]
677+
public void Test_LoadFromRGBBuffer()
678+
{
679+
// Arrange
680+
int width = 2;
681+
int height = 2;
682+
byte[] buffer = new byte[]
683+
{
684+
255, 0, 0, // red
685+
0, 255, 0, // green
686+
0, 0, 255, // blue
687+
255, 255, 255, // white
688+
};
689+
690+
// Act
691+
AnyBitmap result = AnyBitmap.LoadAnyBitmapFromRGBBuffer(buffer, width, height);
692+
693+
// Assert
694+
byte[] resultData = result.GetRGBBuffer();
695+
Assert.Equal(buffer, resultData);
696+
}
697+
698+
[FactWithAutomaticDisplayName]
699+
public void TestLoadAnyBitmapFromRGBBuffer()
700+
{
701+
string imagePath = GetRelativeFilePath("checkmark.jpg");
702+
703+
using var bitmap = SixLabors.ImageSharp.Image.Load<Rgb24>(imagePath);
704+
var width = bitmap.Width;
705+
var height = bitmap.Height;
706+
707+
var buffer = GetRGBBuffer(imagePath);
708+
709+
AnyBitmap result = AnyBitmap.LoadAnyBitmapFromRGBBuffer(buffer, width, height);
710+
711+
for (int y = 0; y < height; y++)
712+
{
713+
for (int x = 0; x < width; x++)
714+
{
715+
var expectedColor = bitmap[x, y];
716+
var actualColor = result.GetPixel(x, y);
717+
718+
Assert.Equal(expectedColor.R, actualColor.R);
719+
Assert.Equal(expectedColor.G, actualColor.G);
720+
Assert.Equal(expectedColor.B, actualColor.B);
721+
}
722+
}
723+
}
724+
658725
#if !NETFRAMEWORK
659726
[FactWithAutomaticDisplayName]
660727
public void CastMaui_to_AnyBitmap()
@@ -683,5 +750,13 @@ public void CastMaui_from_AnyBitmap()
683750
}
684751
#endif
685752

753+
[FactWithAutomaticDisplayName]
754+
public void Should_Read_Tiff_With_Zero_Width_or_Height()
755+
{
756+
string imagePath = GetRelativeFilePath("partial_valid.tif");
757+
var anyBitmap = AnyBitmap.FromFile(imagePath);
758+
anyBitmap.FrameCount.Should().BeGreaterThan(0);
759+
}
760+
686761
}
687762
}

IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/Compare.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using SixLabors.ImageSharp.PixelFormats;
2+
using System;
23
using System.IO;
34
using System.Linq;
45
using System.Security.Cryptography;
@@ -156,5 +157,35 @@ protected static bool IsGrayScale(SkiaSharp.SKBitmap image)
156157

157158
return res;
158159
}
160+
161+
protected byte[] GetRGBBuffer(string imagePath)
162+
{
163+
using var image = SixLabors.ImageSharp.Image.Load<Rgb24>(imagePath); // Load image from file
164+
165+
int width = image.Width;
166+
int height = image.Height;
167+
168+
byte[] rgbBuffer = new byte[width * height * 3]; // 3 bytes per pixel (RGB)
169+
170+
image.ProcessPixelRows(accessor =>
171+
{
172+
for (int y = 0; y < accessor.Height; y++)
173+
{
174+
Span<Rgb24> pixelRow = accessor.GetRowSpan(y);
175+
176+
for (int x = 0; x < accessor.Width; x++)
177+
{
178+
ref Rgb24 pixel = ref pixelRow[x];
179+
180+
int bufferIndex = (y * width + x) * 3;
181+
rgbBuffer[bufferIndex] = pixel.R;
182+
rgbBuffer[bufferIndex + 1] = pixel.G;
183+
rgbBuffer[bufferIndex + 2] = pixel.B;
184+
}
185+
}
186+
});
187+
188+
return rgbBuffer;
189+
}
159190
}
160191
}

0 commit comments

Comments
 (0)