function [Pix_out] = correlation2_2(Pix_in, threshold_br)
%
%CORRELATION2_2   Performs correlation-based color interpolation 
%                 Kernel 2.2
%
%   Usage: [Pix_out] = correlation2_2(Pix_in, threshold_br)
%
%      Pix_out       = Interpolated image 3D array output
%                      Valid range: 0 <= Pix_out(:,:,:) <= 255 (8 bits)
%
%      Pix_in        = Bayer CFA RGB image 3D array input
%                      Valid range: 0 <= Pix_in(:,:,:) <= 255 (8 bits)
%
%      threshold_br  = Activation threshold for correlation-based 
%                      adjustment to directed-linear-interpolated blue
%                      and red pixels on red and blue centers
%                      The directed-linear interpolation is the average
%                      of the two downward-sloping, or the two upward-
%                      sloping, or all four red or blue pixels, 
%                      depending on the maximum pattern present
%                      Valid range: 0 <= threshold_br <= 765 (3 x 255)
%
%   Assumptions: Pix_in has the following format -
%
%        1 2 3 4 . . . w      d   Color Plane
%     ------------------     --- -------------
%   1 |  G R G R . . . R      1   R
%   2 |  B G B G . . . G      2   G
%   3 |  G R G R . . . R      3   B
%   4 |  B G B G . . . G
%   . |  . . . . . . . R
%   . |  . . . . . . . G
%   . |  . . . . . . . R
%   h |  B G B G B G B G
%
%   Author:    Gary Embler
%   Email:     gary_embler@agilent.com
%   Date:      05/19/01
%   Reference: "Correlation-Based Color Mosaic Interpolation Using a
%              Connectionist Approach", G. Embler
%
%   Copyright (c) 2001 by Gary Embler
%

%======================================================================
% Initialization
%----------------------------------------------------------------------

[h, w, d]   = size(Pix_in);

%======================================================================
% Pixels Pass-Through
%----------------------------------------------------------------------

Pix_out = Pix_in;

%======================================================================
% Common Arrays
%----------------------------------------------------------------------

% Pixel Input Complements

Pix_in_n(1:2:(h-1), 2:2:w,     1)   = ...                        % R_n
   bitcmp(Pix_in(1:2:(h-1), 2:2:w,     1), 8);                   % R

Pix_in_n(1:2:(h-1), 1:2:(w-1), 2)   = ...                        % GR_n
   bitcmp(Pix_in(1:2:(h-1), 1:2:(w-1), 2), 8);                   % GR

Pix_in_n(2:2:h,     2:2:w,     2)   = ...                        % GB_n
   bitcmp(Pix_in(2:2:h,     2:2:w,     2), 8);                   % GB

Pix_in_n(2:2:h,     1:2:(w-1), 3)   = ...                        % B_n
   bitcmp(Pix_in(2:2:h,     1:2:(w-1), 3), 8);                   % B

%======================================================================
% R, B-Center B, R-Interpolator - Kernel 2
%----------------------------------------------------------------------

% Kernel 2 R-Center B Sums

Pix_sum(:,:,1,1,1)      = Pix_in_n(4:2:h,     3:2:(w-1), 3)... % B3_n
                        + Pix_in_n(4:2:h,     1:2:(w-3), 3)... % B2_n
                        + Pix_in_n(2:2:(h-2), 1:2:(w-3), 3)... % B1_n
                        + Pix_in_n(2:2:(h-2), 3:2:(w-1), 3);   % B0_n

Pix_sum(:,:,1,1,2)      = Pix_in_n(4:2:h,     3:2:(w-1), 3)... % B3_n
                        + Pix_in_n(4:2:h,     1:2:(w-3), 3)... % B2_n
                        + Pix_in_n(2:2:(h-2), 1:2:(w-3), 3)... % B1_n
                        + Pix_in(  2:2:(h-2), 3:2:(w-1), 3);   % B0

Pix_sum(:,:,1,1,3)      = Pix_in_n(4:2:h,     3:2:(w-1), 3)... % B3_n
                        + Pix_in_n(4:2:h,     1:2:(w-3), 3)... % B2_n
                        + Pix_in(  2:2:(h-2), 1:2:(w-3), 3)... % B1
                        + Pix_in_n(2:2:(h-2), 3:2:(w-1), 3);   % B0_n

