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

⟦406ac8e83⟧ TextFile

    Length: 13000 (0x32c8)
    Types: TextFile
    Names: »gsdevice.c«

Derivation

└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89
    └─⟦ff23ba0e6⟧ »./ghostscript-1.3.tar.Z« 
        └─⟦a24a58cd3⟧ 
            └─⟦this⟧ »gsdevice.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.  */

/* gsdevice.c */
/* Device operators for GhostScript library */
#include <math.h>			/* for fabs */
#include "gx.h"
#include "gserrors.h"
#include "gxfixed.h"			/* ditto */
#include "gxmatrix.h"			/* for gzstate.h */
#include "gxbitmap.h"
#include "gzstate.h"
#include "gzdevice.h"

/* Device definitions */
int gx_default_open_device(P1(gx_device *));
int gx_default_close_device(P1(gx_device *));
int gx_default_sync_output(P1(gx_device *));
gx_color_index gx_default_map_rgb_color(P4(gx_device *, ushort, ushort, ushort));
int gx_default_map_color_rgb(P3(gx_device *, gx_color_index, ushort *));
/* Following defines the null device */
private int null_fill_rectangle(P6(gx_device *, int, int, int, int, gx_color_index));
private int null_tile_rectangle(P8(gx_device *, gx_bitmap *, int, int, int, int, gx_color_index, gx_color_index));
private int null_copy_mono(P10(gx_device *, byte *, int, int, int, int, int, int, gx_color_index, gx_color_index));
private int null_copy_color(P8(gx_device *, byte *, int, int, int, int, int, int));
private int null_draw_line(P6(gx_device *, int, int, int, int, gx_color_index));
private int null_fill_trapezoid(P8(gx_device *, int, int, int, int, int, int, gx_color_index));
private int null_tile_trapezoid(P10(gx_device *, gx_bitmap *, int, int, int, int, int, int, gx_color_index, gx_color_index));
private gx_device_procs null_procs = {
	gx_default_open_device,
	gx_default_close_device,
	gx_default_map_rgb_color,
	gx_default_map_color_rgb,
	gx_default_sync_output,
	null_fill_rectangle,
	null_tile_rectangle,
	null_copy_mono,
	null_copy_color,
	null_draw_line,
	null_fill_trapezoid,
	null_tile_trapezoid
};
private gx_device null_device = {
	sizeof(device),
	&null_procs,
	identity_matrix_body,
	0x3fff, 0x3fff,			/* 0, 0, */
	0, 1, 1, 1, 1
};

/* The default (only) frame device. */
/* External initialization code must set this up. */
gx_device *gx_device_default_p = &null_device;
/* The null device */
gx_device *gx_device_null_p = &null_device;

/* Synchronize the device with the accumulated page description */
int
gs_copypage(gs_state *pgs)
{	gx_device *dev = pgs->device->info;
	return (*dev->procs->sync_output)(dev);
}

/* Make a device by cloning an existing one */
int
gs_makedevice(gx_device **pnew_dev, gx_device *dev, gs_matrix *pmat,
  uint width, uint height, gs_proc_alloc palloc)
{	register gx_device *new_dev;
	new_dev = (gx_device *)(*palloc)(dev->params_size, "gs_makedevice");
	if ( new_dev == 0 ) return_error(gs_error_VMerror);
	if ( width <= 0 || height <= 0 ) return_error(gs_error_rangecheck);
	*new_dev = *dev;
	new_dev->initial_matrix = *pmat;
	new_dev->width = width;
	new_dev->height = height;
	new_dev->is_open = 0;
	*pnew_dev = new_dev;
	return 0;
}

/* Set the device in the graphics state */
int
gs_setdevice(gs_state *pgs, gx_device *dev)
{	register device *pdev = pgs->device;
	gs_matrix *pmat = &dev->initial_matrix;
	/* Initialize the device */
	if ( !dev->is_open )
	   {	int code = (*dev->procs->open_device)(dev);
		if ( code < 0 ) return code;
		dev->is_open = 1;
	   }
	/* Compute device white and black codes */
	pdev->black = (*dev->procs->map_rgb_color)(dev, 0, 0, 0);
	pdev->white = (*dev->procs->map_rgb_color)(dev, dev->max_rgb_value, dev->max_rgb_value, dev->max_rgb_value);
	pdev->info = dev;
	return 0;
}

/* Select the null device.  This is just a convenience. */
void
gs_nulldevice(gs_state *pgs)
{	gs_setdevice(pgs, gx_device_null_p);
}

/* Get the current device from the graphics state */
gx_device *
gs_currentdevice(gs_state *pgs)
{	return pgs->device->info;
}

/* Read out the current device parameters */
void
gs_deviceparams(gx_device *dev, gs_matrix *pmat, int *pwidth, int *pheight)
{	*pmat = dev->initial_matrix;
	*pwidth = dev->width;
	*pheight = dev->height;
}

