色彩空间转换

来源:互联网 发布:js div onclick事件 编辑:程序博客网 时间:2024/05/16 01:08
// RGB转换为YUVvoid RGB2YUV(double R, double G, double B, double &Y, double &U, double &V){        Y = static_cast<double>(0.257*R + 0.504*G + 0.098*B + 16); // y    U = static_cast<double>(-0.148*R - 0.291*G+ 0.439*B + 128); // u    V = static_cast<double>(0.439*R - 0.368*G - 0.071*B + 128); // v}// YUV转换为RGBvoid YUV2RGB(double Y,double U,double V, double &R, double &G, double &B){    R = static_cast<double>(1.0*Y + 8 + 1.402*(V-128)); // r    G = static_cast<double>(1.0*Y - 0.34413*(U-128) - 0.71414*(V-128));      B = static_cast<double>(1.0*Y + 1.772*(U-128) + 0); }// RGB转换为XYZvoid RGB2XYZ(double r, double g, double b double &x, double &y, double &z){    double var_R,var_G,var_B;               var_R = (r / 255.0f);    var_G = (g / 255.0f);    var_B = (b / 255.0f);    if (var_R > 0.04045)    {        var_R = pow((var_R + 0.055)/1.055, 2.4);            }    else     {        var_R = var_R / 12.92;    }    if (var_G > 0.04045)    {        var_G = pow((var_G + 0.055)/1.055, 2.4);    }    else     {        var_G = var_G / 12.92;    }    if (var_B > 0.04045)    {        var_B = pow((var_B + 0.055)/1.055, 2.4);    }    else    {        var_B = var_B / 12.92;    }             var_R = var_R * 100.0f;       var_G = var_G * 100.0f;      var_B = var_B * 100.0f;      //Observer.   =   2°,   Illuminant   =   D65       x = var_R * 0.4124 + var_G * 0.3576 + var_B * 0.1805;    y = var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722;    z = var_R * 0.0193 + var_G * 0.1192 + var_B * 0.9505;  }// XYZ转换为LABvoid XYZ2LAB(double x, double y, double z, double &L, double &A ,double &B){    double var_X,var_Y,var_Z;    var_X = x / 95.047f;            var_Y = y / 100.000f;              var_Z = z / 108.883f;              if ( var_X > 0.008856 )     {        var_X = pow((float)var_X,1.0f/3.0f);    }    else      {        var_X = ( 7.787 * var_X ) + ( 16 / 116 );    }    if (var_Y > 0.008856)    {        var_Y = pow((float)var_Y,1.0f/3.0f);    }    else     {        var_Y = ( 7.787 * var_Y ) + ( 16 / 116 );    }    if ( var_Z > 0.008856 )    {        var_Z = pow((float)var_Z,1.0f/3.0f);    }    else    {        var_Z = ( 7.787 * var_Z ) + ( 16 / 116 );    }    L = ( 116 * var_Y ) - 16;    A = 500 * ( var_X - var_Y );    B = 200 * ( var_Y - var_Z );}// RGB转换为LABvoid RGB2LAB(double r, double g,double b, double &L, double &A, double &B){    double x,y,z;    RGB2XYZ(r,g,b,x,y,z);    XYZ2LAB(x,y,z,L,A,B);}// LAB转换为RGBvoid LAB2RGB(double L, double A, double B, double &R, double &G, double &B){    double x,y,z[3];    this->LAB2XYZ(L,A,B,x,y,z);    this->XYZ2RGB(x,y,z,R,G,B);}// LAB转换为XYZvoid LAB2XYZ(double lab_L, double lab_A, double lab_B, double &X, double &Y, double &Z){        double var_X,var_Y, var_Z;        var_Y = ( lab_L + 16 ) / 116;    var_X = lab_A / 500 + var_Y;    var_Z = var_Y - lab_B / 200;    if ( pow((double)var_Y,3) > 0.008856 )         var_Y = pow((double)var_Y,3);    else                              var_Y = ( var_Y - 16 / 116 ) / 7.787;    if ( pow((double)var_X,3) > 0.008856 )         var_X = pow((double)var_X,3);    else                 var_X = ( var_X - 16 / 116 ) / 7.787;    if ( pow((double)var_Z,3) > 0.008856 )         var_Z = pow((double)var_Z,3);    else                              var_Z = ( var_Z - 16 / 116 ) / 7.787;    X = 95.047 * var_X;         Y = 100.000 * var_Y;        Z = 108.883 * var_Z;    }// XYZ转换为RGBvoid XYZ2RGB(double x, double y, double z, double &R, double &G, double &B){    double var_X,var_Y,var_Z,var_R,var_G,var_B;    double x,y,z;    var_X = x / 100.0f;        //X from 0 to  95.047      (Observer = 2°, Illuminant = D65)    var_Y = y / 100.0f;        //Y from 0 to 100.000    var_Z = z / 100.0f;        //Z from 0 to 108.883    var_R = var_X *  3.2406 + var_Y * -1.5372 + var_Z * -0.4986;    var_G = var_X * -0.9689 + var_Y *  1.8758 + var_Z *  0.0415;    var_B = var_X *  0.0557 + var_Y * -0.2040 + var_Z *  1.0570;    if ( var_R > 0.0031308 )         var_R = 1.055 * ( pow(var_R,( 1 / 2.4 ) )) - 0.055;    else                             var_R = 12.92 * var_R;    if ( var_G > 0.0031308 )        var_G = 1.055 * ( pow(var_G, ( 1 / 2.4 )) ) - 0.055;    else                             var_G = 12.92 * var_G;    if ( var_B > 0.0031308 )         var_B = 1.055 * ( pow(var_B, ( 1 / 2.4 )) ) - 0.055;    else                            var_B = 12.92 * var_B;    R = var_R * 255.0f;    G = var_G * 255.0f;    b = var_B * 255.0f;}// RGB转换为HSVvoid RGB2HSV(double R, double G, double B, double &H, double &S, double V){    double var_R,var_G,var_B;    var_R = ( R / 255 );                    //RGB from 0 to 255    var_G = ( G / 255 );    var_B = ( B / 255 );    double var_Min,var_Max,del_Max;    var_Min = min(min(var_R,var_G),var_B);    var_Max = max(max(var_R,var_G),var_B);    del_Max = var_Max - var_Min;            //Delta RGB value     double H,S,V;    V = var_Max;    if ( del_Max == 0 )                     //This is a gray, no chroma    {        H = 0;                               //HSV results from 0 to 1        S = 0;    }    else                                    //Chromatic data    {        S = del_Max / var_Max;                double del_R,del_G,del_B;        del_R = ( ( ( var_Max - var_R ) / 6 ) + ( del_Max / 2 ) ) / del_Max;        del_G = ( ( ( var_Max - var_G ) / 6 ) + ( del_Max / 2 ) ) / del_Max;        del_B = ( ( ( var_Max - var_B ) / 6 ) + ( del_Max / 2 ) ) / del_Max;        if ( var_R == var_Max )             H = del_B - del_G;        else if ( var_G == var_Max )            H = ( 1 / 3 ) + del_R - del_B;        else if ( var_B == var_Max )             H = ( 2 / 3 ) + del_G - del_R;        if ( H < 0 )             H += 1;        if ( H > 1 )             H -= 1;    }}// HSV转换为RGBvoid HSV2RGB(double H, double S, double V, double &R, double &G, double &B){    double R,G,B;    if ( S == 0 )                       //HSV from 0 to 1    {        R = V * 255.0f;        G = V * 255.0f;        B = V * 255.0f;    }    else    {        double var_h,var_i,var_1,var_2,var_3;        var_h = H * 6.0f;        if ( var_h == 6 )             var_h = 0;     //H must be < 1        var_i = int( var_h );            //Or  var_i = floor( var_h )        var_1 = V * ( 1 - S );        var_2 = V * ( 1 - S * ( var_h - var_i ) );        var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );        double var_r,var_g,var_b;        if ( var_i == 0 )         {            var_r = V;             var_g = var_3 ;            var_b = var_1;        }        else if ( var_i == 1 )         {             var_r = var_2 ;            var_g = V;            var_b = var_1;        }            else if ( var_i == 2 )         {            var_r = var_1 ;            var_g = V;             var_b = var_3;        }        else if ( var_i == 3 )         {             var_r = var_1 ;             var_g = var_2 ;            var_b = V;        }        else if ( var_i == 4 )         {             var_r = var_3 ;            var_g = var_1 ;             var_b = V;        }        else            {             var_r = V;             var_g = var_1 ;             var_b = var_2;        }        R = var_r * 255.0f;                 //RGB results from 0 to 255        G = var_g * 255.0f;        B = var_b * 255.0f;    }}// YUV转换为XYZvoid YUV2XYZ(double L, double U, double V, double &X, double &Y, double &Z){    double var_Y,ref_X,ref_Y,ref_Z,ref_U,ref_V,var_U,var_V;    var_Y = ( L + 16 ) / 116;    if ( pow(var_Y,3) > 0.008856 )         var_Y = pow(var_Y,3);    else                              var_Y = ( var_Y - 16 / 116 ) / 7.787;    ref_X =  95.047;      //Observer= 2°, Illuminant= D65    ref_Y = 100.000;    ref_Z = 108.883;    ref_U = ( 4 * ref_X ) / ( ref_X + ( 15 * ref_Y ) + ( 3 * ref_Z ) );    ref_V = ( 9 * ref_Y ) / ( ref_X + ( 15 * ref_Y ) + ( 3 * ref_Z ) );    var_U = U / ( 13 * L ) + ref_U;    var_V = V / ( 13 * L) + ref_V;    Y = var_Y * 100;    X =  - ( 9 * Y * var_U ) / ( ( var_U - 4 ) * var_V  - var_U * var_V );    Z = ( 9 * Y - ( 15 * var_V * Y ) - ( var_V * X ) ) / ( 3 * var_V );}// LAB转换为HSVvoid LAB2HSV(double L, double A, double B, double &H, double &S, double &V){    double X,Y,Z;    this->LAB2XYZ(L,A,B,X,Y,Z);    this->XYZ2HSV(X,Y,Z,H,S,V);}// XYZ转换为HSVvoid XYZ2HSV(double X, double Y, double Z,double &H, double &S, double &V){    double R,G,B;    this->XYZ2RGB(X,Y,Z,R,G,B);    this->RGB2HSV(R,G,B,H,S,V);}// RGB转换为CMYvoid RGB2CMY(double R, double G, double B, double &C, double &M, double Y){    C = 1.0 - ( R / 255.0f );    M = 1.0 - ( G / 255.0f );    Y = 1.0 - ( B / 255.0f );}//CMY转换为RGBvoid CMY2RGB(double C, double M, double Y, double &R, double &G, double &B){    R = ( 1.0f - C ) * 255.0f;    G = ( 1.0f - M ) * 255.0f;    B = ( 1.0f - Y ) * 255.0f;}//RGB转换为CMYKvoid RGB2CMYK(double R, double G, double B, double &C, double &M, double Y, double &K){    double bufCMY[3];    //RGB2CMY    this->RGB2CMY(R,G,B,C,M,Y);    //CMYK and CMY values from 0 to 1    double var_K;    var_K = 1;    if ( C < var_K )          var_K = C;    if ( M < var_K )           var_K = M;    if ( Y < var_K )          var_K = Y;    if ( var_K == 1 )    { //Black        C = 0;        M = 0;        Y = 0;    }    else     {        C = ( C - var_K ) / ( 1 - var_K );        M = ( M - var_K ) / ( 1 - var_K );        Y = ( Y - var_K ) / ( 1 - var_K );    }    K = var_K;}//CMYK转换为CMYvoid CMYK2CMY(double C1, double M1, double Y1, double K1, double &C2, double &M2, double Y2){    C2 = ( C1 * ( 1 - K1 ) + K1 );    M2 = ( M1 * ( 1 - K1 ) + K1 );    Y2 = ( Y1 * ( 1 - K1 ) + K1 );}


0 0
原创粉丝点击