<> <> <> <> <<>> 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: BOOLEAN _ TRUE; 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 outName _ Rope.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 trcList _ LIST[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 = { <> s:State _ NARROW[clientData]; IO.PutF[s.out,"\nABORT\n"]; s.abort _ TRUE; }; <<>> Select: Buttons.ButtonProc = { <> ViewerTools.SetSelection[NARROW[clientData]]; }; -- ************************************************************ <<>> Init: PROCEDURE [f:Rope.ROPE _ NIL] = { 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.