function [out] = pngconvert(FNOM, RES, FLAG, OTIF);
% PNGCONVERT  Creates a PNG version of an EPS (or PS) figure
%
%   Use:
%
%     >> pngconvert('filename.eps', RES, FLAG);
%
%   Where:
%
%     RES  = PNG resolution (default = 300)
%     FLAG = convert PNG file back to EPS (for LaTeX)
%            if -1 (or absent), only PNG file created
%            if 0, EPS file deleted; PNG file retained
%            if 1, EPS file created; PNG file retained
%            if 2, EPS file created; PNG file retained; EPS file moved to
%                  original name
%            if 3, EPS file created; PNG file deleted
%            if 4, EPS file created; PNG file deleted; EPS file moved to
%                  original name
%     OTIF = if present, this argument triggers the creation of an
%            additional TIFF file (using 'tiff12nc')

nargs = nargin;
if nargs == 0
  error ('Please supply some inputs');
elseif nargs == 1
  res  = 300;
  flag = -1;
  otif  = 0;
elseif nargs == 2
  res  = RES;
  flag = -1;
  otif  = 0;
elseif nargs == 3
  res  = RES;
  flag = FLAG;
  otif  = 0;
elseif nargs == 4
  res  = RES;
  flag = FLAG;
  otif  = 1;
else
  error ('Too many inputs; try again');
end

% check for file extension
errflag = 0;
if FNOM(end-3:end) == '.eps'
  % do nothing
elseif FNOM(end-2:end) == '.ps'
  % do nothing
else
  % and this might not always work ...
  fprintf('- No file extension; adding one\n');
  FNOM1 = [FNOM '.eps'];
  FNOM2 = [FNOM '.ps'];
  val1 = size(dir(FNOM1));
  val2 = size(dir(FNOM2));
  if val1(1) == 1, FNOM = FNOM1; fprintf('- New file name = %s\n', FNOM); end
  if val2(1) == 1, FNOM = FNOM2; fprintf('- New file name = %s\n', FNOM); end
  if val1(1) == 0 && val2(1) == 0
    error('- No useable file exists');
    errflag = 1;
  end
end

if errflag == 0
  
  % sort out filename, etc.
  fprintf('- Processing file %s\n', FNOM);
  if FNOM(end-2:end) == 'eps',
    pnom = sprintf('%s.png', FNOM(1:end-4));
    fnom = sprintf('%s_PNG.eps', FNOM(1:end-4));
  else,
    pnom = sprintf('%s.png', FNOM(1:end-3));
    fnom = sprintf('%s_PNG.eps', FNOM(1:end-3));
  end
  
  % convert EPS to PNG
  fprintf('- Creating PNG file %s\n', pnom);
  str = sprintf('system (''gs -r%d -dEPSCrop -dTextAlphaBits=4 -sDEVICE=png16m -sOutputFile=%s -dBATCH -dNOPAUSE %s'');\n', ...
    res, pnom, FNOM);
  fprintf('%s', str);
  eval(str);
  
  % create TIFF file if requested
  if otif == 1
    tnom = sprintf('%s.tiff', FNOM(1:end-4));
    fprintf('- Creating TIFF file %s\n', tnom);
    str = sprintf('system (''gs -r%d -dEPSCrop -dTextAlphaBits=4 -sDEVICE=tiff12nc -sOutputFile=%s -dBATCH -dNOPAUSE %s'');\n', ...
      res, tnom, FNOM);
    fprintf('%s', str);
    eval(str);
  end
  
  % anything else?
  
  % delete EPS file (2016-08-22)
  if flag == 0
    % delete EPS
    fprintf('- Deleting EPS file %s\n', FNOM);
    str = sprintf('system(''rm %s'');\n', FNOM);
    fprintf('%s', str);
    eval(str);
  end
  
  % create EPS file from PNG file
  if flag > 0
    % convert back to EPS
    fprintf('- Creating EPS file %s\n', fnom);
    str = sprintf('system(''convert %s eps2:%s'');\n', pnom, fnom);
    fprintf('%s', str);
    eval(str);
  end
  
  % delete intermediate file?
  if flag > 2
    fprintf('- Deleting PNG file %s\n', pnom);
    str = sprintf('system(''rm %s'');\n', pnom);
    fprintf('%s', str);
    eval(str);
  end
  
  % delete intermediate file?
  if flag == 2 | flag == 4
    fprintf('- Moving *_PNG.eps file to %s\n', FNOM);
    str = sprintf('system(''mv %s %s'');\n', fnom, FNOM);
    fprintf('%s', str);
    eval(str);
  end
  
end
