Code covered by the BSD License  

Highlights from
HTML Indexer

image thumbnail

HTML Indexer

by

 

16 Aug 2004 (Updated )

automated multi-language html index, index for print, batch image scaling

prjRenderSheet(FileList, Options, Dir, ThGyOrig)
function [sheet, placing_info] = prjRenderSheet(FileList, Options, Dir, ThGyOrig)
% [sheet, placing_info] = prjRenderSheet(FileList, Options, Dir, ThGyOrig)
%
% create sheet() by tiling from list
% v.05  - as new, 09-31.07.2004, ok
% v.0.5.2   - fix thumb/gallery/original 3-lemma : use thumbnails

if nargin < 4, ThGyOrig = 'th'; end %
if nargin < 2, Options = uConstr('Options'); end;

if ~strcmp(Options.prjtype, 'contact')
    disp('prjRenderSheet: warning: not contact-type project'); 
end;

prefTermination = '.jpg'; % only when original FileList(i).type; never used...
 
% 1st inspect, this allows you to properly allocate, 
% and also to make bckgnd any colour you like
placing_info = uConstr('placing_info');
placing_info.x0_next = 1; 
placing_info.y0_next = 1;
placing_info.current_sheet = 1;

for i = 1:size(FileList,2)
    % fn_src = [ Dir.Dest FileList(i).base '.jpg'];
    fn_src = fullfile(FileList(i).path, FileList(i).name);
    % here look (or not for _th.jpg... higher level selection later)
    fid_src = fopen(fn_src); 
    placing_info.found(i) = 0;
    if fid_src <= 0
        disp(['image ' num2str(i)  ' not found, skipping...']);
        placing_info.x0(i) = 0; placing_info.y0(i) = 0;
        placing_info.on_sheet(i) = 0;
    else
        fclose(fid_src);
        fprintf(1, '.');
        placing_info.found(i) = 1;
        placing_info.on_sheet(i) = placing_info.current_sheet;
        placing_info.new_sheet(i) = 0;
%        info = imfinfo(fn_src,  FileList(i).extension);
%        FileList(i).th_width = info(1).Width; % should be done at top later
%        FileList(i).th_height = info(1).Height; % strange 2/poz array output for tiff
        % tile :
        placing_info = imgTile(placing_info, FileList(i), Options, ThGyOrig);
    end
end

if ~sum(placing_info.found)
    disp('prjRenderSheet: warning, no images found, contact sheet will be empty')
end

% --- now do the actual tiling of pixels ---

% - background colour - black !!!
nColSheet = 3; % later check if all thumbnails _are_ BW :-)
sheet = zeros(Options.contact.sheet_height, Options.contact.sheet_width, nColSheet, 'uint8');

for i = 1:size(FileList,2)
    if placing_info.found(i)
        if placing_info.new_sheet(i)
            %finished_sheet(placing_info.on_sheet(i)-1,:,:,:) = sheet;
            % write it to file then
            SheetNoStr = num2str(placing_info.on_sheet(i)-1);
            fprintf(1, '%s\n',  ['writing sheet ' SheetNoStr '...']);
            fName = fullfile(Dir.Dest, [Options.contact.file_prefix '_'  SheetNoStr '.jpg']);
            imwrite(sheet, fName, 'jpg','Quality', Options.GyQual);
            
            % reinit new sheet :-)
            sheet = zeros(Options.contact.sheet_height, Options.contact.sheet_width, nColSheet, 'uint8');
        end
        
        % fn_src = [ Dir.Dest FileList(i).base '.jpg'];
        fn_src = fullfile(Dir.Dest, [FileList(i).base '_' ThGyOrig prefTermination]);
        % get stored data !!! (store it when rendering)
        img_data = imread(fn_src); % check BW
        
        [dy, dx, nCol] = size(img_data);
        
        if placing_info.rotate(i) % was : FileList(i).th_height > FileList(i).th_width
            % clockwise if portrait
            
            img_buff = uint8(zeros(dx,dy,nCol));
            for ixC = 1:nCol
                img_buff(:,:,ixC) = rot90(img_data(:,:,ixC));
            end
            img_data = img_buff; clear img_buff
            [dy, dx, nCol] = size(img_data);
        end
        
        
        if nCol==3
            for icol=1:3
                sheet((placing_info.y0(i):placing_info.y0(i)+dy-1),...
                    (placing_info.x0(i):placing_info.x0(i)+dx-1),icol) = img_data(:,:,icol);
            end
        else % repeat all layers
            for icol=1:3
                sheet((placing_info.y0(i):placing_info.y0(i)+dy-1),...
                    (placing_info.x0(i):placing_info.x0(i)+dx-1),icol) = img_data(:,:);
            end           
        end
        %sheet((placing_info.y0(i):placing_info.y0(i)+dy-1),(placing_info.x0(i):placing_info.x0(i)+dx-1),2) = img_data;
        %sheet((placing_info.y0(i):placing_info.y0(i)+dy-1),(placing_info.x0(i):placing_info.x0(i)+dx-1),3) = img_data;
    end;
