Sunteți pe pagina 1din 98

Exp No : 1 Date :

BRESENHAMS ALGORITHM FOR LINE, CIRCLE AND ELLIPSE DRAWING

AIM : To write a C program to implement Bresenhams algorithm for line, circle and ellipse drawing.

ALGORITHM FOR MAIN PROGRAM: Step 1 : Start the program. Step 2 : Define the required variables and functions. Step 3 : Create the menu with the following options, (i) (ii) (iii) (iv) Line Circle Ellipse Exit

Step 4 : Enter the value of choice to select options. Step 5 : If choice is less than 3, then go to step 7. Else if choice greater than 3 then print invalid choice and goto step 10 Step 6 : If choice is equal to 0, then call the line() function. Step 7 : If choice is equal to 1, then call the circle() function. Step 8 : If choice is equal to 2, then call the ellipse() function. Step 9 : If choice equal to 3, then exit. and go to next step. Step 10 : Stop the program.

ALGORITHM FOR LINE() FUNCTION : Step 1 : Declare the variables x0, y0, dx, dy, 2dy, 2dx, xend, P. Step 2 : Read the two line endpoints and store the left end point in (x0, y0) and right end point in (x1,y1). Step 3 : Load (x0, y0) and plot the first point. Step 4 : Calculate the values of dx, dy, 2dy, 2dxdy as dx=abs(x0-x1) dy=abs(y0-y1)

2dy=2*dy 2dydx=2*(dy-dx) Step 5 : Calculate the decision parameter as, P=2dy-dx Step 6: If x0 is greater than x1, then initialize x=x1 y=y1 xend=x0 else, initialize x=x0 y=y0 xend=x1 Step 7 : While the value of x is less than xend, then increment the value of x Step 8 : If the value of p is less than 0, then P+ = 2dy. Otherwise increment the value of y and calculate the decision Parameter as, P+ = 2dydx. Step 9 : Display the plotted pixel. Step 10 : Repeat step 8 and step 9 until x is less than xend. Step 11 : Return to the main program.

ALGORITHM FOR CIRCLE () FUNCTION: Step 1 : Declare the variables x, y, r, p. Step 2 : Read the coordinate values of (x,y) and the radius of circle. Step 3 : Initialize x=0 and y=radius and calculate the decision parameter P using, P = 1-r. Step 4 : While x < y, increment the value of x and if P < 0, then calculate the decision parameter as, P+ = 2*x+1,Otherwise, decrement the value of y and calculate the decision parameter as, P+ = 2*(x-y)+1. Step 5 : Display the pixels drawn using circle plot points (x, y, r). Step 6 : Repeat the step 4 and step 5 until x < y. Step 7 : Return to the main program.

ALGORITHM FOR ELLIPSE() FUNCTION : Step 1 : Declare the variables x, y, P, px, py, Rx2, Ry2, twoRx2, twoRy2. Step 2 : Read the coordinate values of (x,y). Step 3 : Read the starting and ending angles and the radius(x,y) of the ellipse to be drawn. Step 4: Display the plotted points. Step 5: Return to main program.

FLOW CHART FOR MAIN PROGRAM :

Start Declare the variables and functions

Enter the value of choice, using the variable choice

If (choice < 3)
0. LINE 1. CIRCLE Yes

No

2. ELLIPSE

NO

If (choice >=3) If (choice ==0) If (choice ==1) If (choice ==2)


YES

Print Invalid choice

Call line() function A

Call circle() function B

Call ellipse() function Stop C

FLOW CHART FOR LINE() FUNCTION :

Declare the variables x0,x1,y0,y1,dx,dy,p, xend

Read the coordinates of (x0,y0)(x1,y1)

dx=abs(x0-x1) dy=abs(y0-y1) 2dy=2*dy 2dydx=2*(dy-dx) P = 2*dy-dx

Yes

If(x0>x1)

No

x=x1 y=y1 xend=x0

x=x0 y=y0 xend=x1

While (x<xend)
TRUE

FALSE

X++
NO

If(p<0)
YES

P+ = 2dy

Y++ P+ = 2dydx

Display the plotted pixels

Return to main function FLOW CHART FOR CIRCLE() FUNCTION : B

Declare the variables x,y,r,p Read the values of coordinate values of x,y & radius of circle x=0; y=r P = 1-r
E

While (x<y)
YES

NO

X++
YES

If(p<0)
NO

P+ = 2*X+1

Y-P+ = 2*(X-Y)+1 Display the plotted pixels

Return to main function

FLOWCHART FOR ELLIPSE FUNCTION :

Declare the variables x, y, px, py, Rx2, Ry2, P, twoRx2, twoRy2

Read the coordinate values of (x,y)

Read starting and ending angles and radius of (x,y) Display ploted points

Return to Main Program

PROGRAM: #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> #include<stdlib.h> void line(); void circle(); void plotpoints(int,int,int,int); void ellipse(); void main() { char choice,choice1; int gd=DETECT,gm;

BRESENHAM'S ALGORITHM

initgraph(&gd,&gm,"d:\\cpp\\bgi"); cleardevice(); printf("\nBRESHAM'S ALGORITHM"); do { printf("\n0.LINE"); printf("\n2.ELLIPSE"); printf("\n3.EXIT"); printf("\n\tENTER UR CHOICE : "); scanf("%s",&choice); if(choice>'3') { printf("\n Invalid Choice......!"); } switch(choice) { case '0': line(); break;

case '1': circle(); break;

case '2': ellipse(); break; case '3': exit(1); } printf("\nDo u want to continue(Y/N):"); scanf("%s",&choice1); } while(choice1=='y'||choice1=='Y'); getch(); }

void line() { int xa,ya,xb,yb,dx,dy,x,y,xend,p; printf("LINE DRAWING:"); printf("\n Enter the starting points of x,y:"); scanf("%d%d",&xa,&ya); printf("\n Enter the ending points of x,y:"); scanf("%d%d",&xb,&yb); dx=abs(xa-xb); dy=abs(ya-yb); p=2*dy-dx; if(xa>xb) { x=xb; y=yb; xend=xa; } else { x=xa; y=yb; xend=xb; } putpixel(x,y,15);

while(x<xend) { x=x+1; if(p<0) p=p+2*dy; else { y=y+1; p=p+2*(dy-dx); } putpixel(x,y,15); } }

void circle() { int xc1,yc1,p,x,y,xc,yc,r; printf("CIRCLE DRAWING:"); printf("\nEnter the x and y coordinates:"); scanf("%d%d",&xc,&yc); printf("\nEnter the radius of the circle:"); scanf("%d",&r); x=0; y=r; p=1-r; while(x<y) { plotpoints(xc,yc,x,y); if(p<0) p=p+(4*x)+6; else { p=p+4*(x-y)+10; y=y-1; }

x=x+1; } plotpoints(xc,yc,x,y); getch(); }

void plotpoints(int xc,int yc,int x,int y) { putpixel(xc+x,yc+y,15); putpixel(xc-x,yc+y,15); putpixel(xc+x,yc-y,15); putpixel(xc-x,yc-y,15); putpixel(xc+y,yc+x,15); putpixel(xc-y,yc+x,15); putpixel(xc+y,yc-x,15); putpixel(xc-y,yc-x,15); }

void ellipse() { int x1,y1,xc,yc,xr,yr,i; printf("\nELLIPSE DRAWING:"); printf("\nEnter the x and y coordinates:"); scanf("%d%d",&xc,&yc); printf("\nEnter the x and y radius:"); scanf("%d%d",&xr,&yr); for(i=0;i<=360;i++) { x1=xc+xr*cos(i); y1=yc+yr*sin(i); putpixel(x1,y1,15); } getch(); }

