[comp.graphics] source from Roy Hall's book, part 1/2

toc@batcomputer.tn.cornell.edu (Timothy F. O'Connor) (09/30/89)

	Well, looks like I got too many mail responses so here's the
	stuff.  This is part 1 of 2 and simply describes how to decode
	part 2.  Enjoy.

							to'c

-cold-beer-cold-beer-cold-beer-cold-beer-cold-beer-cold-beer-cold-beer-cold-beer

------------------------------------------------------------------------------
	"Illumination and Color in Computer Generated Imagery"
	Source for code given in the Appendices
	(c)Roy Hall - 1988,1989
------------------------------------------------------------------------------

	This code was developed to test and demonstrate concepts
	discussed in "Illumination and Color for Computer Generated
	Imagery" (1989 from Springer-Verlag).  While the code was
	used in the generation of the diagrams and images presented
	in the text, it may still contain bugs.  The user is cautioned
	to independently verify that the code is suitable to his/her
	needs.  This code is distributed on an "as is" basis with
	all faults and without any implied or expressed warranties
	or support from either the author, Roy Hall, or from
	Springer-Verlag Publishers.

	I am making this code available to the user community.  You
	are free to use it as you wish.  In return I request two
	things.  First, that this README file always stays with any
	copy of the source, more specifically, that proper credit is
	given to the original author.  Second, that any corrections,
	improvements, etc. are passed back to the author for inclusion
	in subsequent distribution and editions of the book.

	Please note that the code was written for clarity of presentation,
	not for performance.  You will find that optimizations can be
	easily made.  Please do not report these as I will not incorporate
	them into future releases.  Note also that this code relects
	current work I am involved in and may differ slightly from
	that presented in the text.


	Roy Hall
	Program of Computer Graphics
	120 Rand Hall
	Cornell University
	Ithaca, NY 14853

	roy@wisdom.graphics.cornell.edu





	Contents:
		README
		geo.h
		geo.c
		F.h
		F.c
		D.h
		D.c
		G.h
		G.c
		clr.h
		clr.c
		clr_clip.c
		clr_sample.c
		illum_mod.h
		illum_mod.c


------------------------------------------------------------------------------

	This mail message is a precursor to the message containing
	the code described above.

	Write the next message into a file called 'tmp' and
	strip the mail header.  The first line in the file should
	then be:

	    xbtoa Begin

	Run this file through atob (source at the end of the message):

	    atob <tmp >code.tar.Z

	Uncompress this file:

	    uncompress code.tar.Z

	The untar it:

	    tar -xvf code.tar



	Source for atob
----------------------------  cut here  ------------------------------
/* atob: version 4.0
 * stream filter to change printable ascii from "btoa" back into 8 bit bytes
 * if bad chars, or Csums do not match: exit(1) [and NO output]
 *
 *  Paul Rutter         Joe Orost
 *  philabs!per         petsd!joe
 */

#include <stdio.h>

#define reg register

#define streq(s0, s1)  strcmp(s0, s1) == 0

#define times85(x)	((((((x<<2)+x)<<2)+x)<<2)+x)

long int Ceor = 0;
long int Csum = 0;
long int Crot = 0;
long int word = 0;
long int bcount = 0;

fatal() {
  fprintf(stderr, "bad format or Csum to atob\n");
  exit(1);
}

#define DE(c) ((c) - '!')

decode(c) 
  reg c;
{
  if (c == 'z') {
    if (bcount != 0) {
      fatal();
    } else {
      byteout(0);
      byteout(0);
      byteout(0);
      byteout(0);
    }
  } else if ((c >= '!') && (c < ('!' + 85))) {
    if (bcount == 0) {
      word = DE(c);
      ++bcount;
    } else if (bcount < 4) {
      word = times85(word);
      word += DE(c);
      ++bcount;
    } else {
      word = times85(word) + DE(c);
      byteout((int)((word >> 24) & 255));
      byteout((int)((word >> 16) & 255));
      byteout((int)((word >> 8) & 255));
      byteout((int)(word & 255));
      word = 0;
      bcount = 0;
    }
  } else {
    fatal();
  }
}

FILE *output;

byteout(c) 
  reg c;
{
  Ceor ^= c;
  Csum += c;
  Csum += 1;
  if ((Crot & 0x80000000)) {
    Crot <<= 1;
    Crot += 1;
  } else {
    Crot <<= 1;
  }
  Crot += c;
  putc(c, output);
}

main(argc, argv) 
  char **argv;
{
  reg c;
  reg long int i;
  char buf[100];
  long int n1, n2, oeor, osum, orot;
  int start = 1;

  if (argc != 1) {
    fprintf(stderr,"bad args to %s\n", argv[0]);
    exit(2);
  }
  output = stdout;

  for (;;) {
    Ceor = 0;
    Csum = 0;
    Crot = 0;
    word = 0;
    bcount = 0;

    /*search for header line*/
    for (;;) {
      if (fgets(buf, sizeof buf, stdin) == NULL) {
	if (start)
	  fatal();
	goto done;
      }
      if (streq(buf, "xbtoa Begin\n")) {
	break;
      }
    }
    start = 0;

    while ((c = getchar()) != EOF) {
      if (c == '\n') {
	continue;
      } else if (c == 'x') {
	break;
      } else {
	decode(c);
      }
    }
    if(scanf("btoa End N %ld %lx E %lx S %lx R %lx\n",
	   &n1, &n2, &oeor, &osum, &orot) != 5) {
      fatal();
    }
    if ((n1 != n2) || (oeor != Ceor) || (osum != Csum) || (orot != Crot)) {
      fatal();
    }
  }
 done:
  exit(0);
}