/* Install enough of a null device to suppress graphics output */
/* during the execution of stringwidth. */
void
gx_device_no_output(gs_state *pgs)
{	pgs->device->info = &null_device;
}

/* Dummy device procedures */
private int
null_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  gx_color_index color)
{	return 0;
}
private int
null_tile_rectangle(gx_device *dev, gx_bitmap *tile,
  int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
{	return 0;
}
private int
null_copy_mono(gx_device *dev, byte *data,
  int dx, int raster, int x, int y, int w, int h,
  gx_color_index zero, gx_color_index one)
{	return 0;
}
private int
null_copy_color(gx_device *dev, byte *data,
  int dx, int raster, int x, int y, int w, int h)
{	return 0;
}
private int
null_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  gx_color_index color)
{	return 0;
}
private int
null_fill_trapezoid(gx_device *dev,
  int x0, int y0, int w0, int x1, int y1, int w1, gx_color_index color)
{	return 0;
}
private int
null_tile_trapezoid(gx_device *dev, gx_bitmap *tile,
  int x0, int y0, int w0, int x1, int y1, int w1,
  gx_color_index color0, gx_color_index color1)
{	return 0;
}
/* Default (no-op) device open, close, map, and sync procedures */
int
gx_default_open_device(gx_device *dev)
{	return 0;
}
int
gx_default_close_device(gx_device *dev)
{	return 0;
}
gx_color_index
gx_default_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
{	return (gx_color_index)max(max(r, g), b);
}
int
gx_default_map_color_rgb(gx_device *dev, gx_color_index color, ushort *prgb)
{	prgb[0] = prgb[1] = prgb[2] = (ushort)color;
	return 0;
}
int
gx_default_sync_output(gx_device *dev)
{	return 0;
}

#ifdef DEBUG

/* ------ Tracing 'device' ------*/

/* To avoid unpleasant interactions with makedevice, */
/* the tracing 'device' uses an external linked list to keep track of */
/* the real procedures that were replaced in the procedure vector. */

typedef struct trace_record_s trace_record;
struct trace_record_s {
	trace_record *next;
	gx_device_procs *tprocs;
	gx_device_procs procs;
};

private trace_record *trace_list = NULL;

#define rprocs (trace_find_procs(dev->procs))

/* Procedure structure */
int trace_open_device(P1(gx_device *));
int trace_close_device(P1(gx_device *));
int trace_sync_output(P1(gx_device *));
gx_color_index trace_map_rgb_color(P4(gx_device *, ushort, ushort, ushort));
int trace_map_color_rgb(P3(gx_device *, gx_color_index, ushort *));
int trace_fill_rectangle(P6(gx_device *, int, int, int, int, gx_color_index));
int trace_tile_rectangle(P8(gx_device *, gx_bitmap *, int, int, int, int, gx_color_index, gx_color_index));
int trace_copy_mono(P10(gx_device *, byte *, int, int, int, int, int, int, gx_color_index, gx_color_index));
int trace_copy_color(P8(gx_device *, byte *, int, int, int, int, int, int));
int trace_draw_line(P6(gx_device *, int, int, int, int, gx_color_index));
int trace_fill_trapezoid(P8(gx_device *, int, int, int, int, int, int, gx_color_index));
int trace_tile_trapezoid(P10(gx_device *, gx_bitmap *, int, int, int, int, int, int, gx_color_index, gx_color_index));
private gx_device_procs trace_procs = {
	trace_open_device,
	trace_close_device,
	trace_map_rgb_color,
	trace_map_color_rgb,
	trace_sync_output,
	trace_fill_rectangle,
	trace_tile_rectangle,
	trace_copy_mono,
	trace_copy_color,
	trace_draw_line,
	trace_fill_trapezoid,
	trace_tile_trapezoid
};

/* Find the real procedures for a traced device */
private gx_device_procs *
trace_find_procs(gx_device_procs *tprocs)
{	register trace_record *tp = trace_list;
	while ( tp != NULL )
	   {	if ( tp->tprocs == tprocs ) return &tp->procs;
		tp = tp->next;
	   }
	fprintf(stderr, "Traced procedures not found!\n");
	gs_exit(1);
}

/* Trace a device. */
gx_device *
gs_trace_device(gx_device *rdev)
{	trace_record *tp;
	if ( rdev->procs->open_device == trace_procs.open_device )
		return rdev;		/* already traced */
	tp = (trace_record *)gs_malloc(sizeof(trace_record), 
				       "gs_trace_device");
	if ( tp == 0 ) return 0;
	tp->next = trace_list;
	tp->tprocs = rdev->procs;
	tp->procs = *rdev->procs;
	trace_list = tp;
	*rdev->procs = trace_procs;
	return rdev;
}

/* Utilities */
private int
print_code(int result)
{
if ( gs_debug['v'] )
  {	if ( result == 0 )
		printf(";\n");
	else
		printf(";	/* = %d */\n", result);
  }
	return result;
}

