function [Pix_out] = correlation4(Pix_in)
%
%CORRELATION4  Performs correlation-based R and B extrapolation,
%              referenced to G
%
%   Usage: [Pix_out] = correlation4(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/22/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);

%======================================================================
% 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 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));

%======================================================================
% 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)...
         .* fix(...
            (  Colordiff(2:2:(h-2), 2:2:(w-2), 1)...  % R@GB - GB  1
             + Colordiff(4:2:h,     2:2:(w-2), 1)...  % R@GB - GB  2
            )/2)...
 + (  ~Diff_sel_v(3:2:(h-1), 2:2:(w-2), 1)...
    &  Diff_sel_h(3:2:(h-1), 2:2:(w-2), 1))...
         .* fix(...
            (  Colordiff(3:2:(h-1), 1:2:(w-3), 1)...  % R@GR - GR  1
             + Colordiff(3:2:(h-1), 3:2:(w-1), 1)...  % R@GR - GR  2
            )/2)...
 + (  ~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))...
         .* fix(...
            (  Colordiff(2:2:(h-2), 1:2:(w-3), 1)...  % R@B  - G@B 1
             + Colordiff(4:2:h,     3:2:(w-1), 1)...  % R@B  - G@B 2
            )/2)...
 + (  ~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))...
         .* fix(...
            (  Colordiff(4:2:h,     1:2:(w-3), 1)...  % R@B  - G@B 1
             + Colordiff(2:2:(h-2), 3:2:(w-1), 1)...  % R@B  - G@B 2
             )/2)...
 + 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)...
         .* fix(...
            (  Colordiff(2:2:(h-2), 3:2:(w-1), 1)...  % R@B  - G@B 1
             + Colordiff(4:2:h,     3:2:(w-1), 1)...  % R@B  - G@B 2
             )/2)...
 + (  ~Diff_sel_v(3:2:(h-1), 3:2:(w-1), 1)...
    &  Diff_sel_h(3:2:(h-1), 3:2:(w-1), 1))...
         .* fix(...
            (  Colordiff(3:2:(h-1), 2:2:(w-2), 1)...  % R@R  - G@R 1
             + Colordiff(3:2:(h-1), 4:2:w,     1)...  % R@R  - G@R 2
             )/2)...
 + (  ~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))...
         .* fix(...
            (  Colordiff(2:2:(h-2), 2:2:(w-2), 1)...  % R@GB - GB  1
             + Colordiff(4:2:h,     4:2:w,     1)...  % R@GB - GB  2
             )/2)...
 + (  ~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))...
         .* fix(...
            (  Colordiff(4:2:h,     2:2:(w-2), 1)...  % R@GB - GB  1
             + Colordiff(2:2:(h-2), 4:2:w,     1)...  % R@GB - GB  2
             )/2)...
 + 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)...
         .* fix(...
            (  Colordiff(1:2:(h-3), 2:2:(w-2), 1)...  % R@R  - G@R 1
             + Colordiff(3:2:(h-1), 2:2:(w-2), 1)...  % R@R  - G@R 2
             )/2)...
 + (  ~Diff_sel_v(2:2:(h-2), 2:2:(w-2), 1)...
    &  Diff_sel_h(2:2:(h-2), 2:2:(w-2), 1))...
         .* fix(...
            (  Colordiff(2:2:(h-2), 1:2:(w-3), 1)...  % R@B  - G@B 1
             + Colordiff(2:2:(h-2), 3:2:(w-1), 1)...  % R@B  - G@B 2
             )/2)...
 + (  ~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))...
         .* fix(...
            (  Colordiff(1:2:(h-3), 1:2:(w-3), 1)...  % R@GR - GR  1
             + Colordiff(3:2:(h-1), 3:2:(w-1), 1)...  % R@GR - GR  2
             )/2)...
 + (  ~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))...
         .* fix(...
            (  Colordiff(3:2:(h-1), 1:2:(w-3), 1)...  % R@GR - GR  1
             + Colordiff(1:2:(h-3), 3:2:(w-1), 1)...  % R@GR - GR  2
             )/2)...
 + 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)...
         .* fix(...
            (  Colordiff(1:2:(h-3), 3:2:(w-1), 1)...  % R@GR - GR  1
             + Colordiff(3:2:(h-1), 3:2:(w-1), 1)...  % R@GR - GR  2
             )/2)...
 + (  ~Diff_sel_v(2:2:(h-2), 3:2:(w-1), 1)...
    &  Diff_sel_h(2:2:(h-2), 3:2:(w-1), 1))...
         .* fix(...
            (  Colordiff(2:2:(h-2), 2:2:(w-2), 1)...  % R@GB - GB  1
             + Colordiff(2:2:(h-2), 4:2:w,     1)...  % R@GB - GB  2
             )/2)...
 + (  ~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))...
         .* fix(...
            (  Colordiff(1:2:(h-3), 2:2:(w-2), 1)...  % R@R  - G@R 1
             + Colordiff(3:2:(h-1), 4:2:w,     1)...  % R@R  - G@R 2
             )/2)...
 + (  ~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))...
         .* fix(...
            (  Colordiff(3:2:(h-1), 2:2:(w-2), 1)...  % R@R  - G@R 1
             + Colordiff(1:2:(h-3), 4:2:w,     1)...  % R@R  - G@R 2
             )/2)...
 + 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)...
         .* fix(...
            (  Colordiff(2:2:(h-2), 2:2:(w-2), 2)...  % B@GB - GB  1
             + Colordiff(4:2:h,     2:2:(w-2), 2)...  % B@GB - GB  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))...
         .* fix(...
            (  Colordiff(3:2:(h-1), 1:2:(w-3), 2)...  % B@GR - GR  1
             + Colordiff(3:2:(h-1), 3:2:(w-1), 2)...  % B@GR - GR  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))...
         .* fix(...
            (  Colordiff(2:2:(h-2), 1:2:(w-3), 2)...  % B@B  - G@B 1
             + Colordiff(4:2:h,     3:2:(w-1), 2)...  % B@B  - G@B 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))...
         .* fix(...
            (  Colordiff(4:2:h,     1:2:(w-3), 2)...  % B@B  - G@B 1
             + Colordiff(2:2:(h-2), 3:2:(w-1), 2)...  % B@B  - G@B 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)...
         .* fix(...
            (  Colordiff(2:2:(h-2), 3:2:(w-1), 2)...  % B@B  - G@B 1
             + Colordiff(4:2:h,     3:2:(w-1), 2)...  % B@B  - G@B 2
             )/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))...
         .* fix(...
            (  Colordiff(3:2:(h-1), 2:2:(w-2), 2)...  % B@R  - G@R 1
             + Colordiff(3:2:(h-1), 4:2:w,     2)...  % B@R  - G@R 2
             )/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))...
         .* fix(...
            (  Colordiff(2:2:(h-2), 2:2:(w-2), 2)...  % B@GB - GB  1
             + Colordiff(4:2:h,     4:2:w,     2)...  % B@GB - GB  2
             )/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))...
         .* fix(...
            (  Colordiff(4:2:h,     2:2:(w-2), 2)...  % B@GB - GB  1
             + Colordiff(2:2:(h-2), 4:2:w,     2)...  % B@GB - GB  2
             )/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)...
         .* fix(...
            (  Colordiff(1:2:(h-3), 2:2:(w-2), 2)...  % B@R  - G@R 1
             + Colordiff(3:2:(h-1), 2:2:(w-2), 2)...  % B@R  - G@R 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))...
         .* fix(...
            (  Colordiff(2:2:(h-2), 1:2:(w-3), 2)...  % B@B  - G@B 1
             + Colordiff(2:2:(h-2), 3:2:(w-1), 2)...  % B@B  - G@B 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))...
         .* fix(...
            (  Colordiff(1:2:(h-3), 1:2:(w-3), 2)...  % B@GR - GR  1
             + Colordiff(3:2:(h-1), 3:2:(w-1), 2)...  % B@GR - GR  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))...
         .* fix(...
            (  Colordiff(3:2:(h-1), 1:2:(w-3), 2)...  % B@GR - GR  1
             + Colordiff(1:2:(h-3), 3:2:(w-1), 2)...  % B@GR - GR  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)...
         .* fix(...
            (  Colordiff(1:2:(h-3), 3:2:(w-1), 2)...  % B@GR - GR  1
             + Colordiff(3:2:(h-1), 3:2:(w-1), 2)...  % B@GR - GR  2
             )/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))...
         .* fix(...
            (  Colordiff(2:2:(h-2), 2:2:(w-2), 2)...  % B@GB - GB  1
             + Colordiff(2:2:(h-2), 4:2:w,     2)...  % B@GB - GB  2
             )/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))...
         .* fix(...
            (  Colordiff(1:2:(h-3), 2:2:(w-2), 2)...  % B@R  - G@R 1
             + Colordiff(3:2:(h-1), 4:2:w,     2)...  % B@R  - G@R 2
             )/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))...
         .* fix(...
            (  Colordiff(3:2:(h-1), 2:2:(w-2), 2)...  % B@R  - G@R 1
             + Colordiff(1:2:(h-3), 4:2:w,     2)...  % B@R  - G@R 2
             )/2)...
 + Pix_in(2:2:(h-2), 3:2:(w-1), 2)), 0, 255);         % G@B

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