## Copyright (C) 2017   Timothy Parys
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{RGB} =} demosiac (@var{I}, @var{sensorAlignment})
## Convert raw bayer image into color. Input image must use a
## @var{sensorAlignment} of type 'gbrg', 'grbg', 'bggr', or 'rggb'.
## @end deftypefn

## Author: Timothy Parys <tparys@gmail.com>
## Created: April 2017
## Version: 0.1

function RGB = demosiac(I,sensorAlignment)

% Check inputs
if size(size(I))(2) > 2
    error('Image must be two-dimensional')
elseif mod(size(I), 2) != [0 0]
    error('Image dimensions must be even')
end
if strcmp('str', class(sensorAlignment))
    error('Invalid value for sensorAlignment')
elseif sensorAlignment == 'gbrg'
    pat        = [0 0; 1 0];
    pat(:,:,2) = [1 0; 0 1];
    pat(:,:,3) = [0 1; 0 0];
elseif sensorAlignment == 'grbg'
    pat        = [0 1; 0 0];
    pat(:,:,2) = [1 0; 0 1];
    pat(:,:,3) = [0 0; 1 0];
elseif sensorAlignment == 'bggr'
    pat        = [0 0; 0 1];
    pat(:,:,2) = [0 1; 1 0];
    pat(:,:,3) = [1 0; 0 0];
elseif sensorAlignment == 'rggb'
    pat        = [1 0; 0 0];
    pat(:,:,2) = [0 1; 1 0];
    pat(:,:,3) = [0 0; 0 1];
else
    error('Invalid value for sensorAlignment')
end

% Image processing kernels
kern        = [ .25 .5 .25; .5 1 .5; .25 .5 .25 ];
kern(:,:,2) = [ 0 .25 0; .25 1 .25; 0 .25 0 ];
kern(:,:,3) = [ .25 .5 .25; .5 1 .5; .25 .5 .25 ];

% Loop for red, green, blue channels
s = size(I) / 2;
for i = 1:3
    
    % Extract all pixels in this channel
    channel = I .* repmat(pat(:,:,i), s);
    
    % Create border to avoid edge artifacts
    channel = padarray(channel, [1 1], 'reflect');
    
    % Run the kernel
    channel = imfilter(channel, kern(:,:,i));
    
    % Copy back everything but the border
    RGB(:,:,i) = channel(2:end-1, 2:end-1);
    
end
