it 300: computer graphics

115
IT 300: Computer Graphics Lecture 4: Areas, text and colors

Upload: gaia

Post on 10-Feb-2016

19 views

Category:

Documents


1 download

DESCRIPTION

IT 300: Computer Graphics. Lecture 4: Areas, text and colors. Lecturer Details. Dr. Walid Khedr, Ph.D. Email: [email protected] Web: www.staff.zu.edu.eg/wkhedr Department of Information Technology. Course Info. Course Name: Computer Graphics Course Number: IT300 - PowerPoint PPT Presentation

TRANSCRIPT

Slide 1

IT 300: Computer GraphicsLecture 4: Areas, text and colorsLecturer DetailsDr. Walid Khedr, Ph.D.Email: [email protected]: www.staff.zu.edu.eg/wkhedrDepartment of Information TechnologyCourse InfoCourse Name: Computer GraphicsCourse Number: IT300Course Group: http://groups.yahoo.com/group/IT300_Computer_GraphicsProgramming: JAVA Prerequisite: JAVA, Linear AlgebraCourse Materials:Text BookLectures

Main TopicsIntroductionBasic principles of two-dimensional graphicsScan Conversion: Drawing lines and curvesAreas, text and colorsBasic principles of three-dimensional graphicsModeling three-dimensional objectsVisible surface determinationIllumination and shadingSpecial effects and virtual realityAreas, text and colorsObjectivesFilling AreasWhich pixels to fill?Scan line techniqueSeed-fill techniqueScan line seed fill techniqueWith what value to fill them?Color ModelsRGB ModelCMY ModelHSV ModelColor Interpolation

ObjectivesFilling areas and polygons is a general technique of computer graphics.This chapter contains in addition to techniques for filling areas also basic models for colors and a short introduction to handling text in images.Filling AreasAreas are usually bounded by polygons or closed sequences of curves. Two fundamental questions:Which pixels to fill?With what value to fill them?In order to fill a polygon, points inside the polygon must be distinguished from exterior ones. For polygons whose edges do not intersect, it is obvious which points should be considered as inner and outer points. For polygons with intersecting edges it is not immediately clear what its inner parts are. Which pixels to fill?In order to fill a polygon, points inside the polygon must be distinguished from exterior ones. For polygons whose edges do not intersect, it is obvious which points should be considered as inner and outer points. For polygons with intersecting edges it is not immediately clear what its inner parts are.

Filling AreasTwo basic approaches:Scan line technique:Take successive scan lines that cross the area and fill in the spans of adjacent pixels that lie inside the area from left to right.Typically used to fill polygons, circles, and other simple curves.Seed-fill technique:Start from a given interior position and paint outward from this position until we encounter the boundary.Typically used to fill more complex boundaries.Filling Areas

The Odd Parity Rule

The odd parity rule provides a definition of inner and outer points.The Odd Parity RuleThe odd parity rule is a useful mathematical definition of interior and exterior points of a polygonbut it is not suited for implementation, since the computational costs would be unacceptable if the rule is applied separately to each pixel. Scan-Line Polygon Fill AlgorithmThis algorithm handles polygonal regions that are geometrically defined by the coordinates of their vertices (along with the edges that connect the vertices).Although such regions can be filled by the previous technique, the following is a much more efficient approach.Scan-Line Polygon Fill AlgorithmThe algorithm begins with the first scan line the polygon occupies and proceeds line by line towards the last scan line. For each scan line it finds all intersection points between the current scan line and the edges. The intersection points are sorted according to their x coordinates and grouped into pairs such as (a, b) and (c, d).Calculate parity of intersections to determine in/out A line is drawn from the first point to the second point in each pair.

For example, scan line y intersects edges ,, 7, 6. and E4 at points a, b, c, and bt respectively.14Scan-Line Polygon Fill AlgorithmSpecial cases to be handled:Horizontal edges.Horizontal edges are ignored since the pixels that belong to them are automatically filled during scan conversion. For vertices lying on scanlines.Scan-Line Polygon Fill Algorithm

Scan-Line Polygon Fill AlgorithmWhen a scan line intersects a vertex. If the vertex is a local minimum or maximum such as V1 and V7, no special treatment is necessary. The two edges that join at the vertex will each yield an intersection point with the scan line.

