% projectQuery.m
% Projects query CD covers into rectified squares.

clc;
printTitle('Projecting query images ...');

% Loop over all input images
if ~exist('queryFilenames', 'var')
    load(SURFQueryMAT);
end
if ~exist('databaseFilenames', 'var')
    load(SURFDatabaseMAT);
end
if exist('queryImagesGray', 'var')
    clear queryImagesGray;
end
numImages = length(queryFilenames);
allImages = 1:numImages;
corners = load('data/CD-corners.txt');
for nImage = 19:50
    
    disp(sprintf('\nProjecting image %d ... \n', nImage));
    
    plotRows = 3;
    plotCols = 2;
    
    % Show original query image
    imgColor = imread(queryFilenames{nImage});
    figure(1); clf;
    subplot(plotRows,plotCols,1);
    warning off;
    imshow(imgColor); title(sprintf('Original Query %d', nImage));
    warning on;
    pause(3);
    
    % Show database image
    nImageDB = queryToDatabaseNumbers(nImage);
    imgColorDB = imread(databaseFilenames{nImageDB});
    subplot(plotRows,plotCols,2);
    warning off;
    imshow(imgColorDB); title('Database');
    warning on;
    pause(3);
    
    % Project query into database space
    frame = queryFrames{nImage};
    desc = queryDescriptors{nImage};
    frameDB = databaseFrames{nImageDB};
    descDB = databaseDescriptors{nImageDB};
    imgColorProj = projectCD(imgColor, frame, desc, imgColorDB, frameDB, descDB, corners(nImage,:));
    
    % Show projection
    subplot(plotRows,plotCols,3);
    warning off;
    imshow(imgColorProj); title('Projected Query');
    warning on;
    pause(3);
    
    % Project 1st RGB query into database space
    if SURFEnhanced
        enhancedTag = 'enhanced';
    else
        enhancedTag = 'basic';
    end
    restoreName1stRGB = sprintf('%s%.2d-%s-1stRGB.jpg', restoreDir, nImage, enhancedTag);
    restoreName1stLAB = sprintf('%s%.2d-%s-1stLAB.jpg', restoreDir, nImage, enhancedTag);
    restoreName2ndRGB = sprintf('%s%.2d-%s-2ndRGB.jpg', restoreDir, nImage, enhancedTag);
    restoreName1stRGBM = sprintf('%s%.2d-%s-1stRGBM.jpg', restoreDir, nImage, enhancedTag);
    
    if ~exist(restoreName1stRGB, 'file')
        continue;
    end
    imgColor1stRGB = imread(restoreName1stRGB);
    imgColor1stRGBProj = projectCD(imgColor1stRGB, frame, desc, imgColorDB, frameDB, descDB, corners(nImage,:)); 
    
    % Show projection
    subplot(plotRows,plotCols,4);
    warning off;
    imshow(imgColor1stRGBProj); title('Projected Query - 1st RGB');
    warning on;
    pause(3);
    
    % Project 1st LAB query into database space
    if ~exist(restoreName1stLAB, 'file')
        continue;
    end
    imgColor1stLAB = imread(restoreName1stLAB);
    imgColor1stLABProj = projectCD(imgColor1stLAB, frame, desc, imgColorDB, frameDB, descDB, corners(nImage,:));
    
    % Show projection
    subplot(plotRows,plotCols,5);
    warning off;
    imshow(imgColor1stLABProj); title('Projected Query - 1st LAB');
    warning on;
    pause(3);

    % Save projected images
    projName = sprintf('%s%.2d-%s-orig-proj.jpg', restoreDir, nImage, enhancedTag);
    projName1stRGB = sprintf('%s%.2d-%s-1stRGB-proj.jpg', restoreDir, nImage, enhancedTag);
    projName1stLAB = sprintf('%s%.2d-%s-1stLAB-proj.jpg', restoreDir, nImage, enhancedTag);
    imwrite(imgColorProj, projName);
    imwrite(imgColor1stRGBProj, projName1stRGB);
    imwrite(imgColor1stLABProj, projName1stLAB);
    
    % Clean up
    pause(10);
    clear imgColor*;
    
end % nImage