% extract random square subwindows from an image
% version 2 of randwindow
% it takes magnification factor mag as input
% possible inputs for mag: [10 20 40]
function windows = randwindow2_furkan(I,mag)

% I is a grayscale image matrix.
% windows is a s-by-3 matrix containing upper left coordinates and the edge
% length of selected subwindows.
% s is the number of extracted subwindows

[r c] = size(I);

% determine the factor for multiplication by the edge length
if mag == 10
    factor = 0.4;
elseif mag == 20
    factor = 0.66;
elseif mag == 40
    factor = 0.9;
else
    disp('error: magnification factor invalid');
end

% maximum edge length of subwindows
if(2/3 <= r/c && r/c <= 3/2)
    maxlength = ceil(min(r,c)*factor);
else
    maxlength = min(r,c);
end

minlength = ceil(maxlength/10);

windows = zeros(1,3);


% rowr and colr denote the upper left coordinates of the subwindow
rnd = randperm(r-minlength+1);
rowr = rnd(1);
rnd = randperm(c-minlength+1);
colr = rnd(1);

windows(1:2) = [rowr,colr];

% select a random edge length between minlength and maxlength

rnd_length = randperm(maxlength-minlength+1);
randlen = (minlength-1) + rnd_length(1);

while(~(randlen+rowr-1 <= r && randlen+colr-1 <= c))
    rnd_length = randperm(maxlength-minlength+1);
    randlen = (minlength-1) + rnd_length(1);
end

windows(3) = randlen;








