[Index for tmp_for_tar/vrml.doc] [Return to Master Index]

writeppm

(tmp_for_tar/vrml.doc/writeppm.m)


Function Synopsis

writeppm(fname, im, scale)

Help text

       writeppm(fname, im)

 writeppm  Write an image into a file in PPM format.

       writeppm(fname, im[, scale=1])
       fname - File name.
       im    - Image to write into file. Columns are alternatively r, g,
               b, etc.

       writeppm(fid, im[, scale=1])
       fid   - File ID. File should be opened before calling writeppm, and
               be closed afterwards. 
       im    - image to write into file. Columns are alternatively r, g,
               b, etc.
       scale - 0 - Don't scale values to [0,255]
               1 - Scale values to [0,255]

       See also: writepgm, writepnm, readpnm.

 Modified: June   2000 by Etienne Grossmann <etienne@isr.ist.utl.pt>
           to accept a 'scale' option.
 Created: 11.5.98.
 Version: 1.0



Listing of function file tmp_for_tar/vrml.doc/writeppm.m

##       writeppm(fname, im)
##
## 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.
##
##
## writeppm  Write an image into a file in PPM format.
##
##       writeppm(fname, im[, scale=1])
##       fname - File name.
##       im    - Image to write into file. Columns are alternatively r, g,
##               b, etc.
##
##       writeppm(fid, im[, scale=1])
##       fid   - File ID. File should be opened before calling writeppm, and
##               be closed afterwards. 
##       im    - image to write into file. Columns are alternatively r, g,
##               b, etc.
##       scale - 0 - Don't scale values to [0,255]
##               1 - Scale values to [0,255]
##
##       See also: writepgm, writepnm, readpnm.
##

## Author: Ariel Tankus <arielt@math.tau.ac.il>
## Modified: June   2000 by Etienne Grossmann <etienne@isr.ist.utl.pt>
##           to accept a 'scale' option.
## Created: 11.5.98.
## Version: 1.0

function writeppm(fname, im, scale)

  if nargin<3, scale = 1 ; end

  inputAsName = isstr(fname);    # is input given as filename or file ID?
  if (inputAsName)
    fid = fopen(fname, 'w');
    if (fid == -1)
      error(['Unable to open file: ', fname]);
      return;
    end
  else
    fid = fname;
  end

  maxScaledIm = 255;

  ## scaledIm = scl(im, 0, 255);
  if scale,
    minIm = min(min(im));
    maxIm = max(max(im));
    scaledIm = (im - minIm) ./ (maxIm - minIm) .* maxScaledIm;
  else
    scaledIm = im ;
  end

  fprintf(fid, 'P6\n%d %d\n%d\n', size(scaledIm,2)/3, size(scaledIm,1), ...
          maxScaledIm);

  count = fwrite(fid, scaledIm', 'uchar');
  if (count ~= prod(size(scaledIm)))
    error('Write failure. Unable to write all elements.');
  end

  if (inputAsName)
    fclose(fid);
  end

endfunction

Produced by oct2html on Sat Dec 2 19:08:39 2000
Cross-Directory links are: ON