Term-Gnuplot

 view release on metacpan or  search on metacpan

gnuterm/bitmap.c  view on Meta::CPAN

 * documentation for any purpose with or 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.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the complete modified source code.  Modifications are to
 * be distributed as patches to the released version.  Permission to
 * distribute binaries produced by compiling modified sources is granted,
 * provided you
 *   1. distribute the corresponding source modifications from the
 *    released version in the form of a patch file along with the binaries,
 *   2. add special version identification to distinguish your version
 *    in addition to the base release version number,
 *   3. provide your name and address as the primary contact for the
 *    support of your modified version, and
 *   4. retain our contact information in regard to use of the base
 *    software.
 * Permission to distribute the released version of the source code along
 * with corresponding source modifications in the form of a patch file is
 * granted with same provisions 2 through 4 for binary distributions.
 *
 * This software is provided "as is" without express or implied warranty
 * to the extent permitted by applicable law.
]*/


/*
 * AUTHORS
 * 
 *   Original Software:
 *     Jyrki Yli-Nokari <jty@intrin.UUCP>
 *     Ronald J. Hartranft <rjh2@ns.cc.lehigh.edu>
 *     Russell Lang <rjl@monu1.cc.monash.edu.au>
 */


/*
   ** General raster plotting routines.
   ** Raster routines written and copyrighted 1987 by
   ** Jyrki Yli-Nokari (jty@intrin.UUCP)
   ** Intrinsic, Ltd.
   **
   ** You may use this code for anything you like as long as
   ** you are not selling it and the credit is given and
   ** this message retained.
   **
 */

/* Bitmap plotting routines derived from above raster plotting routines
 * Russell Lang, 1990
 */

#include "bitmap.h"

#include "alloc.h"
#include "util.h"

static void b_putc __PROTO((unsigned int, unsigned int, int, unsigned int));
static GP_INLINE void b_setpixel __PROTO((unsigned int x, unsigned int y, unsigned int value));
static GP_INLINE void b_setmaskpixel __PROTO((unsigned int x, unsigned int y, unsigned int value));
static void b_line __PROTO((unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2));

bitmap *b_p = (bitmap *) NULL;	/* global pointer to bitmap */
static unsigned int b_currx, b_curry; /* the current coordinates */
unsigned int b_xsize, b_ysize;	/* the size of the bitmap */
unsigned int b_planes;		/* number of color planes */
unsigned int b_psize;		/* size of each plane */
unsigned int b_rastermode;	/* raster mode rotates -90deg */
unsigned int b_linemask = 0xffff; /* 16 bit mask for dotted lines */
static unsigned int b_value = 1; /* colour of lines */
static unsigned int b_hchar;	/* width of characters */
static unsigned int b_hbits;	/* actual bits in char horizontally */
static unsigned int b_vchar;	/* height of characters */
static unsigned int b_vbits;	/* actual bits in char vertically */
unsigned int b_angle;		/* rotation of text */
static char_box b_font[FNT_CHARS]; /* the current font */
static unsigned int b_pattern[] = { 0xffff, 0x1111, 0xffff, 0x5555,
				    0x3333, 0x7777, 0x3f3f, 0x0f0f, 0x5f5f };
int b_maskcount = 0;
static unsigned int b_lastx;
static unsigned int b_lasty;	/* last pixel set - used by b_line */

#define IN(i,size)  ((unsigned)i < (unsigned)size)

