function [] = GenerateSiftDescriptors( imageFileList, outputFileList, gridSpacing, patchSize )
%function [] = GenerateSiftDescriptors( imageFileList, outputFileList,
%gridSpacing, patchSize )
%
%Generate the dense grid of sift descriptors for each image

fprintf('Building Sift Descriptors\n\n');

% parameters

parfor f = 1:size(imageFileList,1)

    % load image
    imageFName = imageFileList{f};
    outFName = outputFileList{f};
    
    if(size(dir(outFName),1)~=0)
        fprintf('Skipping %s\n', imageFName);
        continue;
    end
    
    I = sp_load_image(imageFName);

    [hgt wid] = size(I);

    % make grid (coordinates of upper left patch corners)
    remX = mod(wid-patchSize,gridSpacing);
    offsetX = floor(remX/2)+1;
    remY = mod(hgt-patchSize,gridSpacing);
    offsetY = floor(remY/2)+1;
    
    [gridX,gridY] = meshgrid(offsetX:gridSpacing:wid-patchSize+1, offsetY:gridSpacing:hgt-patchSize+1);

    fprintf('Processing %s: wid %d, hgt %d, grid size: %d x %d, %d patches\n', ...
             imageFName, wid, hgt, size(gridX,2), size(gridX,1), numel(gridX));

    % find SIFT descriptors
    siftArr = sp_find_sift_grid(I, gridX, gridY, patchSize, 0.8);
    siftArr = sp_normalize_sift(siftArr);
    
    make_dir(outFName);
    parsave(outFName, siftArr, gridX(:) + patchSize/2 - 0.5, gridY(:) + patchSize/2 - 0.5, wid, hgt);

end % for

end % function
