// ScanConvert2.bcpl - overflow from ScanConvert.bcpl

// last modified by Butterfield, April 17, 1980 10:53 AM
// - this file is Joe’s changed version - 4/17/80

//outgoing procedures
external [ ScanColor]

//outgoing statics
external
ScanColorTable//Pointer to matrix for this color
static
ScanColorTable

//incoming procedures
external
[
//OS
MoveBlock
//SCANCHARS
ScanCharColor
//SCANCONVERT
ScanColorPassValue
//ALLOC
FSGet
]

//incoming statics
external
[ currentScanColor;ScanHalftoneDot;colorCache]

// The table generated in ScanHalftoneDot gives
// turn-on sequences: the entry labeled 0 should be made
// black first. The parameter ’color’ is 0 for black (all bits on),
// 255 for while (all bits off).

// The reason for the "currentScanColor" flag is that we often get
// into situations in processing sync entries where successive
// colors are the same: there is, somewhere on the page, an
// intervening color, but not in this area.

let ScanColor(colorV) be
[
let color=ScanColorPassValue(colorV)
if color eq currentScanColor then return
currentScanColor=color

ScanColorTable=colorCache!color

if ScanColorTable eq 0 then //make it up
[ let nWords=ScanHalftoneDot!0
let cycleAmount=ScanHalftoneDot!1
let nLines=ScanHalftoneDot!2
let lenTurnOn=ScanHalftoneDot!3
ScanColorTable=FSGet(nWords*(nLines+15)+3)
if ScanColorTable eq 0 then //allocate failed
for i=1 to 254 do if colorCache!i ne 0 then
[ ScanColorTable=colorCache!i;colorCache!i=0;break]
colorCache!color=ScanColorTable
MoveBlock(ScanColorTable,ScanHalftoneDot,3)
let val=nil
let c=128-color
let xTurnOn=0
for i=0 to nLines-1 do
[ let line=ScanHalftoneDot+4+i*lenTurnOn
for j=0 to nWords-1 do
[for b=0 to 15 do
[val=val lshift 1
if c gr line!xTurnOn then val=val+1
xTurnOn=xTurnOn+1
if xTurnOn ge lenTurnOn then xTurnOn=0
]
ScanColorTable!(3+j+i*nWords)=val
]
]
//and replicate for additional 15 lines
let sourceLine=1
let source=ScanColorTable+3
let dest = source+nWords*nLines
let thisCycle=cycleAmount
for i=1 to 15 do
[MoveBlock(dest,source+thisCycle,nWords-thisCycle)
MoveBlock(dest+nWords-thisCycle,source,thisCycle)
dest=dest+nWords
test sourceLine eq nLines then
[sourceLine=1;source=ScanColorTable+3
thisCycle=(thisCycle+cycleAmount) rem nWords
]
or [ sourceLine=sourceLine+1;source=source+nWords]
]
] //end of "test color eq 0 then .. or [ ]

ScanCharColor(color)//Set up character handler for this color
]