DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T g

⟦5cb5ef90d⟧ TextFile

    Length: 14332 (0x37fc)
    Types: TextFile
    Names: »gdevegad.c«

Derivation

└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89
    └─⟦ff23ba0e6⟧ »./ghostscript-1.3.tar.Z« 
        └─⟦a24a58cd3⟧ 
            └─⟦this⟧ »gdevegad.c« 

TextFile

/* Copyright (C) 1989 Aladdin Enterprises.  All rights reserved.
   Distributed by Free Software Foundation, Inc.

This file is part of Ghostscript.

Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing.  Refer
to the Ghostscript General Public License for full details.

Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License.  A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities.  It should be in a file named COPYING.  Among other
things, the copyright notice and this notice must be preserved on all
copies.  */

/* gdevegad(ir).c */
/* EGA driver for Ghostscript using direct access to frame buffer */
#include "gdevega.h"
#define USE_ASM 1			/* use assembly language */

/* Note that this is the only C file in the Ghostscript library */
/* that compiles in a non-ANSI mode (it uses segmented pointers). */

/* Type and macro for frame buffer pointers. */
/*** Intimately tied to the 80x86 (x<=2) addressing architecture ***/
/* This would have to be changed for a 32-bit 80386 environment. */
typedef byte far *fb_ptr;
#define mk_fb_ptr(offset) ((fb_ptr)MK_FP(regen,(offset)))

/* Structure for operation parameters. */
/* Note that this structure is known to assembly code. */
/* Not all parameters are used for every operation. */
typedef struct rop_params_s {
	fb_ptr dest;			/* pointer to frame buffer */
	int draster;			/* raster of frame buffer */
	byte far *src;			/* pointer to source data */
	int sraster;			/* source raster */
	int width;			/* width in bytes */
	int height;			/* height in scan lines */
	int shift;			/* amount to right shift source */
	int invert;			/* 0 or -1 to invert source */
	int data;			/* data for fill */
} rop_params;

/* Assembly language routines */

#if USE_ASM
void memsetcol(P1(rop_params *)); /* dest, draster, height, data */
#else
private void
memsetcol(rop_params *rop)
{	byte far *addr = rop->dest;
	int yc = rop->height;
	while ( yc-- )
	 { byte discard = *addr;
	   *addr = rop->data;
	   addr += rop->draster;
	 }
}
#endif

#if USE_ASM
void memsetrect(P1(rop_params *)); /* dest, draster, width, height, data */
#else
private void
memsetrect(rop_params *rop)
{	byte far *addr = rop->dest;
	int yc = rop->height;
	while ( yc-- )
	 { int cnt = rop->width;
	   while ( cnt-- ) *addr++ = rop->data;
	   addr += rop->draster - rop->width;
	 }
}
#endif

#if USE_ASM
void memrwcol(P1(rop_params *)); /* dest, draster, src, sraster, height, shift, invert */
#else
private void
memrwcol(rop_params *rop)
{	byte far *dp = rop->dest, *sp = rop->src;
	int yc = rop->height;
	int shift = rop->shift;
	while ( yc-- )
	 { byte discard = *dp;
	   *dp = ((*sp >> shift) + (*sp << (8 - shift))) ^ rop->invert;
	   dp += rop->draster, sp += rop->sraster;
	 }
}
#endif

#if USE_ASM
void memrwcol2(P1(rop_params *)); /* dest, draster, src, sraster, height, shift, invert */
#else
private void
memrwcol2(rop_params *rop)
{	byte far *dp = rop->dest, *sp = rop->src;
	int yc = rop->height;
	int shift = rop->shift;
	while ( yc-- )
	 { byte discard = *dp;
	   *dp = ((sp[1] >> shift) + (*sp << (8 - shift))) ^ rop->invert;
	   dp += rop->draster, sp += rop->sraster;
	 }
}
#endif

/* Forward definitions */
void dot_write(P3(int, int, int));
void fill_rectangle(P3(rop_params *, int, int));
void copy_rectangle(P6(rop_params *, int, int, byte, byte, int));

/* Define the device port and register numbers, and the regen map base */
#define out2(port, index, data)\
  (outportb(port, index), outportb((port)+1, data))