Pix_sum(:,:,1,1,4)      = Pix_in_n(4:2:h,     3:2:(w-1), 3)... % B3_n
                        + Pix_in_n(4:2:h,     1:2:(w-3), 3)... % B2_n
                        + Pix_in(  2:2:(h-2), 1:2:(w-3), 3)... % B1
                        + Pix_in(  2:2:(h-2), 3:2:(w-1), 3);   % B0

Pix_sum(:,:,1,1,5)      = Pix_in_n(4:2:h,     3:2:(w-1), 3)... % B3_n
                        + Pix_in(  4:2:h,     1:2:(w-3), 3)... % B2
                        + Pix_in_n(2:2:(h-2), 1:2:(w-3), 3)... % B1_n
                        + Pix_in_n(2:2:(h-2), 3:2:(w-1), 3);   % B0_n

Pix_sum(:,:,1,1,6)      = Pix_in_n(4:2:h,     3:2:(w-1), 3)... % B3_n
                        + Pix_in(  4:2:h,     1:2:(w-3), 3)... % B2
                        + Pix_in_n(2:2:(h-2), 1:2:(w-3), 3)... % B1_n
                        + Pix_in(  2:2:(h-2), 3:2:(w-1), 3);   % B0

Pix_sum(:,:,1,1,7)      = Pix_in_n(4:2:h,     3:2:(w-1), 3)... % B3_n
                        + Pix_in(  4:2:h,     1:2:(w-3), 3)... % B2
                        + Pix_in(  2:2:(h-2), 1:2:(w-3), 3)... % B1
                        + Pix_in_n(2:2:(h-2), 3:2:(w-1), 3);   % B0_n

Pix_sum(:,:,1,1,8)      = Pix_in_n(4:2:h,     3:2:(w-1), 3)... % B3_n
                        + Pix_in(  4:2:h,     1:2:(w-3), 3)... % B2
                        + Pix_in(  2:2:(h-2), 1:2:(w-3), 3)... % B1
                        + Pix_in(  2:2:(h-2), 3:2:(w-1), 3);   % B0

Pix_sum(:,:,1,1,9)      = Pix_in(  4:2:h,     3:2:(w-1), 3)... % B3
                        + Pix_in_n(4:2:h,     1:2:(w-3), 3)... % B2_n
                        + Pix_in_n(2:2:(h-2), 1:2:(w-3), 3)... % B1_n
                        + Pix_in_n(2:2:(h-2), 3:2:(w-1), 3);   % B0_n

Pix_sum(:,:,1,1,10)     = Pix_in(  4:2:h,     3:2:(w-1), 3)... % B3
                        + Pix_in_n(4:2:h,     1:2:(w-3), 3)... % B2_n
                        + Pix_in_n(2:2:(h-2), 1:2:(w-3), 3)... % B1_n
                        + Pix_in(  2:2:(h-2), 3:2:(w-1), 3);   % B0

Pix_sum(:,:,1,1,11)     = Pix_in(  4:2:h,     3:2:(w-1), 3)... % B3
                        + Pix_in_n(4:2:h,     1:2:(w-3), 3)... % B2_n
                        + Pix_in(  2:2:(h-2), 1:2:(w-3), 3)... % B1
                        + Pix_in_n(2:2:(h-2), 3:2:(w-1), 3);   % B0_n

Pix_sum(:,:,1,1,12)     = Pix_in(  4:2:h,     3:2:(w-1), 3)... % B3
                        + Pix_in_n(4:2:h,     1:2:(w-3), 3)... % B2_n
                        + Pix_in(  2:2:(h-2), 1:2:(w-3), 3)... % B1
                        + Pix_in(  2:2:(h-2), 3:2:(w-1), 3);   % B0

Pix_sum(:,:,1,1,13)     = Pix_in(  4:2:h,     3:2:(w-1), 3)... % B3
                        + Pix_in(  4:2:h,     1:2:(w-3), 3)... % B2
                        + Pix_in_n(2:2:(h-2), 1:2:(w-3), 3)... % B1_n
                        + Pix_in_n(2:2:(h-2), 3:2:(w-1), 3);   % B0_n

