@@ -39,8 +39,6 @@ internal sealed class ResizeWorker<TPixel> : IDisposable
3939
4040 private readonly ResizeKernelMap verticalKernelMap ;
4141
42- private readonly int destWidth ;
43-
4442 private readonly Rectangle targetWorkingRect ;
4543
4644 private readonly Point targetOrigin ;
@@ -57,7 +55,6 @@ public ResizeWorker(
5755 PixelConversionModifiers conversionModifiers ,
5856 ResizeKernelMap horizontalKernelMap ,
5957 ResizeKernelMap verticalKernelMap ,
60- int destWidth ,
6158 Rectangle targetWorkingRect ,
6259 Point targetOrigin )
6360 {
@@ -67,7 +64,6 @@ public ResizeWorker(
6764 this . conversionModifiers = conversionModifiers ;
6865 this . horizontalKernelMap = horizontalKernelMap ;
6966 this . verticalKernelMap = verticalKernelMap ;
70- this . destWidth = destWidth ;
7167 this . targetWorkingRect = targetWorkingRect ;
7268 this . targetOrigin = targetOrigin ;
7369
@@ -80,19 +76,19 @@ public ResizeWorker(
8076
8177 int numberOfWindowBands = ResizeHelper . CalculateResizeWorkerHeightInWindowBands (
8278 this . windowBandHeight ,
83- destWidth ,
79+ targetWorkingRect . Width ,
8480 workingBufferLimitHintInBytes ) ;
8581
8682 this . workerHeight = Math . Min ( this . sourceRectangle . Height , numberOfWindowBands * this . windowBandHeight ) ;
8783
8884 this . transposedFirstPassBuffer = configuration . MemoryAllocator . Allocate2D < Vector4 > (
8985 this . workerHeight ,
90- destWidth ,
86+ targetWorkingRect . Width ,
9187 preferContiguosImageBuffers : true ,
9288 options : AllocationOptions . Clean ) ;
9389
9490 this . tempRowBuffer = configuration . MemoryAllocator . Allocate < Vector4 > ( this . sourceRectangle . Width ) ;
95- this . tempColumnBuffer = configuration . MemoryAllocator . Allocate < Vector4 > ( destWidth ) ;
91+ this . tempColumnBuffer = configuration . MemoryAllocator . Allocate < Vector4 > ( targetWorkingRect . Width ) ;
9692
9793 this . currentWindow = new RowInterval ( 0 , this . workerHeight ) ;
9894 }
@@ -118,6 +114,9 @@ public void FillDestinationPixels(RowInterval rowInterval, Buffer2D<TPixel> dest
118114 // When creating transposedFirstPassBuffer, we made sure it's contiguous:
119115 Span < Vector4 > transposedFirstPassBufferSpan = this . transposedFirstPassBuffer . DangerousGetSingleSpan ( ) ;
120116
117+ int left = this . targetWorkingRect . Left ;
118+ int right = this . targetWorkingRect . Right ;
119+ int width = this . targetWorkingRect . Width ;
121120 for ( int y = rowInterval . Min ; y < rowInterval . Max ; y ++ )
122121 {
123122 // Ensure offsets are normalized for cropping and padding.
@@ -131,17 +130,18 @@ public void FillDestinationPixels(RowInterval rowInterval, Buffer2D<TPixel> dest
131130 ref Vector4 tempRowBase = ref MemoryMarshal . GetReference ( tempColSpan ) ;
132131
133132 int top = kernel . StartIndex - this . currentWindow . Min ;
133+
134134 ref Vector4 fpBase = ref transposedFirstPassBufferSpan [ top ] ;
135135
136- for ( int x = 0 ; x < this . destWidth ; x ++ )
136+ for ( nint x = 0 ; x < ( right - left ) ; x ++ )
137137 {
138138 ref Vector4 firstPassColumnBase = ref Unsafe . Add ( ref fpBase , x * this . workerHeight ) ;
139139
140140 // Destination color components
141141 Unsafe . Add ( ref tempRowBase , x ) = kernel. ConvolveCore ( ref firstPassColumnBase ) ;
142142 }
143143
144- Span < TPixel > targetRowSpan = destination . DangerousGetRowSpan ( y ) ;
144+ Span < TPixel > targetRowSpan = destination . DangerousGetRowSpan ( y ) . Slice ( left , width ) ;
145145
146146 PixelOperations < TPixel > . Instance . FromVector4Destructive ( this . configuration , tempColSpan , targetRowSpan , this . conversionModifiers ) ;
147147 }
@@ -170,6 +170,9 @@ private void CalculateFirstPassValues(RowInterval calculationInterval)
170170 Span < Vector4 > tempRowSpan = this . tempRowBuffer . GetSpan ( ) ;
171171 Span < Vector4 > transposedFirstPassBufferSpan = this . transposedFirstPassBuffer . DangerousGetSingleSpan ( ) ;
172172
173+ int left = this . targetWorkingRect . Left ;
174+ int right = this . targetWorkingRect . Right ;
175+ int targetOriginX = this . targetOrigin . X ;
173176 for ( int y = calculationInterval . Min ; y < calculationInterval . Max ; y ++ )
174177 {
175178 Span < TPixel > sourceRow = this . source . DangerousGetRowSpan ( y ) ;
@@ -184,13 +187,13 @@ private void CalculateFirstPassValues(RowInterval calculationInterval)
184187 // Span<Vector4> firstPassSpan = transposedFirstPassBufferSpan.Slice(y - this.currentWindow.Min);
185188 ref Vector4 firstPassBaseRef = ref transposedFirstPassBufferSpan [ y - this . currentWindow . Min ] ;
186189
187- for ( int x = this . targetWorkingRect . Left ; x < this . targetWorkingRect . Right ; x ++ )
190+ for ( nint x = left , z = 0 ; x < right ; x ++ , z ++ )
188191 {
189- ResizeKernel kernel = this . horizontalKernelMap . GetKernel ( x - this . targetOrigin . X ) ;
192+ ResizeKernel kernel = this . horizontalKernelMap . GetKernel ( x - targetOriginX ) ;
190193
191194 // optimization for:
192195 // firstPassSpan[x * this.workerHeight] = kernel.Convolve(tempRowSpan);
193- Unsafe . Add ( ref firstPassBaseRef , x * this . workerHeight ) = kernel. Convolve ( tempRowSpan ) ;
196+ Unsafe . Add ( ref firstPassBaseRef , z * this . workerHeight ) = kernel. Convolve ( tempRowSpan ) ;
194197 }
195198 }
196199 }
0 commit comments