function [Pix_out] = scalarmedian3x3(Pix_in)
%
%SCALARMEDIAN3X3  Performs median filtering on the R and B planes in
%                 a 3x3 window, referenced to the G plane 
%
%   Usage: [Pix_out] = scalarmedian3x3(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/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(:,:,:);

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

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

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

%======================================================================
% G-Referenced R, B Median Filtering
%----------------------------------------------------------------------

% R Filtering

Pix_out(3:2:(h-1), 3:2:(w-1), 1) =...                          % R@GR
   clip(  fix(medfilt2(Colordiff(3:2:(h-1), 3:2:(w-1), 1))...  % R - G
        + Pix_in(3:2:(h-1), 3:2:(w-1), 2)), 0, 255);           % GR

Pix_out(2:2:(h-2), 2:2:(w-2), 1) =...                          % R@GB
   clip(  fix(medfilt2(Colordiff(2:2:(h-2), 2:2:(w-2), 1))...  % R - G
        + Pix_in(2:2:(h-2), 2:2:(w-2), 2)), 0, 255);           % GB

Pix_out(2:2:(h-2), 3:2:(w-1), 1) =...                          % R@B
   clip(  fix(medfilt2(Colordiff(2:2:(h-2), 3:2:(w-1), 1))...  % R - G
        + Pix_in(2:2:(h-2), 3:2:(w-1), 2)), 0, 255);           % G@B


% B Filtering

Pix_out(3:2:(h-1), 3:2:(w-1), 3) =...                          % B@GR
   clip(  fix(medfilt2(Colordiff(3:2:(h-1), 3:2:(w-1), 2))...  % B - G
        + Pix_in(3:2:(h-1), 3:2:(w-1), 2)), 0, 255);           % GR

Pix_out(2:2:(h-2), 2:2:(w-2), 3) =...                          % B@GB
   clip(  fix(medfilt2(Colordiff(2:2:(h-2), 2:2:(w-2), 2))...  % B - G
        + Pix_in(2:2:(h-2), 2:2:(w-2), 2)), 0, 255);           % GB

Pix_out(3:2:(h-1), 2:2:(w-2), 3) =...                          % B@R
   clip(  fix(medfilt2(Colordiff(3:2:(h-1), 2:2:(w-2), 2))...  % B - G
        + Pix_in(3:2:(h-1), 2:2:(w-2), 2)), 0, 255);           % G@R

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