#define seq_addr 0x3c4
#define s_map 2
#define set_s_map(mask) out2(seq_addr, s_map, (mask))
#define graph_addr 0x3ce
#define g_const 0			/* set/reset */
#define set_g_const(color) out2(graph_addr, g_const, color)
#define g_const_map 1			/* enable set/reset */
#define set_g_const_map(map) out2(graph_addr, g_const_map, map)
#define g_function 3
#define set_g_function(func) out2(graph_addr, g_function, func)
#  define gf_WRITE 0
#  define gf_AND 8
#  define gf_OR 0x10
#  define gf_XOR 0x18
#define g_mask 8
#define regen 0xa000

/* Macro for writing partial words */
#define rwdot(fbptr,data)\
   { byte discard = *(fbptr); *(fbptr) = (data); }

/* Remember the current mask setting. */
/* This is a full int so we can have an out-of-band value. */
private int current_g_mask;
#define set_g_mask(mask)\
  if ( (mask) != current_g_mask )\
    out2(graph_addr, g_mask, (current_g_mask = (mask)))

/* Clean up after writing */
#define dot_end()\
  set_g_mask(0xff)			/* all bits on */

/* Initialize the EGA for graphics mode */
int
ega_open(gx_device *dev)
{	if ( ega_save_mode < 0 ) ega_save_mode = ega_get_mode();
	ega_set_mode(0x10);
	current_g_mask = 0xff;		/* all bits selected */
	set_s_map(-1);			/* enable all maps */
	return 0;
}

/* Write a dot using the EGA color codes. */
int
ega_write_dot(gx_device *dev, int x, int y, gx_color_index color)
{	validate_dot();
	dot_write(x, y, (int)color);
	dot_end();
	return 0;
}

/* Copy a monochrome bitmap.  The colors are given explicitly. */
/* Color = gx_no_color_index means transparent (no effect on the image). */
int
ega_copy_mono(gx_device *dev, byte *base, int sourcex, int raster,
  int x, int y, int w, int h, gx_color_index czero, gx_color_index cone)
{	rop_params params;
	int zero = (czero == gx_no_color_index ? -1 : (int)czero);
	int one = (cone == gx_no_color_index ? -1 : (int)cone);
	int sleft, dleft;
	byte mask, rmask;
	fb_ptr dest;
	byte *src;
	validate_rect();
	params.dest = dest = mk_fb_ptr(y * raster_x + (x >> 3));
	params.draster = raster_x;
	params.src = src = base + (sourcex >> 3);
	params.sraster = raster;
	params.height = h;
	sleft = 8 - (sourcex & 7);
	dleft = 8 - (x & 7);
	mask = 0xff >> (8 - dleft);
	if ( w < dleft )
		mask -= mask >> w,
		rmask = 0;
	else
		rmask = 0xff00 >> ((w - dleft) & 7);
	if ( zero >= 0 && one >= 0 )
	   {	params.data = 0;
		fill_rectangle(&params, x & 7, w);
	   }
	else if ( zero >= 0 )
	   {	if ( zero != 0xf )
		   {	set_g_function(gf_AND);
			params.invert = 0;
			copy_rectangle(&params, sleft, dleft, mask, rmask, w);
		   }
	   }
	else if ( one >= 0 )
	   {	if ( one != 0xf )
		   {	set_g_function(gf_AND);
			params.invert = -1;
			copy_rectangle(&params, sleft, dleft, mask, rmask, w);
		   }
	   }
	else
	  return 0;			/* nothing to do */
	if ( zero > 0 || one > 0 )
	   {	set_g_function(gf_OR);
		if ( zero > 0 )
		   {	set_s_map(zero);
			params.dest = dest;
			params.src = src;
			params.invert = -1;
			copy_rectangle(&params, sleft, dleft, mask, rmask, w);
		   }
		if ( one > 0 )
		   {	set_s_map(one);
			params.dest = dest;
			params.src = src;
			params.invert = 0;
			copy_rectangle(&params, sleft, dleft, mask, rmask, w);
		   }
		set_s_map(-1);
	   }
	set_g_function(gf_WRITE);
	dot_end();
	return 0;
}

/* Copy a color pixelmap.  This is just like a bitmap, */
/* except that each pixel takes 4 bits instead of 1. */
int
ega_copy_color(gx_device *dev, byte *base, int sourcex, int raster,
  int x, int y, int w, int h)
{	byte *line = base + (sourcex >> 1);
	int bitx;
	int py = y;
	int ex = x + w;
	validate_rect();
	bitx = x + sourcex;		/* only the bottom bit matters */
	while ( --h >= 0 )
	{	byte *bptr = line;
		int px = x;
		do
		   {	int color = ((px - bitx) & 1 ? *bptr++ & 0xf : *bptr >> 4);
			dot_write(px, py, color);
		   }
		while ( ++px < ex );
		line += raster;
		py++;
	}
	dot_end();
	return 0;
}

