
L_UP    EQU  -1
L_DOWN  EQU   1

SeedX    DW    ?
SeedY    DW    ?
LD       DW    ?
PrevXL   DW    ?
PrevXR   DW    ?

lx       DW    ?
ly       DW    ?
lxl      DW    ?
lxr      DW    ?
lv       DW    ?
BorderValue  DB  255
FillValue    DB  63

;LineAdjFill( SeedX, SeedY, D, PrevXL, PrevXR )
;int     SeedX,SeedY;            /* seed for current row of pixels */
;int     D;                      /* direction searched to find current row */
;int     PrevXL,PrevXR;          /* endpoints of previous row of pixels */
PROC FILLIT
        MOV     AX,50
        MOV     BX,L_UP
        _ES     0A000h
        MOV     BP,SP
        PUSH    AX
        PUSH    AX
        PUSH    BX
        PUSH    AX
        PUSH    AX
ujrahiv:
        CMP     BP,SP
        JE      kiinnen
        CALL    LINEADJFILL
        JMP     ujrahiv
kiinnen:
        RET
ENDP

PROC LINEADJFILL
        POP     PrevXR
        POP     PrevXL
        POP     LD
        POP     SeedY
        POP     SeedX

   ;     int     x,y;
   ;     int     xl,xr;
   ;     int     v;
        ;y = SeedY;              /* initialize to seed coordinates */
        MOV  lY,SeedY
        ;xl = SeedX;
        MOV  lxl,SeedX
        MOV  lxr,SeedX
        ;xr = SeedX;

        MOV     AX,320
        MUL     ly
        ADD     lxl
        MOV     DI,AX
        STD
ben1:
        LODSB
        CMP     AL,FillValue
        JE      ki1
        CMP     AL,BorderValue
        JE      ki1
        DEC     lxl
        JMP     ben1
ki1:
        CLD
;        ScanLeft( &xl, &y );    /* determine endpoints of seed line segment */
;        ScanRight( &xr, &y );
        MOV     AX,320
        MUL     ly
        ADD     lxr
        MOV     DI,AX
ben1:
        LODSB
        CMP     AL,FillValue
        JE      ki1
        CMP     AL,BorderValue
        JE      ki1
        INC     lxr
        JMP     ben1
ki1:

        ;Line( xl, y, xr, y, FillValue );        /* fill line with FillValue */
        MOV     AX,320
        MUL     ly
        ADD     lxl
        MOV     DI,AX
        MOV     CX,lxr
        SUB     CX,lxl
        INC     CX
        MOV     AL,FillValue
        REP     STOSB

;/* find and fill adjacent line segments in same direction */
;        MOV  lx,
        for (x=xl; x<=xr; x++)          /* inspect adjacent rows of pixels */
        {
          v = ReadPixel( x, y+D );
          if ( (v!=BorderValue) && (v!=FillValue) )
            x = LineAdjFill( x, y+D, D, xl, xr );
        }

/* find and fill adjacent line segments in opposite direction */

        for (x=xl; x<PrevXL; x++)
        {
          v = ReadPixel( x, y-D );
          if ( (v!=BorderValue) && (v!=FillValue) )
            x = LineAdjFill( x, y-D, -D, xl, xr );
        }

        for (x=PrevXR; x<xr; x++)
        {
          v = ReadPixel( x, y-D );
          if ( (v!=BorderValue) && (v!=FillValue) )
            x = LineAdjFill( x, y-D, -D, xl, xr );
        }

        return( xr );
}


ScanLeft( x, y )
int     *x,*y;
{
        int     v;


        do
        {
          --(*x);                       /* move left one pixel */
          v = ReadPixel( *x, *y );      /* determine its value */
        }
        while ( (v!=BorderValue) && (v!=FillValue) );

        ++(*x);         /* x-coordinate of leftmost pixel in row */
}


ScanRight( x, y )
int     *x,*y;
{
        int     v;


        do
        {
          ++(*x);                       /* move right one pixel */
          v = ReadPixel( *x, *y );      /* determine its value */
        }
        while ( (v!=BorderValue) && (v!=FillValue) );

        --(*x);         /* x-coordinate of rightmost pixel in row */
}