Scan-Line Polygon Fill AlgorithmHowever, in this case we dont want to count p1 twice (p0,p1,p1,p2,p3), otherwise we will fill pixels between p1 and p2, which is wrong.To avoid cases where a vertex needs to be counted as one intersection, shorten one of the two intersecting edges by one scan line

Scan-Line Polygon Fill Algorithm

Special cases to be handledcount twice, once for each edgechop lowest pixel to only count oncedelete horizontal edgesScan-Line Polygon Fill AlgorithmCalculating intersections must be done cleverly.In particular , we must avoid the brute-force technique of testing each polygon edge for intersection with each new scan line.Calculations performed in scan-conversion and other graphics algorithms take advantage of various coherence properties What we mean by coherence is simply that the properties of one part of a scene are related in some way to other parts of the scene so that the relationship can be used to reduce processing.Coherence Method

Coherence MethodThis allows for integer calculations to be performed.

Polygon FillIn order to support an efficient implementation of this scan line algorithm we create a data structure called an Edge Table (ET) and an Active Edge Table (AET).Building the sorted edge tableProceed round the edges in a clockwise or anticlockwise direction.At each scan-line crossing an edge, Add an entry:The entries of each scan-line are sorted on the smallest value of yOnly non-horizontal edges are entered in the table.Shorten certain edges (where y changes monotonically) to resolve which intersection points to choose Edges are stored from left to right for each scan-linePolygon FillBuilding the sorted edge tableTraverse Edges to construct an Edge Table.Eliminate horizontal edgesShorten certain edges (where y changes monotonically) to resolve which intersection points to choose For each edge, we add it to the scan-line that it begins with (that is, the scan-line equal to its lowest y-value).For each edge entry, store (1) the x-intercept with the scan-line, (2) the largest y-value of the edge, and (3) the inverse of the slope.Each scan-line entry thus contains a sorted list of edges. The edges are sorted left to right. (To maintain sorted order, just use insertion based on their x value.)Polygon Data Structureedgesxminymax1/m168/4(1, 2)(9, 6)xmin = x value at lowest yymax = highest y

Polygon Fill

Polygon Fill

Polygon FillUse an active-edge table(AET) to keep track of the edges that the current scanline intersectsThe AET contains all edges crossed by the current scan-line.The edges in the AET are sorted in an increasing order, using the x intersection values.

Algorithm (using ET and AET)Process the scan lines from the bottom of the polygon to its topCreate a list of active edges for the first scan-lineFind intersection points (i.e., xk+1 = xk + 1/m, x0 = xmin), sort them, and fill the spans defined by the pairsMove to the next scan-line and update AETDelete edges which are currently in the AET but not intersected by the next scan-line (use ymax to find if next scan-line intersects an edge)Add new edges from ET which are intersected by the next scanlineIf AET is not empty, goto step 2

ExamplePolygon Data Structureafter preprocessingEdge Table (ET) has a list of edges for each scan line.e1e2e3e4e5e6e7e8e9e10e1105101313121110 e6987 e4 e56 e3 e7 e854321 e2 e1 e110 e10 e9e117 e3 e4 e56 e7 e811 e61032Running the Algorithme2e3e4e5e6e7e8e9e10e11051013ET131211 e610987 e3 e4 e56 e7 e854321 e2 e110 e10 e9

