function [A,Xdim,con_s,aniso_s,pol_s,phi_s]=features_preprocess(filename)

%%%% initialize random number generator
rand('seed',sum(100*clock));
%% workaround to avoid matcom bug--don't rely on setting state:
%%cl=clock;
%%rand(1,round(mod(cl(6)*100000,1000)));

xy_wt=1;

[labL,labA,labB]=corelread(filename);

[Ydim,Xdim]=size(labL);

% extract texture features using scale selection
L=labL;
texdesc1;

% set up 6D color/texture coordinates
X1=gaussv(labL/100,SSNEW);
X2=gaussv((labA+50)/100,SSNEW);
X3=gaussv((labB+50)/100,SSNEW);
X4=con_s.*aniso_s;
X5=con_s.*pol_s;
X6=con_s;

[X7,X8]=meshgrid(1:Xdim,1:Ydim);
X7=xy_wt*X7/max(Xdim,Ydim);
X8=xy_wt*X8/max(Xdim,Ydim);
if 1
   % add some noise to keep the covariance matrices from becoming singular
   X1=X1+0.005*randn(size(X1));
   X2=X2+0.005*randn(size(X2));
   X3=X3+0.005*randn(size(X3));
   X4=X4+0.005*randn(size(X4));
   X5=X5+0.005*randn(size(X5));
   X6=X6+0.005*randn(size(X6));
end

% format list of feature vectors,
% after eliminating border effects
X1=trimedge(X1,8);
X2=trimedge(X2,8);
X3=trimedge(X3,8);
X4=trimedge(X4,8);
X5=trimedge(X5,8);
X6=trimedge(X6,8);
X7=trimedge(X7,8);
X8=trimedge(X8,8);
[Ydim2,Xdim2]=size(X1);

A=[X1(:) X2(:) X3(:) X4(:) X5(:) X6(:) X7(:) X8(:)];

return


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function Y=gaussv(X,SSNEW);
% Y=gaussv(X,SSNEW);
% Gaussian smoothing with spatially-variant scale
% SSNEW is an array of the same size as X containing local-kernel sizes
 
SSNEW=round(SSNEW);
max_scale=max(SSNEW(:));

Y0=X;
Y=Y0;

for n=2:max_scale
   % alternate shifts to the left and right to keep image centered
   if (-1)^n==1
      stencil=0.5*[1 1];
   else
      stencil=0.5*[0 1 1];
   end
   Y0=conv2(conv2(Y0,stencil,'same'),stencil','same');
   SSn=(SSNEW==n);
   Y=(1-SSn).*Y+SSn.*Y0;
end

return



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 1997-1999. The Regents of the University
% of California (Regents). All Rights Reserved.
% 
% Permission to use, copy, modify, and distribute this software and its
% documentation for educational, research, and not-for-profit purposes,
% without fee and without a signed licensing agreement, is hereby
% granted, provided that the above copyright notice, this paragraph and
% the following two paragraphs appear in all copies, modifications, and
% distributions. Contact The Office of Technology Licensing, UC Berkeley,
% 2150 Shattuck Avenue, Suite 510, Berkeley, CA 94720-1620, (510)
% 643-7201, for commercial licensing opportunities. Created by
% Chad Carson, Serge Belongie, and Jitendra Malik, Department of Electrical
% Engineering and Computer Sciences, University of California, Berkeley.
% For technical information, contact carson@eecs.berkeley.edu.
% 
% IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
% SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
% ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
% REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
% 
% REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
% ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION
% TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
% MODIFICATIONS.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

