function printcmyk (fname, paperwidth, aspectratio);

% usage printcmyk (fname, paperwidth, aspectratio)
% aspectratio is height:width of a pixel

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% printcmyk.m                       %          
% written 3/5/00 by Ben Olding      %
% paperwidth is in inches           %
% fname is a .mat file with data    %
% stored in variable cmykimage      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

global PSText PSComment

% Requires: al.m, initps.m

% PS commands used --
% newpath     : initialize new graphics path
% setlinewidth: set width of line in points
% setcmykcolor: set color in cmyk coordinates
% stroke      : draw path
% rlineto     : add line in relative coordinates
% showpage    : release page to printer

% load image (previously stored as 'cmykimage' in a .mat file)

load(fname);

% determine pixel size (in points)

pix = (paperwidth*72) / size(cmykimage,2);

% initialize the ps file
initps;

% set the pixelsize
al(['/px ',num2str(pix),' def'],['% Set physical pixel size']);
al([num2str(pix*aspectratio)' setlinewidth']);

% define a pixel
al(['/colorstroke {setcmykcolor stroke} bind def'],['% Color & stroke']);
al(['/putpixel {']);
al(['  newpath']);
al(['  moveto'],                     ['% Current point is on stack']);
al(['  0 px rlineto'],               ['% Move Right']);
% al(['  closepath']);
al(['  cyn mag yel blk colorstroke'],['% Color & stroke pixel']);
al(['} def']);   

% start at top left corner
row = size(cmykimage,1);
col = size(cmykimage,2);
for i=1:row,
  for j=1:col,
    y = pix*(row-(i-1));
    x = pix*(j-1); 
    cyan   =cmykimage(i,j,1);    
    magenta=cmykimage(i,j,2);    
    yellow =cmykimage(i,j,3);    
    black  =cmykimage(i,j,4);
    al (['/cyn ',num2str(cyan),' def'],     ['% Define colors']);
    al (['/mag ',num2str(magenta),' def']);
    al (['/yel ',num2str(yellow),' def']);
    al (['/blk ',num2str(black),' def']);   
    al ([num2str(y),' ',num2str(x),' putpixel'],['% Add pixel']);
  end;
end;

al (['showpage']);

fid=fopen([fname,'.ps'],'w');
fclose(fid);
more off;
spc = repmat (' ',size(PSText,1),1);
diary ([fname,'.ps']);
disp ([PSText spc PSComment]);
diary off;





