int HSV2RGB(unsigned char *HSV, unsigned char *RGB,unsigned int PicHeight,unsigned int PicWidth)
{

    double r,g,b;
    int x,y;
    double h,s,v;
    int BufOffset;
    int iRData,iGData,iBData;
    int i;
    float f, p, q, t;

    for(y=0;y<PicHeight;y++)    
    {
        for(x=0;x<PicWidth;x++)
        {
            BufOffset = y*PicWidth*3+x*3;
            h=(((double)HSV[BufOffset])/255.0*360.0);
            s=((double)HSV[BufOffset+1]/255.0);
            v=((double)HSV[BufOffset+2]/255.0);
            if( s == 0 ){               
                r = g = b = v;
            }else{
                h /= 60; // sector 0 to 5
                i = floor( h );
                f = h - i; // factorial part of h
                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: // case 5:
                        r = v;
                        g = p;
                        b = q;
                        break;
                }
            }
            //-----------------//
            iRData = (int)(r*255.0);        
            if(iRData > 255)
                iRData = 255;
            if(iRData < 0)
                iRData = 0;
            iGData = (int)(g*255.0);
            if(iGData > 255)
                iGData = 255;
            if(iGData < 0)
                iGData = 0;
            iBData = (int)(b*255.0);
            if(iBData > 255)
                iBData = 255;
            if(iBData < 0)
                iBData = 0;
            RGB[BufOffset] = iBData;
            RGB[BufOffset+1] = iGData;
            RGB[BufOffset+2] = iRData;   
            //-----------------//
        }
    }
    return 0;
}



arrow
arrow
    全站熱搜
    創作者介紹
    創作者 gordenhao 的頭像
    gordenhao

    高登

    gordenhao 發表在 痞客邦 留言(0) 人氣()