private unsafe void LinearFloodFill4Gray( int x, int y )
{
// get image pointer for current (X, Y)
byte* p = (byte*) CoordsToPointerGray( x, y );
// find left end of line to fill
int leftLineEdge = x;
byte* ptr = p;
while ( true )
{
// sum value of the current pixel
meanG += *ptr;
pixelsCount++;
// mark the pixel as checked
checkedPixels[y, leftLineEdge] = true;
leftLineEdge--;
ptr -= 1;
// check if we need to stop on the edge of image or color area
if ( ( leftLineEdge < startX ) || ( checkedPixels[y, leftLineEdge] ) || ( !CheckGrayPixel( *ptr ) ) )
break;
}
leftLineEdge++;
// find right end of line to fill
int rightLineEdge = x + 1;
ptr = p + 1;
// while we don't need to stop on the edge of image or color area
while ( !( rightLineEdge > stopX || ( checkedPixels[y, rightLineEdge] ) || ( !CheckGrayPixel( *ptr ) ) ) )
{
// sum value of the current pixel
meanG += *ptr;
pixelsCount++;
// mark the pixel as checked
checkedPixels[y, rightLineEdge] = true;
rightLineEdge++;
ptr += 1;
}
rightLineEdge--;
// loop to go up and down
ptr = (byte*) CoordsToPointerGray( leftLineEdge, y );
for ( int i = leftLineEdge; i <= rightLineEdge; i++, ptr++ )
{
// go up
if ( ( y > startY ) && ( !checkedPixels[y - 1, i] ) && ( CheckGrayPixel( *( ptr - stride ) ) ) )
LinearFloodFill4Gray( i, y - 1 );
// go down
if ( ( y < stopY ) && ( !checkedPixels[y + 1, i] ) && ( CheckGrayPixel( *( ptr + stride ) ) ) )
LinearFloodFill4Gray( i, y + 1 );
}
}