Pix_sum(:,:,1,1,14)     = Pix_in(  4:2:h,     3:2:(w-1), 3)... % B3
                        + Pix_in(  4:2:h,     1:2:(w-3), 3)... % B2
                        + Pix_in_n(2:2:(h-2), 1:2:(w-3), 3)... % B1_n
                        + Pix_in(  2:2:(h-2), 3:2:(w-1), 3);   % B0

Pix_sum(:,:,1,1,15)     = Pix_in(  4:2:h,     3:2:(w-1), 3)... % B3
                        + Pix_in(  4:2:h,     1:2:(w-3), 3)... % B2
                        + Pix_in(  2:2:(h-2), 1:2:(w-3), 3)... % B1
                        + Pix_in_n(2:2:(h-2), 3:2:(w-1), 3);   % B0_n

Pix_sum(:,:,1,1,16)     = Pix_in(  4:2:h,     3:2:(w-1), 3)... % B3
                        + Pix_in(  4:2:h,     1:2:(w-3), 3)... % B2
                        + Pix_in(  2:2:(h-2), 1:2:(w-3), 3)... % B1
                        + Pix_in(  2:2:(h-2), 3:2:(w-1), 3);   % B0


% Kernel 2 R-Center G Sums

Pix_sum(:,:,1,2,1)      = Pix_in_n(3:2:(h-1), 1:2:(w-3), 2)... % G2_n
                        + Pix_in(  3:2:(h-1), 3:2:(w-1), 2);   % G0

Pix_sum(:,:,1,2,2)      = Pix_in(  3:2:(h-1), 1:2:(w-3), 2)... % G2
                        + Pix_in_n(3:2:(h-1), 3:2:(w-1), 2);   % G0_n

Pix_sum(:,:,1,2,3)      = Pix_in_n(4:2:h,     2:2:(w-2), 2)... % G3_n
                        + Pix_in(  2:2:(h-2), 2:2:(w-2), 2);   % G1

Pix_sum(:,:,1,2,4)      = Pix_in(  4:2:h,     2:2:(w-2), 2)... % G3
                        + Pix_in_n(2:2:(h-2), 2:2:(w-2), 2);   % G1_n


% Kernel 2 B-Center R Sums

Pix_sum(:,:,2,1,1)      = Pix_in_n(3:2:(h-1), 4:2:w,     1)... % R3_n
                        + Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R2_n
                        + Pix_in_n(1:2:(h-3), 2:2:(w-2), 1)... % R1_n
                        + Pix_in_n(1:2:(h-3), 4:2:w,     1);   % R0_n

Pix_sum(:,:,2,1,2)      = Pix_in_n(3:2:(h-1), 4:2:w,     1)... % R3_n
                        + Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R2_n
                        + Pix_in_n(1:2:(h-3), 2:2:(w-2), 1)... % R1_n
                        + Pix_in(  1:2:(h-3), 4:2:w,     1);   % R0

Pix_sum(:,:,2,1,3)      = Pix_in_n(3:2:(h-1), 4:2:w,     1)... % R3_n
                        + Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R2_n
                        + Pix_in(  1:2:(h-3), 2:2:(w-2), 1)... % R1
                        + Pix_in_n(1:2:(h-3), 4:2:w,     1);   % R0_n

Pix_sum(:,:,2,1,4)      = Pix_in_n(3:2:(h-1), 4:2:w,     1)... % R3_n
                        + Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R2_n
                        + Pix_in(  1:2:(h-3), 2:2:(w-2), 1)... % R1
                        + Pix_in(  1:2:(h-3), 4:2:w,     1);   % R0

Pix_sum(:,:,2,1,5)      = Pix_in_n(3:2:(h-1), 4:2:w,     1)... % R3_n
                        + Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R2
                        + Pix_in_n(1:2:(h-3), 2:2:(w-2), 1)... % R1_n
                        + Pix_in_n(1:2:(h-3), 4:2:w,     1);   % R0_n

Pix_sum(:,:,2,1,6)      = Pix_in_n(3:2:(h-1), 4:2:w,     1)... % R3_n
                        + Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R2
                        + Pix_in_n(1:2:(h-3), 2:2:(w-2), 1)... % R1_n
                        + Pix_in(  1:2:(h-3), 4:2:w,     1);   % R0

