function [x,r_xx,R_rr]=load_texture(id,M,N,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11)
% LOAD_TEXTURE  Load a texture image and its autocorrelation function
%       x=load_texture(id,M,N) loads the texture identified by the 
%       number id and of size MxN. If id is in the range of Brodatz 
%       numbers, a Brodatz texture is loaded, otherwise another texture 
%       is loaded, associated with that number.
%       
%       [x,r_xx]=load_texture(id,M,N) also loads the autocorrelation 
%       function for the texture from the autocorrelation directory.
%	If the file does not exist, new autocorr. is computed.
%       
%       ...=load_texture(...,'Offset',M_o,N_o) grabs the image using the
%       specified offset from the origin (which is the upper left corner). 
%       Currently only valid for Brodatz textures.
% 					
%       ...=load_texture(id,M,N,'HistEq') loads the texture image and
%       equalizes its histogram
% 					
%       ...=load_texture(id,M,N,'ZeroMean') loads the texture image, 
%       subtracts the mean value and gets the corresponding statistics.
%       
%       ...=load_texture(...,'NewStats') forces the computation of new
%	statistics
%       
%	r_xx=load_texture(...,'r_xx') only loads the autocorrelation
%	
%       See also: MSU_TEXTURE_ID
 
% Paths
MSUPath=[getenv('HOME') '/datafiler/textures/msu'];
BrodatzPath=[getenv('HOME') '/datafiler/textures/brodatz'];
VisTexPath=[getenv('HOME') '/datafiler/textures/vistex/index'];
MeasTexPath=[getenv('HOME') '/datafiler/textures/meastex/index'];
AltBrodatzPath=[getenv('HOME') '/datafiler/textures/brodatz_tmp'];
AutocorrPath=[getenv('HOME') '/datafiler/autocorrs'];

% Maximum supported filter sizes
M_h_max=15;
N_h_max=15;
M_w_max=41;
N_w_max=41;

% Load identification numbers
msu_texture_id

% Parse commandline options
Only_r_xx=0;
ZeroMean=0;
NewStats=0;
HistEq=0;
i=4;
M_o=0; N_o=0;
while i<=nargin,
  Command=eval(sprintf('P%d',i-3));
  if strcmp(lower(Command),lower('ZeroMean'))      ZeroMean=1;
  elseif strcmp(lower(Command),lower('r_xx')), 	   Only_r_xx=1;
  elseif strcmp(lower(Command),lower('NewStats'))  NewStats=1;
  elseif strcmp(lower(Command),lower('HistEq'))    HistEq=1;
  elseif strcmp(lower(Command),lower('Offset'))    
    M_o=eval(sprintf('P%d',i-2));
    N_o=eval(sprintf('P%d',i-1));
    i=i+2;
  else
    error(sprintf('Illegal parameter %s',Command));
  end
  i=i+1;
end


if     id == MSU_GMRF1,   
  Path=MSUPath; FileName='Che2-1.h.gif';     VRange=  0:127; HRange=  0:127; 
  TextureName='GMRF1';
elseif id == MSU_GMRF2,   
  Path=MSUPath; FileName='Che2-1.h.gif';     VRange=  0:127; HRange=128:255;
  TextureName='GMRF2';
elseif id == MSU_GMRF3,   
  Path=MSUPath; FileName='Che2-1.h.gif';     VRange=128:255; HRange=  0:127;
  TextureName='GMRF3';
elseif id == MSU_GMRF4,   
  Path=MSUPath; FileName='Che2-1.h.gif';     VRange=128:255; HRange=128:255;
  TextureName='GMRF4';
elseif id == MSU_L,       
  Path=MSUPath; FileName='L_and_+-8-3.h.gif';VRange= 80:159; HRange= 80:159;
  TextureName='L';
elseif id == MSU_Plus,    
  Path=MSUPath; FileName='L_and_+-8-3.h.gif';VRange=  0: 80; HRange=  0: 80;
  TextureName='Plus';
elseif id == MSU_S,       
  Path=MSUPath; FileName='S-IO2-3.h.gif';    VRange=  0:127; HRange=  0:127;
  TextureName='S';
elseif id == MSU_IO,      
  Path=MSUPath; FileName='S-IO2-3.h.gif';    VRange=  0:127; HRange=128:255;
  TextureName='IO';
elseif id == MSU_Even,    
  Path=MSUPath; FileName='even-odd2-1.h.gif';VRange=  0:127; HRange=  0:127;
  TextureName='Even';
