您的位置:首页 > 其它

RGB与HSI空间相互转换

2009-08-01 11:15 417 查看
void HSItrans(char tp)
{
/*
** input: tp
** tp--varible of char type,it is used to judge whether HSI-RGB or RGB-HSI.
** h:[0,360]
** s:[0,1]
** I:[0,1]

** r:[0,255]
** g:[0,255]
** b:[0,255]
*/

if (myHSIArray==NULL) myHSIArray= new sHSI[myPHeight*myPWidth];

double tH,tS,fR,fG,fB;
BYTE R,G,B,tI;
int i,j;

switch (tp)
{
case 't'://RGB-HSI
for(i=0; i<(int)myPHeight; i++)
{
for (j=0; j<(int)myPWidth; j++)
{
R=GetRValue(myPArray[i*myPWidth+j]);
G=GetGValue(myPArray[i*myPWidth+j]);
B=GetBValue(myPArray[i*myPWidth+j]);

int col1,col2,angle;
int mx,mn;

mx=max(max(R,G),B);
mn=min(min(R,G),B);
if (mx==mn)
{
myHSIArray[i*myPWidth+j].H=0;
myHSIArray[i*myPWidth+j].S=0;
}
else {
if (mn==B) {
angle=0; col1=G; col2=R;
}
else if (mn==R) {
angle=120; col1=B; col2=G;
}
else if (mn==G) {
angle=240; col1=R; col2=B;
}
myHSIArray[i*myPWidth+j].H=calchue(angle,col1,col2,mn);
myHSIArray[i*myPWidth+j].S=(/*255.0**/float(mx-mn)/float(mx));
}
myHSIArray[i*myPWidth+j].I=mx;
}
}
break;

case 'v': //con't HSI-RGB
for(i=0; i<(int)myPHeight; i++)
{
for (j=0; j<(int)myPWidth; j++)
{

tH=myHSIArray[i*myPWidth+j].H;
tS=255.0*myHSIArray[i*myPWidth+j].S;
tI=myHSIArray[i*myPWidth+j].I;

int min,max;
tH=(360.0f*tH/252.0f);
max=tI;

if (tS==0) {
R=G=B=max;
}
else {
min=(int)((255.0-tS)*max/255.0);
if (tH<=120) {
B=min;
if (tH<=60) {
R=max;
G=(int)(min+(double)tH*(max-min)/(120.0-tH));
}
else {
G=max;
R=(int)(min+(120.0-tH)*(max-min)/tH);
}
}
else if (tH<=240) {
R=min;
if (tH<=180) {
G=max;
B=(int)(min+(tH-120.0)*(max-min)/(240.0-tH));
}
else {
B=max;
G=(int)(min+(240.0-tH)*(max-min)/(tH-120.0));
}
}
else {
G=min;
if (tH<=300) {
B=max;
R=(int)(min+(tH-240.0)*(max-min)/(360.0-tH));
}
else {
R=max;
B=(int)(min+(360.0-tH)*(max-min)/(tH-240.0));
}
}
myPArray[i*myPWidth+j]=RGB(R,G,B);
}

}
}
break;
}

}

int calchue(int angle, int col1, int col2, int min)
{
int rcode;

rcode=(int) (252.0*(angle+((120.0)*(col1-min)/
(col1+col2-(min+min))))/360.0);
return(rcode);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: