33
44using System . Buffers ;
55using System . Buffers . Binary ;
6- using System . IO ;
76using System . Runtime . InteropServices ;
8- using SixLabors . ImageSharp . Advanced ;
97using SixLabors . ImageSharp . Common . Helpers ;
108using SixLabors . ImageSharp . Memory ;
119using SixLabors . ImageSharp . Metadata ;
1210using SixLabors . ImageSharp . PixelFormats ;
1311using SixLabors . ImageSharp . Processing ;
1412using SixLabors . ImageSharp . Processing . Processors . Quantization ;
15- using static System . Net . Mime . MediaTypeNames ;
1613
1714namespace SixLabors . ImageSharp . Formats . Bmp ;
1815
@@ -380,6 +377,11 @@ private void WriteImage<TPixel>(Configuration configuration, Stream stream, Imag
380377 this . Write1BitPixelData ( configuration , stream , image ) ;
381378 break ;
382379 }
380+
381+ if ( this . processedAlphaMask )
382+ {
383+ ProcessedAlphaMask ( stream , image ) ;
384+ }
383385 }
384386
385387 private IMemoryOwner < byte > AllocateRow ( int width , int bytesPerPixel )
@@ -488,11 +490,6 @@ private void Write8BitPixelData<TPixel>(Configuration configuration, Stream stre
488490 {
489491 this . Write8BitColor ( configuration , stream , image , colorPalette ) ;
490492 }
491-
492- if ( this . processedAlphaMask )
493- {
494- ProcessedAlphaMask ( stream , image ) ;
495- }
496493 }
497494
498495 /// <summary>
@@ -610,11 +607,6 @@ private void Write4BitPixelData<TPixel>(Configuration configuration, Stream stre
610607 stream . WriteByte ( 0 ) ;
611608 }
612609 }
613-
614- if ( this . processedAlphaMask )
615- {
616- ProcessedAlphaMask ( stream , image ) ;
617- }
618610 }
619611
620612 /// <summary>
@@ -672,11 +664,6 @@ private void Write2BitPixelData<TPixel>(Configuration configuration, Stream stre
672664 stream . WriteByte ( 0 ) ;
673665 }
674666 }
675-
676- if ( this . processedAlphaMask )
677- {
678- ProcessedAlphaMask ( stream , image ) ;
679- }
680667 }
681668
682669 /// <summary>
@@ -727,11 +714,6 @@ private void Write1BitPixelData<TPixel>(Configuration configuration, Stream stre
727714 stream . WriteByte ( 0 ) ;
728715 }
729716 }
730-
731- if ( this . processedAlphaMask )
732- {
733- ProcessedAlphaMask ( stream , image ) ;
734- }
735717 }
736718
737719 /// <summary>
@@ -779,7 +761,6 @@ private static void Write1BitPalette(Stream stream, int startIdx, int endIdx, Re
779761 private static void ProcessedAlphaMask < TPixel > ( Stream stream , Image < TPixel > image )
780762 where TPixel : unmanaged, IPixel < TPixel >
781763 {
782- Rgba32 rgba = default ;
783764 int arrayWidth = image . Width / 8 ;
784765 int padding = arrayWidth % 4 ;
785766 if ( padding is not 0 )
@@ -791,19 +772,31 @@ private static void ProcessedAlphaMask<TPixel>(Stream stream, Image<TPixel> imag
791772 for ( int y = image . Height - 1 ; y >= 0 ; y -- )
792773 {
793774 mask . Clear ( ) ;
794- for ( int x = 0 ; x < image . Width ; x ++ )
775+ Span < TPixel > row = image . GetRootFramePixelBuffer ( ) . DangerousGetRowSpan ( y ) ;
776+
777+ for ( int i = 0 ; i < arrayWidth ; i ++ )
795778 {
796- int bit = x % 8 ;
797- int i = x / 8 ;
798- TPixel pixel = image [ x , y ] ;
799- pixel . ToRgba32 ( ref rgba ) ;
800- if ( rgba . A is not 0 )
779+ int x = i * 8 ;
780+
781+ for ( int j = 0 ; j < 8 ; j ++ )
801782 {
802- mask [ i ] &= unchecked ( ( byte ) ( 0b10000000 >> bit ) ) ;
783+ WriteAlphaMask ( row [ x + j ] , ref mask [ i ] , j ) ;
803784 }
804785 }
805786
806787 stream . Write ( mask ) ;
788+ stream . Skip ( padding ) ;
789+ }
790+ }
791+
792+ private static void WriteAlphaMask < TPixel > ( in TPixel pixel , ref byte mask , in int index )
793+ where TPixel : unmanaged, IPixel < TPixel >
794+ {
795+ Rgba32 rgba = default ;
796+ pixel . ToRgba32 ( ref rgba ) ;
797+ if ( rgba . A is 0 )
798+ {
799+ mask |= unchecked ( ( byte ) ( 0b10000000 >> index ) ) ;
807800 }
808801 }
809802}
0 commit comments