AISAnimationImpl.mesa
Copyright © 1984 by Xerox Corporation. All rights reserved.
Last Edited by: Crow, December 2, 1986 11:10:16 am PST
DIRECTORY
Atom      USING [ PutPropOnList, RemPropFromList ],
CedarProcess    USING [CheckAbort, DoWithPriority],
Rope      USING [Cat, ROPE, Substr],
Convert     USING [RopeFromInt],
Real      USING [FixC, RoundI],
FS       USING [ExpandName, ComponentPositions, FileInfo, Error,
         StreamOpen],
IO       USING [STREAM, GetRopeLiteral, GetInt, EndOf, EndOfStream],
Terminal     USING [Virtual, Current, WaitForBWVerticalRetrace],
AIS      USING [BRef, FRef, WRef, OpenWindow, OpenFile,
         GetWindowParams, CloseFile, Raster, RasterPart, CreateFile,
         Buffer, UnsafeWriteLine, UnsafeReadLine],
SampleMapOps   USING [Create, Get, Put, Transfer, SampleMap],
Pixels      USING [PixelBuffer, XfmMapPlace, SampleSet, GetSampleSet,
         Transfer, TerminalFromBuffer],
QuickViewer    USING [DrawInViewer],
Imager     USING [Context],
ThreeDScenes   USING [Context, Create, DisplayFromVM, Error, FillInBackGround],
ThreeDMisc    USING [PrependWorkingDirectory],
AISAnimation   USING [FrameSequence];
AISAnimationImpl: CEDAR PROGRAM
IMPORTS Atom, FS, IO, Rope, Convert, Terminal, CedarProcess, QuickViewer, Real, AIS, Pixels, SampleMapOps, ThreeDMisc, ThreeDScenes
EXPORTS AISAnimation
~ BEGIN
Internal Declarations
RopeSequence: TYPE ~ RECORD[ SEQUENCE length: NAT OF Rope.ROPE ];
FrameSequence: TYPE ~ AISAnimation.FrameSequence;
bitsPer75thSecond: INT ~ 314573;  -- roughly what bitBlt can move in one LF field time
Procedures for Animation
PlayBackNumberedAISFiles: PUBLIC PROC[context: REF ThreeDScenes.Context,
             fileRoot : Rope.ROPE,
         startNum, numFiles, framesPerSec, secondsPlayingTime : NAT] ~ {
frames: REF FrameSequence ← CacheNumberedAISFiles[context, fileRoot, numFiles, startNum];
PlayBackAISCache[context, frames, framesPerSec, secondsPlayingTime];
};
StoreFiles: PUBLIC PROC[context: REF ThreeDScenes.Context, fileRoot: Rope.ROPE, number: NAT] ~ {
PutAIS[
context,
PasteInSequenceNo[ThreeDMisc.PrependWorkingDirectory[context, fileRoot], number]
];
};
CacheAISFiles: PUBLIC PROC[context: REF ThreeDScenes.Context, fileOfNames: Rope.ROPE,
         frames: REF FrameSequence ← NIL]
     RETURNS[REF FrameSequence] ~ {
pictureNames: REF RopeSequence;
GetName: PROC[number: NAT] RETURNS[Rope.ROPE] ~ {
RETURN[ pictureNames[number] ];
};
Extend: PROC[names: REF RopeSequence] ~ {
newNames: REF RopeSequence ← NEW[ RopeSequence[names.length*2] ];
FOR i: NAT IN [0..names.length) DO newNames[i] ← names[i]; ENDLOOP;
names ← newNames;
};
input: IO.STREAM ← FS.StreamOpen[ fileName: fileOfNames ];
numFiles: NATIO.GetInt[input];    -- first thing in file is number of pictures
pictureNames ← NEW[ RopeSequence[numFiles] ];
numFiles ← 0;
WHILE NOT IO.EndOf[input] DO   -- read robustly in case number is wrong
IF numFiles >= pictureNames.length THEN Extend[pictureNames];
pictureNames[numFiles] ← IO.GetRopeLiteral[input ! IO.EndOfStream => LOOP];
IF pictureNames[numFiles] # NIL THEN numFiles ← numFiles + 1;
ENDLOOP;
RETURN[ CacheFiles[context, numFiles, 0, GetName, frames, TRUE] ];
};
CacheNumberedAISFiles: PUBLIC PROC[context: REF ThreeDScenes.Context,
            fileRoot: Rope.ROPE, numFiles: NAT, start: NAT ← 0]
        RETURNS[REF FrameSequence] ~ {
GetName: PROC[number: NAT] RETURNS[Rope.ROPE] ~ {
RETURN[ PasteInSequenceNo[
ThreeDMisc.PrependWorkingDirectory[context, fileRoot],
number
] ];
};
RETURN[ CacheFiles[context, numFiles, start, GetName, NIL, TRUE] ];
};
CacheFiles: PROC[context: REF ThreeDScenes.Context, numFiles, start: NAT,
      getname: PROC[number: NAT] RETURNS[Rope.ROPE],
      frames: REF FrameSequence ← NIL, doVisibly: BOOLEANFALSE ]
    RETURNS[REF FrameSequence] ~ {
left: INT ← Real.FixC[context.viewPort.x];
bottom: INT ← Real.FixC[context.viewPort.y];
width: INT ← Ceiling[context.viewPort.w];
height: INT ← Ceiling[context.viewPort.h];
Action: PROC ~ {
FOR i: NAT IN [start .. start+numFiles) DO
bufContext: REF ThreeDScenes.Context;
vt: Terminal.Virtual ← Pixels.TerminalFromBuffer[context.display];
bufContext ← ThreeDScenes.Create[];
ThreeDScenes.DisplayFromVM[bufContext, width, height, context.renderMode];
context.props ← Atom.PutPropOnList[context.props, $BufferContext, bufContext];
IF NOT doVisibly THEN ThreeDScenes.FillInBackGround[bufContext]; -- clear memory
[] ← GetAIS[ bufContext, getname[i] ];
IF context.stopMe THEN HoldEverything[];  -- shut down if stop signal received
CedarProcess.CheckAbort[];      -- respond to Stop button
IF frames = NIL THEN frames ← NEW[ FrameSequence[numFiles] ];
IF frames.maxLength <= i - start THEN {   -- extend FrameSequence if necessary
newFrames: REF FrameSequence ← NEW[ FrameSequence[2*frames.maxLength] ];
FOR j: NAT IN [0..frames.maxLength) DO newFrames[i] ← frames[i]; ENDLOOP;
frames ← newFrames;
};
frames[i - start] ← bufContext.display;
frames.length ← i - start + 1;
IF doVisibly THEN Pixels.Transfer[ context.display, bufContext.display];  -- to display
ENDLOOP;
context.props ← Atom.RemPropFromList[context.props, $BufferContext];
};
CedarProcess.DoWithPriority[background, Action];    -- load 'em up quietly
RETURN[frames];
};
PlayBackAISCache: PUBLIC PROC[context: REF ThreeDScenes.Context,
           frames: REF FrameSequence,
           framesPerSec, secondsPlayingTime: NAT,
           bothWays: BOOLEANFALSE, startNum: NAT ← 0] ~ {
vt: Terminal.VirtualTerminal.Current[];
waitCount, transferTime: NAT;
repetitions: INTMAX[1,
       INT[secondsPlayingTime] * framesPerSec / (frames.length-startNum)];
IF frames = NIL THEN {
SIGNAL ThreeDScenes.Error[[$MisMatch, "NoCachedFrames"]]; RETURN;
};
[waitCount, transferTime] ← GetWaitCount[context, framesPerSec];
IF waitCount = 0
THEN repetitions ← (secondsPlayingTime * 75 / transferTime) / (frames.length - startNum);
IF bothWays THEN repetitions ← repetitions/2;
FOR i: INT IN [0..repetitions) DO        -- play back
FOR j: NAT IN [startNum..frames.length) DO
FOR k: NAT IN [0..waitCount) DO vt.WaitForBWVerticalRetrace[]; ENDLOOP;
frames.currentFrame ← j;
ShowAISCacheFrame[context, frames];
IF context.stopMe THEN HoldEverything[];  -- shut down if stop signal received
CedarProcess.CheckAbort[];   -- respond to Stop button
ENDLOOP;
IF bothWays THEN FOR j: NAT DECREASING IN [startNum..frames.length) DO
FOR k: NAT IN [0..waitCount) DO vt.WaitForBWVerticalRetrace[]; ENDLOOP;
frames.currentFrame ← j;
ShowAISCacheFrame[context, frames];
IF context.stopMe THEN HoldEverything[];  -- shut down if stop signal received
CedarProcess.CheckAbort[];   -- respond to Stop button
ENDLOOP;
ENDLOOP;
};
ShowNextAISCacheFrame: PUBLIC PROC[context: REF ThreeDScenes.Context,
           frames: REF FrameSequence, framesPerSec: INTEGER] ~ {
waitCount: NAT;
vt: Terminal.VirtualTerminal.Current[];
IF framesPerSec < 0
THEN frames.currentFrame ← (frames.currentFrame + frames.length-1) MOD frames.length
ELSE frames.currentFrame ← (frames.currentFrame + 1) MOD frames.length;
[waitCount: waitCount] ← GetWaitCount[context, ABS[framesPerSec]];
FOR k: NAT IN [0..waitCount) DO vt.WaitForBWVerticalRetrace[]; ENDLOOP;
ShowAISCacheFrame[context, frames];
};
Support Procedures
Ceiling: PROC[number: REAL] RETURNS[result: INTEGER] ~ {
result ← Real.RoundI[number];
IF result < number THEN result ← result + 1;
};
HoldEverything: PUBLIC PROCEDURE [] ~ {
This shuts down the current process
ERROR ABORTED;
};
PasteInSequenceNo: PUBLIC PROC[fileRoot: Rope.ROPE, number: NAT] RETURNS[Rope.ROPE] ~ {
RETURN[ PasteInLabel[fileRoot, Convert.RopeFromInt[number, 10, FALSE] ] ];
};
PasteInLabel: PROC[fileRoot: Rope.ROPE, label: Rope.ROPE] RETURNS[Rope.ROPE] ~ {
cp: FS.ComponentPositions; fullFName, fName, ext: Rope.ROPE;
[fullFName, cp, ] ← FS.ExpandName[fileRoot];
IF cp.ext.length = 0
THEN {
fName ← Rope.Substr[ fullFName, 0, cp.ext.start];  -- name for filling in numbers
ext ← "ais";
}
ELSE {
fName ← Rope.Substr[ fullFName, 0, cp.ext.start-1];  -- drop "." before ext
ext ← Rope.Substr[ fullFName, cp.ext.start, cp.ext.length];
};
RETURN[ Rope.Cat[fName, label, ".", ext] ];
};
GetWaitCount: PROC[context: REF ThreeDScenes.Context, framesPerSec: NAT]
     RETURNS[waitCount: NAT, transferTime: NAT] ~ {
width: INT ← Ceiling[context.viewPort.w];
height: INT ← Ceiling[context.viewPort.h];
addOn: NATIF context.alphaBuffer THEN 1 ELSE 0;
bitsPerPixel: NAT ← context.display.pixels[addOn].subMap.sampleMap.bitsPerSample;
transferTime ← width * height * bitsPerPixel / bitsPer75thSecond;
IF framesPerSec = 0
THEN waitCount ← 150
ELSE waitCount ← 75 / framesPerSec;  -- LF display VerticalRetrace comes 75/sec.
IF transferTime > waitCount   -- take bitBlt overhead into account
THEN waitCount ← 0
ELSE waitCount ← waitCount - transferTime;
waitCount ← MAX[waitCount, 1];
};
ShowAISCacheFrame: PUBLIC PROC[ context: REF ThreeDScenes.Context,
           frames: REF FrameSequence ] ~ {
DoTransfer: PROCEDURE [imagerCtx: Imager.Context] ~ {
Pixels.Transfer[context.display, frames[frames.currentFrame]];
};
IF frames.currentFrame < 0 OR frames.currentFrame >= frames.length
THEN SIGNAL ThreeDScenes.Error[[$MisMatch, "Frame number out of range"]];
IF frames[frames.currentFrame].pixels # NIL
THEN IF context.viewer # NIL
THEN QuickViewer.DrawInViewer[NARROW[context.viewer], DoTransfer]
ELSE DoTransfer[NIL];
};
standardNames: ARRAY[0..5) OF RECORD[ pref, alt, other: Rope.ROPE ] ← [
["-grey", "-gray", NIL],
["-red", "-r", NIL],
["-grn", "-green", "-g"],
["-blu", "-blue", "-b"],
["-alpha", "-alph", "-a"]
];
FindFile: PROC [fileRoot: Rope.ROPE, names: RECORD[ pref, alt, other: Rope.ROPE ] ]
RETURNS
[name: Rope.ROPE] ~ {
ok: BOOLEANTRUE;
[] ← FS.FileInfo[name ← PasteInLabel[fileRoot, names.pref]
     ! FS.Error => {ok ← FALSE; CONTINUE}];
IF ok THEN RETURN[ name ] ELSE ok ← TRUE;
[] ← FS.FileInfo[name ← PasteInLabel[fileRoot, names.alt]
     ! FS.Error => {ok ← FALSE; CONTINUE}];
IF ok THEN RETURN[ name ] ELSE ok ← TRUE;
[] ← FS.FileInfo[name ← PasteInLabel[fileRoot, names.other]
     ! FS.Error => {ok ← FALSE; CONTINUE}];
IF ok THEN RETURN[ name ]
ELSE {
SIGNAL ThreeDScenes.Error[[
$MisMatch,
Rope.Cat[" Can't find ", fileRoot, " with extensions: ",
   Rope.Cat[names.pref, " ", names.alt, " ", names.other] ]
]];
RETURN[ NIL ];
};
};
GetAIS: PUBLIC PROC[context: REF ThreeDScenes.Context, fileRoot: Rope.ROPE ← "Temp.ais",
      xOffset, yOffset: INTEGER ← 0, center: BOOLEANTRUE ]
     RETURNS
