#include "spdo_prv.h"
#define CLOCKWISE 1
#define DEBUG 0
#define ABS(X) ( (X < 0) ? -X : X)
#if DEBUG
#include <stdio.h>
#define SHOW(X) printf("X = %d\n", X)
#else
#define SHOW(X)
#endif
#if INCL_2D
static void sp_draw_vector_to_2d(PROTO_DECL2 fix15 x0,fix15 y0,fix15 x1,fix15 y1,band_t GLOBALFAR *band);
static void sp_add_intercept_2d(PROTO_DECL2 fix15 y,fix15 x);
static void sp_proc_intercepts_2d(PROTO_DECL1);
#endif
#if INCL_2D
FUNCTION boolean init_2d(
GDECL
specs_t GLOBALFAR *specsarg)
{
if (specsarg->flags & CURVES_OUT)
return FALSE;
#if DEBUG
printf("INIT_OUT__2d()\n");
#endif
return TRUE;
}
#endif
#if INCL_2D
FUNCTION boolean begin_char_2d(
GDECL
point_t Psw,
point_t Pmin,
point_t Pmax)
{
#if DEBUG
printf("BEGIN_CHAR__2d(%3.1f, %3.1f, %3.1f, %3.1f, %3.1f, %3.1f\n",
(real)Psw.x / (real)sp_globals.onepix, (real)Psw.y / (real)sp_globals.onepix,
(real)Pmin.x / (real)sp_globals.onepix, (real)Pmin.y / (real)sp_globals.onepix,
(real)Pmax.x / (real)sp_globals.onepix, (real)Pmax.y / (real)sp_globals.onepix);
#endif
sp_globals.x_scan_active = TRUE;
init_char_out(Psw,Pmin,Pmax);
return TRUE;
}
#endif
#if INCL_2D
FUNCTION void begin_contour_2d(
GDECL
point_t P1,
boolean outside)
{
#if DEBUG
printf("BEGIN_CONTOUR__2d(%3.4f, %3.4f, %s)\n",
(real)P1.x / (real)sp_globals.onepix,
(real)P1.y / (real)sp_globals.onepix,
outside? "outside": "inside");
#endif
sp_globals.x0_spxl = P1.x;
sp_globals.y0_spxl = P1.y;
}
#endif
#if INCL_2D
FUNCTION void line_2d(
GDECL
point_t P1)
{
#if DEBUG
printf("LINE_0(%3.4f, %3.4f)\n",
(real)P1.x / (real)sp_globals.onepix,
(real)P1.y / (real)sp_globals.onepix);
#endif
if (sp_globals.extents_running)
{
if (sp_globals.x0_spxl > sp_globals.bmap_xmax)
sp_globals.bmap_xmax = sp_globals.x0_spxl;
if (sp_globals.x0_spxl < sp_globals.bmap_xmin)
sp_globals.bmap_xmin = sp_globals.x0_spxl;
if (sp_globals.y0_spxl > sp_globals.bmap_ymax)
sp_globals.bmap_ymax = sp_globals.y0_spxl;
if (sp_globals.y0_spxl < sp_globals.bmap_ymin)
sp_globals.bmap_ymin = sp_globals.y0_spxl;
}
if (!sp_globals.intercept_oflo)
{
sp_draw_vector_to_2d(sp_globals.x0_spxl,
sp_globals.y0_spxl,
P1.x,
P1.y,
&sp_globals.y_band);
if (sp_globals.x_scan_active)
sp_draw_vector_to_2d(sp_globals.y0_spxl,
sp_globals.x0_spxl,
P1.y,
P1.x,
&sp_globals.x_band);
}
sp_globals.x0_spxl = P1.x;
sp_globals.y0_spxl = P1.y;
}
FUNCTION static void sp_draw_vector_to_2d(
GDECL
fix15 x0,
fix15 y0,
fix15 x1,
fix15 y1,
band_t GLOBALFAR *band)
{
register fix15 how_many_y;
register fix15 yc;
fix15 temp1;
fix15 temp2;
register fix31 dx_dy;
register fix31 xc;
fix15 y_pxl;
yc = (y0 + sp_globals.pixrnd) >> sp_globals.pixshift;
y_pxl = (y1 + sp_globals.pixrnd) >> sp_globals.pixshift;
if ((how_many_y = y_pxl - yc) == 0) return;
if (how_many_y < 0) yc--;
if (yc > band->band_max)
{
if (y_pxl > band->band_max) return;
how_many_y = y_pxl - (yc = band->band_max) - 1;
}
if (yc < band->band_min)
{
if (y_pxl < band->band_min) return;
how_many_y = y_pxl - (yc = band->band_min);
}
xc = (fix31)(x0 + sp_globals.pixrnd) << 16;
if ( (temp1 = (x1 - x0)) == 0)
{
dx_dy = 0L;
goto skip_calc;
}
dx_dy = ( (fix31)temp1 << 16 )/(fix31)(y1 - y0);
temp1 = (yc << sp_globals.pixshift) - y0 + sp_globals.pixrnd;
temp2 = (fix15)(MULT16(temp1,(fix15)(dx_dy >> 16)) >> 15);
if ( (temp2 != (fix15)0xFFFF) &&
(temp2 != 0x0000) )
{
if (ABS(temp1) < ABS((yc << sp_globals.pixshift) - y1 + sp_globals.pixrnd))
{
xc = (fix31)(x1 + sp_globals.pixrnd) << (16 - sp_globals.pixshift);
}
goto skip_calc;
}
xc += (fix31)temp1 * dx_dy;
dx_dy <<= sp_globals.pixshift;
skip_calc:
yc -= band->band_array_offset;
if (how_many_y < 0)
{
if ((how_many_y += yc + 1) < band->band_floor)
how_many_y = band->band_floor;
while(yc >= how_many_y)
{
temp1 = (fix15)(xc >> 16);
sp_add_intercept_2d(yc--,temp1);
xc -= dx_dy;
}
}
else
{
if ((how_many_y += yc) > band->band_ceiling)
how_many_y = band->band_ceiling;
while(yc < how_many_y)
{
temp1 = (fix15)(xc >> 16);
sp_add_intercept_2d(yc++,temp1);
xc += dx_dy;
}
}
}
#endif
#if INCL_2D
FUNCTION boolean end_char_2d()
{
fix31 xorg;
fix31 yorg;
#if INCL_CLIPPING
fix31 em_max, em_min, bmap_max, bmap_min;
#endif
#if DEBUG
printf("END_CHAR__2d()\n");
#endif
if (sp_globals.first_pass)
{
if (sp_globals.bmap_xmax >= sp_globals.bmap_xmin)
{
sp_globals.xmin = (sp_globals.bmap_xmin + sp_globals.pixrnd + 1) >> sp_globals.pixshift;
sp_globals.xmax = (sp_globals.bmap_xmax + sp_globals.pixrnd) >> sp_globals.pixshift;
}
else
{
sp_globals.xmin = sp_globals.xmax = 0;
}
if (sp_globals.bmap_ymax >= sp_globals.bmap_ymin)
{
#if INCL_CLIPPING
switch(sp_globals.tcb0.xtype)
{
case 1:
if (sp_globals.specs.flags & CLIP_TOP)
{
sp_globals.clip_ymin = (fix31)((fix31)EM_TOP * sp_globals.tcb0.yppo + ((1<<sp_globals.multshift)/2));
sp_globals.clip_ymin = sp_globals.clip_ymin >> sp_globals.multshift;
bmap_min = (sp_globals.bmap_ymin + sp_globals.pixrnd + 1) >> sp_globals.pixshift;
sp_globals.clip_ymin = -1 * sp_globals.clip_ymin;
if (bmap_min < sp_globals.clip_ymin)
sp_globals.ymin = sp_globals.clip_ymin;
else
sp_globals.ymin = bmap_min;
}
if (sp_globals.specs.flags & CLIP_BOTTOM)
{
sp_globals.clip_ymax = (fix31)((fix31)(-1 * EM_BOT) * sp_globals.tcb0.yppo + ((1<<sp_globals.multshift)/2));
sp_globals.clip_ymax = sp_globals.clip_ymax >> sp_globals.multshift;
bmap_max = (sp_globals.bmap_ymax + sp_globals.pixrnd) >> sp_globals.pixshift;
if (bmap_max < sp_globals.clip_ymax)
sp_globals.ymax = bmap_max;
else
sp_globals.ymax = sp_globals.clip_ymax;
}
sp_globals.clip_xmax = -sp_globals.xmin;
sp_globals.clip_xmin = ((sp_globals.set_width.x+32768L) >> 16) -
sp_globals.xmin;
break;
case 2:
if (sp_globals.specs.flags & CLIP_TOP)
{
sp_globals.clip_xmin = (fix31)((fix31)(-1 * EM_BOT) * sp_globals.tcb0.yppo + ((1<<sp_globals.multshift)/2));
sp_globals.clip_xmin = sp_globals.clip_xmin >> sp_globals.multshift;
sp_globals.clip_xmin = -1 * sp_globals.clip_xmin;
bmap_min = (sp_globals.bmap_xmin + sp_globals.pixrnd + 1) >> sp_globals.pixshift;
if (bmap_min > sp_globals.clip_xmin)
sp_globals.clip_xmin = bmap_min;
sp_globals.clip_xmin -= sp_globals.xmin;
}
if (sp_globals.specs.flags & CLIP_BOTTOM)
{
sp_globals.clip_xmax = (fix31)((fix31)EM_TOP * sp_globals.tcb0.yppo + ((1<<sp_globals.multshift)/2));
sp_globals.clip_xmax = sp_globals.clip_xmax >> sp_globals.multshift;
bmap_max = (sp_globals.bmap_xmax + sp_globals.pixrnd) >> sp_globals.pixshift;
if (bmap_max < sp_globals.clip_xmax)
sp_globals.xmax = bmap_max;
else
sp_globals.xmax = sp_globals.clip_xmax;
sp_globals.clip_ymax = 0;
if ((sp_globals.specs.flags & CLIP_TOP) &&
(sp_globals.ymax > sp_globals.clip_ymax))
sp_globals.ymax = sp_globals.clip_ymax;
sp_globals.clip_ymin = ((sp_globals.set_width.y+32768L) >> 16);
if ((sp_globals.specs.flags & CLIP_BOTTOM) &&
(sp_globals.ymin < sp_globals.clip_ymin))
sp_globals.ymin = sp_globals.clip_ymin;
sp_globals.clip_xmax -= sp_globals.xmin;
}
break;
case 3:
if (sp_globals.specs.flags & CLIP_TOP)
{
sp_globals.clip_xmin = (fix31)((fix31)EM_TOP * sp_globals.tcb0.yppo + ((1<<sp_globals.multshift)/2));
sp_globals.clip_xmin = sp_globals.clip_xmin >> sp_globals.multshift;
sp_globals.clip_xmin = -1 * sp_globals.clip_xmin;
bmap_min = (sp_globals.bmap_xmin + sp_globals.pixrnd + 1) >> sp_globals.pixshift;
if (bmap_min > sp_globals.clip_xmin)
sp_globals.clip_xmin = bmap_min;
sp_globals.clip_xmin -= sp_globals.xmin;
}
if (sp_globals.specs.flags & CLIP_BOTTOM)
{
sp_globals.clip_xmax = (fix31)((fix31)(-1 * EM_BOT) * sp_globals.tcb0.yppo + ((1<<sp_globals.multshift)/2));
sp_globals.clip_xmax = sp_globals.clip_xmax >> sp_globals.multshift;
bmap_max = (sp_globals.bmap_xmax + sp_globals.pixrnd) >> sp_globals.pixshift;
if (bmap_max < sp_globals.clip_xmax)
{
sp_globals.xmax = bmap_max;
sp_globals.clip_xmax = bmap_max;
}
else
sp_globals.xmax = sp_globals.clip_xmax;
sp_globals.clip_xmax -= sp_globals.xmin;
}
sp_globals.clip_ymax = ((sp_globals.set_width.y+32768L) >> 16);
if ((sp_globals.specs.flags & CLIP_TOP) &&
(sp_globals.ymax > sp_globals.clip_ymax))
sp_globals.ymax = sp_globals.clip_ymax;
sp_globals.clip_ymin = 0;
if ((sp_globals.specs.flags & CLIP_BOTTOM) &&
(sp_globals.ymin < sp_globals.clip_ymin))
sp_globals.ymin = sp_globals.clip_ymin;
break;
default:
if (sp_globals.specs.flags & CLIP_TOP)
{
sp_globals.clip_ymax = (fix31)((fix31)EM_TOP * sp_globals.tcb0.yppo + ((1<<sp_globals.multshift)/2));
sp_globals.clip_ymax = sp_globals.clip_ymax >> sp_globals.multshift;
bmap_max = (sp_globals.bmap_ymax + sp_globals.pixrnd) >> sp_globals.pixshift;
if (bmap_max > sp_globals.clip_ymax)
sp_globals.ymax = bmap_max;
else
sp_globals.ymax = sp_globals.clip_ymax;
}
if (sp_globals.specs.flags & CLIP_BOTTOM)
{
sp_globals.clip_ymin = (fix31)((fix31)(-1 * EM_BOT) * sp_globals.tcb0.yppo + ((1<<sp_globals.multshift)/2));
sp_globals.clip_ymin = sp_globals.clip_ymin >> sp_globals.multshift;
sp_globals.clip_ymin = - sp_globals.clip_ymin;
bmap_min = (sp_globals.bmap_ymin + sp_globals.pixrnd + 1) >> sp_globals.pixshift;
if (bmap_min < sp_globals.clip_ymin)
sp_globals.ymin = sp_globals.clip_ymin;
else
sp_globals.ymin = bmap_min;
}
sp_globals.clip_xmin = -sp_globals.xmin;
sp_globals.clip_xmax = ((sp_globals.set_width.x+32768L) >> 16) -
sp_globals.xmin;
break;
}
if ( !(sp_globals.specs.flags & CLIP_TOP))
#endif
sp_globals.ymax = (sp_globals.bmap_ymax + sp_globals.pixrnd) >> sp_globals.pixshift;
#if INCL_CLIPPING
if ( !(sp_globals.specs.flags & CLIP_BOTTOM))
#endif
sp_globals.ymin = (sp_globals.bmap_ymin + sp_globals.pixrnd + 1) >> sp_globals.pixshift;
}
else
{
sp_globals.ymin = sp_globals.ymax = 0;
}
if (sp_globals.tcb.xmode == 0)
xorg = (((fix31)sp_globals.xmin << 16) + (sp_globals.rnd_xmin << sp_globals.poshift));
else
if (sp_globals.tcb.xmode == 1)
xorg = (((fix31)sp_globals.xmin << 16) - (sp_globals.rnd_xmin << sp_globals.poshift)) ;
else
xorg = (fix31)sp_globals.xmin << 16;
if (sp_globals.tcb.ymode == 2)
yorg = (((fix31)sp_globals.ymin << 16) + (sp_globals.rnd_xmin << sp_globals.poshift));
else
if (sp_globals.tcb.ymode == 3)
yorg = (((fix31)sp_globals.ymin << 16) - (sp_globals.rnd_xmin << sp_globals.poshift));
else
yorg = (fix31)sp_globals.ymin << 16;
open_bitmap(sp_globals.set_width.x, sp_globals.set_width.y, xorg, yorg,
sp_globals.xmax - sp_globals.xmin, sp_globals.ymax - sp_globals.ymin);
if (sp_globals.intercept_oflo)
{
sp_globals.y_band.band_min = sp_globals.ymin;
sp_globals.y_band.band_max = sp_globals.ymax;
sp_globals.x_scan_active = FALSE;
sp_globals.no_x_lists = 0;
init_intercepts_out();
sp_globals.first_pass = FALSE;
sp_globals.extents_running = FALSE;
return FALSE;
}
else
{
sp_proc_intercepts_2d();
close_bitmap();
return TRUE;
}
}
else
{
if (sp_globals.intercept_oflo)
{
reduce_band_size_out();
init_intercepts_out();
return FALSE;
}
else
{
sp_proc_intercepts_2d();
if (next_band_out())
{
init_intercepts_out();
return FALSE;
}
close_bitmap();
return TRUE;
}
}
}
#endif
#if INCL_2D
FUNCTION static void sp_add_intercept_2d(
GDECL
fix15 y,
fix15 x)
{
register fix15 from;
register fix15 to;
#if DEBUG
if ((y >= MAX_INTERCEPTS) || (y < 0))
{
printf("Intercept out of table!!!!! (%d)\n",y);
return;
}
if (y >= sp_globals.no_y_lists)
{
printf(" Add x intercept(%2d, %f)\n",
y + sp_globals.x_band.band_min - sp_globals.no_y_lists,
(real)x/(real)sp_globals.onepix);
if (y > (sp_globals.no_x_lists + sp_globals.no_y_lists))
{
printf(" Intercept too big for band!!!!!\007\n");
return;
}
}
else
{
printf(" Add y intercept(%2d, %f)\n", y + sp_globals.y_band.band_min,(real)x/(real)sp_globals.onepix);
}
if (y < 0)
{
printf(" Intecerpt less than 0!!!\007\n");
return;
}
#endif
sp_intercepts.car[sp_globals.next_offset] = x;
from = y;
while( (to = sp_intercepts.cdr[from]) >= sp_globals.first_offset)
{
if (x <= sp_intercepts.car[to])
goto insert_element;
from = to;
}
insert_element:
sp_intercepts.cdr[from] = sp_globals.next_offset;
sp_intercepts.cdr[sp_globals.next_offset] = to;
if (++sp_globals.next_offset >= MAX_INTERCEPTS)
{
sp_globals.intercept_oflo = TRUE;
sp_globals.next_offset = sp_globals.first_offset;
}
}
#endif
#if INCL_2D
FUNCTION static void sp_proc_intercepts_2d()
{
register fix15 i;
register fix15 from, to;
register fix15 y;
register fix15 scan_line;
fix15 local_bmap_xmin;
fix15 local_bmap_xmax;
fix15 first_y, last_y;
fix15 j,k;
#if INCL_CLIPPING
if ((sp_globals.specs.flags & CLIP_LEFT) != 0)
clipleft = TRUE;
else
clipleft = FALSE;
if ((sp_globals.specs.flags & CLIP_RIGHT) != 0)
clipright = TRUE;
else
clipright = FALSE;
if (clipleft || clipright)
{
xmax = sp_globals.clip_xmax << sp_globals.pixshift;
xmin = sp_globals.clip_xmin << sp_globals.pixshift;
}
if (!clipright)
xmax = ((sp_globals.set_width.x+32768L) >> 16);
#endif
if (sp_globals.x_scan_active)
{
first_y = sp_globals.x_band.band_floor;
last_y = sp_globals.x_band.band_ceiling;
for (y = first_y; y != last_y; y++)
{
i = sp_intercepts.cdr[y];
while (i != 0)
{
from = sp_intercepts.car[i];
j = i;
i = sp_intercepts.cdr[i];
if (i == 0)
{
#if DEBUG
printf("****** proc_intercepts: odd number of intercepts in x list\n");
#endif
break;
}
to = sp_intercepts.car[i];
k = sp_intercepts.cdr[i];
if (((to >> sp_globals.pixshift) >= (from >> sp_globals.pixshift)) &&
((to - from) < (sp_globals.onepix + 1)))
{
from = ((fix31)to + (fix31)from - (fix31)sp_globals.onepix) >> (sp_globals.pixshift + 1);
if (from > sp_globals.y_band.band_max)
from = sp_globals.y_band.band_max;
if ((from -= sp_globals.y_band.band_min) < 0)
from = 0;
to = ((y - sp_globals.x_band.band_floor + sp_globals.x_band.band_min)
<< sp_globals.pixshift)
+ sp_globals.pixrnd;
sp_intercepts.car[j] = to;
sp_intercepts.car[i] = to + sp_globals.onepix;
sp_intercepts.cdr[i] = sp_intercepts.cdr[from];
sp_intercepts.cdr[from] = j;
}
i = k;
}
}
}
#if DEBUG
printf("\nIntercept lists:\n");
#endif
if ((first_y = sp_globals.y_band.band_max) >= sp_globals.ymax)
first_y = sp_globals.ymax - 1;
if ((last_y = sp_globals.y_band.band_min) < sp_globals.ymin)
last_y = sp_globals.ymin;
last_y -= sp_globals.y_band.band_array_offset;
local_bmap_xmin = sp_globals.xmin << sp_globals.pixshift;
local_bmap_xmax = (sp_globals.xmax << sp_globals.pixshift) + sp_globals.pixrnd;
#if DEBUG
scan_line = sp_globals.ymax - first_y - 1;
for (y = first_y - sp_globals.y_band.band_min; y >= last_y; y--, scan_line++)
{
i = y;
while ((i = sp_intercepts.cdr[i]) != 0)
{
if ((from = sp_intercepts.car[i] - local_bmap_xmin) < 0)
from = 0;
i = sp_intercepts.cdr[i];
if (i == 0)
{
printf("****** proc_intercepts: odd number of intercepts\n");
break;
}
if ((to = sp_intercepts.car[i]) > sp_globals.bmap_xmax)
to = sp_globals.bmap_xmax - local_bmap_xmin;
else
to -= local_bmap_xmin;
printf(" Y = %2d (scanline %2d): %3.4f %3.4f:\n",
y + sp_globals.y_band.band_min,
scan_line,
(real)from / (real)sp_globals.onepix,
(real)to / (real)sp_globals.onepix);
}
}
#endif
scan_line = sp_globals.ymax - first_y - 1;
for (y = first_y - sp_globals.y_band.band_min; y >= last_y; y--, scan_line++)
{
i = y;
while ((i = sp_intercepts.cdr[i]) != 0)
{
if ((from = sp_intercepts.car[i] - local_bmap_xmin) < 0)
from = 0;
i = sp_intercepts.cdr[i];
if ((to = sp_intercepts.car[i]) > local_bmap_xmax)
to = sp_globals.bmap_xmax - local_bmap_xmin;
else
to -= local_bmap_xmin;
#if INCL_CLIPPING
if (clipleft)
{
if (to <= xmin)
continue;
if (from < xmin)
from = xmin;
}
if (clipright)
{
if (from >= xmax)
continue;
if (to > xmax)
to = xmax;
}
#endif
if ( (to - from) <= sp_globals.onepix)
{
from = (to + from - sp_globals.onepix) >> (sp_globals.pixshift + 1);
set_bitmap_bits(scan_line, from, from + 1);
}
else
{
set_bitmap_bits(scan_line, from >> sp_globals.pixshift, to >> sp_globals.pixshift);
}
}
}
}
#endif