function [R,G,B]=colorInterp1(r,g,b);
% [R,G,B]=colorInterp1(r,g,b);
%
% Written by Bao-Jun Jiang
%
% This function interpolates the colors r, g, and b for the
% bayer pattern (as created by the command bayer), and returns
% the [R,G,B] matrices with the missing color values filled
% by using the median interpolation algorithm.
% The bayer pattern is assumed to be that of the following
%
%	GRGRGRGR...
%	BGBGBGBG...
%	GRGRGRGR...
%	BGBGBGBG...
%


R=r;
G=g;
B=b;
[NumOfRow, NumOfColumn]=size(R);

%=============================================
%for pixels that contain red information only
%=============================================
%considering the first row
for j=1:round(NumOfColumn/2-1),
   G(1,2*j)=median([G(1,2*j-1) G(1,2*j+1) G(2,2*j)]);
   B(1,2*j)=median([B(2,2*j-1) B(2,2*j+1)]);
end;
if floor(NumOfColumn/2)==NumOfColumn/2
   G(1,NumOfColumn)=median([G(1,NumOfColumn-1) G(2,NumOfColumn)]);
   B(1,NumOfColumn)=B(2,NumOfColumn-1);
end;
	
%considering the 2nd and later rows
for i=1:floor(NumOfRow/2)-1,
   for j=1:round(NumOfColumn/2-1),
     G(2*i+1,2*j)=median([G(2*i,2*j) G(2*i+1,2*j-1)...
	     G(2*i+1,2*j+1) G(2*i+2,2*j)]);
       B(2*i+1,2*j)=median([B(2*i,2*j-1) B(2*i,2*j+1)...
	       B(2*i+2,2*j-1) B(2*i+2,2*j+1)]);
   end;

   if floor(NumOfColumn/2)==NumOfColumn/2  %ie,if NumOfColumn is even
       G(2*i+1,NumOfColumn)=median([G(2*i,NumOfColumn)...
	       G(2*i+1,NumOfColumn-1) G(2*i+2,NumOfColumn)]);
       B(2*i+1,NumOfColumn)=median([B(2*i,NumOfColumn-1)...
	       B(2*i+2,NumOfColumn-1)]);
   end;
end;
	
% discussing the special case for the last row
if round(NumOfRow/2)==(NumOfRow+1)/2  %ie, NumOfRow is odd
   for j=1:round(NumOfColumn/2-1),
      G(NumOfRow,2*j)=median([G(NumOfRow-1,2*j)...
	      G(NumOfRow,2*j-1) G(NumOfRow,2*j+1)]);
      B(NumOfRow,2*j)=median([B(NumOfRow-1,2*j-1)...
	      B(NumOfRow-1,2*j+1)]);
   end;
   if floor(NumOfColumn/2)==NumOfColumn/2
      G(NumOfRow,NumOfColumn)=median([G(NumOfRow-1,NumOfColumn)...
	      G(NumOfRow,NumOfColumn-1)]);
      B(NumOfRow,NumOfColumn)=B(NumOfRow-1,NumOfColumn-1);
    end; 
end;

%===============================================
%%for pixels that contain blue information only
%===============================================
for i=1:round(NumOfRow/2)-1,
   R(2*i,1)=median([R(2*i-1,2) R(2*i+1,2)]);
   G(2*i,1)=median([G(2*i-1,1) G(2*i,2) G(2*i+1,1)]);

   for j=1:floor(NumOfColumn/2)-1,
      R(2*i,2*j+1)=median([R(2*i-1,2*j) R(2*i-1,2*j+2)...
	      R(2*i+1,2*j) R(2*i+1,2*j+2)]);
      G(2*i,2*j+1)=median([G(2*i-1,2*j+1) G(2*i+1,2*j+1)...
	      G(2*i,2*j) G(2*i,2*j+2)]);
   end;
   if round(NumOfColumn/2)==(NumOfColumn+1)/2  %odd Num
       R(2*i,NumOfColumn)=median([R(2*i-1,NumOfColumn-1)...
	       R(2*i+1,NumOfColumn-1)]);
       G(2*i,NumOfColumn)=median([G(2*i-1,NumOfColumn)...
	       G(2*i,NumOfColumn-1) G(2*i+1,NumOfColumn)]);
   end;
end;

