function [picLoc, resizeFactor] = findPicLoc(picSize, picCtr, PTBParams, varargin)
%--------------------------------------------------------------------------
% function picLoc = findPicLoc(picSize, picCtr,PTBParams,[mode],[scalingFactor])
%
% DESCRIPTION: Given a texture of a particular size, a desired location for
% the center of the picture, and a scaling factor, this function finds the
% screen coordinates that describe the edges of the image, which can be
% used with Screen('DrawTexture') to draw the image.
%
% INPUTS:
%  picSize: a 2-element array with the size of the picture along the y and
%  x dimensions (yes, in that order). This is retrieved in this order
%  automatically if you use the makeTxtrFromImg function.
%
%  picCtr: a 2-element array giving the x- and y-coordinates for the CENTER
%  of the image, specified as a percentage of the screen (e.g., [.5, .5]
%  would be at the exact center of the screen; [.25, .5] would be off to
%  the left, but centered vertically; [.5, .25] would be centered
%  horizontally but would display in the top quarter of the screen)
%
%  PTBParams: a structure containing information about the screen and
%  screen size
%
%  [mode]: specified mode for determining image size (see details below)
%
%  [scalingFactor]: combined with mode, specifies how to size the image as a
%  percentage (e.g. .5) of either the original image size (default) or the
%  screen size
%
% OUTPUTS:
%  picLoc: a 4-element array specifying the left, top, right, and bottom
%  edges of the image in x and y coordinates
%
%  resizeFactor: size as a percentage of the original image (differs from
%  scalingFactor only if you are resizing as a function of screen size, not
%  as a function of image size
%
% USAGE:
%  The function has two modes for finding the edge locations:
%
%  Example 1: mode = 'PicPct' (default)
%  This will display the image as a percentage of the original picture size
%  (in pixels). The example below will display the image at 40% of its
%  original size centered on the coordinates x = 800, y = 600.
%
%  [rectLocation, resizeFactor] = findPicLoc([480, 240], [.5, .5],
%  'PicPct', .4)
%
%  Example 2 - mode = 'ScreenPct'
%  Image displayed as a percentage of the screen dimensions. This can be
%  useful if you are running an experiment on computers with different
%  screen sizes but want the image to display consistently relative to the
%  screen size on all displays. The example below will display the image so
%  that it displays in the upper right-hand corner and takes up 50% of the
%  screen along the x and y dimensions
%
%  Note: if the screen dimensions and picture dimensions do not match, the
%  function finds a 'middle-ground' scaling factor based on both the x- and
%  y-dimensions
%
% [rectLocation, resizeFactor] = findPicLoc([480, 240], [.75, .25],
% 'ScreenPct', .5)
%
% Author: Cendri Hutcherson
% Last Modified: May 27, 2018
%--------------------------------------------------------------------------

if isempty(varargin) % specify defaults
    mode = 'PicPct';
    resizeFactor = 1;
else
    mode = varargin{1};

    switch mode
        case 'PicPct'
            resizeFactor = varargin{2};
        case 'ScreenPct'
            % get current percentages (width and height) of screen real
            % estate
            currentPct = picSize([2,1])./(PTBParams.ctr * 2);
            % approximate resizeFactor by rescaling mean of current % by
            % desired %
            resizeFactor = varargin{2}/mean(currentPct);
    end
end

% create a matrix for center, as well as adjustments needed horizontally
% and vertically
ctrPt = [PTBParams.ctr(1) * picCtr(1)*2,...
         PTBParams.ctr(2) * picCtr(2)*2,...
         PTBParams.ctr(1) * picCtr(1)*2,...
         PTBParams.ctr(2) * picCtr(2)*2];
picSize = (picSize([2,1]) * resizeFactor)/2;

% determine the left, top, right, and bottom edges
picLoc = ctrPt + [-1*picSize,picSize];