%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Saves an RGB image in PPM format
%
%Written by:
%   Alexander Stoytchev 
%   College of Computing 
%   Georgia Tech
%   saho@cc.gatech.edu
%   http://www.cc.gatech.edu/~saho
%
% Returns:
%   The number of bytes written.
%   On failure the function returns -1.
%
% Last modified: Nov 28, 1998
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function n=imwrite_ppm(IMAGE_RGB, fname)

fid=fopen(fname,'w');
if fid== -1
  n=-1;
else
  n=0;
  [R,C,Z]=size(IMAGE_RGB);
  fprintf(fid,'P6\n#\n%d %d\n255\n',C,R);  %Dump the header
  for i=1:R
    w=fwrite(fid, double([IMAGE_RGB(i,:,1);
                          IMAGE_RGB(i,:,2); 
                          IMAGE_RGB(i,:,3)] ),'char*1');
    n=n+w;
  end;
  fclose(fid)
end;