Pix_sum(:,:,2,1,7)      = Pix_in_n(3:2:(h-1), 4:2:w,     1)... % R3_n
                        + Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R2
                        + Pix_in(  1:2:(h-3), 2:2:(w-2), 1)... % R1
                        + Pix_in_n(1:2:(h-3), 4:2:w,     1);   % R0_n

Pix_sum(:,:,2,1,8)      = Pix_in_n(3:2:(h-1), 4:2:w,     1)... % R3_n
                        + Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R2
                        + Pix_in(  1:2:(h-3), 2:2:(w-2), 1)... % R1
                        + Pix_in(  1:2:(h-3), 4:2:w,     1);   % R0

Pix_sum(:,:,2,1,9)      = Pix_in(  3:2:(h-1), 4:2:w,     1)... % R3
                        + Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R2_n
                        + Pix_in_n(1:2:(h-3), 2:2:(w-2), 1)... % R1_n
                        + Pix_in_n(1:2:(h-3), 4:2:w,     1);   % R0_n

Pix_sum(:,:,2,1,10)     = Pix_in(  3:2:(h-1), 4:2:w,     1)... % R3
                        + Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R2_n
                        + Pix_in_n(1:2:(h-3), 2:2:(w-2), 1)... % R1_n
                        + Pix_in(  1:2:(h-3), 4:2:w,     1);   % R0

Pix_sum(:,:,2,1,11)     = Pix_in(  3:2:(h-1), 4:2:w,     1)... % R3
                        + Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R2_n
                        + Pix_in(  1:2:(h-3), 2:2:(w-2), 1)... % R1
                        + Pix_in_n(1:2:(h-3), 4:2:w,     1);   % R0_n

Pix_sum(:,:,2,1,12)     = Pix_in(  3:2:(h-1), 4:2:w,     1)... % R3
                        + Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R2_n
                        + Pix_in(  1:2:(h-3), 2:2:(w-2), 1)... % R1
                        + Pix_in(  1:2:(h-3), 4:2:w,     1);   % R0

Pix_sum(:,:,2,1,13)     = Pix_in(  3:2:(h-1), 4:2:w,     1)... % R3
                        + Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R2
                        + Pix_in_n(1:2:(h-3), 2:2:(w-2), 1)... % R1_n
                        + Pix_in_n(1:2:(h-3), 4:2:w,     1);   % R0_n

Pix_sum(:,:,2,1,14)     = Pix_in(  3:2:(h-1), 4:2:w,     1)... % R3
                        + Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R2
                        + Pix_in_n(1:2:(h-3), 2:2:(w-2), 1)... % R1_n
                        + Pix_in(  1:2:(h-3), 4:2:w,     1);   % R0

Pix_sum(:,:,2,1,15)     = Pix_in(  3:2:(h-1), 4:2:w,     1)... % R3
                        + Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R2
                        + Pix_in(  1:2:(h-3), 2:2:(w-2), 1)... % R1
                        + Pix_in_n(1:2:(h-3), 4:2:w,     1);   % R0_n

Pix_sum(:,:,2,1,16)     = Pix_in(  3:2:(h-1), 4:2:w,     1)... % R3
                        + Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R2
                        + Pix_in(  1:2:(h-3), 2:2:(w-2), 1)... % R1
                        + Pix_in(  1:2:(h-3), 4:2:w,     1);   % R0


% Kernel 2 B-Center G Sums

Pix_sum(:,:,2,2,1)      = Pix_in_n(2:2:(h-2), 2:2:(w-2), 2)... % G2_n
                        + Pix_in(  2:2:(h-2), 4:2:w,     2);   % G0

Pix_sum(:,:,2,2,2)      = Pix_in(  2:2:(h-2), 2:2:(w-2), 2)... % G2
                        + Pix_in_n(2:2:(h-2), 4:2:w,     2);   % G0_n

Pix_sum(:,:,2,2,3)      = Pix_in_n(3:2:(h-1), 3:2:(w-1), 2)... % G3_n
                        + Pix_in(  1:2:(h-3), 3:2:(w-1), 2);   % G1

Pix_sum(:,:,2,2,4)      = Pix_in(  3:2:(h-1), 3:2:(w-1), 2)... % G3
                        + Pix_in_n(1:2:(h-3), 3:2:(w-1), 2);   % G1_n


% Kernel 2 R-Center Negative Select Sums

Sel_sum_n(:,:,1,1)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,1);

