function [mr,s] = mpeg_read(moviefile,frames,show)
%
% [mr,s] = mpeg_read('moviefile',f,s)
%
% Read frames in vector f from mpeg movie moviefile.mpg
% into the variable mr, which is an array(m,n,3,length(f)).
%
% Here, m and n are the size of the individual frame, the
% second index maps into RGB colorspace.
%
% Optional input s generates generous output, also in figures.
% Optional output s contains error string if mpeg_read was not
% successful.
%
%
% mpeg_read uses convert by
%
% John Cristy, E.I. du Pont De Nemours and Company Incorporated
%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% written by ALW 08/24/01 for Linux                %
%                                                  %
% last modified by ALW, 08/24/10 for Linux         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%
% Site dependent settings -------------------------------------------------
%

packagedir = '/home/fs1/wiegmann/matlab/Movie/';

%
% No editing needed beyond this point ---------------------------------
%
% ---------------------------------------------------
% however, you may want to include your architecture
% and or system paths here.
% ---------------------------------------------------
comp = computer;
if sum(comp(1:3) ~= 'ALP') & sum(comp(1:3) ~= 'SGI') & ...
   sum(comp(1:3) ~= 'HP7') & sum(comp(1:3) ~= 'SUN') & ...
   sum(comp(1:3) ~= 'SOL') & sum(comp(1:3) ~= 'PCW')
  disp('mpeg_read currently set up for SUN SPARC, SUN SOLARIS,')
  disp('Intel PCs (Win 95 and NT), DEC ALPHA, SGI and HP only')
  mm = -1 ; error = -2;
  return
elseif sum(comp(1:3) == 'PCW')
  packagedir = 'c:\Matlab\Movie\';
  convert    = [packagedir,computer,'\convert'];
  rm         = ['del '];
else
  if exist([packagedir,computer,'/convert '])
    convert    = [packagedir,computer,'/convert '];
  else
    convert = unix('which convert')
    if any(convert(end-6:end) ~= 'convert')
      disp('Could not find convert in ALWs MPEG Movie distribution')
      disp('nor on your system --- this is fatal.') 
      error('contact andreas@familie-wiegmann.de for help with this problem')
    end
    disp('Could not find convert in ALWs MPEG Movie distribution')
    disp(['Using you own ImageMagick installation, ', ...
          'proceed with fingers crossed'])
  end
  if exist('/usr/bin/rm')
    rm         = ['/usr/bin/rm ']; % let string end in space!
  elseif exist('/usr/ucb/rm')
    rm         = ['/usr/ucb/rm ']; % let string end in space!
  elseif exist('/bin/rm')
    rm         = ['/bin/rm '];     % let string end in space!
  else disp('Having trouble finding rm, proceed with fingers crossed')
    rm         = ['rm '];          % let string end in space!
  end
end %if
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% REALLY NO EDITING needed beyond this point ---------------------------------
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('show')
  show = 0;
end
if show
  disp('Converting mpg to tif');
  [w,s] = unix([convert,' +adjoin ',moviefile,'.mpg ',...
      moviefile,'.tif'])
  else
    [w,s] = unix([convert,' +adjoin ',moviefile,'.mpg ',...
      moviefile,'.tif']);
  end
  D = dir([moviefile,'.tif.*']);
  if isempty(D)
    mr = 0;
    disp(s);
    return
  end
  f = unique(frames);
  l = length(moviefile)+6;
  e = zeros(size(D,1),1);
  for i=1:size(D,1)
    e(i) = str2num(D(i).name(l:end));
  end
  f = intersect(f,e);
  if isempty(f)
    disp('Attention: requested frames do not exist')
    s  = 'Attention: requested frames do not exist';
    mr = 0;
    return
  end
  
if length(f) ~= length(frames) % number of frames changed
    disp('Attention: frame order changed from')
    disp(frames)
    disp('to')
    disp(f)
  elseif f ~= frames % order of frames changed
    disp('Attention: frame order changed from')
    disp(frames)
    disp('to')
    disp(f)
  end
  
MF = max(f);
  
I       = imread([moviefile,'.tif.0']);
  [M,N,L] = size(I);
  l       = length(f)
  mr      = zeros(M,N,L,l);
  set(gcf,'Units','pixels','Position',[400 100 N M])
  for i=1:l
    I = imread([moviefile,'.tif.',num2str(f(i))]);
    mr(:,:,:,i) = I;
    if show
      imagesc(I); axis square; axis equal;
      axis off; axis tight; drawnow
    end
  end
  unix([rm,moviefile,'.tif.*']);
  s = ''
