function w = window(img, center, hwin)

[rows, cols] = size(img);

% Row range endpoints within the image
rs = max(1, center(1) - hwin);
re = min(rows, center(1) + hwin);

% Ditto for columns
cs = max(1, center(2) - hwin);
ce = min(cols, center(2) + hwin);

% Carve out the window from the image
if all(center == floor(center))
    integer = 1;
    crop = img(rs:re, cs:ce);
else
    integer = 0;
    crop = interp2(img, cs:ce, (rs:re)');
end

% Make an output window full of NaNs and of the desired size
wrows = 2*hwin + 1;
wcols = wrows;
w = NaN * ones(wrows, wcols);

% See where crop fits within the output window
wrs = max(1, 2 + hwin - round(center(1)));
wre = wrs + size(crop, 1) - 1;
wcs = max(1, 2 + hwin - round(center(2)));
wce = wcs + size(crop, 2) - 1;
w(wrs:wre, wcs:wce) = crop;