[ xSize, ySize: INTEGER] ~ {
numFiles: NAT;
names: ARRAY[0..4) OF RECORD[ pref, alt, other: Rope.ROPE ];
fileRoot ← ThreeDMisc.PrependWorkingDirectory[context, fileRoot];
SELECT context.renderMode FROM
$Dithered, $PseudoColor, $Grey => {
names[0] ← standardNames[0];
numFiles ← 1;
};
$FullColor, $Dorado24  => {
names[0] ← standardNames[1];
names[1] ← standardNames[2];
names[2] ← standardNames[3];
numFiles ← 3;
};
ENDCASE => SIGNAL ThreeDScenes.Error[[$MisMatch, "Bad RenderMode"]];
IF context.alphaBuffer
THEN { names[numFiles] ← standardNames[4]; numFiles ← numFiles + 1; };
FOR i: NAT IN [0..numFiles) DO   -- get a file for each pixel entry
fileName: Rope.ROPE ← FindFile[fileRoot, names[i]];
fileInfo: AIS.FRef;
wndw: AIS.WRef;
wbufr: AIS.BRef;
samples: Pixels.SampleSet;
scanLine: SampleMapOps.SampleMap;
buffer: AIS.Buffer;
bufXOffset, bufYOffset: INTEGER;    -- start position in pixel buffer
xBeg, yBeg: INTEGER;       -- start position in AIS file
firstScan, lastScan, firstPixel, lastPixel, height, width: INTEGER;
IF fileName = NIL THEN LOOP;     -- skip if no file found
fileInfo ← AIS.OpenFile[ FindFile[fileRoot, names[i]] ];   -- get file
wndw ← AIS.OpenWindow[fileInfo];
wbufr ← wndw.bref;
[firstScan, lastScan, firstPixel, lastPixel] ← AIS.GetWindowParams[wndw];
IF center              -- automatic centering
THEN {
bufXOffset ← xOffset + (Ceiling[context.viewPort.w] - (lastPixel-firstPixel+1)) /2;
bufYOffset ← yOffset + (Ceiling[context.viewPort.h] - (lastScan-firstScan+1)) / 2;
}
ELSE { bufXOffset ← xOffset; bufYOffset ← yOffset; };
IF bufXOffset >= 0       -- set lower left corner in AIS and PixelBuffer
THEN { xBeg ← 0; }
ELSE { xBeg ← -bufXOffset; bufXOffset ← 0; };
IF bufYOffset >= 0
THEN { yBeg ← 0; }
ELSE { yBeg ← -bufYOffset; bufYOffset ← 0; };
height ← MIN[ Ceiling[context.viewPort.h]-bufYOffset, (lastScan-firstScan+1) - yBeg];
width ← MIN[ Ceiling[context.viewPort.w]-bufXOffset, (lastPixel-firstPixel+1) - xBeg ];
xSize ← MAX[width, lastPixel-firstPixel+1];
ySize ← MAX[height, lastScan-firstScan+1];    -- grab height and width to return
yBeg ← lastScan - (yBeg + height-1);  -- flip y-values
[bufXOffset, bufYOffset] ← Pixels.XfmMapPlace[ context.display.pixels[i],
              bufXOffset, bufYOffset+height-1 ];
samples ← Pixels.GetSampleSet[lastPixel-firstPixel+1];
scanLine ← SampleMapOps.Create[
fSize: (lastPixel-firstPixel+1),
sSize: 1,
bitsPerSample: context.display.pixels[i].subMap.sampleMap.bitsPerSample
];
TRUSTED { buffer ← [length: wndw.wordsPerLine, addr: scanLine.base.word]; };
FOR y: INTEGER IN [ 0 .. height ) DO
IF y+yBeg >= 0 AND y+yBeg <= lastScan THEN {
TRUSTED { AIS.UnsafeReadLine[ wndw, buffer, y+yBeg ]; };
IF context.display.pixels[i].df > 1
THEN {
SampleMapOps.Get[ buffer: samples, start: 0, count: lastPixel-firstPixel+1,
sampleMap: scanLine ];
SampleMapOps.Put[ buffer: samples, start: xBeg, count: width,
sampleMap: context.display.pixels[i].subMap.sampleMap,
f: bufXOffset,
s: y+bufYOffset,
df: context.display.pixels[i].df
];
}
ELSE SampleMapOps.Transfer[
dest: context.display.pixels[i].subMap.sampleMap,
destStart: [f: bufXOffset, s: y+bufYOffset],
source: [scanLine, [f: xBeg, s: 0], [f: width, s: 1] ]
];
};
ENDLOOP;
AIS.CloseFile[fileInfo];
ENDLOOP;
};
PutAIS: PUBLIC PROC[context: REF ThreeDScenes.Context, fileRoot: Rope.ROPE ← "Temp.ais" ] ~{
addOn: NATIF context.alphaBuffer THEN 1 ELSE 0;
numFiles: NAT;
bitsPerPixel: NAT ← context.display.pixels[addOn].subMap.sampleMap.bitsPerSample;
raster: AIS.Raster ← NEW[AIS.RasterPart];
samples: Pixels.SampleSet;
scanLine: SampleMapOps.SampleMap;
height, width: INTEGER;
names: ARRAY[0..4) OF Rope.ROPEALL[NIL];
fileRoot ← ThreeDMisc.PrependWorkingDirectory[context, fileRoot];
raster.scanCount ← height ← Ceiling[context.viewPort.h];
raster.scanLength ← width ← Ceiling[context.viewPort.w];
raster.scanMode ← rd;
raster.bitsPerPixel ← bitsPerPixel;
raster.linesPerBlock ← -1;
raster.paddingPerBlock ← 0;
SELECT context.renderMode FROM
$Dithered, $PseudoColor => { names[0] ← NIL; numFiles ← 1; };
$Grey => { names[0] ← "-grey"; numFiles ← 1; };
$FullColor, $Dorado24  => {
names[0] ← "-red"; names[1] ← "-grn"; names[2] ← "-blu"; numFiles ← 3;
};
ENDCASE => SIGNAL ThreeDScenes.Error[[$MisMatch, "Bad RenderMode"]];
IF context.alphaBuffer
AND (context.extentCovered.right > context.extentCovered.left)
AND (context.extentCovered.top > context.extentCovered.bottom)
THEN { names[numFiles] ← "-alpha"; numFiles ← numFiles + 1; };
FOR i: NAT IN [0..numFiles) DO   -- write a file for each pixel entry
fileInfo: AIS.FRef ← AIS.CreateFile[
name: PasteInLabel[fileRoot, names[i]],
raster: raster
];
window: AIS.WRef ← AIS.OpenWindow[fileInfo];
buffer: AIS.Buffer;
bufXOffset, bufYOffset: INTEGER;
[bufXOffset, bufYOffset] ← Pixels.XfmMapPlace[context.display.pixels[i], 0, height-1];
samples ← Pixels.GetSampleSet[ width ];
scanLine ← SampleMapOps.Create[
fSize: width,
sSize: 1,
bitsPerSample: context.display.pixels[i].subMap.sampleMap.bitsPerSample
];
TRUSTED{buffer ← [length: width, addr: scanLine.base.word];};
FOR y: NAT IN [0..height) DO
IF context.display.pixels[i].df > 1
THEN {
SampleMapOps.Get[ buffer: samples, count: width,
sampleMap: context.display.pixels[i].subMap.sampleMap,
f: bufXOffset,
s: y+bufYOffset,
df: context.display.pixels[i].df
];
SampleMapOps.Put[ buffer: samples, count: width, sampleMap: scanLine ];
}
ELSE SampleMapOps.Transfer[
dest: scanLine,
destStart: [f: 0, s: 0],
source: [
sampleMap: context.display.pixels[i].subMap.sampleMap,
start: [f: bufXOffset, s: y+bufYOffset],
size: [f: width, s: 1]
]
];
TRUSTED { AIS.UnsafeWriteLine[window, buffer, y]; };
ENDLOOP;
AIS.CloseFile[fileInfo];
ENDLOOP;
};
END.