Skip to content

Simplify CriticalCopyPixels in BitmapSource by removing duplicate type checks #9395

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

Merged
merged 1 commit into from
Feb 28, 2025
Merged
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 @@ -2,10 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Windows.Media.Composition;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.IO;
using MS.Internal;
using System.Runtime.InteropServices;
using System.Windows.Media.Composition;
using MS.Win32;

using UnsafeNativeMethods = MS.Win32.PresentationCore.UnsafeNativeMethods;
Expand Down Expand Up @@ -663,43 +664,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 Down