LECTURE 13
Graphics programming in C language
Programs:
text
mode
e6_1.c,
e6_2
graphics mode e7_1.c ... e7_5A.c
Tomasz Zieliński
MEMORY USAGE IN TEXT AND GRAPHICS MODES
RAM memory
Command.com
memory
for
programs
Stack
Interrupt survices
Text mode
In/Out devices
Graphics mode
A:0000
B:0000
0:0000
Address
64 kB
64 kB
10 times 64 kB
Graphics
card
Monitor
(hexadecimaly)
11 times 64 kB
SCREEN IN GRAPHICS MODE
640
(0, 0)
(479, 639)
MODE VGA
480
B
G
Other graphics
modes:
320 x 200 (CGA)
640 x 480 (VGA)
800 x 600 (SVGA)
1024 x 768 (XGA)
1280 x 800 (WXGA)
1280 x 1024 (SXGA)
1600 x 1200 (UXGA)
720 x 480 (NTSC)
720 x 576 (PAL)
1280 x 720 (HD 1)
1920 x 1080 (HD 2)
8 b
8 bits
8 b
R
E.g. 1000 pixels x 1000 pixels x
8
bits
x
8 bits
x
8
bits
= 24 000 000 bits
Digital image as a matrix of numbers
a)
b)
x(1, 1)
x(M, N)
x(M, 1)
x(2, 1)
x(3, 1)
x(1, 2)
x(2, 2)
x(3, 2)
x(M, 2)
x(1, N)
x(2, N)
x(3, N)
x(4, 1)
x(4, 2)
x(4, N)
N
M
n
m
11
236
117
43
98
177
22
43
165
125
234
155
33
76
249
0
0
133
44
87
149
0
0
199
55
99
135
217
168
111
66
121
77
88
99
110
6
6
c)
5
10
15
20
25
5
10
15
20
25
0
100
200
n
m
x(
m
,n
)
d)
5
25
20
15
10
5
10
15
20
25
Analog image
⇒ digital image
y
x
x
y
discretization in space
quantization of values
analog image
digital image
matrix of numbers
y
x
x
y
y
x
m
n
Line scanning of digital image
scanning line number 10
1
5
10
15
20
y
1
5
10
15
20
pixel value, grey level number
1
5
10
15
20
64
128
255
10
1
5
15
192
160
→
0
100
200
300
400
500
column number n
0
50
100
150
200
250
300
gr
ey
le
ve
l
hat
hair
VIDEO AS A 3-D MATRIX
film frame 1
film frame 2
film frame 3
n
m
time
!!! Data from Computed Tomography (CT) are 3-D matrices also !!!
CODING COLORS
Analog standard
NTSC
(
YIQ
)
− USA:
0,299
0,587
0,114
0,736(
) 0,268(
) 0,596
0, 274
0,321
0,478(
) 0.413(
) 0,211
0,523
0,311
Y
R
G
B
I
R Y
B Y
R
G
B
Q
R Y
B Y
R
G
B
=
+
+
=
−
−
−
=
−
−
=
−
+
−
=
−
+
Analog standard
PAL
(
YUV
)
− EUROPE:
0,299
0,587
0,114
0,492(
)
0,147
0,289
0,436
0,877(
) 0,615
0,515
0,100
Y
R
G
B
U
B Y
R
G
B
V
R Y
R
G
B
=
+
+
=
−
= −
−
+
=
−
=
−
−
Digital television,
DVD
(
YCbCr
):
0,299
0,587
0,114
0,564(
)
0,1687
0,3313
0,5
0,713(
)0,5
0,4187
0,0813
Y
R
G
B
Cb
B Y
R
G
B
Cr
R Y
R
G
B
=
+
+
=
−
= −
−
+
=
−
−
−
SCREEN IN TEXT MODE
background
color
MODE C80, BW80
(0, 0)
(24, 79)
80
25
I T L E
O P T I O
N
O P T I O
N
O P T I O
O P T I O
N
N
3
1
2
4
8 x 8
forground
kolor
[
]
T
SCREEN OF TEXT MODE
Table of ASCII codes
SCREEN OF TEXT MODE
Table of extended ASCII codes
(decimal number, character)
ZAPIS OBRAZU EKRANU
IN MEMORY
Text mode:
char
attributes
ASCII code of char
background
attributes
8 bits
4 bits
4 bits
attributes = color, possible blinking
Graphics mode:
color index in
palette
x bits
color index in
palette
x bits
color index in
palette
x bits
PALETTE OF COLORS IN GRAPHICS MODE
color =
c
R
⋅
R
⊕
c
G
⋅
G
⊕
c
B
⋅
B
Example:
palette
with 64 grey levels (8 bits per color)
Color index(number)
c
R
(RED)
c
G
(GREEN)
c
B
(BLUE)
0
0
0
0
1
4
4
4
2
8
8
8
3
12
12
12
4
16
16
16
5
20
20
20
...
...
...
...
61
244
244
244
62
248
248
248
63
252
252
252
C LANGUAGE FUNCTIONS FOR TEXT MODE
(Borland C/C++ ver. 3.11) ------------------------------------------------------------------------------------------------------------------------------------------------------
textmode( mode )
mode =
C80, C40, BW80, BW40 (Color, BlackWhite)
clrscr();
clear
screen
textcolor( color )
character color = BLACK, BLUE, GREEN, CYAN,
RED, MAGENTA, BROWN, ..., YELLOW, WHITE
textbackground( color )
background
color
textattr( color )
both: char color (upper 4bits), back color (lower 4b)
highvideo()
intensive color
lowvideo()
pale color
_setcursortype( type )
type = _NOCURSOR (without), _SOLIDCURSOR
(block), _NORMALCURSOR (underlining)
col = wherex()
return
x
coordinate of the
cursor
row = wherey()
return
y
coordinate of the
cursor
goto(
x,
y)
shift
cursor
to the point (x, y)
putch( ch )
print char in the actual
cursor
position
window( x1, y1, x2, y2)
active window (x1, y1) - (x2, y2), relative coordinates
cprintf(”%s”, text); cputs(”text”)
print text in the actual
cursor
position
ch = getch(); cscanf( ”%d”, &zmienna)
read char or number from keyboard
char bufor[82]; string = cgets(bufor)
read string of chars from keyboard buffer
/* Example 6.1 – GRAPHICS – Text mode - Borland C 3.11 DOS */
#include <stdio.h>
#include <conio.h>
#include <string.h>
int
menu
( char *title, char *options[ ], int many, int x, int y, int dx );
void
frame
( int x, int y, int dx, int dy, char *title );
void
textline
( int x1, int y1, int x2, int y2, char c );
/* main program ------------------------------------------------------------------- */
void main()
{
char *title = "MENU";
char *options[] = { " 0000 ", " 1111 ", " 2222 ", " 3333 " };
int many = 4;
int column = 30, row = 10, width = 8;
int numer;
numer =
menu
( title, options, many, column, row, width);
printf("\n Option number %d has been chosen ! \n", numer );
}
/* Auxiliary functions ------------------------------------------------------------ */
int menu( char *title, char *options[ ], int many, int x, int y, int dx)
{
char c;
int i, num;
int xofs, yofs;
xofs = 1; yofs = 1;
// offset in frame
clrscr
();
// clear screen
_setcursortype
(_NOCURSOR);
// do not display cursor
frame
( x-1, y-1, dx+1, many+1, title );
// draw frame
window
( x, y, x+dx, y+many );
// define text window
// options[ 0 ] - intensive, blue letters on yellow background
highvideo
();
textcolor
( BLUE );
textbackground
( YELLOW );
gotoxy
( xofs, yofs );
cprintf
( "%s", options[ 0 ] );
// other options - pale, white letters on black screen
lowvideo
();
textcolor
( WHITE );
textbackground
( BLACK );
for( i=1; i<many; i++)
{
gotoxy
( xofs, yofs+i );
cprintf
( "%s", options[ i ] ); }
// CHOICE LOOP: the chosen option is changed via arrows
num = 0;
while( (c =getch()) != 13 )
// wait for ENTER
{
switch( c ) {
case 80:
// arrow down
lowvideo();
textcolor( WHITE ); textbackground( BLACK );
gotoxy
( xofs, yofs+num );
cprintf
("%s", options[ num ]);
num++;
if ( num > many-1 ) num = 0;
highvideo();
textcolor( BLUE ); textbackground( YELLOW );
gotoxy
( xofs, yofs+num );
cprintf
("%s", options[ num ]);
break;
case 72:
// arrow up
lowvideo();
textcolor( WHITE ); textbackground( BLACK );
gotoxy
( xofs, yofs+num );
cprintf
( "%s", options[ num ]);
num--;
if ( num < 0 ) num = many-1;
highvideo();
textcolor( BLUE );textbackground( YELLOW );
gotoxy
( xofs, yofs+num );
cprintf
("%s", options[ num ]);
break;
}
}
textattr( WHITE ) ; textcolor( WHITE );
// standard
window( 1, 1, 80, 25);
// whole screen
clrscr();
// clear screen
return( num );
}
/* ----------------------------------------------------------------------------------------- */
void
frame
( int x, int y, int dx, int dy, char *title)
{
char *text;
textcolor
( YELLOW );
// line color
// LINES
textline
( x, y, x+dx, y,
205 );
// horizontal upper
textline
( x, y+dy, x+dx, y+dy, 205 );
// horizontal lower
textline
( x, y, x, y+dy, 186 );
// vertical first
textline
( x+dx, y, x+dx, y+dy, 186 );
// vertical second
// CORNERS
gotoxy
( x, y );
putch
( 201 );
// upper left
gotoxy
( x+dx, y );
putch
( 187 );
// upper right
gotoxy
( x+dx, y+dy );
putch
( 188 );
// lower left
gotoxy
( x, y+dy );
putch
( 200 );
// lower right
strcpy( text, "[" );
// TITLE
strcat( text, title );
strcat( text, "]" );
gotoxy
( x+(dx-strlen(title))/2, y );
cprintf
( text );
textcolor( WHITE );
}
/* ---------------------------------------------------------------------------------------- */
void
textline
( int x1, int y1, int x2, int y2, char
c
)
{
int ix, iy;
if ( x1 == x2 )
// vertical line
{ for( iy=y1; iy<=y2; iy++ )
{
gotoxy
( x1, iy );
putch
(
c
); } }
if ( y1 == y2 )
// horizontal line
{ for( ix=x1; ix<=x2; ix++ )
{
gotoxy
( ix, y1 );
putch
(
c
); } }
}
C LANGUAGE FUNCTIONS FOR GRAPHICS MODE (1)
(Borland C/C++ ver. 3.11) ------------------------------------------------------------------------------------------------------------------------------------------------------
initgraph( &driver, &mode);
driver = DETECT, CGA, EGA, VGA
closegraph();
mode = VGALO, VGAMED, VGAHI (640 x 480)
MaxX = getmaxx();
return pixels resolution in ax x
MaxY = getmaxy();
return pixels resolution in ax y
cleardevice();
clear
screen
setviewport( x1, y1, x2, y2);
active window (x1, y1) - (x2, y2)
putpixel( x, y, color );
set color of the pixel (x, y)
color = getpixel( x, y);
get color from the pixel (x, y)
setcolor( color );
set active color for further drawings: pixels,lines
line( x1, y1, x2, y2);
line from point (x1, y1) to point (x2, y2)
moveto( x1, y1);
shift
cursor
to the point (x1, y1)
lineto( x2, y2 );
draw line from
cursor
to the point (x2, y2)
setlinestyle(
style
,
pattern
,
width
);
style
DOTTED_LINE
(dotted)
pattern
0xAB (bit pattern)
SOLID_LINE (solid)
width
CENTER_LINE
(dash-dot)
NORM_WIDTH
(normal)
DASHED_LINE
(dashed)
THICK_WIDTH
(thick)
USERBIT_LINE
(patrz
wzór)
C LANGUAGE FUNCTIONS FOR GRAPHICS MODE (2)
(Borland C/C++ ver. 3.11) ------------------------------------------------------------------------------------------------------------------------------------------------------
// WRITING TEXT IN GRAPHICS MODE
char *s;
//
horizon
: LEFT_TEXT, CENTER_TEXT,
sprintf( s, ”%d”, number );
//
RIGHT_TEXT
settextjustify(
horizon
,
vertical
);
//
vertical
: BOTTOM_TEXT, CENTER_TEXT,
outtext( x, y, s);
//
TOP_TEXT
Tekst
Tekst
Tekst
Tekst
Tekst
Tekst
Tekst
Tekst
Tekst
ANIMATION
(Borland C/C++ ver. 3.11) ------------------------------------------------------------------------------------------------------------------------------------------------------
void *bufor;
// store to buffer image from (x1,y1)
long size;
//
to
(x2,y2)
using
function
getimage()
size = imagesize( x1, y1, x2, y2);
// and next put this image in some other
bufor = malloc( size );
// place, performing logical operation
getimage( x1, y1, x2, y2, bufor );
// XOR, OR, AND on bits “existing” on
putimage( x, y, bufor, COPY_PUT );
// the screen and „overlaid”
XOR_PUT
//
getimage
// beginning
OR_PUT
//
LOOP: putimage(XOR); // old position
AND_PUT
//
putimage(XOR); // new position
NOT_PUT
//
goto LOOP
LOOP:
setactivepage(0);
//
in graphics memory we can have a few graphical
// draw on page(0);
//
„pages”, numbered 0, 1, 2, ...
setvisualpage(0);
//
one page is drown, the other is displayed
setactivepage(1);
//
set page 1 active for drawing
// draw on page(0);
//
draw on page 1
setvisualpage(1);
//
set page 1 to be visualized (displayed)
goto
LOOP;
//
/* Example 7.1: GRAPHICS – graphics mode – Borland C 3.1 DOS*/
/* 2D plot - sum of a few sinusoids */
/* Include libraries ------------------------------------------------------------------*/
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
// add graphics library
#include <math.h>
/* Define GLOBAL constants and variables --------------------------------*/
#define MaxDATA 500
// maximum length of data vector
int
GraphDriver;
// graphics driver
int GraphMode;
// graphics mode
int
ErrorCode;
// error code
/* Define your own functions ----------------------------------------------------*/
// not any
/****************************** Main program *******************************/
main()
{
/* Define LOCAL constants and variables ----------------------------------*/
float data[ MaxDATA ]; // vector with data to be plotted
int MaxX, MaxY;
// max resolution horizontally and vertically
int i, j;
// work indexes
float pi=3.1415927;
// constant
int sX, sY, lenX, lenY;
// other work variables
int nX, nY;
// described below
float fX, dX, fY, dY, miniY, maxiY, diffY, v; //
char *s;
/* Initialize graphics mode ------------------------------------------------------- */
GraphDriver = DETECT;
/* request for autodetection */
initgraph
( &GraphDriver, &GraphMode, "" );
/* initialization */
ErrorCode =
graphresult
();
/* result of initialization */
if( ErrorCode != grOk )
/* any error? */
{
printf("Graphics System Error: %s\n",
grapherrormsg
( ErrorCode ) );
return( 1 );
}
MaxX =
getmaxx
();
// horizontal resolution (number of pixels)
MaxY =
getmaxy
();
// vertical resolution (number of pixels)
cleardevice
();
// clear graphical screen
setviewport
( 0, 0, MaxX, MaxY, 0);
// view port = whole screen
/* Generation of data to be visualized ---------------------------------------*/
for( i = 0 ; i < MaxDATA; i++ )
data[ i ] = 0.1 + 0.5 * sin(2.*pi*i/100.) + 0.25 * cos(2.*pi*i/25.);
/* Visualization -----------------------------------------------------------------------*/
sX = 20;
// upper right plot corner in pixels (S-tart X)
sY = 20;
//
(S-tart Y)
lenX = MaxDATA;
// plot dimension horizontally (X)
lenY = MaxY-100;
// plot dimension vertically (Y)
nX = 5;
// X axis – number of segments
fX = 0.;
// X axis – first value of the axis description
dX = 100.;
// X axis – values spacing (difference)
nY = 8;
// Y axis - number of segments
fY = -1.0;
// Y axis - first value of the axis description
dY = 0.25;
// Y axis - values spacing (difference)
miniY = fY;
// Y axis - minimum value of the label
maxiY = fY + nY*dY;
// Y axis – maximum value of the label
diffY = maxiY - miniY;
// Y axis - difference max-min
setcolor
(getmaxcolor()/2);
// set white color
line
( sX, sY, sX+lenX, sY );
// FRAME
line
( sX+lenX, sY, sX+lenX, sY+lenY );
//
line
( sX+lenX, sY+lenY, sX, sY+lenY );
// four lines
line
( sX, sY+lenY, sX, sY );
//
settextjustify
( LEFT_TEXT, CENTER_TEXT);
// Y AXIS
for( i = 0 ; i <= nY; i++)
// description
{
//
sprintf
( s, "%6.2f", fY + i * dY );
//
outtextxy
( sX+lenX, sY+(nY-i)*lenY/nY, s );
//
}
settextjustify
( CENTER_TEXT, TOP_TEXT);
// X AXIS
for( i = 0 ; i <= nX; i++)
// description
{
//
sprintf
( s, "%3.0f", fX + i * dX );
//
outtextxy
( sX+i*lenX/nX, sY+lenY+8, s);
//
}
//
setlinestyle
( DOTTED_LINE, 0x00, NORM_WIDTH);
// GRID
for( i = 1 ; i < nY; i++)
//
{
line
( sX, sY+i*lenY/nY, sX+lenX, sY+i*lenY/nY ); }
// horizon
for( i = 1 ; i < nX; i++)
//
{
line
( sX+i*lenX/nX, sY, sX+i*lenX/nX, sY+lenY ); }
// vertical
setlinestyle
( SOLID_LINE, 0x00, NORM_WIDTH);
// PLOT
setcolor
(getmaxcolor());
// intensive white
v = data[ 0 ];
//
if ( v < miniY ) v=miniY;
//
if ( v > maxiY ) v=maxiY;
//
j = (int) ( ( (v-miniY)/diffY ) * lenY );
//
moveto
(
sX,
sY+lenY-j
);
//
for( i = 1; i < MaxDATA; i++ )
//
{
//
v = data[ i ];
//
if ( v < miniY ) v=miniY;
//
if ( v > maxiY ) v=maxiY;
//
j = (int) ( ( (v-miniY)/diffY ) * lenY );
//
lineto
(
sX+i,
sY+lenY-j
);
//
}
//
/* Wait for pressing the key <Escape> ------------------------------------------ */
setcolor
(getmaxcolor()-1);
// yellow color
settextjustify
( CENTER_TEXT, TOP_TEXT);
// how to center
outtextxy
( sX+lenX/2, MaxY-10, "WYJSCIE = Esc");
// info out
while( getch() != 27 );
/* The end ------------------------------------------------------------------------------*/
cleardevice();
// clear graphical screen
closegraph();
// close graphical mode
textmode(C80);
// return to text mode
clrscr();
// clear text screen
printf( "\n" );
printf( " Bye ! That’s all for now !\n" );
printf( "\n" );
return(0);
}