ftp.nice.ch/pub/next/unix/graphics/nphoon.N.bs.tar.gz#/nphoon/nphoon.c

This is nphoon.c in view mode; [Download] [Up]

#ifndef lint
static char rcsid[] =
    "@(#) $Header: xphoon.c,v 1.9 88/08/26 22:29:47 jef Exp $ (LBL)";
#endif

/*
** Copyright (C) 1988 by Jef Poskanzer and Craig Leres.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

/*#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>*/
#define BitmapSize(w,h) ((((w)+7)/8)*(h))
#include <stdio.h>
#include <math.h>
#include "tws.h"

char *argv0;
int blackflag = 0;
int demoflag = 0;
/*Display *dpy = NULL;*/

char *malloc();

main(argc, argv)
	int argc;
	char **argv;
{
	char *display = NULL;
	int w, h, cx, cy, r;
	int size;
	char *bits, *xbits;
	int delayminutes = 0;
	long bits_size;

	argv0 = argv[0];
  	getbitmap(&w, &h, &bits, &cx, &cy, &r, &bits_size);

again:
	if (argc > 1 && strcmp(argv[1], "-b") == 0) {
		argv++;
		argc--;
		blackflag = 1;
		goto again;
	}
	if (argc > 1 && strcmp(argv[1], "-d") == 0) {
		argv++;
		argc--;
		demoflag = 1;
		goto again;
	}
	if (argc > 2 && strcmp(argv[1], "-t") == 0) {
		argv++;
		argc--;
		if (sscanf(argv[1], "%d", &delayminutes) != 1)
			goto usage;
		argv++;
		argc--;
		goto again;
	}
	if (argc > 2 && strcmp(argv[1], "-x") == 0) {
		argv++;
		argc--;
		if (sscanf(argv[1], "%d", &cx) != 1)
			goto usage;
		argv++;
		argc--;
		goto again;
	}
	if (argc > 2 && strcmp(argv[1], "-y") == 0) {
		argv++;
		argc--;
		if (sscanf(argv[1], "%d", &cy) != 1)
			goto usage;
		argv++;
		argc--;
		goto again;
	}
	if (argc > 2 && strcmp(argv[1], "-display") == 0) {
		argv++;
		argc--;
		display = argv[1];
		argv++;
		argc--;
		goto again;
	}

	if (argc > 1) {
usage:
		fprintf(stderr,
		    "usage: %s [-b] [-t minutes] [-display display]\n", argv0);
		exit(1);
		
	}

	/*if ((dpy = XOpenDisplay(display)) == 0) {
		fprintf(stderr, "%s: Can't open display \"%s\"\n",
		    argv0, XDisplayName(display));
		exit(1);
	}*/

	if (delayminutes <= 0 && ! demoflag) {
		hackbits(dtwstime(), w, h, bits, cx, cy, r);
		setroot(w, h, bits, bits_size);
		/*XCloseDisplay(dpy);*/
		exit(0);
	}

	size = BitmapSize(w, h);
	xbits = (char *)malloc(size);
	for (;;) {
		bcopy((char *)bits, (char *)xbits, size);
		hackbits(dtwstime(), w, h, xbits, cx, cy, r);
		setroot(w, h, xbits, bits_size);
		if ( demoflag )
		    sleep(1);  /* continuous mode */
		else
		    sleep(delayminutes * 60);
	}
	/* NOTREACHED */
}


setroot(w, h, bits, bits_size)
	int w, h;
	char *bits;
	long bits_size;
{
  FILE *tempfile;
  char *ptr;
  long end;
  int i,j;
  char call_string[1024];
  char *tempfilename = "/usr/tmp/nextmoon.xbm";
  char *psfilename = "/usr/tmp/nextmoon.ps";

  tempfile = fopen(tempfilename, "w");
  if ( tempfile == NULL ) {
        perror("Cannot open temp file\n");
        exit(1);
  }

  fprintf(tempfile, "#define nextmoon_width %d\n", w);
  fprintf(tempfile, "#define nextmoon_height %d\n", h);
  fprintf(tempfile, "static char nextmoon_bits[] = {\n");
  ptr = bits;
  i = 0;
  end = bits_size / XBM_BYTES_PER_LINE;
  if ( end % XBM_BYTES_PER_LINE ) {
	  while ( i++ < end ) {
	        for (j = 0; j < XBM_BYTES_PER_LINE; j++ )
	                fprintf(tempfile, "%#.2hx,", (*ptr++ & 0x00ff));
	        fprintf(tempfile, "\n");
	  }
	  end = (i - 1) * XBM_BYTES_PER_LINE;
	  for (i = end; i < (bits_size - 1); i++ )
	        fprintf(tempfile, "%#.2hx,", (*ptr++ & 0x00ff));
	  fprintf(tempfile, "%#.2hx};\n", (*ptr & 0x00ff));
  }
  else {
	while ( i++ < (end - 1) ) {
		for (j = 0; j < XBM_BYTES_PER_LINE; j++ )
			fprintf(tempfile, "%#.2hx,", (*ptr++ & 0x00ff));
		fprintf(tempfile, "\n");
	for (i = 0; i < 14; i++ )
		fprintf(tempfile, "%#.2hx,", (*ptr++ & 0x00ff));
	fprintf(tempfile, "%#.2hx};\n", (*ptr & 0x00ff));
	}
  }
  fclose(tempfile);
  /*sprintf(call_string, "xbmtopbm %s | pbmtoxwd | xwdtoppm | ppmtopgm | pgmtops > %s", tempfilename, psfilename);*/
  /*sprintf(call_string, "xbmtopbm %s | ppmtopgm | pgmtops > %s", tempfilename, psfilename);*/
  sprintf(call_string, "xbmtopbm %s | pgmtops > %s", tempfilename, psfilename);
  system(call_string);
  sprintf(call_string, "background -c %s", psfilename);
  system(call_string);
  (void) remove(psfilename);
  (void) remove(tempfilename);
}


