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