function [Pix_out] = correlation1_3(Pix_in, threshold_g)
%
%CORRELATION1_3   Performs correlation-based color interpolation 
%                 Kernel 1.3
%
%   Usage: [Pix_out] = correlation1_3(Pix_in, threshold_g)
%
%      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_g   = Activation threshold for correlation-based
%                      adjustment to directed-linear-interpolated
%                      green pixels on red and blue centers
%                      The directed-linear interpolation is the average
%                      of the two horizontal, or the two vertical, or
%                      all four green pixels, depending on the average
%                      value closest to the center pixel value
%                      Valid range: 0 <= threshold_g <= 1275 (5 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/21/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 G-Interpolation - Kernel 1
%----------------------------------------------------------------------

% Kernel 1 R-Center G Sums

Pix_sum(:,:,1,1)        = Pix_in_n(4:2:h,     2:2:(w-2), 2)... % G3_n
                        + Pix_in_n(3:2:(h-1), 1:2:(w-3), 2)... % G2_n
                        + Pix_in_n(2:2:(h-2), 2:2:(w-2), 2)... % G1_n
                        + Pix_in_n(3:2:(h-1), 3:2:(w-1), 2);   % G0_n

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Pix_sum(:,:,1,16)       = Pix_in(  4:2:h,     2:2:(w-2), 2)... % G3
                        + Pix_in(  3:2:(h-1), 1:2:(w-3), 2)... % G2
                        + Pix_in(  2:2:(h-2), 2:2:(w-2), 2)... % G1
                        + Pix_in(  3:2:(h-1), 3:2:(w-1), 2);   % G0


% Kernel 1 B-Center G Sums

Pix_sum(:,:,2,1)        = Pix_in_n(3:2:(h-1), 3:2:(w-1), 2)... % G3_n
                        + Pix_in_n(2:2:(h-2), 2:2:(w-2), 2)... % G2_n
                        + Pix_in_n(1:2:(h-3), 3:2:(w-1), 2)... % G1_n
                        + Pix_in_n(2:2:(h-2), 4:2:w,     2);   % G0_n

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Pix_sum(:,:,2,16)       = Pix_in(  3:2:(h-1), 3:2:(w-1), 2)... % G3
                        + Pix_in(  2:2:(h-2), 2:2:(w-2), 2)... % G2
                        + Pix_in(  1:2:(h-3), 3:2:(w-1), 2)... % G1
                        + Pix_in(  2:2:(h-2), 4:2:w,     2);   % G0


% Kernel 1 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,1);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


% Kernel 1 R-Center Positive Adjustment Sums

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


% Kernel 1 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,1);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


% Kernel 1 B-Center Positive Adjustment Sums

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


% Kernel 1 Averages

Avg_h(:,:,1)   =...
         bitshift((   Pix_in(  3:2:(h-1), 1:2:(w-3), 2)...        % G2
                    + Pix_in(  3:2:(h-1), 3:2:(w-1), 2)), -1);    % G0

Avg_v(:,:,1)   =...
         bitshift((   Pix_in(  4:2:h,     2:2:(w-2), 2)...        % G3
                    + Pix_in(  2:2:(h-2), 2:2:(w-2), 2)), -1);    % G1

Avg_hv(:,:,1)  = bitshift(Pix_sum(:,:,1,16), -2);


Avg_h(:,:,2)   =...
         bitshift((   Pix_in(  2:2:(h-2), 2:2:(w-2), 2)...        % G2
                    + Pix_in(  2:2:(h-2), 4:2:w,     2)), -1);    % G0

Avg_v(:,:,2)   =...
         bitshift((   Pix_in(  3:2:(h-1), 3:2:(w-1), 2)...        % G3
                    + Pix_in(  1:2:(h-3), 3:2:(w-1), 2)), -1);    % G1

Avg_hv(:,:,2)  = bitshift(Pix_sum(:,:,2,16), -2);


Avg_sel_h(:,:,1)  = (   abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_h( :,:,1))...
                     <= abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_v( :,:,1)))...
                  & (   abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_h( :,:,1))...
                     <= abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_hv(:,:,1)));

Avg_sel_v(:,:,1)  = (   abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_v( :,:,1))...
                     <= abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_h( :,:,1)))...
                  & (   abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_v( :,:,1))...
                     <= abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_hv(:,:,1)));

Avg_sel_hv(:,:,1) = (   abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_hv(:,:,1))...
                     <= abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_h( :,:,1)))...
                  & (   abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_hv(:,:,1))...
                     <= abs(   Pix_in(  3:2:(h-1), 2:2:(w-2), 1)...% R
                             - Avg_v( :,:,1)));


