function [ Imageout ] = MotionAnalyst_AdjustImage( Image, BitDepth, edge_detect, edge_threshold, histo_adjust, brightness, contrast)
%MOTIONANALYST_ADJUSTIMAGE Applies brightness,contrast, and edge detection
%to an image

%   given an input, the function will modify the image by applying
%   adjustments in the following order.  Brightness/contrast followed by
%   edge detection.  The edge detection is the Sobel method with no edge
%   thinning.
%
% Author:	Robb Colbrunn, Cleveland Clinic BioRobotics and Mechanical Testing Core
% Date:		5-13-12
% Last change:  5-13-12
% Version:	1.0
% See documentation for complete change history
%Revision    Date     Author      Comment
%1.0         5-13-12  R.Colbrunn  Created as a common function for all of the 
                                % MotionAnalyst suite.
%
%License
%     Copyright (C) 2012  Colbrunn
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/>.
%%
Imageout=Image;
imagetype=class(Image);
RGB=size(size(Image));
range=(2^BitDepth-1);

            
        if histo_adjust == 1
            % Apply brightness and contrast first
            slope=2^contrast;
            b=0.5-slope*(1-brightness);
            hiinput=bound((1-b)/slope,1,0);
            loinput=bound(-b/slope,1,0);
            hioutput=bound(slope+b,1,0);
            looutput=bound(b,1,0);
            Imageout = single(Imageout)/range;
            Imageout = imadjust(Imageout,[loinput hiinput],[looutput hioutput]);
            Imageout = cast((Imageout*range),imagetype);

        end
     % then apply edge detection
        if edge_detect==1
            if RGB(2) > 2
                edge_image(:,:)=Imageout(:,:,1);%use red channel only.
            else 
                edge_image(:,:)=Imageout(:,:);
            end
            edge_image = single(edge_image)/range;
            %edge_threshold=edge_threshold*range
            edge_image=edge(edge_image,edge_threshold,'nothinning');% detect the edge
            
            if RGB(2) > 2
                for i = 1:3
                   Imageout(:,:,i)= edge_image;
                end
 
            else 
                Imageout = edge_image;
            end           
            Imageout = cast((Imageout*range),imagetype);
        end


%===========================================================
function [data] = bound(data, uplim, lolim)
% add cross hairs and circles to an image
if data >= uplim
    data = uplim;
elseif data <= lolim
    data = lolim;
else
    data = data;
end
%%