/* Fill a rectangle. */
int
ega_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  gx_color_index color)
{	rop_params params;
	int bit;
	validate_rect();
	params.dest = mk_fb_ptr(y * raster_x + (x >> 3));
	params.draster = raster_x;
	params.height = h;
	bit = x & 7;
	switch ( (int)color )
	   {
	case 0:
		params.data = 0;
		fill_rectangle(&params, bit, w);
		break;
	case 0xf:
		params.data = -1;
		fill_rectangle(&params, bit, w);
		break;
	default:
		set_g_const(color);
		set_g_const_map(0xf);
		fill_rectangle(&params, bit, w);	/* data is arbitrary */
		set_g_const_map(0);
	   }
	dot_end();
	return 0;
}

/* Tile a rectangle.  Note that the two colors must both be supplied, */
/* i.e. neither one can be gx_no_color_index (transparent). */
int
ega_tile_rectangle(gx_device *dev, gx_bitmap *tile,
  int x, int y, int w, int h, gx_color_index czero, gx_color_index cone)
#define zero (int)czero
#define one (int)cone
{	rop_params params;
	int xmod, width_bytes, tile_height;
	int xbit;
	int lcount;
	int mask, rmask;
	byte again;
	int const_bits, maps;
	int ymod, yleft;
	if ( tile->width & 7 ) return -1;	/* can't do it */
	/* Following is similar to aligned case of copy_mono */	
	validate_rect();
	params.dest = mk_fb_ptr(y * raster_x + (x >> 3));
	params.draster = raster_x;
	params.sraster = tile->raster;
	params.shift = 0;
	xbit = x & 7;
	/* Set up the graphics registers */
	const_bits = (zero ^ one) ^ 0xf;
	if ( const_bits )
	   {	set_g_const(zero);	/* either color will do */
		set_g_const_map(const_bits);
	   }
	if ( (maps = zero & ~one) != 0 )
	   {	set_s_map(maps += const_bits);
		params.invert = -1;
		again = one & ~zero;
	   }
	else
	   {	maps = one & ~zero;
		set_s_map(maps += const_bits);
		params.invert = 0;
		again = 0;
	   }
	xmod = (x % tile->width) >> 3;
	width_bytes = tile->width >> 3;
	tile_height = tile->height;
	mask = 0xff >> xbit;
	if ( w + xbit <= 8 )
		mask -= mask >> w,
		rmask = 0;
	else
		rmask = (0xff00 >> ((w + x) & 7)) & 0xff,
		w += xbit - 8;
	ymod = y % tile_height;
tile:	yleft = tile_height - ymod;
	params.src = tile->data + ymod * params.sraster + xmod;
	lcount = h;
	if ( rmask == 0 && w < 8 )		/* Optimize narrow case */
	   {	set_g_mask(mask);
		if ( lcount > yleft )
		   {	params.height = yleft;
			memrwcol(&params);
			params.dest += yleft * raster_x;
			params.src = tile->data + xmod;
			params.height = tile_height;
			lcount -= yleft;
			while ( lcount >= tile_height )
			   {	memrwcol(&params);
				params.dest += tile_height * raster_x;
				lcount -= tile_height;
			   }
		   }
		if ( lcount )
		   {	params.height = lcount;
			memrwcol(&params);
		   }
	   }
	else
	   {	fb_ptr line = params.dest;
		while ( 1 )
		   {	int xoff = xmod;
			int count = w;
			params.height = (lcount > yleft ? yleft : lcount);
			/* Do first byte */
			set_g_mask(mask);
			memrwcol(&params);
			/* Do full bytes */
			if ( (count -= 8) >= 0 )
			   {	set_g_mask(0xff);
				do
				   {	if ( ++xoff == width_bytes )
						xoff = 0,
						params.src -= params.sraster;
					++params.src, ++params.dest;
					memrwcol(&params);
				   }
				while ( (count -= 8) >= 0 );
			   }
			/* Do last byte */
			if ( rmask )
			   {	if ( ++xoff == width_bytes )
					xoff = 0,
					params.src -= params.sraster;
				set_g_mask(rmask);
				++params.src, ++params.dest;
				memrwcol(&params);
			   }
			if ( (lcount -= params.height) == 0 ) break;
			params.dest = line += params.height * raster_x;
			params.src = tile->data + xmod;
			yleft = tile_height;
		   }
	   }
	/* Now do the second color if needed */
	if ( again )
	   {	maps = again + const_bits;
		set_s_map(maps);
		again = 0;
		params.dest = mk_fb_ptr(y * raster_x + (x >> 3));
		params.invert = 0;
		goto tile;
	   }
	if ( maps != 0xf )
		set_s_map(-1);
	if ( const_bits )
		set_g_const_map(0);
	dot_end();
	return 0;
}

