33
44using System ;
55using System . Threading . Tasks ;
6+ using SixLabors . ImageSharp . Advanced ;
67using SixLabors . ImageSharp . Memory ;
78using SixLabors . ImageSharp . PixelFormats ;
89using SixLabors . ImageSharp . Processing . Drawing . Brushes ;
@@ -49,39 +50,66 @@ protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle source
4950 int minY = Math . Max ( 0 , startY ) ;
5051 int maxY = Math . Min ( source . Height , endY ) ;
5152
52- // Reset offset if necessary.
53- if ( minX > 0 )
54- {
55- startX = 0 ;
56- }
57-
58- if ( minY > 0 )
59- {
60- startY = 0 ;
61- }
62-
6353 int width = maxX - minX ;
6454
65- using ( IBuffer < float > amount = source . MemoryManager . Allocate < float > ( width ) )
66- using ( BrushApplicator < TPixel > applicator = this . brush . CreateApplicator (
67- source ,
68- sourceRectangle ,
69- this . options ) )
55+ // If there's no reason for blending, then avoid it.
56+ if ( this . IsSolidBrushWithoutBlending ( out SolidBrush < TPixel > solidBrush ) )
7057 {
71- amount . Span . Fill ( 1f ) ;
72-
7358 Parallel . For (
7459 minY ,
7560 maxY ,
7661 configuration . ParallelOptions ,
7762 y =>
78- {
79- int offsetY = y - startY ;
80- int offsetX = minX - startX ;
63+ {
64+ source . GetPixelRowSpan ( y ) . Slice ( minX , width ) . Fill ( solidBrush . Color ) ;
65+ } ) ;
66+ }
67+ else
68+ {
69+ // Reset offset if necessary.
70+ if ( minX > 0 )
71+ {
72+ startX = 0 ;
73+ }
74+
75+ if ( minY > 0 )
76+ {
77+ startY = 0 ;
78+ }
79+
80+ using ( IBuffer < float > amount = source . MemoryManager . Allocate < float > ( width ) )
81+ using ( BrushApplicator < TPixel > applicator = this . brush . CreateApplicator (
82+ source ,
83+ sourceRectangle ,
84+ this . options ) )
85+ {
86+ amount . Span . Fill ( 1f ) ;
87+
88+ Parallel . For (
89+ minY ,
90+ maxY ,
91+ configuration . ParallelOptions ,
92+ y =>
93+ {
94+ int offsetY = y - startY ;
95+ int offsetX = minX - startX ;
8196
82- applicator . Apply ( amount . Span , offsetX , offsetY ) ;
83- } ) ;
97+ applicator . Apply ( amount . Span , offsetX , offsetY ) ;
98+ } ) ;
99+ }
84100 }
85101 }
102+
103+ private bool IsSolidBrushWithoutBlending ( out SolidBrush < TPixel > solidBrush )
104+ {
105+ solidBrush = this . brush as SolidBrush < TPixel > ;
106+
107+ return solidBrush != null
108+ && ( ( this . options . BlenderMode == PixelBlenderMode . Normal && this . options . BlendPercentage == 1f
109+ && solidBrush . Color . ToVector4 ( ) . W == 1f )
110+ || ( this . options . BlenderMode == PixelBlenderMode . Over && this . options . BlendPercentage == 1f
111+ && solidBrush . Color . ToVector4 ( ) . W == 1f )
112+ || ( this . options . BlenderMode == PixelBlenderMode . Src ) ) ;
113+ }
86114 }
87115}
0 commit comments