summaryrefslogtreecommitdiff
path: root/src/tga_line.c
blob: 981e40b661be35418cd260ef4743ef9457922369 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
 * Copyright 1999 by Matthew Grossman, Seattle, USA.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of Matthew
 * Grossman not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission.  Matthew Grossman makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * MATTHEW GROSSMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL MATTHEW GROSSMAN BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 *
 * Author:  Matthew Grossman, mattg@oz.net
 * 
 */

/* tga_line.c */
/* accelerated solid and dashed lines */
/* adapted from xaa/xaaLine.c */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <X11/X.h>
#include "misc.h"
#include "xf86.h"
#include "xf86_OSproc.h"

#include "scrnintstr.h"
#include "pixmapstr.h"
#include "miline.h"
#include "xf86str.h"

/*  #include "tga.h" */
#include "tga_regs.h"

#ifdef HAVE_XAA_H
#include "xaa.h"
#include "xaalocal.h"
/* line functions */
extern void
TGASetupForSolidLine(ScrnInfoPtr pScrn, int color, int rop,
		     unsigned int planemask);
extern void
TGASubsequentSolidHorVertLine(ScrnInfoPtr pScrn, int x, int y, int len,
			      int dir);
extern void
TGASubsequentSolidLine(ScrnInfoPtr pScrn, int x1, int y1, int x2, int y2,
		       int octant, int flags);
extern void
TGASetupForClippedLine(ScrnInfoPtr pScrn, int x1, int y1, int x2, int y2,
		       int octant);
extern void
TGASubsequentClippedSolidLine(ScrnInfoPtr pScrn, int x1, int y1, int len,
			      int err);

extern void
TGASetupForDashedLine(ScrnInfoPtr pScrn, int fg, int bg, int rop,
		      unsigned int planemask, int length,
		      unsigned char *pattern);
extern void
TGASubsequentDashedLine(ScrnInfoPtr pScrn, int x1, int y1, int x2, int y2,
			int octant, int flags, int phase);
extern void
TGASubsequentClippedDashedLine(ScrnInfoPtr pScrn, int x1, int y1, int len,
			       int err, int phase);


extern void
TGASync(ScrnInfoPtr pScrn);

void TGAPolySegment(DrawablePtr pDrawable, GCPtr pGC, int nseg,
		    xSegment *pSeg);
void TGAPolyLines(DrawablePtr pDrawable, GCPtr pGC, int mode, int npt,
		  DDXPointPtr pptInit);
void TGAPolySegmentDashed(DrawablePtr pDrawable, GCPtr pGC, int	nseg,
			  xSegment *pSeg);
void TGAPolyLinesDashed(DrawablePtr pDrawable, GCPtr pGC, int mode, int npt,
			DDXPointPtr pptInit);


