22// Licensed under the Apache License, Version 2.0.
33
44using System ;
5- using System . Runtime . CompilerServices ;
6- using System . Runtime . InteropServices ;
7-
85using SixLabors . ImageSharp . Memory ;
96
107namespace SixLabors . ImageSharp . Formats . Jpeg . Components . Decoder
118{
129 /// <summary>
13- /// Encapsulates postprocessing data for one component for <see cref="JpegImagePostProcessor"/> .
10+ /// Encapsulates spectral data to rgba32 processing for one component .
1411 /// </summary>
1512 internal class JpegComponentPostProcessor : IDisposable
1613 {
@@ -27,23 +24,20 @@ internal class JpegComponentPostProcessor : IDisposable
2724 /// <summary>
2825 /// Initializes a new instance of the <see cref="JpegComponentPostProcessor"/> class.
2926 /// </summary>
30- public JpegComponentPostProcessor ( MemoryAllocator memoryAllocator , JpegImagePostProcessor imagePostProcessor , IJpegComponent component )
27+ public JpegComponentPostProcessor ( MemoryAllocator memoryAllocator , IRawJpegData rawJpeg , Size postProcessorBufferSize , IJpegComponent component )
3128 {
3229 this . Component = component ;
33- this . ImagePostProcessor = imagePostProcessor ;
30+ this . RawJpeg = rawJpeg ;
3431 this . blockAreaSize = this . Component . SubSamplingDivisors * 8 ;
3532 this . ColorBuffer = memoryAllocator . Allocate2DOveraligned < float > (
36- imagePostProcessor . PostProcessorBufferSize . Width ,
37- imagePostProcessor . PostProcessorBufferSize . Height ,
33+ postProcessorBufferSize . Width ,
34+ postProcessorBufferSize . Height ,
3835 this . blockAreaSize . Height ) ;
3936
40- this . BlockRowsPerStep = JpegImagePostProcessor . BlockRowsPerStep / this . Component . SubSamplingDivisors . Height ;
37+ this . BlockRowsPerStep = postProcessorBufferSize . Height / 8 / this . Component . SubSamplingDivisors . Height ;
4138 }
4239
43- /// <summary>
44- /// Gets the <see cref="JpegImagePostProcessor"/>
45- /// </summary>
46- public JpegImagePostProcessor ImagePostProcessor { get ; }
40+ public IRawJpegData RawJpeg { get ; }
4741
4842 /// <summary>
4943 /// Gets the <see cref="Component"/>
@@ -66,37 +60,38 @@ public JpegComponentPostProcessor(MemoryAllocator memoryAllocator, JpegImagePost
6660 public int BlockRowsPerStep { get ; }
6761
6862 /// <inheritdoc />
69- public void Dispose ( )
70- {
71- this . ColorBuffer . Dispose ( ) ;
72- }
63+ public void Dispose ( ) => this . ColorBuffer . Dispose ( ) ;
7364
7465 /// <summary>
7566 /// Invoke <see cref="JpegBlockPostProcessor"/> for <see cref="BlockRowsPerStep"/> block rows, copy the result into <see cref="ColorBuffer"/>.
7667 /// </summary>
77- public void CopyBlocksToColorBuffer ( )
68+ public void CopyBlocksToColorBuffer ( int step )
7869 {
79- var blockPp = new JpegBlockPostProcessor ( this . ImagePostProcessor . RawJpeg , this . Component ) ;
80- float maximumValue = MathF . Pow ( 2 , this . ImagePostProcessor . RawJpeg . Precision ) - 1 ;
70+ Buffer2D < Block8x8 > spectralBuffer = this . Component . SpectralBlocks ;
71+
72+ var blockPp = new JpegBlockPostProcessor ( this . RawJpeg , this . Component ) ;
73+ float maximumValue = MathF . Pow ( 2 , this . RawJpeg . Precision ) - 1 ;
8174
8275 int destAreaStride = this . ColorBuffer . Width ;
8376
77+ int yBlockStart = step * this . BlockRowsPerStep ;
78+
8479 for ( int y = 0 ; y < this . BlockRowsPerStep ; y ++ )
8580 {
86- int yBlock = this . currentComponentRowInBlocks + y ;
81+ int yBlock = yBlockStart + y ;
8782
88- if ( yBlock >= this . SizeInBlocks . Height )
83+ if ( yBlock >= spectralBuffer . Height )
8984 {
9085 break ;
9186 }
9287
9388 int yBuffer = y * this . blockAreaSize . Height ;
9489
9590 Span < float > colorBufferRow = this . ColorBuffer . GetRowSpan ( yBuffer ) ;
96- Span < Block8x8 > blockRow = this . Component . SpectralBlocks . GetRowSpan ( yBlock ) ;
91+ Span < Block8x8 > blockRow = spectralBuffer . GetRowSpan ( yBlock ) ;
9792
9893 // see: https://github.com/SixLabors/ImageSharp/issues/824
99- int widthInBlocks = Math . Min ( this . Component . SpectralBlocks . Width , this . SizeInBlocks . Width ) ;
94+ int widthInBlocks = Math . Min ( spectralBuffer . Width , this . SizeInBlocks . Width ) ;
10095
10196 for ( int xBlock = 0 ; xBlock < widthInBlocks ; xBlock ++ )
10297 {
@@ -107,7 +102,20 @@ public void CopyBlocksToColorBuffer()
107102 blockPp . ProcessBlockColorsInto ( ref block , ref destAreaOrigin , destAreaStride , maximumValue ) ;
108103 }
109104 }
105+ }
106+
107+ public void ClearSpectralBuffers ( )
108+ {
109+ Buffer2D < Block8x8 > spectralBlocks = this . Component . SpectralBlocks ;
110+ for ( int i = 0 ; i < spectralBlocks . Height ; i ++ )
111+ {
112+ spectralBlocks . GetRowSpan ( i ) . Clear ( ) ;
113+ }
114+ }
110115
116+ public void CopyBlocksToColorBuffer ( )
117+ {
118+ this . CopyBlocksToColorBuffer ( this . currentComponentRowInBlocks ) ;
111119 this . currentComponentRowInBlocks += this . BlockRowsPerStep ;
112120 }
113121 }
0 commit comments