28 PrepareForFloodFill(pt);
33 int x = (int)pt.X;
int y = (int)pt.Y;
34 int idx = CoordsToByteIndex(ref x, ref y);
35 startColor = bitmap.GetPixel((
int)pt.X,(
int)pt.Y);
37 bool[] pixelsChecked = this.pixelsChecked;
40 LinearFill(ref x, ref y);
43 while (ranges.
Count > 0)
49 int downPxIdx = (bitmapWidth * (range.Y + 1)) + range.StartX;
50 int upPxIdx = (bitmapWidth * (range.Y - 1)) + range.StartX;
51 int upY = range.Y - 1;
52 int downY = range.Y + 1;
54 for (
int i = range.StartX; i <= range.EndX; i++)
58 tempIdx = CoordsToByteIndex(ref i, ref upY);
59 if (range.Y > 0 && (!pixelsChecked[upPxIdx]) &&
CheckPixel(ref tempIdx))
60 LinearFill(ref i, ref upY);
64 tempIdx = CoordsToByteIndex(ref i, ref downY);
65 if (range.Y < (bitmapHeight - 1) && (!pixelsChecked[downPxIdx]) &&
CheckPixel(ref tempIdx))
66 LinearFill(ref i, ref downY);
84 void LinearFill(ref
int x, ref
int y)
89 byte[] bitmapBits = this.bitmapBits;
90 bool[] pixelsChecked = this.pixelsChecked;
91 byte[] byteFillColor = this.byteFillColor.GetBytes(pixelFormat);
92 int bitmapPixelFormatSize = this.bitmapPixelFormatSize;
93 int bitmapWidth = this.bitmapWidth;
94 byte[] bt =
new byte[6];
97 int idx = CoordsToByteIndex(ref x, ref y);
98 int pxIdx = (bitmapWidth * y) + x;
103 if (pixelFormat == PixelFormat.Format8bppIndexed)
105 bitmapBits[idx] = byteFillColor[0];
108 if (pixelFormat == PixelFormat.Format24bppRgb)
110 bitmapBits[idx] = byteFillColor[0];
111 bitmapBits[idx + 1] = byteFillColor[1];
112 bitmapBits[idx + 2] = byteFillColor[2];
114 if (pixelFormat == PixelFormat.Format32bppArgb || pixelFormat == PixelFormat.Format32bppRgb)
116 bitmapBits[idx + 1] = byteFillColor[0];
117 bitmapBits[idx + 2] = byteFillColor[1];
118 bitmapBits[idx + 3] = byteFillColor[2];
120 else if (pixelFormat == PixelFormat.Format16bppGrayScale)
122 bitmapBits[idx] = byteFillColor[0];
123 bitmapBits[idx + 1] = byteFillColor[1];
125 else if (pixelFormat == PixelFormat.Format48bppRgb)
127 bitmapBits[idx] = byteFillColor[0];
128 bitmapBits[idx + 1] = byteFillColor[1];
129 bitmapBits[idx + 2] = byteFillColor[2];
130 bitmapBits[idx + 3] = byteFillColor[3];
131 bitmapBits[idx + 4] = byteFillColor[4];
132 bitmapBits[idx + 5] = byteFillColor[5];
135 pixelsChecked[pxIdx] =
true;
139 idx -= bitmapPixelFormatSize;
141 if (lFillLoc <= 0 || (pixelsChecked[pxIdx]) || !
CheckPixel(ref idx))
149 idx = CoordsToByteIndex(ref x, ref y);
150 pxIdx = (bitmapWidth * y) + x;
155 if (pixelFormat == PixelFormat.Format8bppIndexed)
157 bitmapBits[idx] = byteFillColor[0];
160 if (pixelFormat == PixelFormat.Format24bppRgb)
162 bitmapBits[idx] = byteFillColor[2];
163 bitmapBits[idx + 1] = byteFillColor[1];
164 bitmapBits[idx + 2] = byteFillColor[0];
167 if (pixelFormat == PixelFormat.Format32bppArgb || pixelFormat == PixelFormat.Format32bppRgb)
169 bitmapBits[idx + 1] = byteFillColor[0];
170 bitmapBits[idx + 2] = byteFillColor[1];
171 bitmapBits[idx + 3] = byteFillColor[2];
173 else if (pixelFormat == PixelFormat.Format16bppGrayScale)
175 bitmapBits[idx] = byteFillColor[0];
176 bitmapBits[idx + 1] = byteFillColor[1];
178 else if (pixelFormat == PixelFormat.Format48bppRgb)
180 bitmapBits[idx] = byteFillColor[0];
181 bitmapBits[idx + 1] = byteFillColor[1];
182 bitmapBits[idx + 2] = byteFillColor[2];
183 bitmapBits[idx + 3] = byteFillColor[3];
184 bitmapBits[idx + 4] = byteFillColor[4];
185 bitmapBits[idx + 5] = byteFillColor[5];
188 pixelsChecked[pxIdx] =
true;
192 idx += bitmapPixelFormatSize;
194 if (rFillLoc >= bitmapWidth || pixelsChecked[pxIdx] || !
CheckPixel(ref idx))
201 FloodFillRange r =
new FloodFillRange(lFillLoc, rFillLoc, y);
217 if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed)
219 return (bitmapBits[px] >= (startColor.R - tolerance.R)) && bitmapBits[px] <= (startColor.R + tolerance.R);
222 if (bitmap.PixelFormat == PixelFormat.Format24bppRgb)
224 return (bitmapBits[px] >= (startColor.R - tolerance.R)) && bitmapBits[px] <= (startColor.R + tolerance.R) &&
225 (bitmapBits[px + 1] >= (startColor.G - tolerance.G)) && bitmapBits[px + 1] <= (startColor.G + tolerance.G) &&
226 (bitmapBits[px + 2] >= (startColor.B - tolerance.B)) && bitmapBits[px + 2] <= (startColor.B + tolerance.B);
229 if (bitmap.PixelFormat == PixelFormat.Format32bppArgb || bitmap.PixelFormat == PixelFormat.Format32bppRgb)
231 return (bitmapBits[px + 3] >= (startColor.R - tolerance.R)) && bitmapBits[px + 3] <= (startColor.R + tolerance.R) &&
232 (bitmapBits[px + 2] >= (startColor.G - tolerance.G)) && bitmapBits[px + 2] <= (startColor.G + tolerance.G) &&
233 (bitmapBits[px + 1] >= (startColor.B - tolerance.B)) && bitmapBits[px + 1] <= (startColor.B + tolerance.B);
236 if (bitmap.PixelFormat == PixelFormat.Format16bppGrayScale)
238 ushort r = BitConverter.ToUInt16(bitmapBits, px);
239 return (r >= (startColor.R - tolerance.R)) && r <= (startColor.R + tolerance.R);
242 if (bitmap.PixelFormat == PixelFormat.Format48bppRgb)
244 ushort r = BitConverter.ToUInt16(bitmapBits, px);
245 ushort g = BitConverter.ToUInt16(bitmapBits, px + 2);
246 ushort b = BitConverter.ToUInt16(bitmapBits, px + 4);
247 return (r >= (startColor.R - tolerance.R)) && r <= (startColor.R + tolerance.R) &&
248 (g >= (startColor.G - tolerance.G)) && g <= (startColor.G + tolerance.G) &&
249 (b >= (startColor.B - tolerance.B)) && b <= (startColor.B + tolerance.B);
251 throw new NotSupportedException(bitmap.PixelFormat +
" not supported");
257 protected int CoordsToByteIndex(ref
int x, ref
int y)
259 return (bitmapStride * y) + (x * bitmapPixelFormatSize);
270 return (bitmapWidth * y) + x;