function FileList = prjRenderSmart(FileList, Dir, Options)
% prjRenderSmart(FileList, Dir, Options)
%
% smart rendering of the whole FileList
%
% FileList - o structura provenita din dir, filtrata pe tipuri grafice
% Dir - working directory info
% Options - contains html, output resolutions, scaling, filtering
%
% v.0.46 - 2004.04.10, new, din prjRender, se uita la Options.smartscale, daca N then both
% - 2004.04.16, altfel : chemata doar cind Options.smartscale (to quick test)
% v.0.51 - 2006.01.05, progress bar updated (mesajele veki lasate inca in cmd window)
% v.0.5.2 - 2009.02.14, unified 'smart' and 'exact' rendering,
% give up on separate th, gy
% v.0.5.3 - 2011.07.05, give up on "integer" scaling, not really that much faster
% might swap for systems with less than 128MB RAM :-)
if nargin < 3, error('prjRenderSmart: no options to look at'); end;
%h1 = waitbar(0,'scaling...');
h1 = waitbar(0,'1','Name','start rendering...',...
'CreateCancelBtn',...
'setappdata(gcbf,''canceling'',1)');
setappdata(h1,'canceling',0)
noFiles = size(FileList,2);
for i=1: noFiles
if getappdata(h1,'canceling')
break
end
fn_src = fullfile(FileList(i).path, FileList(i).name);
fid_src = fopen(fn_src);
if fid_src < 0
%FileMissing(i) = 1;
fprintf(1,'\n%s', [ '>img ' num2str(i) 'missing source file']);
else
fclose(fid_src);
% test 'gy', only render if it doesn't exist
fn_dest_gy = fullfile(Dir.Dest, [FileList(i).base '_gy.jpg']);
fid_dest = fopen(fn_dest_gy);
if fid_dest > 0
fclose(fid_dest); img_info = imfinfo(fn_dest_gy);
fprintf(1, '\n%s', ['skipping image ' num2str(i) ' ... [ ' num2str(img_info.Height) '-by-' num2str(img_info.Width) ' ]']);
else
FileList(i) = imgRender(FileList(i), Dir, Options, i, noFiles, h1);
end;
end;
end
delete(h1) % DELETE the waitbar; don't try to CLOSE it.
end
function FileEntry = imgRender(FileEntry, Dir, Options, currFileNo, noFiles, hWaitbar, pref_Verbose)
% scale by the scalefactors found in FileEntry
% 02.07.2011 - fixed bug in Exact "scaling"
if nargin < 7, pref_Verbose = false; end
pref_interp_method = 'cubic';
thisString = ['scaling file ' num2str(currFileNo) '/' num2str(noFiles) ' - '];
fn_src = fullfile(FileEntry.path, FileEntry.name);
fn_dest_gy = fullfile(Dir.Dest, [FileEntry.base '_gy.jpg']);
fn_dest_th = fullfile(Dir.Dest, [FileEntry.base '_th.jpg']);
% --- read source file ---
if pref_Verbose
fprintf(1,'\n%s', [ '>img ' num2str(currFileNo)]);
end
waitbar(currFileNo/noFiles, hWaitbar, [thisString 'read image' ]);
if pref_Verbose
fprintf(1, '.read data '); % later: verify whether a _gy exists, scale from there
tic
end
img_data = imread(fn_src, FileEntry.type);
if pref_Verbose
toc
fprintf(['.done.gy / ' num2str(FileEntry.scalefactor_gy) '...'] );
end
waitbar(currFileNo/noFiles, hWaitbar, [thisString 'doing gy' ]);
% --- SCALE gy+th, 'exact', always ---
% --- do GY using iScale --- filtering was done in divNsmart,
% should replace it somewhere else :-)
if pref_Verbose, tic, end
img_data_gy = iScale(img_data, Options.GySize, pref_interp_method);
if pref_Verbose, toc, fprintf(1, '.write gy...'); end
waitbar(currFileNo/noFiles,hWaitbar, [thisString 'writing gy' ]);
if pref_Verbose, tic, end
imwrite(img_data_gy, fn_dest_gy, 'jpg','Quality', Options.GyQual);
if pref_Verbose, toc, fprintf('done'); end
FileEntry.gy_height = size(img_data_gy,1);
FileEntry.gy_width = size(img_data_gy,2);
clear img_data_gy
% --- do TH using iScale ---
waitbar(currFileNo/noFiles, hWaitbar, [thisString 'doing th' ]);
if pref_Verbose,
fprintf(['.th / ' num2str(FileEntry.scalefactor_th) '...'] );
tic
end
img_data_th = iScale(img_data, Options.ThSize, pref_interp_method);
if pref_Verbose,
toc, fprintf('.done.write th...'); tic
end
waitbar(currFileNo/noFiles, hWaitbar, [thisString 'writing th' ]);
imwrite(img_data_th, fn_dest_th, 'jpg','Quality', Options.ThQual);
if pref_Verbose, toc, fprintf('done'); end
FileEntry.th_height = size(img_data_th,1);
FileEntry.th_width = size(img_data_th,2);
clear img_data_th
waitbar(currFileNo/noFiles, hWaitbar, [thisString 'done' ]);
end
function img_int = iScale(raw, maxsize, method, pref_Verbose)
% scaleaza pastrind proportiile
% img_int = iScale(raw,maxsize, method);
%
% v.0.3 - viteza (cauta un intreg intii)
% v.0.5.2 - 14.02.2009, pasted here from own file
% v.0.5.3 - give up on "integer" scaling, not really that much faster
if nargin < 4, pref_Verbose = false; end;
if nargin < 2, maxsize = 150; end;
if nargin < 3, method = 'cubic'; end;
% check class of raw, in case uint16...
if strcmp(class(raw), 'uint16')
ScFactor_Val = 255/double(intmax(class(raw)));
raw = double(raw)*ScFactor_Val;
end
x = size(raw,2); y = size(raw,1);
ScFactor_Size = maxsize/max(x,y);
if ScFactor_Size < 1
raw = double(raw);
% scrie gridul de interpolare xi,yi
stepxy = 1/ScFactor_Size;
xi = 1:stepxy:x;
yi = 1:stepxy:y;
img_int(:,:,1) = interp2(1:x, 1:y, raw(:,:,1),xi,yi',method);
if pref_Verbose, fprintf(1,'%s', '.r'); end
if size(raw,3) > 1
img_int(:,:,2) = interp2(1:x, 1:y, raw(:,:,2),xi,yi',method);
if pref_Verbose, fprintf(1,'%s', '.g'); end
img_int(:,:,3) = interp2(1:x, 1:y, raw(:,:,3),xi,yi',method);
if pref_Verbose, fprintf(1,'%s', '.b'); end
end;
img_int = uint8(img_int);
else
img_int = uint8(raw);
end
clear raw
end