/* 5x9 font, bottom row first, left pixel in lsb */
const char_row GPFAR fnt5x9[FNT_CHARS][FNT5X9_VBITS] = {
  /* */  {000000,000000,000000,000000,000000,000000,000000,000000,000000},
  /*!*/  {000000,000000,0x0004,000000,0x0004,0x0004,0x0004,0x0004,0x0004},
  /*"*/  {000000,000000,000000,000000,000000,000000,0x000a,0x000a,0x000a},
  /*#*/  {000000,000000,0x000a,0x000a,0x001f,0x000a,0x001f,0x000a,0x000a},
  /*$*/  {000000,000000,0x0004,0x000f,0x0014,0x000e,0x0005,0x001e,0x0004},
  /*%*/  {000000,000000,0x0018,0x0019,0x0002,0x0004,0x0008,0x0013,0x0003},
  /*&*/  {000000,000000,0x0016,0x0009,0x0015,0x0002,0x0005,0x0005,0x0002},
  /*'*/  {000000,000000,000000,000000,000000,0x0002,0x0004,0x0006,0x0006},
  /*(*/  {000000,000000,0x0008,0x0004,0x0002,0x0002,0x0002,0x0004,0x0008},
  /*)*/  {000000,000000,0x0002,0x0004,0x0008,0x0008,0x0008,0x0004,0x0002},
  /***/  {000000,000000,0x0004,0x0015,0x000e,0x001f,0x000e,0x0015,0x0004},
  /*+*/  {000000,000000,000000,0x0004,0x0004,0x001f,0x0004,0x0004,000000},
  /*,*/  {000000,0x0002,0x0004,0x0006,0x0006,000000,000000,000000,000000},
  /*-*/  {000000,000000,000000,000000,000000,0x001f,000000,000000,000000},
  /*.*/  {000000,000000,0x0006,0x0006,000000,000000,000000,000000,000000},
  /*-/-*/{000000,000000,000000,0x0001,0x0002,0x0004,0x0008,0x0010,000000},
  /*0*/  {000000,000000,0x000e,0x0011,0x0013,0x0015,0x0019,0x0011,0x000e},
  /*1*/  {000000,000000,0x000e,0x0004,0x0004,0x0004,0x0004,0x0006,0x0004},
  /*2*/  {000000,000000,0x001f,0x0001,0x0001,0x000e,0x0010,0x0011,0x000e},
  /*3*/  {000000,000000,0x000e,0x0011,0x0010,0x000c,0x0010,0x0011,0x000e},
  /*4*/  {000000,000000,0x0008,0x0008,0x001f,0x0009,0x000a,0x000c,0x0008},
  /*5*/  {000000,000000,0x000e,0x0011,0x0010,0x0010,0x000f,0x0001,0x001f},
  /*6*/  {000000,000000,0x000e,0x0011,0x0011,0x000f,0x0001,0x0002,0x000c},
  /*7*/  {000000,000000,0x0001,0x0001,0x0002,0x0004,0x0008,0x0010,0x001f},
  /*8*/  {000000,000000,0x000e,0x0011,0x0011,0x000e,0x0011,0x0011,0x000e},
  /*9*/  {000000,000000,0x0006,0x0008,0x0010,0x001e,0x0011,0x0011,0x000e},
  /*:*/  {000000,000000,000000,0x0006,0x0006,000000,0x0006,0x0006,000000},
  /*;*/  {000000,0x0001,0x0002,0x0006,0x0006,000000,0x0006,0x0006,000000},
  /*<*/  {000000,000000,0x0008,0x0004,0x0002,0x0001,0x0002,0x0004,0x0008},
  /*=*/  {000000,000000,000000,000000,0x001f,000000,0x001f,000000,000000},
  /*>*/  {000000,000000,0x0002,0x0004,0x0008,0x0010,0x0008,0x0004,0x0002},
  /*?*/  {000000,000000,0x0004,000000,0x0004,0x0008,0x0010,0x0011,0x000e},
  /*@*/  {000000,000000,0x000e,0x0015,0x0015,0x0016,0x0010,0x0011,0x000e},
  /*A*/  {000000,000000,0x0011,0x0011,0x001f,0x0011,0x0011,0x000a,0x0004},

gnuterm/bitmap.c  view on Meta::CPAN

}


/*
 * allocate the bitmap
 */
void
b_makebitmap(x, y, planes)
unsigned int x, y, planes;
{
    register unsigned j;
    unsigned rows;

    x = 8 * (unsigned int) (x / 8.0 + 0.9);	/* round up to multiple of 8 */
    y = 8 * (unsigned int) (y / 8.0 + 0.9);	/* round up to multiple of 8 */
    b_psize = y / 8;		/* size of each plane */
    rows = b_psize * planes;	/* total number of rows of 8 pixels high */
    b_xsize = x;
    b_ysize = y;
    b_currx = b_curry = 0;
    b_planes = planes;
    b_value = 1;
    b_angle = 0;
    b_rastermode = 0;
    /* allocate row pointers */
    b_p = (bitmap *) gp_alloc(rows * sizeof(pixels *), "bitmap row buffer");
    memset(b_p, 0, rows * sizeof(pixels *));
    for (j = 0; j < rows; j++) {
	/* allocate bitmap buffers */
	(*b_p)[j] = (pixels *) gp_alloc(x * sizeof(pixels), (char *) NULL);
	if ((*b_p)[j] == (pixels *) NULL) {
	    b_freebitmap();	/* free what we have already allocated */
	    int_error(NO_CARET, "out of memory for bitmap buffer");
	}
	memset((*b_p)[j], 0, x * sizeof(pixels));
    }
}


