Thursday, May 15, 2008

Graphics in C

Basic concepts for computer generated Graphics:

A computer draws a picture (graphics) as a combination of dots(pixels) in different colors in different positions . This graphics is displayed on a monitor or sent to a printer .

Concept 1 : Monitor resolution

The monitor may display the output in text mode or graphics mode.

Text Mode :

In text mode (DOS) the monitor displays 80 characters in 25 lines [ 80 X 25 ] .

Graphics Mode :

In graphics mode the monitor resolution is measured in terms of pixels number of dots that can be displayed in each row and column.This resolution is determined by :-

  • The type of display adapter card we are using
  • The Operating System’s monitor display specification that we have set
  • Greater the resolution finer the picture .

Concept 2 :Graphics are mathematical in origin

Graphics are based on some mathematical algorithms (formula or equations or principles). For e.g a circle has an equation and to draw a circle you generate a set of points of points(x , y ) satisfying that equation.

Concept 3 :Graphical Functions in C

C has a number of library functions for managing simple graphical programs . Include header file

You may also write your own functions to create specific graphical objects .The graphics.h header file also includes several constants (enums) that we can use in our programs

Concept 4 : Graphics files like .jpg , .bmp , .gif

When we draw graphics using graphical packages like paintbrush the files get saves in a specific file format like with extensions like .bmp , .jif , .gpg etc. These files store data about the various graphical functions and data structures , data etc into a persistent storage device(hard disk) that helps us to retrieve the picture as and when we want.

Concept 5 : initgraph( ) function in C

A graphics program in C requires to call the initgraph() function that initializes the graphical system by loading a graphics driver from the system .initgraph() also resets all the graphical settings like color , palette , current position … to their defaults . It then resets graphresult to 0.

Declaration:

void far initgraph(int far *graphdriver, int far *graphmode,

char far *pathtodriver);

*graphdriver :- Integer that specifies the graphics driver to

be used.

*graphmode :- Integer that specifies the initial graphics

mode (unless *graphdriver = DETECT).

If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver. You can give graphmode a value using a constant of the graphics_modes enumeration type.

pathtodriver : Specifies the directory path where initgraph

looks for graphics drivers (*.BGI) first.

If they're not there, initgraph looks in the current directory. If pathtodriver is null, the driver files must be in the current directory.

This is also the path settextstyle searches for the stroked character font files (*.CHR).

Return Value of initgraph( ) function:

initgraph sets the internal error code On success code is set to to 0 and On error, initgraph sets *graphdriver to -2, -3, -4, or -5, and graphresult returns the same value.

Note :

*graphdriver and *graphmode must be set to valid graphics drivers and graphics mode values or you'll get unpredictable results.

(The exception is graphdriver = DETECT for auto detection.)

After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode is set to the current graphics mode.

Program:

#include

#include

#include

#include

int main(void)

{

int gdriver = DETECT, gmode, errorcode;

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

errorcode = graphresult();

if (errorcode != grOk)

{ _

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1);

}

line(0, 0, getmaxx(), getmaxy());

getch();

closegraph();

return 0;

}

Concept 6 : Graphical library functions are available. One can use them to create graphical images.

Some Common functions are:

arc( ) ,bar( ) ,bar3d( ), circle( ) , closegraph ( ) , drawpoly( ) , ellipse ( ) , fillellipse( ), fillpoly ( ), floodfill( ), getbkcolor( ) getcolor( ) , getimage( ) , getmaxx ( ) , getmaxy( ), getpixel , getx( ) , gety ( ) , line( ), lineto ( ) , moveto ( ) , outtext( ) ,

outtextxy( ), pieslice ( ) , putimage( ) , putpixel( ), rectangle( ) , setbkcolor ( ) , setcolor( ) , setfillpattern ( ) , setfillstyle( ) , setlinestyle ( ) , settextjustify( ) , settextstyle( ) , textheight( ) , textwidth( )

bgi.exe is a graphics demo file bundled with Borlands Turboc and one can learn a lot by going through the source code bgi.c

No comments: