%% Load data.

close all; clear; clc

%-----------------------------------------------
% Specify the name of the COMPASS root directory
dataPath='/path/to/COMPASS/repository/';
%-----------------------------------------------
addpath(genpath([dataPath 'Scripts/']))

dataFile=[dataPath 'Data/COMPASS_SceneInfo.xlsx'];
[~, txtData, allData]=xlsread(dataFile); header=txtData(1,:);
sceneInfo=cell2table(allData(2:end,:),'VariableNames',header);

screenSize=get(0,'ScreenSize');
inFolder=[dataPath 'Materials/E-Prime Task/'];
outFolderResized=[dataPath 'Materials/Resized Images/'];
outFolderSample=[dataPath 'Materials/Watermarked Images/'];

if ~exist(outFolderResized,'dir')
    mkdir(outFolderResized)
end

if ~exist(outFolderSample,'dir')
    mkdir(outFolderSample)
end

%% Resize and pad images with zeros. Determine image luminance and contrast.

for ii=1:height(sceneInfo)   
    scene=imread([inFolder sceneInfo.ImageFile{ii}]);
    [dim1, dim2, ~]=size(scene);
    if dim1<dim2
        orient=1; % Landscape orientation
    elseif dim1>dim2
        orient=2; % Portrait orientation
    end
    
    newdim2=round((dim1*4)/3,0); % Resize each image to 3:4 aspect ratio (500 x 675)
    extraPxls=(dim2-newdim2)/2; % Detemine how much to pad the top/bottom or the sides    
    sceneInfo.ImgOrientation(ii,1)=orient;   

    if ~strcmp(sceneInfo.ImageFile{ii},'scene993.jpg')
        % If the image has >675 dim2 pixels, resize the image (make dim2=675)
        % and pad the top & bottom with zeros (make dim1=500)
        if extraPxls > 0
            sceneResized=zeros(dim1,newdim2,3,'uint8'); % Create a black background (placeholder)
            imrsz=imresize(scene,[NaN,newdim2]); % Resize the image to make dim2=675
            padding=(dim1-size(imrsz,1))/2; % Determine how much to pad the top & bottom
            if mod(padding,1)==0 & padding~=0
                sceneResized(abs(padding):size(imrsz,1)+abs(padding)-1,:,:)=imrsz;
            elseif mod(padding,1)~=0 & padding~=0
                sceneResized(ceil(abs(padding)):size(imrsz,1)+abs(padding),:,:)=imrsz;
            elseif padding==0
                sceneResized=scene;
            end            
        % If we the image has <675 dim2 pixels, pad the the sides with zeros
        elseif extraPxls < 0
            sceneResized=zeros(dim1,newdim2,3,'uint8'); % Create a black background (placeholder)         
            if mod(extraPxls,1)==0
                sceneResized(:,abs(extraPxls):end-1-abs(extraPxls),:)=scene;
            else
                sceneResized(:,ceil(abs(extraPxls)):end-ceil(abs(extraPxls)),:)=scene;
            end            
        % If the scene doesn't need resizing, just copy it over
        elseif extraPxls==0
            sceneResized=scene;
        end     
    elseif strcmp(sceneInfo.ImageFile{ii},'scene993.jpg') % scene993.jpg should be cropped rather than padded
        sceneResized=scene(:,ceil(extraPxls):end-ceil(extraPxls),:);
    end
    
    fileName=[outFolderResized sceneInfo.ImageName{ii} '.png'];
    imwrite(sceneResized,fileName)
    
    % Determine luminance & contrast of the original image
    grayScene=rgb2gray(scene);
    % Luminance = average pixel value of the grayscaled image
    sceneInfo.Luminance(ii,1)=nanmean(reshape(double(grayScene),1,numel(grayScene)));
    % Contrast = standard deviation across all pixels of the grayscaled image
    sceneInfo.Contrast(ii,1)=nanstd(reshape(double(grayScene),1,numel(grayScene)));    
end

%% Watermark and shrink sample images.

for ii=1:height(sceneInfo)    
    scene=imread([outFolderResized sceneInfo.ImageName{ii} '.png']);
    [dim1, dim2, ~]=size(scene);
    imrsz=imresize(scene,[dim1/2,dim2/2]);
    f=figure('color',[.4 .4 .4]);
    xlim([0 dim2/2]); ylim([0 dim1/2]);set(gca,'Color',[.4 .4 .4]);axis off
    t=text(dim2/4,dim1/4,'For Review Only','FontSize',34,'Color','w',...
        'FontWeight','bold','HorizontalAlignment','center');
    txt=getframe; txtrsz=imresize(txt.cdata,[dim1/2,dim2/2]); close(f)
    f=figure('color','w'); a=imshow(imrsz,'Border','tight'); hold on
    b=imshow(txtrsz,'Border','tight'); alpha(b,.3); 
    fileName=[outFolderSample sceneInfo.ImageName{ii} '.png'];
    print(fileName,'-dpng'); close(f)
end
