Wednesday, April 1, 2009

c program

#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"math.h"
#define min(a,b)(a#define max(a,b)(a>b?a:b)
#define NO_HUE-1
float r,g,b,h,s,v,c,n,y,y1,u,v1,m,a;
void main()
{
int gd=DETECT,gm,ch;
void rgbtohsv(float,float,float);
void hsvtorgb(float,float,float);
void cmytorgb(float,float,float);
void rgbtocmy(float,float,float);
void yuvtorgb(float,float,float);
void rgbtoyuv(float,float,float);
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();
do
{
printf("\n\t conversion of color model \n\t 1.HSV to GB \n\t 2.RGB to HSV \n\t 3.CMV to RGB \n\t 4.RGB to CMV \n\t 5.YUV to RGB \n\t 6.RGB to YUV \n");
printf("\n your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter the HSV coordinates\n");
scanf("%f%f%f",&h,&s,&v);
hsvtorgb(h,s,v);
break;
case 2:
printf("\n enter the RGB cooordinate\n");
scanf("%f%f%f",&r,&g,&b);
rgbtohsv(r,g,b);
break;
case 3:
printf("\n enter the CMY coordinates\n");
scanf("%f%f%f",&c,&m,&y);
cmytorgb(c,m,y);
break;
case 4:
printf("\n Enter the RGB coordinates\n");
scanf("%f%f%f",&r,&g,&b);
rgbtocmy(r,g,b);
break;
case 5:
printf("\n Enter the YUV coordinates\n");
scanf("%f%f%f",&y1,&u,&v1);
yuvtorgb(y1,u,v1);
break;
case 6:
printf("\n Enter the RGB coordinates\n");
scanf("%f%f%f",&r,&g,&b);
rgbtoyuv(r,g,b);
break;
default:
break;
}}
while(ch>0&&ch<7);
getch();
}
void hsvtorgb(float h,float s,float v)
{
float aa,bb,cc,f;
int i;
if(s==0.0)
r=g=b=v;
else
{
if(h==1.0)
h=0.0;
h=h*6.0;
i=floor(h);
f=h-i;
aa=v*(1-s);
bb=v*(1-(s*f));
cc=v*(1-(s*(1-f)));
switch(i)
{
case 0:
r=v;g=cc;b=aa;
break;
case 1:
r=bb;g=v;b=aa;
break;
case 2:
r=aa;g=v;b=cc;
break;
case 3:
r=aa;g=bb;b=v;
break;
case 4:
r=cc;g=aa;b=v;
break;
case 5:
r=v;g=aa;b=bb;
break;
}}
printf("\n values of RGB coordinates:(r=%f,g=%f,b=%f)",r,g,b);
}
void rgbtohsv(float r,float g,float b)
{
float mx=max(r,max(g,b)),mn=min(r,min(g,b));
float delta=mx-mn;
v=mx;
if(mx!=0.0)
s=delta/mx;
else
s=0.0;
if(s==0.0)
h=NO_HUE;
else
{
if(r==mx)
h=(g-b)/delta;
else if(g==mx)
h=2+(b-r)/delta;
else if(b==mx)
h=4+(r-g)/delta;
h=h*60.0;
if(h<0)
h=h/360.0;
}
printf("\n values of HSV coordinates(h=%f,s=%f,v=%f)",h,s,v);
}
void cmytorgb(float c,float m,float y)
{
r=1-c;
g=1-m;
b=1-y;
printf("\n values of RGB coordinates(r=%f,g=%f,b=%f)",r,g,b);
}
void rgbtocmy(float r,float g,float b)
{
c=1-r;
m=1-g;y=1-b;
printf("\n values of CMV coordinates(c=%f,m=%f,r=%f)",c,m,y);
}
void yuvtorgb(float y1,float u,float v1)
{
r=(1.0*y1)+(0.956*u)+(0.620*v1);
g=(1.0*y1)-(0.272*u)-(0.647*v1);
b=(1.0*y1)-(1.108*u)+(1.705*v1);
printf("\n values of RGB coordinates(r=%f,g=%f,b=%f)",r,g,b);
}
void rgbtoyuv(float r,float g,float b)
{
y1=(0.299*r)+(0.587*g)+(0.114*b);
u=(0.596*r)-(0.275*g)-(0.321*b);
v1=(0.212*r)-(0.528*g)+(0.311*b);
printf("\n values of YUV coordinates(y=%f,u=%f,v=%f)",y1,u,v1);
}


OUTPUT:

Conversion of color model
1.hsv to rgb
2.rgb to hsv
3.cmy to rgb
4.rgb to cmy
5.yuv to rgb
6.rgb to yuv


your choice 1
Enter the HSV coordinates 1 1 1
Values of RGB coordinates
{r=1.000000 g=0.000000 b=0.000000}conversion of color model
your choice 2
Enter the RGB coordinates 1 1 1
Values of HSV coordinates
{h=1.000000 s=0.000000 v=1.000000}conversion of color model
your choice 3
Enter the CMY coordinates 1 1 1
Values of RGB coordinates
{r=0.000000 g=0.000000 b=0.000000}conversion of color model


your choice 4
Enter the RGB coordinates 1 1 1
Values of CMY coordinates
{c=0.000000 m=0.000000 y=0.000000}conversion of color model
your choice 5
Enter the YUV coordinates 1 1 1
Values of RGB coordinates
{2.577000 g=0.081000 b=1.596900}conversion color model
your choice 6
Enter the RGB coordinates 1 1 1
Values of YUV coordinates
{y=1.000000 u=0.027000 v=0.000000}conversion of color model