jerq layers code (Part 3 of 4)

S.Kenyon sk at ukc.UUCP
Wed Apr 10 20:41:37 AEST 1985


#!/bin/sh
echo 'Start of jerq layers code, part 03 of 04:'
echo 'x - README'
sed 's/^X//' > README << '/'
X
X                              Jerq
X                               by
X                          Simon Kenyon
X                            (c) 1985
X
XThis is a collection of files that tries to implement the layers code
Xdescribed by Rob Pike in "Graphics in overlapping bitmap layers",
XACM Transactions on Graphics, 2 135 (1983). This original paper has
Xbeen augmented by two others, "Hardware/Software Trade-offs for Bitmap
XGraphics on the Blit", Software - Practice and Experience, Vol 15(2)
X131-151 (Feb 1985) and "The Blit: a multiplexed graphics terminal",
XBSTJ 63 No 8 Part 2 1607 1631 (October 1984).
XI have the first two, but have only briefly read the last.
X
XAnyway, this code might be of use to somebody, so here it is.
XThe line drawing as described in the first paper is not included as
XI don't have the clipping working. In it's place is a generalised line
Xdrawing algorithm taken from the 1st Smalltalk-80 book, as is the
Xbitblt code.
X
XSend any problem, suggestions or enhancements to me and I will try
Xand deal with them as and when I can, but as this is a part-time
Xactivity, don't hold your breath.
X
XThis is in the public domain, Rob having given his permission for me
Xto publish this. I have had no access whatsoever to the Blit hardware
Xor software, which I'm sure is pretty obvious, so there are no licencing
Xhassles in me distributing this code. As a matter of fact it was debugged
Xon a Printronix 300 printer/plotter. If you use it, it would be nice if
Xyou left my name in the code.
X
XSimon Kenyon
X ...!mcvax!simon
X ...!ukc!sk
X
XWork:                                         Home:
X
XPrime Computer (Ireland) Limited
XClonshaugh Industrial Estate                  10 Castle Avenue
XCoolock                                       Clontarf
XDublin 5                                      Dublin 3
XIreland                                       Ireland
X
X+353 1 477888 x 319                           +353 1 331497
/
echo 'x - h/layers.h'
sed 's/^X//' > h/layers.h << '/'
X
X/*
X *  File:        layers.h
X *
X *  Sccs-id:     @(#)layers.h  1.1  85/03/24
X *
X *  Description: This file contains all the defined constants and type
X *               declarations that are used in the jerq routines.
X *
X *  Author:      Simon Kenyon.
X *
X *  History:     SCK  1.1  85/03/24  Created by amalgamating all the
X *                                   other include files into one.
X */
X
X#ifndef NULL
X#define NULL 0
X#endif NULL
X
X#define FALSE 0
X#define TRUE  1
X
X#define boolean int
X
X#define WORDSIZE 16                    /* size of words used in bit map */
X#define BYTESIZE 8
X
X#define MAXLINE  128
X
X#define CLR   1
X#define OR    2
X#define XOR   3
X#define STORE 4
X
X#define ALL_ZEROS  0
X#define S_AND_D    1
X#define S_AND_ND   2
X#define S          3
X#define NS_AND_D   4
X#define D          5
X#define S_XOR_D    6
X#define S_OR_D     7
X#define NS_AND_ND  8
X#define NS_XOR_D   9
X#define ND        10
X#define S_OR_ND   11
X#define NS        12
X#define NS_OR_D   13
X#define NS_OR_ND  14
X#define ALL_ONES  15
X
Xstruct Point {
X    int     x;                         /* x and y coordinates of point */
X    int     y;
X};
X
Xstruct Rectangle {
X    struct Point    origin;            /* min x,y */
X    struct Point    corner;            /* max x,y */
X};
X
Xstruct Bitmap {
X    unsigned short *base;              /* start of data */
X    int     width;                     /* width in words */
X    struct Rectangle    rect;          /* image rectangle */
X    struct Obscured *obs;              /* linked list of obscured rectangles,
X                                          for compatability with Layer */
X    struct Obscured *endobs;           /* end of above linked list */
X};
X
Xstruct Layer {
X    unsigned short *base;              /* start of data */
X    int     width;                     /* width in words */
X    struct Rectangle    rect;          /* image rectangle */
X    struct Obscured *obs;              /* linked list of obscured rectangles */
X    struct Obscured *endobs;           /* end of above linked list */
X    struct Layer   *front;             /* adjacent layer in front */
X    struct Layer   *back;              /* adjacent layer behind */
X};
X
Xstruct Obscured {
X    struct Layer   *lobs;              /* frontmost obscuring layer */
X    struct Bitmap  *bmap;              /* where the obscured data resides */
X    struct Rectangle    rect;          /* image rectangle */
X    struct Obscured *next;             /* next bitmap in obscured list */
X    struct Obscured *prev;             /* previous bitmap in obscured list */
X};
X
Xstruct ListElement {
X    struct Rectangle    rect;
X    struct Bitmap  *bp;
X    struct ListElement *next;
X};
/
echo 'x - man/jerq.3x'
sed 's/^X//' > man/jerq.3x << '/'
X
X.TH JERQ 3X 85/03/26 "PR1ME Computer"
X.SH NAME
Xjerq \- routines to handle "overlapping asynchronous windows" ala Rob
X.SH SYNOPSIS
X.nf
X.B background (rect)
X.B struct Rectangle rect;
X.PP
X.B dellayer (lp)
X.B struct Layer *lp;
X.PP
X.B layers ()
X.PP
X.B lbitblt (sl, rect, dl, pt, hb, f)
X.B struct Layer *sl;
X.B struct Rectangle rect;
X.B struct Layer *dl;
X.B struct Point *pt;
X.B struct Bitmap *hb;
X.B int f;
X.PP
X.B lblt (l, sb, r, hb, pt, f)
X.B struct Layer *l;
X.B struct Bitmap *sb;
X.B struct Rectangle r;
X.B struct Bitmap *hb;
X.B struct Point pt;
X.B int f;
X.PP
X.B lline (lp, p0, p1, f)
X.B struct Layer *lp;
X.B struct Point p0;
X.B struct Point p1;
X.B int f;
X.PP
X.B struct Layer newlayer (bp, r, hb)
X.B struct Bitmap *bp;
X.B struct Rectangle r;
X.B struct Bitmap *hb;
X.PP
X.B plot (sb, r, size)
X.B struct Bitmap *sb;
X.B struct Rectangle r;
X.B int size;
X.PP
X.B upfront (lp)
X.B struct Layer *lp;
X.fi
X.SH DESCRIPTION
X.I Background
Xfills
X.I rect
Xwith the background colour.
X.PP
X.I Dellayer
Xdeletes layer
X.I lp
Xfrom the display and fills it in with the background colour.
X.PP
X.I Layers
Xinitialises the layers package,
Xcreating the display bitmap and
Xfilling it in with the background colour.
X.PP
X.I Lbitblt
Xperforms a bitblt operation from the source rectangle
X.I rect
Xin layer
X.I sl
Xto the corresponding rectangle with origin
X.I pt
Xin the destination layer
X.IR dl .
XThe bitmap
X.I hb
Xis
Xan optional halftone pattern.
X.PP
X.IR Lblt ,
Xgiven a layer
X.IR l ,
Xa bitmap
X.IR sb ,
Xa rectangle
X.I r
Xand a function code
X.IR f ,
Xcopies the off-screen bitmap
X.I sb
Xto a rectangle
X.I r
Xwithin layer
X.IR l .
X.PP
X.I Lline
Xdraws a line from
X.I p0
Xto
X.I p1
Xin a layer
X.IR lp ,
Xusing code
X.IR f .
X.PP
X.I Newlayer
Xcreates a new layer in rectangle
X.I r
Xof bitmap
X.I bp
Xand eithers fill the layer with optional halftone
X.I hb
Xor clears it.
XIt returns a pointer to the new layer.
X.PP
X.IR Plot ,
Xgiven a bitmap
X.IR sb ,
Xa rectangle
X.I r
Xand a scale factor
X.I size
Xcreates a plot file suitable for the Printronix.
X.PP
X.I Upfront
Xpulls layer
X.I lp
Xto the front of the screen,
Xso that it is obscured by no other layer.
X.SH SEE ALSO
Xjerq(5)
X.SH AUTHOR
XSimon Kenyon
X.SH BUGS
XIn
X.I background
Xthe bitmap and the background colour to fill it with
Xare hard-coded into the routine.
X.PP
XIn
X.I layers
Xthe display bitmap and the background colour
Xare hard-coded into the routine.
X.PP
XThe code in
X.I lline
Xis nowhere as efficient as the restartable dda
Xdescribed in Rob's original paper,
Xbut I was having so much hassle with the clipping in that,
Xand I wanted to ship this...
X.PP
XIn
X.I plot
Xthe output is sent to
X.IR stdout .
/
echo 'x - plot/reformat.c'
sed 's/^X//' > plot/reformat.c << '/'
X
X/*
X *  File:        reformat.c
X *
X *  Sccs-id:     @(#)reformat.c  1.4  85/03/24
X *
X *  Description: This file contains one routine reformat which
X *               diddles the bits in a plot line so that the
X *               Printronix printer/plotter understands them.
X *
X *  Author:      Simon Kenyon.
X *
X *  History:     SCK  1.1  83/10/03  Created.
X *               SCK  1.2  84/11/29  Made it work.
X *               SCK  1.3  85/03/04  Tidied up for release.
X *               SCK  1.4  85/03/24  Changed the include files around.
X */
X
X#include "../h/layers.h"
X
Xextern int  bitptr;
Xextern unsigned short   line[];
Xextern unsigned short   wordmask[];
X
Xunsigned short  bytemask[] = {
X    0x80, 0x40, 0x20, 0x10,
X    0x08, 0x04, 0x02, 0x01
X};
Xunsigned char   outlin[1024];
Xint     outptr;
X
X/*
X *  Name:        reformat
X *
X *  Description: Diddle the bits in a plot line so that the
X *               Printronix printer/plotter understands them.
X *
X *  Synopsis:    reformat ()
X *
X *  Globals:     bitptr    (r)
X *               line      (r)
X *               wordmask  (r)
X *               bytemask  (r)
X *               outlin    (w)
X *               outptr    (r/w)
X *
X *  Calls:       Nothing.
X *
X *  Called by:   plot  (plot.c)
X */
Xreformat ()
X{
X    int     i;
X
X /*
X  * note that because this code runs under PRIMOS
X  * the top bit of each byte is being set
X  */
X    outlin[0] = 0x85;                  /* that's what the man said */
X    outptr = 15;
X    for (i = 0; i < bitptr; i++) {
X        if (line[i / WORDSIZE] & wordmask[i % WORDSIZE])
X            outlin[outptr / BYTESIZE] |= bytemask[outptr % BYTESIZE];
X        else
X            outlin[outptr / BYTESIZE] &= ~bytemask[outptr % BYTESIZE];
X        if ((outptr % BYTESIZE) == 2) {
X            outlin[outptr / BYTESIZE] |= 0xC0;
X            outptr += 13;              /* left as an exercise to the reader */
X        }
X        else
X            outptr--;
X    }
X}
/
echo 'x - src/addrect.c'
sed 's/^X//' > src/addrect.c << '/'
X
X/*
X *  File:        addrect.c
X *
X *  Sccs-id:     @(#)addrect.c  1.4  85/03/24
X *
X *  Description: This file contains one routine addrect which
X *               adds rectangle r to the obscured list of new layer lp
X *               iff it is unique.
X *
X *  Author:      Simon Kenyon.
X *
X *  History:     SCK  1.1  83/10/03  Created.
X *               SCK  1.2  84/11/29  Made it work.
X *               SCK  1.3  85/03/01  Tidied up for release.
X *               SCK  1.4  85/03/24  Changed the include files around.
X */
X
X#include "../h/layers.h"
X
Xextern struct Obscured *obs;
Xextern struct Obscured *endobs;
X
X/*
X *  Name:        addrect
X *
X *  Description: Add rectangle r to the obscured list of new layer lp
X *               iff it is unique. (no spelling mistake)
X *
X *  Synopsis:    addrect (r, lp)
X *               struct Rectangle    r;
X *               struct Layer   *lp;
X *
X *  Globals:     obs     (r/w)
X *               endobs  (r/w)
X *
X *  Calls:       malloc  (libc)
X *
X *  Called by:   addpiece  (addpiece.c)
X *               addobs    (addobs.c)
X */
Xaddrect (r, lp)
Xstruct Rectangle    r;
Xstruct Layer   *lp;                    /* layer currently occupying rectangle r
X                                          on screen */
X{
X    struct Obscured *op;
X    struct Obscured *newop;
X
X    char   *malloc ();
X
X    for (op = obs; op != NULL; op = op -> next)/* op = each element of obs */
X        if ((op -> rect.origin.x == r.origin.x) &&
X                (op -> rect.origin.y == r.origin.y))
X            return;                    /* not unique */
X /*
X  * newop = new struct Obscured
X  */
X    newop = (struct Obscured   *) malloc ((unsigned) sizeof (struct Obscured));
X    newop -> rect = r;
X    newop -> lobs = lp;
X    newop -> bmap = NULL;
X /*
X  * link newop into end of obs list
X  */
X    if (endobs != NULL)
X        endobs -> next = newop;
X    else
X        obs = newop;
X    newop -> prev = endobs;
X    newop -> next = NULL;
X    endobs = newop;
X}
/
echo 'x - src/balloc.c'
sed 's/^X//' > src/balloc.c << '/'
X
X/*
X *  File:        balloc.c
X *
X *  Sccs-id:     @(#)balloc.c  1.4  85/03/24
X *
X *  Description: This file contains the one function balloc which
X *               allocates a bitmap and return a pointer to it,
X *               using the rectangle r, which is the on-screen
X *               rectangle that corresponds to the bitmap image.
X *
X *  Author:      Simon Kenyon.
X *
X *  History:     SCK  1.1  83/10/03  Created.
X *               SCK  1.2  84/11/29  Made it work.
X *               SCK  1.3  85/03/04  Tidied up for release.
X *               SCK  1.4  85/03/24  Changed the include files around.
X */
X
X#include "../h/layers.h"
X
X/*
X *  Name:        balloc
X *
X *  Description: Allocate a bitmap and return a pointer to it,
X *               using the rectangle r, which is the on-screen
X *               rectangle that corresponds to the bitmap image.
X *
X *  Synopsis:    struct Bitmap *balloc (r)
X *               struct Rectangle   r;
X *
X *  Globals:     None.
X *
X *  Calls:       malloc  (libc)
X *
X *  Called by:   newlayer  (newlayer.c)
X *               addobs    (addobs.c)
X */
Xstruct Bitmap  *balloc (r)
Xstruct Rectangle    r;
X{
X    struct Bitmap  *b;
X    int     h;
X    int     w;
X    int     startBits;
X    int     nWords;
X    int     size;
X
X    char   *malloc ();
X
X /*
X  * allocate storage for a bitmap and return a pointer to it
X  */
X    w = r.corner.x - r.origin.x;
X    h = r.corner.y - r.origin.y;
X    startBits = WORDSIZE - (r.origin.x % WORDSIZE);
X    if (w <= startBits)
X        nWords = 1;
X    else
X        nWords = (w - startBits - 1) / WORDSIZE + 2;
X    size = nWords * h + 1;             /* the extra word is because of the way
X                                          my implementation of bitblt works */
X    b = (struct Bitmap *) malloc ((unsigned) sizeof (struct Bitmap));
X    b -> base = (unsigned short *) malloc ((unsigned) (size * 2));
X    b -> width = nWords;
X    b -> rect = r;
X    b -> obs = NULL;                   /* not used in a bitmap */
X    return (b);
X}
/
echo 'x - src/dellayer.c'
sed 's/^X//' > src/dellayer.c << '/'
X
X/*
X *  File:        dellayer.c
X *
X *  Sccs-id:     @(#)dellayer.c  1.4  85/03/24
X *
X *  Description: This file contains one routine dellayer which
X *               deletes layer lp from the screen.
X *
X *  Author:      Simon Kenyon.
X *
X *  History:     SCK  1.1  83/10/03  Created.
X *               SCK  1.2  84/11/29  Made it work.
X *               SCK  1.3  85/02/27  Tidied up for release.
X *               SCK  1.4  85/03/24  Changed the include files around.
X */
X
X#include "../h/layers.h"
X
Xextern struct Layer *TopLayer;
Xextern struct Layer *BottomLayer;
X
X/*
X *  Name:        dellayer
X *
X *  Description: Delete layer lp from the screen.
X *
X *  Synopsis:    dellayer (lp)
X *               struct Layer   *lp;
X *
X *  Globals:     BottomLayer  (r/w)
X *               TopLayer     (w)
X *
X *  Calls:       upfront     (upfront.c)
X *               background  (background.c)
X *               bfree       (bfree.c)
X *               free        (libc)
X *
X *  Called by:   This is a top level routine.
X */
Xdellayer (lp)
Xstruct Layer   *lp;
X{
X    struct Obscured *op;
X    struct Obscured *nop;
X
X /*
X  * pull the layer to the front
X  * it now has no obscured pieces,
X  * and is a contiguous rectangle on the screen
X  */
X    (void) upfront (lp);
X /*
X  * colour the screen rectangle the background colour
X  */
X    (void) background (lp -> rect);
X /*
X  * push the layer to the back using upfront()
X  */
X    while (lp -> back != NULL)         /* lp not rearmost layer */
X        (void) upfront (BottomLayer);  /* rearmost layer */
X /*
X  * all storage needed for the obscured portions of the layer
X  * is now bound to the layer,
X  * since it obscures no other layer,
X  * so free all storage associated with the layer
X  */
X    for (op = lp -> obs; op != NULL;) {
X    /* op = each obscured part of lp */
X        nop = op;
X        op = op -> next;
X        (void) bfree (nop -> bmap);
X        free ((char *) nop);
X    }
X /*
X  * unlink the layer from the layer list
X  */
X    if (lp -> back != NULL)
X        lp -> back -> front = lp -> front;
X    else
X        BottomLayer = lp -> front;
X    if (lp -> front != NULL)
X        lp -> front -> back = lp -> back;
X    else
X        TopLayer = lp -> back;
X /*
X  * free the layer storage
X  */
X    free ((char *) lp);
X}
/
echo 'x - src/layerop.c'
sed 's/^X//' > src/layerop.c << '/'
X
X/*
X *  File:        layerop.c
X *
X *  Sccs-id:     @(#)layerop.c  1.4  85/03/24
X *
X *  Description: This file contains the one routine layerop which
X *               given a layer lp, a rectangle within the layer r,
X *               and a bitmap operator fn, recursively subdivide
X *               the rectangle r into rectangles contained within
X *               single bitmaps, and invokes the operator on the
X *               rectangle/bitmap pairs.
X *
X *  Author:      Simon Kenyon.
X *
X *  History:     SCK  1.1  83/10/03  Created.
X *               SCK  1.2  84/11/29  Made it work.
X *               SCK  1.3  85/03/04  Tidied up for release.
X *               SCK  1.4  85/03/24  Changed the include files around.
X */
X
X#include "../h/layers.h"
X
X/*
X *  Name:        layerop
X *
X *  Description: Given a layer lp, a rectangle within the layer r,
X *               and a bitmap operator fn, recursively subdivide
X *               the rectangle r into rectangles contained within
X *               single bitmaps, and invokes the operator on the
X *               rectangle/bitmap pairs.
X *               Also passed is a pointer to a set of parameters
X *               for the bitmap operator otherargs.
X *
X *  Synopsis:    layerop (lp, fn, r, p1, p2, p3, p4)
X *               struct Layer   *lp;
X *               int     (*fn) ();
X *               struct Rectangle    r;
X *               int    *p1;
X *               int    *p2;
X *               int    *p3;
X *               int    *p4;
X *
X *  Globals:     None.
X *
X *  Calls:       rectXrect     (rectxrect.c)
X *               intersection  (intersection.c)
X *               Rlayerop      (rlayerop.c)
X *
X *  Called by:   newlayer  (newlayer.c)
X *               lblt      (lblt.c)
X *               lbitblt   (lbitblt.c)
X */
Xlayerop (lp, fn, r, p1, p2, p3, p4)
Xstruct Layer   *lp;
Xint     (*fn) ();                      /* pointer to bitmap operator */
Xstruct Rectangle    r;
Xint    *p1;                            /* other arguments used by (*fn)() */
Xint    *p2;
Xint    *p3;
Xint    *p4;
X{
X    struct Rectangle    intersection ();
X
X /*
X  * clip to outer rectangle of layer, then call Rlayerop()
X  */
X    if (rectXrect (r, lp -> rect)) {
X        r = intersection (r, lp -> rect);
X        (void) Rlayerop (lp, fn, r, lp -> obs, p1, p2, p3, p4);
X    }
X}
/
echo 'x - src/layers.c'
sed 's/^X//' > src/layers.c << '/'
X
X/*
X *  File:        layers.c
X *
X *  Sccs-id:     @(#)layers.c  1.2  85/03/24
X *
X *  Description: This file contains the one routine layers which
X *               initialises the layers code.
X *
X *  Author:      Simon Kenyon.
X *
X *  History:     SCK  1.1  85/03/21  Created.
X *               SCK  1.2  85/03/24  Changed the include files around.
X */
X
X#include "../h/layers.h"
X
Xstruct Layer   *TopLayer;
Xstruct Layer   *BottomLayer;
Xstruct Bitmap  *display;
Xstruct Rectangle    disprect = {{
X        0, 0
X}, {
X    256, 256
X}
X};
X
X/*
X *  Name:        layers
X *
X *  Description: Initialise my implementation of Layers
X *               as described in
X *
X *               acm Transactions On Graphics,
X *               April 1983 - Vol. 2, No. 2.
X *
X *               and also in
X *
X *               Software - Practice and Experience,
X *               February 1985 - Vol. 15, No. 2.
X *
X *  Synopsis:    layers ()
X *
X *  Globals:     TopLayer     (w)
X *               BottomLayer  (w)
X *               display      (w)
X *               disprect     (r/w)
X *
X *  Calls:       balloc      (balloc.c)
X *               background  (background.c)
X *
X *  Called by:   This is a top-level routine.
X */
Xlayers ()
X{
X    struct Bitmap  *balloc ();
X
X    TopLayer = NULL;
X    BottomLayer = NULL;
X    display = balloc (disprect);
X    (void) background (display -> rect);
X}
/
echo 'x - src/lbitblt.c'
sed 's/^X//' > src/lbitblt.c << '/'
X
X/*
X *  File:        lbitblt.c
X *
X *  Sccs-id:     @(#)lbitblt.c  1.4  85/03/24
X *
X *  Description: This file contains one routine lbitblt which
X *               bitblts the source rectangle rect in layer sl
X *               to the corresponding rectangle with origin pt
X *               in the destination layer dl. The bitmap hb is
X *               an optional halftone pattern.
X *
X *  Author:      Simon Kenyon.
X *
X *  History:     SCK  1.1  83/10/03  Created.
X *               SCK  1.2  84/11/29  Made it work.
X *               SCK  1.3  85/03/04  Tidied up for release.
X *               SCK  1.4  85/03/24  Changed the include files around.
X */
X
X#include "../h/layers.h"
X
Xstruct Point delta;
X
X/*
X *  Name:        lbitblt
X *
X *  Description: Bitblt the source rectangle rect in layer sl
X *               to the corresponding rectangle with origin pt
X *               in the destination layer dl. The bitmap hb is
X *               an optional halftone pattern.
X *
X *  Synopsis:    lbitblt (sl, rect, dl, pt, hb, f)
X *               struct Layer   *sl;
X *               struct Rectangle    rect;
X *               struct Layer   *dl;
X *               struct Point    pt;
X *               struct Bitmap  *hb;
X *               int     f;
X *
X *  Globals:    Pass ()  (pass.c)
X *              delta    (r/w)
X *
X *  Calls:      layerop  (layerop.c)
X *              lblt     (lblt.c)
X *
X *  Called by:  This is a top level function.
X */
Xlbitblt (sl, rect, dl, pt, hb, f)
Xstruct Layer   *sl;
Xstruct Rectangle    rect;
Xstruct Layer   *dl;
Xstruct Point    pt;
Xstruct Bitmap  *hb;                    /* halftone bitmap */
Xint     f;
X{
X    struct ListElement *SrcList;
X    struct ListElement *s;
X    struct Rectangle    r;
X    struct Point    p;
X
X    int     Pass ();
X
X    SrcList = NULL;
X    delta.x = pt.x - rect.origin.x;
X    delta.y = pt.y - rect.origin.y;
X    (void) layerop (sl, Pass, rect, &SrcList, NULL, NULL, NULL);
X    for (s = SrcList; s != NULL; s = s -> next) {
X    /*
X     * s = each element of SrcList
X     */
X        p.x = s -> rect.origin.x + delta.x;
X        p.y = s -> rect.origin.y + delta.y;
X        (void) lblt (dl, s -> bp, s -> rect, hb, p, f);
X    }
X}
/
echo 'x - src/lline.c'
sed 's/^X//' > src/lline.c << '/'
X
X/*
X *  File:        lline.c
X *
X *  Sccs-id:     @(#)lline.c  1.3  85/03/24
X *
X *  Description: This file contains a generalised line drawing routine.
X *
X *
X *  Author:      Simon Kenyon.
X *
X *  History:     SCK  1.1  85/03/01  Created.
X *               SCK  1.2  85/03/08  Tidied up for release.
X *               SCK  1.3  85/03/24  Changed the include files around.
X */
X
X#include "../h/layers.h"
X
Xunsigned short  penmap[] = {
X    0xFFFF
X};
Xstruct Bitmap   pen = {
X    penmap, 1, {{
X            0, 0
X    }, {
X        1, 1
X    }
X    }, 0, 0
X};
X
X/*
X *  Name:        sign
X *
X *  Description: Return the sign of the argument a.
X *
X *  Synopsis:    int     sign (a)
X *               int     a;
X *
X *  Globals:     None.
X *
X *  Calls:       Nothing.
X *
X *  Called by:   lline  (lline.c)
X */
Xint     sign (a)
Xint     a;
X{
X    if (a < 0)
X        return (-1);
X    else
X        if (a > 0)
X            return (1);
X        else
X            return (0);
X}
X
X/*
X *  Name:        lline
X *
X *  Description: Draw a line from p0 to p1 in a layer lp, using code f.
X *               (This was snarfed from the 1st Smalltalk-80 book. It
X *               is nowhere as efficient as the restartable dda described
X *               in Rob's original paper, but I was having so much hassle
X *               with the clipping in that, and I wanted to ship this...)
X *
X *  Synopsis:    lline (lp, p0, p1, f)
X *               struct Layer   *lp;
X *               struct Point    p0;
X *               struct Point    p1;
X *               int     f;
X *
X *  Globals:     pen  (r)
X *
X *  Calls:       lblt (lblt.c)
X *
X *  Called by:   This is a top level routine.
X */
Xlline (lp, p0, p1, f)
Xstruct Layer   *lp;
Xstruct Point    p0;
Xstruct Point    p1;
Xint     f;
X{
X    int     dx;
X    int     dy;
X    int     px;
X    int     py;
X    int     p;
X    struct Point    pt;
X    int     i;
X
X    dx = sign (p1.x - p0.x);
X    dy = sign (p1.y - p0.y);
X    px = abs (p1.y - p0.y);
X    py = abs (p1.x - p0.x);
X    pt.x = p0.x + lp -> rect.origin.x;
X    pt.y = p0.x + lp -> rect.origin.x;
X    lblt (lp, &pen, pen.rect, NULL, pt, f);/* first point */
X    if (py > px) {                     /* more horizontal */
X        p = py / 2;
X        for (i = 1; i < py; i++) {
X            pt.x += dx;
X            if ((p -= px) < 0) {
X                pt.y += dy;
X                p += py;
X            }
X            lblt (lp, &pen, pen.rect, NULL, pt, f);
X        }
X    }
X    else {                             /* more horizontal */
X        p = px / 2;
X        for (i = 1; i <= px; i++) {
X            pt.y += dy;
X            if ((p -= py) < 0) {
X                pt.x += dx;
X                p += px;
X            }
X            lblt (lp, &pen, pen.rect, NULL, pt, f);
X        }
X    }
X}
/
echo 'x - src/merge.c'
sed 's/^X//' > src/merge.c << '/'
X
X/*
X *  File:        merge.c
X *
X *  Sccs-id:     @(#)merge.c  1.4  85/03/24
X *
X *  Description: This file contains the one function merge which
X *               merges sourceWord with destinationWord using the
X *               boolean operation specified by the function code f.
X *
X *  Author:      Simon Kenyon.
X *
X *  History:     SCK  1.1  83/10/03  Created.
X *               SCK  1.2  84/11/29  Made it work.
X *               SCK  1.3  85/02/26  Tidied up for release.
X *               SCK  1.4  85/03/24  Changed the include files around.
X */
X
X#include "../h/layers.h"
X
Xextern unsigned short   AllOnes;
X
X/*
X *  Name:        merge
X *
X *  Description: Merge sourceWord with destinationWord using the
X *               boolean operation specified by the function code f.
X *
X *  Synopsis:    unsigned short  merge (sourceWord, destinationWord, f)
X *               unsigned short  sourceWord;
X *               unsigned short  destinationWord;
X *               int     f;
X *
X *  Globals:     AllOnes  (r)
X *
X *  Calls:       Nothing.
X *
X *  Called by:   bitblt  (bitblt.c)
X */
Xunsigned short  merge (sourceWord, destinationWord, f)
Xunsigned short  sourceWord;
Xunsigned short  destinationWord;
Xint     f;
X{
X /*
X  * these are the 16 combination rules
X  */
X    switch (f) {
X        case ALL_ZEROS:
X            return (0);
X        case S_AND_D:
X            return (sourceWord & destinationWord);
X        case S_AND_ND:
X            return (sourceWord & ~destinationWord);
X        case S:
X            return (sourceWord);
X        case NS_AND_D:
X            return (~sourceWord & destinationWord);
X        case D:
X            return (destinationWord);
X        case S_XOR_D:
X            return (sourceWord ^ destinationWord);
X        case S_OR_D:
X            return (sourceWord | destinationWord);
X        case NS_AND_ND:
X            return (~sourceWord & ~destinationWord);
X        case NS_XOR_D:
X            return (~sourceWord ^ destinationWord);
X        case ND:
X            return (~destinationWord);
X        case S_OR_ND:
X            return (sourceWord | ~destinationWord);
X        case NS:
X            return (~sourceWord);
X        case NS_OR_D:
X            return (~sourceWord | destinationWord);
X        case NS_OR_ND:
X            return (~sourceWord | ~destinationWord);
X        case ALL_ONES:
X            return (AllOnes);
X    }
X}
/
echo 'Part 03 of jerq layers code complete.'
exit



More information about the Comp.sources.unix mailing list