3
3
4
4
using System . Buffers ;
5
5
using System . Buffers . Binary ;
6
- using System . IO ;
7
6
using System . Runtime . InteropServices ;
8
- using SixLabors . ImageSharp . Advanced ;
9
7
using SixLabors . ImageSharp . Common . Helpers ;
10
8
using SixLabors . ImageSharp . Memory ;
11
9
using SixLabors . ImageSharp . Metadata ;
12
10
using SixLabors . ImageSharp . PixelFormats ;
13
11
using SixLabors . ImageSharp . Processing ;
14
12
using SixLabors . ImageSharp . Processing . Processors . Quantization ;
15
- using static System . Net . Mime . MediaTypeNames ;
16
13
17
14
namespace SixLabors . ImageSharp . Formats . Bmp ;
18
15
@@ -380,6 +377,11 @@ private void WriteImage<TPixel>(Configuration configuration, Stream stream, Imag
380
377
this . Write1BitPixelData ( configuration , stream , image ) ;
381
378
break ;
382
379
}
380
+
381
+ if ( this . processedAlphaMask )
382
+ {
383
+ ProcessedAlphaMask ( stream , image ) ;
384
+ }
383
385
}
384
386
385
387
private IMemoryOwner < byte > AllocateRow ( int width , int bytesPerPixel )
@@ -488,11 +490,6 @@ private void Write8BitPixelData<TPixel>(Configuration configuration, Stream stre
488
490
{
489
491
this . Write8BitColor ( configuration , stream , image , colorPalette ) ;
490
492
}
491
-
492
- if ( this . processedAlphaMask )
493
- {
494
- ProcessedAlphaMask ( stream , image ) ;
495
- }
496
493
}
497
494
498
495
/// <summary>
@@ -610,11 +607,6 @@ private void Write4BitPixelData<TPixel>(Configuration configuration, Stream stre
610
607
stream . WriteByte ( 0 ) ;
611
608
}
612
609
}
613
-
614
- if ( this . processedAlphaMask )
615
- {
616
- ProcessedAlphaMask ( stream , image ) ;
617
- }
618
610
}
619
611
620
612
/// <summary>
@@ -672,11 +664,6 @@ private void Write2BitPixelData<TPixel>(Configuration configuration, Stream stre
672
664
stream . WriteByte ( 0 ) ;
673
665
}
674
666
}
675
-
676
- if ( this . processedAlphaMask )
677
- {
678
- ProcessedAlphaMask ( stream , image ) ;
679
- }
680
667
}
681
668
682
669
/// <summary>
@@ -727,11 +714,6 @@ private void Write1BitPixelData<TPixel>(Configuration configuration, Stream stre
727
714
stream . WriteByte ( 0 ) ;
728
715
}
729
716
}
730
-
731
- if ( this . processedAlphaMask )
732
- {
733
- ProcessedAlphaMask ( stream , image ) ;
734
- }
735
717
}
736
718
737
719
/// <summary>
@@ -779,7 +761,6 @@ private static void Write1BitPalette(Stream stream, int startIdx, int endIdx, Re
779
761
private static void ProcessedAlphaMask < TPixel > ( Stream stream , Image < TPixel > image )
780
762
where TPixel : unmanaged, IPixel < TPixel >
781
763
{
782
- Rgba32 rgba = default ;
783
764
int arrayWidth = image . Width / 8 ;
784
765
int padding = arrayWidth % 4 ;
785
766
if ( padding is not 0 )
@@ -791,19 +772,31 @@ private static void ProcessedAlphaMask<TPixel>(Stream stream, Image<TPixel> imag
791
772
for ( int y = image . Height - 1 ; y >= 0 ; y -- )
792
773
{
793
774
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 ++ )
795
778
{
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 ++ )
801
782
{
802
- mask [ i ] &= unchecked ( ( byte ) ( 0b10000000 >> bit ) ) ;
783
+ WriteAlphaMask ( row [ x + j ] , ref mask [ i ] , j ) ;
803
784
}
804
785
}
805
786
806
787
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 ) ) ;
807
800
}
808
801
}
809
802
}
0 commit comments