/*
 * free the allocated bitmap
 */
void
b_freebitmap()
{
    unsigned int j, rows;

    rows = b_psize * b_planes;	/* total number of rows of 8 pixels high */
    for (j = 0; j < rows; j++) {
	(void) free((char *) (*b_p)[j]);
    }
    (void) free((char *) b_p);
    b_p = (bitmap *) (NULL);
}


/*
 * set pixel at (x,y) with color b_value and dotted mask b_linemask.
 */
static GP_INLINE void
b_setmaskpixel(x, y, value)
    unsigned int x, y, value;
{
    /* dotted line generator */
    if ((b_linemask >> b_maskcount) & (unsigned int) (1)) {
	b_setpixel(x, y, value);
    }
    b_maskcount = (b_maskcount + 1) % 16;
    b_lastx = x;		/* last pixel set with mask */
    b_lasty = y;
}


/*
 * draw a line from (x1,y1) to (x2,y2)
 * with color b_value and dotted mask b_linemask.
 */
static void
b_line(x1, y1, x2, y2)
unsigned int x1, y1, x2, y2;
{
    int runcount;
    int dx, dy;
    int xinc, yinc;
    unsigned int xplot, yplot;

    runcount = 0;
    dx = abs((int) (x1) - (int) (x2));
    if (x2 > x1)
	xinc = 1;
    else if (x2 == x1)
	xinc = 0;
    else
	xinc = -1;
    dy = abs((int) (y1) - (int) (y2));
    if (y2 > y1)
	yinc = 1;
    else if (y2 == y1)
	yinc = 0;
    else
	yinc = -1;
    xplot = x1;
    yplot = y1;
    if (dx > dy) {
	/* iterate x */
	if ((b_linemask == 0xffff) ||
	    ((xplot != b_lastx) && (yplot != b_lasty)))
	    b_setmaskpixel(xplot, yplot, b_value);
	while (xplot != x2) {
	    xplot += xinc;
	    runcount += dy;
	    if (runcount >= (dx - runcount)) {
		yplot += yinc;
		runcount -= dx;
	    }
	    b_setmaskpixel(xplot, yplot, b_value);
	}
    } else {
	/* iterate y */
	if ((b_linemask == 0xffff) ||
	    ((xplot != b_lastx) && (yplot != b_lasty)))
	    b_setmaskpixel(xplot, yplot, b_value);
	while (yplot != y2) {
	    yplot += yinc;
	    runcount += dx;
	    if (runcount >= (dy - runcount)) {
		xplot += xinc;
		runcount -= dy;
	    }
	    b_setmaskpixel(xplot, yplot, b_value);
	}
    }
}


/*
 * set character size
 */
void
b_charsize(size)
unsigned int size;
{
    int j;
    switch (size) {
    case FNT5X9:
	b_hchar = FNT5X9_HCHAR;
	b_hbits = FNT5X9_HBITS;
	b_vchar = FNT5X9_VCHAR;
	b_vbits = FNT5X9_VBITS;
	for (j = 0; j < FNT_CHARS; j++)
	    b_font[j] = fnt5x9[j];
	break;
    case FNT9X17:
	b_hchar = FNT9X17_HCHAR;
	b_hbits = FNT9X17_HBITS;
	b_vchar = FNT9X17_VCHAR;
	b_vbits = FNT9X17_VBITS;
	for (j = 0; j < FNT_CHARS; j++)
	    b_font[j] = fnt9x17[j];
	break;
    case FNT13X25:
	b_hchar = FNT13X25_HCHAR;
	b_hbits = FNT13X25_HBITS;
	b_vchar = FNT13X25_VCHAR;
	b_vbits = FNT13X25_VBITS;
	for (j = 0; j < FNT_CHARS; j++)
	    b_font[j] = fnt13x25[j];
	break;
    default:
	int_error(NO_CARET, "Unknown character size");
    }
}


/*
 * put characater c at (x,y) rotated by angle with color b_value.
 */
static
void b_putc(x, y, c, c_angle)
unsigned int x, y;
int c;
unsigned int c_angle;
{
    unsigned int i, j, k;
    char_row fc;

    j = c - ' ';

    if (j >= FNT_CHARS)
	return;			/* unknown (top-bit-set ?) character */



( run in 2.074 seconds using v1.01-cache-2.11-cpan-6fb7bf0f510 )