/* Procedures */
private int
trace_open_device(gx_device *dev)
{	int result = (*rprocs->open_device)(dev);
if ( gs_debug['v'] )
	printf("[v]\topen_device(dev)");
	return print_code(result);
}
private int
trace_close_device(gx_device *dev)
{	int result = (*rprocs->close_device)(dev);
if ( gs_debug['v'] )
	printf("[v]\tclose_device(dev)");
	return print_code(result);
}
private gx_color_index
trace_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
{	gx_color_index result = (*rprocs->map_rgb_color)(dev, r, g, b);
if ( gs_debug['v'] )
	printf("[v]\tmap_rgb_color(dev, %u, %u, %u) /*= %ld */;\n", r, g, b, (long)result);
	return result;
}
private int
trace_map_color_rgb(gx_device *dev, gx_color_index color, ushort *prgb)
{	int result = (*rprocs->map_color_rgb)(dev, color, prgb);
if ( gs_debug['v'] )
	printf("\t{ ushort rgb[3]; T(map_color_rgb)(dev, %ld, rgb /* %u, %u, %u */); }",
		(long)color, prgb[0], prgb[1], prgb[2]);
	return print_code(result);
}
private int
trace_sync_output(gx_device *dev)
{	int result = (*rprocs->sync_output)(dev);
if ( gs_debug['v'] )
	printf("[v]\tsync_output(dev)");
	return print_code(result);
}
private int
trace_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  gx_color_index color)
{	int result = (*rprocs->fill_rectangle)(dev, x, y, w, h, color);
if ( gs_debug['v'] )
	printf("[v]\tfill_rectangle(dev, %d, %d, %d, %d, %ld)", x, y, w, h, (long)color);
	return print_code(result);
}
private int
trace_tile_rectangle(gx_device *dev, gx_bitmap *tile,
  int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
{	int result = (*rprocs->tile_rectangle)(dev, tile,
		x, y, w, h, zero, one);
if ( gs_debug['v'] )
   {	int i;
	printf("\t{ static byte data = { 0x%x", tile->data[0]);
	for ( i = 1; i < tile->raster * tile->height; i++ )
		printf(", 0x%x", tile->data[i]);
	printf(" };\n\t  static gx_bitmap tile = { &data, %d, %d, %d };\n",
		tile->raster, tile->width, tile->height);
	printf("\t  T(tile_rectangle)(dev, &tile, %d, %d, %d, %d, %ld, %ld);\n\t}",
		x, y, w, h, (long)zero, (long)one);
   }
	return print_code(result);
}
private int
trace_copy_mono(gx_device *dev, byte *data,
  int dx, int raster, int x, int y, int w, int h,
  gx_color_index zero, gx_color_index one)
{	int result = (*rprocs->copy_mono)(dev, data,
		dx, raster, x, y, w, h, zero, one);
if ( gs_debug['v'] )
	printf("[v]\tcopy_mono(dev, data, %d, %d, %d, %d, %d, %d, %ld, %ld)",
		dx, raster, x, y, w, h, (long)zero, (long)one);
	return print_code(result);
}
private int
trace_copy_color(gx_device *dev, byte *data,
  int dx, int raster, int x, int y, int w, int h)
{	int result = (*rprocs->copy_color)(dev, data,
		dx, raster, x, y, w, h);
if ( gs_debug['v'] )
	printf("[v]\tcopy_color(dev, data, %d, %d, %d, %d, %d, %d)",
		dx, raster, x, y, w, h);
	return print_code(result);
}
private int
trace_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  gx_color_index color)
{	int result = (*rprocs->draw_line)(dev, x0, y0, x1, y1, color);
if ( gs_debug['v'] )
	printf("[v]\tdraw_line(dev, %d, %d, %d, %d, %ld)",
		x0, y0, x1, y1, (long)color);
	return print_code(result);
}
private int
trace_fill_trapezoid(gx_device *dev,
  int x0, int y0, int w0, int x1, int y1, int w1, gx_color_index color)
{	int result = (*rprocs->fill_trapezoid)(dev,
		x0, y0, w0, x1, y1, w1, color);
if ( gs_debug['v'] )
	printf("[v]\tfill_trapezoid(dev, %d, %d, %d, %d, %d, %d, %ld)",
		x0, y0, w0, x1, y1, w1, (long)color);
	return print_code(result);
}
private int
trace_tile_trapezoid(gx_device *dev, gx_bitmap *tile,
  int x0, int y0, int w0, int x1, int y1, int w1,
  gx_color_index color0, gx_color_index color1)
{	int result = (*rprocs->tile_trapezoid)(dev, tile,
		x0, y0, w0, x1, y1, w1, color0, color1);
if ( gs_debug['v'] )
	printf("[v]\ttile_trapezoid(dev, %d, %d, %d, %d, %d, %d, %ld, %ld)",
		x0, y0, w0, x1, y1, w1, (long)color0, (long)color1);
	return print_code(result);
}

#endif