You are on page 1of 60

Computer Graphics & Multimedia

Unit I

Veena Khandelwal

Depatment of Computer Science & Engineering

Indore Institute of Science & Technology, Indore


August 28, 2018

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 1 / 60
Outline
1 Computer Graphics Hardware
Video Display Devices
Refresh Cathode-Ray Tubes
Raster Scan Displays
Random Scan Displays
Raster Scan Display Processor
2 Output Primitives
Line Drawing Algorithms
Slope Intercept Line Drawing
DDA Algorithm
Bresenhams Line Algorithm
Circle Drawing Algorithm
Bresenhams Circle Algorithm
Midpoint Circle Algorithm
3 Region Filling
Boundary-Fill Algorithm
Flood-fill Algorithm
Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 2 / 60
Computer Graphics Hardware Video Display Devices

Refresh Cathode-Ray Tubes I

1 A beam of electrons (cathode rays), emitted by an electron gun,


passes through focusing and deflection systems that direct the beam
toward specified positions on the phosphor-coated screen.

Figure: Basic design of a magnetic-deflection CRT.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 3 / 60
Computer Graphics Hardware Video Display Devices

Refresh Cathode-Ray Tubes II

2 The phosphor then emits a small spot of light at each position


contacted by the electron beam. Because the light emitted by the
phosphor fades very rapidly, some method is needed for maintaining
the screen picture.
3 The most common method employed for maintaining phosphor glow
is to redraw the picture repeatedly by quickly directing the electron
beam back over the same screen points. This type of display is called
a refreshCRT, and the frequency at which a picture is redrawn on the
screen is referred to as the refresh rate.
4 The primary components of an electron gun in a CRT are the heated
metal cathode and a control grid. Heat is supplied to the cathode by
directing a current through a coil of wire, called the filament, inside
the cylindrical cathode structure. This causes electrons to be boiled
off the hot cathode surface.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 4 / 60
Computer Graphics Hardware Video Display Devices

Refresh Cathode-Ray Tubes III

Figure: Operation of an electron gun with an accelerating anode.

5 In the vacuum inside the CRT envelope, the free, negatively charged
electrons are then accelerated toward the phosphor coating by a high
positive voltage generated with an accelerating anode.
6 Intensity of the electron beam is controlled by the voltage at the
control grid.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 5 / 60
Computer Graphics Hardware Video Display Devices

Refresh Cathode-Ray Tubes IV

7 Since the amount of light emitted by the phosphor coating depends


on the number of electrons striking the screen, the brightness of a
display point is controlled by varying the voltage on the control grid.
This brightness, or intensity level, is specified for individual screen
positions with graphics software commands.
8 The focusing system in a CRT forces the electron beam to converge
to a small cross section as it strikes the phosphor. Focusing is
accomplished with either electric or magnetic fields.
9 As with focusing, deflection of the electron beam can be controlled
with either electric or magnetic fields. Cathode-ray tubes are now
commonly constructed with magnetic-deflection coils mounted on the
outside of the CRT envelope, as illustrated in Figure 1.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 6 / 60
Computer Graphics Hardware Video Display Devices

Refresh Cathode-Ray Tubes V

10 Spots of light are produced on the screen by the transfer of the CRT
beam energy to the phosphor. After a short time, the excited
phosphor electrons begin dropping back to their stable ground state,
giving up their extra energy.
11 Fluoresence: It refers to the light given off by a phosphor while it is
being exposed to an electron beam.
12 Phosphoresence: The term used to describe the light given off by a
phosphor after it has been exposed to an electron beam.
13 Different kinds of phosphors are available for use in CRTs. Besides
color, a major difference between phosphors is their persistence: how
long they continue to emit light (that is, how long it is before all
excited electrons have returned to the ground state) after the CRT
beam is removed.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 7 / 60
Computer Graphics Hardware Video Display Devices

Refresh Cathode-Ray Tubes VI