Sel_sum_n(:,:,1,2)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,2);

Sel_sum_n(:,:,1,3)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,3);

Sel_sum_n(:,:,1,4)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,4);

Sel_sum_n(:,:,1,5)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,5);

Sel_sum_n(:,:,1,6)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,6);

Sel_sum_n(:,:,1,7)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,7);

Sel_sum_n(:,:,1,8)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,8);

Sel_sum_n(:,:,1,9)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,9);

Sel_sum_n(:,:,1,10)     = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,10);

Sel_sum_n(:,:,1,11)     = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,11);

Sel_sum_n(:,:,1,12)     = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,12);

Sel_sum_n(:,:,1,13)     = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,13);

Sel_sum_n(:,:,1,14)     = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,14);

Sel_sum_n(:,:,1,15)     = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,15);

Sel_sum_n(:,:,1,16)     = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,1,16);


% Kernel 2 R-Center Positive Select Sums

Sel_sum(:,:,1,1)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,1);

Sel_sum(:,:,1,2)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,2);

Sel_sum(:,:,1,3)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,3);

Sel_sum(:,:,1,4)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,4);

Sel_sum(:,:,1,5)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,5);

Sel_sum(:,:,1,6)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,6);

Sel_sum(:,:,1,7)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,7);

Sel_sum(:,:,1,8)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,8);

Sel_sum(:,:,1,9)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,9);

Sel_sum(:,:,1,10)       = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,10);

Sel_sum(:,:,1,11)       = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,11);

Sel_sum(:,:,1,12)       = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,12);

Sel_sum(:,:,1,13)       = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,13);

Sel_sum(:,:,1,14)       = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,14);

Sel_sum(:,:,1,15)       = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,15);

Sel_sum(:,:,1,16)       = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,1,16);


% Kernel 2 B-Center Negative Select Sums

Sel_sum_n(:,:,2,1)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,1);

Sel_sum_n(:,:,2,2)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,2);

Sel_sum_n(:,:,2,3)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,3);

Sel_sum_n(:,:,2,4)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,4);

Sel_sum_n(:,:,2,5)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,5);

Sel_sum_n(:,:,2,6)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,6);

Sel_sum_n(:,:,2,7)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,7);

Sel_sum_n(:,:,2,8)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,8);

Sel_sum_n(:,:,2,9)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,9);

Sel_sum_n(:,:,2,10)     = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,10);

Sel_sum_n(:,:,2,11)     = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,11);

Sel_sum_n(:,:,2,12)     = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,12);

Sel_sum_n(:,:,2,13)     = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,13);

Sel_sum_n(:,:,2,14)     = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,14);

Sel_sum_n(:,:,2,15)     = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,15);

Sel_sum_n(:,:,2,16)     = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,1,16);


% Kernel 2 B-Center Positive Select Sums

Sel_sum(:,:,2,1)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,1);

Sel_sum(:,:,2,2)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,2);

Sel_sum(:,:,2,3)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,3);

Sel_sum(:,:,2,4)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,4);

Sel_sum(:,:,2,5)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,5);

Sel_sum(:,:,2,6)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,6);

Sel_sum(:,:,2,7)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,7);

Sel_sum(:,:,2,8)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,8);

Sel_sum(:,:,2,9)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,9);

Sel_sum(:,:,2,10)       = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,10);

Sel_sum(:,:,2,11)       = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,11);

Sel_sum(:,:,2,12)       = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,12);

Sel_sum(:,:,2,13)       = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,13);

Sel_sum(:,:,2,14)       = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,14);

Sel_sum(:,:,2,15)       = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,15);

Sel_sum(:,:,2,16)       = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,1,16);


% Kernel 2 R-Center Negative Adjustment Sums

Adj_sum_n(:,:,1,1)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,2,1);

Adj_sum_n(:,:,1,2)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,2,2);

Adj_sum_n(:,:,1,3)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,2,3);

Adj_sum_n(:,:,1,4)      = Pix_in_n(3:2:(h-1), 2:2:(w-2), 1)... % R_n
                        + Pix_sum(:,:,1,2,4);


% Kernel 2 R-Center Positive Adjustment Sums

Adj_sum(:,:,1,1)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,2,1);

Adj_sum(:,:,1,2)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,2,2);

Adj_sum(:,:,1,3)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,2,3);