% discussing a special case of last row
if round(NumOfRow/2)==NumOfRow/2  %ie, if NumOfRow is even
   R(NumOfRow,1)=R(NumOfRow-1,2);
   G(NumOfRow,1)=median([G(NumOfRow-1,1) G(NumOfRow,2)]);
   for j=1:floor(NumOfColumn/2)-1,
        R(NumOfRow,2*j+1)=median([R(NumOfRow-1,2*j)...
		R(NumOfRow-1,2*j+2)]);
        G(NumOfRow,2*j+1)=median([G(NumOfRow-1,2*j+1)...
		G(NumOfRow,2*j)  G(NumOfRow,2*j+2)]);
   end;
   if round(NumOfColumn/2)==(NumOfColumn+1)/2  %odd Num
       R(NumOfRow,NumOfColumn)=R(NumOfRow-1,NumOfColumn-1);
       G(NumOfRow,NumOfColumn)=median([G(NumOfRow,NumOfColumn-1)...
	       G(NumOfRow-1,NumOfColumn)]);
   end;
end;

		
%===============================================
%%for pixels that contain green information only
%===============================================
%first row
R(1,1)=R(1,2);
B(1,1)=B(2,1);
for j=1:floor(NumOfColumn/2-1),
   R(1,2*j+1)=median([R(1,2*j) R(1,2*j+2)]);
   B(1,2*j+1)=B(2,2*j+1);
end;
if floor(NumOfColumn/2)==(NumOfColumn-1)/2  %ie, NumOfColumn is odd
   R(1,NumOfColumn)=R(1,NumOfColumn-1);
   B(1,NumOfColumn)=B(2,NumOfColumn);
end;

%other odd rows 

for i=1:floor(NumOfRow/2)-1,
   R(2*i+1,1)=R(2*i+1,2);
   B(2*i+1,1)=median([B(2*i,1) B(2*i+2,1)]);    
   for j=1:floor(NumOfColumn/2-1),
      R(2*i+1,2*j+1)=median([R(2*i+1,2*j) R(2*i+1,2*j+2)]);
      B(2*i+1,2*j+1)=median([B(2*i,2*j+1) B(2*i+2,2*j+1)]);
   end;
   if floor(NumOfColumn/2)==(NumOfColumn-1)/2  %ie, NumOfColumn is odd
      R(2*i+1,NumOfColumn)=R(2*i+1,NumOfColumn-1);
      B(2*i+1,NumOfColumn)=median([B(2*i,NumOfColumn)...
	      B(2*i+2,NumOfColumn)]);
   end;
end;

if floor(NumOfRow/2)==(NumOfRow-1)/2  %ie., NumOfRow is odd
   R(NumOfRow,1)=R(NumOfRow,2);
   B(NumOfRow,1)=B(NumOfRow-1,1);
   for j=1:floor(NumOfColumn/2-1),
      R(NumOfRow,2*j+1)=median([R(NumOfRow,2*j) R(NumOfRow,2*j+2)]);
      B(NumOfRow,2*j+1)=B(NumOfRow-1,2*j+1);
   end;
   if floor(NumOfColumn/2)==(NumOfColumn-1)/2  %ie, NumOfColumn is odd
      R(NumOfRow,NumOfColumn)=R(NumOfRow,NumOfColumn-1);
      B(NumOfRow,NumOfColumn)=B(NumOfRow-1,NumOfColumn);
   end;
end;

 
%even rows

for i=1:round(NumOfRow/2)-1,
   for j=1:round(NumOfColumn/2-1),
      R(2*i,2*j)=median([R(2*i-1,2*j) R(2*i+1,2*j)]);
      B(2*i,2*j)=median([B(2*i,2*j-1) B(2*i,2*j+1)]);
   end;
   if floor(NumOfColumn/2)==NumOfColumn/2  %ie, NumOfColumn is even
      R(2*i,NumOfColumn)=median([R(2*i-1,NumOfColumn)...
	      R(2*i+1,NumOfColumn)]);
      B(2*i,NumOfColumn)=B(2*i,NumOfColumn-1);
   end;
end;

if round(NumOfRow/2)==NumOfRow/2  %ie, NumOfRow is even
   R(NumOfRow,2)=R(NumOfRow-1,2);
   B(NumOfRow,2)=median([B(NumOfRow,1) B(NumOfRow,3)]);
   for j=2:round(NumOfColumn/2-1),
      R(NumOfRow,2*j)=R(NumOfRow-1,2*j);
      B(NumOfRow,2*j)=median([B(NumOfRow,2*j-1) B(NumOfRow,2*j+1)]);
   end;
   if floor(NumOfColumn/2)==NumOfColumn/2  %ie, NumOfColumn is even
      R(NumOfRow,NumOfColumn)=R(NumOfRow-1,NumOfColumn);
      B(NumOfRow,NumOfColumn)=B(NumOfRow,NumOfColumn-1);
   end;
end;







