CDColorPDPlot.mesa
Copyright © 1983, 1985 by Xerox Corporation. All rights reserved.
written by E. McCreight, August 1, 1983 2:00 PM
Last Edited by: McCreight, November 23, 1983 4:58 pm
Last Edited by: Jacobi, October 27, 1983 12:36 pm
Last Edited by: Jacobi, April 11, 1985 3:21:48 pm PST
Last Edited by: Kimr, October 29, 1984 9:49:48 am PST
DIRECTORY
CD,
CDBasics,
CDCommandOps,
CDExtras,
CDIO,
CDViewer,
CDPolygons,
CDMenus,
CDOps,
CDOrient,
CDProperties,
CDSequencer,
CDValue,
CGArea,
CGClipper,
CGReducer,
CornerStitching,
Graphics,
GraphicsBasic,
IO,
PDFileFormat,
PDFileWriter,
Process,
Real,
Rope,
TerminalIO,
ViewerClasses,
ViewerSpecs;
CDColorPDPlot: CEDAR PROGRAM
IMPORTS CD, CDBasics, CDCommandOps, CDExtras, CDIO, CDMenus, CDValue, CDViewer, CDOps, CDOrient, CDPolygons, CDProperties, CDSequencer, CGArea, CGClipper, CGReducer, CornerStitching, IO, PDFileWriter, Process, Real, TerminalIO =
BEGIN
--handling colors
Toner: TYPE = PDFileFormat.Toner; -- {black, cyan, magenta, yellow};
TonerKeys: TYPE = ARRAY Toner OF REF;
LoadArray: TYPE = ARRAY Toner OF PDFileWriter.LoadReference;
ColorDescription: TYPE = ARRAY CD.Layer OF REF LoadArray ← ALL[NIL];
whiteLoadRef: PDFileWriter.LoadReference = LAST[PDFileWriter.LoadReference];
-- used to identify tiles without toner so that you can avoid recording them
tonerToKeyKeys: REF TonerKeys = NEW[TonerKeys ← [
$CDxColorPDPlotBlack, $CDxColorPDPlotCyan, $CDxColorPDPlotMagenta, $CDxColorPDPlotYellow
]];
--gives a key to get the keys to get the color stipple as property of the layers
--device description
deviceCode: PDFileFormat.DeviceCode ← last;
sRes: CARDINAL ← 200; -- resolution (slow directition), pixels / inch
fRes: CARDINAL ← 200; -- resolution (fast direction), pixels / inch
scanLineWidth: CARDINAL ← 8000; --for the wide-bed Versatec, length in the "fast" direction;
toners: PDFileWriter.TonerSet ← ALL[TRUE];
leftOverMode: BOOLFALSE;
bandSSize: CARDINAL ← 64; -- number chosen because of buffer-size of versatec
overLap: CARDINAL ← 300; -- number of pixels by which to overlap strips
maxPixPerLambda: REAL ← 20; -- maximum # of pixels to make lambda
paged: BOOLFALSE;
pageSlowSize: INT ← -1; --only interesting if paged
stippleKey: ATOM ← $CDxVersatec;
tonerToKey: REF TonerKeys ← NIL;
--gives a key to get the color stipple as property of the layers
ColorVersatec: PROC [] =
BEGIN
deviceCode ← last; --does not yet has an assigned value, use "last"
sRes ← 200; -- resolution (slow directition), pixels / inch
fRes ← 200; -- resolution (fast direction), pixels / inch
scanLineWidth ← 8000; --for the wide-bed Versatec, length in the "fast" direction;
toners ← ALL[TRUE];
bandSSize ← 64; -- number chosen because of buffer-size of versatec
overLap ← 300; -- number of pixels by which to overlap strips
maxPixPerLambda ← 20; -- maximum # of pixels to make lambda
paged ← FALSE;
leftOverMode ← FALSE;
stippleKey ← $CDxVersatec;
TerminalIO.WriteRope["color versatec"];
END;
InkJet: PROC [] =
BEGIN
deviceCode ← last;
sRes ← 120; -- resolution (slow directition), pixels / inch
fRes ← 120; -- resolution (fast direction), pixels / inch
scanLineWidth ← 120/10*75; --
toners ← ALL[TRUE];
bandSSize ← 64; --
overLap ← 30; -- number of pixels by which to overlap strips
maxPixPerLambda ← 20; -- maximum # of pixels to make lambda
paged ← FALSE;
leftOverMode ← FALSE;
stippleKey ← $CDxVersatec;
TerminalIO.WriteRope["ink jet"];
END;
Puffin: PROC [] =
BEGIN
deviceCode ← PDFileFormat.DeviceCode[puffin];
sRes ← 384; -- resolution (slow directition), pixels / inch
fRes ← 384; -- resolution (fast direction), pixels / inch
scanLineWidth ← fRes * 17 / 2;
toners ← [FALSE, TRUE, TRUE, TRUE];
bandSSize ← 16; --
overLap ← 30; -- number of pixels by which to overlap strips
maxPixPerLambda ← 20; -- maximum # of pixels to make lambda
paged ← TRUE;
pageSlowSize ← sRes*11;
leftOverMode ← TRUE;
stippleKey ← $CDxPuffin;
TerminalIO.WriteRope["Puffin"];
END;
--state of the plot
debugging: BOOLFALSE;
technologyPropertyKey: REF ← $CDxLastTechnology; --a property of the stippleKey
abortPlot: REF BOOLNEW[BOOLFALSE];
PlotStateRef: TYPE = REF PlotState;
PlotState: TYPE = RECORD [
tes: ARRAY CD.Layer OF REF CornerStitching.Tesselation ← ALL[NIL],
scale: REAL ← 1.0, -- pixels per CD.DesignNumber
totalPlotClip, bandClip: CD.DesignRect ← [0, 0, 0, 0],
rectangles in design space touching all geometry in the plot, or this band
pdState: PDFileWriter.PDState,
colorLoads: REF ColorDescription,
anouncedLayer: INT ← -1
];
pdx, pdy: CARDINAL ← 0;  -- origin of band in pixel coords
cdx, cdy: INT ← 0;    -- origin of band in chipndale coords
xoffset, yoffset: INT ← 0; -- offset in transforming between coord systems
plotScale: REAL;
-- scale factor between systems, is negative for y-axis due to reversed direction of increasing values (when compared with pixel coordinates)
imageSSize: REAL;
imageFSize: CARDINAL;
ps: PlotStateRef;
CheckStipples: PROC [technology: CD.Technology] =
BEGIN
x: REF ← CDValue.Fetch[technology, stippleKey];
WITH x SELECT FROM
r: Rope.ROPE => {
TerminalIO.WriteRope["color stipples used: ["];
TerminalIO.WriteRope[r];
TerminalIO.WriteRope["]\n"];
}
ENDCASE => {
TerminalIO.WriteRope["**color stipples are not defined\n"];
ERROR ABORTED
};
IF technology.key#CDProperties.GetPropFromAtom[stippleKey, technologyPropertyKey] THEN
TerminalIO.WriteRope["**Warning: another technology registered in the meantime; the technology independent stipples are redefined\n"];
tonerToKey ← NEW[TonerKeys ← ALL[NIL]];
FOR t: PDFileWriter.Toner IN PDFileWriter.Toner DO
tonerToKey^[t] ← CDProperties.GetPropFromAtom[stippleKey, tonerToKeyKeys[t]];
IF ~toners[t] OR tonerToKey^[t]=NIL THEN {
toners[t] ← FALSE;
tonerToKey^[t] ← NEW[INT];
}
ENDLOOP;
END;
ProtectedPlotDesign: PROC [comm: CDSequencer.Command] =
BEGIN
design: CD.Design = comm.design;
plotClip: CD.DesignRect;
plotSize: CD.DesignPosition;
s: IO.STREAMNIL;
strips: INT;
TerminalIO.WriteRope["Starting Color plot for "];
SELECT comm.a FROM
$VersatecColorPlot => ColorVersatec[];
$InkJetColorPlot => InkJet[];
$PuffinColorPlot => Puffin[];
ENDCASE => ERROR;
TerminalIO.WriteLn[];
CheckStipples[design.technology];
SELECT TerminalIO.RequestSelection["Plot", LIST["complete design", "rectangle"]] FROM
2 => {
plotClip ← CDBasics.ToRect[comm.pos, comm.sPos];
TerminalIO.WriteRope["plot rectangle\n"];
};
ENDCASE => {
plotClip ← CDExtras.BoundingBox[comm.design];
TerminalIO.WriteRope["plot all\n"];
};
plotSize ← CDBasics.SizeOfRect[plotClip];
IF plotSize.x<=0 OR plotSize.y<=0 THEN {
TerminalIO.WriteRope["**cannot plot empty area\n"];
RETURN
};
TRUSTED {Process.SetPriority[Process.priorityBackground]};
IF paged THEN strips ← 1
ELSE {
strips ← TerminalIO.RequestInt["How many vertical strips? [1..10] "];
strips ← MAX[1, MIN[10, strips]];
};
BEGIN
ENABLE { -- for ERRORs
UNWIND => {
s ← AbortFile[s];
CDViewer.RemoveArrow[design];
TerminalIO.WriteRope[" ** plot aborted ** "]
};
};
dr: CD.DrawRef = CD.CreateDrawRef[design];
scale: REALMIN[
maxPixPerLambda/CD.lambda,
(REAL[scanLineWidth-overLap]*strips+overLap) / plotSize.x
];
--Center the x range of the selected area of the design on the plotter bed, with at most maxPixPerLambda pixels per lambda. If multiple strips are called for, overlap adjacent ones by "overLap" pixels.
-- total number of pixels across plot = REAL[scanLineWidth]+REAL[scanLineWidth-overLap]*(strips-1))
ps ← NEW[PlotState ← [
scale: scale,
totalPlotClip: plotClip,
colorLoads: NEW[ColorDescription ← ALL[NIL]]
]];
IF paged THEN {
scale ← MIN[scale, REAL[pageSlowSize]/plotSize.x];
};
imageFSize ← scanLineWidth;
imageSSize ← plotSize.y*scale; -- length of plot <in slow direction> in pixels
plotScale ← scale;
IF debugging THEN {
TerminalIO.WriteRope[" Scale factor (pixels/design#)*100 --> "];
TerminalIO.WriteInt[Real.RoundLI[plotScale*100]];
TerminalIO.WriteLn[];
};
dr.minimalSize ← 0;
dr.stopFlag ← abortPlot;
dr.drawRect ← dr.saveRect ← NoteRectangle;
dr.drawContext ← DrawContext;
dr.contextFilter ← contextFilter;
dr.devicePrivate ← ps;
FOR strip: INT IN [0..strips) DO
-- Determine clip rectangle for strip
clip: CD.DesignRect = [
x1: plotClip.x1+strip*(plotSize.x/strips),
y1: plotClip.y1,
x2: plotClip.x1+(strip+1)*(plotSize.x/strips)+
Real.Fix[overLap/scale]+1,
y2: plotClip.y2
];
localFileName: Rope.ROPE = CDIO.MakeName[
wDir: "///temp/",
base: "plot",
modifier: IO.PutFR["%d", IO.int[strip+1]],
ext: "pd"
];
TerminalIO.WriteRope["Recording strip on file "];
TerminalIO.WriteRope[localFileName];
TerminalIO.WriteLn[];
ps.pdState ← PDFileWriter.Create[
fileName: localFileName,
deviceCode: deviceCode,
sResolution: sRes,
fResolution: fRes,
imageSSize: (IF paged THEN pageSlowSize ELSE Real.RoundC[imageSSize + 1]),
imageFSize: imageFSize,
bandSSize: bandSSize,
leftOverMode: leftOverMode
];
ps.colorLoads^ ← ALL[NIL];
PDFileWriter.StartImage[pdState: ps.pdState, toners: toners];
-- For each band in the strip
FOR topLine: INT ← 0, topLine+bandSSize WHILE topLine<imageSSize DO
x1, y1, x2, y2: REAL;
dc: CD.DesignRect;
IF abortPlot^ THEN GOTO AbortPlot;
-- Determine coordinate transformations
pdx ← 0;
pdy ← topLine;
cdx ← clip.x1;
cdy ← clip.y2 - Real.Fix[topLine*(1./scale)];
xoffset ← pdx - Real.Fix[cdx*scale];
yoffset ← pdy - Real.Fix[-cdy*scale];
-- (negative scale to account for CD increasing y north, PD increasing y going south)
x1 ← (-1-xoffset) / scale;
y1 ← -(pdy+bandSSize+1-yoffset) / scale;
x2 ← (scanLineWidth-xoffset) / scale;
y2 ← -(pdy-yoffset) / scale;
dc ← CDBasics.NormalizeRect[[x1: Real.Fix[x1], y1: Real.Fix[y1], x2: Real.Fix[x2], y2: Real.Fix[y2]]] ;
ps.bandClip ← dr.interestClip ← [
x1: MAX[plotClip.x1, dc.x1-1],
y1: MAX[plotClip.y1, dc.y1-1],
x2: MIN[plotClip.x2, dc.x2+1],
y2: MIN[plotClip.y2, dc.y2+1]
];
-- rectangle in design space that ChipNDale can use to clip its recursive drawing
IF debugging THEN {
TerminalIO.WriteRope["\n pdy: "];
TerminalIO.WriteInt[pdy];
TerminalIO.WriteRope["\n CD band: "];
TerminalIO.WriteInt[ps.bandClip.x1];
TerminalIO.WriteInt[ps.bandClip.y1];
TerminalIO.WriteInt[ps.bandClip.x2];
TerminalIO.WriteInt[ps.bandClip.y2];
TerminalIO.WriteLn[]
};
--Display current band under consideration to pacify user
CDViewer.ShowArrow[design: design, pos: [
x: (dr.interestClip.x1+dr.interestClip.x2)/2,
y: (dr.interestClip.y1+dr.interestClip.y2)/2]
];
-- clear previous tessalations
ps.anouncedLayer ← -1;
FOR l: CD.Layer IN CD.Layer DO
IF ps.tes[l]#NIL THEN
CornerStitching.ChangeRect[plane: ps.tes[l], rect: CDBasics.universe, newValue: NIL];
ENDLOOP;
CDOps.DrawDesign[design, dr]; -- build tesselations of the relevant design rectangle
AnalyzeTesselations[ps]; --will actually draw
TerminalIO.WriteRope["."];
ENDLOOP; -- for each band in the strip
PDFileWriter.EndPage[ps.pdState];
PDFileWriter.Close[ps.pdState];
TerminalIO.WriteRope["*\n"];
ENDLOOP; -- for each strip
TerminalIO.WriteRope[" finished plot(s)\n"];
CDViewer.RemoveArrow[design: design];
EXITS
AbortPlot => {
s ← AbortFile[s];
CDViewer.RemoveArrow[design];
TerminalIO.WriteRope[" ** plot aborted ** "];
};
END; -- enable
END; -- PlotDesign
NoteRectangle: PROC [r: CD.DesignRect, l: CD.Layer, pr: CD.DrawRef] =
BEGIN
ps: PlotStateRef = NARROW[pr.devicePrivate];
IF CDBasics.NonEmpty[r] THEN {
IF ps.tes[l]=NIL THEN ps.tes[l] ← CornerStitching.NewTesselation[];
ps.tes[l].ChangeRect[rect: r, newValue: $covered];
};
END;
SetColor: PROC [ps: PlotStateRef, lev: CD.Layer] =
BEGIN
Stipple16: TYPE = ARRAY[0..16) OF CARDINAL;
Stipple8: TYPE = ARRAY[0..8) OF [0..256);
Stipple4: TYPE = ARRAY[0..4) OF [0..16);
ToTexture: PROC [pattern: REF ANY] RETURNS [texture: Stipple16] =
--tries to convert pattern to a texture stipple
BEGIN
IF pattern=NIL THEN RETURN[Stipple16[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]; -- should not occur
WITH pattern SELECT FROM
s16: REF Stipple16 => texture ← s16^;
s8: REF Stipple8 =>
FOR i: [0..8) IN [0..8) DO
texture[i] ← texture[i+8] ← (256+1)*s8[i];
ENDLOOP;
s4: REF Stipple4 =>
FOR i: [0..4) IN [0..4) DO
texture[i] ← texture[i+4] ← texture[i+8] ← texture[i+12] ← s4[i]*1111H;
ENDLOOP;
ENDCASE => RETURN[Stipple16[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0FFFFH]]; -- error texture
RETURN [texture]
END; -- ToTexture
MakeLoadref: PROC [pattern: REF ANY] RETURNS [loadRef: PDFileWriter.LoadReference] =
TRUSTED BEGIN
texture: Stipple16 ← ToTexture[pattern];
loadRef ← PDFileWriter.LoadContiguousColorTile[pdState: ps.pdState, phase: 0, sMin: 0, fMin: 0, sSize: 16, fSize: 16, bitsPtr: @texture];
END; -- MakeLoadref
MakeLoad: PROC [ps: PlotStateRef, lev: CD.Layer] =
--makes the load if it does not already exist
BEGIN
IF ps.colorLoads^[lev]=NIL THEN {
ps.colorLoads^[lev] ← NEW[LoadArray];
FOR toner: Toner IN Toner DO
tex: REF ← CDProperties.GetPropFromLayer[from: lev, prop: tonerToKey[toner]];
IF tex=NIL THEN ps.colorLoads^[lev][toner] ← whiteLoadRef
ELSE ps.colorLoads^[lev][toner] ← MakeLoadref[tex]
ENDLOOP;
};
END; --MakeLoad
--SetColor
IF ps.anouncedLayer=lev THEN RETURN;
IF ps.colorLoads^[lev]=NIL THEN MakeLoad[ps, lev];
FOR toner: Toner IN Toner DO
IF ps.colorLoads^[lev][toner] = whiteLoadRef THEN
PDFileWriter.SetColorOff[ps.pdState, toner]
ELSE
PDFileWriter.SetColorTile[ps.pdState, toner, ps.colorLoads^[lev][toner], transparent];
ENDLOOP;
ps.anouncedLayer ← lev
END;
contextFilter: REF CD.ContextFilter = NEW[CD.ContextFilter←ALL[[doit: TRUE]]];
PlotPolygon: PROC [ob: CD.ObPtr, pos: CD.DesignPosition, orient: CD.Orientation] =
BEGIN
OutputTrapezoid: PROCEDURE [xbotL: REAL, xbotR: REAL, ybot: REAL, xtopL: REAL, xtopR: REAL, ytop: REAL] =
--lower left x, lower right x, lower y, upper left x, upper right x, upper y
BEGIN
--global variables ps, xoffset, yoffset, plotScale, imageFSize, imageSSize, pdy
--x-Fast
--y-Slow
PDFileWriter.MaskTrapezoid[
pdState: ps.pdState,
sMin: Real.RoundC[ybot],
sSize: Real.RoundC[ytop-ybot],
fMin: Real.RoundC[xbotL],
fSize: Real.RoundC[xbotR-xbotL],
fMinLast: Real.RoundC[xtopL],
fSizeLast: Real.RoundC[xtopR-xtopL]
];
END; --OutputTrapezoid
--PlotPolygon
pp: CDPolygons.PolygonPtr = NARROW[ob.specificRef];
polygon: CGReducer.Ref = CGReducer.New[size: 8];
tiling: CGArea.Ref = CGArea.New[size: 4];
clipRef: CGClipper.Ref = CGClipper.New[size: 4];
clip: GraphicsBasic.Box ← [
xmin: 0,
ymin: pdy,
xmax: MIN[xoffset + ps.bandClip.x2*plotScale, imageFSize],
ymax: pdy+bandSSize
];
CGClipper.SetBox[self: clipRef, box: clip];
CGClipper.Load[self: clipRef, reducer: polygon];
FOR p: LIST OF CD.DesignPosition ← pp.points, p.rest WHILE p#NIL DO
at: CD.DesignPosition = CDOrient.MapPoint[
pointInCell: p.first,
cellSize: ob.size,
cellInstOrient: orient,
cellInstPos: pos
];
CGReducer.Vertex[self: polygon, v: [
x: xoffset + at.x*plotScale,
y: yoffset - at.y*plotScale
]]
ENDLOOP;
CGReducer.Close[polygon];
CGReducer.Generate[self: polygon, area: tiling];
UNTIL CGArea.Empty[tiling] DO
t: GraphicsBasic.Trap = CGArea.Remove[tiling];
OutputTrapezoid[t.xbotL, t.xbotR, t.ybot, t.xtopL, t.xtopR, t.ytop];
ENDLOOP;
END; --PlotPolygon
DrawContext: PROC [pr: CD.DrawRef, proc: CD.DrawContextLayerProc, ob: CD.ObPtr, pos: CD.DesignPosition, orient: CD.Orientation, layer: CD.Layer] =
BEGIN
IF CDPolygons.IsPolygon[ob] THEN {
SetColor[ps, layer];
PlotPolygon[ob, pos, orient];
}
END;
AnalyzeTesselations: PROC [ps: PlotStateRef] =
BEGIN
FOR lev: CD.Layer IN CD.Layer DO
IF ps.tes[lev]#NIL THEN {
SetColor[ps, lev];
[] ← ps.tes[lev].EnumerateArea[
rect: ps.bandClip,
perTile: ProcessTile,
data: ps,
backgroundValue: $none
];
};
ENDLOOP;
END;
ProcessTile: PROCEDURE [tile: CornerStitching.TilePtr, data: REF ANY] =
CornerStitching.PerTileProc
BEGIN
IF tile.Value = $covered THEN {
rightBound, x1, y1, x2, y2: INT;
pdx1, pdx2, pdy1, pdy2: CARDINAL;
sSize, fSize: CARDINAL;
ps: PlotStateRef = NARROW[data];
r: CD.DesignRect = tile.Area;
-- Calculate absolute coords of rectangle here as offset from plot origin
x1 ← Real.RoundLI[xoffset + r.x1*plotScale];
y1 ← Real.RoundLI[yoffset - r.y1*plotScale];
x2 ← Real.RoundLI[xoffset + r.x2*plotScale];
y2 ← Real.RoundLI[yoffset - r.y2*plotScale];
rightBound ← Real.RoundLI[xoffset + ps.bandClip.x2*plotScale];
-- if rectangle is in band area, clip and write; otherwise disregard rectangle
IF NOT (
x1>rightBound OR
x2<0 OR
y1<pdy OR
y2>MIN[imageSSize, pdy+bandSSize]
) THEN {
pdx1 ← MAX[x1, 0];
pdx2 ← MIN[x2, rightBound, imageFSize];
pdy1 ← MIN[y1, pdy+bandSSize];
pdy2 ← MAX[y2, pdy];
fSize ← pdx2 - pdx1;
sSize ← pdy1 - pdy2;
-- clip on imageSSize
IF pdy1>=imageSSize THEN sSize ← Real.Fix[imageSSize - pdy2];
IF sSize#0 AND fSize#0 THEN
PDFileWriter.MaskRectangle[pdState: ps.pdState,
sMin: pdy2, fMin: pdx1, sSize: sSize, fSize: fSize
];
}
};
END;
AbortFile: PROC [s: IO.STREAM] RETURNS [IO.STREAM] = {
IF s#NIL THEN s.Close[abort: TRUE];
RETURN[NIL]
};
PDColorPlotComm: PROC [comm: CDSequencer.Command] =
BEGIN
TerminalIO.WriteRope["Color plot\n"];
[] ← CDCommandOps.CallWithResource[ProtectedPlotDesign, comm, $ColorPlot, abortPlot];
END;
Init: PROC ~ {
CDSequencer.ImplementCommand[a: $VersatecColorPlot, p: PDColorPlotComm, queue: doQueue];
CDSequencer.ImplementCommand[a: $InkJetColorPlot, p: PDColorPlotComm, queue: doQueue];
CDSequencer.ImplementCommand[a: $PuffinColorPlot, p: PDColorPlotComm, queue: doQueue];
CDMenus.CreateEntry[$HardCopyMenu, "Color Versatec", $VersatecColorPlot];
CDMenus.CreateEntry[$HardCopyMenu, "Ink Jet", $InkJetColorPlot];
CDMenus.CreateEntry[$HardCopyMenu, "Puffin, (1 page)", $PuffinColorPlot];
TerminalIO.WriteRope["ChipNDale color plot program loaded\n"];
};
Init[];
END.