14 Persistence: The duration of phosphorescence exhibited by a
phosphor. Persistence is defined as the time that it takes the emitted
light from the screen to decay to one-tenth of its original intensity.
Lower-persistence phosphors require higher refresh rates to maintain a
picture on the screen without flicker. A phosphor with low persistence
can be useful for animation, while high-persistence phosphors are
better suited for displaying highly complex, static pictures.
15 Although some phosphors have persistence values greater than 1
second, general-purpose graphics monitors are usually constructed
with persistence in the range from 10 to 60 microseconds.
16 The maximum number of points that can be displayed without
overlap on a CRT is referred to as the resolution. A more precise
definition of resolution is the number of points per inch/centimeter
that can be plotted horizontally and vertically.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 8 / 60
Computer Graphics Hardware Video Display Devices

Refresh Cathode-Ray Tubes VII

17 The physical size of a graphics monitor, on the other hand, is given as


the length of the screen diagonal, with sizes varying from about 12
inches to 27 inches or more.
18 Compute the size of a 640×480 image at 240 pixels per inch.
640 480
×
240 240
19 Compute the resolution of a 2×2 inch image that has 512×512
512
pixels.
2
20 What is an image aspect ratio? The ratio of its width to its height,
Width
measured in unit length or number of pixels. = aspect ratio
height
21 If an image has a height of 2 inches and an aspect ratio of 1.5, what
is its width? width = height × aspect ratio = 1.5 × 2 = 3 inches

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 9 / 60
Computer Graphics Hardware Video Display Devices

Refresh Cathode-Ray Tubes VIII

22 If we want to resize a 1024×768 image to one that is 640 pixels wide


with the same aspect ratio, what would be the height of the resized
640
image? 768 × = 480
1024
23 If we want to cut a 512×512 sub-image out from the center of a
800×600 image, what are the coordinates of the pixel in the large
image that is at the lower left corner of the small image?
(800 − 512) (600 − 512)
, = (144, 44)
2 2

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 10 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Displays I

1 Based on television technology


2 A non-interlaced display is a cathode-ray tube ( CRT ) display in
which the lines are scanned sequentially from the top to the bottom
of the screen.The electronic beam is swept across the screen, one row
at a time from top to bottom.
3 As the electroic beam moves across each row, the beam intensity is
turned on and off to create a pattern of illuminated spots.
4 Picture definition is stored in a memory area called the refresh buffer
or the frame buffer.
5 This memory area holds the set of intensity values for all the screen
points.
6 Stored intensity values are then retrieved from the refresh buffer and
painted on the screen one row at a time. Each screen point is referred
to as a pixel or pel.
Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 11 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Displays II


7 The capability of a raster-scan system to store intensity information
for each screen point makes it well suited for the realistic displays of
scenes containing subtle shading and color patterns. Example: Home
television screens.
8 Intensity range for pixel positions depends on the capability of the
raster system. In a simple black and white system, each screen point
is either on or off so only one bit per pixel is needed to control the
intensity of screen positions.
9 Additional bits are needed when color and intensity variations can be
displayed. Up to 24 bits per pixels are included in high quality
systems, which can require several megabytes of storage for the frame
buffer, depending on the resolution of the system.
10 A system with 24 bits per pixel and a screen resolution of 1024 by
1024 requires 3 MB of storage for the frame buffer.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 12 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Displays III

11 On a black and white system with one bit per pixel, the frame buffer
is commonly called a bitmap.
12 For systems with multiple bits per pixel, the frame buffer is often
referred to as a pixmap.
13 Refreshing on raster-scan displays is carried out at the rate of 60 to
80 frames per second or cycles per second.
14 At the end of each scan line, the electron beam returns to the left
side of the screen to begin displaying the next scan line.
15 The return to the left of the screen, after refreshing each scan line is
called the horizontal retrace of the electon beam.
16 At the end of each frame the electrom beam returns (vertical retrace)
to the top left corner of the screen to begin the next frame.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 13 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Displays IV