end;

% write the last one too
SheetNoStr = num2str(placing_info.on_sheet(i));
fprintf(1, '%s\n',  ['writing sheet ' SheetNoStr '...']);
fName = fullfile(Dir.Dest, [Options.contact.file_prefix '_'  SheetNoStr '.jpg']);
imwrite(sheet, fName, 'jpg','Quality', Options.GyQual);

end

function placing_info = imgTile(placing_info, FileEntry, Options, ThGyOrig)
% tiles frame into sheet at x0, y0 
% v.0.5 - new, 21.05.2004
%       - calc.only info, 11.07.2004
% v.0.5.2   - 14.02.2009, fix th/gy/orig 3-lemma
%           - copied in here from own file
%
% x0, y0 se calculeaza in renderSheet... sau aci ?

if nargin < 4, ThGyOrig = 'th'; end %
if nargin < 3, Options = uConstr('Options'); disp('warning: def.Options'); end;
if nargin < 2, disp('imgTile: error: no file passed'); end;

% current position :
%ix = placing_info.last_pic+1;
ix = size(placing_info.found,2);

switch lower(ThGyOrig)
    case 'th'   % thumbnail
        dx = FileEntry.th_width;  dy = FileEntry.th_height;
    case 'gy'   % gallery
        dx = FileEntry.gy_width;  dy = FileEntry.gy_height;
    case 'or'   % original
        dx = FileEntry.or_width;  dy = FileEntry.or_height;
end

placing_info.rotate(ix) = 0; % turn clockwise if portrait
if dx < dy % swapp'em
    buff = dy; dy = dx; dx = buff;
    placing_info.rotate(ix) = 1; 
end

% short-writing
x0_next = placing_info.x0_next;  y0_next = placing_info.y0_next;

th_placed = 0;
% ---
while ~th_placed  
    % test if you still can tile h (=row)
    candidate_xm = x0_next + dx;
    if candidate_xm <= Options.contact.sheet_width % try horizontally
        % test vertically, if enough y space
        candidate_ym = y0_next + dy - 1;
        if candidate_ym <= Options.contact.sheet_height % place then
            x0 = x0_next; y0 = y0_next; % most likely
            x0_next = x0 + dx + Options.contact.margine(1);
            y0_next = y0;
            % update upper limit
            placing_info.curr_row_ymax = max(placing_info.curr_row_ymax, candidate_ym);
            % assert
            th_placed = 1;
        else 
            % not enough y space, go to a new sheet... later better
            fprintf(1,'next sheet, insufficient height');
            placing_info.new_sheet(ix) = 1;
            placing_info.current_sheet = placing_info.current_sheet + 1;
            placing_info.on_sheet(ix) = placing_info.current_sheet;
            x0 = 1; y0 = 1; placing_info.curr_row_ymax = 0;
            x0_next = dx + Options.contact.margine(1);
            y0_next = y0;
            % assert
            th_placed = 1;
        end;
    else  % try vertical
        x0 = 1; y0 = placing_info.curr_row_ymax + Options.contact.margine(2);
        candidate_ym = y0 + dy - 1;
        if candidate_ym <= Options.contact.sheet_height
            disp('new row');
            candidate_xm = dx;
            if candidate_xm <= Options.contact.sheet_width % proceed h
                placing_info.prev_row_ymax = placing_info.curr_row_ymax + Options.contact.margine(2);
                placing_info.curr_row_ymax = placing_info.prev_row_ymax + dy;
                % store x0y0; then
                th_placed = 1;
                x0_next = dx + Options.contact.margine(1);
                y0_next = y0;
            end;
        else
            disp('new sheet, row finished');
            placing_info.new_sheet(ix) = 1;
            placing_info.current_sheet = placing_info.current_sheet + 1;
            placing_info.on_sheet(ix) = placing_info.current_sheet;
            x0 = 1; y0 = 1; placing_info.curr_row_ymax = 0;
            x0_next = dx + Options.contact.margine(1);  y0_next = y0;
            % assert
            th_placed = 1;
        end;
    end;
end;


% % store info
placing_info.x0(ix) = x0; placing_info.y0(ix) = y0;
placing_info.x0_next = x0_next; placing_info.y0_next = y0_next;
placing_info.th_placed(ix) = th_placed;

% movepointer
placing_info.last_pic = ix;

placing_info.dx(ix) = dx; placing_info.dy(ix) = dy; % debug
end

Contact us