Skip to content

Commit 83241ee

Browse files
authored
Replace ineffective array type-checking with simple array-ref pinning (#9395)
1 parent 2873221 commit 83241ee

File tree

1 file changed

+9
-38
lines changed
  • src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging

1 file changed

+9
-38
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapSource.cs

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Windows.Media.Composition;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
58
using System.IO;
69
using MS.Internal;
7-
using System.Runtime.InteropServices;
8-
using System.Windows.Media.Composition;
910
using MS.Win32;
1011

1112
using UnsafeNativeMethods = MS.Win32.PresentationCore.UnsafeNativeMethods;
@@ -663,43 +664,13 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
663664

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

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

667-
if (pixels is byte[])
668-
{
669-
fixed (void* pixelArray = &((byte[])pixels)[offset])
670-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
671-
}
672-
else if (pixels is short[])
673-
{
674-
fixed (void* pixelArray = &((short[])pixels)[offset])
675-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
676-
}
677-
else if (pixels is ushort[])
678-
{
679-
fixed (void* pixelArray = &((ushort[])pixels)[offset])
680-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
681-
}
682-
else if (pixels is int[])
683-
{
684-
fixed (void* pixelArray = &((int[])pixels)[offset])
685-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
686-
}
687-
else if (pixels is uint[])
688-
{
689-
fixed (void* pixelArray = &((uint[])pixels)[offset])
690-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
691-
}
692-
else if (pixels is float[])
693-
{
694-
fixed (void* pixelArray = &((float[])pixels)[offset])
695-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
696-
}
697-
else if (pixels is double[])
698-
{
699-
fixed (void* pixelArray = &((double[])pixels)[offset])
700-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
701-
}
702-
}
671+
fixed (byte* pixelArray = &Unsafe.AddByteOffset(ref MemoryMarshal.GetArrayDataReference(pixels), (nint)offset * elementSize))
672+
CriticalCopyPixels(sourceRect, (nint)pixelArray, destBufferSize, stride);
673+
}
703674

704675
/// <summary>
705676
/// CriticalCopyPixels

0 commit comments

Comments
 (0)