OUTPUT : BRESENHAM S ALGORITHM: 0. LINE 1. CIRCLE 2. ELLIPSE 3. EXIT

Enter ur choice : 0

LINE DRAWING : Enter the xa coordinate value : 100 Enter the ya coordinate value : 100 Enter the xb coordinate value : 50 Enter the yb coordinate value : 50

Do u want to continue(y/n) : Y

0. LINE 1. CIRCLE 2. ELLIPSE 3. EXIT

Enter ur choice : 1

CIRCLE DRAWING : Enter the x coordinate value : 100 Enter the y coordinate value : 100

Enter the radius : 50

Do u want to continue(y/n) : Y

0. LINE 1. CIRCLE 2. ELLIPSE 3. EXIT

Enter ur choice : 2

ELLIPSE DRAWING : Enter the values of x and y : 100,100 Enter the starting and ending angles : 0,360 Enter the radius of x and y : 50,25

Do u want to continue(y/n) : Y

0. LINE 1. CIRCLE 2. ELLIPSE 3. EXIT

Enter ur choice : 4 INVALID CHOICE

Do u want to continue(y/n) : Y 0. LINE 1. CIRCLE 2. ELLIPSE 3. EXIT

Enter ur choice : 3

RESULT : Thus the C program to implement Bresenhams algorithm for line, circle and ellipse drawing is completed successfully and the output is verified..

Exp No : 02 Date :

TWO DIMENSIONAL TRANSFORMATION

AIM: To write a C program to perform Translation, Rotation, Scaling and Reflection using 2D transformation.

ALGORITHM FOR MAIN PROGRAM: Step 1 : Start the program. Step 2 : Declare the variables and function. Step 3 : Create the menu with options as, (i) (ii) (iii) (iv) (v) Translation. Rotation. Scaling. Reflection. Exit.

Step 4 : Enter the value of the choice from the menu. Step 5 : If choice is lesser then 5, then go to step 6. Else if choice is equal to or greater then 5 print invalid choice and go to step 11. Step 6 : If choice is equal to 0, then call the translation function. Step 7 : If choice is equal to 1, then call the rotation function. Step 8 : If choice is equal to 2, then call the Scaling function. Step 9 : If choice is equal to 3, then call the Reflection function. Step 10 : Stop the program.

ALGORITHM FOR TRANSLATION () FUNCTION: Step 1 : Declare the variables x0,y0,x1,y1,tx,ty. Step 2 : Read the coordinates of (x,y). Step 3 : Read the translation factors tx, ty. Step 4 : Display the object before translation. Step 5 : Calculate the values of x0, y0, x1 and y1 as x0+=tx, y0+=ty, x1+=tx and y1+=ty.

Step 6 : Display the translated object. Step 7 : Return to the main program.

ALGORITHM FOR ROTATION () FUNCTION: Step 1 : Declare the variables. Step 2 : Read the coordinates of (x,y). Step 3 : Display the object before rotation. Step 4 : Read the values of the angle to be rotated. Step 5 : Display the rotated object. Step 6 : Return to main function.

ALGORITHM FOR SCALING () FUNCTION: Step 1 : Declare the variables. Step 2 : Read the coordinates of (x,y) and values of d1 and d2. Step 3 : Read the scaling factors sx,sy. Step 4 : Display the object before scaling. Step 5 : Calculate the values of x0, y0, x1 and y1 as x0 = abs( x2 x0 ) x1 = x1 + ( x1 * sx ) y0 = abs( y1 y0 ) y1 = y0 + ( y0 * sy ) Step 6 : Display the object after scaling. Step 7 : Return to the main program.

ALGORITHM FOR REFLECTION() FUNCTION : Step 1 : Define the variables and functions. Step 2 : Create the menu with the options as, a. b. c. Reflection about x axis Reflection about y axis Exit

Step 3 : Enter the value of choice. Step 4 : If choice is equal to a, then call the xaxis() function. Step 5 : If choice is equal to b, then call the yaxis() function. Step 6 : If choice is equal to d, print invalid choice and go to next step. Step 7 : Return to the main program.

ALGORITHM FOR XAXIS() FUNCTION : Step 1 : Declare the variables. Step 2 : Read the vertices of the triangle. Step 3 : Display the rotated object along the x axis. Step 4 : Return to the Reflection() function.

ALGORITHM FOR YAXIS() FUNCTION : Step 1 : Declare the variables. Step 2 : Read the vertices of the triangle. Step 3 : Display the rotated object along the y axis. Step 4 : Return to the Reflection() function.

FLOW CHART FOR MAIN PROGRAM:

Start

Read the variable choice and functions

Enter the value of choice using the variable, choice

If (choice<=4) 0. Translation 1. Rotation 2. Scaling

NO

3. Reflection
YES

4. Exit

If (choice ==0)

If (choice ==1)

If (choice ==2)

If (choice ==3)

If (choice ==4)

Call translation () function

Call rotate () function

Call scaling() function

Call reflection() function

Exit
N O

If (choice >=5)
YES

Print Invalid choice

Stop

FLOW CHART FOR TRANSLATION() FUNCTION :

Declare the variables

Read the coordinates of (x,y) Read the translation factors tx, ty Display the object before translation

X0+=tx, Y0+=ty X1+=tx, Y1+=ty Display the object after translation

Return to main program

FLOW CHART FOR ROTATION() FUNCTION :

Declare the variables

Read the coordinates of (x,y)

Display the object before rotation Read the angles to be rotated

Display the rotated object

Return to main program

FLOWCHART FOR SCALING() FUNCTION :

Declare the variables

Read the coordinates of (x,y) & values of d1, d2

Read the translation factors sx, sy

Display the object before scaling

x0 = abs( x2 x0 ) x1 = x1 + ( x1 * sx ) y0 = abs( y1 y0 ) y1 = y0 + ( y0 * sy ) Display the object after scaling Return to main program

FLOW CHART FOR REFLECTION() FUNCTION :


D

Read the required variables and function

Enter the value of choice using the variable choice (a) XAXIS ( b) YAXIS (d) Exit

If (choice ==a)

If (choice ==b)

Print Invalid Choice

Stop
Call xaxis () function Call yaxis() function

FLOW CHART FOR XAXIS() FUNCTION :


F

Read the vertices of the triangle Display the rotated object along x axis

Return to reflection() function

FLOW CHART FOR YAXIS() FUNCTION :


G

Read the vertices of the triangle Display the rotated object along y axis Return to reflection() function

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<stdlib.h> void trans(); void scale(); void rot(); void ref(); void main() { char choice,choice1; int gd=DETECT,gm;

2D TRANSFORMATIONS

initgraph(&gd,&gm,"d:\\tc"); cleardevice(); printf("2D TRANSFORMATIONS:"); do { printf("\n0.TRANSLATION:"); printf("\n1.SCALING:"); printf("\n2.ROTATION:"); printf("\n3.REFLECTION:"); printf("\n4.EXIT:"); printf("\n\tENTER UR CHOICE:"); scanf("%s",&choice); if(choice>'4') { printf("\n Invalid Choice......!"); } switch(choice) { case '0': printf("TRANSLATION:");

trans(); break;

case '1': printf("SCALING:"); scale(); break;

case '2': printf("ROTATION:"); rot(); break;

case '3': printf("REFLECTION:"); ref(); break; case '4': exit(1); } printf("\nDo u want to continue(Y/N):"); scanf("%s",&choice1); } while(choice1=='y'||choice1=='Y'); getch(); }