void
#ifdef POLYSEGMENT
TGAPolySegment(
	       DrawablePtr	pDrawable,
	       GCPtr	pGC,
	       int		nseg,
	       xSegment	*pSeg
#else
	       TGAPolyLines(
			    DrawablePtr pDrawable,
			    GCPtr	pGC,
			    int		mode,		/* Origin or Previous */
			    int		npt,		/* number of points */
			    DDXPointPtr pptInit
#endif
			    ){
  XAAInfoRecPtr infoRec = GET_XAAINFORECPTR_FROM_GC(pGC);
  BoxPtr pboxInit = REGION_RECTS(pGC->pCompositeClip);
  int nboxInit = REGION_NUM_RECTS(pGC->pCompositeClip);
  unsigned int bias = miGetZeroLineBias(pDrawable->pScreen);
  int xorg = pDrawable->x;
  int yorg = pDrawable->y;
  int nbox;
  BoxPtr pbox;
#ifndef POLYSEGMENT
  DDXPointPtr ppt;
#endif
  int x1, x2, y1, y2, tmp, len;

#ifdef POLYSEGMENT
/*    ErrorF("TGAPolySegment called!\n"); */
#else
/*    ErrorF("TGAPolyLines called\n"); */
#endif
    
  if(!nboxInit)
    return;
  /****************/
  /* TGA FUNCTION */
  /****************/
  TGASetupForSolidLine(infoRec->pScrn, pGC->fgPixel, pGC->alu,
		       pGC->planemask);

#ifdef POLYSEGMENT
  while (nseg--)
#else
    ppt = pptInit;
  x2 = ppt->x + xorg;
  y2 = ppt->y + yorg;
  while(--npt)
#endif
    {
      nbox = nboxInit;
      pbox = pboxInit;

#ifdef POLYSEGMENT
      x1 = pSeg->x1 + xorg;	
      y1 = pSeg->y1 + yorg;
      x2 = pSeg->x2 + xorg;	
      y2 = pSeg->y2 + yorg;
      pSeg++;
#else
      x1 = x2; 
      y1 = y2;
      ++ppt;
      if (mode == CoordModePrevious) {
	xorg = x1; 
	yorg = y1;
      }
      x2 = ppt->x + xorg; 
      y2 = ppt->y + yorg;
#endif

      if (x1 == x2) { /* vertical line */
	/* make the line go top to bottom of screen, keeping
	   endpoint semantics
	*/
	if (y1 > y2) {
	  tmp = y2; 
	  y2 = y1 + 1; 
	  y1 = tmp + 1;
#ifdef POLYSEGMENT
	  if (pGC->capStyle != CapNotLast) y1--;
#endif
	}
#ifdef POLYSEGMENT
	else if (pGC->capStyle != CapNotLast) y2++;
#endif
	/* get to first band that might contain part of line */
	while(nbox && (pbox->y2 <= y1)) {
	  pbox++;
	  nbox--;
	}

	/* stop when lower edge of box is beyond end of line */
	while(nbox && (y2 >= pbox->y1)) {
	  if ((x1 >= pbox->x1) && (x1 < pbox->x2)) {
	    tmp = max(y1, pbox->y1);
	    len = min(y2, pbox->y2) - tmp;
	    if (len)
	      TGASubsequentSolidHorVertLine(infoRec->pScrn, x1, tmp,
					    len, DEGREES_270);
	  }
	  nbox--;
	  pbox++;
	}
#ifndef POLYSEGMENT
	y2 = ppt->y + yorg;
#endif
      } else if (y1 == y2) { /* horizontal line */
	/* force line from left to right, keeping endpoint semantics */
	if (x1 > x2) {
	  tmp = x2; 
	  x2 = x1 + 1; 
	  x1 = tmp + 1;
#ifdef POLYSEGMENT
	  if (pGC->capStyle != CapNotLast)  x1--;
#endif
	}
#ifdef POLYSEGMENT
	else if (pGC->capStyle != CapNotLast) x2++;
#endif

	/* find the correct band */
	while(nbox && (pbox->y2 <= y1)) {
	  pbox++;
	  nbox--;
	}

	/* try to draw the line, if we haven't gone beyond it */
	if (nbox && (pbox->y1 <= y1)) {
	  int orig_y = pbox->y1;
	  /* when we leave this band, we're done */
	  while(nbox && (orig_y == pbox->y1)) {
	    if (pbox->x2 <= x1) {
	      /* skip boxes until one might contain start point */
	      nbox--;
	      pbox++;
	      continue;
	    }

	    /* stop if left of box is beyond right of line */
	    if (pbox->x1 >= x2) {
	      nbox = 0;
	      break;
	    }

	    tmp = max(x1, pbox->x1);
	    len = min(x2, pbox->x2) - tmp;
	    if (len)
	      TGASubsequentSolidHorVertLine(infoRec->pScrn, tmp,
					    y1, len, DEGREES_0);
	    nbox--;
	    pbox++;
	  }
	}
#ifndef POLYSEGMENT
	x2 = ppt->x + xorg;
#endif
      } else{ /* sloped line */
	unsigned int oc1, oc2;
	int dmin, dmaj, e, octant;


	if((dmaj = x2 - x1) < 0) {
	  dmaj = -dmaj;
	  octant = XDECREASING;
	} else octant = 0;		   

	if((dmin = y2 - y1) < 0) {
	  dmin = -dmin;
	  octant |= YDECREASING;
	}	
	
	if(dmin >= dmaj){
	  tmp = dmin; dmin = dmaj; dmaj = tmp;
	  octant |= YMAJOR;
	}

	e = -dmaj - ((bias >> octant) & 1);
	len = dmaj;
	dmin *= 2;
	dmaj *= 2;


	while(nbox--) {
	  oc1 = oc2 = 0;
	  OUTCODES(oc1, x1, y1, pbox);
	  OUTCODES(oc2, x2, y2, pbox);
	  if (!(oc1 | oc2)) {   /* unclipped */
	    TGASubsequentSolidLine(infoRec->pScrn, x1, y1, x2, y2,
				   octant, 
#ifdef POLYSEGMENT
				   (pGC->capStyle != CapNotLast) ? 0 :
#endif
				   OMIT_LAST
				   );
	    break;
	  } else if (oc1 & oc2) { /* completely clipped */
	    pbox++;
		    
	  } else { /* partially clipped */
	    int new_x1 = x1, new_y1 = y1, new_x2 = x2, new_y2 = y2;
	    int clip1 = 0, clip2 = 0;
	    int err, adx, ady;
		    
	    if(octant & YMAJOR) {
	      ady = dmaj /= 2;
	      adx = dmin /= 2;
	    } else {
	      ady = dmin /= 2;
	      adx = dmaj /= 2;
	    }

	    if (miZeroClipLine(pbox->x1, pbox->y1, 
			       pbox->x2 - 1, pbox->y2 - 1,
			       &new_x1, &new_y1, &new_x2, &new_y2,
			       adx, ady, &clip1, &clip2,
			       octant, bias, oc1, oc2) == -1)
	      {
		pbox++;
		continue;
	      }

	    if (octant & YMAJOR)
	      len = abs(new_y2 - new_y1);
	    else
	      len = abs(new_x2 - new_x1);
#ifdef POLYSEGMENT
	    if (clip2 != 0 || pGC->capStyle != CapNotLast)
	      len++;
#else
	    len += (clip2 != 0);
#endif
	    if (len) { /* we have a real line */
	      int abserr, clipdx, clipdy;

	      /* unwind bresenham error term to first point */
	      if (clip1) {
		clipdx = abs(new_x1 - x1);
		clipdy = abs(new_y1 - y1);

		if (octant & YMAJOR)
		  err = e + clipdy*dmin - clipdx*dmaj;
		else
		  err = e + clipdx*dmin - clipdy*dmaj;
	      } else
		err = e;

#define range infoRec->SolidBresenhamLineErrorTermBits
	      abserr = abs(err);			    
	      while((abserr & range) || 
		    (dmaj & range) ||
		    (dmin & range)) {
		dmin /= 2;
		dmaj /= 2;
		abserr /= 2;
		err /= 2;
	      }
	      TGASetupForClippedLine(infoRec->pScrn, x1, y1, x2,
				     y2, octant);
	      TGASubsequentClippedSolidLine(infoRec->pScrn,
					    new_x1, new_y1, len,
					    err);


	    }
	    pbox++;
	  }
	} /* while (nbox--) */
      } /* sloped line */
    } /* while (nline--) */

#ifndef POLYSEGMENT
  /* paint the last point if the end style isn't CapNotLast.
       (Assume that a projecting, butt, or round cap that is one
        pixel wide is the same as the single pixel of the endpoint.)
    */

  if ((pGC->capStyle != CapNotLast) &&
      ((ppt->x + xorg != pptInit->x + pDrawable->x) ||
       (ppt->y + yorg != pptInit->y + pDrawable->y) ||
       (ppt == pptInit + 1)))
    {
      nbox = nboxInit;
      pbox = pboxInit;
      while (nbox--)
	{
	  if ((x2 >= pbox->x1) && (y2 >= pbox->y1) &&
	      (x2 <  pbox->x2) && (y2 <  pbox->y2))
	    {
	      TGASubsequentSolidHorVertLine(infoRec->pScrn, x2, y2, 1,
					    DEGREES_0);
	      break;
	    }
	  else
	    pbox++;
	}
    }
#endif

  TGASync(infoRec->pScrn);
  return;
}

#undef range

  void
#ifdef POLYSEGMENT
  TGAPolySegmentDashed(
		       DrawablePtr	pDrawable,
		       GCPtr	pGC,
		       int		nseg,
		       xSegment	*pSeg
#else
		       TGAPolyLinesDashed(
					  DrawablePtr pDrawable,
					  GCPtr	pGC,
					  int		mode,		/* Origin or Previous */
					  int		npt,		/* number of points */
					  DDXPointPtr pptInit
#endif
					  ){
    XAAInfoRecPtr infoRec = GET_XAAINFORECPTR_FROM_GC(pGC);
#if TGA_OLDPRIV
    XAAGCPtr   pGCPriv = (XAAGCPtr) (pGC)->devPrivates[XAAGetGCIndex()].ptr;
#else
    XAAGCPtr   pGCPriv = (XAAGCPtr)dixLookupPrivate(&(pGC)->devPrivates, XAAGetGCKey());
#endif
    BoxPtr pboxInit = REGION_RECTS(pGC->pCompositeClip);
    int nboxInit = REGION_NUM_RECTS(pGC->pCompositeClip);
    unsigned int bias = miGetZeroLineBias(pDrawable->pScreen);
    int xorg = pDrawable->x;
    int yorg = pDrawable->y;
    int nbox;
    BoxPtr pbox;
#ifndef POLYSEGMENT
    DDXPointPtr ppt;
#endif
    unsigned int oc1, oc2;
    int dmin, dmaj, e, octant;
    int x1, x2, y1, y2, tmp, len, offset;
    int PatternLength, PatternOffset;

#ifdef POLYSEGMENT
/*      ErrorF("TGAPolySegmentDashed called\n"); */
#else
/*      ErrorF("TGAPolyLinesDashed called\n"); */
#endif
    
    if(!nboxInit)
      return;

    PatternLength = pGCPriv->DashLength; 
    PatternOffset = pGC->dashOffset % PatternLength;

    TGASetupForDashedLine(infoRec->pScrn, pGC->fgPixel,
			  (pGC->lineStyle == LineDoubleDash) ? pGC->bgPixel : -1,
			  pGC->alu, pGC->planemask, PatternLength, pGCPriv->DashPattern);


#ifdef POLYSEGMENT
    while (nseg--)
#else
      ppt = pptInit;
    x2 = ppt->x + xorg;
    y2 = ppt->y + yorg;
    while(--npt)
#endif
      {
	nbox = nboxInit;
	pbox = pboxInit;

#ifdef POLYSEGMENT
	x1 = pSeg->x1 + xorg;	
	y1 = pSeg->y1 + yorg;
	x2 = pSeg->x2 + xorg;	
	y2 = pSeg->y2 + yorg;
	pSeg++;
#else
	x1 = x2; 
	y1 = y2;
	++ppt;
	if (mode == CoordModePrevious) {
	  xorg = x1; 
	  yorg = y1;
	}
	x2 = ppt->x + xorg; 
	y2 = ppt->y + yorg;
#endif



	if((dmaj = x2 - x1) < 0) {
	  dmaj = -dmaj;
	  octant = XDECREASING;
	} else octant = 0;		   

	if((dmin = y2 - y1) < 0) {
	  dmin = -dmin;
	  octant |= YDECREASING;
	}	
	
	if(dmin >= dmaj){
	  tmp = dmin; dmin = dmaj; dmaj = tmp;
	  octant |= YMAJOR;
	}

	e = -dmaj - ((bias >> octant) & 1);
	len = dmaj;
	dmin <<= 1;
	dmaj <<= 1;


	while(nbox--) {
	  oc1 = oc2 = 0;
	  OUTCODES(oc1, x1, y1, pbox);
	  OUTCODES(oc2, x2, y2, pbox);
	  if (!(oc1 | oc2)) {   /* unclipped */
	    TGASubsequentDashedLine(infoRec->pScrn, x1, y1, x2, y2,
				    octant,
#ifdef POLYSEGMENT
				    (pGC->capStyle != CapNotLast) ? 0 :
#endif
				    OMIT_LAST, PatternOffset);
	    break;
	  } else if (oc1 & oc2) { /* completely clipped */
	    pbox++;
	  } else { /* partially clipped */
	    int new_x1 = x1, new_y1 = y1, new_x2 = x2, new_y2 = y2;
	    int clip1 = 0, clip2 = 0;
	    int err, adx, ady;
		    
	    if(octant & YMAJOR) {
	      ady = dmaj >> 1;
	      adx = dmin >> 1;
	    } else {
	      ady = dmin >> 1;
	      adx = dmaj >> 1;
	    }

	    if (miZeroClipLine(pbox->x1, pbox->y1, 
			       pbox->x2 - 1, pbox->y2 - 1,
			       &new_x1, &new_y1, &new_x2, &new_y2,
			       adx, ady, &clip1, &clip2,
			       octant, bias, oc1, oc2) == -1)
	      {
		pbox++;
		continue;
	      }

	    if (octant & YMAJOR)
	      len = abs(new_y2 - new_y1);
	    else
	      len = abs(new_x2 - new_x1);
#ifdef POLYSEGMENT
	    if (clip2 != 0 || pGC->capStyle != CapNotLast)
	      len++;
#else
	    len += (clip2 != 0);
#endif
	    if (len) {
	      int abserr, clipdx, clipdy;
	      /* unwind bresenham error term to first point */
	      if (clip1) {
		clipdx = abs(new_x1 - x1);
		clipdy = abs(new_y1 - y1);

		if (octant & YMAJOR)
		  err = e + clipdy*dmin - clipdx*dmaj;
		else
		  err = e + clipdx*dmin - clipdy*dmaj;
	      } else
		err = e;

#define range infoRec->DashedBresenhamLineErrorTermBits
	      abserr = abs(err);			    
	      while((abserr & range) || 
		    (dmaj & range) ||
		    (dmin & range)) {
		dmin >>= 1;
		dmaj >>= 1;
		abserr >>= 1;
		err /= 2;
	      }

	      if(octant & YMAJOR)
		offset = abs(new_y1 - y1);
	      else 
		offset = abs(new_x1 - x1);

	      offset += PatternOffset;
	      offset %= PatternLength;

	      TGASetupForClippedLine(infoRec->pScrn, x1, x2, y1, y2,
				     octant);
	      TGASubsequentClippedDashedLine(infoRec->pScrn, new_x1,
					     new_y1, len, err,
					     PatternOffset);
	    }
	    pbox++;
	  }
	} /* while (nbox--) */
#ifndef POLYSEGMENT
	len = abs(y2 - y1);
	tmp = abs(x2 - x1);
	PatternOffset += (len > tmp) ? len : tmp;
	PatternOffset %= PatternLength;
#endif
      } /* while (nline--) */

#ifndef POLYSEGMENT
    /* paint the last point if the end style isn't CapNotLast.
       (Assume that a projecting, butt, or round cap that is one
        pixel wide is the same as the single pixel of the endpoint.)
    */

    if ((pGC->capStyle != CapNotLast) &&
	((ppt->x + xorg != pptInit->x + pDrawable->x) ||
	 (ppt->y + yorg != pptInit->y + pDrawable->y) ||
	 (ppt == pptInit + 1)))
      {
	nbox = nboxInit;
	pbox = pboxInit;
	while (nbox--) {
	  if ((x2 >= pbox->x1) && (y2 >= pbox->y1) &&
	      (x2 <  pbox->x2) && (y2 <  pbox->y2))
	    {
	      TGASubsequentDashedLine(infoRec->pScrn, x2, y2, x2, y2, 0, 0,
				      PatternOffset);
	      break;
	    } else
		pbox++;
	}
    }
#endif

    TGASync(infoRec->pScrn);
    return;
}

    
#endif