17 A frame buffer has a size 1024x1024 with 12 bits per pixel. Compute
the time required to load it if transfer rate is 12 bits per second.
1024 × 1024 × 12
seconds.
12
Figure: Color Lookup table and Frame buffer

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 14 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Displays V


18 How long would it take to load a 640 by 480 frame buffer with 12 bits
per pixel, if 105 bits can be transferred per second? How long would
it take to load a 24-bit per pixel frame buffer with a resolution of
1280 by 1024 using this same transfer rate?
Total number of bits for the frame = 640 x 480 x 12 bits = 3686400
bits.
The time needed to load the frame buffer = 3686400
105 sec = 36.864 sec.
Total number of bits for the frame = 1280 x 1024 x 24 bits =
31457280 bits.
The time needed to load the frame buffer = 31457280
105 sec = 314.5728
sec.
19 Assuming that a certain full-color (24-bit per pixel) RGB raster
system has a 512-by-512 frame buffer, how many distinct color
choices (intensity levels) would we have available? How many
different colors could we display at any one time?

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 15 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Displays VI

Total number of distinct color available is 224 .


Total number of colors we could display at one time is 512 x 512.
20 Assuming that a certain RGB raster system has 512x512 frame buffer
with 12 bit per pixel and color lookup table with 24 bit for each entry.
How many distinct color choice we have available? How many
different color could we display at any one time? How much storage
spent altogether for the frame buffer and the color lookup table?
Total number of distinct color available is 224 .
Total number of different color could display at any one time is 212 .
The storage spent for frame buffer is 512 X 512 X 12 bit = 3145728
bit.
The storage spent for the color lookup table is 212 X 24 bit = 98304
bit.
So the total storage spent altogether is 3145728 + 98304 = 3244032
bit.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 16 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Displays VII


Figure: A raster-scan system displays an object as a set of discrete points
across each scan line.

21 On some raster-scan systems each frame is displayed in two passes


using an interlaced refresh procedure.
22 In the first pass, the beam sweeps across every other scan line from
top to bottom.
Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 17 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Displays VIII

23 Then after the vertical retrace, the beam sweeps out the remaining
scan lines.
24 Interlacing of scan lines in this way allows us to see the entire screen
displayed in one-half the time it would have taken to sweep across all
the lines at once from top to bottom.

Figure: Interlacing scan lines on a raster-scan display.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 18 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Displays IX

25 Interlacing is primarily used with slower refreshing rates. On an older,


30 frames-per-second, non interlaced display, for instance, some
flicker is noticeable. But with interlacing, each of the two passes can
be accomplished in 1/60 of a second, which brings the refresh rate
nearer to 60 frames per second. This is an effective technique for
avoiding flicker, providing that adjacent scan lines contain similar
display information.
26 Most CRT computer monitors scan each line in turn from top to
bottom at the lowest resolution levels (640 x 480 and 800 x 600 pixel
s). However, at the higher resolutions, such as 1024 x 768 or 1200 x
800, the frame is sometimes scanned in interlaced fashion: first the
odd-numbered lines, and then the even-numbered lines. This allows
for a lower refresh rate without producing flicker.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 19 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Displays X


27 With text and fixed graphics displays, this scheme can work well.
However, with animated graphics – especially images that move or
change form rapidly – interlacing can produce a fluttering effect at
least as irritating as screen flicker. For serious animated-graphics work
and video editing, a non- interlaced CRT display is recommended.
The refresh rate should be as high as the system will allow, ideally 70
Hz or more.
28 What is the fraction of the total refresh time per frame spent in
retrace of the electron beam for a noninterlaced raster system with a
resolution of 1280 by 1024, a refresh rate of 60 Hz, a horizontal
retrace time of 5 microseconds, and a vertical retrace time of 500
microseconds?
1 sec = 106 usec
1
Refresh rate = 60Hz = 60 sec to scan = 16.7 msec
The time for horizontal retrace = 1024 x 5=5120 usec
The time for vertical retrace = 500 usec
Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 20 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Displays XI

