@@ -184,34 +184,39 @@ public override void PrepareForDecoding()
184184
185185 MemoryAllocator allocator = this . Configuration . MemoryAllocator ;
186186
187- // color converter from RGB to TPixel
187+ // Color converter from RGB to TPixel
188188 JpegColorConverterBase converter = this . GetColorConverter ( this . frame , this . jpegData ) ;
189189 this . colorConverter = converter ;
190190
191- // resulting image size
191+ // Resulting image size
192192 Size pixelSize = CalculateResultingImageSize ( this . frame . PixelSize , this . targetSize , out int blockPixelSize ) ;
193193
194- // iteration data
194+ // Iteration data
195195 int majorBlockWidth = this . frame . Components . Max ( ( component ) => component . SizeInBlocks . Width ) ;
196196 int majorVerticalSamplingFactor = this . frame . Components . Max ( ( component ) => component . SamplingFactors . Height ) ;
197197
198198 this . pixelRowsPerStep = majorVerticalSamplingFactor * blockPixelSize ;
199199
200- // pixel buffer for resulting image
200+ // Pixel buffer for resulting image
201201 this . pixelBuffer = allocator . Allocate2D < TPixel > (
202202 pixelSize . Width ,
203203 pixelSize . Height ,
204204 this . Configuration . PreferContiguousImageBuffers ) ;
205205 this . paddedProxyPixelRow = allocator . Allocate < TPixel > ( pixelSize . Width + 3 ) ;
206206
207- // component processors from spectral to RGB
207+ // Component processors from spectral to RGB
208208 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 ) ;
212217 this . componentProcessors = this . CreateComponentProcessors ( this . frame , this . jpegData , blockPixelSize , postProcessorBufferSize ) ;
213218
214- // single 'stride' rgba32 buffer for conversion between spectral and TPixel
219+ // Single 'stride' rgba32 buffer for conversion between spectral and TPixel
215220 this . rgbBuffer = allocator . Allocate < byte > ( pixelSize . Width * 3 ) ;
216221 }
217222
0 commit comments