ImagerColorMain.mesa
Copyright © 1984 by Xerox Corporation. All rights reserved.
Authored by Frank Crow
Last Edited by: Crow, November 29, 1984 5:12:33 pm PST
DIRECTORY       
Basics     USING [BITSHIFT],
Rope     USING [ROPE],
IO      USING [PutRope, PutF, STREAM],
ViewerIO    USING [CreateViewerStreams],
Graphics    USING [Context],
QuickViewer   USING [BuildViewer],
ImagerBridge   USING [SetViewFromGraphicsContext],
ImagerColorUtilities USING [SetNamedColor, SetRGBColor, Show8BitMap, LoadStd8BitMap,
         Rotate8BitMap, Load8BitRampMap, SetClipRectangle, FillRectangle,
         FillScreenTrap, DrawLine, ShowRope, SetDevice, GetAISFile,
         Get3AISFiles, PutAISFile, PutFastAISFile, Put3AISFiles, SetContext],
Imager    USING [Context, Pair, SpecialOp],
ImagerBasic   USING [IntRectangle],
ImagerDisplay  USING [DisplayData],
ImagerPixelMapsExtras USING [DrawLine, DrawBltLine],
ConstantColors  USING [ColorToName, HSLToColor],
ColorNames   USING [HSLToRope];
ImagerColorMain: CEDAR PROGRAM
IMPORTS Imager, ConstantColors, ColorNames, IO, ViewerIO, QuickViewer, ImagerPixelMapsExtras, Basics, ImagerColorUtilities
ImagerBridge,
= BEGIN
displayContext: Imager.Context ← NIL;
contexts: ARRAY [0..4) OF Imager.Context ← ALL[NIL];
contextMax: NAT ← 4;
contextCount: NAT ← 0;
in, out: IO.STREAM;         -- I/O to log viewer
nullRectangle: ImagerBasic.IntRectangle ← [0, 0, 0, 0];
activeButton: ATOM;
NameColor: PROC [color: Rope.ROPE] ~ {
ImagerColorUtilities.SetNamedColor[color];
};
RGBColor: PROC [r, g, b: REAL] ~ {
ImagerColorUtilities.SetRGBColor[r, g, b];
};
HSLToName: PROC [h, s, l: REAL] ~ {
name: Rope.ROPE;
name ← ConstantColors.ColorToName[ConstantColors.HSLToColor[h, s, l]];
IO.PutRope[out, name];
IO.PutF[out, "\n"];
};
HSLToLongName: PROC [h, s, l: REAL, levels: NAT] ~ {
name: Rope.ROPE;
name ← ColorNames.HSLToRope[h, s, l, levels];
IO.PutRope[out, name];
IO.PutF[out, "\n"];
};
LoadStd8BitMap: PROC [] ~ {
ImagerColorUtilities.LoadStd8BitMap[];
};
Rotate8BitMap: PROC [firstValue, lastValue, duration: NAT] ~ {  -- rotates color map without moving zeroth entry
ImagerColorUtilities.Rotate8BitMap[firstValue, lastValue, duration];
};
Show8BitMap: PROC [] ~ {
ImagerColorUtilities.Show8BitMap[];
};
Load8BitRampMap: PROC [r1, g1, b1, r2, g2, b2, exponent: REAL] ~ {
ImagerColorUtilities.Load8BitRampMap[r1, g1, b1, r2, g2, b2, exponent];
};
ClipRectangle: PROC [x, y, w, h: REAL] ~ {
ImagerColorUtilities.SetClipRectangle[x, y, w, h];
};
FillRectangle: PROC [x, y, w, h: REAL] ~ {
ImagerColorUtilities.FillRectangle[x, y, w, h];
};
FillTrap: PROC [top, bottom, leftTop, leftBot, rightTop, rightBot: INTEGER, color: NAT] ~ {
ImagerColorUtilities.FillScreenTrap[top, bottom, leftTop, leftBot, rightTop, rightBot, color];
};
DrawLine: PROC [pt1, pt2: Imager.Pair, width: REAL] ~ {
ImagerColorUtilities.DrawLine[pt1, pt2, width];
};
LineTest: PROC[color: CARDINAL, length, times: NAT] ~ {
ax, ay, bx, by, xOffset, yOffset: INTEGER;
displayData: ImagerDisplay.DisplayData ← NARROW[displayContext.data,
               ImagerDisplay.DisplayData];
logBitsPerPixel: NAT ← displayData[0].refRep.lgBitsPerPixel;
mapLength: NAT ← Basics.BITSHIFT[1, Basics.BITSHIFT[1, logBitsPerPixel]];
xMax: NAT ← displayData[0].fSize; yMax: NAT ← displayData[0].sSize;
IF length > yMax THEN length ← yMax;
xOffset ← (xMax - length) / 2; yOffset ← (yMax - length) / 2;
FOR i: NAT IN [0..times) DO
FOR j: NAT IN [0..length] DO
ax ← j + xOffset;    ay ← length + yOffset;
bx ← length + xOffset;  by ← length - j + yOffset;
color ← (color MOD mapLength) + 1;
ImagerPixelMapsExtras.DrawLine[displayData[0], [ax, ay], [bx, by], color];
ENDLOOP;
FOR j: NAT IN [0..length] DO
ax ← length + xOffset;  ay ← length - j + yOffset;
bx ← length - j + xOffset; by ← 0 + yOffset;
color ← (color MOD mapLength) + 1;
ImagerPixelMapsExtras.DrawLine[displayData[0], [ax, ay], [bx, by], color];
ENDLOOP;
FOR j: NAT IN [0..length] DO
ax ← length - j + xOffset; ay ← 0 + yOffset;
bx ← 0 + xOffset;    by ← j + yOffset;
color ← (color MOD mapLength) + 1;
ImagerPixelMapsExtras.DrawLine[displayData[0], [ax, ay], [bx, by], color];
ENDLOOP;
FOR j: NAT IN [0..length] DO
ax ← 0 + xOffset;    ay ← j + yOffset;
bx ← j + xOffset;    by ← length + yOffset;
color ← (color MOD mapLength) + 1;
ImagerPixelMapsExtras.DrawLine[displayData[0], [ax, ay], [bx, by], color];
ENDLOOP;
ENDLOOP;
};
BltLineTest: PROC[color: CARDINAL, length, times: NAT] ~ {
ax, ay, bx, by, xOffset, yOffset: INTEGER;
displayData: ImagerDisplay.DisplayData ← NARROW[displayContext.data,
               ImagerDisplay.DisplayData];
logBitsPerPixel: NAT ← displayData[0].refRep.lgBitsPerPixel;
mapLength: NAT ← Basics.BITSHIFT[1, Basics.BITSHIFT[1, logBitsPerPixel]];
xMax: NAT ← displayData[0].fSize; yMax: NAT ← displayData[0].sSize;
IF length > yMax THEN length ← yMax;
xOffset ← (xMax - length) / 2; yOffset ← (yMax - length) / 2;
FOR i: NAT IN [0..times) DO
FOR j: NAT IN [0..length] DO
ax ← j + xOffset;    ay ← length + yOffset;
bx ← length + xOffset;  by ← length - j + yOffset;
color ← (color MOD mapLength) + 1;
ImagerPixelMapsExtras.DrawBltLine[displayData[0], [ax, ay], [bx, by], color, [xor, null]];
ENDLOOP;
FOR j: NAT IN [0..length] DO
ax ← length + xOffset;  ay ← length - j + yOffset;
bx ← length - j + xOffset; by ← 0 + yOffset;
color ← (color MOD mapLength) + 1;
ImagerPixelMapsExtras.DrawBltLine[displayData[0], [ax, ay], [bx, by], color, [xor, null]];
ENDLOOP;
FOR j: NAT IN [0..length] DO
ax ← length - j + xOffset; ay ← 0 + yOffset;
bx ← 0 + xOffset;    by ← j + yOffset;
color ← (color MOD mapLength) + 1;
ImagerPixelMapsExtras.DrawBltLine[displayData[0], [ax, ay], [bx, by], color, [xor, null]];
ENDLOOP;
FOR j: NAT IN [0..length] DO
ax ← 0 + xOffset;    ay ← j + yOffset;
bx ← j + xOffset;    by ← length + yOffset;
color ← (color MOD mapLength) + 1;
ImagerPixelMapsExtras.DrawBltLine[displayData[0], [ax, ay], [bx, by], color, [xor, null]];
ENDLOOP;
ENDLOOP;
};
ShowRope: PROC[x, y: REAL, rope: Rope.ROPE, fontRope: Rope.ROPENIL, size: REAL ← .008] ~ {
ImagerColorUtilities.ShowRope[x, y, rope, fontRope, size];
};
SetDevice: PROC[deviceType: ATOM, box, box2: ImagerBasic.IntRectangle, pinned: BOOLEAN] ~{
Caches last 4 contexts
displayContext ← ImagerColorUtilities.SetDevice[deviceType, box, box2, pinned];
contextCount ← contextCount + 1;
IF contextCount > contextMax THEN {
FOR i: NAT IN (0..4) DO contexts[i - 1] ← contexts[i]; ENDLOOP;
contextCount ← contextMax;
};
contexts[contextCount] ← displayContext;
};
RecoverContext: PROC[contextNumber: NAT] ~ {
displayContext ← contexts[contextNumber];
ImagerColorUtilities.SetContext[displayContext];
};
MoveOverlayTo: PROC[x, y: NAT] ~ {
refX: REF INTEGERNEW[INTEGER ← x];
refY: REF INTEGERNEW[INTEGER ← y];
data: LIST OF REF ANYLIST[refX, refY];
[] ← Imager.SpecialOp[displayContext, $MoveOverlay, data];
};
SwitchBuffers: PROC[] ~ {
[] ← Imager.SpecialOp[displayContext, $SwitchBuffers, NIL];
};
GetAISFile: PUBLIC PROC[fileName: Rope.ROPE, xOffSet, yOffSet: INTEGER ← 0] ~ {
ImagerColorUtilities.GetAISFile[fileName, xOffSet, yOffSet];
};
Get3AISFiles: PUBLIC PROC[redFile, greenFile, blueFile: Rope.ROPE,
         xOffSet, yOffSet: INTEGER ← 0] ~ {
ImagerColorUtilities.Get3AISFiles[redFile, greenFile, blueFile, xOffSet, yOffSet];
};
PutAISFile: PUBLIC PROC[fileName: Rope.ROPE] ~ {
ImagerColorUtilities.PutAISFile[fileName];
};
PutFastAISFile: PUBLIC PROC[fileName: Rope.ROPE] ~ {
ImagerColorUtilities.PutFastAISFile[fileName];
};
Put3AISFiles: PUBLIC PROC[redFile, greenFile, blueFile: Rope.ROPE] ~ {
ImagerColorUtilities.Put3AISFiles[redFile, greenFile, blueFile];
};
MenuHit: PROCEDURE[command: ATOM, x, y: REAL] = { 
SELECT command FROM
$Button => activeButton ← NIL;
$MoveDCB => { activeButton ← $MoveDCB; IO.PutF[out, "Button received\n"]; };
$Boxes, $Circles, $Exes, $Octagons => drawMode ← command;
$LeftButton, $LeftHeld => { pointList ← CONS[ [x, y], pointList];
         SELECT drawMode FROM
         $Exes  => DrawX[pointList.first, size, black];
         $Boxes => DrawBox[pointList.first, size, black];
         $Circles => DrawCircle[pointList.first, size, black];
         $Octagons => DrawOct[pointList.first, size, black];
           ENDCASE;
        };
$MiddleButton => DrawX[[x, y], size, black];
$RightButton => DrawCircle[[x, y], size, black];
$MiddleHeld  => DrawX[[x, y], size, white];
$RightHeld  => IF activeButton = $MoveDCB THEN {
displayData: ImagerDisplay.DisplayData ← NARROW[contexts[0].data,
               ImagerDisplay.DisplayData];
displayData[0].sOrigin ← 50 + Real.FixI[-y];
displayData[0].fOrigin ← 100 + Real.FixI[x];
displayContext ← contexts[0];
PinMap[];
};
$LeftHeld  => IF activeButton = $MoveDCB THEN {
displayData: ImagerDisplay.DisplayData ← NARROW[contexts[1].data,
               ImagerDisplay.DisplayData];
displayData[0].sOrigin ← 50 + Real.FixI[-y];
displayData[0].fOrigin ← 100 + Real.FixI[x];
displayContext ← contexts[1];
PinOverlay[];
IO.PutF[out, "x = %g, y = %g\n",
        IO.int[displayData[0].fOrigin], IO.int[displayData[0].sOrigin]];
};
ENDCASE;
};
ReDraw:  PROCEDURE [dc: Graphics.Context] = {
};
ShutDown: PROCEDURE [] = {
IO.PutF[out, "Imager Color Test over -- Bye\n"];        -- say goodbye
};
Init: PROCEDURE [] = {
QuickViewer.BuildViewer[LIST[$Button, $MoveDCB],
        ReDraw, ShutDown, MenuHit, "ImagerColorTest"];
[in, out] ← ViewerIO.CreateViewerStreams["ImagerColorTest.log"]; -- initialize i/o viewer
};
END.