function [Pix_out] = correlation4_1(Pix_in)
%
%CORRELATION4_1   Performs correlation-based R and B extrapolation,
%                 referenced to G
%
%   Usage: [Pix_out] = correlation4_1(Pix_in)
%
%      Pix_out       = Filtered image 3D array output
%                      Valid range: 0 <= Pix_out(:,:,:) <= 255 (8 bits)
%
%      Pix_in        = RGB image 3D array input
%                      Valid range: 0 <= Pix_in(:,:,:) <= 255 (8 bits)
%
%   Assumptions: Pix_in has the following format -
%
%   d   Color Plane
%  --- -------------
%   1   R
%   2   G
%   3   B
%
%   Author:    Gary Embler
%   Email:     gary_embler@agilent.com
%   Date:      05/24/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);

Diff_sum_h(:,:,:,:)  = zeros(h,w,2,2);

Diff_sum_v(:,:,:,:)  = zeros(h,w,2,2);

Diff_sum_d(:,:,:,:)  = zeros(h,w,2,2);

Diff_sum_u(:,:,:,:)  = zeros(h,w,2,2);

Diff_h(:,:,:,:)      = zeros(h,w,2,3);

Diff_v(:,:,:,:)      = zeros(h,w,2,3);

Diff_d(:,:,:,:)      = zeros(h,w,2,3);

Diff_u(:,:,:,:)      = zeros(h,w,2,3);

Diff_min_h(:,:,:)    = zeros(h,w,2);

Diff_max_h(:,:,:)    = zeros(h,w,2);

Diff_min_v(:,:,:)    = zeros(h,w,2);

Diff_max_v(:,:,:)    = zeros(h,w,2);

Diff_min_d(:,:,:)    = zeros(h,w,2);

Diff_max_d(:,:,:)    = zeros(h,w,2);

Diff_min_u(:,:,:)    = zeros(h,w,2);

Diff_max_u(:,:,:)    = zeros(h,w,2);

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

Pix_out(:,:,2) = Pix_in(:,:,2);

%======================================================================
% Color Differences
%----------------------------------------------------------------------

Colordiff(:,:,1)     = Pix_in(:,:,1) - Pix_in(:,:,2);       % R - G

Colordiff(:,:,2)     = Pix_in(:,:,3) - Pix_in(:,:,2);       % B - G

%======================================================================
% Color Differences True and Complement
%----------------------------------------------------------------------

Colordiff_p(:,:,:)   = Colordiff(:,:,:) + 255;

Colordiff_n(:,:,:)   = bitcmp(Colordiff_p(:,:,:), 9);

%======================================================================
% Color Difference Sums
%----------------------------------------------------------------------

% R,B - G H Sums