void trans() { int x1,y1,x4,y4,tx,ty,i; settextstyle(7,0,5); outtextxy(20,10,"\nRECTANGLE TRANSLATION:"); getch(); printf("\nEnter the values of x1 and y1:"); scanf("%d%d",&x1,&y1); printf("\nEnter the values of x4 and y4");

scanf("%d%d",&x4,&y4); printf("\nEnter the translation factors tx,ty"); scanf("%d%d",&tx,&ty); getch(); cleardevice(); rectangle(0,0,639,479); settextstyle(7,0,3); outtextxy(20,20,"RECTANGLE BEFORE AND AFTER TRANSLATION"); rectangle(x1,y1,x4,y4); setlinestyle(1,1,1); x1+=tx; x4+=tx; y1+=ty; y4+=ty; setcolor(6); rectangle(x1,y1,x4,y4); getch(); }

void scale() { int x1,y1,x4,y4,sx,sy; settextstyle(7,0,5); outtextxy(20,10,"RECTANGLE SCALING"); getch(); printf("\n Enter the values of x1,y1:"); scanf("%d%d",&x1,&y1); printf("\n Enter the values of x4,y4:"); scanf("%d%d",&x4,&y4); printf("\n Enter Scaling Factors sx,sy:"); scanf("%d%d",&sx,&sy); getch(); cleardevice(); rectangle(0,0,639,479); settextstyle(7,0,3);

outtextxy(20,20,"Rectangle before and after scaling:"); rectangle(x1,y1,x4,y4); x1=abs(x4-x1); x4=x1+(x1*sx); y1=abs(y4-y1); y4=y1+(y1*sy); setlinestyle(1,1,1); setcolor(1); rectangle(x1,y1,x4,y4); getch(); }

void rot() { int x1,y1,x2,y2,x3,y3,x4,y4,r,xr,yr; int xa,ya,xb,yb,xc,yc,xd,yd; settextstyle(7,0,5); outtextxy(50,10,"RECTANGLE ROTATION"); getch(); printf("\nEnter the coordinate values of x1 and y1:"); scanf("%d%d",&x1,&y1); printf("\nEnter the coordinate values of x2 and y2:"); scanf("%d%d",&x2,&y2); printf("\nEnter the coordinate values of x3 and y3:"); scanf("%d%d",&x3,&y3); printf("\nEnter the coordinate values of x4 and y4:"); scanf("%d%d",&x4,&y4); cleardevice(); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x4,y4,x1,y1); setcolor(5); putpixel(xr,yr,5); printf("\nEnter reference points xr and yr:");

scanf("%d%d",&xr,&yr); printf("\nEnter the angle value:"); scanf("%d",&r); outtextxy(20,20,"Rectangle before and after rotation"); r=r*(22/7)/180; xa=floor(xr+(x1-xr)*cos(r)-(y1-yr)*sin(r)); ya=floor(yr+(x1-xr)*sin(r)+(y1-yr)*cos(r)); xb=floor(xr+(x2-xr)*cos(r)-(y2-yr)*sin(r)); yb=floor(yr+(x2-xr)*sin(r)+(y2-yr)*cos(r)); xc=floor(xr+(x3-xr)*cos(r)-(y3-yr)*sin(r)); yc=floor(yr+(x3-xr)*sin(r)+(y3-yr)*cos(r)); xd=floor(yr+(x4-xr)*cos(r)-(y4-yr)*sin(r)); yd=floor(yr+(x4-xr)*sin(r)+(y4-yr)*cos(r)); setcolor(2); line(xa,ya,xb,yb); line(xb,yb,xc,yc); line(xc,yc,xd,yd); line(xd,yd,xa,ya); getch(); }

void ref() { int xa,ya,xb,yb,xa1,ya1,xb1,yb1,a,b,c,d; char choice,choice1,z1,z; XX: settextstyle(7,0,5); outtextxy(50,10,"REFLECTION"); getch(); line(0,250,650,250); line(300,0,300,600); printf("\n Enter xa,ya:"); scanf("%d%d",&xa,&ya); printf("\n Enter xb,yb:"); scanf("%d%d",&xb,&yb);

line(xa,ya,xb,yb); printf("\n Reflection along x or y axis(X/Y):"); scanf("%s",&choice); z=choice; if(z=='x') { a=250-xa; b=250-xb; ya1=150+a; yb1=250+b; line(xa,ya1,xb,yb1); } else if(z=='y') { c=300-ya; d=300-yb; xa1=300+c; xb1=300+d; line(xa1,ya,xb1,yb); } printf("Still continue(y/n):\n"); scanf("%s",&choice1); z1=choice1; if(z1=='y') { clearviewport(); goto XX; } getch(); }

OUTPUT:

2D TRANSFORMATIONS
0. Translation 1. Rotation 2. Scaling 3. Reflection 4. Exit Enter the choice: 0 2D TRANSLATION : Enter the X0 coordinate: 100 Enter the Y0 coordinate: 200 Enter the X1 coordinate: 300 Enter the Y1 coordinate: 400 Enter the translation factors: Enter the tx value: 30 Enter the ty value: 40

BEFORE AND AFTER TRANSLATION

Do u want to continue(y/n) : Y

0. Translation 1. Rotation 2. Scaling 3. Reflection 4. Exit

Enter the choice: 1 2D ROTATION Enter the x1 coordinate: 100 Enter the y1 coordinate: 200 Enter the x2 coordinate: 300 Enter the y2 coordinate: 400 Enter the x3 coordinate: 100 Enter the y3 coordinate: 200 Enter the x4 coordinate: 300 Enter the y4 coordinate: 400 Enter the reference points : 150 175 Enter the angle : 45 RECTANGLE BEFORE AND AFTER ROTATION

Do u want to continue(y/n) : Y

0. Translation 1. Rotation 2. Scaling 3. Reflection 4. Exit

Enter the choice: 2

2D SCALING Enter the x1 coordinate : 100 Enter the y1 coordinate : 100 Enter the x2 coordinate : 200 Enter the y2 coordinate : 200 Enter the scaling factors : Enter the sx value : 0.5 Enter the sy value : 0.5 RECTANGLE BEFORE AND AFTER SCALING

Do u want to continue(y/n) : Y Enter the choice: 3 2D REFLECTION (a) XAXIS. (b) YAXIS. Enter the choice : a REFLECTION ABOUT XAXIS Enter the vertices of the triangle : 50, 50, 50, 150 ,150, 150
y

Do you want to continue(y/n): Y

Enter the choice : b REFLECTION ABOUT YAXIS Enter the vertices of the triangle : 50, 50, 50, 150 ,150, 150
y

Still continue(y/n): N

Do you want to continue (y/n) Y Enter the choice : 5 INVALID CHOICE Do you want to continue (y/n) Y 0. Translation 1. Rotation 2. Scaling 3. Reflection 4. Exit Enter the choice : 4

RESULT:
Thus the C program to perform Translation, Rotation, Scaling and Reflection using 2D transformations is executed successfully and the output is verified.

Exp No : 3

COHEN-SUTHERLAND 2D CLIPPING AND WINDOW - VIEWPORT MAPPING

AIM : To write a C program for implementation of Cohen Sutherland 2D clipping.

ALGORITHM : Step 1 : Start the program. Step 2 : Declare the required variables and functions. Step 3 : Read the input values for x and y coordinates. Sept 4 : Display the object before clipping Step 5 : Calculate the slope of line by, m = ( y2 y 1 )/( x2 x1 ). Step 6 : Calculate the y coordinate intersection point by, y = y1 + m( x x1 ). Step 7 : Calculate the x coordinate intersection point by, x = x1 + ( y y1 )/m. Step 8 : Display the resulting line clipping of x and y coordinate values. Step 9 : Stop the program.

FLOWCHART FOR MAIN PROGRAM :

Start

Declare the variables

Read the coordinate values of (xmin,ymin),(xmax,ymax),(x1,y1),(x2,y2) Display the object before clipping

m = ( y2 y 1 )/( x2 x1 )