xminymax1/me226-2/5e31/3 121/3e4412-2/5e54130e66 2/313-4/3e71010-1/2e81082e91183/8e10114-3/4e11642/350101533Running the Algorithme2e3e4e5e6e7e8e9e10e11051013501015y=0 SLB 114-3/41183/8e9e1010 1/411 3/834Running the Algorithme2e3e4e5e6e7e8e9e10e11051013501015y=1 SLB 26-2/5642/3e11e21 3/510 1/44-3/411 3/883/8e9e106 2/39 1/211 6/835Running the Algorithme2e3e4e5e6e7e8e9e10e11051013501015y=2 SLB 1 3/56-2/56 2/342/3e11e29 1/24-3/411 6/883/8e9e1012 1/88 3/47 1/31 1/536Running the Algorithme3e4e5e6e7e8e9e10051013501015y=3 SLB 1 1/56-2/57 1/342/3e11e28 3/44-3/412 1/883/8e9e1012 4/8884/5e11e237Running the Algorithme3e4e5e6e7e8e10051013501015y=4 SLB 4/56-2/5842/3e11e284-3/412 4/883/8e9e10e11e2e9Remove these edges.38Running the Algorithme3e4e5e6e7e8051013501015y=4 SLB 4/56-2/5e212 4/883/8e912 7/82/5e2e11e10e9e11 and e10 are removed.39Running the Algorithme3e4e5e6e7e8051013501015y=5 SLB 2/56-2/5e212 7/883/8e913 2/80e2e11e10e940Running the Algorithme3e4e5e6e7e8051013501015y=6 SLB 06-2/5e21010-1/2e7e2e11e10e9Remove this edge.1082e813 2/883/8e99 1/21213 5/841Running the Algorithme3e4e5e6e7e8051013501015y=7 SLB 4130e59 1/210-1/2e7e2e11e10e91282e813 5/883/8e9Add these edges.412-2/5e41/3121/3e342Seed Fill AlgorithmPolygon filling process start from seed pixel. As soon as the filling process is initialed, the neighborhood of seed point is checked for its color content. For neighboring pixels with old or previous color, they are flooded with new color. The algorithm terminates once encountering a boundary pixel or pixels of new color in all sides.Disadvantage: considerable stacking is needed

44The order of pixels that should be added to stack using 4-connected is above, below, left, and right. For 8-connected is above, below, left, right, above-left, above-right, below-left, and below-right.

45Start PositionBoundary Fill Algorithm4-connected (Example) 46321123Boundary Fill Algorithm4-connected (Example) 47142421Boundary Fill Algorithm4-connected (Example) 481221Boundary Fill Algorithm4-connected (Example) 495151Boundary Fill Algorithm4-connected (Example) 5011Boundary Fill Algorithm4-connected (Example) 51Boundary Fill Algorithm4-connected (Example) 52Start PositionBoundary Fill Algorithm8-connected (Example) 534152354321Boundary Fill Algorithm8-connected (Example) 54Boundary Fill Algorithm8-connected (Example) 641236432155Boundary Fill Algorithm8-connected (Example) 78412387432156Boundary Fill Algorithm8-connected (Example) 11912710412312111097432157Boundary Fill Algorithm8-connected (Example) 1197104123111097432158Boundary Fill Algorithm8-connected (Example) 971041231097432159Boundary Fill Algorithm8-connected (Example) 97412397432160Boundary Fill Algorithm8-connected (Example) 741237432161Boundary Fill Algorithm8-connected (Example) 4123432162Boundary Fill Algorithm8-connected (Example) 12332163Boundary Fill Algorithm8-connected (Example) 122164Boundary Fill Algorithm8-connected (Example) 1165Boundary Fill Algorithm8-connected (Example) Scan Line Seed Fill Algorithm The seed fill algorithm makes the stack quite large because in every loop the algorithm pushes at most 8 pixels into the stack. In contrast scan line seed fill algorithm minimize stack size by pushing to the stack only one pixel instead of proceeding along 8-connccted pixels. This algorithm proceeds as follows:Scan Line Seed Fill Algorithm Step 1: A seed pixel on a scan line within the area is popped from a Stack containing the seed pixel.Step 2: The line or span containing the seed pixel is filled to the right and left of the seed pixel including the seed pixel itself until a boundary is found.Step 3: The extreme left and extreme right pixels in the scan are saved as :Xleft and Xright respectively.Step 4: In the range Xleft and Xright the scan lines immediately above the current scan lines and immediately below the current scan line are examined to see if they completely contain boundary pixels or previously filled pixels, then in the range of Xleft and Xright the extreme left pixel in each span is marked as a seed pixel and pushed into the stack.

69Scan Line Seed Fill Algorithm

Since the previous procedure requires considerable stacking of neighboring pixels, more efficient methods are generally employed.

These methods (Span Flood-Fill) fill horizontal pixel spans across scan lines, instead of proceeding to 4-connected or 8-connected neighboring pixels.

