@@ -250,6 +250,7 @@ public static void ProcessPaletteScanline<TPixel>(
250250 ref TPixel rowSpanRef = ref MemoryMarshal . GetReference ( rowSpan ) ;
251251 ReadOnlySpan < Rgb24 > palettePixels = MemoryMarshal . Cast < byte , Rgb24 > ( palette ) ;
252252 ref Rgb24 palettePixelsRef = ref MemoryMarshal . GetReference ( palettePixels ) ;
253+ int maxIndex = palettePixels . Length - 1 ;
253254
254255 if ( paletteAlpha ? . Length > 0 )
255256 {
@@ -260,7 +261,7 @@ public static void ProcessPaletteScanline<TPixel>(
260261
261262 for ( int x = 0 ; x < header . Width ; x ++ )
262263 {
263- int index = Unsafe . Add ( ref scanlineSpanRef , x ) ;
264+ int index = Numerics . Clamp ( Unsafe . Add ( ref scanlineSpanRef , x ) , 0 , maxIndex ) ;
264265 rgba . Rgb = Unsafe . Add ( ref palettePixelsRef , index ) ;
265266 rgba . A = paletteAlpha . Length > index ? Unsafe . Add ( ref paletteAlphaRef , index ) : byte . MaxValue ;
266267
@@ -272,8 +273,8 @@ public static void ProcessPaletteScanline<TPixel>(
272273 {
273274 for ( int x = 0 ; x < header . Width ; x ++ )
274275 {
275- int index = Unsafe . Add ( ref scanlineSpanRef , x ) ;
276- Rgb24 rgb = Unsafe . Add ( ref palettePixelsRef , index ) ;
276+ uint index = Unsafe . Add ( ref scanlineSpanRef , x ) ;
277+ Rgb24 rgb = Unsafe . Add ( ref palettePixelsRef , ( int ) Math . Min ( index , maxIndex ) ) ;
277278
278279 pixel . FromRgb24 ( rgb ) ;
279280 Unsafe . Add ( ref rowSpanRef , x ) = pixel;
0 commit comments