@@ -307,39 +307,37 @@ private void ReadPaletted<TPixel>(BufferedReadStream stream, int width, int heig
307
307
private void ReadPalettedRle < TPixel > ( BufferedReadStream stream , int width , int height , Buffer2D < TPixel > pixels , Span < byte > palette , int colorMapPixelSizeInBytes , TgaImageOrigin origin )
308
308
where TPixel : unmanaged, IPixel < TPixel >
309
309
{
310
- using ( IMemoryOwner < byte > buffer = this . memoryAllocator . Allocate < byte > ( width * height , AllocationOptions . Clean ) )
311
- {
312
- TPixel color = default ;
313
- Span < byte > bufferSpan = buffer . GetSpan ( ) ;
314
- this . UncompressRle ( stream , width , height , bufferSpan , bytesPerPixel : 1 ) ;
310
+ using IMemoryOwner < byte > buffer = this . memoryAllocator . Allocate < byte > ( width * height , AllocationOptions . Clean ) ;
311
+ TPixel color = default ;
312
+ Span < byte > bufferSpan = buffer . GetSpan ( ) ;
313
+ this . UncompressRle ( stream , width , height , bufferSpan , bytesPerPixel : 1 ) ;
315
314
316
- for ( int y = 0 ; y < height ; y ++ )
315
+ for ( int y = 0 ; y < height ; y ++ )
316
+ {
317
+ int newY = InvertY ( y , height , origin ) ;
318
+ Span < TPixel > pixelRow = pixels . DangerousGetRowSpan ( newY ) ;
319
+ int rowStartIdx = y * width ;
320
+ for ( int x = 0 ; x < width ; x ++ )
317
321
{
318
- int newY = InvertY ( y , height , origin ) ;
319
- Span < TPixel > pixelRow = pixels . DangerousGetRowSpan ( newY ) ;
320
- int rowStartIdx = y * width ;
321
- for ( int x = 0 ; x < width ; x ++ )
322
+ int idx = rowStartIdx + x ;
323
+ switch ( colorMapPixelSizeInBytes )
322
324
{
323
- int idx = rowStartIdx + x ;
324
- switch ( colorMapPixelSizeInBytes )
325
- {
326
- case 1 :
327
- color . FromL8 ( Unsafe . As < byte , L8 > ( ref palette [ bufferSpan [ idx ] * colorMapPixelSizeInBytes ] ) ) ;
328
- break ;
329
- case 2 :
330
- this . ReadPalettedBgra16Pixel ( palette , bufferSpan [ idx ] , colorMapPixelSizeInBytes , ref color ) ;
331
- break ;
332
- case 3 :
333
- color . FromBgr24 ( Unsafe . As < byte , Bgr24 > ( ref palette [ bufferSpan [ idx ] * colorMapPixelSizeInBytes ] ) ) ;
334
- break ;
335
- case 4 :
336
- color . FromBgra32 ( Unsafe . As < byte , Bgra32 > ( ref palette [ bufferSpan [ idx ] * colorMapPixelSizeInBytes ] ) ) ;
337
- break ;
338
- }
339
-
340
- int newX = InvertX ( x , width , origin ) ;
341
- pixelRow [ newX ] = color ;
325
+ case 1 :
326
+ color . FromL8 ( Unsafe . As < byte , L8 > ( ref palette [ bufferSpan [ idx ] * colorMapPixelSizeInBytes ] ) ) ;
327
+ break ;
328
+ case 2 :
329
+ this . ReadPalettedBgra16Pixel ( palette , bufferSpan [ idx ] , colorMapPixelSizeInBytes , ref color ) ;
330
+ break ;
331
+ case 3 :
332
+ color . FromBgr24 ( Unsafe . As < byte , Bgr24 > ( ref palette [ bufferSpan [ idx ] * colorMapPixelSizeInBytes ] ) ) ;
333
+ break ;
334
+ case 4 :
335
+ color . FromBgra32 ( Unsafe . As < byte , Bgra32 > ( ref palette [ bufferSpan [ idx ] * colorMapPixelSizeInBytes ] ) ) ;
336
+ break ;
342
337
}
338
+
339
+ int newX = InvertX ( x , width , origin ) ;
340
+ pixelRow [ newX ] = color ;
343
341
}
344
342
}
345
343
}
@@ -598,60 +596,58 @@ private void ReadRle<TPixel>(BufferedReadStream stream, int width, int height, B
598
596
Guard . NotNull ( this . tgaMetadata ) ;
599
597
600
598
byte alphaBits = this . tgaMetadata . AlphaChannelBits ;
601
- using ( IMemoryOwner < byte > buffer = this . memoryAllocator . Allocate < byte > ( width * height * bytesPerPixel , AllocationOptions . Clean ) )
599
+ using IMemoryOwner < byte > buffer = this . memoryAllocator . Allocate < byte > ( width * height * bytesPerPixel , AllocationOptions . Clean ) ;
600
+ Span < byte > bufferSpan = buffer . GetSpan ( ) ;
601
+ this . UncompressRle ( stream , width , height , bufferSpan , bytesPerPixel ) ;
602
+ for ( int y = 0 ; y < height ; y ++ )
602
603
{
603
- Span < byte > bufferSpan = buffer . GetSpan ( ) ;
604
- this . UncompressRle ( stream , width , height , bufferSpan , bytesPerPixel ) ;
605
- for ( int y = 0 ; y < height ; y ++ )
604
+ int newY = InvertY ( y , height , origin ) ;
605
+ Span < TPixel > pixelRow = pixels . DangerousGetRowSpan ( newY ) ;
606
+ int rowStartIdx = y * width * bytesPerPixel ;
607
+ for ( int x = 0 ; x < width ; x ++ )
606
608
{
607
- int newY = InvertY ( y , height , origin ) ;
608
- Span < TPixel > pixelRow = pixels . DangerousGetRowSpan ( newY ) ;
609
- int rowStartIdx = y * width * bytesPerPixel ;
610
- for ( int x = 0 ; x < width ; x ++ )
609
+ int idx = rowStartIdx + ( x * bytesPerPixel ) ;
610
+ switch ( bytesPerPixel )
611
611
{
612
- int idx = rowStartIdx + ( x * bytesPerPixel ) ;
613
- switch ( bytesPerPixel )
614
- {
615
- case 1 :
616
- color . FromL8 ( Unsafe . As < byte , L8 > ( ref bufferSpan [ idx ] ) ) ;
617
- break ;
618
- case 2 :
619
- if ( ! this . hasAlpha )
620
- {
621
- // Set alpha value to 1, to treat it as opaque for Bgra5551.
622
- bufferSpan [ idx + 1 ] = ( byte ) ( bufferSpan [ idx + 1 ] | 128 ) ;
623
- }
624
-
625
- if ( this . fileHeader . ImageType == TgaImageType . RleBlackAndWhite )
626
- {
627
- color . FromLa16 ( Unsafe . As < byte , La16 > ( ref bufferSpan [ idx ] ) ) ;
628
- }
629
- else
630
- {
631
- color . FromBgra5551 ( Unsafe . As < byte , Bgra5551 > ( ref bufferSpan [ idx ] ) ) ;
632
- }
633
-
634
- break ;
635
- case 3 :
636
- color . FromBgr24 ( Unsafe . As < byte , Bgr24 > ( ref bufferSpan [ idx ] ) ) ;
637
- break ;
638
- case 4 :
639
- if ( this . hasAlpha )
640
- {
641
- color . FromBgra32 ( Unsafe . As < byte , Bgra32 > ( ref bufferSpan [ idx ] ) ) ;
642
- }
643
- else
644
- {
645
- byte alpha = alphaBits == 0 ? byte . MaxValue : bufferSpan [ idx + 3 ] ;
646
- color . FromBgra32 ( new Bgra32 ( bufferSpan [ idx + 2 ] , bufferSpan [ idx + 1 ] , bufferSpan [ idx ] , alpha ) ) ;
647
- }
648
-
649
- break ;
650
- }
612
+ case 1 :
613
+ color . FromL8 ( Unsafe . As < byte , L8 > ( ref bufferSpan [ idx ] ) ) ;
614
+ break ;
615
+ case 2 :
616
+ if ( ! this . hasAlpha )
617
+ {
618
+ // Set alpha value to 1, to treat it as opaque for Bgra5551.
619
+ bufferSpan [ idx + 1 ] = ( byte ) ( bufferSpan [ idx + 1 ] | 128 ) ;
620
+ }
651
621
652
- int newX = InvertX ( x , width , origin ) ;
653
- pixelRow [ newX ] = color ;
622
+ if ( this . fileHeader . ImageType == TgaImageType . RleBlackAndWhite )
623
+ {
624
+ color . FromLa16 ( Unsafe . As < byte , La16 > ( ref bufferSpan [ idx ] ) ) ;
625
+ }
626
+ else
627
+ {
628
+ color . FromBgra5551 ( Unsafe . As < byte , Bgra5551 > ( ref bufferSpan [ idx ] ) ) ;
629
+ }
630
+
631
+ break ;
632
+ case 3 :
633
+ color . FromBgr24 ( Unsafe . As < byte , Bgr24 > ( ref bufferSpan [ idx ] ) ) ;
634
+ break ;
635
+ case 4 :
636
+ if ( this . hasAlpha )
637
+ {
638
+ color . FromBgra32 ( Unsafe . As < byte , Bgra32 > ( ref bufferSpan [ idx ] ) ) ;
639
+ }
640
+ else
641
+ {
642
+ byte alpha = alphaBits == 0 ? byte . MaxValue : bufferSpan [ idx + 3 ] ;
643
+ color . FromBgra32 ( new Bgra32 ( bufferSpan [ idx + 2 ] , bufferSpan [ idx + 1 ] , bufferSpan [ idx ] , alpha ) ) ;
644
+ }
645
+
646
+ break ;
654
647
}
648
+
649
+ int newX = InvertX ( x , width , origin ) ;
650
+ pixelRow [ newX ] = color ;
655
651
}
656
652
}
657
653
}
@@ -952,7 +948,6 @@ private TgaImageOrigin ReadFileHeader(BufferedReadStream stream)
952
948
this . hasAlpha = alphaBits > 0 ;
953
949
954
950
// Bits 4 and 5 describe the image origin.
955
- TgaImageOrigin origin = ( TgaImageOrigin ) ( ( this . fileHeader . ImageDescriptor & 0x30 ) >> 4 ) ;
956
- return origin ;
951
+ return ( TgaImageOrigin ) ( ( this . fileHeader . ImageDescriptor & 0x30 ) >> 4 ) ;
957
952
}
958
953
}
0 commit comments