Avg_sel_h(:,:,2)  = (   abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_h( :,:,2))...
                     <= abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_v( :,:,2)))...
                  & (   abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_h( :,:,2))...
                     <= abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_hv(:,:,2)));

Avg_sel_v(:,:,2)  = (   abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_v( :,:,2))...
                     <= abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_h( :,:,2)))...
                  & (   abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_v( :,:,2))...
                     <= abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_hv(:,:,2)));

Avg_sel_hv(:,:,2) = (   abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_hv(:,:,2))...
                     <= abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_h( :,:,2)))...
                  & (   abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_hv(:,:,2))...
                     <= abs(   Pix_in(  2:2:(h-2), 3:2:(w-1), 3)...% B
                             - Avg_v( :,:,2)));


Avg(:,:,:)  =...
        Avg_sel_v(:,:,:)...
   .*   Avg_v( :,:,:)...
 +    (~Avg_sel_v(:,:,:) &  Avg_sel_h(:,:,:))...
   .*   Avg_h( :,:,:)...
 +    (~Avg_sel_v(:,:,:) & ~Avg_sel_h(:,:,:) & Avg_sel_hv(:,:,:))...
   .*   Avg_hv(:,:,:);


% Kernel 1 Maximum Adjustment Sum

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


% Kernel 1 Negative Adjustment

Adj_sel_h8_n(:,:,:)  = (Adj_sum_max(:,:,:) == Adj_sum_n(:,:,:,8));

Adj_sel_h14_n(:,:,:) = (Adj_sum_max(:,:,:) == Adj_sum_n(:,:,:,14));

Adj_sel_v12_n(:,:,:) = (Adj_sum_max(:,:,:) == Adj_sum_n(:,:,:,12));

Adj_sel_v15_n(:,:,:) = (Adj_sum_max(:,:,:) == Adj_sum_n(:,:,:,15));

Adj_sel_hv4_n(:,:,:) = (Adj_sum_max(:,:,:) == Adj_sum_n(:,:,:,4));

Adj_sel_hv7_n(:,:,:) = (Adj_sum_max(:,:,:) == Adj_sum_n(:,:,:,7));

Adj_sel_hv10_n(:,:,:)= (Adj_sum_max(:,:,:) == Adj_sum_n(:,:,:,10));

Adj_sel_hv13_n(:,:,:)= (Adj_sum_max(:,:,:) == Adj_sum_n(:,:,:,13));

Adj_n(:,:,:)   =...
   (   Avg_sel_v(:,:,:)...
    &  Adj_sel_v12_n(:,:,:))...
         .* clip((Adj_sum_n(:,:,:,12) - threshold_g), 0, 1275)...
 + (   Avg_sel_v(:,:,:)...
    & ~Adj_sel_v12_n(:,:,:)  &  Adj_sel_v15_n(:,:,:))...
         .* clip((Adj_sum_n(:,:,:,15) - threshold_g), 0, 1275)...
 + (  ~Avg_sel_v(:,:,:)...
    &  Avg_sel_h(:,:,:)...
    &  Adj_sel_h8_n(:,:,:))...
         .* clip((Adj_sum_n(:,:,:,8)  - threshold_g), 0, 1275)...
 + (  ~Avg_sel_v(:,:,:)...
    &  Avg_sel_h(:,:,:)...
    & ~Adj_sel_h8_n(:,:,:)   &  Adj_sel_h14_n(:,:,:))...
         .* clip((Adj_sum_n(:,:,:,14) - threshold_g), 0, 1275)...
 + (  ~Avg_sel_v(:,:,:)...
    & ~Avg_sel_h(:,:,:)...
    &  Avg_sel_hv(:,:,:)...
    &  Adj_sel_hv4_n(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_n(:,:,:,4)  - threshold_g), 0, 1275), -1)...
 + (  ~Avg_sel_v(:,:,:)...
    & ~Avg_sel_h(:,:,:)...
    &  Avg_sel_hv(:,:,:)...
    & ~Adj_sel_hv4_n(:,:,:)  &  Adj_sel_hv7_n(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_n(:,:,:,7)  - threshold_g), 0, 1275), -1)...
 + (  ~Avg_sel_v(:,:,:)...
    & ~Avg_sel_h(:,:,:)...
    &  Avg_sel_hv(:,:,:)...
    & ~Adj_sel_hv4_n(:,:,:)  & ~Adj_sel_hv7_n(:,:,:)...
    &  Adj_sel_hv10_n(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_n(:,:,:,10) - threshold_g), 0, 1275), -1)...
 + (  ~Avg_sel_v(:,:,:)...
    & ~Avg_sel_h(:,:,:)...
    &  Avg_sel_hv(:,:,:)...
    & ~Adj_sel_hv4_n(:,:,:)  & ~Adj_sel_hv7_n(:,:,:)...
    & ~Adj_sel_hv10_n(:,:,:) &  Adj_sel_hv13_n(:,:,:))...
         .* bitshift(...
            clip((Adj_sum_n(:,:,:,13) - threshold_g), 0, 1275), -1);