Total time spent for retrace = 5120 + 500 = 5620 usec = 5.62 msec
The fraction of the total refresh time frame spent in retrace = 5.62
16.7 =
0.337
29 Suppose an RGB raster system is to be designed using an 8-inch by
10-inch screen with a resolution of 100 pixels per inch in each
direction. If we want to store 6 bits per pixel in the frame buffer, how
much storage (in bytes) do we need for the frame buffer?
The size of frame buffer is (8 x 100 x 10x 100 x 6)/ 8= 600000 bytes

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 21 / 60
Computer Graphics Hardware Video Display Devices

Random Scan Displays I

1 When operated as a random scan display unit, a CRT has the


electrom beam directed only to the parts of the screen where a
picture is to be drawn.

Figure: A random-scan system draws the component lines of an object in


any specified order.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 22 / 60
Computer Graphics Hardware Video Display Devices

Random Scan Displays II


2 Pictures are generated as line drawings, with the electron beam
tracing out the component lines one after the other. For this reason,
random-scan monitors are also referred to as vector displays.
3 The component lines of a picture can be drawn and refreshed by a
random-scan system in a specified order. Example: Pen plotter
4 Refresh rate on a random-scan system depends on the number of
lines to be displayed on that system.
5 Picture definition is stored as a set of line-drawing commands in an
area of memory referred to as the display list, refresh display file,
vector file, or display program.
6 To display a specified picture, the system cycles through the set of
commands in the display file, drawing each component line in turn.
After all line-drawing commands have been processed, the system
cycles back to the first line command in the list.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 23 / 60
Computer Graphics Hardware Video Display Devices

Random Scan Displays III

7 Random-scan displays are designed to draw all the component lines of


a picture 30 to 60 times each second, with up to 100,000 short lines
in the display list.
8 When a small set of lines is to be displayed, each refresh cycle is
delayed to avoid very high refresh rates, which could burn out the
phosphor.
9 Random-scan systems were designed for line-drawing applications,
such as architectural and engineering layouts, and they cannot display
realistic shaded scenes.
10 Since picture definition is stored as a set of line-drawing instructions
rather than as a set of intensity values for all screen points, vector
displays generally have higher resolutions than raster systems.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 24 / 60
Computer Graphics Hardware Video Display Devices

Random Scan Displays IV

11 Also, vector displays produce smooth line drawings because the CRT
beam directly follows the line path. A raster system, by contrast,
produces jagged lines that are plotted as discrete point sets.
12 However, the greater flexibility and improved line-drawing capabilities
of raster systems have resulted in the abandonment of vector
technology.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 25 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Display Processor I

1 The purpose of the display processor is to free the CPU from the
graphics chores. In addition to the system memory, a separate
display-processor memory area can be provided.

Figure: Architecture of a raster-graphics system with a display processor.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 26 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Display Processor II

2 A major task of the display processor is digitizing a picture definition


given in an application program into a set of pixel values for storage
in the frame buffer.
3 Many pictures from 2D drawing to projected views of 3D objects
consist of graphical primitives such as lines, points, circles and filled
polygons. These picture components are often defined in continuous
space at a higher level of abstraction than individual pixels in the
discrete image space. Graphics commands specifying straight lines
and other geometric objects are scan converted into a set of discrete
points, corresponding to screen pixel positions.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 27 / 60
Computer Graphics Hardware Video Display Devices

Raster Scan Display Processor III