Then we need only stack a beginning position for each horizontal pixel spans, instead of stacking all unprocessed neighboring positions around the current position.70Scan Line Seed Fill Algorithm

The algorithm is summarized as follows: Starting from the initial interior pixel, then fill in the contiguous span of pixels on this starting scan line.

Then locate and stack starting positions for spans on the adjacent scan lines, where spans are defined as the contiguous horizontal string of positions bounded by pixels displayed in the area border color.

At each subsequent step, unstack the next start position and repeat the process.

75111098765S4321001234567891011121314Span Flood-Fill Algorithm (example)761110987625S4132100123456789101112131421Scan Line Seed Fill Algorithm (example)7711109873625S4132100123456789101112131431Scan Line Seed Fill Algorithm (example)7811109856736425S413210012345678910111213146541Scan Line Seed Fill Algorithm (example)79111097856736425S413210012345678910111213147541Scan Line Seed Fill Algorithm (example)80111097856736425S41321001234567891011121314541Scan Line Seed Fill Algorithm (example)811110987856736425S41321001234567891011121314841Span Flood-Fill Algorithm (example)821110987856736425S4132100123456789101112131441Span Flood-Fill Algorithm (example)8311109878567364259S4132100123456789101112131491Span Flood-Fill Algorithm (example)8411109878567364259S4101321001234567891011121314101Span Flood-Fill Algorithm (example)8511109878567364259S410131121001234567891011121314111Span Flood-Fill Algorithm (example)8611109878567364259S41013112121001234567891011121314121Span Flood-Fill Algorithm (example)8711109878567364259S410131121210012345678910111213141Span Flood-Fill Algorithm (example)Seed Fill Algorithm ExampleAs an example of the application of the algorithm consider the boundary-defined polygon defined by vertices (1,1), (1,5), (4,5), (4,1).

Edges in the edge list become active when the y coordinate of the current scan line matches their ymin value. Only active edges are involved in the calculation of intersection points. The first intersection point between an active edge and a scan line is always the lower endpoint of the edge, whose coordinates are already stored in the edge's record. Implementation of the Scan-Line Fill AlgorithmFor example, when the algorithm begins at the first scan line, edges ] and 7 become active. They intersect the scan line at (X|,y.).

93Implementation of the Scan-Line Fill AlgorithmAn edge is deactivated or may even be removed from the edge list once the scan line whose y coordinate matches its ymax value has been processed.The need that was mentioned early to give special treatment to a vertex where two monotonically increasing or decreasing edges meet is elegantly addressed by subtracting one from the ymax value of the lower edge. This means that the lower edge is deactivated one line before the scan line that intersects the vertex. Thus only the upper edge produces an intersection point with that scan line (see K2 in Fig. 3-19). This explains why the initial ^ value for edges , and 4 has been decreased by one.94ColorWhat Is Color?

Color is a property of materials (as in the case of a red dress).Color is a property of light (as in the case of a red traffic light).Newton already knew that light rays are not colored.ColorTo understand how to make realistic images, we need a basic understanding of the physics and physiology of vision. Here we step away from the code and math for a bit to talk about basic principles of colorsElements of ColorHow do we perceive color ?Some white light, coming usually from the sun is absorbed and reflected on some surface, depending on the type of surface. We perceive the white light subtracted by the absorbed light.

Elements of ColorFor example, if white light is shone onto a green object most wavelengths are absorbed, while green light is reflected from the object.

White LightColours AbsorbedGreen LightVisible SpectrumWe perceive electromagnetic energy having wavelengths in the range 400-700 nm as visible light. Visible light is a very small part of electromagnetic energy : a narrow frequency band within the electromagnetic spectrumRed : 4.3 x 1014 HZ : 700nm Violet : 7.5 x 1014 Hz : 400nmVisible Spectrum

Electromagnetic Spectrum WavelengthAs an electromagnetic wave, light can be described by either the frequency f or the wavelength .f = c/,c = 300,000 kilometers per second Frequency is constant in all material, the wavelength varies.

The span of the white light Sun or light bulb emit all frequencies within the visible range to produce what we perceive as the "white light"When white light is incident upon an object, some frequencies are reflected and some are absorbed by the object.It is the combination of frequencies present in the reflected light that determines what we perceive as the color of the object

