Seed filling algorithm of CGA filling algorithm
The plane area filling algorithm is an important algorithm in the field of computer graphics, which gives the boundary of an area (or the absence of a boundary, but only the specified color), which requires that all the pixel elements within the boundary range be modified to the specified color (or it may be a hatch). The most common area fill is polygon fills, this article discusses the seed fill algorithm (seed Filling)
If the area you want to populate is given as image metadata , you typically use the seed fill algorithm (seed Filling) for area fills. The seed filling algorithm needs to give the area of image data, and a point within the region, which is suitable for the image filling operation of human-computer interaction, and is not suitable for automatic processing and judging of fill color. Depending on how the boundary of the image is defined and how the color of the point is modified, the seed fill can be subdivided into several categories:
For example: ① injection Fill algorithm (Flood fill algorithm),
② Boundary Fill algorithm (Boundary fill algorithm) and
③ improved scanning line seed filling algorithm to reduce recursion and stack count, and so on.
The core of all the seed filling algorithms is actually a recursive algorithm , which starts from the specified seed point, searches in all directions, processes pixels by pixel, until a boundary is encountered, and the various seed filling algorithms differ only in how colors and boundaries are handled.
Before we begin to introduce the seed filling algorithm, we first introduce two concepts, namely "4-Unicom algorithm" and "8-unicom algorithm".
Since the search is related to the direction of the search, from any point in the region, if only through the top, bottom, left, and right four directions to search for any pixel in the region, then the area filled with this method is called the four connected domain, this fill method is called "4-unicom algorithm." If you start from any point in the region, through the upper, lower, left, right, top left, bottom left, right, upper and lower right all eight directions to reach any pixel within the area, then this method fills the area is called the eight connected domain, this fill method is called "8-unicom algorithm."
1 (a), assuming that the center's Blue point is the current processing point, if it is a "4-unicom algorithm", then only the four points to deal with the surrounding blue identification, if the "8-Unicom algorithm" In addition to processing the top, bottom, left, and right four blue identified points, but also the processing of four red identified points. The fill effects of the two search algorithms are shown in 1 (b) and Figure 1 (c), respectively. If all are filled from the yellow point, then "4-Unicom algorithm" 1 (b) is only searched for the area that fills the lower left corner, while the "8-Unicom algorithm" is 1 (c), the lower-left and upper-right corner of the area are populated.
Not just because the fill effect of Figure 1 is that "8-unicom algorithm" is better than the "4-unicom algorithm", should be based on the application environment and the actual needs of the choice of Unicom search mode, in many cases, only the "4-unicom algorithm" to get the correct results.
1. Inject Fill algorithm (Flood fill algorithm)
The boundary filling algorithm is the same as the essence of the injected filling algorithm, which is recursive and search, the difference is only the confirmation of the boundary, that is, the end condition of recursion is not the same. The injection-fill algorithm has no boundary concept, but replaces the specified color in the Unicom region, while the boundary filling algorithm emphasizes the existence of the boundary, as long as the point within the boundary, regardless of the color, is replaced by the specified color. The boundary filling algorithm is also widely used in the application, and the "Paint bucket" function in drawing software is an example of the boundary filling algorithm. The following is an implementation of the Boundary fill algorithm:
void Floodseedfill (int. int int int new_color) { if( Getpixelcolor (x, y) = = Old_color) { setpixelcolor (x, y, New_color) ; for (int0; i < count_of (direction_8); i++) { + direction_8[i].x_offset, + direction_8[i].y_offset, Old_color, New_color); } }}
The for Loop implements a recursive search to 8 unicom directions, the secret is defined in Direction_8:
typedefstructtagdirection{intX_offset; intY_offset;} DIRECTION; DIRECTION direction_8[]= { {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1, -1}, {0, -1}, {-1, -1} };
Is the use of this algorithm to achieve the "4-unicom" and "8-unicom" filling effect:
2. Boundary Fill algorithm (Boundary fill algorithm)
The boundary filling algorithm is the same as the essence of the injected filling algorithm, which is recursive and search, the difference is only the confirmation of the boundary, that is, the end condition of recursion is not the same. The injection-fill algorithm has no boundary concept, but replaces the specified color in the Unicom region, while the boundary filling algorithm emphasizes the existence of the boundary, as long as the point within the boundary, regardless of the color, is replaced by the specified color. The boundary filling algorithm is also widely used in the application, and the "Paint bucket" function in drawing software is an example of the boundary filling algorithm. The following is an implementation of the Boundary fill algorithm:
voidBoundaryseedfill (intXintYintNew_color,intBoundary_color) { intCurcolor =getpixelcolor (x, y); if((Curcolor! = Boundary_color) && (curcolor! =New_color)) {Setpixelcolor (x, y, New_color); for(inti =0; I < count_of (direction_8); i++) {Boundaryseedfill (x+Direction_8[i].x_offset, y+Direction_8[i].y_offset, New_color, Boundary_color); } }}
For a description of the Direction_8, refer to the previous section, figure 3 is the implementation of the algorithm using the "4-unicom" and "8-unicom" fill effect (where the color value is 1 of the point is the specified boundary):
3. Boundary Fill algorithm (Boundary fill algorithm)
Reference URL:
http://blog.csdn.net/orbit/article/details/7323090
Seed filling algorithm of CGA filling algorithm