function output=blur(img,w)

img=double(img);

d=w*2+1

s=size(img)

output=[]

for r=1:d:s(1)

row=[];

for c=1:d:s(2)

if r+w<=s(1) && c+w<=s(2) && r-w>=1 && c-w>=1

tot=sum(sum(img((r-w):(r+w),(c-w):(c+w))));

val=tot/(d*d);

nxt=ones(d,d);

nxt(1:end,1:end)=val;

row=[row nxt];

elseif c+w<=s(2) && r-w>=1 && c-w>=1

tot=sum(sum(img((r-w):end,(c-w):(c+w))));

val=tot/(d*(s(1)-r+w));

nxt=ones((s(1)-r+w),d);

nxt(1:end,1:end)=val;

row=[row nxt];

elseif r+w<=s(1) && r-w>=1 && c-w>=1

tot=sum(sum(img((r-w):(r+w),(c-w):end)));

val=tot/(d*(s(2)-c+w));

nxt=ones(d,(s(2)-c+w));

nxt(1:end,1:end)=val;

row=[row nxt];

elseif r+w<=s(1) && c+w<=s(2) && r-w>=1

tot=sum(sum(img((r-w):(r+w),1:(c+w))));

val=tot/(d*(c+w));

nxt=ones(d,(c+w));

nxt(1:end,1:end)=val;

row=[row nxt];

elseif r+w<=s(1) && c+w<=s(2) && c-w>=1

tot=sum(sum(img(1:(r+w),(c-w):(c+w))));

val=tot/(d*(r+w));

nxt=ones((r+w),d);

nxt(1:end,1:end)=val;

row=[row nxt];

elseif r+w<=s(1) && c+w<=s(2)

tot=sum(sum(img(1:(r+w),1:(c+w))));

val=tot/((r+w)*(c+w));

nxt=ones((c+w),(r+w));

nxt(1:end,1:end)=val;

row=[row nxt];

elseif r-w>=1 && c-w>=1

tot=sum(sum(img((r-w):end,(c-w):end)));

val=tot/((s(1)-r+w)*(s(2)-c+w));

nxt=ones((s(1)-r+w),(s(2)-c+w));

nxt(1:end,1:end)=val;

row=[row nxt];

elseif r+w<=s(1) && c-w>=1

tot=sum(sum(img(1:(r+w),(c-w):end)));

val=tot/((r+w)*(s(2)-c+w));

nxt=ones((r+w),(s(2)-c+w));

nxt(1:end,1:end)=val;

row=[row nxt];

elseif c+w<=s(2) && r-w>=1

tot=sum(sum(img((r-w):end,1:(c+w))));

val=tot/((s(1)-r+w)*(c+w));

nxt=ones((s(1)-r+w),(c+w));

nxt(1:end,1:end)=val;

row=[row nxt];

end

end

output=[output;row];

end

row=[]

s2=size(output)

if s2(1)<s(1)

for c=1:d:s(2)

if c+w<=s(2) && c-w>=1

tot=sum(sum(img((s2(1)+1):s(1),(c-w):(c+w))));

val=tot/((s(1)-s2(1))*d);

nxt=ones((s(1)-s2(1)),d);

nxt(1:end,1:end)=val;

row=[row nxt];

elseif c+w<=s(2)

tot=sum(sum(img((s2(1)+1):s(1),1:(c+w))));

val=tot/((s(1)-s2(1))*(c+w));

nxt=ones((s(1)-s2(1)),(c+w));

nxt(1:end,1:end)=val;

row=[row nxt];

elseif c-w>=1

tot=sum(sum(img((s2(1)+1):s(1),(c-w):end)));

val=tot/((s(1)-s2(1))*(s2(2)-c+w));

nxt=ones((s(1)-s2(1)),(s2(2)-c+w));

nxt(1:end,1:end)=val;

row=[row nxt];

end

end

output=[output;row];

end

row=[]

col=[]

s3=size(output)

if s3(2)<s(2)

for r=1:d:s(1)

if r+w<=s(1) && r-w>=1

tot=sum(sum(img((r-w):(r+w),(s3(2)+1):s(2))));

val=tot/(d*(s(2)-s3(2)));

nxt=ones(d,(s(2)-s3(2)));

nxt(1:end,1:end)=val;

col=[col;nxt];

elseif r+w<=s(1)

tot=sum(sum(img(1:(r+w),(s3(2)+1):s(2))));

val=tot/((r+w)*(s(2)-s3(2)));

nxt=ones((r+w),(s(2)-s3(2)));

nxt(1:end,1:end)=val;

col=[col;nxt];

elseif r-w>=1

tot=sum(sum(img((r-w):(r+w),(s3(2)+1):s(2))));

val=tot/((s3(1)-r+w)*(s(2)-s3(2)));

nxt=ones((s3(1)-r+w),(s(2)-s3(2)));

nxt(1:end,1:end)=val;

col=[col;nxt];

end

end

s4=size(col)

if s4(1)<s3(1) || s4(1)<s(1)

tot=sum(sum(img((s2(1)+1):s(1),(s3(2)+1):s(2))));

val=tot/((s(1)-s2(1))*(s(2)-s3(2)));

nxt=ones((s(1)-s2(1)),(s(2)-s3(2)));

nxt(1:end,1:end)=val;

col=[col;nxt];

end

output=[output col];

end

output=uint8(output);