4 For instance, a line is defined by 2 end points and its line equation,
where as a circle is defined by its radius center position and the circle
equation. It is the responsibility of the graphics system or the
application program to convert each primitive from its geometric
definition into a set of pixels that make up the primitive in image
space. This conversion task is generally referred to as Scan
Conversion / Rasterization
5 Display processors are also designed to perform a number of
additional operations.These functions include generating various line
styles (dashed, dotted, or solid), displaying color areas, and applying
transformations to the objects in a scene. Also, display processors are
typically designed to interface with interactive input devices, such as
a mouse.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 28 / 60
Computer Graphics Hardware Video Display Devices

Character Generation I

1 The overall design style for a set of characters is called a typeface.


Typefaces can be divided into two broad groups serif and senserif.
Serif type has small lines or accents at the end of the main character
stroke while sans-serif type does not have accents. Serif type is
generally more readable. Sans serif type is more legible(easier to
recognize) therefore good for labeling and short headings.
2 Two different representations are used for storing computer fonts.
3 Bitmap font: Use rectangular grid pattern to represent character
shapes. They are simplest to define and display. the character grid
only need to be mapped to a frame buffer position. Require more
space because each variation (size and format) must be stored in a
font cache.It is possible to generate different sizes and other
variations such as bold and italic , from one set, but this usually does
not produce good results.
Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 29 / 60
Computer Graphics Hardware Video Display Devices

Character Generation II

4 Outline Font: Describe character shapes using straight lines and curve
sections.(Post Script fonts). Require less storage since each variation
does not require a distinct font cache. We can produce boldface,
italic or different sizes by manipulating the curve definitions for the
character outlines. But it does take more time to process the outline
fonts, because they must be scan converted into the frame buffer.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 30 / 60
Computer Graphics Hardware Video Display Devices

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 31 / 60
Computer Graphics Hardware Video Display Devices

Coordinate Representation I

1 We can construct the shape of individual object such as a tree in a


scene within separate coordinate reference frame called modeling
coordinates.
2 Once individual object shapes have been specified, we can place the
objects into appropriate positions within the scene using a reference
frame called world coordinates.
3 Finally the world coordinate description is transferred to one or more
output device reference frames for display. These display coordinate
systems are referred to as device coordinates or screen coodinates.
4 Generally, a graphics system first converts world coordinate positions
to normalized device coordinates, in the range from 0 to 1 , before
final conversion to specific device coordinates. This makes the system
independent of the various devices that might be used at a particular
workstation.
Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 32 / 60
Output Primitives Line Drawing Algorithms

Slope Intercept Line Drawing I

1 dx = x2 − x1
2 dy = y2 − y1
dy
3 m= dx
4 b = y1 − m × x1
5 Set x = x1, y = y 1
6 Set xend = x2
7 If x > xend stop
8 Plot a point at current (x, y ) coordinate.
9 Increment x = x + 1
10 Compute the next value of y from the equation y = mx + b
11 Goto step 6

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 33 / 60
Output Primitives Line Drawing Algorithms

DDA Algorithm I

1 The digital differential analyzer (DDA) is a scan-conversion line


algorithm based on calculating either δy or δx, using equations
δy = mδx and δx = δy m
2 A line is sampled at unit intervals in one coordinate and the
corresponding integer values nearest the line path are determined for
the other coordinate.
3 If the slope is less than or equal to 1, we sample at unit x intervals
δx = 1 and compute successive y values as yk+1 = yk + m
4 Because m can be any real number between 0.0 and 1.0, each
calculated y value must be rounded to the nearest integer
corresponding to a screen pixel position in the x column that we are
processing.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 34 / 60
Output Primitives Line Drawing Algorithms

