function canvas = sirds( elev, w, pad, p )
%
%C=SIRDS( M, W [, PAD[, P]] )
%	This function generates and plots the Single Image Random-Dot
%	Stereogram (SIRDS) mapping for an integer depth matrix M.  It
%	uses a random strip of with W on the left side.
%
%	Additionally, a zero-height border of width PAD surrounds the
%	left and right, and PAD/2 on the top and bottom.  If PAD is
%	not specified, a value of W is used.
%
%	The argument P is the probability that a given bit in the left
%	hand strip is set.  This affects the density of dots, and can
%	control banding (somewhat).
%
%	The SIRDS mapping is returned.
%
%	This code is copyright (c) 1993, Dave Caughey.
%
%	E-mail: caughey@engr.uvic.ca
%	Dept. of Electrical and Computer Engineering
%	University of Victoria
%	P.0.Box 3055, Victoria, B.C., V8W 3P6 Canada
%
if nargin == 2,
    pad = w;
    p = 0.5;
elseif nargin == 3,
    p = 0.5;
elseif nargin ~= 4,
    help sirds
end;
%
% Determine the dimensions of the mapping and pad with zeros
%
[N0Y,N0X] = size(elev);
NY = N0Y+pad;
NX = N0X+2*pad;
d = zeros(NY,NX);
d((1:N0Y)+pad/2,(1:N0X)+pad) = elev;
%
% Initialize the canvas, and the increment vectors
%
canvas = (rand(w,NY) >= p);
off = w*(0:(NY-1));
offd = (0:(NY-1));
%
% Calculate the each columns' pixels
%
for c = 1:NX,
    cc = c-d(:,c)'+off;
    canvas(c+w,:) = canvas(cc);
    off = off + offd;
end
%
%  Plot the SIRDS
%
spy( canvas' )

