function redimg = reduce(img)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS223b HOMEWORK 1, "HALVE IMAGE DIMENSIONS"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Emilio Antunez (1/27/2003)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% this is the 5x5 blurring kernel used in lecture
kernel = [1;4;6;4;1]*[1 4 6 4 1]/16^2;

% for cleaner notation, we want the dimensions of img in an array
dim = size(img);

% add padding rows/columns for filtering, as suggested in B&A, '83
img = [2*img(:,1)-img(:,3)  2*img(:,1)-img(:,2)  img  2*img(:,dim(2))-img(:,dim(2)-1)];
img = [2*img(1,:)-img(3,:); 2*img(1,:)-img(2,:); img; 2*img(dim(1),:)-img(dim(1)-1,:)];

% lowpass filter image (while implicitly removing padding with 'valid')
redimg = filter2(kernel,img,'valid');

% subsample image at 2:1 ratio
redimg = redimg(1:2:dim(1),1:2:dim(2));