DDA Algorithm II
5 For lines with a positive slope greater than 1.0, we reverse the roles of
x and y .That is,we sample at unit y intervals (δy = 1) and calculate
consecutive x values as xk+1 = xk + m1 . In this case, each computed
x value is rounded to the nearest pixel position along the current y
scan line.
6 If the starting endpoint is at the right, then either we have δx = −1
and yk+1 = yk − m or (when the slope is greater than 1) we have
δy = −1 with xk+1 = xk − m1
7 The DDA algorithm is a faster method for calculating pixel positions
than one that directly implements line equation y = mx + b.
8 It eliminates the multiplication in line equation by using raster
characteristics, so that appropriate increments are applied in the x or
y directions to step from one pixel position to another along the line
path.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 35 / 60
Output Primitives Line Drawing Algorithms

DDA Algorithm III

9 The accumulation of round-off error in successive additions of the


floating-point increment, however, can cause the calculated pixel
positions to drift away from the true line path for long line segments.
10 Furthermore, the rounding operations and floating-point arithmetic in
this procedure are still time-consuming. We can improve the
performance of the DDA algorithm by separating the increments m
and m1 into integer and fractional parts so that all calculations are
reduced to integer operations.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 36 / 60
Output Primitives Line Drawing Algorithms

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 37 / 60
Output Primitives Line Drawing Algorithms

Bresenhams Line Algorithm I

1 Bresenhams line algorithm is highly efficient incremental method for


scan-converting lines. It produces mathematically accurate results
using only integer addition, subtraction and multiplication by 2, which
can be accomplished by a simple arithmetic shift operation.
2 Assume that we want to scan-convert the line where 0 < m < 1. We
start with pixel P1‘ (x1‘ , y1‘ ), then select subsequent pixels as we work
our way to the right, one pixel position at a time in the horizontal
direction towards P2‘ (x2‘ , y2‘ ).

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 38 / 60
Output Primitives Line Drawing Algorithms

Bresenhams Line Algorithm II

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 39 / 60
Output Primitives Line Drawing Algorithms

Bresenhams Line Algorithm I

1 Once a pixel is chosen at any step, the next pixel is either the one to
its right (which constitutes a lower bound for the line) or the one to
its right and up (which constitutes an upper bound for the line) due
to the limit on m.
2 The line is best approximated by those pixels that fall the least
distance from its true path between P1‘ and P2‘ .
3 The coordinates of the last chosen pixel upon entering step i are
(xi , yi ). Our task is to choose the next one between the bottom pixel
S and the top pixel T. If S is chosen, we have xi+1 = xi + 1 and
yi+1 = yi . If T is chosen, we have xi+1 = xi + 1 and yi+1 = yi + 1 .
4 When s<t the closest pixel is S and when s>=t the closest pixel is T.
This difference is s − t = (y − yi ) − [(yi + 1) − y ]=2y − 2yi − 1.
5 The y coordinate on the mathematical line at pixel column position
xi + 1 is calculated as y = m(xi + 1) + b
Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 40 / 60
Output Primitives Line Drawing Algorithms

Bresenhams Line Algorithm II


6 s − t = 2m(xi + 1) + 2b − 2yi − 1
7 Substituting m by ∆y /∆x and introducing a decision variable
di = ∆x(s − t),
di = 2∆y × xi − 2∆x × yi + C where C = 2∆y + ∆x(2b − 1)
8 Similarly, we can write the decision variable di+1 for the next step as
di+1 = 2∆y × xi+1 − 2∆x × yi+1 + C
9 di+1 − di = 2∆y (xi+1 − xi ) − 2∆x(yi+1 − yi )
10 Since xi+1 = xi + 1, we have di+1 = di + 2∆y − 2∆x(yi+1 − yi )
11 If the chosen pixel is the top pixel T i.e., di ≥ 0 then yi+1 = yi + 1
and so di+1 = di + 2(∆y − ∆x). On the other hand if the chosen
pixel is the bottom pixel S i.e., di < 0 then yi+1 = yi and so
di+1 = di + 2∆y
12 We calculate d1 from the original definition of di , point(6) × ∆x by
substituting mx1 + b − y1 = 0, we have d1 = 2∆y − ∆x
Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 41 / 60
Output Primitives Line Drawing Algorithms

