%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                           UPRM, ECE Department                                  %
%                       INEL 5046 Pattern Recognition                             %
%                         Profesor: Luis O. Jimenez                               %
%                        Routine to open BIL images                               %
%                              pixels = imgbil                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            Descripcion de Variables                             %
%                          filename: nombre de la imagen                          %
%                          bands: numero de bandas                                %
%                          lines: numero de lineas                                %
%                          columns: numero de columnas                            %
%      pixelsize: numero del 0 al 13 que representa la precision del pixel        %
%  b2u vector que contiene los numeros de bandas a ser utilizadas en el analisis  %
% Revised by EAC 2002                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function pixels = imgbil

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Rutina para cargar la imagen  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[filename, pathname]=uigetfile('*.*','load image');
image=sprintf('%s%s',pathname,filename);
fid = fopen(image,'r','s');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Prompt para obtener valores   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

bands = input('\nEnter the Number of Bands \n');
lines = input('\nEnter the Number of Rows\n');
columns = input('\nEnter the Number of Columns\n');
pixelsize = input('\nEnter the Pixel Size\n');
b2u = 1:bands;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Determina psize: precision de pixel          %
% nb: numero de bytes para la precision psize  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

switch(pixelsize)   
case 1           
   psize = ('char'); 
   nb = 1;
case 2
   psize = ('uchar');
   nb = 1;
case 3
   psize = ('schar');
   nb = 1;
case 4
   psize = ('int8');
   nb = 1;
case 5
   psize = ('int16');
   nb = 2;
case 6
   psize = ('int32');
   nb = 4;
case 7
   psize = ('int64');
   nb = 8;
case 8
   psize = ('uint8');
   nb = 1;
case 9
   psize = ('uint16');
   nb = 2;
case 10
   psize = ('uint32');
   nb = 4;
case 11
   psize = ('uint64');
   nb = 8;
case 12
   psize = ('float32');
   nb = 4;
case 13
   psize = ('float64');
   nb = 8;
case 14
   psize = ('double');
   nb = 8;
end  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% dimensiones para la matriz de output pixels  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

newcolumns = lines*columns;
[i newlines] = size(b2u);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ajuste para obtener los offsets al primer elemento de cada banda escogida %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

b=b2u-1;
c=b*nb*columns;

pixels=zeros(newcolumns,newlines);

for n= 1:newlines,
   fseek(fid,c(1,n),'bof');
   j=1;
   for m= 1:(lines),
      temp=fread(fid,columns,psize); 
      pixels(j:j+columns-1,n)=temp;
      fseek(fid,(bands-1)*columns*nb,'cof');
      j=j+columns;
   end
end
fclose(fid);
pixels = pixels';

