function readAsSigned = getReadBytesAsSigned(mRCImage)

% getReadBytesAsSigned  Determine whether to read a mode=0 file as signed 
%                       or unsigned bytes.
%
% This file is part of PEET (Particle Estimation for Electron Tomography).
% Copyright 2000-2012 The Regents of the University of Colorado & BLD3EMC:
%           The Boulder Laboratory For 3D Electron Microscopy of Cells.
% See PEETCopyright.txt for more details.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  $Author: John Heumann $
%
%  $Date: 2012/01/12 17:22:51 $
%
%  $Revision: 04b6cb6df697 $
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

readAsSigned = 0;
if mRCImage.header.mode ~= 0
  return;    % This flag is only relevant for files with mode = 0
end

readBytesEnvVal = getenv('READ_MODE0_SIGNED');
if ~isempty(readBytesEnvVal)
  readBytesEnvVal = str2double(readBytesEnvVal);
else
  readBytesEnvVal = 0;
end

if mRCImage.header.imodStamp == defaultIMODStamp()
  readAsSigned = mRCImage.header.imodFlags & 1;
  if readBytesEnvVal < -1
    readAsSigned = 0;
  elseif readBytesEnvVal > 1
    readAsSigned = 1;
  end
else
  % Try to choose type based on min / max density
  dmin = mRCImage.header.minDensity;
  dmax = mRCImage.header.maxDensity;
  if dmin < 0.0 && dmax < 128.0
    readAsSigned = 1;
  elseif dmin >= 0.0 && dmax >= 128.0
    readAsSigned = 0;
  elseif dmin < 0.0 && dmax > 128.0
    if -dmin > dmax - 128.0
      readAsSigned = 1;
    else
      readAsSigned = 0;
    end
  else
    readAsSigned = 0;  % Ambiguous... could be either signed or unsigned
  end
  % Let the environment variable over-ride automatic selection
  if readBytesEnvVal < 0
    readAsSigned = 0;
  elseif readBytesEnvVal > 0
    readAsSigned = 1;
  end
end
