%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% READING AND VISUALIZING THE IMAGES IN THE IPL COLOR DATABASE %
%                                                              % 
%           V. Laparra, S. Jimenez and J. Malo                 %   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This is a demo about:
%    (1) how you have to use the downloaded files to access the 
%        individual images in the database.
%    (2) how to properly visualize the images.
%
% ----------------------
% (1) Reading the Images
% ----------------------
%
% The database is organized in three kinds of images (see web for details):
%    * Munsell  (10 scenes)
%    * Natural I (50 scenes)
%    * Natural II (15 scenes)
% and two illuminants are available for each scene, CIE D65 and CIE A.
%
% Each group of scenes with certain illumination is stored in a different file:
%
%    * images_munsell_A  (273 Mb)
%    * images_munsell_D  (273 Mb)
%
%    * images_natural_I_A  (1.36 Gb)
%    * images_natural_I_D  (1.36 Gb)
%
%    * images_natural_II_A  (420 Mb)
%    * images_natural_II_D  (420 Mb)
%
% If the RAM memory of your system is less than 2Gb you may have problems to 
% load the large files. In order to avoid this problem, the images are organized 
% in a different way in the large and small files.
%
% In the small files ( < 1Gb), images are organized in a single struct variable 
% with the same name as the corresponding file and stored in the field 'images'.
% For instance, in order to access one particular image of the Munsell group, 
% e.g. n-th image of the set under A illumination, you have to do this:
%

     load images_munsell_A
     im_n_A = images_munsell_A(n).images;   % Where n = 1:10 in this group

% However, in larger files, information is broken in pieces so you can load 
% them one by one at your convenience: instead of using a single struct variable,
% images are organized in several struct variables
% In this case, each file contains 5 of such structures with 10 images each. 
% The name of the n-th struct is:
%             name_of_file_n
% So, assuming the picture order in the web page (left-right and up-bottom),
% if you want to access the 26th image of the Natural I set under both A and 
% D illuminations, first you just have to load the third piece (struct variable) 
% for both illuminants:

    load ('images_natural_I_A', 'images_natural_I_A_3')
    load ('images_natural_I_D', 'images_natural_I_D_3')
    
% and then go for the 6-th image in that pices:
    
    im_26_A = images_natural_I_A_3(6).images;
    im_26_D = images_natural_I_D_3(6).images;

% A simple example to load and (wrongly!) visualize all the images
% in the Natural I set under A illuminant is this:
    
    for i=1:50
        piece = ceil(i/10); 
        load('images_natural_I_A',['images_natural_I_A_',num2str(piece)])
        n = i - (piece-1)*10;
        eval(['im_A = images_natural_I_A_',num2str(piece),'(n).images;'])
        figure,imagesc(im_A/max(im_A(:))),title(['Image', num2str(i)])
        pause
        clear all
    end
    
% -------------------------------------------
% (2) Appropriate visualization of the Images
% -------------------------------------------
%
% When displying images in Matlab, using the absolute CIE XYZ values as 
% if they were device-dependent RGB digital counts is incorrect. 
% Therefore, appropriate visualization is only possible if you have a
% calibrated display and you know the function to transform the meaningful
% (device-independent) XYZ values into the meaningless (device-dependent) 
% RGB values carelessly used in computers.
% 
% Congratulations if you have your own calibration routines!
%
% If not, you can use COLORLAB 
%     http://www.uv.es/vista/vistavalencia/software/colorlab.html
% for a proper visualization.
%
% Below we suggest a piece of COLORLAB-based code for an approximately
% correct visualization. Only "approximately correct" because it is using 
% the default standard calibration file that comes with COLORLAB.
% Better visualization would involve calibrating your own display 
% (according to the available procedure in COLORLAB) and using that specific
% calibration file.
%
% Uncomment and execute beyond this line after installing COLORLAB 
% (note the particular path in loading the colorimetric parameters!)
% 
%   % Load CIE XYZ colorimetric data 
%
%       [T_l,Yw,Mbx]=loadsysm('/colorlab/colordat/systems/ciexyz.mat');
%
%   % Load Standard CRT calibration data
%
%       [tm,a,g]=loadmonm('/colorlab/colordat/monitor/std_crt.mat',Mbx);
%
%   % Load the 6th Munsell images (as described above)
%
%       load images_munsell_A
%       im_6_A = images_munsell_A(6).images;
%       load images_munsell_D
%       im_6_D = images_munsell_D(6).images;
%
%   % Quantize the total numbers of different colors in the image to 'q' colors
%   %
%   %  The more correct visualization approach would involve transforming *all* the 
%   %  colors from XYZ to your particular RGB representation, but the problem is that 
%   %  each image may have millions of colors (each pixel may have different color) 
%   %  and this the transform would take a while.
%   %  A convenient alternative is reducing the number of XYZ colors prior to the 
%   %  transformation to RGB. In our case, as in classical VGA palettes, we used 
%   %  q = 256, colors (you may increase q as you wish).
%
%       [X_A,map_A] = true2pal(im_6_A,256);
%       [X_D,map_D] = true2pal(im_6_D,256);
%
%   % Compute digital values RGB from (a small number of) tristimulus vectors XYZ
%
%       [n_A,saturat_A,Tn_A] = tri2val(map_A,Yw,tm,a,g,8);
%       [n_D,saturat_D,Tn_D] = tri2val(map_D,Yw,tm,a,g,8);
%
%   % Reconstruct the image using the digital values
%
%       im_6_RGB_A = reshape(n_A(X_A,:),1000,1280,3);
%       im_6_RGB_D = reshape(n_D(X_D,:),1000,1280,3);
%
%   % Visualize the image, using any display-function of Matlab
%
%       figure, 
%       subplot(121), imagesc(im_6_RGB_A),axis image,axis off,title('Image 6 under A illuminant')
%       subplot(122), imagesc(im_6_RGB_D),axis image,axis off,title('Image 6 under D65 illuminant')
%       