#ifdef notdef
short leftmask[16] = {
    0xffff, 0xfffe, 0xfffc, 0xfff8, 0xfff0, 0xffe0, 0xffc0, 0xff80,
    0xff00, 0xfe00, 0xfc00, 0xf800, 0xf000, 0xe000, 0xc000, 0x8000,
};
short rightmask[16] = {
    0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f,
    0x00ff, 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff,
};
#endif notdef
static char  leftmask[8] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
static char rightmask[8] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f};

static char  shade_0_bits[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
static char  shade_1_bits[] = {0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
static char  shade_2_bits[] = {0xfe,0xff,0xfb,0xff,0xff,0xff,0xff,0xff};
static char  shade_3_bits[] = {0xfe,0xff,0xfb,0xff,0x7f,0xff,0xff,0xff};
static char  shade_4_bits[] = {0xfe,0xff,0xfb,0xff,0x7f,0xff,0xff,0xef};
static char  shade_5_bits[] = {0xfe,0xbf,0xfb,0xff,0x7f,0xff,0xff,0xef};
static char  shade_6_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7f,0xff,0xff,0xef};
static char  shade_7_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7f,0xff,0xfe,0xef};
static char  shade_8_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7f,0xdf,0xfe,0xef};
static char  shade_9_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7d,0xdf,0xfe,0xef};
static char shade_10_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7d,0xdf,0xfa,0xef};
static char shade_11_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7d,0xdf,0xfa,0xaf};
static char shade_12_bits[] = {0xfe,0xbf,0xfa,0xdf,0x7d,0xdf,0xfa,0xaf};
static char shade_13_bits[] = {0xfe,0xaf,0xfa,0xdf,0x7d,0xdf,0xfa,0xaf};
static char shade_14_bits[] = {0xfe,0xaf,0xfa,0xdf,0x75,0xdf,0xfa,0xaf};
static char shade_15_bits[] = {0xfa,0xaf,0xfa,0xdf,0x75,0xdf,0xfa,0xaf};

char *shades[16] = {
    shade_0_bits,  shade_1_bits,  shade_2_bits,  shade_3_bits,
    shade_4_bits,  shade_5_bits,  shade_6_bits,  shade_7_bits,
    shade_8_bits,  shade_9_bits,  shade_10_bits, shade_11_bits,
    shade_12_bits, shade_13_bits, shade_14_bits, shade_15_bits };

#define PI 3.14159265358979323846  /* Assume not near black hole or in
				      Tennessee */

double jtime(), phase();

hackbits(t, w, h, bits, cx, cy, r)
	struct tws *t;
	int w, h;
	char *bits;
	int cx, cy, r;
{
	double jd, angphase, cphase, aom, cdist, cangdia, csund, csuang;
	int i;
	register int x, y;
	int xleft, xright;
	double fxleft, fxright;
	double fy;
	int wxright, bxright, wxleft, bxleft;
	int off;
	double cap, ratio;
	int shadeindex;
	char shade;
	static double demoinc = 0.0;

	jd = jtime( t );
	if ( demoflag ) {
		/* Jump ahead a day each time through. */
		jd += demoinc;
		demoinc += 1.0;
	}

	angphase = phase( jd, &cphase, &aom, &cdist, &cangdia, &csund, &csuang);
	cap = cos( angphase );

	/* Hack to figure approximate earthlighting. */
	if ( cphase < 0.1 ) cphase = 0.1;
	if ( cphase > 0.9 ) cphase = 0.9;
	ratio = (1.0 - cphase) / cphase;  /* ratio varies from 9.0 to 0.111 */
	shadeindex = (int) ( ratio / 9.0 * 15.9999 );

#ifdef DEBUG
printf("angphase %f, cap %f\n", angphase, cap);
#endif

	for (i = 0; i < 2 * r; i++) {
		y = cy - r + i;
		fy = i - r;
		fxright = r * sqrt(1.0 - (fy * fy) / (r * r));
		fxleft = - fxright;
		if (angphase >= 0.0 && angphase < PI)
			fxright *= cap;
		else
			fxleft *= cap;

		xright = fxright + cx;
		xleft = fxleft + cx;

		wxright = xright / 8;
		bxright = xright % 8;

		wxleft = xleft / 8;
		bxleft = xleft % 8;

		off = y * ((w + 7) / 8);

		if ( blackflag )
			shade = 0xff;
		else
			shade = shades[shadeindex][y % 8];
		if (wxleft == wxright)
			bits[wxleft + off] |=
				leftmask[bxleft] & shade & rightmask[bxright];
		else {
			bits[wxleft + off] |= leftmask[bxleft] & shade;
			for (x = wxleft + 1; x < wxright; x++)
				bits[x + off] |= shade;
			bits[wxright + off] |= rightmask[bxright] & shade;
		}
	}
	
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.