ApplyTRCImpl.mesa
Mik Lamming - December 21, 1984 10:20:07 am PST PST PST PST
Last Edited by: Shayegan, August 10, 1984 2:13:36 pm PDT
Last edited by: Mik Lamming - February 3, 1986 9:48:58 am PST
DIRECTORY
AIS, Buttons, Commander, Containers, IO, FS, Labels, PieViewers, Rope, ViewerOps, ViewerClasses, ViewerIO, ViewerTools, UserProfileOps, UserProfile;
ApplyTRCImpl: CEDAR MONITOR
IMPORTS AIS, Buttons, Commander, Containers, IO, FS, PieViewers, Rope, ViewerOps, ViewerIO, ViewerTools, UserProfileOps, UserProfile
= BEGIN
State: TYPE = REF StateRec;
StateRec: TYPE = RECORD [
container, inFnViewer, outFnViewer, logViewer, pieR, pieG, pieB, pieBW, trcFnViewer, wdViewer :ViewerClasses.Viewer,
out:IO.STREAM,
imageH, imageW:CARDINAL ← 2048, -- size of AIS file currently shown
showFilename:Rope.ROPE,
abort,parameterError:BOOLEAN ← FALSE,
wd: Rope.ROPE,
scale: REAL ← 1.0
];
GoSpec: TYPE = REF GoSpecRec;
GoSpecRec: TYPE = RECORD [s:State, whatToDo:ATOM ];
IndexArray: TYPE = REF IndexArrayRec;
IndexArrayRec: TYPE = ARRAY [0..255] OF CARDINAL;
outputIndex: IndexArray ← NEW[IndexArrayRec ← ALL[0]];
-- ************************************************************
-- parameter gathering and checking routines
GetFileName: PROC[s: State, v: ViewerClasses.Viewer, extension: Rope.ROPE] RETURNS[hasExtensionAIS: BOOLEAN, fileStem: Rope.ROPE] = BEGIN
fileName: Rope.ROPE ← ViewerTools.GetContents[v];
startDotAIS: INTEGER ← Rope.Find[fileName, extension, 0, FALSE];
IF hasExtensionAIS ← (startDotAIS >= 0) THEN
fileStem ← Rope.Substr[fileName, 0, startDotAIS]
ELSE
fileStem ← fileName;
IF Rope.Length[fileName] = 0 THEN {
IO.PutF[s.out, "\nSpecify a filename please"];
s.parameterError ← TRUE;
RETURN;
};
END;
ProcessFile: PROC[
s:State,
outFn:Rope.ROPE,
inList, trcList:LIST OF Rope.ROPE,
pie:ViewerClasses.Viewer
] = BEGIN
Apply: PROC[] RETURNS[] = BEGIN
FOR i:CARDINAL IN [0..s.imageW) DO
buf[i] ← outputIndex[buf[i]];
ENDLOOP;
END;
Buffer: TYPE = REF BufferRec;
BufferRec: TYPE = PACKED ARRAY [0..2048) OF [0..255];
buf: Buffer;
fdIn,fdOut:AIS.FRef;
rIn: AIS.Raster ← NEW[AIS.RasterPart];
rOut:AIS.Raster ← NEW[AIS.RasterPart];
wIn,wOut:AIS.WRef;
in :IO.STREAM;
success: BOOLEANTRUE;
outputIndex: IndexArray ← NEW[IndexArrayRec ← ALL[0]];
DO {
in ← FS.StreamOpen[fileName:trcList.first
! FS.Error => {
success ← FALSE;
CONTINUE}];
IF success THEN EXIT;
IF trcList.rest = NIL THEN {
IO.PutF[s.out, "\nCouldn't open trc file"];
s.parameterError ← TRUE;
RETURN;
}
ELSE trcList ← trcList.rest
};
ENDLOOP;
[] ← in.GetID[ !
IO.Error => {
IO.PutF[s.out, "TRC format error - Couldn't find interpolation mode flag\n"];
s.parameterError ← TRUE;
CONTINUE;
}
];
IF s.parameterError THEN RETURN;
FOR i:CARDINAL IN [0..255] DO
index:CARDINAL;
prev:CARDINAL ← index;
index ← IO.GetCard[in !
IO.EndOfStream => {
s.parameterError ← TRUE; CONTINUE;
};
IO.Error => {
s.parameterError ← TRUE;
s.out.PutF["TRC format error following vertex %g", IO.card[prev]];
CONTINUE;
}];
IF index>0 AND prev+1#index THEN {
s.parameterError ← TRUE;
s.out.PutF["Invalid TRC - Vertex %g absent", IO.card[prev+1]];
RETURN;
};
prev ← index;
IF s.parameterError THEN EXIT;
outputIndex[index] ← IO.GetCard[in !
IO.EndOfStream => {
s.parameterError ← TRUE;
s.out.PutF["TRC format error - Unexpected EOF"];
CONTINUE;
};
IO.Error => {
s.parameterError ← TRUE;
s.out.PutF["TRC format error at vertex %g", IO.card[index]];
CONTINUE;
}];
IF s.parameterError THEN EXIT;
[] ← in.GetLineRope[ !
IO.EndOfStream => {s.parameterError ← TRUE; CONTINUE}];
IF s.parameterError THEN EXIT;
ENDLOOP;
TRUSTED {
DO {
success ← TRUE;
fdIn ← AIS.OpenFile[name: inList.first
! FS.Error => {
success ← FALSE;
CONTINUE}];
IF success THEN EXIT;
IF inList.rest = NIL THEN {
IO.PutF[s.out, "\nCouldn't open input file"];
s.parameterError ← TRUE;
GOTO notThere;
}
ELSE inList ← inList.rest
};
ENDLOOP;
IO.PutF[s.out, "\nOpening %g", IO.rope[inList.first]];
rIn ← AIS.ReadRaster[fdIn];
rOut^ ← rIn^;
s.imageH ← rIn.scanCount;
s.imageW ← rIn.scanLength;
s.showFilename ← inList.first;
IO.PutF[s.out, "\nCreating %g", IO.rope[outFn]];
fdOut ← AIS.CreateFile[name:outFn, raster:rOut];
wIn ← AIS.OpenWindow[fdIn];
wOut ← AIS.OpenWindow[fdOut];
IO.PutF[s.out, "\nProcessing %g", IO.rope[outFn]];
buf ← NEW[BufferRec];
FOR i:CARDINAL IN [0..s.imageH) DO
AIS.UnsafeReadLine[w:wIn, buffer:[length:s.imageW, addr:LOOPHOLE[buf]], line:i];
Apply[];
AIS.UnsafeWriteLine[w:wOut, buffer:[length:s.imageW, addr:LOOPHOLE[buf]], line:i];
PieViewers.Set[pie, ((s.imageH-i)*100.0)/s.imageH];
IF s.abort THEN GOTO Abort;
ENDLOOP;
AIS.CloseWindow[wIn];
AIS.CloseWindow[wOut];
AIS.CloseFile[fdIn];
AIS.CloseFile[fdOut];
IO.PutF[s.out, "\nDone"];
IF ~s.abort THEN {
PieViewers.Set[pie, 0];
};
EXITS
notThere => NULL;
Abort => TRUSTED {
AIS.CloseWindow[wIn];
AIS.CloseWindow[wOut];
AIS.CloseFile[fdIn];
AIS.CloseFile[fdOut];
}
}
END;
Go: ENTRY Buttons.ButtonProc = {
goSpec:GoSpec ← NARROW[clientData];
s:State ← goSpec.s;
inStem, outStem, trcStem, outName: Rope.ROPE;
inList, trcList: LIST OF Rope.ROPE ;
inExt, outExt, trcExt:BOOLEAN;
GetProfileOptions: PROC [colorSeparationFlag: Rope.ROPE, defaultExt: Rope.ROPE] RETURNS [] ~ {
IF inExt THEN inList ← LIST[Rope.Cat[inStem, ".AIS"]]
ELSE
inList ← UserProfileOps.Expand[
"%g%k|%g|%g|.%k|AISExtensions|AIS|",
IO.rope[inStem],
IO.rope[colorSeparationFlag], IO.rope[defaultExt]];
IF outExt THEN outNameRope.Cat[outStem, ".AIS"]
ELSE
outName ← UserProfileOps.Expand[
"%g%k|%g|%g|.%k|AISExtensions|AIS|",
IO.rope[outStem],
IO.rope[colorSeparationFlag], IO.rope[defaultExt]].first;
IF trcExt THEN trcListLIST[Rope.Cat[trcStem, ".TRC"]]
ELSE
trcList ← UserProfileOps.Expand[
"%g%k|%g|%g|.%k|TRCExtensions|TRC|",
IO.rope[trcStem],
IO.rope[colorSeparationFlag], IO.rope[defaultExt]];
};
s.parameterError ← FALSE;
s.abort ← FALSE;
FS.SetDefaultWDir[ViewerTools.GetContents[s.wdViewer]
! FS.Error => {
s.parameterError ← TRUE;
IO.PutF[s.out, "\nBad WD: %g is not a local directory", IO.rope[ViewerTools.GetContents[s.wdViewer]]];
CONTINUE}];
IF s.parameterError THEN RETURN[];
[inExt, inStem] ← GetFileName[s, s.inFnViewer, ".ais"];
[outExt, outStem] ← GetFileName[s, s.outFnViewer, ".ais"];
[trcExt, trcStem] ← GetFileName[s, s.trcFnViewer, ".trc"];
IF s.parameterError THEN RETURN[];
PieViewers.Set[s.pieR, 0];
PieViewers.Set[s.pieG, 0];
PieViewers.Set[s.pieB, 0];
PieViewers.Set[s.pieBW, 0];
SELECT goSpec.whatToDo FROM
$RGB => {
PieViewers.Set[s.pieR, 100];
PieViewers.Set[s.pieG, 100];
PieViewers.Set[s.pieB, 100];
GetProfileOptions["AISseparationKeys.red", "-red"];
IF ~s.abort THEN ProcessFile[s, outName, inList, trcList, s.pieR];
GetProfileOptions["AISseparationKeys.green", "-grn"];
IF ~s.abort THEN ProcessFile[s, outName, inList, trcList, s.pieG];
GetProfileOptions["AISseparationKeys.blue", "-blu"];
IF ~s.abort THEN ProcessFile[s, outName, inList, trcList, s.pieB];
IF inExt THEN
IO.PutF[s.out, "\nWARNING: The RGB processes were applied to a single file"];
IF outExt THEN
IO.PutF[s.out, "\nWARNING: The RGB outputs were all put in the same file"];
};
$R => {
GetProfileOptions["AISseparationKeys.red", "-red"];
PieViewers.Set[s.pieR, 100];
IF ~s.abort THEN ProcessFile[s, outName, inList, trcList, s.pieR];
};
$G => {
GetProfileOptions["AISseparationKeys.green", "-grn"];
PieViewers.Set[s.pieG, 100];
IF ~s.abort THEN ProcessFile[s, outName, inList, trcList, s.pieG];
};
$B => {
GetProfileOptions["AISseparationKeys.blue", "-blu"];
PieViewers.Set[s.pieB, 100];
IF ~s.abort THEN ProcessFile[s, outName, inList, trcList, s.pieB];
};
$BW => {
GetProfileOptions["AISseparationKeys.black", ""];
PieViewers.Set[s.pieBW, 100];
IF ~s.abort THEN ProcessFile[s, outName, inList, trcList, s.pieBW];
};
ENDCASE => ERROR;
};
Abort: Buttons.ButtonProc = {
Sets a flag so scanning loops will terminate early
s:State ← NARROW[clientData];
IO.PutF[s.out,"\nABORT\n"];
s.abort ← TRUE;
};
Select: Buttons.ButtonProc = {
Red bug: pending-delete select
ViewerTools.SetSelection[NARROW[clientData]];
};
-- ************************************************************
Init: PROCEDURE [f:Rope.ROPENIL] = {
s:State ← NEW[StateRec];
SetUpProfileGlobals[s];
s.container ← ViewerOps.CreateViewer[flavor: $Container, info: [name: "Apply TRC", column: right, scrollable: FALSE, openHeight: 120, data: s]];
s.wdViewer ← ViewerTools.MakeNewTextViewer[info:[wx:170, wy:10, ww:230, wh:15, parent:s.container, border:FALSE]];
[] ← Buttons.Create[info:[name:"WD", wx:115, wy:10, parent:s.container], proc:Select, clientData:s.wdViewer];
ViewerTools.SetContents[s.wdViewer, s.wd];
s.inFnViewer ← ViewerTools.MakeNewTextViewer[info:[wx:170, wy:30, ww:230, wh:15, parent:s.container, border:FALSE]];
[] ← Buttons.Create[info:[name:"Input", wx:115, wy:30, parent:s.container], proc:Select, clientData:s.inFnViewer];
s.outFnViewer ← ViewerTools.MakeNewTextViewer[info:[wx:170, wy:50, ww:230, wh:15, parent:s.container, border:FALSE]];
[] ← Buttons.Create[info:[name:"Output", wx:115, wy:50, parent:s.container], proc:Select, clientData:s.outFnViewer];
s.trcFnViewer ← ViewerTools.MakeNewTextViewer[info:[wx:170, wy:70, ww:230, wh:15, parent:s.container, border:FALSE]];
[] ← Buttons.Create[info:[name:"TRC", wx:115, wy:70, parent:s.container], proc:Select, clientData:s.trcFnViewer];
[] ← Buttons.Create[info:[name:"R", wx:10, wy:50, parent:s.container], proc:Go, clientData:NEW[GoSpecRec ← [s, $R]]];
[] ← Buttons.Create[info:[name:"G", wx:30, wy:50, parent:s.container], proc:Go, clientData:NEW[GoSpecRec ← [s, $G]]];
[] ← Buttons.Create[info:[name:"B", wx:50, wy:50, parent:s.container], proc:Go, clientData:NEW[GoSpecRec ← [s, $B]]];
[] ← Buttons.Create[info:[name:"B/W", wx:70, wy:50, parent:s.container], proc:Go, clientData:NEW[GoSpecRec ← [s, $BW]]];
[] ← Buttons.Create[info:[name:" RGB ", wx:10, wy:30, parent:s.container], proc:Go, clientData:NEW[GoSpecRec ← [s, $RGB]]];
[] ← Buttons.Create[info:[name:"ABORT", wx:10, wy:10, parent: s.container], proc:Abort, clientData:s, guarded: TRUE];
s.logViewer ← ViewerOps.CreateViewer[flavor: $TypeScript, info:[wx:10, wy:90, ww:400, wh:15, parent:s.container]];
Containers.ChildYBound[s.container, s.logViewer];
Containers.ChildXBound[s.container, s.logViewer];
[ , s.out] ← ViewerIO.CreateViewerStreams[viewer: s.logViewer, name:NIL];
[] ← Rules.Create[info:[wx:0, wy:67, ww:1000, wh:2, parent:s.container, data:s]];
s.pieR ← PieViewers.Create[parent:s.container, x:10, y:70];
s.pieG ← PieViewers.Create[parent:s.container, x:30, y:70];
s.pieB ← PieViewers.Create[parent:s.container, x:50, y:70];
s.pieBW ← PieViewers.Create[parent:s.container, x:70, y:70];
};
SetUpProfileGlobals: PROC [state:State] = {
state.wd ← UserProfile.Token["ApplyTRC.WorkingDirectory", NIL];
IF state.wd=NIL THEN {
state.wd ← Commander.PrependWorkingDir["a"];
state.wd ← Rope.Substr[state.wd, 0, Rope.Size[state.wd]-1];
};
};
StartNewTRCProcess: Commander.CommandProc = BEGIN
-- creates a new incarnation of the scanner program
Init[];
END;
Commander.Register[key:"ApplyTRC", proc:StartNewTRCProcess, doc:"TRC program"];
END.