Adj_sum(:,:,1,4)        = Pix_in(  3:2:(h-1), 2:2:(w-2), 1)... % R
                        + Pix_sum(:,:,1,2,4);


% Kernel 2 B-Center Negative Adjustment Sums

Adj_sum_n(:,:,2,1)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,2,1);

Adj_sum_n(:,:,2,2)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,2,2);

Adj_sum_n(:,:,2,3)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,2,3);

Adj_sum_n(:,:,2,4)      = Pix_in_n(2:2:(h-2), 3:2:(w-1), 3)... % B_n
                        + Pix_sum(:,:,2,2,4);


% Kernel 2 B-Center Positive Adjustment Sums

Adj_sum(:,:,2,1)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,2,1);

Adj_sum(:,:,2,2)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,2,2);

Adj_sum(:,:,2,3)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,2,3);

Adj_sum(:,:,2,4)        = Pix_in(  2:2:(h-2), 3:2:(w-1), 3)... % B
                        + Pix_sum(:,:,2,2,4);


% Kernel 2 Averages

Sel_sum_max(:,:,:)   = max(max(Sel_sum_n, [], 4), max(Sel_sum, [], 4));

Avg_sel_d(:,:,:)  = (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,2))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,5))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,6))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,12))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,15))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,2))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,5))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,11))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,12))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,15));

Avg_sel_u(:,:,:)  = (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,3))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,8))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,9))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,11))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,14))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,3))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,6))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,8))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,9))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,14));

Avg_sel_du(:,:,:) = (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,1))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,4))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,7))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,10))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,13))...
                  | (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,16))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,1))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,4))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,7))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,10))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,13))...
                  | (Sel_sum_max(:,:,:) == Sel_sum(  :,:,:,16));

Avg(:,:,1)  =...
     Avg_sel_d(:,:,1)...
      .* bitshift((   Pix_in(  4:2:h,     3:2:(w-1), 3)...        % B3
                    + Pix_in(  2:2:(h-2), 1:2:(w-3), 3)), -1)...  % B1
 + (~Avg_sel_d(:,:,1) &  Avg_sel_u(:,:,1))...
      .* bitshift((   Pix_in(  4:2:h,     1:2:(w-3), 3)...        % B2
                    + Pix_in(  2:2:(h-2), 3:2:(w-1), 3)), -1)...  % B0
 + (~Avg_sel_d(:,:,1) & ~Avg_sel_u(:,:,1) & Avg_sel_du(:,:,1))...
      .* bitshift(Pix_sum(:,:,1,1,16), -2);

Avg(:,:,2)  =...
     Avg_sel_d(:,:,2)...
      .* bitshift((   Pix_in(  3:2:(h-1), 4:2:w,     1)...        % R3
                    + Pix_in(  1:2:(h-3), 2:2:(w-2), 1)), -1)...  % R1
 + (~Avg_sel_d(:,:,2) &  Avg_sel_u(:,:,2))...
      .* bitshift((   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...        % R2
                    + Pix_in(  1:2:(h-3), 4:2:w,     1)), -1)...  % R0
 + (~Avg_sel_d(:,:,2) & ~Avg_sel_u(:,:,2) & Avg_sel_du(:,:,2))...
      .* bitshift(Pix_sum(:,:,2,1,16), -2);


% Kernel 2 Negative Adjustment

Adj_sum_max_n(:,:,:) = max(Adj_sum_n, [], 4);

Adj_sel_d12_n(:,:,:) = (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,12));

Adj_sel_d15_n(:,:,:) = (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,15));

Adj_sel_u8_n(:,:,:)  = (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,8));

Adj_sel_u14_n(:,:,:) = (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,14));

Adj_sel_du4_n(:,:,:) = (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,4));

Adj_sel_du7_n(:,:,:) = (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,7));

Adj_sel_du10_n(:,:,:)= (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,10));

Adj_sel_du13_n(:,:,:)= (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,13));

Adj_sel_du16_n(:,:,:)= (Sel_sum_max(:,:,:) == Sel_sum_n(:,:,:,16));

