%Experiment with resolution of SVAVISCA camera 
%30 Aug, 2001
%B. Micusik, micusb1@cmp.felk.cvut.cz

clear all;
%close all;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fields for setting
const_mult = [8]; %[1 2 4];           %how many times heigher resolution
				  
pixel_compare = 0; %comparing ratio of conventional camera and SVAVISCA pixels
                   %as a function of distance from the center of imager
				   %1...Yes,   0...No
				   
img_wanted = [0 0 0 0 1 0 0];  %which images will be computed 
					   %1. image with a grid
					   %2. image with Tomas Svoboda
					   %3. outdoor image of Prague1
					   %4. outdoor image of Prague2
					   %5. outdoor image of Prague3
					   %6. outdoor image of Prague4
					   %7. lab(oscar)
img_resample = 1; %resample images to SVAVISCA imager coordinates  1..Yes, 0..No					   
					   
write_file = 1;   %write results to bmp file 1..Yes, 0..No					   
cut_fovea = 1;  	  %cut fovea from image  1..Yes, 0..No
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for cc=1:size(const_mult,2)
c1=const_mult(cc);
%wanted resolution of SVAVISCA
num_pxl_ring=c1*252;
num_rings_retina=c1*110;
num_rings_fovea=c1*42;

%constant for existing SVAVISCA
k=1.02337;            %growth
y1=110;               %number of rings in retina area
ro0=272.73;           %[micrometer] radius of fovea

%computation of new size of pixel and function of growth
x2=num_pxl_ring;
y2=num_rings_retina;

k2=exp(y1*log(k)/y2);
rx=2*pi*ro0/x2;              %px  ... pixel size in x axis
ry=ro0*(k2-1);               %py  ... pixel size in y axis

spar = [rx ry rx ry k2 num_rings_fovea num_rings_retina num_pxl_ring];
s_k2=['growth rate: ' num2str(k2)];
s_rx=['pixel size in x axis: ' num2str(rx)];
s_ry=['pixel size in y axis: ' num2str(ry)];
disp(s_k2);
disp(s_rx);
disp(s_ry);

fi = 0;

for i=1:size(img_wanted,2)
 if(img_wanted(i)==1)
  switch(i)
   case 1,
	pno = [245 2 2/245 248 246];       % svavisca parameters for cylinder.bmp  
	src_name='cylinder';
   case 2,
    pno = [472 1 1/472 496 496];       % svavisca parameters for svob1.bmp	
	src_name='svob1';
   case 3,
	fi=3*pi/2;
    pno = [323 1 1/323 330 333];       % svavisca parameters for prague1.bmp  
	src_name='prague1';
   case 4,
	fi=3*pi/2;
    pno = [334 1 1/334 339 333];       % svavisca parameters for prague2.bmp  
	src_name='prague2';
   case 5,
	fi=4.8*pi/2;
    pno = [349 1 1/349 352 347];       % svavisca parameters for prague3.bmp  
	src_name='prague3';
   case 6,
	fi=3*pi/2;
    pno = [348 1 1/348 353 348];       % svavisca parameters for prague4.bmp  
	src_name='prague4';
   case 7,
	fi=3*pi/2;
    pno = [193 1 1/193 198 195];       % svavisca parameters for prague4.bmp  
	src_name='ov-oscar01a';
	
  end

  %%%%%%%%%%%%%pixel_compare%%%%%%%%%%%%%
  if(pixel_compare)
              %we go to generate coordinates for SVAVISCA imager, 
			  %1.row contains radius of rings in micrometer
			  %2.row contains sizes of pixels
	sv_rings=[ry:ry:ro0];               %fovea area
	sv_rings=[sv_rings;ry*ones(1,size(sv_rings,2))];
	min_ro = ro0*k2^1;                  %retina area
    max_ro = ro0*k2^num_rings_retina;	
	for ii=1:num_rings_retina
       akt_ro=ro0+ro0*k2^ii;
	   lst_ro=ro0+ro0*k2^(ii-1);
       sv_rings=[sv_rings [akt_ro;akt_ro-lst_ro]];	  %computation of size of pixel
	end
	
	sv_range_ro=sv_rings(1,size(sv_rings,2));
	
	ratio=[];
	for ii=1:pno(1)
	  sv_pxl_size=ii*sv_range_ro/pno(1);
	  rr=interp1(sv_rings(1,:),sv_rings(2,:),sv_pxl_size);
      ratio=[ratio 1/(rr*pno(1)/sv_range_ro)];
	end
	
%	figure;
    plot(1:pno(1),ratio,'-','linewidth',3);
	hold on;
    text(5,ratio(1)+0.15,[num2str(num_pxl_ring) 'x' num2str(num_rings_fovea+num_rings_retina)]);
	
	if(size(get(gca,'children'),1)<3)
  	  plot([0,pno(1)],[1,1],'--');
  	  xlabel('radius in standard image[pixels]');
	  ylabel('pixel size of standard/pixel size of SVAVISCA camera');
      title('Pixel size as a function of the distance from the center of image for different SVAVISCA imagers');
	  set(gca,'YLim',[0 10]);
	  switch(i)
		case {1},
   	       leg=legend('OSCAR 768x576','   diameter 490');
		case {2},
   	       leg=legend('PULNIX 1000x1000','   diameter 944');
	  end
	  hh=get(leg,'children');
	  set(hh(1:2),'visible','off');
	  set(hh(3),'position',[0.1 0.5]);
	end
  end
  
  %%%%%%%%%%%%%img_resample%%%%%%%%%%%%%%
  if(img_resample)
    [im map]=imread([src_name '.bmp']);
    figure;
    imshow(im,map);
  
    switch(i)
	 case {1,2,7},
	      im=ind2gray(im,map);
  	  	  map=gray(256);
          imt = svsim(im,pno,fi,spar);        %simulation of SVAVISCA 
    	  ims = imt((2+cut_fovea*num_rings_fovea):end,:);
		  ims=ims*255;
      case {3,4,5,6},
          imr=im(:,:,1); img=im(:,:,2); imb=im(:,:,3);
          imsr = svsim(imr,pno,fi,spar); disp('rr');
          imsg = svsim(img,pno,fi,spar); disp('gg');
          imsb = svsim(imb,pno,fi,spar); disp('bb');
  
          %finally we must put them together
          imt=ones(size(imsr,1),size(imsr,2),3);
          imt(:,:,1)=imsr;  imt(:,:,2)=imsg; imt(:,:,3)=imsb;
  		  ims = imt((2+cut_fovea*num_rings_fovea):end,:,:);
		  clear imt imsr imsg imsb;
		  ims=ims/255;
    end

    figure;
    imshow(ims,map);
    
	%%%%%%%%%%write_file%%%%%%%%%%%%%%%%%%
    if(write_file)
      dst_name=[src_name '_' int2str(c1) 'x.bmp'];
      if(size(ims,3) == 3) imwrite(ims,dst_name,'bmp');
  	  else
  		imwrite(ims,map,dst_name,'bmp');
  	  end %if
    end %if write_file
  end  %if img_resample	
 end %if img_wanted
end  %for
end  %for const_mult