elseif id == MSU_Odd,     
  Path=MSUPath; FileName='even-odd2-1.h.gif';VRange=128:255; HRange=  0:127;
  TextureName='Odd';
elseif id == MSU_Triangle,
  Path=MSUPath; FileName='tri-arr2-3.h.gif'; VRange=  0:127; HRange=  0:127;
  TextureName='Triangle';
elseif id == MSU_Arrow,   
  Path=MSUPath; FileName='tri-arr2-3.h.gif'; VRange=  0:127; HRange=128:255;
  TextureName='Arrow';
elseif id == MSU_triangle,
  Path=MSUPath; FileName='tri-arr2.hips.gif';VRange=  0:127; HRange=  0:127;
  TextureName='triangle';
elseif id == MSU_arrow,   
  Path=MSUPath; FileName='tri-arr2.hips.gif';VRange=128:255; HRange=  0:127;
  TextureName='arrow';
elseif id >=2000 & id<3000
  Path=VisTexPath;
  TextureName=sprintf('%04d',id-2000);
  FileName=[TextureName '.gif'];
  VRange=(0:M-1)+M_o;
  HRange=(0:N-1)+N_o;
elseif id >=3000 & id<4000
  Path=MeasTexPath;
  TextureName=sprintf('%04d',id-3000);
  FileName=[TextureName '.gif'];
  VRange=(0:M-1)+M_o;
  HRange=(0:N-1)+N_o;
else
  Path=BrodatzPath;
  TextureName=sprintf('D%d',id);
  FileName=[TextureName '.hips'];
  if exist(FileName)~=2
    Path=AltBrodatzPath;
  end
  VRange=(0:M-1)+M_o;
  HRange=(0:N-1)+N_o;
end

  
AutoCorrName=sprintf('r_xx_%s',TextureName);
if ~Only_r_xx
  if lower(FileName(length(FileName)-3:length(FileName)))=='.gif'
    % Load GIF file
    [x,map_x]=gifread([Path '/' FileName]);
    x=round(scale(ind2gray(x,map_x)));
    x=x(VRange+1,HRange+1);
  elseif lower(FileName(length(FileName)-4:length(FileName)))=='.hips'
    % Load HIPS file
    fprintf(1,'Load tx. #%d: ',id);
    x=hipsread([Path '/' FileName],VRange,HRange);
  else
    error('Unexpected file format');
  end
  
  if HistEq
    % Histogram equalization
    MinX=min(x(:));
    MaxX=max(x(:));
    x=MaxX*histeq((x-MinX)/(MaxX-MinX),MaxX-MinX+1)+MinX;
  end

  % If the extracted image is smaller than requested, pad it with its periodic
  % extension 
  while M>size(x,1),
    x=[x;x(1:min(size(x,1),M-size(x,1)),:)];
  end
  while N>size(x,2),
    x=[x x(:,1:min(size(x,2),N-size(x,2)))];
  end
 
  % Zero mean?
  if ZeroMean,
    x=x-mean(mean(x));
  end
end

% Modify file name
AutoCorrName=sprintf('%s_%03d_%03d',AutoCorrName,M_o,N_o);
if ZeroMean,
  AutoCorrName=[AutoCorrName '_zero_mean'];
end
if HistEq,
  AutoCorrName=[AutoCorrName '_histeq'];
end
 
% Autocorrelation loading or estimation if necessary
AutoCorrFileName=[AutocorrPath '/' AutoCorrName '.mat'];
if nargout > 1 | Only_r_xx
  if exist(AutoCorrFileName)==2 & ~NewStats
    eval(sprintf('load %s',AutoCorrFileName)); 
  else
    fprintf(1,'\tCompute statistics of texture...');
    r_xx=xcorrf2(x)/(size(x,1)*size(x,2));
    [M_r,N_r]=size(r_xx);
    ZeroLagV=(M_r+1)/2;
    ZeroLagH=(N_r+1)/2;
    r_xx=r_xx(ZeroLagV+(-M_h_max-M_w_max:M_h_max+M_w_max),...
              ZeroLagH+(-N_h_max-N_w_max:N_h_max+N_w_max));
    fprintf(1,' save...');
    if strcmp(version,'4.2c')
      eval(sprintf('save %s r_xx',AutoCorrFileName));
    else
      eval(sprintf('save %s r_xx -v4',AutoCorrFileName));
    end
    fprintf(1,' done\n');
  end
end

if Only_r_xx
  x=r_xx;
end


