This is code I need to maintain. I'm trying to make this as an example for code gone bad, for C programmers going to C++.
Please provide any comment you can. The original code is about 40 pages long, so I just gave this "small example".
void Algo::compute( Image* pInImg, Image* pImg, Image* deBugImg)
{
int ind_r, ind_c, ind_app_r, ind_app_c , ind_g_r, ind_g_c;
int Gv_approx[7][7], Gh_approx[7][7], Bv_approx[5][5], Bh_approx[5][5], Rv_approx[5][5], Rh_approx[5][5];
int Gv_clip_approx[7][7], Gh_clip_approx[7][7];
int HH_h, HH_v;
int Yv_approx[5][3], Yh_approx[3][5], Uv_approx[5][3], Uh_approx[3][5], Vv_approx[5][3], Vh_approx[3][5] ;
int R_ahd[5][5], G_ahd[5][5], B_ahd[5][5];
int Y_ahd[5][5], U_ahd[5][5], V_ahd[5][5];
int w_v_q, w_h_q;
int avg_gr_q=0, avg_gb_q=0, avg_G_q, avg_G3_q, G_std_q, G3_std_q, Gr_std_q, Gb_std_q, AvgG_std_q;
int avg_G3_q_rm1, avg_G3_q_rp1, avg_G3_q_cm1, avg_G3_q_cp1, G3_std_q_rm1, G3_std_q_rp1, G3_std_q_cm1, G3_std_q_cp1;// Research checkers FCC
int std_prec = (1<<HWP_MSB_STD)-1;
int HL_h, HL_v,HW_h, HW_v, HW_h2, HW_v2;
int HL_hq, HL_vq,HH_hq, HH_vq, wh_quant;
int correction_coef_q;
int avg_Uv, avg_Uh, avg_Vv, avg_Vh, avg_Cvq, avg_Chq;
int coring_shift;
int* ByerrBufferInLine;
int* ByerrBufferInLine_min1;
int* ByerrBufferInLine_min2;
int* ByerrBufferInLine_pls1;
int* ByerrBufferInLine_pls2;
int margin_h = 5, margin_v = 0,buf_r;
int MaxPxlVal = (1<<HWP_PXL_BIT)-1;
int valinlog, MSB, MSB2;
int OLD_VERSION = 0;
/// DBG
std::array<int, 1> x_vect = {2310};
std::array<int, 1> y_vect = {1723};
FILE *fp_log=fopen(m_OutputFileName.c_str(),"wt");
int w_clp_array[7], w_clp_array_even[7], w_clp_array_odd[7];
// define config units
Prepare_Config_Unit_DIAG1();
Prepare_config_Unit_DIAG2();
Prepare_config_Unit_HF();
Prepare_config_Unit_DIAGSAT();
// produce corinng threshold calculation outside main loop
int Corin_TH_array[4096];
for(int ii = 0; ii<4096; ii++)
{
if(ii>1)
{
MSB = (int)(log((float)ii)/log(2.0));
valinlog= ii - (1<<MSB);
if(valinlog>0)
{
MSB2 =(int)(log((float)valinlog)/log(2.0));
Corin_TH_array[ii] = MSB + (MSB2 == (MSB-1));
}
else
{
Corin_TH_array[ii] = MSB;
}
}
else
{
Corin_TH_array[ii] = 0;
}
}
for( int row=0 + margin_v; row < m_Height - margin_v; row++){
// define rellevant lines' pointers. Our restriction that inout buffert have full quads only => even number of lines
buf_r = min( max(row-2,abs((row-2)& 1)) , m_Height - 2 + abs((row-2)& 1) );
int* ByerrBufferLine_min2 = pInImg->m_R[buf_r];
buf_r = min( max(row-1,abs((row-1)& 1)) , m_Height - 2 + abs((row-1)& 1) );
int* ByerrBufferLine_min1 = pInImg->m_R[buf_r];
buf_r = min( max(row,abs((row)& 1)) , m_Height - 2 + abs((row)& 1) );
int* ByerrBufferLine = pInImg->m_R[buf_r];
buf_r = min( max(row+1,abs((row+1)& 1)) , m_Height - 2 + abs((row+1)& 1) );
int* ByerrBufferLine_pls1 = pInImg->m_R[buf_r];
buf_r = min( max(row+2,abs((row+2)& 1)) , m_Height - 2 + abs((row+2)& 1) );
int* ByerrBufferLine_pls2 = pInImg->m_R[buf_r];
//// DBG
buf_r = min( max(row+3,abs((row+3)& 1)) , m_Height - 3 + abs((row+3)& 1) );
int* ByerrBufferLine_pls3 = pInImg->m_R[buf_r];
buf_r = min( max(row+4,abs((row+4)& 1)) , m_Height - 4 + abs((row+4)& 1) );
int* ByerrBufferLine_pls4 = pInImg->m_R[buf_r];
buf_r = min( max(row-3,abs((row-3)& 1)) , m_Height - 3 + abs((row-3)& 1) );
int* ByerrBufferLine_min3 = pInImg->m_R[buf_r];
buf_r = min( max(row-4,abs((row-4)& 1)) , m_Height - 4 + abs((row-4)& 1) );
int* ByerrBufferLine_min4 = pInImg->m_R[buf_r];
for( int col=0 + margin_h; col < m_Width - margin_h; col++){
////////////// DBG
//if(DEBUG_MODE)
//{
int log_this_pix=0;
for(int dbg_indx = 0; dbg_indx < x_vect.size(); dbg_indx ++)
{
if((fp_log != NULL) && (((col == x_vect[dbg_indx] && (row == y_vect[dbg_indx]) ))))
{
log_this_pix = 1;
break;
}
}
if(log_this_pix)
{
fprintf(fp_log,"org_pix %d x- %d y- %d",ByerrBufferLine[col], col, row);
if(FC(row,col)== Green)
{
if(FC(row,col-1)== Red)
{
fprintf(fp_log,"\tpixel GREEN RED\n");
}
else
{
fprintf(fp_log,"\tpixel GREEN BLUE\n");
}
}
else if(FC(row,col)== Red)
{
fprintf(fp_log,"\tpixel RED\n");
}
else
{
fprintf(fp_log,"\tpixel BLUE\n");
}
for(int dbg_x_shft = col-4; dbg_x_shft<col+4; dbg_x_shft++)
{
fprintf(fp_log,"%d\t", ByerrBufferLine_min4[dbg_x_shft]);
}
fprintf(fp_log,"\n");
for(int dbg_x_shft = col-4; dbg_x_shft<col+4; dbg_x_shft++)
{
fprintf(fp_log,"%d\t", ByerrBufferLine_min3[dbg_x_shft]);
}
fprintf(fp_log,"\n");
for(int dbg_x_shft = col-4; dbg_x_shft<col+4; dbg_x_shft++)
{
fprintf(fp_log,"%d\t", ByerrBufferLine_min2[dbg_x_shft]);
}
fprintf(fp_log,"\n");
for(int dbg_x_shft = col-4; dbg_x_shft<col+4; dbg_x_shft++)
{
fprintf(fp_log,"%d\t", ByerrBufferLine_min1[dbg_x_shft]);
}
fprintf(fp_log,"\n");
for(int dbg_x_shft = col-4; dbg_x_shft<col+4; dbg_x_shft++)
{
fprintf(fp_log,"%d\t", ByerrBufferLine[dbg_x_shft]);
}
fprintf(fp_log,"\n");
for(int dbg_x_shft = col-4; dbg_x_shft<col+4; dbg_x_shft++)
{
fprintf(fp_log,"%d\t", ByerrBufferLine_pls1[dbg_x_shft]);
}
fprintf(fp_log,"\n");
for(int dbg_x_shft = col-4; dbg_x_shft<col+4; dbg_x_shft++)
{
fprintf(fp_log,"%d\t", ByerrBufferLine_pls2[dbg_x_shft]);
}
fprintf(fp_log,"\n");
for(int dbg_x_shft = col-4; dbg_x_shft<col+4; dbg_x_shft++)
{
fprintf(fp_log,"%d\t", ByerrBufferLine_pls3[dbg_x_shft]);
}
fprintf(fp_log,"\n");
for(int dbg_x_shft = col-4; dbg_x_shft<col+4; dbg_x_shft++)
{
fprintf(fp_log,"%d\t", ByerrBufferLine_pls4[dbg_x_shft]);
}
fprintf(fp_log,"\n");
}
//}
///////////////////////////////
// Patch statistics calculation.
//////////////////////
// Calculate statistics
if(FC(row,col)== Green)
{
if(FC(row, col-1)== Red)
{
avg_gr_q = (455*(int)(ByerrBufferLine_min2 [col-2] + ByerrBufferLine_min2 [col] + ByerrBufferLine_min2 [col+2]
+ByerrBufferLine[col-2] + ByerrBufferLine[col] + ByerrBufferLine[col+2]
+ByerrBufferLine_pls2[col-2] + ByerrBufferLine_pls2[col] + ByerrBufferLine_pls2[col+2]))>>12;
avg_gr_q = CLIP_VAL(avg_gr_q, 0, MaxPxlVal);
avg_gb_q = ((int)((ByerrBufferLine_min1[col-1] + ByerrBufferLine_min1[col+1]
+ ByerrBufferLine_pls1[col-1] + ByerrBufferLine_pls1[col+1] + 2)>>2));
avg_gb_q = CLIP_VAL(avg_gb_q, 0, MaxPxlVal);
}
else
{
avg_gr_q = (int)((ByerrBufferLine_min1[col-1] + ByerrBufferLine_min1[col+1]
+ ByerrBufferLine_pls1[col-1] + ByerrBufferLine_pls1[col+1] + 2)>>2);
avg_gr_q = CLIP_VAL(avg_gr_q, 0, MaxPxlVal);
avg_gb_q = (455*(int)(ByerrBufferLine_min2[col-2] + ByerrBufferLine_min2[col] + ByerrBufferLine_min2[col+2]
+ ByerrBufferLine[col-2] + ByerrBufferLine[col] + ByerrBufferLine[col+2]
+ ByerrBufferLine_pls2[col-2] + ByerrBufferLine_pls2[col] + ByerrBufferLine_pls2[col+2])>>12);
avg_gb_q = CLIP_VAL(avg_gb_q, 0, MaxPxlVal);
}
avg_G3_q = 819*(int)(ByerrBufferLine_min1[col-1] + ByerrBufferLine_min1[col+1] + ByerrBufferLine[col] +
ByerrBufferLine_pls1[col-1] + ByerrBufferLine_pls1[col+1])>>12;
avg_G3_q = CLIP_VAL(avg_G3_q, 0, MaxPxlVal);
// Research FCC checker removal
avg_G3_q_rm1 = (int)(ByerrBufferLine_min2[col] + ByerrBufferLine_min1[col-1] + ByerrBufferLine_min1[col+1] +
ByerrBufferLine[col] + 2) >> 2;
avg_G3_q_rm1 = CLIP_VAL(avg_G3_q_rm1, 0, MaxPxlVal);
avg_G3_q_rp1 = (int)(ByerrBufferLine[col] + ByerrBufferLine_pls1[col-1] + ByerrBufferLine_pls1[col+1] +
ByerrBufferLine_pls2[col] + 2) >> 2;
avg_G3_q_rp1 = CLIP_VAL(avg_G3_q_rp1, 0, MaxPxlVal);
avg_G3_q_cm1 = (int)(ByerrBufferLine[col-2] + ByerrBufferLine_min1[col-1] + ByerrBufferLine[col] +
ByerrBufferLine_pls1[col-1] + 2) >> 2;
avg_G3_q_cm1 = CLIP_VAL(avg_G3_q_cm1, 0, MaxPxlVal);
avg_G3_q_cp1 = (int)(ByerrBufferLine[col] + ByerrBufferLine_min1[col+1] + ByerrBufferLine[col+2] +
ByerrBufferLine_pls1[col+1] + 2) >> 2;
avg_G3_q_cp1 = CLIP_VAL(avg_G3_q_cp1, 0, MaxPxlVal);
}
else
{
if(FC(row,col)== Red)
{
avg_gr_q = (682*(int)(ByerrBufferLine_min2[col-1] + ByerrBufferLine_min2[col+1]
+ByerrBufferLine[col-1] + ByerrBufferLine[col+1]
+ByerrBufferLine_pls2[col-1] + ByerrBufferLine_pls2[col+1])>>12);
avg_gr_q = CLIP_VAL(avg_gr_q, 0, MaxPxlVal);
avg_gb_q = (682*(int)(ByerrBufferLine_min1[col-2] + ByerrBufferLine_min1[col] + ByerrBufferLine_min1[col+2]
+ByerrBufferLine_pls1[col-2] + ByerrBufferLine_pls1[col] + ByerrBufferLine_pls1[col+2])>>12);
avg_gb_q = CLIP_VAL(avg_gb_q, 0, MaxPxlVal);
}
else
{
avg_gr_q = (682*(int)(ByerrBufferLine_min1[col-2] + ByerrBufferLine_min1[col] + ByerrBufferLine_min1[col+2]
+ByerrBufferLine_pls1[col-2] + ByerrBufferLine_pls1[col] + ByerrBufferLine_pls1[col+2])>>12);
avg_gr_q = CLIP_VAL(avg_gr_q, 0, MaxPxlVal);
avg_gb_q = (682*(int)(ByerrBufferLine_min2[col-1] + ByerrBufferLine_min2[col+1]
+ByerrBufferLine[col-1] + ByerrBufferLine[col+1]
+ByerrBufferLine_pls2[col-1] + ByerrBufferLine_pls2[col+1])>>12);
avg_gb_q = CLIP_VAL(avg_gb_q, 0, MaxPxlVal);
}
avg_G3_q = (int)(ByerrBufferLine_min1[col] + ByerrBufferLine[col-1] + ByerrBufferLine[col+1] +
ByerrBufferLine_pls1[col] + 2) >> 2;
avg_G3_q = CLIP_VAL(avg_G3_q, 0, MaxPxlVal);
/// Research FCC, checker removal
avg_G3_q_rm1 = 819*(int)(ByerrBufferLine_min2[col-1] + ByerrBufferLine_min2[col+1] + ByerrBufferLine_min1[col] +
ByerrBufferLine[col-1] + ByerrBufferLine[col+1])>>12;
avg_G3_q_rm1 = CLIP_VAL(avg_G3_q_rm1, 0, MaxPxlVal);
avg_G3_q_rp1 = 819*(int)(ByerrBufferLine[col-1] + ByerrBufferLine[col+1] + ByerrBufferLine_pls1[col] +
ByerrBufferLine_pls2[col-1] + ByerrBufferLine_pls2[col+1])>>12;
avg_G3_q_rp1 = CLIP_VAL(avg_G3_q_rp1, 0, MaxPxlVal);
avg_G3_q_cm1 =819*(int)(ByerrBufferLine_min1[col-2] + ByerrBufferLine_min1[col] + ByerrBufferLine[col-1] +
ByerrBufferLine_pls1[col-2] + ByerrBufferLine_pls1[col+2])>>12;
avg_G3_q_cm1 = CLIP_VAL(avg_G3_q_cm1, 0, MaxPxlVal);
avg_G3_q_cp1 = 819*(int)(ByerrBufferLine_min1[col] + ByerrBufferLine_min1[col+2] + ByerrBufferLine[col+1] +
ByerrBufferLine_pls1[col] + ByerrBufferLine_pls1[col+2])>>12;
avg_G3_q_cp1 = CLIP_VAL(avg_G3_q_cp1, 0, MaxPxlVal);
}
avg_G_q = (avg_gr_q + avg_gb_q + 1)>>1;
// calc std
if(FC(row,col)== Green){
if(FC(row,col-1)== Red)
{
Gr_std_q = (455*(int)(min(abs(ByerrBufferLine_min2[col-2] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_min2[col] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_min2[col+2] - avg_gr_q) , std_prec )
+min(abs(ByerrBufferLine[col-2] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine[col] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine[col+2] - avg_gr_q) , std_prec )
+min(abs(ByerrBufferLine_pls2[col-2] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_pls2[col] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_pls2[col+2] - avg_gr_q) , std_prec )))>>12;
Gb_std_q = (min(abs(ByerrBufferLine_min1[col-1] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col+1] - avg_gb_q) , std_prec )
+min(abs(ByerrBufferLine_pls1[col-1] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col+1] - avg_gb_q) , std_prec ) + 2)>>2;
}
else
{
Gr_std_q = (min(abs(ByerrBufferLine_min1[col-1] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col+1] - avg_gr_q) , std_prec )
+min(abs(ByerrBufferLine_pls1[col-1] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col+1] - avg_gr_q) , std_prec ) + 2)>>2;
Gb_std_q = (455*(int)(min(abs(ByerrBufferLine_min2[col-2] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_min2[col] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_min2[col+2] - avg_gb_q) , std_prec )
+min(abs(ByerrBufferLine[col-2] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine[col] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine[col+2] - avg_gb_q) , std_prec )
+min(abs(ByerrBufferLine_pls2[col-2] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_pls2[col] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_pls2[col+2] - avg_gb_q) , std_prec )))>>12;
}
G3_std_q = (819*(int)(min(abs(ByerrBufferLine_min1[col-1] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col+1] - avg_G3_q) , std_prec )
+min(abs(ByerrBufferLine[col] - avg_G3_q) , std_prec )
+min(abs(ByerrBufferLine_pls1[col-1] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col+1] - avg_G3_q) , std_prec )))>>12;
G_std_q = (315*(int)(min(abs(ByerrBufferLine_min2[col-2] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_min2[col] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_min2[col+2] - avg_G_q) , std_prec )
+min(abs(ByerrBufferLine_min1[col-1] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col+1] - avg_G_q) , std_prec )
+min(abs(ByerrBufferLine[col-2] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine[col] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine[col+2] - avg_G_q) , std_prec )
+min(abs(ByerrBufferLine_pls1[col-1] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col+1] - avg_G_q) , std_prec )
+min(abs(ByerrBufferLine_pls2[col-2] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_pls2[col] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_pls2[col+2] - avg_G_q) , std_prec )))>>12;
/// Research FCC, checker removal
G3_std_q_rm1 = (int)( min(abs(ByerrBufferLine_min2[col] - avg_G3_q_rm1) , std_prec )
+ min(abs(ByerrBufferLine_min1[col-1] - avg_G3_q_rm1) , std_prec ) + min(abs(ByerrBufferLine_min1[col+1] - avg_G3_q_rm1) , std_prec )
+ min(abs(ByerrBufferLine[col] - avg_G3_q_rm1) , std_prec ) + 2)>>2;
G3_std_q_rp1 = (int)( min(abs(ByerrBufferLine[col] - avg_G3_q) , std_prec )
+ min(abs(ByerrBufferLine_pls1[col-1] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col+1] - avg_G3_q) , std_prec )
+ min(abs(ByerrBufferLine_pls2[col] - avg_G3_q) , std_prec ) + 2)>>2;
G3_std_q_cm1 = (int)( min(abs(ByerrBufferLine_min1[col-1] - avg_G3_q) , std_prec )
+ min(abs(ByerrBufferLine[col-2] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine[col] - avg_G3_q) , std_prec )
+ min(abs(ByerrBufferLine_pls1[col-1] - avg_G3_q) , std_prec ) + 2)>>2;
G3_std_q_cp1 = (int)( min(abs(ByerrBufferLine_min1[col+1] - avg_G3_q) , std_prec )
+ min(abs(ByerrBufferLine[col] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine[col+2] - avg_G3_q) , std_prec )
+ min(abs(ByerrBufferLine_pls1[col+1] - avg_G3_q) , std_prec ) + 2)>>2;
}
else{
if(FC(row,col)== Red){
Gr_std_q = (682*(int)(min(abs(ByerrBufferLine_min2[col-1] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_min2[col+1] - avg_gr_q) , std_prec )
+min(abs(ByerrBufferLine[col-1] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine[col+1] - avg_gr_q) , std_prec )
+min(abs(ByerrBufferLine_pls2[col-1] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_pls2[col+1] - avg_gr_q) , std_prec )))>>12;
Gb_std_q = (682*(int)(min(abs(ByerrBufferLine_min1[col-2] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col+2] - avg_gb_q) , std_prec )
+min(abs(ByerrBufferLine_pls1[col-2] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col+2] - avg_gb_q) , std_prec )))>>12;
}
else{
Gr_std_q = (682*(int)(min(abs(ByerrBufferLine_min1[col-2] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col+2] - avg_gr_q) , std_prec )
+min(abs(ByerrBufferLine_pls1[col-2] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col] - avg_gr_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col+2] - avg_gr_q) , std_prec )))>>12;
Gb_std_q = (682*(int)(min(abs(ByerrBufferLine_min2[col-1] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_min2[col+1] - avg_gb_q) , std_prec )
+min(abs(ByerrBufferLine[col-1] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine[col+1] - avg_gb_q) , std_prec )
+min(abs(ByerrBufferLine_pls2[col-1] - avg_gb_q) , std_prec ) + min(abs(ByerrBufferLine_pls2[col+1] - avg_gb_q) , std_prec )))>>12;
}
G3_std_q = (int)( min(abs(ByerrBufferLine_min1[col] - avg_G3_q) , std_prec )
+ min(abs(ByerrBufferLine[col-1] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine[col+1] - avg_G3_q) , std_prec )
+ min(abs(ByerrBufferLine_pls1[col] - avg_G3_q) , std_prec ) + 2)>>2;
G_std_q = (341*(int)(min(abs(ByerrBufferLine_min2[col-1] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_min2[col+1] - avg_G_q) , std_prec )
+min(abs(ByerrBufferLine_min1[col-2] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col+2] - avg_G_q) , std_prec )
+min(abs(ByerrBufferLine[col-1] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine[col+1] - avg_G_q) , std_prec )
+min(abs(ByerrBufferLine_pls1[col-2] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col+2] - avg_G_q) , std_prec )
+min(abs(ByerrBufferLine_pls2[col-1] - avg_G_q) , std_prec ) + min(abs(ByerrBufferLine_pls2[col+1] - avg_G_q) , std_prec )))>>12;
/// Research FCC, checker removal
G3_std_q_rm1 = (819*(int)(min(abs(ByerrBufferLine_min2[col-1] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine_min2[col+1] - avg_G3_q) , std_prec )
+min(abs(ByerrBufferLine_min1[col] - avg_G3_q) , std_prec )
+min(abs(ByerrBufferLine[col-1] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine[col+1] - avg_G3_q) , std_prec )))>>12;
G3_std_q_rp1 = (819*(int)(min(abs(ByerrBufferLine[col-1] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine[col+1] - avg_G3_q) , std_prec )
+min(abs(ByerrBufferLine_pls1[col] - avg_G3_q) , std_prec )
+min(abs(ByerrBufferLine_pls2[col-1] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine_pls2[col+1] - avg_G3_q) , std_prec )))>>12;
G3_std_q_cm1 = (819*(int)(min(abs(ByerrBufferLine_min1[col-2] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col] - avg_G3_q) , std_prec )
+min(abs(ByerrBufferLine[col-1] - avg_G3_q) , std_prec )
+min(abs(ByerrBufferLine_pls1[col-2] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col] - avg_G3_q) , std_prec )))>>12;
G3_std_q_cp1 = (819*(int)(min(abs(ByerrBufferLine_min1[col] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine_min1[col+2] - avg_G3_q) , std_prec )
+min(abs(ByerrBufferLine[col+1] - avg_G3_q) , std_prec )
+min(abs(ByerrBufferLine_pls1[col] - avg_G3_q) , std_prec ) + min(abs(ByerrBufferLine_pls1[col+2] - avg_G3_q) , std_prec )))>>12;
}
AvgG_std_q = (Gr_std_q + Gb_std_q + 1)>>1;
//// Diagonal
int metric_diag, metric_overshrpn2;
int Y_est[3][3];
int G1, G2, G3, G4;
int cu_diag1_out, cu_diag2_out, cu_diag3_out;
int ClpdW;
////////////////// STEP 1 ///////////////////
/////////////////////////////////////////////////
////// Green clip weigh (Wgclp) calculation
////// calculated only in even rows like avg between even and odd rows
/////////////////////////////////////////////////
int G_std_q_array[7];
// calculate w_gclp for each column independently based on central row and store it in array
for (int gl_shift_c = - 3 ; gl_shift_c <= 3 ; ++gl_shift_c )
{
int col_w = col+gl_shift_c;
for(int shift_r = - 1 ; shift_r <= 1 ; ++shift_r )
{
for(int shift_c = - 1 ; shift_c <= 1 ; ++shift_c )
{
int row_ind = max(min(row+shift_r,m_Height-2+abs(row+shift_r)& 1),abs(row+shift_r)& 1);
int col_ind = col + (shift_c+gl_shift_c);
// buf_r = min( max(row+1,abs((row+1)& 1)) , m_Height - 2 + abs((row+1)& 1) );buf_r = min( max(row-1,abs((row-1)& 1)) , m_Height - 2 + abs((row-1)& 1) );
Y_est[shift_r+1][shift_c+1] = ( pInImg->m_R[max(row_ind-1,abs(row_ind-1)&1)][col_ind-1] + pInImg->m_R[max(row_ind-1,abs(row_ind-1)&1)][col_ind]*2 + pInImg->m_R[max(row_ind-1,abs(row_ind-1)&1)][col_ind+1]
+ pInImg->m_R[row_ind][col_ind-1]*2 + pInImg->m_R[row_ind][col_ind]*4 + pInImg->m_R[row_ind][col_ind+1]*2
+ pInImg->m_R[min(row_ind+1,m_Height-2+abs(row_ind+1)&1)][col_ind-1] + pInImg->m_R[min(row_ind+1,m_Height-2+abs(row_ind+1)&1)][col_ind]*2 + pInImg->m_R[min(row_ind+1,m_Height-2+abs(row_ind+1)&1)][col_ind+1] + 8)>>4;
}
}
metric_diag = min((abs(Y_est[0][0]-Y_est[1][1]) + abs(Y_est[1][1]-Y_est[2][2]) + abs(Y_est[0][1]-Y_est[1][2]) + abs(Y_est[1][0]-Y_est[2][1]) +2 )>>2,
(abs(Y_est[0][2]-Y_est[1][1]) + abs(Y_est[1][1]-Y_est[2][0]) + abs(Y_est[0][1]-Y_est[1][0]) + abs(Y_est[1][2]-Y_est[2][1]) +2 )>>2 );
metric_diag = min(metric_diag,(1<<HWP_DIAG_METR)-1);
// calculate metric_diag2
G1 = MAX( (abs(pInImg->m_R[max(row-2,abs(row-2)&1)][col_w-2] - pInImg->m_R[row][col_w]) + abs(pInImg->m_R[row][col_w - 2] - pInImg->m_R[min(row + 2,m_Height-2+(row + 2)%1)][col_w]) +
abs(pInImg->m_R[row][col_w] - pInImg->m_R[min(row + 2,m_Height-2+(row + 2)%1)][col_w + 2]) + abs(pInImg->m_R[max(row - 2,abs(row-2)&1)][col_w] - pInImg->m_R[row][col_w + 2]) + (1<<(HWP_SH_DOWN+1)))>>(2+HWP_SH_DOWN) ,
(abs(pInImg->m_R[max(row-2,abs(row-2)&1)][col_w] - pInImg->m_R[row][col_w - 2]) + abs(pInImg->m_R[max(row - 2,abs(row-2)&1)][col_w + 2] - pInImg->m_R[row][col_w])+
abs( pInImg->m_R[row][col_w]-pInImg->m_R[min(row + 2,m_Height-2+(row + 2)%1)][col_w-2]) + abs(pInImg->m_R[row][col_w+2]-pInImg->m_R[min(row+2,m_Height-2+(row + 2)%1)][col_w]) + (1<<(HWP_SH_DOWN+1)))>>(2+HWP_SH_DOWN));
G2 = MAX( (abs(pInImg->m_R[max(row-1,abs(row-1)&1)][col_w-1] - pInImg->m_R[min(row + 1,m_Height-2+(row + 1)%1)][col_w+1]) + (1<<(HWP_SH_DOWN-1)))>>HWP_SH_DOWN,
(abs(pInImg->m_R[max(row-1,abs(row-1)&1)][col_w+1] - pInImg->m_R[min(row + 1,m_Height-2+(row + 1)%1)][col_w-1]) + (1<<(HWP_SH_DOWN-1)))>>HWP_SH_DOWN);
G3 = MAX( (abs(pInImg->m_R[max(row-2,abs(row-2)&1)][col_w-1] - pInImg->m_R[row][col_w+1]) + abs(pInImg->m_R[row][col_w-1]-pInImg->m_R[min(row + 2,m_Height-1)][col_w+1]) + (1<<HWP_SH_DOWN) )>>
....