Bresenhams Line Algorithm I

1 x = x1, y = y 1
2 int dx = x2 − x1, dy = y 2 − y 1
3 dT = 2(dy − dx), dS = 2dy
4 int d = 2dy − dx
5 setpixel(x, y )
6 while(x < x2)
7 x ++
8 if (d < 0) d = d + dS
9 else y + + , d = d + dT
10 setpixel(x, y )

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 42 / 60
Output Primitives Line Drawing Algorithms

Bresenhams Line Algorithm II

1 Indicate which raster locations would be chosen by Bresenham’s


algorithm when scan converting a line from pixel coordinate (1,1) to
pixel coordinate(8,5).
2 dx=7, dy=4, dS=2dy=8, dT=2(dy-dx)=-6, d=2dy-dx=1
3 (x,y,d)=(1,1,1), (2,2,-5), (3,2,3), (4,3,-3), (5,3,5), (6,4,-1), (7,4,7),
(8,5,1)

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 43 / 60
Output Primitives Circle Drawing Algorithm

Eight Way Symmetry I

1 A circle is a symmetric figure. Any circle generating algorithm can


take advantage of the circle’s symmetry to plot eight point for each
value that the algorithm calculates.
2 Eight way symmetry is used by reflecting each calculated point
around each 45 degree axis.
3 P1 = (x, y ) P5 = (−x, −y )
4 P2 = (y , x) P6 = (−y , −x)
5 P3 = (−y , x) P7 = (y , −x)
6 P4 = (−x, y ) P8 = (x, −y )

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 44 / 60
Output Primitives Circle Drawing Algorithm

Eight Way Symmetry II

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 45 / 60
Output Primitives Circle Drawing Algorithm

Bresenhams Circle Algorithm I

1 Highly efficient as it avoids trigonometric and power functions for


drawining circle. Performs only integer addition, subtraction. Uses
eight way symmetry to generate a circle.
2 Avoids square-root calculations by comparing the squares of the pixel
separation distances.
3 The best approximation of the true circle is described by those pixels
in the raster that fall the least distance from the true circle.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 46 / 60
Output Primitives Circle Drawing Algorithm

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 47 / 60
Output Primitives Circle Drawing Algorithm

Bresenhams Circle Algorithm I

1 int x = 0, y = r , d = 3 − 2r
2 while(x <= y )
3 setpixel(x, y )
4 x ++
5 if (d < 0) d = d + 4x + 6, y = y
6 else y − − , d = d + 4(x − y ) + 10
7 end while
8 (x=0, r=8), Values(x,y,d)=(0,8,-13), (1,8,-3), (2,8,11),
(3,7,5),(4,6,7), (5,5,17)

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 48 / 60
Output Primitives Circle Drawing Algorithm

Midpoint Circle Algorithm I

1 Consider the coordinates of the point halfway between pixel T and


pixel S.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 49 / 60
Output Primitives Circle Drawing Algorithm

Midpoint Circle Algorithm I

1 int x = 0, y = r , d = 1 − r
2 while(x <= y )
3 setpixel(x, y )
4 x ++
5 if (d < 0), d = d + 2x + 1, y = y
6 else y − − , d = d + 2x + 1 − 2y
7 end while
8 (x=0, r=8), Values(x,y,d)=(0,8,-7), (1,8,-4), (2,8,1), (3,7,-6),(4,7,3),
(5,6,2),(6,5,5)

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 50 / 60
Region Filling

Region Filling I

1 Region filling is the process of ”coloring in” a definite image area or


region. Regions may be defined at the pixel or geometric level. At the
pixel level, we describe a region either in terms of the bounding pixels
that outline it or as the totality of pixels that comprise it. In the first
case the region is called boundary-defined and the collection of
algorithms used for filling such a region are collectively called
boundary-fill algorithms. The other type of region is called an
interior-defined region and the accompanying algorithms are called
flood-fill algorithms.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 51 / 60
Region Filling

