function img = LMimread(D, ndx, HOMEIMAGES)
%
% Reads one image from the database. 
%     img = LMimread(database, ndx, HOMEIMAGES);
%
% This function is similar to LMread. Just for compatibility.

filename = fullfile(HOMEIMAGES, D(ndx).annotation.folder, D(ndx).annotation.filename);


if strcmp(filename(1:5), 'http:')
    filename = strrep(filename, '\', '/');
    filename = strrep(filename, '%20', ' '); % replace space character
    
    % read image from url (the next is more robust than imread)
    tempname = sprintf('tmp_%s.jpg', num2str(round(rand*100000)));
    status=0;
    while status == 0
        [tmpfile,status] = urlwrite(filename, tempname, 'Timeout', 20);
        if status == 0
            disp(sprintf('Warning: Failure reading img %s. Trying again...',filename))
            drawnow
        end
        if ~exist(tmpfile, 'file')
            disp(sprintf('Warning: img %s not downloaded. Trying again...',filename))
            status=0;
        end
    end
    img = imread(tmpfile);
    delete(tmpfile);
else
    filename = strrep(filename, '/', filesep);
    filename = strrep(filename, '\', filesep);
    
    % normal img read
    img = imread(filename);
end


