% tv.m

function tv(r,g,b);
%fd = fopen("deleteme_deleteme.pnm","w");
fname = sprintf("deleteme_%12d.pnm",rand*(10^12));
[Mr Nr]=size(r);
if nargin==1
  fprintf(fd,"P5\n");
  fprintf(fd,"%d  %d\n",Nr,Mr);
  fprintf(fd,"255\n");
  %maxr=max(max(r));
  %minr=min(min(r));
  %r=round(255.0*(r-minr)/(maxr-minr));
  r=round(max(min(r,255),0));
  fwrite(fd,r,"uchar");
else%if
  [Mg Ng]=size(g);
  [Mb Nb]=size(b);
  if (Mr==Mg) & (Mr==Mb) & (Nr==Ng) & (Nr==Nb)
    M=Mr; N=Nr;  % all the same
    fprintf(fd,"P6\n")
    fprintf(fd,"%d  %d\n",N,M);
    fprintf(fd,"255\n")
    %maxr=max(max(r)); minr=min(min(r)); r=round(255.0*(r-minr)/(maxr-minr));
    %maxg=max(max(g)); ming=min(min(g)); g=round(255.0*(g-ming)/(maxg-ming));
    %maxb=max(max(b)); minb=min(min(b)); b=round(255.0*(b-minb)/(maxb-minb));
    r=round(max(min(r,255),0));
    g=round(max(min(g,255),0));
    b=round(max(min(b,255),0));
    r = reshape(r,1,M*N);
    g = reshape(g,1,M*N);
    b = reshape(b,1,M*N);
    fwrite(fd,[r;g;b],"uchar");
  else%if
    error("tvs.m: images must be same size")
  end%if
end%if
fclose(fd);
system("xv deleteme_deleteme.pnm &");

%keyboard

