Skip to content

Instantly share code, notes, and snippets.

@njligames
Created April 7, 2020 19:50
Show Gist options
  • Save njligames/26eb15aabf41df73a9301086ea4039dd to your computer and use it in GitHub Desktop.
Save njligames/26eb15aabf41df73a9301086ea4039dd to your computer and use it in GitHub Desktop.
#include <algorithm>
// Source
// https://www.nottingham.ac.uk/~etzpc/nz/sirds/creation.html#Subject22
static int round(int X) {
return (int)((X)+0.5);
}
static const int DPI = 72;
static float E = round(2.5 * DPI);
static float mu = (1/3.0);
static float separation(float Z) {
return round((1-mu*Z)*E/(2-mu*Z));
}
static float far = separation(0);
//#define maxX 256
//#define maxY 256
static void DrawAutoStereogram(float **Z, int maxX, int maxY, int **out)
{
int x, y;
for( y = 0; y < maxY; y++ ) {
int pix[maxX];
int same[maxX];
int s;
int left, right;
/* initialise the links */
for( x = 0; x < maxX; x++ )
same[x] = x;
/* calculate the links for the Z[][] object */
for( x = 0; x < maxX; x++ ) {
s = separation(Z[x][y]);
left = x - (s/2);
right = left + s;
if( 0 <= left && right < maxX ){
{ int k;
for(k=same[left]; k!=left && k!=right; k=same[left])
if( k < right )
left = k;
else {
left = right;
right = k;
}
same[left] = right;
}
}
}
/* assign the colors */
for( x = maxX-1; x >= 0; x-- ) {
if( same[x] == x ) pix[x] = random()&1;
else pix[x] = pix[same[x]];
out[x][y] = pix[x];
// Set_Pixel(x, y, pix[x]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment