Home > RPSstuff > writeppm.m

writeppm

PURPOSE ^

WRITEPPM Writes an Image and a Colormap as a PPM file

SYNOPSIS ^

function writeppm(im,map,file);

DESCRIPTION ^

 WRITEPPM  Writes an Image and a Colormap as a PPM file

 Usage: writeppm(im,map,file);

 Inputs: im = image
         map = image colormap
         file = output PPM file

 Description:  Writes image in PPM format, a widely-used, simple, 
      portable, but non-compressed format.  PPM images can be converted
      to gif, jpg, tif, bmp, pict, and nearly every other image format
      know to man (or nerd).   Look for the 'netpbm' distribution on 
      the internet.


 Rich Signell
 U.S. Geological Survey
 rsignell@usgs.gov

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function writeppm(im,map,file);
0002 % WRITEPPM  Writes an Image and a Colormap as a PPM file
0003 %
0004 % Usage: writeppm(im,map,file);
0005 %
0006 % Inputs: im = image
0007 %         map = image colormap
0008 %         file = output PPM file
0009 %
0010 % Description:  Writes image in PPM format, a widely-used, simple,
0011 %      portable, but non-compressed format.  PPM images can be converted
0012 %      to gif, jpg, tif, bmp, pict, and nearly every other image format
0013 %      know to man (or nerd).   Look for the 'netpbm' distribution on
0014 %      the internet.
0015 %
0016 %
0017 % Rich Signell
0018 % U.S. Geological Survey
0019 % rsignell@usgs.gov
0020 
0021 [height,width]=size(im);
0022 [ncols,nrgb]=size(map);
0023 
0024 bad=find(im>ncols);
0025 if(~isempty(bad)),
0026   im(bad)=ncols*ones(size(im(bad)));
0027 end
0028 map=map*255;
0029 maxc=max(map(:));
0030 
0031 fid=fopen(file,'w');
0032 fprintf(fid,'P6\n');
0033 fprintf(fid,'#Generated by Matlab routine WRITEPPM\n');
0034 fprintf(fid,'%4d %4d\n',[width height]);
0035 fprintf(fid,'%4d\n',maxc);
0036 for i=1:height;
0037   row=im(i,:);
0038   rgb=[map(row,1) map(row,2) map(row,3)]';
0039   fwrite(fid,rgb,'uchar');
0040 end
0041 fclose(fid);

Generated on Wed 30-Nov-2005 14:45:02 by m2html © 2003