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
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,36 @@ public void AnyBitmap_should_ToString()
Assert.Equal(expected, result);
}

[FactWithAutomaticDisplayName]
public void AnyBitmap_GetPixel_should_not_throw_OutOfMemoryException()
{
//Should not throw exception
//previously GetPixel always called CloneAs() which resulted to OutOfMemoryException
string imagePath = GetRelativeFilePath("google_large_1500dpi.bmp");
using Image<Rgb24> formatRgb24 = Image.Load<Rgb24>(imagePath);
using Image<Abgr32> formatAbgr32 = Image.Load<Abgr32>(imagePath);
using Image<Argb32> formatArgb32 = Image.Load<Argb32>(imagePath);
using Image<Bgr24> formatBgr24 = Image.Load<Bgr24>(imagePath);
using Image<Bgra32> formatBgra32 = Image.Load<Bgra32>(imagePath);

Image[] images = { formatRgb24, formatAbgr32, formatArgb32, formatBgr24, formatBgra32 };

foreach (Image image in images)
{
AnyBitmap bitmap = (AnyBitmap)image;

int hash = 0;
for (int y = 0; y < bitmap.Height; y += 8)
{
for (int x = 0; x < bitmap.Width; x += 8)
{
var pixel = bitmap.GetPixel(x, y);
hash += pixel.ToArgb();
}
}
}
}

[FactWithAutomaticDisplayName]
public void Clone_AnyBitmap()
{
Expand Down
51 changes: 29 additions & 22 deletions IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2545,29 +2545,36 @@ private static void ConvertRGBAtoBGRA(byte[] data, int width, int height, int sa

private Color GetPixelColor(int x, int y)
{
if (Image is Image<Rgb24>)
{
return (Color)Image.CloneAs<Rgb24>()[x, y];
}
else if (Image is Image<Abgr32>)
{
return (Color)Image.CloneAs<Abgr32>()[x, y];
}
else if (Image is Image<Argb32>)
{
return (Color)Image.CloneAs<Argb32>()[x, y];
}
else if (Image is Image<Bgr24>)
{
return (Color)Image.CloneAs<Bgr24>()[x, y];
}
else if (Image is Image<Bgra32>)
{
return (Color)Image.CloneAs<Bgra32>()[x, y];
}
else
switch (Image)
{
return (Color)Image.CloneAs<Rgba32>()[x, y];
case Image<Rgba32> imageAsFormat:
return imageAsFormat[x, y];
case Image<Rgb24> imageAsFormat:
return imageAsFormat[x, y];
case Image<Abgr32> imageAsFormat:
return imageAsFormat[x, y];
case Image<Argb32> imageAsFormat:
return imageAsFormat[x, y];
case Image<Bgr24> imageAsFormat:
return imageAsFormat[x, y];
case Image<Bgra32> imageAsFormat:
return imageAsFormat[x, y];
case Image<Rgb48> imageAsFormat:
return imageAsFormat[x, y];
case Image<Rgba64> imageAsFormat:
return imageAsFormat[x, y];
default:
//Fallback

//We didn't have Converter to IronSoftware.Drawing.Color for other pixel format
//A8, L8, L16, La16, Bgr565, Bgra4444, RgbaVector

//CloneAs() is expensive!
//Can throw out of memory exception, when this fucntion get called too much
using (Image<Rgb24> converted = Image.CloneAs<Rgb24>())
{
return converted[x, y];
}
}
}

Expand Down