function [r,g,b]=bayer(filename);
% [r,g,b] = bayer(filename);
%
% Written by Bao-Jun Jiang
%
% This function is used to create a sub-sampled Bayer pattern 
% from a full-color jpeg image file.  It is done by properly 
% dropping two color values at each pixel.  The bayer pattern 
% created is that of the following
%
%       GRGRGRGR...
%       BGBGBGBG...
%       GRGRGRGR...
%       BGBGBGBG...
%
%
% The function returns three matrices for r, g, and b. If a 
% color value is dropped at a pixel, a value of 0 is stored 
% at that pixel in the corresponding color matrix.


[r,g,b]=jpgread(filename);

[NumOfRow, NumOfColumn]=size(r);
noInfo=0;  % This is used as a indicator

N1=round(NumOfRow/2);
N2=round(NumOfColumn/2);
F1=floor(NumOfRow/2);
F2=floor(NumOfColumn/2);

%==========================================
% Drop r and b info at every other pixel
% Note that even and odd rows are different
%==========================================
for i=1:N1,
  for j=1:N2,
	r(2*i-1,2*j-1)=noInfo;
	b(2*i-1,2*j-1)=noInfo;
  end;
end;

for i=1:F1,
  for j=1:F2,
        r(2*i,2*j)=noInfo;
        b(2*i,2*j)=noInfo;
  end;
end;


%==========================================
% Drop g and b info at every other pixel
% on odd (1st, 3rd, 5th, ... ) rows
%==========================================
for i=1:N1,
  for j=1:F2,
        g(2*i-1,2*j)=noInfo;
        b(2*i-1,2*j)=noInfo;
  end;
end;

%==========================================
% Drop r and g info at every other pixel
% on even (2nd, 4th, 4th, ... ) rows
%==========================================
for i=1:F1,
  for j=1:N2,
        r(2*i,2*j-1)=noInfo;
        g(2*i,2*j-1)=noInfo;
  end;
end;
	





