file/ 0040755 0000764 0001040 00000000000 07205206420 013055 5 ustar jepson Administrators file/num2strPad.m 0100644 0000764 0001040 00000000245 07205527022 015274 0 ustar jepson Administrators function s=num2strPad(n, pad) % % s=num2strPad(n, pad) % if ~exist('pad') pad =1; end s = num2str(n); while (length(s) < pad) s = ['0' s]; end file/endianness.m 0100644 0000764 0001040 00000000756 07355075334 015405 0 ustar jepson Administrators function resp = endianess(); % return endianess of machine % %% Check output format for stdout [n p format] = fopen(1); resp = 'UNKNOWN'; if (strcmp(format, 'ieee-le')) resp = 'LITTLE'; elseif (strcmp(format, 'ieee-be')) resp = 'BIG'; elseif (strcmp(format, 'native')) %% Check for a PCWIN computer type, or an Linux x86 indicator sys = computer; if (findstr('PCWIN', sys)>0) | (findstr('LNX86', sys) > 0) resp = 'LITTLE'; else resp = 'BIG'; end end file/pgmRead.m 0100644 0000764 0001040 00000002617 07355103546 014630 0 ustar jepson Administrators % IM = pgmread( FILENAME ) % % Load a pgm image into a MatLab matrix. % This format is accessible from the XV image browsing utility. % Only works for 8bit gray images (raw or ascii) % Hany Farid, Spring '96. Modified by Eero Simoncelli, 6/96. % Modified by Allan Jepson, 9/99. function im = pgmread( fname ); global SIZE_LIMIT; if SIZE_LIMIT fprintf(1, 'SIZE_LIMIT recognized by pgmread\n'); end [fid,msg] = fopen( fname, 'r' ); if (fid == -1) error(msg); end [pars type]= pnmReadHeader(fid); if (pars==-1) fclose(fid); error([fname ': cannot parse pgm header']); end if ~(type == 'P2' | type == 'P5') fclose(fid); error([fname ': Not of type P2 or P5.']); end xdim = pars(1); ydim = pars(2); %%% Maximum pixel value maxval = pars(3); fprintf(1, 'original image size: cols %d rows %d\n', xdim, ydim) sz = xdim * ydim; if SIZE_LIMIT if sz >= 16384 ydim = floor(16384/xdim); sz = xdim * ydim; fprintf(1, 'truncated image size: cols %d rows %d\n', xdim, ydim) end end if (type == 'P2') [im,count] = fscanf(fid,'%d',sz); elseif (type == 'P5') [im,count] = fread(fid,sz,'uchar'); else %% This should not occur, we have already checked the type. fclose(fid); error([fname ': Not of type P2 or P5.']); end fclose(fid); if (count == sz) im = reshape( im, xdim, ydim )'; else fprintf(1,'Warning: File ended early!'); im = reshape( [im ; zeros(sz-count,1)], xdim, ydim)'; end file/pfmRead.m 0100644 0000764 0001040 00000003145 07355107500 014616 0 ustar jepson Administrators function im = pfmRead( fname ); % im = pfmRead( fname ) % % Load a pfm image into a MatLab matrix. % Compatability with Jepson pfm output format % [fid,msg] = fopen( fname, 'r' ); if (fid == -1) error(msg); end [pars type]= pnmReadHeader(fid); if (pars==-1) fclose(fid); error([fname ': cannot parse pnm header']); end fileEndianness = 'UNKNOWN'; if (type == 'PL' | type == 'FP') fileEndianness = 'LITTLE'; elseif (type == 'PB' | type == 'FU') fileEndianness = 'BIG'; else fclose(fid); error('PFM file must be of type PL/PB'); end xdim = pars(1); ydim = pars(2); %%% Maximum pixel value maxval = pars(3); fprintf(1, 'original image size: cols %d rows %d\n', xdim, ydim) sz = xdim * ydim; if strcmp(endianness, fileEndianness) %% Reading file in same endianness format [im,count] = fread(fid,xdim*ydim,'float32'); else %% Reading file in opposite endianness format %% Get current file position in bytes filePos = ftell(fid); if (filePos < 0) fclose(fid); error(ferror(fid)); end %% Close file and reopen in the correct endianness format. fclose(fid); if strcmp(fileEndiannes, 'LITTLE') format = 'ieee-le'; else % assume big endian format = 'ieee-be'; end [fid,msg] = fopen( fname, 'r', format ); if (fid < 0) error(msg); end %% Rewind to beginning of data if fseek(fid, filePos, 'bof') < 0 fclose(fid); error(ferror(fid)); end %% Read the data [im,count]=fread(fid,xdim*ydim,'float32'); end fclose(fid); if (count == sz) im = reshape( im, xdim, ydim )'; else fprintf(1,'Warning: File ended early!'); im = reshape( [im ; zeros(sz-count,1)], xdim, ydim)'; end file/ppmRead.m 0100644 0000764 0001040 00000002165 07355745316 014646 0 ustar jepson Administrators function im = ppmRead(fname) % function im = ppmRead(filename) % Read an image from the named file in unsigned chars with header. % The image format used is ppm for color images. If the file name % is pix.ppm, type: % im = ppmRead('pix.ppm') % Assumes a big endian byte order. % [fid,msg] = fopen( fname, 'r' ); if (fid == -1) error(msg); end [pars type]= pnmReadHeader(fid); if (pars==-1) fclose(fid); error([fname ': cannot parse pgm header']); end if ~(type == 'P6') fclose(fid); error([fname ': Not of type P6.']); end xdim = pars(1); ydim = pars(2); %%% Maximum pixel value maxval = pars(3); fprintf(1, 'original image size: cols %d rows %d\n', xdim, ydim) sz = xdim * ydim * 3; [imShuffle,count] = fread(fid,sz,'uint8'); fclose(fid); if (count == sz) imShuffle = reshape( imShuffle, 3, xdim, ydim ); else fprintf(1,'Warning: File ended early!'); imShuffle = reshape( [imShuffle ; zeros(sz-count,1)], 3, xdim, ydim); end im = zeros(ydim, xdim, 3); im(:,:,1) = reshape(imShuffle(1, :, :), xdim, ydim)'; im(:,:,2) = reshape(imShuffle(2, :, :), xdim, ydim)'; im(:,:,3) = reshape(imShuffle(3, :, :), xdim, ydim)'; file/resizeImageFig.m 0100644 0000764 0001040 00000000773 07355752666 016161 0 ustar jepson Administrators function resizeImageFig(h, sz, frac) % function resizeImageFig(h, sz, frac) % Resize figure with handle h to have pixels of size frac. % sz = image size. % frac (default = 1, provides truesize when sz = size of displayed image). if (nargin <3) frac = 1; end pos = get(h, 'Position'); set(h, 'Units', 'pixels', 'Position', ... [pos(1), pos(2)+pos(4)-frac*sz(1), ... %% Keep top left corner fixed frac*sz(2), frac*sz(1)]); set(gca,'Position', [0 0 1 1], 'Visible', 'off'); file/pnmReadHeader.m 0100644 0000764 0001040 00000004762 10034267346 015750 0 ustar jepson Administrators function [pars, type]= pnmReadHeader(fid) % [pars, type]= pnmReadHeader(fid) % Read and parse a pgm style image file header % For a successful parse, return: % pars = [xdim ydim maxval] % xdim, ydim = number of columns, number of rows % maxval = maximum pixel value % and type: % "P1" = ascii bitmap, "P2" = ascii greymap, % "P3" = ascii pixmap, "P4" = raw bitmap, % "P5" = raw greymap, "P6" = raw colour pixmap % "PL", "FP" = float file, ieee-le format % "PB", "FU" = float file, ieee-be format % For an unsuccessful parse, returns % pars=-1 % Written by A. Jepson, 9/99 pars = -1; type = 'Unknown'; %%% First line contains type string: TheLine = fgetl(fid); szLine = size(TheLine); endLine = szLine(2); if (endLine < 2) fprintf(1, ['Unrecognized PNM file type\n']); pars = -1; return; end type = TheLine(1:2); ok = 0; if (type(1) == 'P') if (type(2) == '1' | type(2) == '2' | ... type(2) == '3' | type(2) == '4' | ... type(2) == '5' | type(2) == '6' | ... type(2) == 'B' | type(2) == 'L') ok = 1; else fprintf(1, ['Unrecognized PNM file type: ' type '\n']); end end if (type(1) == 'F') if (type(2) == 'P' | type(2) == 'U') ok = 1; else fprintf(1, ['Unrecognized PNM file type: ' type '\n']); end end if ~ok pars = -1; return; end %% First two characters of first line have been understood. current = 3; % index of current character in TheLine parIndex=1; % index for next parameter to be read while(parIndex < 4) while (current > endLine) TheLine = fgetl(fid); if (TheLine == -1) fprintf(1, 'Unexpected EOF\n'); pars = -1; return; end szLine = size(TheLine); endLine = szLine(2); current = 1; end [token, count, errmsg, nextindex] = ... sscanf(TheLine(current:endLine),'%s',1); nextindex = nextindex+current-1; if (count==0) if (nextindex > endLine) % Read to end of line current = nextindex; else % Problem pars = -1; fprintf(1, 'Unexpected EOF\n'); return; end else % Found a token if token(1) == '#' current = endLine+1; else [pars(parIndex), count, errmsg, nextindex] = ... sscanf(TheLine(current: endLine), '%d', 1); if ~(count==1) fprintf(1,'Confused reading pgm header\n'); pars=-1; return; end parIndex = parIndex+1; current = current+nextindex-1; end end end file/pgmWrite.m 0100644 0000764 0001040 00000007073 10034266514 015041 0 ustar jepson Administrators % 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'. % % COMMENT (optional) A string to be inserted as a comment in the header. % Defaults to ''. % 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 if (exist('comment') ~= 1) comment = ''; 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
0 EOL = 10; % End of line if comment(end) ~= EOL comment = [comment EOL]; % Add an end of line character to the comment. end lineEnds = findstr(EOL, comment); for j=1:length(lineEnds) if j == 1 line = ['# ' comment(1:lineEnds(j))]; else line = ['# ' comment((lineEnds(j-1)+1):lineEnds(j))]; end fprintf(fid, '%s', line); fprintf(2,'%s', line); end end %%% dimensions fprintf(fid,'%d %d\n',size(mtx,2),size(mtx,1)); %%% Maximum pixel value fprintf(fid,'255\n'); %% MatLab's "fprintf" floors when writing floats, so we compute %% (mtx-r1)*255/(r2-r1)+0.5 mult = (255 / (range(2)-range(1))); mtx = (mult * mtx) + (0.5 - mult * range(1)); mtx = max(-0.5+eps,min(255.5-eps,mtx)); if (format == 2) count = fprintf(fid,'%d ',mtx'); elseif (format == 5) count = fwrite(fid,mtx','uchar'); end fclose(fid); if (count ~= size(mtx,1)*size(mtx,2)) fprintf(1,'Warning: File output terminated early!'); end return; %%% TEST: % foo = 257*rand(100)-1; % pgmWrite(foo,'foo.pgm',[0 255]); % foo2=pgmRead('foo.pgm'); % size(find((foo2-round(foo))~=0)) % foo(find((foo2-round(foo))~=0)) file/ppmWrite.m 0100644 0000764 0001040 00000003136 10034265340 015042 0 ustar jepson Administrators function ppmWrite(filename, im, comment) % function ppmWrite(filename, im, comment) % Write out an image to the named file in unsigned chars with header. % The image format used is ppm for color images. If the file name is % pix.ppm, and the color planes are in im(:,1:3), type: % writeppm('pix.ppm',im) % comment is an optional comment string to be written in the ppm header. % It can contain newline characters. Default ''. if ~exist('comment') comment = ''; end fid = fopen(filename,'w'); [rows, cols, colours] = size(im); if ~(colours == 3) error('Not an RGB image.'); end imShuffle = uint8(zeros(3, cols, rows)); if ((min(im(:)) >= 0.0) & (max(im) <= 1.0)) imShuffle(1, :, :) = round(reshape(im(:, :, 1), rows, cols)' * 255); imShuffle(2, :, :) = round(reshape(im(:, :, 2), rows, cols)' * 255); imShuffle(3, :, :) = round(reshape(im(:, :, 3), rows, cols)' * 255); else imShuffle(1, :, :) = round(reshape(im(:, :, 1), rows, cols)' ); imShuffle(2, :, :) = round(reshape(im(:, :, 2), rows, cols)' ); imShuffle(3, :, :) = round(reshape(im(:, :, 3), rows, cols)' ); end fprintf(fid,'P6\n'); %% Output comments if length(comment)>0 EOL = 10; % End of line if comment(end) ~= EOL comment = [comment EOL]; % Add an end of line character to the comment. end lineEnds = findstr(EOL, comment); for j=1:length(lineEnds) if j == 1 line = ['# ' comment(1:lineEnds(j))]; else line = ['# ' comment((lineEnds(j-1)+1):lineEnds(j))]; end fprintf(fid, line); end end %% Output dimension fprintf(fid,'%d %d\n255\b', cols, rows); fwrite(fid,imShuffle,'uint8'); fclose(fid); file/pfmWrite.m 0100644 0000764 0001040 00000002563 10034264556 015043 0 ustar jepson Administrators function [] = pfmWrite(mtx, fname, comment); % pfmWrite(mtx, filename) % % Write a MatLab matrix to a pfm (graylevel image) file. % comments (optional) a string to be included in the pnm style header. if ~exist('comment') comment = ''; end [fid,msg] = fopen( fname, 'w' ); if (fid == -1) error(msg); end %%% First line contains ID string: %%% "PB" = big endian float file %%% "PL" = big endian float file endian = endianness; if strcmp(endian,'LITTLE') fprintf(fid,'PL\n'); elseif strcmp(endian,'BIG') fprintf(fid,'PB\n'); else error(strcat('Unknown endianness: ', endianness)); end fprintf(fid,'# MatLab PFMWRITE file, saved %s\n',date); %% Output comments if length(comment)>0 EOL = 10; % End of line if comment(end) ~= EOL comment = [comment EOL]; % Add an end of line character to the comment. end lineEnds = findstr(EOL, comment); for j=1:length(lineEnds) if j == 1 line = ['# ' comment(1:lineEnds(j))]; else line = ['# ' comment((lineEnds(j-1)+1):lineEnds(j))]; end fprintf(fid, line); end end %%% dimensions fprintf(fid,'%d %d\n',size(mtx,2),size(mtx,1)); %%% Maximum pixel value (meaningless in pfm images, but conforms to pnm %%% style headers) fprintf(fid,' 0\n'); count = fwrite(fid, mtx','float'); fclose(fid); if (count ~= size(mtx,1)*size(mtx,2)) fprintf(1,'Warning: File output terminated early!'); end