-
-
-#define IMTRUNC(x) ((int)(x*16))
-
-
-/*
-typedef struct {
- short ms,ls;
-} pcord;
-*/
-
-typedef int pcord;
-
-struct p_point {
- int n;
- pcord x,y;
-};
-
-struct p_line {
- int n;
- pcord x1,y1;
- pcord x2,y2;
- pcord miny,maxy;
-};
-
-struct p_slice {
- int n;
- double x;
-};
-
-int
-p_compy(const struct p_point *p1, const struct p_point *p2) {
- if (p1->y > p2->y) return 1;
- if (p1->y < p2->y) return -1;
- return 0;
-}
-
-int
-p_compx(const struct p_slice *p1, const struct p_slice *p2) {
- if (p1->x > p2->x) return 1;
- if (p1->x < p2->x) return -1;
- return 0;
-}
-
-/* Change this to int? and round right goddamn it! */
-
-double
-p_eval_aty(struct p_line *l,pcord y) {
- int t;
- t=l->y2-l->y1;
- if (t) return ( (y-l->y1)*l->x2 + (l->y2-y)*l->x1 )/t;
- return (l->x1+l->x2)/2.0;
-}
-
-double
-p_eval_atx(struct p_line *l,pcord x) {
- int t;
- t=l->x2-l->x1;
- if (t) return ( (x-l->x1)*l->y2 + (l->x2-x)*l->y1 )/t;
- return (l->y1+l->y2)/2.0;
-}
-
-
-/* Algorithm to count the pixels covered by line going through pixel (x,y)
- in coarse coords.
-*/
-
-/*
-static int
-p_eval_coverage(struct p_line *l, int lc, int x, pcord y1, pcord y2) {
-
- return 0;
-}
-*/
-
-
-/* Antialiasing polygon algorithm
- specs:
- 1. only nice polygons - no crossovers
- 2. 1/16 pixel resolution # previously - floating point co-ordinates
- 3. full antialiasing ( complete spectrum of blends )
- 4. uses hardly any memory
- 5. no subsampling phase
-
- For each interval we must:
- 1. find which lines are in it
- 2. order the lines from in increasing x order.
- since we are assuming no crossovers it is sufficent
- to check a single point on each line.
-*/
-
-/*
- Terms:
-
- 1. Interval: A vertical segment in which no lines cross nor end.
- 2. Scanline: A physical line, contains 16 subpixels in the horizontal direction
- 3. Slice: A start stop line pair.
-
- */
-
-/* Templine logic:
-
- The variable tempflush describes if there is anything in the templine array or not.
-
- if tempflush is 0 then the array is clean.
- if tempflush is 1 then the array contains a partial filled scanline
-
- */
-
-/* Rendering of a single start stop pair:
-
-?? REWRITE
-
- The rendering is split in three parts
- 1. From the first start pixel to the first stop pixel
- 2. Area from the first end pixel to the last start pixel
- 3. Area from the first end pixel to the last start pixel
-
- */
-
-
-void
-i_poly_aa(i_img *im,int l,double *x,double *y,i_color *val) {
- int i,k; /* Index variables */
- int clc; /* Index of next item on interval linelist */
- int tx; /* Coarse x coord within a scanline */
- pcord miny,maxy; /* Min and max values of the current slice in the subcord system */
- pcord minacy,maxacy; /* Min and max values of the current scanline bounded by the slice
- in the subcord system */
- int cscl; /* Current scanline */
- pcord cc; /* Current vertical centerpoint of interval */
- int mt1,mt2;
- int minsx,minex,maxsx,maxex; /* The horizontal stretches of the lines beloning to the current slice within a scanline */
- int *templine; /* Line accumulator */
-
- struct p_point *pset; /* List of points in polygon */
- struct p_line *lset; /* List of lines in polygon */
- struct p_slice *tllist; /* List of slices */
-
- i_color red,blue,yellow;
- red.rgb.r=255;
- red.rgb.g=0;
- red.rgb.b=0;
-
- blue.rgb.r=0;
- blue.rgb.g=0;
- blue.rgb.b=255;
-
- yellow.rgb.r=255;
- yellow.rgb.g=255;
- yellow.rgb.b=255;
-
- if ( (pset=mymalloc(sizeof(struct p_point)*l)) == NULL) { m_fatal(2,"malloc failed\n"); return; }
- if ( (lset=mymalloc(sizeof(struct p_line)*l)) == NULL) { m_fatal(2,"malloc failed\n"); return; }
- if ( (tllist=mymalloc(sizeof(struct p_slice)*l)) == NULL) { m_fatal(2,"malloc failed\n"); return; }
- if ( (templine=mymalloc(sizeof(int)*im->xsize)) == NULL) { m_fatal(2,"malloc failed\n"); return; }
-
- /* insert the lines into the line list */
-
- for(i=0;i<l;i++) {
- pset[i].n=i;
- pset[i].x=IMTRUNC(x[i]);
- pset[i].y=IMTRUNC(y[i]);
- lset[i].n=i;
- lset[i].x1=IMTRUNC(x[i]);
- lset[i].y1=IMTRUNC(y[i]);
- lset[i].x2=IMTRUNC(x[(i+1)%l]);
- lset[i].y2=IMTRUNC(y[(i+1)%l]);
- lset[i].miny=min(lset[i].y1,lset[i].y2);
- lset[i].maxy=max(lset[i].y1,lset[i].y2);
- }
-
- qsort(pset,l,sizeof(struct p_point),(int(*)(const void *,const void *))p_compy);
-
- printf("post point list (sorted in ascending y order)\n");
- for(i=0;i<l;i++) {
- printf("%d [ %d ] %d %d\n",i,pset[i].n,pset[i].x,pset[i].y);
- }
-
- printf("line list\n");
- for(i=0;i<l;i++) {
- printf("%d [ %d ] (%d , %d) -> (%d , %d) yspan ( %d , %d )\n",i,lset[i].n,lset[i].x1,lset[i].y1,lset[i].x2,lset[i].y2,lset[i].miny,lset[i].maxy);
- }
-
- printf("MAIN LOOP\n\n");
-
- /* Zero templine buffer */
- /* Templine buffer flushed everytime a scan line ends */
- for(i=0;i<im->xsize;i++) templine[i]=0;
-
-
- /* loop on intervals */
- for(i=0;i<l-1;i++) {
- cc=(pset[i].y+pset[i+1].y)/2;
- printf("current slice is: %d to %d ( cpoint %d )\n",pset[i].y,pset[i+1].y,cc);
- clc=0;
-
- /* stuff this in a function ?? */
-
- /* Check what lines belong to interval */
- for(k=0;k<l;k++) {
- printf("checking line: %d [ %d ] (%d , %d) -> (%d, %d) yspan ( %d , %d )",
- k,lset[k].n,lset[k].x1,lset[k].y1,lset[k].x2,lset[k].y2,lset[k].miny,lset[k].maxy);
- if (cc >= lset[k].miny && cc <= lset[k].maxy) {
- if (lset[k].miny == lset[k].maxy) printf(" HORIZONTAL - skipped\n");
- else {
- printf(" INSIDE\n");
- tllist[clc].x=p_eval_aty(&lset[k],cc);
- tllist[clc++].n=k;
- }
- } else printf(" OUTSIDE\n");
- }
-
- /*
- at this point a table of pixels that need special care should
- be generated from the line list - it should be ordered so that only
- one needs to be checked - options: rendering to a list then order - or
- rendering in the right order might be possible to do nicely with the
- following heuristic:
-
- 1. Draw leftmost pixel for this line
- 2. If preceeding pixel was occupied check next one else go to 1 again.
- */
-
- printf("lines in current interval:");
- for(k=0;k<clc;k++) printf(" %d (%.2f)",tllist[k].n,tllist[k].x);
- printf("\n");
-
- /* evaluate the lines in the middle of the slice */
-
- printf("Sort lines left to right within interval\n");
- qsort(tllist,clc,sizeof(struct p_slice),(int(*)(const void *,const void *))p_compx);
-
- printf("sorted lines in interval - output:");
- for(k=0;k<clc;k++) printf(" %d",tllist[k].n);
- printf("\n");
-
- miny=pset[i].y;
- maxy=pset[i+1].y;
-
- /* iterate over scanlines */
- for(cscl=(miny)/16;cscl<=maxy/16;cscl++) {
- minacy=max(miny,cscl*16);
- maxacy=min(maxy,cscl*16+15);
-
- printf("Scanline bound %d - %d\n",minacy, maxacy);
-
- /* iterate over line pairs (slices) within interval */
- for(k=0;k<clc-1;k+=2) {
-
- mt1=p_eval_aty(&lset[tllist[k].n],minacy); /* upper corner */
- mt2=p_eval_aty(&lset[tllist[k].n],maxacy); /* lower corner */
- minsx=min(mt1,mt2);
- minex=max(mt1,mt2);
- mt1=p_eval_aty(&lset[tllist[k+1].n],minacy); /* upper corner */
- mt2=p_eval_aty(&lset[tllist[k+1].n],maxacy); /* lower corner */
- maxsx=min(mt1,mt2);
- maxex=max(mt1,mt2);
-
- printf("minsx: %d minex: %d\n",minsx,minex);
- printf("maxsx: %d maxex: %d\n",maxsx,maxex);
-
- if (minex/16<maxsx/16) printf("Scan slice is simple!\n");
- else printf("Scan slice is complicated!\n");
-
- if (minsx/16 == minex/16) { /* The line starts and ends in the same pixel */
- printf("Low slant start pixel\n");
- templine[minsx/16]=(maxacy-minacy+1)*(minex-minsx+1)/2+((minex | 0xF)-minex)*(maxacy-minacy+1);
- } else {
- for(tx=minsx/16;tx<minex/16+1;tx++) {
- int minx,maxx,minxy,maxxy;
- minx=max(minsx, tx*16 );
- maxx=min(minex, tx*16+15);
-
- if (minx == maxx) {
- templine[tx]=(maxacy-minacy+1);
- } else {
-
- minxy=p_eval_atx(&lset[tllist[k].n], minx);
- maxxy=p_eval_atx(&lset[tllist[k].n], maxx);
-
- templine[tx]+=(abs(minxy-maxxy)+1)*(minex-minsx+1)/2; /* The triangle between the points */
- if (mt1 < mt2) { /* \ slant */
- /* ((minex | 0xF)-minex)*(maxacy-minacy+1); FIXME: unfinished */
-
-
-
- } else {
- templine[tx]+=((minex | 0xF)-minex)*(maxacy-minacy+1);
- }
-
- }
- }
- }
-
- for(tx=maxsx/16;tx<maxex/16+1;tx++) templine[tx]+=16*(maxacy-minacy+1);
-
- /* for(tx=minex/16+1;tx<maxsx/16;tx++) 0; */
-
-
- printf("line %d: painting %d - %d\n",cscl,minex/16+1,maxsx/16);
- if ( (minacy != cscl*16) || (maxacy != cscl*16+15) ) {
- for(tx=minsx/16;tx<maxex/16+1;tx++) {
- i_ppix(im,tx,cscl,&yellow);
- }
- }
- else {
- for(tx=minsx/16;tx<minex/16+1;tx++) i_ppix(im,tx,cscl,&red);
- for(tx=maxsx/16;tx<maxex/16+1;tx++) i_ppix(im,tx,cscl,&blue);
- for(tx=minex/16+1;tx<maxsx/16;tx++) i_ppix(im,tx,cscl,val);
- }
-
- } /* Slices */
- } /* Scanlines */
- } /* Intervals */
-} /* Function */
-
-
-
-
-
-
-