@@ -184,34 +184,39 @@ public override void PrepareForDecoding()
184
184
185
185
MemoryAllocator allocator = this . Configuration . MemoryAllocator ;
186
186
187
- // color converter from RGB to TPixel
187
+ // Color converter from RGB to TPixel
188
188
JpegColorConverterBase converter = this . GetColorConverter ( this . frame , this . jpegData ) ;
189
189
this . colorConverter = converter ;
190
190
191
- // resulting image size
191
+ // Resulting image size
192
192
Size pixelSize = CalculateResultingImageSize ( this . frame . PixelSize , this . targetSize , out int blockPixelSize ) ;
193
193
194
- // iteration data
194
+ // Iteration data
195
195
int majorBlockWidth = this . frame . Components . Max ( ( component ) => component . SizeInBlocks . Width ) ;
196
196
int majorVerticalSamplingFactor = this . frame . Components . Max ( ( component ) => component . SamplingFactors . Height ) ;
197
197
198
198
this . pixelRowsPerStep = majorVerticalSamplingFactor * blockPixelSize ;
199
199
200
- // pixel buffer for resulting image
200
+ // Pixel buffer for resulting image
201
201
this . pixelBuffer = allocator . Allocate2D < TPixel > (
202
202
pixelSize . Width ,
203
203
pixelSize . Height ,
204
204
this . Configuration . PreferContiguousImageBuffers ) ;
205
205
this . paddedProxyPixelRow = allocator . Allocate < TPixel > ( pixelSize . Width + 3 ) ;
206
206
207
- // component processors from spectral to RGB
207
+ // Component processors from spectral to RGB
208
208
int bufferWidth = majorBlockWidth * blockPixelSize ;
209
- int batchSize = converter . ElementsPerBatch ;
210
- int batchRemainder = bufferWidth & ( batchSize - 1 ) ;
211
- Size postProcessorBufferSize = new ( bufferWidth + ( batchSize - batchRemainder ) , this . pixelRowsPerStep ) ;
209
+
210
+ // Converters process pixels in batches and require target buffer size to be divisible by a batch size
211
+ // Corner case: image size including jpeg padding is already divisible by a batch size or remainder == 0
212
+ int elementsPerBatch = converter . ElementsPerBatch ;
213
+ int batchRemainder = bufferWidth & ( elementsPerBatch - 1 ) ;
214
+ int widthComplementaryValue = batchRemainder == 0 ? 0 : elementsPerBatch - batchRemainder ;
215
+
216
+ Size postProcessorBufferSize = new ( bufferWidth + widthComplementaryValue , this . pixelRowsPerStep ) ;
212
217
this . componentProcessors = this . CreateComponentProcessors ( this . frame , this . jpegData , blockPixelSize , postProcessorBufferSize ) ;
213
218
214
- // single 'stride' rgba32 buffer for conversion between spectral and TPixel
219
+ // Single 'stride' rgba32 buffer for conversion between spectral and TPixel
215
220
this . rgbBuffer = allocator . Allocate < byte > ( pixelSize . Width * 3 ) ;
216
221
}
217
222
0 commit comments