y = y1 + m( x x1 )

x = x1 + ( y y1 )/m.

Stop

PROGRAM : #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> int m,x1,x2,y1,y2,i,xmin,xmax,ymin,ymax,x,y,a,b; int acc,rej,p[5],q[5],r[10]; void main() { int j,don,dis; int gd=DETECT,gm; initgraph(&gd,&gm,"d:\\cpp\\bgi"); settextstyle(7,0,7); outtextxy(50,5,"Line Clipping"); printf("\n\n\n\n\n\n"); printf("Enter the values of xmin and ymin:"); scanf("%d%d",&xmin,&ymin); printf("Enter the values of xmax and ymax:"); scanf("%d%d",&xmax,&ymax); printf("Enter the values of x1 and y1:"); scanf("%d%d",&x1,&y1); printf("Enter the values of x2 and y2:"); scanf("%d%d",&x2,&y2); getch(); cleardevice(); settextstyle(7,0,6); outtextxy(20,10,"Before Clipping:"); line(x1,y1,x2,y2); rectangle(xmin,ymin,xmax,ymax); getch(); cleardevice(); outtextxy(50,10,"After Clipping"); rectangle(xmin,ymin,xmax,ymax); don=0; dis=0; while(don==0) { x=x1; y=y1;

if(y>ymax) p[1]=1; else p[1]=0; if(y<ymin) p[2]=1; else p[2]=0; if(x>xmax) p[3]=1; else p[3]=0; if(x<xmin) p[4]=1; else p[4]=0; x=x2; if(y>ymax) q[1]=1; else q[1]=0; if(y<ymin) q[2]=1; else q[2]=0; if(x>xmax) q[3]=1; else q[3]=0; if(x<xmin) q[4]=1; else q[4]=0; acc=1; for(i=1;i<=4;i++) { if(p[i]==1||q[i]==1) y=y2;

acc=0; } if(acc==1) { don=1; dis=1; } else { rej=0; for(i=1;i<=4;i++) { if(p[i]==1&&q[i]==1) rej=1; } if(rej==1) don=1; else { if((x1>=xmin)&&(x1<=xmax)&&(y1<=ymax)&&(y1>=ymin)) { a=x1; b=y1; x1=x2; y1=y2; x2=a; y2=b; for(j=1;j<=4;j++) { r[j]=p[j]; p[j]=q[j]; q[j]=r[j]; } } if(x1!=x2) { m=(y2-y1)/(x2-x1); if(p[4]==1) { y1=y1+(xmin-x1)*m;

x1=xmin; } else if(p[3]==1) { y1=y1+(xmax-x1)*m; x1=xmax; } else if(p[2]==1) { x1=x1+(ymin-y1)/m; y1=ymin; } else if(p[1]==1) { x1=x1+(ymax-y1)/m; y1=ymax; } } } } } if (dis==1) { rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); } else { if(dis==0) rectangle(xmin,ymin,xmax,ymax); } getch(); }

OUTPUT : 2D CLIPPING :

Enter the coordinates (xmin,ymin) : 100, 200 Enter the coordinates (xmax,ymax) : 300, 400 Enter the coordinates (x1,y1) : 50, 100 Enter the coordinates (x2,y2) : 50, 100

OBJECT BEFORE CLIPPING :

OBJECT AFTER CLIPPING :

RESULT : Thus the C program to implement Cohen - Sutherland 2D Clipping was completed and executed successfully.

Exp No: 4

THREE DIMENSIONAL TRANSFORMATION

AIM: To write a C program to perform Translation, Rotation & Scaling using 3D transformation.

ALGORITHM FOR MAIN PROGRAM: Step 1 : Start the program. Step 2 : Declare the variables and function. Step 3 : Create the menu with options as, i) ii) iii) iv) Translation. Rotation. Scaling. Exit.

Step 4 : Enter the choice from the menu. Step 5 : If choice is less than or equal to 2, then goto step 6. Else if choice is greater than 2, then print invalid choice and goto step 9. Step 6 : If choice is equal to 0, then call the translation function. Step 7 : If choice is equal to 1, then call the rotation function. Step 8 : If choice is equal to 2, then call the Scaling function. Step 9 : Stop the program.

ALGORITHM FOR TRANSLATION () FUNCTION: Step 1 : Declare the variables. Step 2 : Read the coordinates of (x,y) and values of d1 and d2. Step 3 : Read the translation factors tx, ty. Step 4 : Display the object before translation. Step 5 : Calculate the values of x0, y0, x1 and y1 as x0+=tx, y0+=ty, x1+=tx and y1+=ty.

Step 6 : Display the translated object. Step 7 : Return to the main program.

ALGORITHM FOR ROTATION () FUNCTION: Step 1 : Declare the variables. Step 2 : Read the coordinates of (x,y) and values of d1 and d2. Step 3 : Display the object before rotation. Step 4 : Read the values of the angle to be rotated. Step 5 : Display the rotated object. Step 6 : Return to main function.

ALGORITHM FOR SCALING () FUNCTION: Step 1 : Declare the variables. Step 2 : Read the coordinates of (x,y) and values of d1 and d2. Step 3 : Read the scaling factors SX, SY. Step 4 : Display the object before scaling. Step 5 : Calculate the values of x0, y0, x1 and y1 as x0*=sx, y0*=sy, x1*=sx, y1*=sy. Step 6 : Display the object after scaling. Step 7 : Return to the main program.

FLOWCHART FOR MAIN PROGRAM:

Start

Read the required variables and function

Enter the value of choice using the variable choice


NO NO

If (choice<=2)
YES

If (choice>2)

0. Translation

1. Rotation

2. Scaling

YES