% Kernel 1 Positive Adjustment

Adj_sel_h3(:,:,:)    = (Adj_sum_max(:,:,:) == Adj_sum(:,:,:,3));

Adj_sel_h9(:,:,:)    = (Adj_sum_max(:,:,:) == Adj_sum(:,:,:,9));

Adj_sel_v2(:,:,:)    = (Adj_sum_max(:,:,:) == Adj_sum(:,:,:,2));

Adj_sel_v5(:,:,:)    = (Adj_sum_max(:,:,:) == Adj_sum(:,:,:,5));

Adj_sel_hv4(:,:,:)   = (Adj_sum_max(:,:,:) == Adj_sum(:,:,:,4));

Adj_sel_hv7(:,:,:)   = (Adj_sum_max(:,:,:) == Adj_sum(:,:,:,7));

Adj_sel_hv10(:,:,:)  = (Adj_sum_max(:,:,:) == Adj_sum(:,:,:,10));

Adj_sel_hv13(:,:,:)  = (Adj_sum_max(:,:,:) == Adj_sum(:,:,:,13));

Adj(:,:,:)     =...
   (   Avg_sel_v(:,:,:)...
    &  Adj_sel_v2(:,:,:))...
         .* clip((Adj_sum(:,:,:,2) - threshold_g), 0, 1275)...
 + (   Avg_sel_v(:,:,:)...
    & ~Adj_sel_v2(:,:,:)   &  Adj_sel_v5(:,:,:))...
         .* clip((Adj_sum(:,:,:,5) - threshold_g), 0, 1275)...
 + (  ~Avg_sel_v(:,:,:)...
    &  Avg_sel_h(:,:,:)...
    &  Adj_sel_h3(:,:,:))...
         .* clip((Adj_sum(:,:,:,3)  - threshold_g), 0, 1275)...
 + (  ~Avg_sel_v(:,:,:)...
    &  Avg_sel_h(:,:,:)...
    & ~Adj_sel_h3(:,:,:)   &  Adj_sel_h9(:,:,:))...
         .* clip((Adj_sum(:,:,:,9) - threshold_g), 0, 1275)...
 + (  ~Avg_sel_v(:,:,:)...
    & ~Avg_sel_h(:,:,:)...
    &  Avg_sel_hv(:,:,:)...
    &  Adj_sel_hv4(:,:,:))...
         .* bitshift(...
            clip((Adj_sum(:,:,:,4)  - threshold_g), 0, 1275), -1)...
 + (  ~Avg_sel_v(:,:,:)...
    & ~Avg_sel_h(:,:,:)...
    &  Avg_sel_hv(:,:,:)...
    & ~Adj_sel_hv4(:,:,:)  &  Adj_sel_hv7(:,:,:))...
         .* bitshift(...
            clip((Adj_sum(:,:,:,7)  - threshold_g), 0, 1275), -1)...
 + (  ~Avg_sel_v(:,:,:)...
    & ~Avg_sel_h(:,:,:)...
    &  Avg_sel_hv(:,:,:)...
    & ~Adj_sel_hv4(:,:,:)  & ~Adj_sel_hv7(:,:,:)...
    &  Adj_sel_hv10(:,:,:))...
         .* bitshift(...
            clip((Adj_sum(:,:,:,10) - threshold_g), 0, 1275), -1)...
 + (  ~Avg_sel_v(:,:,:)...
    & ~Avg_sel_h(:,:,:)...
    &  Avg_sel_hv(:,:,:)...
    & ~Adj_sel_hv4(:,:,:)  & ~Adj_sel_hv7(:,:,:)...
    & ~Adj_sel_hv10(:,:,:) &  Adj_sel_hv13(:,:,:))...
         .* bitshift(...
            clip((Adj_sum(:,:,:,13) - threshold_g), 0, 1275), -1);


% Kernel 1 R-Center G-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), 2) =...
   (~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 1 B-Center G-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), 2) =...
   (~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

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