% --------------------------------------------------------------------------- % Function grload() % -- Xenios Papademetris papad@noodle.med.yale.edu 5th Sep 1995 % % -- used to load images stored in the simple format (.grl) used for the % -- EE445a course, this has an unsigned short magic number (29571) % -- and the width and height of the image in 4-byte integers % -- followed by the actual data in unsigned char format % -- check for zero image lhs 2/2000 % -- handles swapping lhs 2/2000 % -- really handles swapping 1/2001 % --------------------------------------------------------------------------- function outimage=grload(filename) % --------------------------------------------------------------------------- % Step 1 -- Check for right number of parameters % -- and check for valid filename % --------------------------------------------------------------------------- if nargin ~= 1 error('Requires one input argument eg grload(filename)'); elseif nargin == 1 & isstr(filename) if (isempty(findstr(filename,'.'))==1) filename=[filename,'.grl']; end end % --------------------------------------------------------------------------- % Step 2 -- Open filename % -- Check for valid file % --------------------------------------------------------------------------- handle=fopen(filename,'r'); if handle == -1 ff=['grload::cannot open file ' , filename]; error (ff); end magic=fread(handle,1,'ushort'); if (magic~=29571 & magic ~=33651) ff=['grload::file ' , filename,' is not a grl file']; error (ff); end % --------------------------------------------------------------------------- % Step 3 -- Read width and height % --------------------------------------------------------------------------- width1=fread(handle,1,'uchar'); width2=fread(handle,1,'uchar'); width3=fread(handle,1,'uchar'); width4=fread(handle,1,'uchar'); width = width4 + width3*256 + width2*256*256 + width1*256*256*256; if (width>4096) width = width1 + width2*256 + width3*256*256 + width4*256*256*256; end height1=fread(handle,1,'uchar'); height2=fread(handle,1,'uchar'); height3=fread(handle,1,'uchar'); height4=fread(handle,1,'uchar'); height = height4 + height3*256 + height2*256*256 + height1*256*256*256; if (height>4096) height = height1 + height3*256 + height3*256*256 + height4*256*256*256; end disp('image size:'); disp([width,height]); % --------------------------------------------------------------------------- % Step 4 -- Read actual Image % --------------------------------------------------------------------------- outimage=fread(handle,[height,width],'uchar'); imax = max(max(outimage)); if imax ~= 0 outimage=outimage'/imax; else disp('Zero image'); end % --------------------------------------------------------------------------- % Step 5 -- Close file % --------------------------------------------------------------------------- fclose(handle); % ---------------------------------------------------------------------------