[Index for tmp_for_tar/imgio] [Return to Master Index]

readpnm

(tmp_for_tar/imgio/readpnm.m)


Function Synopsis

[im, rgb,maxval] = readpnm_patched(fname)

Help text

 [im, rgb,maxval] = readpnm (fname) - Read a pnm, ppm or pgm image.

 fname  : string : Name of PNM file.
 im     : the image as an Octave matrix.
 rgb    : 1 if im is in PPM format.
          0 if im is a graylevel matrix.
 maxval : maximum value of levels

       [im, isPpm] = readpnm(fname)

 Created:       08/05/1998
 Last Modified: 13/09/2002 Etienne Grossmann <etienne@isr.ist.utl.pt>
 Version: 1.0



Listing of function file tmp_for_tar/imgio/readpnm.m

## [im, rgb,maxval] = readpnm (fname) - Read a pnm, ppm or pgm image.
##
## fname  : string : Name of PNM file.
## im     : the image as an Octave matrix.
## rgb    : 1 if im is in PPM format.
##          0 if im is a graylevel matrix.
## maxval : maximum value of levels

##       [im, isPpm] = readpnm(fname)
##
## Copyright (C) 1998 Ariel Tankus
## 
## This program is free software.
## This file is part of the Image Processing Toolbox for Octave
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##


## Author: Ariel Tankus <arielt@math.tau.ac.il>
## Created:       08/05/1998
## Last Modified: 13/09/2002 Etienne Grossmann <etienne@isr.ist.utl.pt>
## Version: 1.0

function [im, rgb,maxval] = readpnm_patched(fname)

## constants:
ascPgmMagic = "P2";
binPgmMagic = "P5";
ascPpmMagic = "P3";
binPpmMagic = "P6";

[fid, magicnum] = getmagicnum(fname);

[width,height,maxval] = readnums (fid,3)

if magicnum(1) == "P",

  if (magicnum(2) == ascPpmMagic(2)) || (magicnum(2) == binPpmMagic(2)),
    ## reserve room for color info
    rgb = 1; 
  else
    rgb = 0;
  end

  if (magicnum(2) == ascPgmMagic(2)) ||  (magicnum(2) == ascPpmMagic(2)),

    [data, count] = fscanf(fid, "%d", [width*(1+2*rgb), height]);

  elseif (magicnum(2) == binPgmMagic(2)) || (magicnum(2) == binPpmMagic(2)),

    [data, count] = fread(fid, [width*(1+2*rgb), height], "uchar");
  end
else
  
  error ("Magic number '%s' unknown!", magicnum);

end

if (count < width*(1+2*rgb)*height)
  printf ("Could only read %d out of %d bytes. File might be truncated.\n",\
	  count, width*(1+2*rgb)*height);
  keyboard
end
				# convert image [x,y] to matrix [m,n]
if rgb				# Yet another ugly hack
  im = zeros (3*height,width);
  im(1:3:3*height,:) = data(1:3:3*width,:)';
  im(2:3:3*height,:) = data(2:3:3*width,:)';
  im(3:3:3*height,:) = data(3:3:3*width,:)';
else
  im = data';
end

fclose(fid);

endfunction

Produced by oct2html on Sat Sep 14 9:47:03 2002
Cross-Directory links are: ON