function  SauvePnm(nom,image)
%  SauvePnm(nom,image) : Sauvegarde d'une image au format pnm
%    exemple : SauvePgm('resultat.pnm',image);
%
image=uint8(image);
[nbli nbco k]=size(image);

s=size(nom);
s=s(2);
while nom(s)==' '
    s=s-1;
end

fid=fopen(nom(1:s),'w');

fprintf(fid,'P6\n');
fprintf(fid,'# Created by SauvePnm (J.M. Odobez)\n');
fprintf(fid,'%d %d\n%d\n',nbco,nbli,255);  


%%% Recuperation des donnees dans une matrice
image=uint8(image);
%image=image';

if 0
for li=1:nbli
for co=1:nbco
for b=1:k
ww=fwrite(fid,image(li,co,b),'uchar');
end
end
end

else % much faster !!!
    ImC=zeros(k*nbli*nbco,1);
    ind=0:k:k*nbli*nbco-1;
    imageT=zeros(nbco,nbli);
    for b=1:k
       imageT=image(:,:,b)';
       ind=ind+1;
       ImC(ind)=imageT;   
    end
    ImC=(uint8(ImC));
    ww=fwrite(fid,ImC,'uchar');
end

fclose(fid);
