+void YUV::init(double Kr, double Kb, int mpeg)
+{
+ this->mpeg = mpeg;
+ int ymin = !mpeg? 0 : 16;
+ int ymax = !mpeg? 255 : 235;
+ int uvmin = !mpeg? 0 : 16;
+ int uvmax = !mpeg? 255 : 240;
+ this->ymin8 = ymin;
+ this->ymax8 = ymax;
+ this->ymin16 = ymin*0x100;
+ this->ymax16 = (ymax+1)*0x100 - 1;
+ this->yzero = 0x10000 * ymin;
+ this->uvmin8 = uvmin;
+ this->uvmax8 = uvmax;
+ this->uvmin16 = uvmin*0x100;
+ this->uvmax16 = (uvmax+1)*0x100 - 1;
+ this->uvzero = 0x800000;
+ this->yminf = ymin / 256.;
+ this->ymaxf = (ymax+1) / 256.;
+ this->yrangef = ymaxf - yminf;
+ this->uvminf = uvmin / 256.;
+ this->uvmaxf = (uvmax+1) / 256.;
+ this->uvrangef = uvmaxf - uvminf;
+ this->Kr = Kr;
+ this->Kg = 1. - Kr - Kb;
+ this->Kb = Kb;
+ double R_to_Y = Kr;
+ double G_to_Y = Kg;
+ double B_to_Y = Kb;
+ double R_to_U = -0.5*Kr/(1-Kb);
+ double G_to_U = -0.5*Kg/(1-Kb);
+ double B_to_U = 0.5;
+ double R_to_V = 0.5;
+ double G_to_V = -0.5*Kg/(1-Kr);
+ double B_to_V = -0.5*Kb/(1-Kr);
+ double V_to_R = 2.0*(1-Kr);
+ double V_to_G = -2.0*Kr*(1-Kr)/Kg;
+ double U_to_G = -2.0*Kb*(1-Kb)/Kg;
+ double U_to_B = 2.0*(1-Kb);
+
+ this->r_to_y = yrangef * R_to_Y;
+ this->g_to_y = yrangef * G_to_Y;
+ this->b_to_y = yrangef * B_to_Y;
+ this->r_to_u = uvrangef * R_to_U;
+ this->g_to_u = uvrangef * G_to_U;
+ this->b_to_u = uvrangef * B_to_U;
+ this->r_to_v = uvrangef * R_to_V;
+ this->g_to_v = uvrangef * G_to_V;
+ this->b_to_v = uvrangef * B_to_V;
+ this->v_to_r = V_to_R / uvrangef;
+ this->v_to_g = V_to_G / uvrangef;
+ this->u_to_g = U_to_G / uvrangef;
+ this->u_to_b = U_to_B / uvrangef;
+
+ init_tables(0x100,
+ rtoy8, rtou8, rtov8,
+ gtoy8, gtou8, gtov8,
+ btoy8, btou8, btov8,
+ ytab8, vtor8, vtog8, utog8, utob8);
+ init_tables(0x10000,
+ rtoy16, rtou16, rtov16,
+ gtoy16, gtou16, gtov16,
+ btoy16, btou16, btov16,
+ ytab16, vtor16, vtog16, utog16, utob16);
+ init_tables(0x100,
+ vtor8f, vtog8f, utog8f, utob8f);
+ init_tables(0x10000,
+ vtor16f, vtog16f, utog16f, utob16f);
+
+ rgb_to_yuv_matrix[0] = r_to_y;
+ rgb_to_yuv_matrix[1] = g_to_y;
+ rgb_to_yuv_matrix[2] = b_to_y;
+ rgb_to_yuv_matrix[3] = r_to_u;
+ rgb_to_yuv_matrix[4] = g_to_u;
+ rgb_to_yuv_matrix[5] = b_to_u;
+ rgb_to_yuv_matrix[6] = r_to_v;
+ rgb_to_yuv_matrix[7] = g_to_v;
+ rgb_to_yuv_matrix[8] = b_to_v;
+
+ float yscale = 1.f / yrangef;
+ yuv_to_rgb_matrix[0] = yscale;
+ yuv_to_rgb_matrix[1] = 0;
+ yuv_to_rgb_matrix[2] = v_to_r;
+ yuv_to_rgb_matrix[3] = yscale;
+ yuv_to_rgb_matrix[4] = u_to_g;
+ yuv_to_rgb_matrix[5] = v_to_g;
+ yuv_to_rgb_matrix[6] = yscale;
+ yuv_to_rgb_matrix[7] = u_to_b;
+ yuv_to_rgb_matrix[8] = 0;
+}