fn cdef_find_dir(img: &[u16], stride: usize, var: &mut i32, coeff_shift: i32) -> i32 {
    let mut cost: [i32; 8] = [0; 8];
    let mut partial: [[i32; 15]; 8] = [[0; 15]; 8];
    let mut best_cost: i32 = 0;
    let mut best_dir = 0;
    for i in 0..8 {
        for j in 0..8 {
            // We subtract 128 here to reduce the maximum range of the squared
            // partial sums. 
            debug_assert!((img[i * stride + j] >> coeff_shift) <= 255);
            let x = (img[i * stride + j] as i32 >> coeff_shift) - 128;
            partial[0][i + j] += x;
            partial[1][i + j / 2] += x;
            partial[2][i] += x;
            partial[3][3 + i - j / 2] += x;
            partial[4][7 + i - j] += x;
            partial[5][3 - i / 2 + j] += x;
            partial[6][j] += x;
            partial[7][i / 2 + j] += x;
        }
    }
    for i in 0..8 {
        cost[2] += partial[2][i] * partial[2][i];
        cost[6] += partial[6][i] * partial[6][i];
    }
    cost[2] *= CDEF_DIV_TABLE[8];
    cost[6] *= CDEF_DIV_TABLE[8];
    for i in 0..7 {
        cost[0] += (partial[0][i]*partial[0][i] +
                    partial[0][14-i]*partial[0][14-i]) * CDEF_DIV_TABLE[i + 1];
        cost[4] += (partial[4][i]*partial[4][i] +
                    partial[4][14-i]*partial[4][14-i]) * CDEF_DIV_TABLE[i + 1];
    }
    cost[0] += partial[0][7] * partial[0][7] * CDEF_DIV_TABLE[8];
    cost[4] += partial[4][7] * partial[4][7] * CDEF_DIV_TABLE[8];
    let mut i = 1;
    while i<8 {
        for j in 0..5 {
            cost[i] += partial[i][3 + j] * partial[i][3 + j];
        }
        cost[i] *= CDEF_DIV_TABLE[8];
        for j in 0..3 {
            cost[i] += (partial[i][j]*partial[i][j] +
                        partial[i][10-j]*partial[i][10-j]) * CDEF_DIV_TABLE[2 * j + 2];
        }
        i+=2;
    }
    for i in 0..8 {
        if cost[i] > best_cost {
            best_cost = cost[i];
            best_dir = i;
        }
    }
    // Difference between the optimal variance and the variance along the
    // orthogonal direction. Again, the sum(x^2) terms cancel out. 
    // We'd normally divide by 840, but dividing by 1024 is close enough
    // for what we're going to do with this. */
    *var = (best_cost - cost[(best_dir + 4) & 7]) >> 10;
        
    best_dir as i32
}
// Detect direction. 0 means 45-degree up-right, 2 is horizontal, and so on.
// The search minimizes the weighted variance along all the lines in a
// particular direction, i.e. the squared error between the input and a
// "predicted" block where each pixel is replaced by the average along a line
// in a particular direction. Since each direction have the same sum(x^2) term,
// that term is never computed. See Section 2, step 2, of:
// http://jmvalin.ca/notes/intra_paint.pdf
Detect direction. 0 means 45-degree up-right, 2 is horizontal, and so on.
The search minimizes the weighted variance along all the lines in a
particular direction, i.e. the squared error between the input and a
"predicted" block where each pixel is replaced by the average along a line
in a particular direction. Since each direction have the same sum(x^2) term,
that term is never computed.
Detect direction . 0 means 45 - degree up - right 2 is horizontal and so on . The search minimizes the weighted variance along all the lines in a particular direction i . e . the squared error between the input and a " predicted " block where each pixel is replaced by the average along a line in a particular direction . Since each direction have the same sum ( x^2 ) term that term is never computed .
fn cdef_find_dir(img: &[u16], stride: usize, var: &mut i32, coeff_shift: i32) -> i32 {
    let mut cost: [i32; 8] = [0; 8];
    let mut partial: [[i32; 15]; 8] = [[0; 15]; 8];
    let mut best_cost: i32 = 0;
    let mut best_dir = 0;
    for i in 0..8 {
        for j in 0..8 {
            // We subtract 128 here to reduce the maximum range of the squared
            // partial sums. 
            debug_assert!((img[i * stride + j] >> coeff_shift) <= 255);
            let x = (img[i * stride + j] as i32 >> coeff_shift) - 128;
            partial[0][i + j] += x;
            partial[1][i + j / 2] += x;
            partial[2][i] += x;
            partial[3][3 + i - j / 2] += x;
            partial[4][7 + i - j] += x;
            partial[5][3 - i / 2 + j] += x;
            partial[6][j] += x;
            partial[7][i / 2 + j] += x;
        }
    }
    for i in 0..8 {
        cost[2] += partial[2][i] * partial[2][i];
        cost[6] += partial[6][i] * partial[6][i];
    }
    cost[2] *= CDEF_DIV_TABLE[8];
    cost[6] *= CDEF_DIV_TABLE[8];
    for i in 0..7 {
        cost[0] += (partial[0][i]*partial[0][i] +
                    partial[0][14-i]*partial[0][14-i]) * CDEF_DIV_TABLE[i + 1];
        cost[4] += (partial[4][i]*partial[4][i] +
                    partial[4][14-i]*partial[4][14-i]) * CDEF_DIV_TABLE[i + 1];
    }
    cost[0] += partial[0][7] * partial[0][7] * CDEF_DIV_TABLE[8];
    cost[4] += partial[4][7] * partial[4][7] * CDEF_DIV_TABLE[8];
    let mut i = 1;
    while i<8 {
        for j in 0..5 {
            cost[i] += partial[i][3 + j] * partial[i][3 + j];
        }
        cost[i] *= CDEF_DIV_TABLE[8];
        for j in 0..3 {
            cost[i] += (partial[i][j]*partial[i][j] +
                        partial[i][10-j]*partial[i][10-j]) * CDEF_DIV_TABLE[2 * j + 2];
        }
        i+=2;
    }
    for i in 0..8 {
        if cost[i] > best_cost {
            best_cost = cost[i];
            best_dir = i;
        }
    }
    // Difference between the optimal variance and the variance along the
    // orthogonal direction. Again, the sum(x^2) terms cancel out. 
    // We'd normally divide by 840, but dividing by 1024 is close enough
    // for what we're going to do with this. */
    *var = (best_cost - cost[(best_dir + 4) & 7]) >> 10;
    best_dir as i32
}
fn cdef_find_dir ( img : & [ u16 ] , stride : usize , var : & mut i32 , coeff_shift : i32 ) -> i32 { let mut cost : [ i32 ; 8 ] = [ 0 ; 8 ] ; let mut partial : [ [ i32 ; 15 ] ; 8 ] = [ [ 0 ; 15 ] ; 8 ] ; let mut best_cost : i32 = 0 ; let mut best_dir = 0 ; for i in 0 .. 8 { for j in 0 .. 8 { // We subtract 128 here to reduce the maximum range of the squared // partial sums.  debug_assert ! ( ( img [ i stride j ] coeff_shift ) 255 ) ; let x = ( img [ i * stride + j ] as i32 >> coeff_shift ) - 128 ; partial [ 0 ] [ i + j ] += x ; partial [ 1 ] [ i + j / 2 ] += x ; partial [ 2 ] [ i ] += x ; partial [ 3 ] [ 3 + i - j / 2 ] += x ; partial [ 4 ] [ 7 + i - j ] += x ; partial [ 5 ] [ 3 - i / 2 + j ] += x ; partial [ 6 ] [ j ] += x ; partial [ 7 ] [ i / 2 + j ] += x ; } } for i in 0 .. 8 { cost [ 2 ] += partial [ 2 ] [ i ] * partial [ 2 ] [ i ] ; cost [ 6 ] += partial [ 6 ] [ i ] * partial [ 6 ] [ i ] ; } cost [ 2 ] *= CDEF_DIV_TABLE [ 8 ] ; cost [ 6 ] *= CDEF_DIV_TABLE [ 8 ] ; for i in 0 .. 7 { cost [ 0 ] += ( partial [ 0 ] [ i ] * partial [ 0 ] [ i ] + partial [ 0 ] [ 14 - i ] * partial [ 0 ] [ 14 - i ] ) * CDEF_DIV_TABLE [ i + 1 ] ; cost [ 4 ] += ( partial [ 4 ] [ i ] * partial [ 4 ] [ i ] + partial [ 4 ] [ 14 - i ] * partial [ 4 ] [ 14 - i ] ) * CDEF_DIV_TABLE [ i + 1 ] ; } cost [ 0 ] += partial [ 0 ] [ 7 ] * partial [ 0 ] [ 7 ] * CDEF_DIV_TABLE [ 8 ] ; cost [ 4 ] += partial [ 4 ] [ 7 ] * partial [ 4 ] [ 7 ] * CDEF_DIV_TABLE [ 8 ] ; let mut i = 1 ; while i < 8 { for j in 0 .. 5 { cost [ i ] += partial [ i ] [ 3 + j ] * partial [ i ] [ 3 + j ] ; } cost [ i ] *= CDEF_DIV_TABLE [ 8 ] ; for j in 0 .. 3 { cost [ i ] += ( partial [ i ] [ j ] * partial [ i ] [ j ] + partial [ i ] [ 10 - j ] * partial [ i ] [ 10 - j ] ) * CDEF_DIV_TABLE [ 2 * j + 2 ] ; } i += 2 ; } for i in 0 .. 8 { if cost [ i ] > best_cost { best_cost = cost [ i ] ; best_dir = i ; } } // Difference between the optimal variance and the variance along the // orthogonal direction. Again, the sum(x^2) terms cancel out.  // We'd normally divide by 840, but dividing by 1024 is close enough // for what we're going to do with this. */ * var = ( best_cost - cost [ ( best_dir + 4 ) & 7 ] ) >> 10 ; best_dir as i32 }
Detect direction.
Detect direction .
img
&[u16]
stride
usize
var
&mut i32
coeff_shift
i32