You are on page 1of 25

PAINT USING ‘C’

A PROJECT REPORT Submitted by

SANTOSH KUMAR T (Y6IT297) SREEKANTH G (Y6IT301) SURESH KUMAR B (Y6IT315)

in partial fulfillment for the award of the degree of

BACHELOR OF TECHNOLOGY
IN

INFORMATION SCIENCE AND TECHNOLOGY

KONERU LAKSHMAIAH COLLEGE OF ENGINEERING GREEN FIELDS, VADDESWARAM

APRIL 2008

Koneru Lakshamaiah College of Engineering
(Autonomous) Green Fields, Vaddeswaram, Guntur Dist, AP, India – 522 502

CERTIFICATE
g{|á |á àÉ vxÜà|yç à{tà à{|á Å|Ç| ÑÜÉ}xvà ãÉÜ~ xÇà|àÄxw |á t

“PAINT USING ‘C’ ”

ÜxvÉÜw Éy ãÉÜ~ wÉÇx uç SANTOSH KUMAR T (Y6IT297), SREEKANTH G (Y6IT301) &
SURESH KUMAR B (Y6IT315)

áâuÅ|ààxw |Ç ÑtÜà|tÄ yâÄy|ÄÄÅxÇà Éy à{x ÜxÖâ|ÜxÅxÇàá yÉÜ à{x

tãtÜw wxzÜxx Éy Utv{xÄÉÜ Éy gxv{ÇÉÄÉzç |Ç \ÇyÉÜÅtà|ÉÇ fv|xÇvx tÇw gxv{ÇÉÄÉzç âÇwxÜ Åç áâÑxÜä|á|ÉÇ tÇw zâ|wtÇvx wâÜ|Çz à{x tvtwxÅ|v çxtÜ ECCJ @ CKA

K. Sreekanth Head of the Department

Project – in – charge

However. ellipses. The objective of the project is to create an application that can draw simple images. lines. So applications are required to create images and save them. The Paint using ‘C’ application is created to easily draw images using several tool options. We can further retrieve the images and view them.1 Abstract Modern Technology brought the use of pictures in various fields. the options for editing of images like cropping are not provided. Also a simple picture speaks thousand words. . So the images can be viewed only in this application. C language has been choosen for the purpose. An attempt has been made to do the same using simple algorithms. As C is the simple language for coding. rectangles. They include options like drawing circles. However. The images created using this application can be saved. We are able to save the images into raw file format. polygon and solid fill areas in 2D. complex 3D images are not considered for the project. This application is compatible with Windows Operating System and C compiler.

Sreekanth. School of Computing. Principal for the motivation and support provided.S. K Rajasekhara Rao. for their guidance. Finally we thank one and all for extending their support in successful completion of our mini project work. S. Reddy. We wish to express our sincere and heartfelt gratitude to all those who have helped us in one-way or the other for the successful completion of this mini project. without which it would have been difficult for us to complete this mini project work successfully. We take this opportunity to express our sincere gratitude and indebtedness to the project guide for giving us timely suggestions and directing us towards perfection. We thank Prof. We express our thanks to K. L . advice and constant encouragement. for kindly permitting us to carryout this work. Dean. Dr. Division Head.2 ACKNOWLEDGEMENTS The success accomplished in our project would not have been possible but for the timely help and guidance of many people. Department of Information Science and Technology. Santosh Kumr T (Y6IT297) Sreekanth G (Y6IT301) Suresh Kumar B (Y6IT315) .

Scope for future implementation Conclusion 1 2 3 4 4 4 7 7 7 7 9 9 9 10 10 12 14 17 20 21 21 22 22 22 22 23 .3. Mouse pointer 5.2.5.1.2.5.2.6.6.1. Credits 4. Fill area tool 4. New 4.3. Menu bar 4.2.1.2.4.3. 6. Open 4.1.1. 3. Polygon tool 4. Colour tool bar 4. Status bar 4. Draw pad 4.4. Exit 4. Line tool 4. Circle tool 4. Introduction Initialising Graphics Structure Algorithm 4.5.2. Rectangle tool 4.3 INDEX S. 2.4. Tool bar 4.1.1.2. Save 4. 4.2.No Content Page No Abstract Acknowledgement Index 1.1. Pencil tool 4.2.

of tool bars (rectangle tool bar. so that only 16 colors are available to use. The execution of the programs may work a bit slowly. 4.h'. color tool bar. the graphics driver. 3. it should be changed to graphics mode. line tool. A few no. 5. Initializing Graphics Initially ‘c’ language is in text mode.. coding and implementation of ‘C’ paint we discussed further. circle tool. The initgraph() function takes three parameters. Here we are assuming that the driver files are in the directory 'c:\\turboc30\\bgi'. initgraph() sets up the numeric values of the graphics mode chosen in the variables gd and gm respectively. 3. In our program the function initgraph() is responsible for switching the mode from text to graphics .4 1. 2. The algorithms.DETECT is a macro defined in 'graphics.. Draw pad facilitates the user to draw the pictures inside draw pad window. It requests initgraph() to automatically determine which graphics driver to load in order to switch to the highest resolution graphics mode. Once the driver has been loaded.) are provided by ‘C’ paint. Hence the path passed to initgraph() is 'c:\\turboc30\\bgi'. status bar and draw pad to draw pictures. Structure of Paint using ‘C’ The basic structure of ‘c’ paint is shown below. tool bar. the graphics mode and the path to the driver file. . The structure of ‘c’ paint is divided into menu bar. Hence we must first change over to graphics mode. The coding is done in such a way that it can be improved and implemented with more applications in an easy way. Status bar displays help messages while using ‘C’ paint. etc. ’C’ graphics provides 16 number of colors. It allows drawing pictures and saving them. Introduction ‘C’ paint is just similar to MS paint. 2. The screen or the window is divided into 5 areas: 1. Menu bar Tool bar Color tool bar Status bar Draw pad The following gives a overview of the structure.

Setbkcolor() is used to change back ground color. using outtextxy() new. 6. A special symbol is placed at the bottom to indicate current drawing color and current back ground color and there are drawn with bar() functions. 9. open. All the menu bar options are placed inside a rectangle window using rectangle() function. The flow chart the main function which gives the entire structure of the program is show here. save. Using rectangle() function 15 squares boxes are drawn at right side of the screen at fixed x position and varying y position. 4. . Using rectangle() function 6 squares are drawn with same size at fixed x position and varying y position.5 Fig: Basic Structure of the ‘C’ Paint application Various pre-defined functions used to design the structure are: 1. Status bar placed at the bottom is drawn using bar() function . 3. 2. 7. 8. Using bar() and setfillstyle() function draw pad is drawn. In the status bar using outtextxy() x and y positions are displayed. about. 5. exit options are displayed.

6 .

2. 2. It consists of five options. Move mouse pointer to open option. user defined function a new file is opened.2. Using new(). 3. Using open(). 5.1. Close the temp file If file doesn’t exists display an error message (incorrect file name). 5. 4. Move mouse pointer to new option. 2. Plot each pixel using putpixel() function by reading data from temp file. Procedure to open an existing file 1. . 4. Copy the data into a temp file and save it temporarily. 1.7 4. Move mouse pointer to save option. Menubar Menu bar provides the users with open and save options for the pictures. Use left click of mouse to select it. 3. New: clear the draw pad area using bar() function. 4. Using save(). 3.1. 3. user defined function existing file is opened.1. 2. 2. 4. Use left click of mouse to select it. New Open Save Credits Exit Procedure to open a new file: 1. Procedure to save the picture: 1. Use left click of mouse to select it. Open(): 1. Algorthim 4.1. Using fopen() function open file given by the user. 3. user defined function the picture is saved with given file name.

8 .

About(): Gives the information about the creators and developers of the program. Using fopen() function open a new file with given name in write mode.1. . 4.9 4.3. Save(): 1. After writing the data close the file using fclose() function.5.1.1.4. Exit(): Exits from ‘c’ paint. 4. 2. 3. Get each pixel using getpixel() function and write it into the file.

2.px=1.tx.ty=y.fy). rectangle(tx.t=0. } if(button!=1) { setcolor(fg).fx. } else if(button==1 && flag==1 ) { fx=x.1. . flag=1. } changemouseptr(plus). Line tool 5. The various tool bars provided are: 1. setcolor(bg). Circle tool 4.fx. Toolbar Tool bar makes the users works or drawing pictures easier. Use left click of mouse to select it. while(1) { getmousepos(). Rectangle tool 2. Fill area tool 6. hidemouseptr(). if(button==1&&flag==0) { ix=x.but. rectangle(tx. 3.fy). if(x>=35&&x<=600&&y>=40&&y<=450) { if(button==1 && t==0) { tx=x. showmouseptr(). if(px!=x||py!=y) printmousepos().py=1. t=1.fy=y. Using rect(). setcolor(fg).iy=y. Pencil tool 3.2.ty.fx.iy.ty. rectangle(tx. Move mouse pointer to rectangle tool. Procedure to draw rectangle: 1.fy.fx.10 4.ty.2.ty. user defined function a rectangle is drawn. Polygon tool 4. void rect() { int ix. flag=0.fy).flag=0.

fy). py=y. } t==0.ty.11 t=0. changemouseptr(hand). break. } } else { if(t==1) { setcolor(fg).fx. rectangle(tx. } px=x. } } .

flag=0.12 4. void freehand() { int px=1. Pencil Tool It is used for drawing images with free hand. .2.2.py=1.but.

if(px!=x||py!=y) printmousepos().x. break.y). showmouseptr(). } else if(flag!=0) { showmouseptr(). if(x>=35&&x<=600&&y>=40&&y<=450) { changemouseptr(pencil). py=y. } else line(px. flag=1. } } } . } px=x. flag=0.13 while(1) { getmousepos(). } else { changemouseptr(hand).py. if(button==1) { if(flag==0) { hidemouseptr().

a1. void circ() { int ix.q.iy.a. while(1) { getmousepos(). .b1.3.b.flag=0. Circle We can draw either circle or ellipse or circle using this tool. The following code describes the procedure for a circle function.py.p. if(x>=35&&x<=600&&y>=40&&y<=450) { changemouseptr(pencil).p1.but.px.q1.2.14 4.

q1=q.a1. showmouseptr().b1=b.iy=y.a1=a. q=(iy+y)/2.q1. if(p1!=p||q1!=q) { Ellipse1(p1. } else { p=(ix+x)/2. Ellipse(p. } } } else if(flag!=0) { showmouseptr().a. } if(button==1) { if(flag==0) { hidemouseptr().b1).15 if(px!=x||py!=y) { printmousepos(). b=abs(iy-q).q. a=abs(ix-p). ix=x. } else { changemouseptr(hand). } } } . flag=0.b). p1=p. } px=x. py=y. p1=q1=a1=b1=0. flag=1. break.

16 .

y2 = t .4.17 4. y2 = t . i .x1 .y1 . } for ( i = y1. . y1 = y2 . if ( x1 > x2 ) { t = x1 . i <= y2 . t = y1 . } dx = x2 . int x2. Line Tool Using the bresenhams algorithm for drawing lines. i++ . } return . i < x2 . x1 = x2 . t. i. if ( dx == 0 ) /* vertical line */ { if ( y1 > y2 ) { t = y1 .i). } if ( dy == 0 ) /* horizontal line */ { for ( i = x1. y1 = y2 .j=0 . int y1.2.j++ ) { buffer[j]=getpixel (x1. we can draw a line. i++. int y2 ) { int incdec. x2 = t . void bressline ( int x1. dy = y2 .fg) .y1).j=0 .j++) { buffer[j]=getpixel (i. putpixel ( x1.

drawline ( x1. y2. } /* 0 < m < 1 */ if ( dy < dx && dy > 0 ) { e_noinc = 2 * dy . y1. PREDY. e = 2 * dy . y1. e = 2 * dy . DECR ) . x2. } /* 1 < m < infinity */ if ( dy > dx && dy > 0 ) { e_noinc = 2 * dx . } return . e_inc = 2 * ( dy . y2. INCR ) . x2. e_inc = 2 * ( dy . DECR ) . PREDX. e_noinc = . drawline ( x1. INCR ) .dy ) .2 * ( dx . . y1. PREDX. e_inc = 2 * ( dy .dx .dx .dy . . } /* m = -1 */ if ( dy == -dx && dy < 0 ) { dy = -dy . y1. drawline ( x1. } INCR ) . drawline ( x1.( 2*dx ) . y1.18 putpixel ( i. y2. x2.dx . drawline ( x2.dx .dx ) . DECR ) . e_inc = 2 * ( dy . PREDY. } /* -1 > m > 0 */ if ( -dy > dx && dy < 0 ) { dx = -dx . x1. } /* 0 > m > -1 */ if ( -dy < dx && dy < 0 ) { dy = -dy . e = 2 * dy . y1. e_noinc = ( 2 * dy ) . e_inc = 2 * ( dx . y2.dx ) .dy . x2. y1.dx ) . y2. } /* m = 1 */ if ( dy == dx && dy > 0 ) { e_noinc = 2 * dy . x2.dy ) .dx ) . e_noinc = 2 * dy . drawline ( x1. fg ) . y2. e = 2 * dx . e = 2 * dy . e = 2 * dx e_inc = . PREDX. PREDX.

19 .

if(button==1) { hidemouseptr(). py=y.20 4. if(px!=x||py!=y) printmousepos().5. showmouseptr(). } } else { changemouseptr(hand).fg). void paint() { int px=1. break. if(x>=35&&x<=600&&y>=40&&y<=450) { changemouseptr(bottle).but.2. } px=x. setfillstyle(SOLID_FILL.y.bg). floodfill(x. Fill Area tool This is used to fill a closed area. while(1) { getmousepos().py=1. we fill the required area. } } . Using the flood fill algorithm.

BLUE (1). LIGHTMAGENTA (13). LIGHTGREEN (10). WHITE (15).2. MAGENTA (5). we draw the lines that are necessary to construct the polygon and use a simple for loop to join the lines.6. YELLOW (14). LIGHTBLUE (9). 4. LIGHTRED (12). DARKGRAY(8). GREEN(2). All these 16 colors are available in the directory “ bgi ”. The 16 colors and their corresponding integer values are given below: BLACK (0). BROWN(6). CYAN (3).3. . RED (4). Polygon tool: By using bresenhams line algorithm. LIGHTCYAN (11). Color tool bar ‘C’ graphics provides only 16 different colors shown in EGA directory. LIGHTGRAY (7).21 4.

2.click.2.1. 4. Status bar The status bar is placed at the bottom of the display screen to show the help options and also to show where the pointer is currently present. Mouse pointer The various mouse functions can be accessed by setting up the AX register with different values (service number) and issuing interrupt number 51. first the mouse pointer is kept hidden.When this function is called in main() it hides the mouse pointer. This function is useful while drawing figures. 4. if not it returns zero. setmouseptr() In this function AX is set to "4". .3.6. Scope for future work 1. We need to improve the algorithms. When this function is called in main() it displays the mouse pointer. It contains three parameters. hidemouseptr() In this function AX is set to "2". 4. The algorithms used in this project work bit a slow.6. 5. then the figure is been drawn and again the mouse pointer is been called. 4.5. The position of the pointer can be changed by using the mouse.4. Click is the integer variable which returns the values 1. 4. CX is been loaded by x co-ordinate of the mouse pointer and DX is been loaded with the y co-ordinate of the mouse pointer.ypos.4. This function returns the position of the mouse pointer.22 The colors with integer value >8 cannot be used as background color. The functions are listed below.3 corresponding to the button pressed on the mouse and 0 for buttons being not pressed. xpos and ypos returns the position of x co-ordinate and y co-ordinate respectively. getmouseptr() In this function AX is set to "3".6.they are xpos.6. This function sets the mouse pointer to specific position . initmouseptr() In this function AX is set to "1". 4. 4. We can select the color the background of the drawpad.6. Drawpad A draw pad is provided to draw the required image. If any key is pressed kbhit returns nonzero integer.

The algorithm if written in languages other than C can make us add more applications easily. Conclusion Finally we are able to produce an application that would allow us to draw images using several options provided and save them and retrieve them whenever necessary. The program can be made user friendly by adding more help options. 4.jpeg format. 3.bmp or . 6.23 2. 7. The display of the output can be improved. More applications can be added to the current applications. The program must be extended to save the file into . We may extend it to draw 3D images. 5. 6. Currently the images are saved in raw file format. .