If (choice ==0

If (choice ==1)

If (choice ==2)

Print Invalid choice

Call translation () function

Call rotate () function

Call scaling() function

Stop

FLOWCHART FOR TRANSLATION() FUNCTION :

Declare the variables

Read the coordinates of (x,y) & values of d1, d2 Read the translation factors tx, ty Display the object before translation X0+=tx, Y0+=ty X1+=tx, Y1+=ty Display the object after translation Return to main program

FLOWCHART FOR ROTATION () FUNCTION:

Declare the variables

Read the coordinates of (x,y)

Display the object before rotation Read the angles to be rotated

Display the rotated object

Return to main program

FLOWCHART FOR SCALING () FUNCTION:


C

Declare the variables

Read the coordinates of (x,y) & values of d1, d2

Read the translation factors sx, sy Display the object before scaling X0+=sx, Y0+=sy X1+=sx, Y1+=sy Display the object after scaling

Return to main program

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<process.h> void trans(); void scale(); void rot(); void main() { char chr,chr1; int gd=DETECT,gm; initgraph(&gd,&gm,""); cleardevice; settextstyle(7,0,5); outtextxy(20,30,"3D TRANSFORMATIONS"); rectangle(125,350,400,150); settextstyle(7,0,3); do { outtextxy(150,165,"0.TRANSLATION"); outtextxy(150,205,"1.SCALING"); outtextxy(150,245,"2.ROTATION"); outtextxy(150,285,"3.EXIT"); outtextxy(150,400,"Enter ur choice:"); chr=getch(); if(chr>'3') { printf("\n Invalid Choice....."); } switch(chr) { case '0' : { outtextxy(435,400,"0"); getch(); trans(); break; } case '1' : { outtextxy(435,400,"1"); getch(); scale(); break; } case '2' : { outtextxy(435,400,"2"); getch(); rot();

break; } case '3' : { outtextxy(435,400,"3"); getch(); exit(1); break; } } printf("\n Do U Want 2 Continue..?/(y/n):"); scanf("%s",&chr1); } while(chr1=='y'||chr1=='Y'); getch(); } void trans() { int x0,y0,x1,y1,d1,d2,tx,ty; int gd=DETECT,gm; initgraph(&gd,&gm,""); cleardevice; settextstyle(7,0,5); outtextxy(50,10,"3D-TRANSLATION:"); getch(); printf("\n\n\n\n\n"); printf("\nEnter the values of x0 and y0:"); scanf("%d%d",&x0,&y0); printf("\nEnter the values of x1 and y1:"); scanf("%d%d",&x1,&y1); printf("\nEnter the depth values d1 and d2:"); scanf("%d%d",&d1,&d2); printf("\nEnter the translation fractors tx,ty:"); scanf("%d%d",&tx,&ty); getch(); cleardevice(); rectangle(0,0,639,479); settextstyle(7,0,3); outtextxy(20,20,"3D-Bar b4 and after translation"); bar3d(x0,y0,x1,y1,d1,d2); setlinestyle(1,1,1); x0+=tx; x1+=tx; y0+=ty; y1+=ty; setcolor(6); bar3d(x0,y0,x1,y1,d1,d2); getch(); } void scale() {

int x0,y0,x1,y1,d1,d2,sx,sy; int gd=DETECT,gm; initgraph(&gd,&gm,""); cleardevice(); settextstyle(7,0,6); outtextxy(50,5,"3D-SCALING"); printf("\n\n\n\n\n\n"); printf("\nEnter Values of x0 and y0:"); scanf("%d%d",&x0,&y0); printf("\nEnter Values of x1 and y1:"); scanf("%d%d",&x1,&y1); printf("\nEnter Values of d1 and d2:"); scanf("%d%d",&d1,&d2); printf("\nEnter the scaling Factors sx and sy:"); scanf("%d%d",&sx,&sy); getch(); cleardevice(); rectangle(0,0,639,479); settextstyle(7,0,3); outtextxy(20,20,"3D-Bar b4 and after scaling"); bar3d(x0,y0,x1,y1,d1,d2); setlinestyle(1,1,1); x0*=sx; y0*=sy; x1*=sx; y1*=sy; setcolor(6); bar3d(x0,y0,x1,y1,d1,d2); getch(); } void rot() { int x0,y0,x1,y1,d1,d2,r,xa,ya,xb,yb; int gd=DETECT,gm; initgraph(&gd,&gm,""); cleardevice(); settextstyle(7,0,6); outtextxy(50,5,"3D-ROTATION"); printf("\n\n\n\n\n"); printf("\nEnter Values of x0 and y0:"); scanf("%d%d",&x0,&y0); printf("\nEnter Values of x1 and y1:"); scanf("%d%d",&x1,&y1); printf("\nEnter Values of d1 and d2:"); scanf("%d%d",&d1,&d2); printf("\nEnter the angle value r:"); scanf("%d",&r); getch(); cleardevice(); rectangle(0,0,639,479); settextstyle(7,0,3);

outtextxy(20,20,"3D-Bar b4 and after rotation"); bar3d(x0,y0,x1,y1,d1,d2); setlinestyle(1,1,1); getch(); xa=x0*cos(r)-y0*sin(r); ya=y0*cos(r)+x0*sin(r); xb=x1*cos(r)-y1*sin(r); yb=y1*cos(r)+x1*sin(r); setcolor(6); bar3d(xa,ya,xb,yb,d1,d2); getch(); }

OUTPUT:

3D TRANSFORMATIONS
0. TRANSLATION 1. SCALING 2. ROTATION 3. EXIT Enter the choice: 0 3D TRANSLATION Enter the X0 coordinate: 100 Enter the Y0 coordinate: 200 Enter the X1 coordinate: 300 Enter the Y1 coordinate: 400 Enter the d1 value: 50 Enter the d2 value: 75 Enter the translation factors: Enter the tx value: 30 Enter the ty value: 40 3D BAR BEFORE AND AFTER TRANSLATION

Do u want to continue(y/n) : Y Enter the choice: 1

3D SCALING Enter the X0 coordinate: 75 Enter the Y0 coordinate: 125 Enter the X1 coordinate: 175 Enter the Y1 coordinate: 190 Enter the d1 value: 50 Enter the d2 value: 75 Enter the scaling factors: Enter the sx value: 2.0 Enter the sy value: 2.0

3D BAR BFORE AND AFTER SCALING

Do u want to continue(y/n) : Y Enter the choice: 2 3D ROTATION Enter the X0 coordinate: 100 Enter the Y0 coordinate: 200 Enter the X1 coordinate: 300 Enter the Y1 coordinate: 400 Enter the d1 value: 50 Enter the d2 value: 75 Enter the angle value: 25

3D BAR BEFORE AND AFTER ROTATION

Do u want to continue(y/n) : Y Enter ur choice : 3 INVALID CHOICE

Do u want to continue(y/n) : Y Enter ur choice : 4 Exit.

RESULT: Thus the C program to perform Translation, Rotation & Scaling using 3D transformations was completed and executed successfully.

Exp No : 5 Date :

VISUALIZE PROJECTIONS OF 3D IMAGES

AIM : To write a C program to visualize projections of 3D images. drawing.

ALGORITHM: Step 1 : Start the program. Step 2 : Read the coordinates of x and y to draw the 3D cube. Step 3 : Create the menu with the options as, (i) (ii) (iii) Step 4 : Enter the value of choice. Step 5 : If choice is less than or equal to 1, then goto step 6. Else if choice is greater than 2, then print Invalid Choice and goto step 8. Step 6 : If choice is equal to 0, then Display the 3D cube that is generated already and select different positions to view the top, bottom, front and side view of the object in 2D. Step 7 : If choice is equal to1, then Display the object along the converging paths. Step 8 : Stop the program. Parallel projection of the 3D cube. Perspective projection of the 3D cube. Exit.

FLOWCHART:

Start

Read the coordinates of (x,y) of the cube Enter the value of choice using the variable choice
NO NO YES

If (choice<=1)
0. Parallel projection YES

If (choice>1)
1. Prespective projection

If(choice==0)

If (choice==1)

Display the object in 3D and select the viewing positions

Display the object along the converging paths

Print Invalid choice Stop

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<process.h> void parallel(); void perspective(); void main() { char chr,chr1; int gd=DETECT,gm; initgraph(&gd,&gm,"d:\\tc"); cleardevice; settextstyle(7,0,5); outtextxy(20,30,"3D PROJECTIONS"); settextstyle(7,0,3); do { printf("\n\n\n\n"); printf("\n0.PARALLEL PROJECTION"); printf("\n1.PERSPECTIVE PROJECTION"); printf("\n2.EXIT"); printf("\nEnter ur choice:"); scanf("%s",&chr); if(chr>'2') { printf("\n Invalid Choice....."); } switch(chr) { case '0' : {

parallel(); break; } case '1' : { perspective(); break; } case '2' : { exit(1); break; } } printf("\n Do U Want 2 Continue..?/(y/n):"); scanf("%s",&chr1); } while(chr1=='y'||chr1=='Y'); getch(); }

void parallel() { char chr2,chr3; int x,y,x0,y0,x1,y1,d1,d2,x2,y2,x3,y3,x4,y4, x11,y11,x22,y22,x33,y33,x44,y44,x111, y111,x222,y222,x333,y333,x444,y444; cleardevice; settextstyle(7,0,5); outtextxy(50,10,"3D OBJECT INPUT:"); printf("\n\n\n\nEnter the inputs for a 3D cube"); getch(); printf("\n\n\n"); printf("\nEnter the values of x and y:"); scanf("%d%d",&x,&y); printf("\nEnter the values of x0 and y0:");

scanf("%d%d",&x0,&y0); printf("\nEnter the depth values d1 and d2:"); scanf("%d%d",&d1,&d2); getch(); cleardevice(); rectangle(0,0,639,479); settextstyle(7,0,3); bar3d(x,y,x0,y0,d1,d2); do { printf("\n\n\n0.Top and Bottom View"); printf("\n1.Left and Right view"); printf("\n2.Front and Back view"); printf("\nEnter ur choice:"); scanf("%s",&chr2); switch(chr2) { case '0' : { printf("\nTop and Bottom view"); x1=100;y1=100;x2=320;y2=100;x3=270;y3=200;x4=30;y4=200; line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x4,y4,x1,y1); break; } case '1' : { printf("\nLeft and Right view"); x11=200;y11=100;x22=100;y22=200;x33=100;y33=400;x44=200;y44=300; line(x11,y11,x22,y22); line(x22,y22,x33,y33); line(x33,y33,x44,y44); line(x44,y44,x11,y11);

break; } case '2' : { printf("\nFront and Back view"); x111=50;y111=100;x222=300;y222=300;//x333=300;y333=500;x444=100;y444=5 00;//x5=100;y5=100; rectangle(x111,y111,x222,y222); break; } } printf("\n Do U Want 2 Continue in parallel proj..?/(y/n):"); scanf("%s",&chr3); } while(chr3=='y'||chr3=='Y'); getch(); }

void perspective() { int x0,y0,x1,y1,d1,d2,d11,d22,x11,y11,x22,y22; cleardevice; settextstyle(7,0,5); outtextxy(50,10,"3D OBJECT INPUT:"); printf("\n\n\n\n\nEnter the dimensions for the cube:"); getch(); printf("\n\n\n"); printf("\nEnter the values of x0 and y0:"); scanf("%d%d",&x0,&y0); printf("\nEnter the values of x1 and y1:"); scanf("%d%d",&x1,&y1); printf("\nEnter the depth values d1 and d2:"); scanf("%d%d",&d1,&d2); getch(); cleardevice();

rectangle(0,0,639,479); settextstyle(7,0,3); outtextxy(20,20,"3D-Bar after perspective projection"); bar3d(x0,y0,x1,y1,d1,d2); x11=x0+100;y11=y0+200;x22=x1+100;y22=y1+200,d11=d1+20,d22=d2+20; bar3d(x11,y11,x22,y22,d11,d22); getch(); }

OUTPUT:

PROJECTIONS OF 3D IMAGES : 0. PARALLEL PROJECTION 1. PRESPECTIVE PROJECTION 2. EXIT Enter the choice : 0

3D OBJECT INPUT:

Enter the input for cube: Enter the coordinate value(x,y) Enter the coordinate value(x0,y0) Enter the depth value : : : 100,125 150,175 30,40

PARALLEL PROJECTION OF 3D CUBE

0. Top and bottom view 1. Left and right view 2. Front and Back view Enetr your choice: 0

TOP AND BOTTOM VIEW

Do you want to continue(y/n): y 0. Top and bottom view 1. Left and right view 2. Front and Back view

Enetr your choice: 1 LEFT AND RIGHT VIEW

Do you want to continue(y/n):y 0. Top and bottom view 1. Left and right view 2. Front and Back view Enetr your choice: 2

FRONT AND BACK VIEW

Do you want to continue(y/n): n

PROJECTIONS OF 3D IMAGES : 0. PARALLEL PROJECTION 1. PRESPECTIVE PROJECTION

2. EXIT Enter the choice: 1

Enter the coordinate value(x0,y0) : Enter the coordinate value(x1,y1): Enter the depth value : 60,80

100,125 150,175

Do you want to continue(y/n) : Y Enter the choice : 3 INVALID CHOICE

RESULT:

Thus the C program to visualize projection of 3D images was completed and executed successfully.

Exp No: 6 Date:

CONVERSIONS BETWEEN COLOR MODELS

AIM: To write a c program to convert RGB color models to HSV color models and HSV color models to RGB color models.

ALGORITHM FOR MAPPING RGB VALUES TO HSV VALUES: Step 1 : Start the program. Step 2 : Read the required variables. Step 3 : If s is equal to 0, then initialize r=g=b=v and goto step 4. Else if h==1.0 goto step 5. Else, initialize h=0, h*=6.0, i=ffloor(h), aa=v*(1-s) bb=v*(1-(s*f)) cc=v*(1-(s*(1-f))) and goto step 4. Step 4 : Enter the value of choice. Step 5 : If choice is less than or equal to 5, then goto step 6. Else if choice is greater than 5, then goto step 12. Step 6 : If choice is equal to 0, then initialize *r=v, *g=cc, *b=aa and goto step 12. Step 7 : If choice is equal to 1, then initialize *r=bb, *g=v, *b=aa and goto step 12.. Step 8: If choice is equal to 2, then initialize *r=aa, *g=v, *b=cc and goto step 12.. Step 9 : If choice is equal to 3, then initialize *r=aa, *g=bb, *b=v and goto step 12.. Step 10 : If choice is equal to 4, then initialize *r=cc, *g=aa, *b=v and

goto step 12.. Step 11: If choice is equal to 5, then initialize *r=v, *g=aa, *b=bb and goto step 12.. Step 12 : Stop the program.

ALGORITHM FOR TRANSFORMATION FROM HSV TO RGB: Step 1 : Start the program. Step 2 : Read the required values. Step 3 : Declare the variables max, min, delta. Step 4 : If max is not equal to 0.0, then calculate the value of sas, *s=delta/max and goto step 5.

Otherwise initialize s is equal to 0.0 and goto step 5. Step 5 : If s is equal to 0.0 then, initialize h is equal to No hue and goto step 6. Else goto step 6. Step 6 : If r is equal to max, then calculate h as, *h=(g-b)/delta and goto step 7. Else goto step 7. Step 7 : If g is equal to max, then calculate h as, *h=2+(b-r)/delta and goto step 8. Else goto step 8. Step 8 : If b is equal to max, then calculate h as, *h=4+(r-g)/delta and goto step 9. Otherwise calculate h as, *h=h*6.0 and goto step 9. Step 9 : If *h is less than 0, then calculate, *h+=360.0 *h/=360.0 and goto step 8. Else goto step 10. Step10 : Stop the program.

FLOWCHART FOR MAPPING RGB VALUES TO HSV VALUES:

Start

Read the required variables

Yes

If (s==0)
No

*r=*g=*b=v

If (h==1.0)
Yes

h=0, h*=6.0 i=ffloor(h), f=h-i aa=v*(1-s) bb=v*(1-(s-f)) cc=v*(1-(s*(1-f))) A

A
NO NO YES

If ch<=5
0 1 2 YES 3 4

If ch>5
5

If ch==0

If ch==1

If ch==2

If ch==3

If ch==4

If ch==5

*r=v *g=cc *b=aa

*r=bb *g=v *b=aa

*r=aa *g=v *b=cc

*r=aa *g=bb *b=v

*r=cc *g=aa *b=v

*r=v *g=aa *b=bb

Stop

FLOWCHART FOR TRANSFORMATION FROM HSV TO RGB:

Start

Read the required variables

max=MAX(r,MAX(g,b)) min=MIN(r,MIN(g,b)) delta=max-min If max!=0.0 Yes S=delta/max Yes If (s=0.0) No Yes If (r==max) h=(g-b)/delta h=no hue No *s=0.0

No Yes If (g==max) h=2+(b-r)/delta

No A

Yes If (b==max) h=4+(r-g)/delta

No h=h*60.0 Yes If (h<0) No Stop *h+=360.0 *h/=360.0

PROGRAM: #include<stdlib.h> #include<stdio.h> #define MIN(a,b) (a<b?a:b) #define MAX(a,b) (a<b?a:b) #include<conio.h> #define NO_HUE -1 void RGBtoHSV( float r, float g, float b, float *h, float *s, float *v ) { float min, max, delta; min = MIN(r,MIN(g,b)); max = MAX(r,MAX(g,b)); *v = max; delta = max - min; if( max != 0.0 ) *s = delta / max; else { if(*s==0.0) { *h = -1;} else { if( r == max ) *h = ( g - b ) / delta; else if( g == max ) *h = 2 + ( b - r ) / delta; else if( b == max ) *s = 0.0; }

*h = 4 + ( r - g ) / delta; *h *= 60.0; if( *h < 0 ) *h += 360.0; *h/=360.0; } }

void HSVtoRGB( float *r, float *g, float *b, float h, float s, float v ) { int i; if( s == 0 ) { *r = *g = *b = v; else{if(h==1.0) {h=0.; h *= 6.0; i = floor( h ); f = h - i; p = v * ( 1 - s ); q = v * ( 1 - s * f ); t = v * ( 1 - s * ( 1 - f ) ); switch( i ) { case 0: *r = v;*g = t;*b = p; break; case 1: *r = q;*g = v;*b = p; break; case 2: *r = p;*g = v;*b = t; break; case 3: *r = p;*g = q;*b = v; break; case 4: *r = t;*g = p;*b = v; break; default:*r = v;*g = p;*b = q; break; } } } } void main() } float f, p, q, t;

{ float *r,*g,*b,*h,*s,*v; clrscr(); RGBtoHSV(*r,*g,*b,h,s,v); HSVtoRGB(h,s,v,*r,*g,*b); getch(); }

OUTPUT:

RESULT: Thus the C program to convert RGB color models to HSV color models and HSV color models to RGB color models was completed and executed successfully.

EXPT NO 7: Date:

TEXT COMPRESSION AND DECOMPRESSION

AIM : To write a C program to implement the text compression and decompression algorithm.

ALGORITHM FOR COMPRESSION: Step 1 : Start the program. Step 2 : Read the text and the text size of the original text. Step 3 : Determine the frequency of occurrence of various characters. Step 4 : Find out the character which has the maximum frequency and the next maximum frequency. Step 5 : Compare these characters with the input text and determine their positions. Step 6 : Now set the bit relative to the position of these characters in a single byte character. Step 7 : Write this byte into the compressed text along with the characters. Step 8 : Repeat the steps 5 to 7 until all the occurrences of these characters are compressed. Step 9 : Repeat steps 5 to 8 with characters having next highest frequencies. Step 10: Display the compressed text. Step 11: Stop the program.

ALGORITHM FOR DECOMPRESSION:

Step 1 : Start the program. Step 2 : Read the text and the text size of the compressed text. Step 3 : Determine the frequency of occurrence of various characters. Step 4 : Find out the character which has the maximum frequency and the next maximum frequency. Step 5 : Compare these characters with the input text and determine their positions. Step 6 : Now set the bit relative to the position of these characters in a single byte character. Step 7 : Write this byte into the compressed text along with the characters. Step 8 : Repeat the steps 5 to 7 until all the occurrences of these characters are compressed. Step 9 : Repeat steps 5 to 8 with characters having next highest frequencies. Step 10: Display the decompressed text. Step 11: Stop the program.

FLOW CHART COMPRESSION:


Start

Read the input text file

Determine the frequency of occurrence of various characters

False

While(a[n]!=EOF)
True

N=0

False

While(a[n]==high)
True

N++

Compare the characters with input text and determine their bit relative positions

Display the bits along with the characters Determine the next highest frequency of

FLOW CHART DECOMPRESSION:

Start

Read the input text file

Determine the frequency of occurrence of various characters


False

While(a[n]!=EOF)
True

N=0

False

While(a[n]==high)
True

N++

Compare the characters with input text and determine their bit relative positions Display the bits along with the characters Determine the next highest frequency of the characters

PROGRAM: COMPRESSION #include<fstream.h> #include<process.h> #include<dos.h> #include<stdio.h> char ftc[40],cf[40]; struct dat { unsigned char a; long int size; int tabno; int code[17]; }; class comp { public: unsigned char r; dat data[256]; long int siz; int buf[8]; unsigned char w; void xtract() { fstream f1; f1.open(ftc,ios::in|ios::binary);

int re=0; if(f1==NULL) { cout<<"Cannot open input file.Exitting...\n"; exit(0); } while(ftc[re]!='.') { cf[re] =ftc[re]; re++; } cf[re]= '.'; cf[re+1] = 'c'; cf[re+2] = 'm'; cf[re+3] = 'm'; cf[re+4] = '\0'; dat a[256]; int sto[256];int m; siz=1; f1.read((char *)&r,1); a[int(r)].a=r; for(int i=0;i<256;i++) { a[i].size =0; } m =int(r); a[m].size= 1; int c=0; sto[c]= int(r); c++; while(!f1.eof()) { f1.read((char *)&r,1); m =int(r); if(a[m].size!=0) a[m].size++;

else { a[m].a = r; a[m].size=1; sto[c]=int(r); c++; siz++; } } for( i=0;i<siz;i++) { data[i]= a[sto[i]]; } q2sort(0,siz-1); f1.close(); }

void assign_table() { int row[10],element,notab,ctab; element=0;notab=0;ctab=0; for(int k=0;k<10;k++) row[k]=0; int j=0; for(int i=0;j<siz;i++) { if(element<row[ctab]+2) { data[j].tabno= ctab; element++; j++; } else { row[ctab]++;

if(ctab<notab) { ctab++; element=0; continue; } if(ctab==notab) { if(row[ctab]<=2) { ctab=0; element=0; } else { ctab++;notab++; element=0; } } } } fstream f2; f2.open(cf,ios::out|ios::binary); if(f2==NULL) { cout<<"Cannot open output file.Exitting...\n"; exit(0); } f2.write((char *)&siz,sizeof(long int)); for(i=0;i<siz;i++) { f2.write((char *)&data[i].a,1); } f2.close(); }

void code_gen() { int m=0;int n=0;int track1=1,track2=0,id;int rows[20]; int i; for(i=0;i<20;i++) { rows[i]=1; } while(m<siz) { i=0; for(int j= track1;j>=0&&m<siz;j--) { n=0;

for(int h=0;h<track2;h++) {data[m].code[n]=1; n++;data[m].code[n]=1; n++;} for(int k=0;k<i;k++) {data[m].code[n]=0;n++;} data[m].code[n]=1;n++; for(int l=0;l<j;l++) {data[m].code[n]=0;n++;} data[m].code[n]=1; n++; data[m].code[n]=2; i++; m++; if(data[m].tabno!=track2) { rows[track2]++;track2=data[m].tabno;j=rows[track2]+1;i=0;//track1=0; } } if(m<9) {rows[track2]++;} track1++; } } void code_subs()

{ dat temp; fstream f1,f2; q2sort1(0,siz-1); unsigned char w; int dbuf[8]; f1.open(ftc,ios::in|ios::binary); f2.open(cf,ios::app|ios::binary); int track; int track2=0; int track1;

f1.read((char *)&r,1); while(!f1.eof()) { track= bin_search(r); if(track==-1){cout<<"tatata";} for(int n=0;data[track].code[n]!=2;n++) { buf[track2]= data[track].code[n]; track2++; if(track2==8) { register int wr=0; for(register int bc=0; bc<8;bc++) { if(buf[bc]) wr= wr+( buf[bc]<<bc); } w = char(wr); f2.write((char*)&w,1); track2=0; } } f1.read((char *)&r,1); } if(track2!=0)

{ register int wr=0; for(int i=0;i<track2;i++) { if(buf[i]) wr = wr+(buf[i]<<i); } w = char(wr); f2.write((char *)&w,1); } f2.close(); f1.close(); }

int bin_search(char c) { int beg,mid,end; beg=0; end=siz; mid = siz/2; for(int i=0;i<siz&&data[mid].a!=c;i++) { if(data[mid].a>c) { end=mid; mid = (beg+end)/2; } else { beg=mid; mid = (beg+end)/2; } } if(data[mid].a==c) return(mid);

else return(-1); } void q2sort(int head,int tail) { int in,h,t;h=head;t=tail;int d; dat a; while(1) { while(data[head].size>=data[tail].size && head<tail) head++; while(data[tail].size<=data[head].size && tail>head)tail--; if(head>=tail)break; a=data[head];data[head]=data[tail];data[tail]=a; } d=t-h; if(d<1){return;} else if(d==1) { if(data[t].size>data[h].size) {a=data[h];data[h]=data[t];data[t]=a;} return; } q2sort(h,head-1); q2sort(tail,t); } void q2sort1(int head,int tail) { int in,h,t;h=head;t=tail;int d; dat a; while(1) { while(data[head].a<=data[tail].a && head<tail) head++; while(data[tail].a>=data[head].a && tail>head)tail--; if(head>=tail)break;

a=data[head];data[head]=data[tail];data[tail]=a; } d=t-h; if(d<1){return;} else if(d==1) { if(data[t].a<data[h].a) {a=data[h];data[h]=data[t];data[t]=a;} return; } q2sort1(h,head-1); q2sort1(tail,t); } };

void main() { cout<<"Enter name of file to be compressed\n"; cin>>ftc; comp c; struct time t; gettime(&t); printf("\n\nCompression starting at: %2d:%02d:%02d.%02d\n", t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund); c.xtract(); c.assign_table(); cout<<"The Compressed file will be stored as "<<cf; c.code_gen(); c.code_subs(); cout<<"\nExecution complete"; gettime(&t); printf("\n\nCompression finished at: %2d:%02d:%02d.%02d\n", t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund); }

PROGRAM DECOMPRESSION:

#include<fstream.h> #include<conio.h> #include<math.h> #include<process.h> char ftc[40],cf[40]; struct dat { unsigned char a; int tabno; int code[17]; long int val; }; class decomp { public: dat data[256]; long int siz; fstream f1; decomp()

{ int re=0; while(ftc[re]!='.') { cf[re] =ftc[re]; re++; } cf[re]= '.'; cf[re+1] = 'l'; cf[re+2] = 'b'; cf[re+3] = 'e'; cf[re+4] = '\0'; } void read() { f1.open(ftc,ios::in|ios::binary); if(f1==NULL) { cout<<"Cannot open input file.Exitting....\n"; exit(0); } f1.read((char *)&siz,sizeof(long int)); } void assign_table() { for(long int i=0;i<siz;i++) { f1.read((char *)&data[i].a,sizeof(char)); } int row[20],element,notab,ctab; element=0;notab=0;ctab=0; for(int k=0;k<20;k++) row[k]=0; int j=0; for( i=0;j<siz;i++)

{ if(element<row[ctab]+2) { data[j].tabno= ctab; element++; j++; } else { row[ctab]++; if(ctab<notab) { ctab++; element=0; continue; } if(ctab==notab) { if(row[ctab]<=2) { ctab=0; element=0; } else { ctab++;notab++; element=0; } } } } } void code_gen() { int m=0;int n=0;int track1=1,track2=0,id;int rows[20];

int i; for(i=0;i<20;i++) { rows[i]=1; } while(m<siz) { i=0; for(int j= track1;j>=0&&m<siz;j--) { n=0;

for(int h=0;h<track2;h++) {data[m].code[n]=1; n++;data[m].code[n]=1; n++;} for(int k=0;k<i;k++) {data[m].code[n]=0;n++;} data[m].code[n]=1;n++; for(int l=0;l<j;l++)

{ data[m].code[n]=0;n++;} data[m].code[n]=1; n++; data[m].code[n]=2; i++; m++; if(data[m].tabno!=track2) { rows[track2]++;track2=data[m].tabno;j=rows[track2]+1;i=0; } } if(m<9) rows[track2]++; track1++; } long int wr=0; for( i=0;i<siz;i++) {

wr=0; for(int n=0;data[i].code[n]!=2;n++) { wr= wr+( data[i].code[n]* pow(2,n)); } data[i].val=wr; } dat temp; for(i=0;i<siz-1;i++) { for(int j=i+1;j<siz;j++) { if(data[i].val>data[j].val) { temp =data[i]; data[i]=data[j]; data[j]=temp; } } } } void regen() { fstream f2; f2.open(cf,ios::out|ios::binary); if(f2==NULL) { cout<<"Cannot open output file.exitting.....\n"; exit(0); } unsigned char r;int wr;int buf[20];int track1=0,tempo=0;int flagt=0; int flagc=1,flagr=0,flag0=0; int num=0; long int subs=0; f1.read((char *)&r,1); while(!f1.eof()) {

wr =int(r); for(int i=0;i<20;i++) { buf[i]=0; } track1=0; while(wr||track1<8) { buf[track1] = wr%2; if(buf[track1]==1) { subs= subs + pow(2,num); num++; if(flagc==1&&flag0==0) { flagt=1; flagc=0; } else { if(flagt!=1) { if(flagr==0) { flagc=0; flagr=1; } else { flagc=1; } } } char writ; if(flagr==1&&flagc==1)

{ flagr=0; flagt=0; writ= bin_search(subs); f2.write((char *)&writ,1); num=0; subs=0; flag0=0; } } if(buf[track1]==0) { flag0=1; if(flagt==1) { flagt=0; if(num%2==1) flagr=1; } num++; } wr = wr/2; track1++; } tempo++; f1.read((char *)&r,1); } f2.close(); } char bin_search(long int c) { int beg,mid,end; beg=0; end=siz; mid = siz/2;

for(int i=0;i<siz&&data[mid].val!=c;i++) { if(data[mid].val>c) { end=mid; mid = (beg+end)/2; } else { beg=mid; mid = (beg+end)/2; } } if(data[mid].val==c) return(data[mid].a); else return('p'); } }; void main() { cout<<"enter name of file to be decompressed\n"; cin>>ftc; decomp d; clrscr(); d.read(); d.assign_table(); d.code_gen(); clrscr(); d.regen(); cout<<"\nThe decompressed file was stored as "<<cf<<"\n"; }

OUTPUT: Enter the input to the file gm.cpp:

Graphics and Multimedia Lab Compression algorithm Decompression algorithm

Enter the filename to be compressed : gm.cpp

Compression starting at: 4:20:45.74 The Compressed file will be stored as gm.cmm Execution complete Compression finished at: 4:20:45.80

COMPRESSED OUTPUT: ante sipr;"cof)(.lmyhb\d,E%:u&g0{D=}>*/12A+vw]5BP[T<#CLMGN7Cw^$&L= ca<.DwGJ12DwGJ12|<tb%c LyRo%`nbeW%0&(0t ,I1yd 0. dyh]&t]8&K|%4t^+Vr*A8H3t*UN2 o%(-d*s^E+V9U[22(Ax.%SUSqHgMa<Cn "XT9n(&x.%SU2I<Cn"XT9n(!?)LWq@:|3t* UNqQ9W<C *aqH9MP:Cb&qqs%$<CJghRL$.tsr]|JghRL$+4t*U ,o0,.5MIz.

DECOMPRESSED OUTPUT:

Enter the filename to decompress: gm.cmm The decompressed file is stored as gm.lbe

Graphics and Multimedia Lab Compression algorithm Decompression algorithm

Exit

RESULT: Thus the C program to implement text compression and decompression algorithm was completed and executed successfully.

S-ar putea să vă placă și