//======== Algorithm for drawing an autostereogram ========
#define DPI 75 // Output device has
// 75 pixels per inch
#define E 2.5*DPI // Eye separation is
// assumed to be 2.5"
#define mu (1/3.0) // Depth of field
float separation(float Z)
{
return (255-mu*Z)*E/(510-mu*Z); // Stereo separation
} // corresponding to Z
#define MAXX 256 // screen dimensions
#define MAXY 256
int same[MAXX]; // Points to a pixel to
// the right that is
// constrained to be
// this color
int pix[MAXX]; // Colors for pixels
int separationLookup[256]; // lookup table for ste-
// reoseparation values
char lHidden[MAXX]; // Z-puffer as seen by
char rHidden[MAXX]; // left and right eye
void calcSeparationTable(void) // pre-calculate stereo-
{ // disparity
int i,t;
for(i=0;i<256;i++)
{
separationLookup[i]=separation(i);
}
}
void DrawAutoStereogram(unsigned char zBuffer[][])
{ // Objects depth is
// Z[x][y],from 0 to 255
int x,y,i; // Coordinates of the
// current point
unsigned s; // Stereo separation at
// this point
int left, right; // X-values correspond-
// ing to left,right eye
for(y=0;y=0)
lHidden[left]=zBuffer[y][i];
}
for (x=0; x < MAXX; x++) // process scanline
{
left = x - separationLookup[zBuffer[x]]/2;
right = x +separationLookup[zBuffer[x]]-
separationLookup[zBuffer[x]]/2;
if (0 <= left && right < MAXX && // only if both imgs
lHidden[left] == zBuffer[x] && // are visible
rHidden[right]== zBuffer[x])
{
same[left] = right; // set constraint
}
}
for (x=MAXX-1 ; x>= 0 ; x--) // set the pixels of a
{ // scan line
if (same[x]==x) pix[x] = random(2); // Free choice
else pix[x] = pix[same[x]]; // Constrained
setPixel(x,y,pix[x]); // show pixel on screen
}
}
}