From: kenh@leps5.phys.psu.edu (Ken Hornstein)
Newsgroups: alt.3d
Subject: Here's my RDS program
Date: 9 Nov 92 21:45:30 GMT
Organization: Penn State, Laboratory for Elementary Particle Science

I've received a lot of people asking for a copy of my RDS program.  Well,
here it is.  Please note that this is NOT an example of a well-written C
program; it's a cheap hack, but it works :-)  Any comments/suggestions are
welcome.

Share and Enjoy,

--Ken
-------------------------------cut here-----------------------------------
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  Makefile README sirds.c
# Wrapped by kenh@leps5 on Mon Nov  9 16:37:39 1992
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(147 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
X#
X# Very simple makefile for sirds
X#
X
XCC=cc
XOBJS=sirds.o
X
Xall: sirds
X
Xsirds: $(OBJS)
X	$(CC) -o sirds $(OBJS) -lm
X
Xclean:
X	rm -f $(OBJS) sirds core
END_OF_FILE
if test 147 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'README' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(1574 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
XSirds - a VERY simple Single Image Random Dot Stereogram Generator.
X
XI have always been facinated by those random-dot stereograms featured in Games
XMagazine.  So, I decided to write my own.  Sirds generates a PostScript file
Xthat when printed will generate a random dot stereogram.
X
XHow do you use it?  After unpacking the archive, type "make".  The code has
Xonly been tested under Ultrix, but it should be trivial to port to other
Xoperating systems.  After doing this, type in "sirds > test.ps", and ship
Xtest.ps off to your favorite laserprinter.  When you use the merge triangles
Xat the top of the page, you should see a torus.
X
XThe algorithm is very simple; a array of random pixels is created the same
Xlength as the distance between the focusing trianges.  pixel j is computed
Xusing the following forumla:
X
X		pixel[j]=pixel[j - (offset - height)]
X
XThe function "zval" returns the value for "height" in my program.  Right
Xnow it's set up to return the z values for a torus sitting in a pit; the
Xfunction in comments will generate a "bullseye" picture.  It would be
Xeasy to extent zval to read, for example, a pgm file, using the gray level
Xas a height value.
X
XThis program is by no means a complete or finished project; it's a quick hack
Xthat I whipped up so I could play with RDS images.  Having to recompile it
Xeverytime you change the function is a major drag; maybe I'll put in some
Xsort of interpreter eventually.  Maybe not :-)
X
XAny comments/suggestions/improvements are welcome.  Let me know what you
Xthink of it!
X
XKen Hornstein
X<kenh@leps5.phys.psu.edu>
X11/9/92
END_OF_FILE
if test 1574 -ne `wc -c <'README'`; then
    echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'sirds.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'sirds.c'\"
else
echo shar: Extracting \"'sirds.c'\" \(2566 characters\)
sed "s/^X//" >'sirds.c' <<'END_OF_FILE'
X/* A simple program to generate a single-image random dot stereogram */
X
X#include <stdio.h>
X#include <fcntl.h>
X#include <math.h>
X#include <time.h>
X
Xchar protoheader[] = "%%!PS-Adobe-2.0\n\
X%%%%Creator: sirds - By Ken Hornstein\n\
X%%%%CreationDate: %s\
X%%%%EndComments\n\
X\n\
X%% Define some functions to be used later\n\
X\n\
X/inch { 72 mul } def\n\
X/drawtick {\n\
X	moveto\n\
X	10 -7 rlineto\n\
X	-20 0 rlineto\n\
X	closepath fill\n\
X} def\n\
X\n\
X%% Set up the page for the image command\n\
X\n\
X7 inch 1.25 inch translate\n\
X90 rotate\n\
Xgsave\n\n";
X
Xchar footer[] = "\ngrestore\n\nnewpath\n\n250 0 drawtick\n\
X250 %d add 0 drawtick\n\
X\n\
Xshowpage\n";
X
Xint zval();
X
Xint xwidth,ywidth;
X
Xmain(argc, argv)
Xint argc;
Xchar **argv;
X{
X	float z;
X	int i,j,x,y,offset,n;
X	short bits[2000];
X	time_t t;
X	
X	xwidth=1200;
X	ywidth=800;
X
X	offset=180;
X
X	srandom((int) time(NULL));
X
X/* output a PostScript header */
X
X	t=time(NULL);
X
X	printf(protoheader,ctime(&t));
X
X	printf("/picstr %d string def\n\n",xwidth/4);
X	printf("%d %d scale\n\n",xwidth/2,ywidth/2);
X
X	printf("%% Generate image from bitmap data\n\n");
X	printf("%d %d 1 [ %d 0 0 %d 0 0 ]\n",xwidth,ywidth,xwidth,ywidth);
X	printf("{ currentfile picstr readhexstring pop }\nimage\n");
X	
X
X/* For the first "offset" entries, seed the array with random 1's and 0's */
X
X	for(y=0; y<800; y++) {
X
X		for(i=0; i<offset; i++)
X			bits[i]=(short)(random()&01);
X
X/* from offset to max, compute bits based on Z value.  note that the function
X   that computes a Z value must return an integer that's smaller than the
X   offset */
X
X		for(i=offset, x=0; x<1200; i++, x++)
X			bits[i]=bits[i-(offset-zval(x,y))];
X
X/* output a hex string suitable for PostScript */
X
X		for(i=offset; i<1200+offset; i+=8) {
X			n=0;
X			for(j=0; j<8; j++)
X				n += bits[i+j]<<(7-j);
X			printf("%02x",n); }
X
X		printf("\n");
X
X	}
X
X	printf("\ngrestore\n\n%% Draw focusing trianges\n\nnewpath\n");
X	printf("%d 0 drawtick\n",xwidth/4-offset/4);
X	printf("%d 0 drawtick\n",xwidth/4+offset/4);
X	printf("\nshowpage\n");
X
X	exit(0);
X}
X
X/* Our z-function goes here.  Return a z-value for a given x and y */
X
Xint zval(x,y)
Xint x;
Xint y;
X{
X	double d;
X	
X	int a,b,r;
X
X	a=100;b=280;
X
X	r=(b-a)/2;
X	
X
X	if (y<100 || y>700)
X		return(0);
X	else if (x<200 || x>1100)
X		return(0);
X	else {
X		d=sqrt((double)(x-xwidth/2)*(x-xwidth/2)+
X		               (y-ywidth/2)*(y-ywidth/2));
X		if (d>b || d<a)
X			return (-20);
X		else
X	 	  return(sqrt(r*r - (d-b/2-a/2)*(d-b/2-a/2))/r*15 - 15);
X	}
X/*		return(15*cos(sqrt ((double) ((x-xwidth/2)*(x-xwidth/2) +
X			 (y-ywidth/2)*(y-ywidth/2)))/100.0*3.14159));
X*/
X}
END_OF_FILE
if test 2566 -ne `wc -c <'sirds.c'`; then
    echo shar: \"'sirds.c'\" unpacked with wrong size!
fi
# end of 'sirds.c'
fi
echo shar: End of shell archive.
exit 0
Newsgroups: alt.3d