| 1 |
/* $XConsortium: lines.c,v 1.2 91/10/10 11:18:21 rws Exp $ */ |
| 2 |
/* Copyright International Business Machines, Corp. 1991 |
| 3 |
* All Rights Reserved |
| 4 |
* Copyright Lexmark International, Inc. 1991 |
| 5 |
* All Rights Reserved |
| 6 |
* |
| 7 |
* License to use, copy, modify, and distribute this software and its |
| 8 |
* documentation for any purpose and without fee is hereby granted, |
| 9 |
* provided that the above copyright notice appear in all copies and that |
| 10 |
* both that copyright notice and this permission notice appear in |
| 11 |
* supporting documentation, and that the name of IBM or Lexmark not be |
| 12 |
* used in advertising or publicity pertaining to distribution of the |
| 13 |
* software without specific, written prior permission. |
| 14 |
* |
| 15 |
* IBM AND LEXMARK PROVIDE THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES OF |
| 16 |
* ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO ANY |
| 17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, |
| 18 |
* AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. THE ENTIRE RISK AS TO THE |
| 19 |
* QUALITY AND PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT |
| 20 |
* OR MAINTAIN, BELONGS TO THE LICENSEE. SHOULD ANY PORTION OF THE |
| 21 |
* SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM OR LEXMARK) ASSUMES THE |
| 22 |
* ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION. IN NO EVENT SHALL |
| 23 |
* IBM OR LEXMARK BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 24 |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 25 |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 26 |
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF |
| 27 |
* THIS SOFTWARE. |
| 28 |
*/ |
| 29 |
/* LINES CWEB V0003 ******** */ |
| 30 |
/* |
| 31 |
:h1.LINES Module - Rasterizing Lines |
| 32 |
|
| 33 |
&author. Duaine W. Pryor, Jr. and Jeffrey B. Lotspiech ([email protected]) |
| 34 |
|
| 35 |
|
| 36 |
:h3.Include Files |
| 37 |
|
| 38 |
The included files are: |
| 39 |
*/ |
| 40 |
|
| 41 |
#include "types.h" |
| 42 |
#include "objects.h" |
| 43 |
#include "spaces.h" |
| 44 |
#include "paths.h" |
| 45 |
#include "regions.h" |
| 46 |
#include "lines.h" |
| 47 |
|
| 48 |
/* |
| 49 |
:h3.Functions Provided to the TYPE1IMAGER User |
| 50 |
|
| 51 |
None. |
| 52 |
*/ |
| 53 |
|
| 54 |
/* |
| 55 |
:h3.Functions Provided to Other Modules |
| 56 |
|
| 57 |
This module provides the following entry point to other modules: |
| 58 |
*/ |
| 59 |
|
| 60 |
/*SHARED LINE(S) ORIGINATED HERE*/ |
| 61 |
|
| 62 |
/* |
| 63 |
:h3.Macros Provided to Other Modules |
| 64 |
|
| 65 |
None. |
| 66 |
*/ |
| 67 |
|
| 68 |
/* |
| 69 |
:h2.StepLine() - Produces Run Ends for a Line After Checks |
| 70 |
|
| 71 |
The main work is done by Bresenham(); here we just perform checks and |
| 72 |
get the line so that its Y direction is always increasing: |
| 73 |
*/ |
| 74 |
|
| 75 |
static void Bresenham(pel *, fractpel, fractpel, fractpel, fractpel); |
| 76 |
|
| 77 |
void StepLine( |
| 78 |
register struct region *R, /* region being built */ |
| 79 |
register fractpel x1, /* starting ... */ |
| 80 |
register fractpel y1, /* ... point */ |
| 81 |
register fractpel x2, /* ending ... */ |
| 82 |
register fractpel y2) /* ... point */ |
| 83 |
{ |
| 84 |
register fractpel dy; |
| 85 |
|
| 86 |
IfTrace4((LineDebug > 0), ".....StepLine: (%dl,%dl) to (%dl,%dl)\n", |
| 87 |
x1, y1, x2, y2); |
| 88 |
|
| 89 |
dy = y2 - y1; |
| 90 |
|
| 91 |
/* |
| 92 |
We execute the "GOING_TO" macro to call back the REGIONS module, if |
| 93 |
necessary (like if the Y direction of the edge has changed): |
| 94 |
*/ |
| 95 |
GOING_TO(R, x1, y1, x2, y2, dy); |
| 96 |
|
| 97 |
if (dy == 0) |
| 98 |
return; |
| 99 |
|
| 100 |
if (dy < 0) |
| 101 |
Bresenham(R->edge, x2, y2, x1, y1); |
| 102 |
else |
| 103 |
Bresenham(R->edge, x1, y1, x2, y2); |
| 104 |
return; |
| 105 |
} |
| 106 |
/* |
| 107 |
:h3.Bresenham() - Actually Produces Run Ends |
| 108 |
|
| 109 |
This routine runs a Bresenham line-stepping |
| 110 |
algorithm. See, for example, Newman and Sproul, :hp1/Principles |
| 111 |
of Interactive Computer Graphics/, pp. 25-27. |
| 112 |
When we enter this, we |
| 113 |
are guaranteed that dy is positive. |
| 114 |
We'd like to work in 8 bit precision, so we'll define some macros and |
| 115 |
constants to let us do that: |
| 116 |
*/ |
| 117 |
|
| 118 |
#define PREC 8 /* we'll keep fraction pels in 8 bit precision */ |
| 119 |
/* |
| 120 |
RoundFP() rounds down by 'b' bits: |
| 121 |
*/ |
| 122 |
#define RoundFP(xy,b) (((xy)+(1<<((b)-1)))>>(b)) |
| 123 |
|
| 124 |
/* |
| 125 |
TruncFP() truncates down by 'b' bits: |
| 126 |
*/ |
| 127 |
#define TruncFP(xy,b) ((xy)>>(b)) |
| 128 |
|
| 129 |
|
| 130 |
static void Bresenham( |
| 131 |
register pel *edgeP, /* pointer to top of list (y == 0) */ |
| 132 |
register fractpel x1, /* starting point ... */ |
| 133 |
register fractpel y1, /* ... on line */ |
| 134 |
register fractpel x2, /* ending point ... */ |
| 135 |
register fractpel y2) /* ... on the line (down) */ |
| 136 |
{ |
| 137 |
register int32_t dx,dy; /* change in x and y, in my own precision */ |
| 138 |
register int32_t x,y; /* integer pel starting point */ |
| 139 |
register int count; /* integer pel delta y */ |
| 140 |
register int32_t d; /* the Bresenham algorithm error term */ |
| 141 |
|
| 142 |
x1 = TruncFP(x1, FRACTBITS-PREC); |
| 143 |
y1 = TruncFP(y1, FRACTBITS-PREC); |
| 144 |
x2 = TruncFP(x2, FRACTBITS-PREC); |
| 145 |
y2 = TruncFP(y2, FRACTBITS-PREC); |
| 146 |
|
| 147 |
dx = x2 - x1; |
| 148 |
dy = y2 - y1; |
| 149 |
/* |
| 150 |
Find the starting x and y integer pel coordinates: |
| 151 |
*/ |
| 152 |
|
| 153 |
x = RoundFP(x1,PREC); |
| 154 |
y = RoundFP(y1,PREC); |
| 155 |
edgeP += y; |
| 156 |
count = RoundFP(y2,PREC) - y; |
| 157 |
/*------------------------------------------------------------------*/ |
| 158 |
/* Force dx to be positive so that dfy will be negative */ |
| 159 |
/* this means that vertical moves will decrease d */ |
| 160 |
/*------------------------------------------------------------------*/ |
| 161 |
if (dx<0) |
| 162 |
{ |
| 163 |
dx = -dx; |
| 164 |
#define P PREC |
| 165 |
d=(dy*(x1-(x<<P)+(1<<(P-1)))-dx*((y<<P)-y1+(1<<(P-1))))>>P; |
| 166 |
#undef P |
| 167 |
while(--count >= 0 ) |
| 168 |
{ |
| 169 |
while(d<0) |
| 170 |
{ |
| 171 |
--x; |
| 172 |
d += dy; |
| 173 |
} |
| 174 |
*(edgeP++) = x; |
| 175 |
d -= dx; |
| 176 |
} |
| 177 |
} |
| 178 |
else /* positive dx */ |
| 179 |
{ |
| 180 |
#define P PREC |
| 181 |
d = (dy*((x<<P)-x1+(1<<(P-1)))-dx*((y<<P)-y1+(1<<(P-1))))>>P; |
| 182 |
#undef P |
| 183 |
while(--count >= 0 ) |
| 184 |
{ |
| 185 |
while(d<0) |
| 186 |
{ |
| 187 |
++x; |
| 188 |
d += dy; |
| 189 |
} |
| 190 |
*(edgeP++) = x; |
| 191 |
d -= dx; |
| 192 |
} |
| 193 |
} |
| 194 |
} |