Adj_n(:,:,:)   =...
   (   Avg_sel_d(:,:,:)...
    &  Adj_sel_d12_n(:,:,:))...
         .* clip((Adj_sum_max_n(:,:,:) - threshold_br), 0, 765)...
 + (   Avg_sel_d(:,:,:)...
    & ~Adj_sel_d12_n(:,:,:)  &  Adj_sel_d15_n(:,:,:))...
         .* clip((Adj_sum_max_n(:,:,:) - threshold_br), 0, 765)...
 + (  ~Avg_sel_d(:,:,:)...
    &  Avg_sel_u(:,:,:)...
    &  Adj_sel_u8_n(:,:,:))...
         .* clip((Adj_sum_max_n(:,:,:) - threshold_br), 0, 765)...
 + (  ~Avg_sel_d(:,:,:)...
    &  Avg_sel_u(:,:,:)...
    & ~Adj_sel_u8_n(:,:,:)   &  Adj_sel_u14_n(:,:,:))...
         .* clip((Adj_sum_max_n(:,:,:) - threshold_br), 0, 765)...
 + (  ~Avg_sel_d(:,:,:)...
    & ~Avg_sel_u(:,:,:)...
    &  Avg_sel_du(:,:,:)...
    &  Adj_sel_du4_n(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_max_n(:,:,:) - threshold_br), 0, 765), -1)...
 + (  ~Avg_sel_d(:,:,:)...
    & ~Avg_sel_u(:,:,:)...
    &  Avg_sel_du(:,:,:)...
    & ~Adj_sel_du4_n(:,:,:)  &  Adj_sel_du7_n(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_max_n(:,:,:) - threshold_br), 0, 765), -1)...
 + (  ~Avg_sel_d(:,:,:)...
    & ~Avg_sel_u(:,:,:)...
    &  Avg_sel_du(:,:,:)...
    & ~Adj_sel_du4_n(:,:,:)  & ~Adj_sel_du7_n(:,:,:)...
    &  Adj_sel_du10_n(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_max_n(:,:,:) - threshold_br), 0, 765), -1)...
 + (  ~Avg_sel_d(:,:,:)...
    & ~Avg_sel_u(:,:,:)...
    &  Avg_sel_du(:,:,:)...
    & ~Adj_sel_du4_n(:,:,:)  & ~Adj_sel_du7_n(:,:,:)...
    & ~Adj_sel_du10_n(:,:,:) &  Adj_sel_du13_n(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_max_n(:,:,:) - threshold_br), 0, 765), -1)...
 + (  ~Avg_sel_d(:,:,:)...
    & ~Avg_sel_u(:,:,:)...
    &  Avg_sel_du(:,:,:)...
    & ~Adj_sel_du4_n(:,:,:)  & ~Adj_sel_du7_n(:,:,:)...
    & ~Adj_sel_du10_n(:,:,:) & ~Adj_sel_du13_n(:,:,:)...
    &  Adj_sel_du16_n(:,:,:))...
         .* clip((Adj_sum_max_n(:,:,:) - threshold_br), 0, 765);


% Kernel 2 Positive Adjustment

Adj_sum_max(:,:,:)   = max(Adj_sum, [], 4);

Adj_sel_d2(:,:,:)    = (Sel_sum_max(:,:,:) == Sel_sum(:,:,:,2));

Adj_sel_d5(:,:,:)    = (Sel_sum_max(:,:,:) == Sel_sum(:,:,:,5));

Adj_sel_u3(:,:,:)    = (Sel_sum_max(:,:,:) == Sel_sum(:,:,:,3));

Adj_sel_u9(:,:,:)    = (Sel_sum_max(:,:,:) == Sel_sum(:,:,:,9));

Adj_sel_du1(:,:,:)   = (Sel_sum_max(:,:,:) == Sel_sum(:,:,:,1));

Adj_sel_du4(:,:,:)   = (Sel_sum_max(:,:,:) == Sel_sum(:,:,:,4));

Adj_sel_du7(:,:,:)   = (Sel_sum_max(:,:,:) == Sel_sum(:,:,:,7));

Adj_sel_du10(:,:,:)  = (Sel_sum_max(:,:,:) == Sel_sum(:,:,:,10));

Adj_sel_du13(:,:,:)  = (Sel_sum_max(:,:,:) == Sel_sum(:,:,:,13));