4-connected and 8-connected I

1 4-connected: In this technique 4-connected pixels are used. We put


the pixels above, below, to the right, and to the left side of the
current pixels and this process will continue until we find a boundary
with different color.
2 There is a problem with this technique. Sometimes, the image is filled
only partially. In such cases, 4-connected pixels technique cannot be
used.
3 8-connected: In this technique 8-connected pixels are used. We are
putting pixels above, below, right and left side of the current pixels as
we were doing in 4-connected technique.
4 In addition to this, we are also putting pixels in diagonals so that
entire area of the current pixel is covered. This process will continue
until we find a boundary with different color.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 52 / 60
Region Filling

4-connected and 8-connected II

5 The 4-connected pixel technique failed to fill the area as marked in the
following figure which won’t happen with the 8-connected technique.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 53 / 60
Region Filling

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 54 / 60
Region Filling

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 55 / 60
Region Filling Boundary-Fill Algorithm

Boundary-Fill Algorithm I

1 This is a recursive algorithm that begins with a starting pixel, called a


seed, inside the region. The algorithm checks to see if this pixel is a
boundary pixel or has already been filled. If the answer is no, it fills
the pixel and makes a recursive call to itself using each and every
neighboring pixel as a new seed. If the answer is yes, the algorithm
simply returns to its caller.
2 This algorithm works elegantly on an arbitrarily shaped region by
chasing and filling all non-boundary pixels that are connected to the
seed, either directly or indirectly through a chain of neighboring
relations.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 56 / 60
Region Filling Boundary-Fill Algorithm

Boundary-Fill Algorithm II

3 Recursive boundary-fill algorithms may not fill regions correctly if


some interior pixels are already displayed in the fill color. This occurs
because the algorithm checks the next pixels both for boundary color
and for fill color. Encountering a pixel with the fill color can cause a
recursive branch to terminate, leaving other interior pixels unfilled. To
avoid this, we can first change the color of any interior pixels that are
initially set to the fill color before applying the boundary-fill procedure.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 57 / 60
Region Filling Boundary-Fill Algorithm

void BoundaryFill4(int x, int y, color newcolor, color edgecolor)


begin
int current;
current = ReadPixel(x, y);
if(current != edgecolor and current != newcolor)
begin
BoundaryFill4(x+1, y, newcolor, edgecolor);
BoundaryFill4(x-1, y, newcolor, edgecolor);
BoundaryFill4(x, y+1, newcolor, edgecolor);
BoundaryFill4(x, y-1, newcolor, edgecolor);
end
end

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 58 / 60
Region Filling Flood-fill Algorithm

Flood-fill Algorithm I

1 Flood fill algorithm is useful in cases where there are no single color
coundary for the polygon, i.e., the boundary has multiple colors.
2 In flood fill algorithm instead of filling color till you encounter a
specific boundary color you just fill the pixels with default color.
3 It is used in the ”bucket” fill tool of paint programs to fill connected,
similarly colored areas with a different color.
4 This algorithm also begins with a seed (starting pixel) inside the
region. It checks to see if the pixel has the regions original color. If
the answer is yes, it fills the pixel with a new color and uses each of
the pixels neighbors as a new seed in a recursive call. If the answer is
no, it returns to the caller.

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 59 / 60
Region Filling Flood-fill Algorithm

void FloodFill4(int x, int y, color newcolor, color oldColor)


begin
if(ReadPixel(x, y) == oldColor)
begin
FloodFill4(x+1, y, newcolor, oldColor);
FloodFill4(x-1, y, newcolor, oldColor);
FloodFill4(x, y+1, newcolor, oldColor);
FloodFill4(x, y-1, newcolor, oldColor);
end
end

Veena Khandelwal (Dept of CSE IIST Indore) Computer Graphics & Multimedia August 28, 2018 60 / 60

You might also like