% RANGE = pgmWrite(MTX, FILENAME, RANGE, TYPE, COMMENT) % % Write a MatLab matrix to a pgm (graylevel image) file. % This format is accessible from the XV image browsing utility. % % RANGE (optional) is a 2-vector specifying the values that map to % black and white, respectively. Passing a value of 'auto' (default) % sets RANGE=[min,max] (as in MatLab's imagesc). 'auto2' sets % RANGE=[mean-2*stdev, mean+2*stdev]. 'auto3' sets % RANGE=[p1-(p2-p1)/8, p2+(p2-p1)/8], where p1 is the 10th percentile % value of the sorted MATRIX samples, and p2 is the 90th percentile % value. % % TYPE (optional) should be 'raw' or 'ascii'. Defaults to 'raw'. % Hany Farid, Spring '96. Modified by Eero Simoncelli, 6/96. function range = pgmWrite(mtx, fname, range, type, comment ); [fid,msg] = fopen( fname, 'w' ); if (fid == -1) error(msg); end %------------------------------------------------------------ %% optional ARGS: if (exist('range') ~= 1) range = 'auto'; end if (exist('type') ~= 1) type = 'raw'; end %------------------------------------------------------------ %% Automatic range calculation: if (strcmp(range,'auto1') | strcmp(range,'auto')) [mn,mx] = range2(mtx); range = [mn,mx]; elseif strcmp(range,'auto2') stdev = sqrt(var2(mtx)); av = mean2(mtx); range = [av-2*stdev,av+2*stdev]; % MAGIC NUMBER: 2 stdevs elseif strcmp(range, 'auto3') percentile = 0.1; % MAGIC NUMBER: 0