function [STIM,imgNames] = loadDirsOffscreen(win,whichDirs,stimSize,contrastScale,maxNumImages);
%  [STIM,imgNames] = loadDirsOffscreen(win,whichDirs,[contrastScale,stimSize,maxNumImages]);
% a function that grabs all image files (currently of type .jpg) in 
% the specified directories, normalizes intensity, scales to a specified
% contrast, and loads into a series of offscreen windows.
%
% imgNames is a cell-of-strings with the name of each image loaded.
%
% ras 03/03
if ~exist('contrastScale','var')	contrastScale = 100;	end
if ~exist('stimSize','var')		stimSize = [];			end
if ~exist('maxNumImages','var')		maxNumImages = [];			end
rect = screen(win,'Rect');
X = rect(3); Y = rect(4);
gray = 127;
images = {};
imgNames = {};
done = 0;
fprintf('Loading images.');
for i = 1:length(whichDirs)
	nextdir = [machinename,whichDirs{i}];
	w = filterdir('.jpg',nextdir);
	numfiles = length(w);
	for j = 1:numfiles
		imgfile = [nextdir,w(j).name];
		images{end+1} = imread(imgfile,'jpg');
		imgNames{end+1} = imgfile;
		fprintf('.');
		if length(images) >= maxNumImages
			done = 1;
			break;
		end
	end
	if done	break;	end
end
fprintf('\n');
images = normalizeIntensity(images);
images = scaleContrast(images,contrastScale/100);
if isempty(stimSize)
	stimSize = length(images{1});
end
stimRect = [X-stimSize Y-stimSize X+stimSize Y+stimSize]/2;
for i = 1:length(images)
	STIM(i) = screen(win,'OpenOffScreenWindow',gray);
	screen(STIM(i),'PutImage',images{i},stimRect,'srcCopyQuickly');
end
return
