% NormalizedHistogram - histogram = NormalizedHistogram(imageIn,numBins,min,max,device) 
function histogram = NormalizedHistogram(imageIn,numBins,min,max)
    % check for Cuda capable devices
    curPath = which('ImProc.Cuda');
    curPath = fileparts(curPath);
    n = ImProc.Cuda.DeviceCount();

    % if there are devices find the availble one and grab the mutex
    if (n>0)
       foundDevice = false;
       device = -1;
       
       while(~foundDevice)
        for deviceIdx=1:n
            mutexfile = fullfile(curPath,sprintf('device%02d.txt',deviceIdx));
            if (~exist(mutexfile,'file'))
                try
                       fclose(fopen(mutexfile,'wt'));
                catch errMsg
                       continue;
                end
                foundDevice = true;
                device = deviceIdx;
                break;
            end
        end
        if (~foundDevice)
            pause(2);
        end
       end
       
       try
            histogram = ImProc.Cuda.NormalizedHistogram(imageIn,numBins,min,max,device);
        catch errMsg
        	delete(mutexfile);
        	throw(errMsg);
        end
        
        delete(mutexfile);
    else
        histogram = lclNormalizedHistogram(imageIn,numBins,min,max);
    end
end

function histogram = lclNormalizedHistogram(imageIn,numBins,min,max)
    if nargin < 1 || nargin > 5
        error('Incorrect number of inputs!')
    end
    
    if length(size(imageIn))>3
        error('Image can have a maximum of three dimensions!')
    end
    
    if isempty(numBins) || ~exist('numBins') || numBins == 0
        histogram = imhist(imageIn(imageIn > min)) ./ sum(imhist(imageIn(imageIn > min)));
    else
        histogram = imhist(imageIn(imageIn > min),numBins) ./ sum(imhist(imageIn(imageIn > min),numBins));
    end
end