protected override unsafe void Diffuse( int error, byte* ptr )
{
int ed; // error diffusion
// do error diffusion to right-standing neighbors
int[] coefficientsRow = coefficients[0];
for ( int jI = 1, jC = 0, k = coefficientsRow.Length; jC < k; jI++, jC++ )
{
if ( x + jI >= stopX )
break;
ed = ptr[jI] + ( error * coefficientsRow[jC] ) / coefficientsSum;
ed = ( ed < 0 ) ? 0 : ( ( ed > 255 ) ? 255 : ed );
ptr[jI] = (byte) ed;
}
// do error diffusion to bottom neigbors
for ( int i = 1, n = coefficients.Length; i < n; i++ )
{
if ( y + i >= stopY )
break;
// move pointer to next image line
ptr += stride;
// get coefficients of the row
coefficientsRow = coefficients[i];
// process the row
for ( int jC = 0, k = coefficientsRow.Length, jI = -( k >> 1 ); jC < k; jI++, jC++ )
{
if ( x + jI >= stopX )
break;
if ( x + jI < startX )
continue;
ed = ptr[jI] + ( error * coefficientsRow[jC] ) / coefficientsSum;
ed = ( ed < 0 ) ? 0 : ( ( ed > 255 ) ? 255 : ed );
ptr[jI] = (byte) ed;
}
}
}