The Human EyeThe retina of the eye is its light-sensitive membrane. It lines the rear portion of the eye's wall and contains two kinds of receptor cells: cones and rods.The cones are the color-sensitive cells, each of which responds to a particular color, red, green, or blue. The rods cannot distinguish colors, nor can they see fine detail.

The Human Eye

The Human Eye: ConesThe color we see is the result of our cones relative responses to red, green, and blue light. The human eye can distinguish about 200 intensities of red, green, and blue, each.An eye has 6 to 7 million cones, concentrated in a small portion of the retina called the fovea. Each cone has its own nerve cell, allowing the eye to discern tiny details. To see an object in detail, the eye looks directly at it in order to bring the image onto the fovea.The Human Eye: RodsSeventy-five million to 150 million rods are crowded onto the retina surrounding the fovea. A single nerve cell has many rods attached to it, preventing the discrimination of fine detail.Rods are very sensitive to low levels of light and can see things in dim light that the cones miss. The human eye receptors typesThe human eye cones have three different types of receptors. Each receptor type is more sensitive to a certain smaller range of the spectrum. They are called red-, green- and blue-receptors. Most sensitive to red light (610 nm) Most sensitive to green light (560 nm)Most sensitive to blue light (430 nm)The main characteristics of a color for human perception are the following ones:Hue, Saturation and Lightness or brightness The human eye receptors typesIt is a sensation produced by the brain (the perceived color) in response to light received at the retina, so that one would say, for example, the color produced by some given light is red or whatever.Color blindness results from missing cone type(s)Color Blind TestSpecifying ColorColor perception usually involves three quantities:Hue: Distinguishes between colors like red, green, blue, etcSaturation: How far the color is from a gray of equal intensity. (pink is less saturated than red)Lightness: The perceived intensity of a reflecting objectSometimes lightness is called brightness if the object is emitting light instead of reflecting it.In order to use color precisely in computer graphics, we need to be able to specify and measure colors.

Specifying ColorHue: corresponding to the dominant wavelength in the spectrum of the color.Saturation or purity: which is high when the spectrum consists of a narrow peak at the dominant wavelength, and which is low for a flatter spectrum.Dominant wavelength is the wavelength of the color we seeLightness: depending on the energy of the spectrum. The higher the energy of the single frequencies, the higher is the intensity.Specifying Color

White LightOrange LightSpecifying ColorHue is the actual color. It is measured in angular degrees around the cone starting and ending at red = 0 or 360 (so yellow = 60, green = 120, etc.). Saturation is the purity of the color, measured in percent from the center of the cone (0) to the surface (100). At 0% saturation, hue is meaningless.Brightness is measured in percent from black (0) to white (100). At 0% brightness, both hue and saturation are meaningless.

Specifying ColorCIE Diagram International Commission on Illumination (CIE) diagram is one of the first mathematically defined color spaces.Points on the boundary are fully saturated colors

CIE Diagram

The RGB Model

The RGB ModelThe CMY Model

The CMY ModelThe HSV Model

H = HueS = SaturationV = Value (or brightness)

ValueSaturationHueThe HSV ModelColor InterpolationColor GradientAn area is filled with changing color.The transformation of one image to another.Color GradientsFor points on the connecting line between P0 and P1, the corresponding convex combination of the colors is used.For the point P = (1)P1+P0 (where [0, 1]) the color (1)(r1, g1, b1)+(r0, g0, b0) is used.Shape and Color Interpolation

Shape and Color Interpolation

Shape and Color InterpolationThe triangles for the intermediate image are defined in the same way.If the points pi, pj and pk form a triangle in the first image andThe associated points pi', pj' and pk' form a triangle in the second image, then The points (1 )pl + pl', l {i, j, k} form a triangle in the intermediate image.Shape and Color InterpolationWithin one triangle, the color for the pixels is determined by color interpolation.

This is a system of linear equations with three variables 1, 2, 3 and three equations.

Shape and Color InterpolationIn each triangle of an intermediate image, the color value of a pixel is obtained as the corresponding convex combination of the corresponding two pixels in the initial and final image. (1)(r0, g0, b0)+(r1, g1, b1)

ReadingChapter 4Next LectureBasic principles of three-dimensional graphics131