/*
** 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.
*/

#define CENTER_X ((fm_w / 2) - 1 + *cx)
#define CENTER_Y ((fm_h * 645 / 1280) + *cy)
#define RADIUS_X ((378 * fm_w / 1600) + 1 + *rx)
#define RADIUS_Y ((378 * fm_h / 1280) + 1 + *ry)

checkbitmapsize(w, h, fm_w, fm_h, cx, cy, rx, ry)
	int *w, *h;
        int fm_w, fm_h;
	int *cx, *cy, *rx, *ry;
{
	int minw, minh;

	minw = ((CENTER_X + RADIUS_X + 7)/8 - (CENTER_X - RADIUS_X)/8) * 8;
	minh = 2*RADIUS_Y;
	if (*w < minw) *w = minw;
	if (*h < minh) *h = minh;

	if (*w > fm_w)  *w = fm_w;
	if (*h > fm_h) *h = fm_h;
}

getbitmap(w, h, fm_w, fm_h, bits_p, cx, cy, rx, ry)
	int w, h, fm_w, fm_h;
	char **bits_p;
	int *cx, *cy, *rx, *ry;
{
	int bx, by;	/* origin of fullmoon bitmap in display coordinates */
	int fl, dl;	/* length of scan line in bytes of fullmoon, display */
	int fb; 	/* useable bytes of image */
	int y;		/* Current position in display bitmap */
	char *fbp, *dbp;	/* Pointers to current bitmap position */

	dl = ((w + 15) / 16) * 2;
	fl = ((fm_w + 15) / 16) * 2;
	fb = (fm_w + 7) / 8;

	bx = (dl-fb)/2;
	by = (h-fm_h)/2;

	*cx = CENTER_X + bx * 8;
	*cy = CENTER_Y + by;
	*rx = RADIUS_X;
	*ry = RADIUS_Y;

	if (w == fm_w) {
	  *bits_p = *bits_p - by * fl;
	  return;
	}
	dbp = *bits_p;
	for (y=0; y<h; y++) {
		fbp = *bits_p + (y-by)*fl - bx;
		bcopy(fbp,dbp,dl);
		dbp += dl;
	}
}