Adj(:,:,:)     =...
   (   Avg_sel_d(:,:,:)...
    &  Adj_sel_d2(:,:,:))...
         .* clip((Adj_sum_max(:,:,:) - threshold_br), 0, 765)...
 + (   Avg_sel_d(:,:,:)...
    & ~Adj_sel_d2(:,:,:)   &  Adj_sel_d5(:,:,:))...
         .* clip((Adj_sum_max(:,:,:) - threshold_br), 0, 765)...
 + (  ~Avg_sel_d(:,:,:)...
    &  Avg_sel_u(:,:,:)...
    &  Adj_sel_u3(:,:,:))...
         .* clip((Adj_sum_max(:,:,:) - threshold_br), 0, 765)...
 + (  ~Avg_sel_d(:,:,:)...
    &  Avg_sel_u(:,:,:)...
    & ~Adj_sel_u3(:,:,:)   &  Adj_sel_u9(:,:,:))...
         .* clip((Adj_sum_max(:,:,:) - threshold_br), 0, 765)...
 + (  ~Avg_sel_d(:,:,:)...
    & ~Avg_sel_u(:,:,:)...
    &  Avg_sel_du(:,:,:)...
    &  Adj_sel_du4(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_max(:,:,:) - threshold_br), 0, 765), -1)...
 + (  ~Avg_sel_d(:,:,:)...
    & ~Avg_sel_u(:,:,:)...
    &  Avg_sel_du(:,:,:)...
    & ~Adj_sel_du4(:,:,:)  &  Adj_sel_du7(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_max(:,:,:) - threshold_br), 0, 765), -1)...
 + (  ~Avg_sel_d(:,:,:)...
    & ~Avg_sel_u(:,:,:)...
    &  Avg_sel_du(:,:,:)...
    & ~Adj_sel_du4(:,:,:)  & ~Adj_sel_du7(:,:,:)...
    &  Adj_sel_du10(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_max(:,:,:) - threshold_br), 0, 765), -1)...
 + (  ~Avg_sel_d(:,:,:)...
    & ~Avg_sel_u(:,:,:)...
    &  Avg_sel_du(:,:,:)...
    & ~Adj_sel_du4(:,:,:)  & ~Adj_sel_du7(:,:,:)...
    & ~Adj_sel_du10(:,:,:) &  Adj_sel_du13(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_max(:,:,:) - threshold_br), 0, 765), -1)...
 + (  ~Avg_sel_d(:,:,:)...
    & ~Avg_sel_u(:,:,:)...
    &  Avg_sel_du(:,:,:)...
    & ~Adj_sel_du4(:,:,:)  & ~Adj_sel_du7(:,:,:)...
    & ~Adj_sel_du10(:,:,:) & ~Adj_sel_du13(:,:,:)...
    &  Adj_sel_du1(:,:,:))...
         .* clip((Adj_sum_max(:,:,:) - threshold_br), 0, 765);


% Kernel 2 R-Center B-Output

Adj_sel(:,:,1) =   Avg(:,:,1) < Pix_in(3:2:(h-1), 2:2:(w-2), 1);  % R

Pix_out(3:2:(h-1), 2:2:(w-2), 3) =...
   (~Adj_sel(:,:,1) .* clip((Avg(:,:,1) - Adj_n(:,:,1)),...
                             Pix_in(3:2:(h-1), 2:2:(w-2), 1),...  % R
                             Avg(:,:,1)))...
 + ( Adj_sel(:,:,1) .* clip((Avg(:,:,1) + Adj(  :,:,1)),...
                             Avg(:,:,1),...
                             Pix_in(3:2:(h-1), 2:2:(w-2), 1)));   % R

% Kernel 2 B-Center R-Output

Adj_sel(:,:,2) =   Avg(:,:,2) < Pix_in(2:2:(h-2), 3:2:(w-1), 3);  % B

Pix_out(2:2:(h-2), 3:2:(w-1), 1) =...
   (~Adj_sel(:,:,2) .* clip((Avg(:,:,2) - Adj_n(:,:,2)),...
                             Pix_in(2:2:(h-2), 3:2:(w-1), 3),...  % B
                             Avg(:,:,2)))...
 + ( Adj_sel(:,:,2) .* clip((Avg(:,:,2) + Adj(  :,:,2)),...
                             Avg(:,:,2),...
                             Pix_in(2:2:(h-2), 3:2:(w-1), 3)));   % B

%======================================================================