/* ------ Internal routines ------ */

/* Write a single dot.  This doesn't have to be efficient. */
private void
dot_write(int x, int y, int color)
{	fb_ptr fbptr = mk_fb_ptr(y * raster_x + (x >> 3));
	byte mask = 0x80 >> (x & 7);
	set_g_mask(mask);
	switch ( color )
	   {
	case 0: rwdot(fbptr, 0); break;
	case 0xf: rwdot(fbptr, -1); break;
	default:
		rwdot(fbptr, 0);
		set_s_map(color);
		rwdot(fbptr, -1);
		set_s_map(0xf);
	   }
}

/* Fill a rectangle specified by pointer into frame buffer, */
/* starting bit within byte, width, and height. */
/* Note that only a subset of the maps may be selected: */
/* this is up to the caller. */
/* Smashes rop->dest. */
private void
fill_rectangle(register rop_params *rop, int bit, int w)
  /* rop: dest, draster, height, data */
{	static byte rmask_tab[9] =
	   {	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
	   };
	if ( bit + w <= 8 )
	   {	/* Less than one byte */
		int mask = rmask_tab[w] >> bit;
		set_g_mask(mask);
		memsetcol(rop);
	   }
	else
	   {	int byte_count, right_mask;
		if ( bit )
		   {	int mask = 0xff >> bit;
			set_g_mask(mask);
			memsetcol(rop);
			rop->dest++;
			w += bit - 8;
		   }
		if ( (byte_count = w >> 3) != 0 )
		   {	set_g_mask(0xff);	/* all bits */
			rop->width = byte_count;
			memsetrect(rop);
			rop->dest += byte_count;
		   }
		if ( (right_mask = rmask_tab[w & 7]) != 0 )
		   {	set_g_mask(right_mask);
			memsetcol(rop);
		   }
	   }
}

/* Copy from a monochrome bitmap into the frame buffer. */
/* Caller has set plane select and function, if desired. */
/* Smashes rop->dest and rop->src. */
private void
copy_rectangle(register rop_params *rop, int sleft, int dleft,
  byte mask, byte rmask, int w)
  /* rop: dest, draster, src, sraster, height, invert */
{	int count = w - dleft;
	int skew = rop->shift = (sleft - dleft) & 7;
	if ( skew == 0 )		/* optimize the aligned case */
	   {	/* Do left column */
		if ( mask )
		   {	set_g_mask(mask);
			memrwcol(rop);
		   }
		rop->src++, rop->dest++;
		/* Do center */
		while ( (count -= 8) >= 0 )
		   {	set_g_mask(0xff);
			memrwcol(rop);
			rop->src++, rop->dest++;
		   }
		/* Do right column */
		if ( rmask )
		   {	set_g_mask(rmask);
			memrwcol(rop);
		   }
	   }
	else
	   {	/* Do left column */
		if ( mask )
		   {	set_g_mask(mask);
			if ( sleft >= dleft || w <= sleft )
			   {	/* Source fits in one byte */
				memrwcol(rop);
			   }
			else
			   {	memrwcol2(rop);
			   }
		   }
		if ( sleft < dleft ) rop->src++;
		rop->dest++;
		/* Do center */
		while ( (count -= 8) >= 0 )
		   {	set_g_mask(0xff);
			memrwcol2(rop);
			rop->src++, rop->dest++;
		   }
		/* Do right column */
		if ( rmask )
		   {	set_g_mask(rmask);
			if ( count + 8 <= skew )
				memrwcol(rop);
			else
				memrwcol2(rop);
		   }
	   }
}