8

Click here to load reader

A two-pass filling algorithm for raster graphics

Embed Size (px)

Citation preview

Page 1: A two-pass filling algorithm for raster graphics

COMPUTER GRAPHICS AND IMAGE PROCESSING 20, 288-295 (1982)

NOTE

A Two-Pass Filling Algorithm for Raster Graphics

A. DISTANTE

AND

N. VENEZIANI

Istiruro di Fisica, Gruppo Nazionale Cibernetica e Biofisica, Universita di Barr. Via Amendolu I 73, 70100 Bari, Itab

Received August 24, 198 1

An algorithm for filling irregular polygons is presented. It is designed to operate in raster mode and uses the refresh memory of a graphic display as working space. The algorithm has been implemented in a host computer but it can be implemented easily within the micro- processor controlling the display. All areas contoured by closed lines are shaded at the same time according to a preassigned color code.

I INTRODUCTION

In many applications, such as scene analysis, pattern recognition, computer graphics, and especially computer cartography, it is necessary to fill the interior of a polygon. This work describes a two-pass filling algorithm that works in raster mode, by using a videographic system with refresh memory. In the literature there are several algorithms that are implemented using either the host computer memory or the refresh memory of a graphic display. We briefly recall here some types of filling algorithms and how they work.

(I) Purity check. A pixel is assigned to the interior of a polygon if a horizontal half-line going through it intersects the contour an odd number of times. Although this approach is quite adequate theoretically, in practice there are problems when one has to fill complicated polygons. Some of these problems can be solved when the filling operation is performed in the ma in memory using some kind of labeling [ 1, 2, 4, 91.

(2) Decomposit ion of polygons. This is a different approach, based on the decomposit ion of arbitrary polygons into simpler shapes, particularly triangles 13. 5. 6, 81.

(3) Contour line following [2].

Our approach uses a method of direct filling to produce geological maps, which is particularly suitable for handl ing any type of polygonal contour required by the geological information to be displayed.

2. DATA ACQUISITION AND PROCEDURE

Initially, the map can be obtained from different devices such as table digitizers, TV cameras connected to a display device, or flying-spot scanners. The algorithm also works correctly, on variable-width polygonal contour lines of the map (this

288 0146-664X/82/1 10288-08$02.00/O Copyright Q 1982 by Academic Press, Inc. All rights of reproduction in any form reserved

Page 2: A two-pass filling algorithm for raster graphics

TWO-PASS ALGORITHM 289

happens when the map is obtained from a TV camera or a flying-spot scanner). The filling algorithm can be applied to maps with dimensions lim ited only by the refresh-memory size.

The interactive procedure contains the following steps.

(1) Load the map in the refresh memory. This operation can be done automati- cally from a TV camera or a flying-spot scanner connected to the refresh memory of the graphic display.

(2) Filter the map so that each pixel has the proper value (VCONT) representing either an element of the contour or an element of the area to be filled.

(3) Select the area of interest on the CRT by using a track ball [lo]. The whole picture will be considered by default.

(4) Assign the color codes for the partitioned regions. (5) Apply the filling algorithm.

3. A TWO-PASS ALGORITHM

Pass 1

The pixels are examined sequentially in raster mode. During each raster, only two horizontal lines are used. The algorithm classifies each pixel according to its relationship with the pixels already classified in the current line and in the above line as belonging to a contour or to interval regions.

AoL. C,Ln

a

A.L. - CoLa -

e A.L. - C,L, -

A,Ls C.L.

b

S in 2

I I equivalence relation ZIS is defined

A.L. - C.L. -

ZinYonC.L$

Oil C.L.

FIG. 1. Configurations recognized by the algorithm during first pass. 0, pixel to fill; C, pixel to contour line; X, Y, Z, S, classes; A, L, above line; C, L, current line.

Page 3: A two-pass filling algorithm for raster graphics

290 DISTANTE AND VENEZIANI

There is a limited number of legitimate configurations which are recognized by the filling algorithm during Pass 1. These are shown in Fig. 1. Each pixel on a line, which does not follow a contour pixel, belongs to the same class as the previous pixel on its left side (Figs. la and h). If the pixel above belongs to a different class. the current pixel must be assigned to the same class as that pixel (Fig. If). In particular, for a case like the one shown in Fig. lb, an equivalence relation is set.

A pixel that follows a contour line belongs to the same class as the pixel above if the last one is not a contour pixel (Fig. lc). A pixel that has contour pixels on its left and above is classified in a new class (Fig. Id). In this case, if the following pixel is again a contour pixel, the pixel on the right side of the one above must be examined to determine the need for an equivalence relation (Fig. lc). Each pixel on a line that belongs to a contour line is classified in the class “left pixel” (Fig. lg).

The number of different classes to which the pixels can be assigned is limited only by the number of channels in the refresh memory. With a refresh memory with 10 channels, we can assign up to 1024 classes. Some notation must be introduced to read the algorithm correctly. Each pixel in the refresh memory is represented by P( J. J) where Z indicates the line, .Z the column, and P( I, J) the intensity value. (Initially P( I, J) is equal to VCONT for pixels belonging to contours.)

The following variables are introduced: CLAS is the last class assigned, NRST the number of raster lines to process, NPXLS the number of pixels in each raster line, VRAST(J) the input value of current pixel, CLCR(J) the output value of current pixel, CLPR(.Z) the output value of above pixel, VRASTPR(J) the input value of above pixel, and VCONT the value of contour pixels. Using these notational conventions, the filling algorithm of Pass 1 is

Set CLAS = 1 For J = 1 TO NRST do: BEGIN 3. Set Z = 0 4. WHILE (I 5 NPXLS)do:

BEGIN 5. IF(VRAST(Z) = VCONT then DO:

BEGIN 6. CLCR(Z) = CLPR(Z) END ELSE do: BEGIN 7. IF VRASTPR(Z) = VCONT then DO:

BEGIN 8. If VRAST(Z - 1) = VCONT then DO:

BEGIN 9. CLAS = CLAS + 1 10. CLCR(Z) = CLAS

1 END ELSE CLCR(Z) = CLCR( Z - 1) END

ELSE DO: Begin 11. CLCR( I) = CLPR( I) 12. If VRAST(Z - 1) = VCONT then DO:

Page 4: A two-pass filling algorithm for raster graphics

TWO-PASS ALGORITHM 291

BEGIN 13. If CLCR(I) = CLCR(1 - 1) then DO:

BEGIN 14. Each pixel on the left of Ith

element must be assigned by CLRAST(1) 15. Store equivalence class CLPR( I) = CLCR(1) END

END END

END END

Pass 2

During this pass, the algorithm will assign a unique class for each region. This is done in the following way.

(a) We resolve all equivalence relations, writing an appropriate look-up table. Given the equivalence relations between pairs of classes, the sets of equivalent classes must be found. Then a unique class value must be reassigned to every class belonging to a set. It is useful to store the information relative to equivalence relations as a tree structure. The equivalent-class set can be seen as a tree and all trees form a forest. To organize a binary forest in memory, Fisher and Galler’s

n HEAD V

STORE BINMY FOREST IN MB*)RY

VISITIt& FOREST

GET TREES

il

c, c, c3 c4 cj c.5 c, c1O c1, h5 cl7 V

c1O24 STORE 1~

Cl Cl c3 c4 c4 c4 c, c4 c4 c4 LKT

LWK-UP TABLE OF vIDEffiFcAw

FIG. 2. Algorithm phases for the solution of equivalence relations and for calculation of equivalent classes sets.

Page 5: A two-pass filling algorithm for raster graphics

292 DISTANTE AND VENEZIANI

algorithm [7] is applied. The equivalent-class set is now retrieved by visiting the forest in “preorder mode.” Then the equivalent-class sets are stored in the look-up table (LKT) (Fig. 2).

(b) The map is read line by line from the refresh memory, and the value of each pixel is used as a pointer in LKT. In this way all pixels are updated and each line is rewritten in the refresh memory.

In some applications phase (b) of Pass 2 can be avoided in order to reduce the CPU time. The map classified by Pass 1 can in fact be displayed with a unique color for each region, writing into the hardware color look-up table of the videographic system the proper values stored in the array LKT.

4. RESULTS AND CONCLUSIONS

The algorithm has been applied to the pictures shown in Figs. 3a-c. The first one shows a simple picture with one polygon; the second one shows a more complex

FIG. 3. Contours of digitized pictures used for algorithm test.

Page 6: A two-pass filling algorithm for raster graphics

TWO-PASS ALGORITHM 293

TABLE I Test Picture Data

Picture Number of

polygons Number of

vertices

Execution time (set)”

STAP 1 STAP 2

Number of

pixels

t 3 1 35 5 26 18 5 8 290 310 x X 315 320

C 71 9806’ 50 19 512 X 512

“Executive time measured by RSX routine “time” (I/O time is included). ‘Total pixels belonging to continuous contour lines.

FIG. 4. Filling of contour picture 3(c) after first pass

picture with three regions; and the third one shows a piece of a map with many complex regions. The first two pictures have been obtained from a table digitizer. In the third case, continuous lines have been obtained, using a flying-spot scanner. Some details of these pictures are given in Table 1.

The results for Fig. 3c after Pass 1 are shown in Fig. 4. The original contours have been overlayed to show that at this level, in some regions, there are pixels assigned to different classes. During this pass, however, appropriate equivalences between classes belonging to same region have been defined.

The outputs in the three cases after Pass 2 are shown in Fig. 5. The shading can be changed by changing the look-up table of the videographic display.

The filling algorithm is implemented on a VDC-501 display connected to a PDPl1/45 computer running under the RSX-IlM V3.2 operating system. The

Page 7: A two-pass filling algorithm for raster graphics

294 DISTANTE AND VENEZIANI

FIG. 5. Example of filling algorithm application to test pictures in Fig. 3

VDC-501 system consists of a 512 X 512 X lo-bit refresh memory, with a program- mable look-up table of 1 K of memory. The refresh memory is used as working storage and it is addressed in horizontal raster mode to minimize the I/O time as required during the two passes of the algorithm.

Under this condition, during Pass 1, we keep four intermediate buffers (with the same length as the refresh-memory line) in the host computer memory. In this way we optimize the time required to read/write lines from refresh memory and the computing time. For a map of 512 X 512 pixels the execution time required for Pass 1 is about 50 set and for Pass 2 about 20 set, as shown in Fig. 5c (Pass 2a takes about 1 set).

The filling algorithm can be easily implemented to run on the microprocessor of the videographic display using a pixel access mode. The program, implemented in Fortran-plus, takes about 8 KW of memory (including systems routines) on the host

Page 8: A two-pass filling algorithm for raster graphics

TWO-PASS ALGORITHM 295

TABLE 2 Code and Data-Memory Requirements (bytes)

Filling algorithm Pass 1 950

I/O buffer and data IWO+ 1000 Equivalence relation routine 190 Building and Visiting tree Structure routine 621 Filling algorithm Pass 2 76

Total 3837

computer. The core memory required for code and buffer of the program routines is shown in Table 2. The algorithm uses only buffer space to store the equivalence classes. No extra memory is needed to store labellings or reference pointers of contour lines (Pavlidis, 1970).

ACKNOWLEDGMENTS

We express our gratitude to Professor L. Guerriero for guidance and stimulating discussions during the course of this work.

REFERENCES 1. T. Pavlidis, Filling algorithms for raster graphics, Computer Graphics and Image Processing 10, 1979,

126-141. 2. S. A. Dudani, Region extraction using boundary following, in Pattern Recognition and Artificial

Intelligence (C. H. Chen, Ed.), pp. 216-232, Academic Press, New York, 1976. 3. D. L. Fulton, A Plasma-panel Interactive Graphic System, Proceedings of the SID, 15-2, Second

Quarter 1974, pp. 74-80. 4. R. D. Merrill, Representation of contours and regions for efficient computer search, Commun. ACM

16, 1973, 69-82. 5. G. W. Romney, G. S. Watkins, and D. C. Evans, Real-time Display of Computer Generated

Half-tone Perspective Pictures, Proceedings IFIP 1968, pp. 973-978, North-Holland, Amsterdam, 1969.

6. C. Wylie, G. Romney, D. Evans, and A. Erdahl, Half-tone Perspective Drawings by Computer, Proceedings Fall Joint Computer Conference, Thompson Books, Anaheim, Calif. 1967.

7. D. E. Kauth, The Art of Computer Programming, Addison-Wesley, Reading, Mass., 1973. 8. F. C. Crow, Shadow Algorithms for Computer Graphics, ACM SIGGRAPH Computer Graphics,

Vol. II, No. 2, Summer 1977, Proceedings of SIGGRAPH ‘77, San Jose, pp. 242-248. 9. B. D. A&land and N. Weste, The edge flag algorithm--A fill method for raster scan display, IEEE

Trans. Comput. 30, 1981,41-47. 10. B. Marangelli and N. Mirizzi, A pointer for analog refresh memories, Computer Graphics and Image

Processing 14, 1980, 170- 176.