Diff_sum_h(3:2:(h-1), 2:2:(w-2), :, 1) =...
   Colordiff_n(3:2:(h-1), 1:2:(w-3), :)...            % R,B@GR - GR  1
 + Colordiff_n(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R
 + Colordiff_n(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR  2

Diff_sum_h(3:2:(h-1), 2:2:(w-2), :, 2) =...
   Colordiff_p(3:2:(h-1), 1:2:(w-3), :)...            % R,B@GR - GR  1
 + Colordiff_p(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R
 + Colordiff_p(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR  2

Diff_sum_h(3:2:(h-1), 3:2:(w-1), :, 1) =...
   Colordiff_n(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R 1
 + Colordiff_n(3:2:(h-1), 3:2:(w-1), :)...            % R,B@GR - GR
 + Colordiff_n(3:2:(h-1), 4:2:w,     :);              % R,B@R  - G@R 2

Diff_sum_h(3:2:(h-1), 3:2:(w-1), :, 2) =...
   Colordiff_p(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R 1
 + Colordiff_p(3:2:(h-1), 3:2:(w-1), :)...            % R,B@GR - GR
 + Colordiff_p(3:2:(h-1), 4:2:w,     :);              % R,B@R  - G@R 2

Diff_sum_h(2:2:(h-2), 2:2:(w-2), :, 1) =...
   Colordiff_n(2:2:(h-2), 1:2:(w-3), :)...            % R,B@B  - G@B 1
 + Colordiff_n(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB
 + Colordiff_n(2:2:(h-2), 3:2:(w-1), :);              % R,B@B  - G@B 2

Diff_sum_h(2:2:(h-2), 2:2:(w-2), :, 2) =...
   Colordiff_p(2:2:(h-2), 1:2:(w-3), :)...            % R,B@B  - G@B 1
 + Colordiff_p(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB
 + Colordiff_p(2:2:(h-2), 3:2:(w-1), :);              % R,B@B  - G@B 2

Diff_sum_h(2:2:(h-2), 3:2:(w-1), :, 1) =...
   Colordiff_n(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB  1
 + Colordiff_n(2:2:(h-2), 3:2:(w-1), :)...            % R,B@B  - G@B
 + Colordiff_n(2:2:(h-2), 4:2:w,     :);              % R,B@GB - GB  2

Diff_sum_h(2:2:(h-2), 3:2:(w-1), :, 2) =...
   Colordiff_p(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB  1
 + Colordiff_p(2:2:(h-2), 3:2:(w-1), :)...            % R,B@B  - G@B
 + Colordiff_p(2:2:(h-2), 4:2:w,     :);              % R,B@GB - GB  2


% R,B - G V Sums

Diff_sum_v(3:2:(h-1), 2:2:(w-2), :, 1) =...
   Colordiff_n(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB  1
 + Colordiff_n(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R
 + Colordiff_n(4:2:h,     2:2:(w-2), :);              % R,B@GB - GB  2

Diff_sum_v(3:2:(h-1), 2:2:(w-2), :, 2) =...
   Colordiff_p(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB  1
 + Colordiff_p(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R
 + Colordiff_p(4:2:h,     2:2:(w-2), :);              % R,B@GB - GB  2

Diff_sum_v(3:2:(h-1), 3:2:(w-1), :, 1) =...
   Colordiff_n(2:2:(h-2), 3:2:(w-1), :)...            % R,B@B  - G@B 1
 + Colordiff_n(3:2:(h-1), 3:2:(w-1), :)...            % R,B@GR - GR
 + Colordiff_n(4:2:h,     3:2:(w-1), :);              % R,B@B  - G@B 2

Diff_sum_v(3:2:(h-1), 3:2:(w-1), :, 2) =...
   Colordiff_p(2:2:(h-2), 3:2:(w-1), :)...            % R,B@B  - G@B 1
 + Colordiff_p(3:2:(h-1), 3:2:(w-1), :)...            % R,B@GR - GR
 + Colordiff_p(4:2:h,     3:2:(w-1), :);              % R,B@B  - G@B 2

Diff_sum_v(2:2:(h-2), 2:2:(w-2), :, 1) =...
   Colordiff_n(1:2:(h-3), 2:2:(w-2), :)...            % R,B@R  - G@R 1
 + Colordiff_n(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB
 + Colordiff_n(3:2:(h-1), 2:2:(w-2), :);              % R,B@R  - G@R 2

Diff_sum_v(2:2:(h-2), 2:2:(w-2), :, 2) =...
   Colordiff_p(1:2:(h-3), 2:2:(w-2), :)...            % R,B@R  - G@R 1
 + Colordiff_p(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB
 + Colordiff_p(3:2:(h-1), 2:2:(w-2), :);              % R,B@R  - G@R 2

Diff_sum_v(2:2:(h-2), 3:2:(w-1), :, 1) =...
   Colordiff_n(1:2:(h-3), 3:2:(w-1), :)...            % R,B@GR - GR  1
 + Colordiff_n(2:2:(h-2), 3:2:(w-1), :)...            % R,B@B  - G@B
 + Colordiff_n(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR  2

Diff_sum_v(2:2:(h-2), 3:2:(w-1), :, 2) =...
   Colordiff_p(1:2:(h-3), 3:2:(w-1), :)...            % R,B@GR - GR  1
 + Colordiff_p(2:2:(h-2), 3:2:(w-1), :)...            % R,B@B  - G@B
 + Colordiff_p(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR  2


% R,B - G D Sums

Diff_sum_d(3:2:(h-1), 2:2:(w-2), :, 1) =...
   Colordiff_n(2:2:(h-2), 1:2:(w-3), :)...            % R,B@B  - G@B 1
 + Colordiff_n(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R
 + Colordiff_n(4:2:h,     3:2:(w-1), :);              % R,B@B  - G@B 2

Diff_sum_d(3:2:(h-1), 2:2:(w-2), :, 2) =...
   Colordiff_p(2:2:(h-2), 1:2:(w-3), :)...            % R,B@B  - G@B 1
 + Colordiff_p(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R
 + Colordiff_p(4:2:h,     3:2:(w-1), :);              % R,B@B  - G@B 2

Diff_sum_d(3:2:(h-1), 3:2:(w-1), :, 1) =...
   Colordiff_n(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB  1
 + Colordiff_n(3:2:(h-1), 3:2:(w-1), :)...            % R,B@GR - GR
 + Colordiff_n(4:2:h,     4:2:w,     :);              % R,B@GB - GB  2

Diff_sum_d(3:2:(h-1), 3:2:(w-1), :, 2) =...
   Colordiff_p(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB  1
 + Colordiff_p(3:2:(h-1), 3:2:(w-1), :)...            % R,B@GR - GR
 + Colordiff_p(4:2:h,     4:2:w,     :);              % R,B@GB - GB  2

Diff_sum_d(2:2:(h-2), 2:2:(w-2), :, 1) =...
   Colordiff_n(1:2:(h-3), 1:2:(w-3), :)...            % R,B@GR - GR  1
 + Colordiff_n(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB
 + Colordiff_n(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR  2

Diff_sum_d(2:2:(h-2), 2:2:(w-2), :, 2) =...
   Colordiff_p(1:2:(h-3), 1:2:(w-3), :)...            % R,B@GR - GR  1
 + Colordiff_p(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB
 + Colordiff_p(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR  2

Diff_sum_d(2:2:(h-2), 3:2:(w-1), :, 1) =...
   Colordiff_n(1:2:(h-3), 2:2:(w-2), :)...            % R,B@R  - G@R 1
 + Colordiff_n(2:2:(h-2), 3:2:(w-1), :)...            % R,B@B  - G@B
 + Colordiff_n(3:2:(h-1), 4:2:w,     :);              % R,B@R  - G@R 2

Diff_sum_d(2:2:(h-2), 3:2:(w-1), :, 2) =...
   Colordiff_p(1:2:(h-3), 2:2:(w-2), :)...            % R,B@R  - G@R 1
 + Colordiff_p(2:2:(h-2), 3:2:(w-1), :)...            % R,B@B  - G@B
 + Colordiff_p(3:2:(h-1), 4:2:w,     :);              % R,B@R  - G@R 2


% R,B - G U Sums

Diff_sum_u(3:2:(h-1), 2:2:(w-2), :, 1) =...
   Colordiff_n(4:2:h,     1:2:(w-3), :)...            % R,B@B  - G@B 1
 + Colordiff_n(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R
 + Colordiff_n(2:2:(h-2), 3:2:(w-1), :);              % R,B@B  - G@B 2

Diff_sum_u(3:2:(h-1), 2:2:(w-2), :, 2) =...
   Colordiff_p(4:2:h,     1:2:(w-3), :)...            % R,B@B  - G@B 1
 + Colordiff_p(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R
 + Colordiff_p(2:2:(h-2), 3:2:(w-1), :);              % R,B@B  - G@B 2

Diff_sum_u(3:2:(h-1), 3:2:(w-1), :, 1) =...
   Colordiff_n(4:2:h,     2:2:(w-2), :)...            % R,B@GB - GB  1
 + Colordiff_n(3:2:(h-1), 3:2:(w-1), :)...            % R,B@GR - GR
 + Colordiff_n(2:2:(h-2), 4:2:w,     :);              % R,B@GB - GB  2

Diff_sum_u(3:2:(h-1), 3:2:(w-1), :, 2) =...
   Colordiff_p(4:2:h,     2:2:(w-2), :)...            % R,B@GB - GB  1
 + Colordiff_p(3:2:(h-1), 3:2:(w-1), :)...            % R,B@GR - GR
 + Colordiff_p(2:2:(h-2), 4:2:w,     :);              % R,B@GB - GB  2

Diff_sum_u(2:2:(h-2), 2:2:(w-2), :, 1) =...
   Colordiff_n(3:2:(h-1), 1:2:(w-3), :)...            % R,B@GR - GR  1
 + Colordiff_n(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB
 + Colordiff_n(1:2:(h-3), 3:2:(w-1), :);              % R,B@GR - GR  2

Diff_sum_u(2:2:(h-2), 2:2:(w-2), :, 2) =...
   Colordiff_p(3:2:(h-1), 1:2:(w-3), :)...            % R,B@GR - GR  1
 + Colordiff_p(2:2:(h-2), 2:2:(w-2), :)...            % R,B@GB - GB
 + Colordiff_p(1:2:(h-3), 3:2:(w-1), :);              % R,B@GR - GR  2

Diff_sum_u(2:2:(h-2), 3:2:(w-1), :, 1) =...
   Colordiff_n(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R 1
 + Colordiff_n(2:2:(h-2), 3:2:(w-1), :)...            % R,B@B  - G@B
 + Colordiff_n(1:2:(h-3), 4:2:w,     :);              % R,B@R  - G@R 2

Diff_sum_u(2:2:(h-2), 3:2:(w-1), :, 2) =...
   Colordiff_p(3:2:(h-1), 2:2:(w-2), :)...            % R,B@R  - G@R 1
 + Colordiff_p(2:2:(h-2), 3:2:(w-1), :)...            % R,B@B  - G@B
 + Colordiff_p(1:2:(h-3), 4:2:w,     :);              % R,B@R  - G@R 2


%======================================================================
% Color Difference Arrays
%----------------------------------------------------------------------

% R, B - G H Array

Diff_h(        3:2:(h-1), 1:2:(w-3), :, 1) = ...
   Colordiff_p(3:2:(h-1), 1:2:(w-3), :);              % R,B@GR - GR  1

Diff_h(        3:2:(h-1), 2:2:(w-2), :, 2) = ...
   Colordiff_p(3:2:(h-1), 2:2:(w-2), :);              % R,B@R  - G@R

Diff_h(        3:2:(h-1), 3:2:(w-1), :, 3) = ...
   Colordiff_p(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR  2


Diff_h(        3:2:(h-1), 2:2:(w-2), :, 1) = ...
   Colordiff_p(3:2:(h-1), 2:2:(w-2), :);              % R,B@R  - G@R 1

Diff_h(        3:2:(h-1), 3:2:(w-1), :, 2) = ...
   Colordiff_p(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR

Diff_h(        3:2:(h-1), 4:2:w,     :, 3) = ...
   Colordiff_p(3:2:(h-1), 4:2:w,     :);              % R,B@R  - G@R 2


Diff_h(        2:2:(h-2), 1:2:(w-3), :, 1) = ...
   Colordiff_p(2:2:(h-2), 1:2:(w-3), :);              % R,B@B  - G@B 1

Diff_h(        2:2:(h-2), 2:2:(w-2), :, 2) = ...
   Colordiff_p(2:2:(h-2), 2:2:(w-2), :);              % R,B@GB - GB

Diff_h(        2:2:(h-2), 3:2:(w-1), :, 3) = ...
   Colordiff_p(2:2:(h-2), 3:2:(w-1), :);              % R,B@B  - G@B 2


Diff_h(        2:2:(h-2), 2:2:(w-2), :, 1) = ...
   Colordiff_p(2:2:(h-2), 2:2:(w-2), :);              % R,B@GB - GB  1

Diff_h(        2:2:(h-2), 3:2:(w-1), :, 2) = ...
   Colordiff_p(2:2:(h-2), 3:2:(w-1), :);              % R,B@B  - G@B

Diff_h(        2:2:(h-2), 4:2:w,     :, 3) = ...
   Colordiff_p(2:2:(h-2), 4:2:w,     :);              % R,B@GB - GB  2


% R, B - G V Array

Diff_v(        2:2:(h-2), 2:2:(w-2), :, 1) = ...
   Colordiff_p(2:2:(h-2), 2:2:(w-2), :);              % R,B@GB - GB  1

Diff_v(        3:2:(h-1), 2:2:(w-2), :, 2) = ...
   Colordiff_p(3:2:(h-1), 2:2:(w-2), :);              % R,B@R  - G@R

Diff_v(        4:2:h,     2:2:(w-2), :, 3) = ...
   Colordiff_p(4:2:h,     2:2:(w-2), :);              % R,B@GB - GB  2


Diff_v(        2:2:(h-2), 3:2:(w-1), :, 1) = ...
   Colordiff_p(2:2:(h-2), 3:2:(w-1), :);              % R,B@B  - G@B 1

Diff_v(        3:2:(h-1), 3:2:(w-1), :, 2) = ...
   Colordiff_p(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR

Diff_v(        4:2:h,     3:2:(w-1), :, 3) = ...
   Colordiff_p(4:2:h,     3:2:(w-1), :);              % R,B@B  - G@B 2


Diff_v(        1:2:(h-3), 2:2:(w-2), :, 1) = ...
   Colordiff_p(1:2:(h-3), 2:2:(w-2), :);              % R,B@R  - G@R 1

Diff_v(        2:2:(h-2), 2:2:(w-2), :, 2) = ...
   Colordiff_p(2:2:(h-2), 2:2:(w-2), :);              % R,B@GB - GB

Diff_v(        3:2:(h-1), 2:2:(w-2), :, 3) = ...
   Colordiff_p(3:2:(h-1), 2:2:(w-2), :);              % R,B@R  - G@R 2


Diff_v(        1:2:(h-3), 3:2:(w-1), :, 1) = ...
   Colordiff_p(1:2:(h-3), 3:2:(w-1), :);              % R,B@GR - GR  1

Diff_v(        2:2:(h-2), 3:2:(w-1), :, 2) = ...
   Colordiff_p(2:2:(h-2), 3:2:(w-1), :);              % R,B@B  - G@B

Diff_v(        3:2:(h-1), 3:2:(w-1), :, 3) = ...
   Colordiff_p(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR  2


% R, B - G D Array

Diff_d(        2:2:(h-2), 1:2:(w-3), :, 1) = ...
   Colordiff_p(2:2:(h-2), 1:2:(w-3), :);              % R,B@B  - G@B 1

Diff_d(        3:2:(h-1), 2:2:(w-2), :, 2) = ...
   Colordiff_p(3:2:(h-1), 2:2:(w-2), :);              % R,B@R  - G@R

Diff_d(        4:2:h,     3:2:(w-1), :, 3) = ...
   Colordiff_p(4:2:h,     3:2:(w-1), :);              % R,B@B  - G@B 2


Diff_d(        2:2:(h-2), 2:2:(w-2), :, 1) = ...
   Colordiff_p(2:2:(h-2), 2:2:(w-2), :);              % R,B@GB - GB  1

Diff_d(        3:2:(h-1), 3:2:(w-1), :, 2) = ...
   Colordiff_p(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR

Diff_d(        4:2:h,     4:2:w,     :, 3) = ...
   Colordiff_p(4:2:h,     4:2:w,     :);              % R,B@GB - GB  2


Diff_d(        1:2:(h-3), 1:2:(w-3), :, 1) = ...
   Colordiff_p(1:2:(h-3), 1:2:(w-3), :);              % R,B@GR - GR  1

Diff_d(        2:2:(h-2), 2:2:(w-2), :, 2) = ...
   Colordiff_p(2:2:(h-2), 2:2:(w-2), :);              % R,B@GB - GB

Diff_d(        3:2:(h-1), 3:2:(w-1), :, 3) = ...
   Colordiff_p(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR  2


Diff_d(        1:2:(h-3), 2:2:(w-2), :, 1) = ...
   Colordiff_p(1:2:(h-3), 2:2:(w-2), :);              % R,B@R  - G@R 1

Diff_d(        2:2:(h-2), 3:2:(w-1), :, 2) = ...
   Colordiff_p(2:2:(h-2), 3:2:(w-1), :);              % R,B@B  - G@B

Diff_d(        3:2:(h-1), 4:2:w,     :, 3) = ...
   Colordiff_p(3:2:(h-1), 4:2:w,     :);              % R,B@R  - G@R 2


% R, B - G U Array

Diff_u(        4:2:h,     1:2:(w-3), :, 1) = ...
   Colordiff_p(4:2:h,     1:2:(w-3), :);              % R,B@B  - G@B 1

Diff_u(        3:2:(h-1), 2:2:(w-2), :, 2) = ...
   Colordiff_p(3:2:(h-1), 2:2:(w-2), :);              % R,B@R  - G@R

Diff_u(        2:2:(h-2), 3:2:(w-1), :, 3) = ...
   Colordiff_p(2:2:(h-2), 3:2:(w-1), :);              % R,B@B  - G@B 2


Diff_u(        4:2:h,     2:2:(w-2), :, 1) = ...
   Colordiff_p(4:2:h,     2:2:(w-2), :);              % R,B@GB - GB  1

Diff_u(        3:2:(h-1), 3:2:(w-1), :, 2) = ...
   Colordiff_p(3:2:(h-1), 3:2:(w-1), :);              % R,B@GR - GR

Diff_u(        2:2:(h-2), 4:2:w,     :, 3) = ...
   Colordiff_p(2:2:(h-2), 4:2:w,     :);              % R,B@GB - GB  2


Diff_u(        3:2:(h-1), 1:2:(w-3), :, 1) = ...
   Colordiff_p(3:2:(h-1), 1:2:(w-3), :);              % R,B@GR - GR  1

Diff_u(        2:2:(h-2), 2:2:(w-2), :, 2) = ...
   Colordiff_p(2:2:(h-2), 2:2:(w-2), :);              % R,B@GB - GB

Diff_u(        1:2:(h-3), 3:2:(w-1), :, 3) = ...
   Colordiff_p(1:2:(h-3), 3:2:(w-1), :);              % R,B@GR - GR  2


Diff_u(        3:2:(h-1), 2:2:(w-2), :, 1) = ...
   Colordiff_p(3:2:(h-1), 2:2:(w-2), :);              % R,B@R  - G@R 1

Diff_u(        2:2:(h-2), 3:2:(w-1), :, 2) = ...
   Colordiff_p(2:2:(h-2), 3:2:(w-1), :);              % R,B@B  - G@B

Diff_u(        1:2:(h-3), 4:2:w,     :, 3) = ...
   Colordiff_p(1:2:(h-3), 4:2:w,     :);              % R,B@R  - G@R 2


%======================================================================
% Color Difference Mins and Maxs
%----------------------------------------------------------------------

Diff_min_h(:,:,:) = min(Diff_h, [], 4);

Diff_max_h(:,:,:) = max(Diff_h, [], 4);

Diff_min_v(:,:,:) = min(Diff_v, [], 4);

Diff_max_v(:,:,:) = max(Diff_v, [], 4);

Diff_min_d(:,:,:) = min(Diff_d, [], 4);

Diff_max_d(:,:,:) = max(Diff_d, [], 4);

Diff_min_u(:,:,:) = min(Diff_u, [], 4);

Diff_max_u(:,:,:) = max(Diff_u, [], 4);


%======================================================================
% Color Difference Select
%----------------------------------------------------------------------

Diff_sum_max(:,:,:)  =...
   max(max(max(Diff_sum_h, [], 4), max(Diff_sum_v, [], 4)),...
       max(max(Diff_sum_d, [], 4), max(Diff_sum_u, [], 4)));

Diff_sel_h(:,:,:) = (Diff_sum_max(:,:,:) == Diff_sum_h(:,:,:,1))...
                  | (Diff_sum_max(:,:,:) == Diff_sum_h(:,:,:,2));

Diff_sel_v(:,:,:) = (Diff_sum_max(:,:,:) == Diff_sum_v(:,:,:,1))...
                  | (Diff_sum_max(:,:,:) == Diff_sum_v(:,:,:,2));

Diff_sel_d(:,:,:) = (Diff_sum_max(:,:,:) == Diff_sum_d(:,:,:,1))...
                  | (Diff_sum_max(:,:,:) == Diff_sum_d(:,:,:,2));

Diff_sel_u(:,:,:) = (Diff_sum_max(:,:,:) == Diff_sum_u(:,:,:,1))...
                  | (Diff_sum_max(:,:,:) == Diff_sum_u(:,:,:,2));

%======================================================================
% Color Difference Medians
%----------------------------------------------------------------------

Diff_med_h(:,:,:) = Diff_sum_h(:,:,:,2)...
                  - Diff_min_h(:,:,:)...
                  - Diff_max_h(:,:,:)...
                  - 255;

Diff_med_v(:,:,:) = Diff_sum_v(:,:,:,2)...
                  - Diff_min_v(:,:,:)...
                  - Diff_max_v(:,:,:)...
                  - 255;

Diff_med_d(:,:,:) = Diff_sum_d(:,:,:,2)...
                  - Diff_min_d(:,:,:)...
                  - Diff_max_d(:,:,:)...
                  - 255;

Diff_med_u(:,:,:) = Diff_sum_u(:,:,:,2)...
                  - Diff_min_u(:,:,:)...
                  - Diff_max_u(:,:,:)...
                  - 255;

%======================================================================
% R, B Outputs
%----------------------------------------------------------------------

% R @ R

Pix_out(3:2:(h-1), 2:2:(w-2), 1) = clip((...
       Diff_sel_v(3:2:(h-1), 2:2:(w-2), 1)...
    .* Diff_med_v(3:2:(h-1), 2:2:(w-2), 1)...
 + (  ~Diff_sel_v(3:2:(h-1), 2:2:(w-2), 1)...
    &  Diff_sel_h(3:2:(h-1), 2:2:(w-2), 1))...
    .* Diff_med_h(3:2:(h-1), 2:2:(w-2), 1)...
 + (  ~Diff_sel_v(3:2:(h-1), 2:2:(w-2), 1)...
    & ~Diff_sel_h(3:2:(h-1), 2:2:(w-2), 1)...
    &  Diff_sel_d(3:2:(h-1), 2:2:(w-2), 1))...
    .* Diff_med_d(3:2:(h-1), 2:2:(w-2), 1)...
 + (  ~Diff_sel_v(3:2:(h-1), 2:2:(w-2), 1)...
    & ~Diff_sel_h(3:2:(h-1), 2:2:(w-2), 1)...
    & ~Diff_sel_d(3:2:(h-1), 2:2:(w-2), 1)...
    &  Diff_sel_u(3:2:(h-1), 2:2:(w-2), 1))...
    .* Diff_med_u(3:2:(h-1), 2:2:(w-2), 1)...
 + Pix_in(3:2:(h-1), 2:2:(w-2), 2)), 0, 255);               % G@R


% R @ GR

Pix_out(3:2:(h-1), 3:2:(w-1), 1) = clip((...
       Diff_sel_v(3:2:(h-1), 3:2:(w-1), 1)...
    .* Diff_med_v(3:2:(h-1), 3:2:(w-1), 1)...
 + (  ~Diff_sel_v(3:2:(h-1), 3:2:(w-1), 1)...
    &  Diff_sel_h(3:2:(h-1), 3:2:(w-1), 1))...
    .* Diff_med_h(3:2:(h-1), 3:2:(w-1), 1)...
 + (  ~Diff_sel_v(3:2:(h-1), 3:2:(w-1), 1)...
    & ~Diff_sel_h(3:2:(h-1), 3:2:(w-1), 1)...
    &  Diff_sel_d(3:2:(h-1), 3:2:(w-1), 1))...
    .* Diff_med_d(3:2:(h-1), 3:2:(w-1), 1)...
 + (  ~Diff_sel_v(3:2:(h-1), 3:2:(w-1), 1)...
    & ~Diff_sel_h(3:2:(h-1), 3:2:(w-1), 1)...
    & ~Diff_sel_d(3:2:(h-1), 3:2:(w-1), 1)...
    &  Diff_sel_u(3:2:(h-1), 3:2:(w-1), 1))...
    .* Diff_med_u(3:2:(h-1), 3:2:(w-1), 1)...
 + Pix_in(3:2:(h-1), 3:2:(w-1), 2)), 0, 255);               % GR


% R @ GB

Pix_out(2:2:(h-2), 2:2:(w-2), 1) = clip((...
       Diff_sel_v(2:2:(h-2), 2:2:(w-2), 1)...
    .* Diff_med_v(2:2:(h-2), 2:2:(w-2), 1)...
 + (  ~Diff_sel_v(2:2:(h-2), 2:2:(w-2), 1)...
    &  Diff_sel_h(2:2:(h-2), 2:2:(w-2), 1))...
    .* Diff_med_h(2:2:(h-2), 2:2:(w-2), 1)...
 + (  ~Diff_sel_v(2:2:(h-2), 2:2:(w-2), 1)...
    & ~Diff_sel_h(2:2:(h-2), 2:2:(w-2), 1)...
    &  Diff_sel_d(2:2:(h-2), 2:2:(w-2), 1))...
    .* Diff_med_d(2:2:(h-2), 2:2:(w-2), 1)...
 + (  ~Diff_sel_v(2:2:(h-2), 2:2:(w-2), 1)...
    & ~Diff_sel_h(2:2:(h-2), 2:2:(w-2), 1)...
    & ~Diff_sel_d(2:2:(h-2), 2:2:(w-2), 1)...
    &  Diff_sel_u(2:2:(h-2), 2:2:(w-2), 1))...
    .* Diff_med_u(2:2:(h-2), 2:2:(w-2), 1)...
 + Pix_in(2:2:(h-2), 2:2:(w-2), 2)), 0, 255);               % GB


% R @ B

Pix_out(2:2:(h-2), 3:2:(w-1), 1) = clip((...
       Diff_sel_v(2:2:(h-2), 3:2:(w-1), 1)...
    .* Diff_med_v(2:2:(h-2), 3:2:(w-1), 1)...
 + (  ~Diff_sel_v(2:2:(h-2), 3:2:(w-1), 1)...
    &  Diff_sel_h(2:2:(h-2), 3:2:(w-1), 1))...
    .* Diff_med_h(2:2:(h-2), 3:2:(w-1), 1)...
 + (  ~Diff_sel_v(2:2:(h-2), 3:2:(w-1), 1)...
    & ~Diff_sel_h(2:2:(h-2), 3:2:(w-1), 1)...
    &  Diff_sel_d(2:2:(h-2), 3:2:(w-1), 1))...
    .* Diff_med_d(2:2:(h-2), 3:2:(w-1), 1)...
 + (  ~Diff_sel_v(2:2:(h-2), 3:2:(w-1), 1)...
    & ~Diff_sel_h(2:2:(h-2), 3:2:(w-1), 1)...
    & ~Diff_sel_d(2:2:(h-2), 3:2:(w-1), 1)...
    &  Diff_sel_u(2:2:(h-2), 3:2:(w-1), 1))...
    .* Diff_med_u(2:2:(h-2), 3:2:(w-1), 1)...
 + Pix_in(2:2:(h-2), 3:2:(w-1), 2)), 0, 255);               % G@B


% B @ R

Pix_out(3:2:(h-1), 2:2:(w-2), 3) = clip((...
       Diff_sel_v(3:2:(h-1), 2:2:(w-2), 2)...
    .* Diff_med_v(3:2:(h-1), 2:2:(w-2), 2)...
 + (  ~Diff_sel_v(3:2:(h-1), 2:2:(w-2), 2)...
    &  Diff_sel_h(3:2:(h-1), 2:2:(w-2), 2))...
    .* Diff_med_h(3:2:(h-1), 2:2:(w-2), 2)...
 + (  ~Diff_sel_v(3:2:(h-1), 2:2:(w-2), 2)...
    & ~Diff_sel_h(3:2:(h-1), 2:2:(w-2), 2)...
    &  Diff_sel_d(3:2:(h-1), 2:2:(w-2), 2))...
    .* Diff_med_d(3:2:(h-1), 2:2:(w-2), 2)...
 + (  ~Diff_sel_v(3:2:(h-1), 2:2:(w-2), 2)...
    & ~Diff_sel_h(3:2:(h-1), 2:2:(w-2), 2)...
    & ~Diff_sel_d(3:2:(h-1), 2:2:(w-2), 2)...
    &  Diff_sel_u(3:2:(h-1), 2:2:(w-2), 2))...
    .* Diff_med_u(3:2:(h-1), 2:2:(w-2), 2)...
 + Pix_in(3:2:(h-1), 2:2:(w-2), 2)), 0, 255);               % G@R


% B @ GR

Pix_out(3:2:(h-1), 3:2:(w-1), 3) = clip((...
       Diff_sel_v(3:2:(h-1), 3:2:(w-1), 2)...
    .* Diff_med_v(3:2:(h-1), 3:2:(w-1), 2)...
 + (  ~Diff_sel_v(3:2:(h-1), 3:2:(w-1), 2)...
    &  Diff_sel_h(3:2:(h-1), 3:2:(w-1), 2))...
    .* Diff_med_h(3:2:(h-1), 3:2:(w-1), 2)...
 + (  ~Diff_sel_v(3:2:(h-1), 3:2:(w-1), 2)...
    & ~Diff_sel_h(3:2:(h-1), 3:2:(w-1), 2)...
    &  Diff_sel_d(3:2:(h-1), 3:2:(w-1), 2))...
    .* Diff_med_d(3:2:(h-1), 3:2:(w-1), 2)...
 + (  ~Diff_sel_v(3:2:(h-1), 3:2:(w-1), 2)...
    & ~Diff_sel_h(3:2:(h-1), 3:2:(w-1), 2)...
    & ~Diff_sel_d(3:2:(h-1), 3:2:(w-1), 2)...
    &  Diff_sel_u(3:2:(h-1), 3:2:(w-1), 2))...
    .* Diff_med_u(3:2:(h-1), 3:2:(w-1), 2)...
 + Pix_in(3:2:(h-1), 3:2:(w-1), 2)), 0, 255);               % GR


% B @ GB

Pix_out(2:2:(h-2), 2:2:(w-2), 3) = clip((...
       Diff_sel_v(2:2:(h-2), 2:2:(w-2), 2)...
    .* Diff_med_v(2:2:(h-2), 2:2:(w-2), 2)...
 + (  ~Diff_sel_v(2:2:(h-2), 2:2:(w-2), 2)...
    &  Diff_sel_h(2:2:(h-2), 2:2:(w-2), 2))...
    .* Diff_med_h(2:2:(h-2), 2:2:(w-2), 2)...
 + (  ~Diff_sel_v(2:2:(h-2), 2:2:(w-2), 2)...
    & ~Diff_sel_h(2:2:(h-2), 2:2:(w-2), 2)...
    &  Diff_sel_d(2:2:(h-2), 2:2:(w-2), 2))...
    .* Diff_med_d(2:2:(h-2), 2:2:(w-2), 2)...
 + (  ~Diff_sel_v(2:2:(h-2), 2:2:(w-2), 2)...
    & ~Diff_sel_h(2:2:(h-2), 2:2:(w-2), 2)...
    & ~Diff_sel_d(2:2:(h-2), 2:2:(w-2), 2)...
    &  Diff_sel_u(2:2:(h-2), 2:2:(w-2), 2))...
    .* Diff_med_u(2:2:(h-2), 2:2:(w-2), 2)...
 + Pix_in(2:2:(h-2), 2:2:(w-2), 2)), 0, 255);               % GB


% B @ B

Pix_out(2:2:(h-2), 3:2:(w-1), 3) = clip((...
       Diff_sel_v(2:2:(h-2), 3:2:(w-1), 2)...
    .* Diff_med_v(2:2:(h-2), 3:2:(w-1), 2)...
 + (  ~Diff_sel_v(2:2:(h-2), 3:2:(w-1), 2)...
    &  Diff_sel_h(2:2:(h-2), 3:2:(w-1), 2))...
    .* Diff_med_h(2:2:(h-2), 3:2:(w-1), 2)...
 + (  ~Diff_sel_v(2:2:(h-2), 3:2:(w-1), 2)...
    & ~Diff_sel_h(2:2:(h-2), 3:2:(w-1), 2)...
    &  Diff_sel_d(2:2:(h-2), 3:2:(w-1), 2))...
    .* Diff_med_d(2:2:(h-2), 3:2:(w-1), 2)...
 + (  ~Diff_sel_v(2:2:(h-2), 3:2:(w-1), 2)...
    & ~Diff_sel_h(2:2:(h-2), 3:2:(w-1), 2)...
    & ~Diff_sel_d(2:2:(h-2), 3:2:(w-1), 2)...
    &  Diff_sel_u(2:2:(h-2), 3:2:(w-1), 2))...
    .* Diff_med_u(2:2:(h-2), 3:2:(w-1), 2)...
 + Pix_in(2:2:(h-2), 3:2:(w-1), 2)), 0, 255);               % G@B

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