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

Simplify CriticalCopyPixels in BitmapSource by removing duplicate type checks #9395

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
using System.IO.Packaging;
using UnsafeNativeMethods = MS.Win32.PresentationCore.UnsafeNativeMethods;
using SR = MS.Internal.PresentationCore.SR;
using MS.Internal.PresentationCore;
using System.Runtime.CompilerServices;

namespace System.Windows.Media.Imaging
{
Expand Down Expand Up @@ -655,7 +657,7 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
ArgumentNullException.ThrowIfNull(pixels);

if (pixels.Rank != 1)
throw new ArgumentException(SR.Collection_BadRank, "pixels");
throw new ArgumentException(SR.Collection_BadRank, nameof(pixels));

if (offset < 0)
{
Expand All @@ -678,43 +680,13 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int

int destBufferSize = checked(elementSize * (pixels.Length - offset));

// Check whether offset is out of bounds manually
if (offset >= pixels.Length)
throw new IndexOutOfRangeException();

if (pixels is byte[])
{
fixed (void* pixelArray = &((byte[])pixels)[offset])
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
}
else if (pixels is short[])
{
fixed (void* pixelArray = &((short[])pixels)[offset])
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
}
else if (pixels is ushort[])
{
fixed (void* pixelArray = &((ushort[])pixels)[offset])
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
}
else if (pixels is int[])
{
fixed (void* pixelArray = &((int[])pixels)[offset])
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
}
else if (pixels is uint[])
{
fixed (void* pixelArray = &((uint[])pixels)[offset])
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
}
else if (pixels is float[])
{
fixed (void* pixelArray = &((float[])pixels)[offset])
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
}
else if (pixels is double[])
{
fixed (void* pixelArray = &((double[])pixels)[offset])
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
}
}
fixed (byte* pixelArray = &Unsafe.AddByteOffset(ref MemoryMarshal.GetArrayDataReference(pixels), (nint)offset * elementSize))
CriticalCopyPixels(sourceRect, (nint)pixelArray, destBufferSize, stride);
}

/// <summary>
/// CriticalCopyPixels
Expand All @@ -726,7 +698,7 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
internal void CriticalCopyPixels(Int32Rect sourceRect, IntPtr buffer, int bufferSize, int stride)
{
if (buffer == IntPtr.Zero)
throw new ArgumentNullException("buffer");
throw new ArgumentNullException(nameof(buffer));

ArgumentOutOfRangeException.ThrowIfNegativeOrZero(stride);

Expand Down