% resolutiion_main.m
% main prgram structure
% ImageEngineering Dietmar Wueller / Uwe Artmann

function varargout = resolution_main(filedir,filename,ext,resdir,segments,periods,size_part,comma,starcut);


ImageIN=imread([filedir,filesep,filename,ext]);                 %read the image

[h,w,d]=size(ImageIN);            % height, width, dimensions of Input-Image

if d==3
        ImageIN=rgb2gray(ImageIN);   % convert image to grayscale if its RGB
end

g=figure('Name','Select ROI');
[XIM, YIM, STAR, rectIM]=imcrop(ImageIN);         % crop image        
close(g);

wh = waitbar(1/5,'Detecting Center','Position',[1 40 300 50]);

[centerx,centery]=resolution_center(STAR,starcut); % get center and reproduction scale

waitbar(2/5,wh,'Calibrating');

[STARCAL,CheckImage]=resolution_calibrate(STAR,centerx,centery); % calibrate the image

CheckImage([centery-4:centery+4],[centerx-4:centerx+4])=255;

waitbar(3/5,wh,'Calculating Distortion');

[ds,segfactor]=resolution_distortion(STARCAL,centerx,centery,segments);

waitbar(4/5,wh,'Calculating Modulation');     
        
[Results]=resolution_modulation(STARCAL,centerx,centery,segfactor,ds,size_part,segments,h,periods);
            
waitbar(5/5,wh,'Writing Results');  

resolution_print(Results,filedir,filename,segments,'Siemens',resdir,comma); 
resolution_plot(Results, segments,filename);
[CheckImage]=resolution_checkimage(CheckImage,ds,centerx,centery,periods);

imwrite(CheckImage,[resdir,'\',filename,'_','_check','.jpg']); 

close(wh);