@@ -354,9 +354,11 @@ public void EncodeComponents(ReadOnlySpan<byte> source, int sourceComponentCount
354354 }
355355 else
356356 {
357- int scanStride = CheckStrideAndSourceLength ( source . Length , stride , sourceComponentCount ) ;
357+ int scanWidth = GetScanWidth ( _encodedComponentCount ) ;
358+ int scanHeight = GetScanHeight ( _encodedComponentCount ) ;
359+ int scanStride = CheckStrideAndSourceLength ( source . Length , stride , scanWidth , scanHeight , sourceComponentCount ) ;
358360 _writer . WriteStartOfScanSegment ( sourceComponentCount , NearLossless , InterleaveMode ) ;
359- EncodeScan ( source , scanStride , FrameInfo . Width , FrameInfo . Height , sourceComponentCount , explicitCodingParameters ) ;
361+ EncodeScan ( source , scanStride , scanWidth , scanHeight , sourceComponentCount , explicitCodingParameters ) ;
360362 }
361363
362364 _encodedComponentCount += sourceComponentCount ;
@@ -651,9 +653,9 @@ private void CheckInterleaveModeAgainstComponentCount(int componentCount)
651653 ThrowHelper . ThrowArgumentException ( ErrorCode . InvalidArgumentInterleaveMode ) ;
652654 }
653655
654- private int CheckStrideAndSourceLength ( int sourceLength , int stride , int sourceComponentCount )
656+ private int CheckStrideAndSourceLength ( int sourceLength , int stride , int scanWidth , int scanHeight , int sourceComponentCount )
655657 {
656- int minimumStride = CalculateMinimumStride ( sourceComponentCount ) ;
658+ int minimumStride = CalculateMinimumStride ( scanWidth , sourceComponentCount ) ;
657659
658660 if ( stride == AutoCalculateStride )
659661 {
@@ -667,8 +669,8 @@ private int CheckStrideAndSourceLength(int sourceLength, int stride, int sourceC
667669
668670 int notUsedBytesAtEnd = stride - minimumStride ;
669671 int minimumSourceLength = InterleaveMode == InterleaveMode . None
670- ? ( stride * sourceComponentCount * FrameInfo . Height ) - notUsedBytesAtEnd
671- : ( stride * FrameInfo . Height ) - notUsedBytesAtEnd ;
672+ ? ( stride * sourceComponentCount * scanHeight ) - notUsedBytesAtEnd
673+ : ( stride * scanHeight ) - notUsedBytesAtEnd ;
672674
673675 if ( sourceLength < minimumSourceLength )
674676 ThrowHelper . ThrowArgumentException ( ErrorCode . InvalidArgumentSize ) ;
@@ -698,13 +700,20 @@ private int CheckStrideAndSourceLengthInterleaveModeNone(int sourceLength, int s
698700 return stride ;
699701 }
700702
701- private int CalculateMinimumStride ( int sourceComponentCount )
703+ private int CalculateMinimumStride ( int scanWidth , int sourceComponentCount )
702704 {
703- int stride = FrameInfo . Width * BitToByteCount ( FrameInfo . BitsPerSample ) ;
704- if ( _interleaveMode == InterleaveMode . None )
705- return stride ;
705+ switch ( _interleaveMode )
706+ {
707+ case InterleaveMode . None :
708+ return FrameInfo . Width * BitToByteCount ( FrameInfo . BitsPerSample ) ;
709+
710+ case InterleaveMode . Line :
711+ return FrameInfo . Width * BitToByteCount ( FrameInfo . BitsPerSample ) * sourceComponentCount ;
706712
707- return stride * sourceComponentCount ;
713+ default :
714+ Debug . Assert ( _interleaveMode == InterleaveMode . Sample ) ;
715+ return scanWidth * BitToByteCount ( FrameInfo . BitsPerSample ) * sourceComponentCount ;
716+ }
708717 }
709718
710719 private void DetermineMaxSamplingFactors ( )
0 commit comments