Category : C Source Code
Archive   : GHSTSCPT.ZIP
Filename : GDEVMEM.C

 
Output of file : GDEVMEM.C contained in archive : GHSTSCPT.ZIP
/* 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. */

/* gdevmem.c */
/* "Memory" (stored bitmap) device for GhostScript library. */
#include "gx.h" /* should be std.h, but needs gx_bitmap */
#include "gxdevice.h"

/* Procedures */

int mem_copy_mono(P10(gx_device *, byte *, int, int, int, int, int, int, int, int));

int mem_copy_color(P9(gx_device *, byte *, int, int, int, int, int, int, int));

int mem_fill_rectangle(P6(gx_device *, int, int, int, int, int));

/* The device descriptor */
static gx_device_procs mem_procs = {
gx_default_open_device,
gx_default_close_device,
gx_default_map_rgb_color,
gx_default_map_color_rgb,
gx_default_sync_output,
mem_fill_rectangle,
gx_default_tile_rectangle,
mem_copy_mono,
mem_copy_color,
gx_default_draw_line,
gx_default_fill_trapezoid,
gx_default_tile_trapezoid
};
/* The instance is public. */
gx_device_memory mem_device = {
sizeof(gx_device_memory),
&mem_procs,
0, 0, /* x and y extent (filled in) */
0, 1, 1, /* monochrome */
1, /* bit-big-endian */
0, /* raster (filled in) */
0 /* base (filled in) */
};
/* Macro for casting gx_device argument */
#define map ((gx_device_memory *)dev)

/* Macro to unpack and validate arguments */
#define unpack_rect(dest,dest_raster)\
if ( w <= 0 || h <= 0 ) return 0;\
if ( x < 0 || x > map->width - w || y < 0 || y > map->height - h )\
return -1;\
dest_raster = map->raster;\
dest = map->base + y * dest_raster + (x >> 3)

/* Fill a rectangle with a color. */
int
mem_fill_rectangle(gx_device *dev, int x, int y, int w, int h, int color)
{ int bit;
byte right_mask;
byte fill;
byte *dest;
int dest_raster;
unpack_rect(dest, dest_raster);
#define write_loop(stat)\
{ int line_count = h;\
byte *ptr = dest;\
do { stat; ptr += dest_raster; } while ( --line_count );\
}
#define write_partial(msk)\
if ( fill ) write_loop(*ptr |= msk)\
else write_loop(*ptr &= ~msk)
switch ( color )
{
case 0: fill = 0; break;
case 1: fill = -1; break;
case -1: return 0; /* transparent */
default: return -1; /* invalid */
}
bit = x & 7;
if ( bit + w <= 8 )
{ /* Only one word */
right_mask = ((0xff00 >> w) & 0xff) >> bit;
}
else
{ int byte_count;
if ( bit )
{ byte mask = 0xff >> bit;
write_partial(mask);
dest++;
w += bit - 8;
}
right_mask = (0xff00 >> (w & 7)) & 0xff;
if ( (byte_count = w >> 3) != 0 )
{ write_loop(memset(ptr, fill, byte_count));
dest += byte_count;
}
}
if ( right_mask )
write_partial(right_mask);
return 0;
}

/* Copy a monochrome bitmap. */
static byte bit_color0, bit_color1;
int
mem_copy_mono(gx_device *dev, byte *base, int sourcex, int raster,
int x, int y, int w, int h, int zero, int one)
{ byte *line;
int sleft, dleft;
int mask, rmask;
int invert, zmask, omask;
byte *dest;
int dest_raster;
if ( one == zero ) /* vacuous case */
return mem_fill_rectangle(dev, x, y, w, h, zero);
unpack_rect(dest, dest_raster);
line = base + (sourcex >> 3);
sleft = 8 - (sourcex & 7);
dleft = 8 - (x & 7);
mask = 0xff >> (8 - dleft);
if ( w < dleft )
mask -= mask >> w;
else
rmask = 0xff00 >> ((w - dleft) & 7);
/* Macros for writing partial bytes. */
/* bits has already been inverted by xor'ing with invert. */
#define write_byte_masked(ptr, bits, mask)\
*ptr = ((bits | ~mask | zmask) & *ptr | (bits & mask & omask))
#define write_byte(ptr, bits)\
*ptr = ((bits | zmask) & *ptr | (bits & omask))
invert = (zero == 1 || one == 0 ? -1 : 0);
zmask = (zero == 0 || one == 0 ? 0 : -1);
omask = (zero == 1 || one == 1 ? -1 : 0);
if ( sleft == dleft ) /* optimize the aligned case */
{ w -= dleft;
while ( --h >= 0 )
{ byte *bptr = line;
int count = w;
byte *optr = dest;
register int bits = *bptr ^ invert; /* first partial byte */
write_byte_masked(optr, bits, mask);
/* Do full bytes. */
while ( (count -= 8) >= 0 )
{ bits = *++bptr ^ invert;
write_byte(++optr, bits);
}
/* Do last byte */
if ( count & 7 )
{ bits = *++bptr ^ invert;
write_byte_masked(++optr, bits, rmask);
}
dest += dest_raster;
line += raster;
}
}
else
{ int skew = (sleft - dleft) & 7;
int cskew = 8 - skew;
while ( --h >= 0 )
{ byte *bptr = line;
int count = w;
byte *optr = dest;
register int bits;
/* Do the first partial byte */
if ( sleft >= dleft )
{ bits = *bptr >> skew;
}
else /* ( sleft < dleft ) */
{ bits = *bptr++ << cskew;
if ( count > sleft )
bits += *bptr >> skew;
}
bits ^= invert;
write_byte_masked(optr, bits, mask);
count -= dleft;
optr++;
/* Do full bytes. */
while ( count >= 8 )
{ bits = *bptr++ << cskew;
bits += *bptr >> skew;
bits ^= invert;
write_byte(optr, bits);
count -= 8;
optr++;
}
/* Do last byte */
if ( count > 0 )
{ bits = *bptr++ << cskew;
if ( count > skew ) bits += *bptr >> skew;
bits ^= invert;
write_byte_masked(optr, bits, rmask);
}
dest += dest_raster;
line += raster;
}
}
return 0;
}

/* Copy a "color" bitmap. Since "color" is the same as monochrome, */
/* this just reduces to copying a monochrome bitmap. */
int
mem_copy_color(gx_device *dev, byte *base, int sourcex, int raster,
int x, int y, int w, int h, int color_transparent)
{ int zero = 0, one = 1;
switch ( color_transparent )
{
case 0: zero = -1; break;
case 1: one = -1; break;
}
return mem_copy_mono(dev, base, sourcex, raster, x, y, w, h, zero, one);
}


  3 Responses to “Category : C Source Code
Archive   : GHSTSCPT.ZIP
Filename : GDEVMEM.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/