function ctxanim(fname, data, varargin) % function ctxanim(fname, data, mincolor, maxcolor, mask) % % Create a Cortex animation or bitmap out of a data matrix. % Writes the Cortex file to the string in fname. % % The data matrix is rescaled so that the global minimum and maximum % map to the range defined by mincolor and maxcolor. mincolor defaults to % 128 and maxcolor defaults to 255. Cortex typically uses the lower portion % of the colormap for default colors, so it is recommended that you specify % a higher value than zero. % % The optional mask argument can be passed as a multiplicative factor to % apply to each individual re-scaled frame. One reason you may want to use % this feature is to create a region of the bitmap which will correspond % to whatever the background color is. By setting the mask to zero % in the areas that correspond to background and one in the areas that % will be foreground, you can achieve this effect. The default mask is % all ones. % % Note that this function requires Matlab 5 to work properly, as it % uses multi-dimensional arrays and Endian options for file writing, % both not available in Matlab 4. % % Version 1.0 % For additional code, updated versions, questions or comments, please refer % to the Active Perception Lab home page % http://www.cnbc.cmu.edu/~tai/lab.html % Copyright 1998 % Rick Romero % rickr+@cmu.edu % Permission to use and modify is granted, % but not to redistribute original or modified code. xsize = size(data,2); ysize = size(data,1); frames = size(data,3); if (length(varargin) < 3) mask = ones(xsize, ysize); else mask = varargin{3}; end if (length(varargin) < 2) maxcolor = 255; else maxcolor = varargin{2}; end if (length(varargin) < 1) mincolor = 128; else mincolor = varargin{1}; end fid = fopen(fname, 'w', 'ieee-le'); header = [0 0 0 0 0 0 xsize ysize (frames-1)]; if (frames > 1) minx = min(min(min(data,[],1),[],2),[],3); maxx = max(max(max(data,[],1),[],2),[],3); else minx = min(min(data,[],1),[],2); maxx = max(max(data,[],1),[],2); end if (maxx == minx) maxx = minx + 1; end for i = 1:frames data2 = mask .* round(((data(:,:,i) - minx) ./ (maxx - minx)) .* (maxcolor - mincolor) + mincolor)'; fwrite(fid, header, 'int16'); fwrite(fid, data2, 'uchar'); end fclose(fid);