%function reduce for the gaussian pyramid
%filter image and subsample

function g1 = reduce(g0,w)

[C,R]=size(g0);
g1=[];
%use gaussian filter with weight w
for I=2:(C+1)/2-1
   for J=2:(R+1)/2-1
      g1(I,J)=sum(sum(w.*g0(2*I-3:2*I+1,2*J-3:2*J+1)));
   end
end

%compute 4 edges of the output image
wtemp=w(:,3:5)/sum(sum(w(:,3:5)));
for I=2:(C-1)/2
   g1(I,1)=sum(sum(wtemp.*g0(2*I-3:2*I+1,1:3)));
end

wtemp=w(:,1:3)/sum(sum(w(:,1:3)));
for I=2:(C-1)/2
   g1(I,(R+1)/2)=sum(sum(wtemp.*g0(2*I-3:2*I+1,R-2:R)));
end

wtemp=w(3:5,:)/sum(sum(w(3:5,:)));
for I=2:(R-1)/2
   g1(1,I)=sum(sum(wtemp.*g0(1:3,2*I-3:2*I+1)));
end

wtemp=w(1:3,:)/sum(sum(w(1:3,:)));
for I=2:(R-1)/2
   g1((C+1)/2,I)=sum(sum(wtemp.*g0(C-2:C,2*I-3:2*I+1)));
end

%compute 4 corners of the output image
wtemp=w(3:5,3:5)/sum(sum(w(3:5,3:5)));
g1(1,1)=sum(sum(wtemp.*g0(1:3,1:3)));
wtemp=w(1:3,3:5)/sum(sum(w(1:3,3:5)));
g1((C+1)/2,1)=sum(sum(wtemp.*g0(C-2:C,1:3)));
wtemp=w(3:5,1:3)/sum(sum(w(3:5,1:3)));
g1(1,(R+1)/2)=sum(sum(wtemp.*g0(1:3,R-2:R)));
wtemp=w(1:3,1:3)/sum(sum(w(1:3,1:3)));
g1((C+1)/2,(R+1)/2)=sum(sum(wtemp.*g0(C-2:C,R-2:R)));

