ImagerStd4BitDisplayImpl.mesa
Copyright © 1984 by Xerox Corporation. All rights reserved.
Frank Crow, May 11, 1984 4:43:23 pm PDT
DIRECTORY
Imager    USING [Error, Context],
ImagerBasic   USING [DeviceRectangle, Color, ColorRep],
ImagerDisplay  USING [DisplayClass, DisplayClassRep, DisplayData,
         CreateImagerClass],
ImagerStdColorDisplay USING [SetUpMapProc, CachedColorProc, Create, ApplyMask,
          LoadColorMap, DoUnderLock, ColorMapData, MoveOverlay,
          ColorSequence, PinPixelMap, ReleasePixelMap,
          LoadColorProc],
ImagerMasks   USING [Mask],
ImagerPrivate  USING [Class, RegisterDevice],
ColorModels   USING [Calibration],
ConstantColors  USING [ColorToRGB, ColorToHSV,
         black, white, red, cyan, green, magenta, blue,
         yellow, orange, purple, brown, grey, darkGrey,
         veryLightGrey, lightGrey, veryDarkGrey],
Terminal    USING [ColorMode, Current, SetColor, Virtual],
Atom     USING [GetPropFromList],
Real     USING [FixC],
RealFns    USING [Power];
ImagerStd4BitDisplayImpl: CEDAR MONITOR
IMPORTS Imager, ImagerDisplay, ImagerPrivate, ConstantColors, Terminal, Real, RealFns, Atom,
   ImagerStdColorDisplay
~ BEGIN
This implements a 4-bit-per-pixel color-mapped display with no antialiasing.
DisplayClass: TYPE ~ ImagerDisplay.DisplayClass;
DisplayClassRep: TYPE ~ ImagerDisplay.DisplayClassRep;
DisplayData: TYPE ~ ImagerDisplay.DisplayData;
Color: TYPE ~ ImagerBasic.Color;
ColorRep: TYPE ~ ImagerBasic.ColorRep;
ConstantColor: TYPE = REF ColorRep.constant;
Mask: TYPE ~ ImagerMasks.Mask;
on: BOOLEAN ~ TRUE;
off: BOOLEAN ~ FALSE;
bitsPerPixel: NAT ← 6;
Maximum summed-squares distance allowed in color matching. Determined by trial to allow hits with 2-level color names (eg. "vivid dark bluish green")
colorAccuracy: REAL ← .1;
vt: Terminal.Virtual;
mode: Terminal.ColorMode ~ [full: FALSE, bitsPerPixelChannelA: 4, bitsPerPixelChannelB: 2]; 
std4BitDisplayClass: ImagerDisplay.DisplayClass ~ NEW[ImagerDisplay.DisplayClassRep ← [
displayType: $Std4bpp,
viewUnitsPerPixel: 1,
Create: Create,
ApplyMask: ApplyMask,
DoUnderLock: ImagerStdColorDisplay.DoUnderLock
]];
ColorDisplayError: PUBLIC ERROR [reason: ATOM] ~ CODE;
Sqr: PROCEDURE [number: REAL] RETURNS [REAL] ~ INLINE { RETURN[number * number]; };
Create: PROC [displayClass: DisplayClass, creationData: REF]
RETURNS [displayData: DisplayData] ~ {
displayData ← ImagerStdColorDisplay.Create[
       vt, mode, displayClass, creationData, bitsPerPixel, SetUpColorMap];
};
ApplyMask: PROC [displayData: DisplayData, color: Color, mask: Mask,
      sTranslate, fTranslate: INTEGER] ~ {
ImagerStdColorDisplay.ApplyMask[displayData, color, mask, sTranslate, fTranslate, CacheColor];
};
SpecialOp: PROC[context: Imager.Context, op: ATOM, data: REF] RETURNS[REF] ~ {
SELECT op FROM
$DisplayContext => { -- Pin a pixel map to the color display, replace what was there before.
ImagerStdColorDisplay.PinPixelMap[vt, NARROW[context.data, ImagerDisplay.DisplayData],
           mode ];
};
$UnDisplayContext => ImagerStdColorDisplay.ReleasePixelMap[vt,
            NARROW[context.data, ImagerDisplay.DisplayData]];
$MoveOverlay => ImagerStdColorDisplay.MoveOverlay[vt, data]; -- change DCB offsets
$LoadColorMap => ImagerStdColorDisplay.LoadColorMap[vt, data,
            NARROW[context.data, ImagerDisplay.DisplayData],
            LoadColor ];
$SetColorAccuracy => colorAccuracy ←
        NARROW[ NARROW[ data, LIST OF REF ANY].first , REF REAL]^;
ENDCASE => Imager.Error[$UnimplementedSpecialOp];
RETURN[ NIL ];
};
SetUpColorMap: ImagerStdColorDisplay.SetUpMapProc ~ {
PROC[displayData: DisplayData]
colorData: REF ImagerStdColorDisplay.ColorMapData ← NARROW[displayData.cachedColorData];
colorMap: REF ImagerStdColorDisplay.ColorSequence ←
              NEW[ImagerStdColorDisplay.ColorSequence[16]];
 Set up 16 standard colors so that complements contrast
colorMap[0] ← ConstantColors.black;    colorMap[15] ← ConstantColors.white;
colorMap[1] ← ConstantColors.red;    colorMap[14] ← ConstantColors.cyan;
colorMap[2] ← ConstantColors.green;    colorMap[13] ← ConstantColors.magenta;
colorMap[3] ← ConstantColors.blue;    colorMap[12] ← ConstantColors.yellow;
colorMap[4] ← ConstantColors.orange;   colorMap[11] ← ConstantColors.purple;
colorMap[5] ← ConstantColors.brown;   colorMap[10] ← ConstantColors.grey;
colorMap[6] ← ConstantColors.darkGrey;  colorMap[ 9] ← ConstantColors.veryLightGrey;
colorMap[7] ← ConstantColors.lightGrey;  colorMap[ 8] ← ConstantColors.veryDarkGrey;
colorData.map ← colorMap;        -- plug the map into the colorData record
IF Atom.GetPropFromList[displayData.props, $PixelMapStatus] = $Displayed THEN
FOR i: NAT IN [0..16) DO
LoadColor[colorData.map[i], colorData.colorCalibration, i];
ENDLOOP;
};
LoadColor: ImagerStdColorDisplay.LoadColorProc ~ {
PROC [ color: ConstantColor, colorCalibration: ColorModels.Calibration, mapEntry: [0..1024) ]
Map entries are assumed to be in four quadrants of 16 entries each
mapIndex, quadrant: [0..256);
r, g, b: REAL;
nr, ng, nb: [0..256);
[r, g, b] ← ConstantColors.ColorToRGB[color, colorCalibration];
r ← MIN[1.0, MAX[0.0, r]]; g ← MIN[1.0, MAX[0.0, g]]; b ← MIN[1.0, MAX[0.0, b]]; 
nr ← Real.FixC[255.0 * RealFns.Power[r, .43]]; 
ng ← Real.FixC[255.0 * RealFns.Power[g, .43]]; 
nb ← Real.FixC[255.0 * RealFns.Power[b, .43]];
mapIndex ← mapEntry MOD 16;
quadrant ← MIN[mapEntry / 16, 3];
vt.SetColor[ mapIndex, quadrant, nr, ng, nb];
};
CacheColor: ImagerStdColorDisplay.CachedColorProc ~ {
PROC [displayData: DisplayData, color: Color]
Check for presence of color in this context's cache.
colorData: REF ImagerStdColorDisplay.ColorMapData ← NARROW[displayData.cachedColorData];
displayData.cachedColor ← NIL;
WITH color SELECT FROM
constantColor: ConstantColor => {
FOR i: NAT IN [0..colorData.map.length) DO IF color = colorData.map[i] THEN {
colorData.pixelValueList ← LIST[ i ];
displayData.cachedColor ← color;         -- already in colormap
};
ENDLOOP;
IF displayData.cachedColor = NIL THEN FOR i: NAT IN [0..colorData.map.length) DO
IF   constantColor.x = colorData.map[i].x   -- check if identical to a previous color
AND constantColor.y = colorData.map[i].y
AND constantColor.Y = colorData.map[i].Y
THEN {               -- already in colormap
colorData.pixelValueList ← LIST[ i ];
displayData.cachedColor ← color;
};
ENDLOOP;
Failed completely, find closest color
IF displayData.cachedColor = NIL THEN {
minIndex: NAT;
minDist: REAL ← 2.0;   -- max value for min. summed-squares distance in CIE space
h, s, v: REAL ← 0;
[h, s, v] ← ConstantColors.ColorToHSV[constantColor, colorData.colorCalibration];
FOR i: NAT IN [0..colorData.map.length) DO
mapHue, mapSat, mapVal: REAL; dist: REAL;
[mapHue, mapSat, mapVal] ← ConstantColors.ColorToHSV[colorData.map[i],
                 colorData.colorCalibration];
dist ← Sqr[h - mapHue] * 2 + Sqr[s - mapSat] + Sqr[v - mapVal];
IF dist < minDist THEN { minIndex ← i; minDist ← dist; };
ENDLOOP;
IF minDist < colorAccuracy THEN {
colorData.pixelValueList ← LIST[ minIndex ];
displayData.cachedColor ← color;
};
};
};
ENDCASE => Imager.Error[$UnsupportedColorType];
IF displayData.cachedColor = NIL THEN Imager.Error[$CantMatchColor];  -- sorry!
};
Init: PROC[] ~ {
std4BitImagerClass: ImagerPrivate.Class ~ ImagerDisplay.CreateImagerClass[std4BitDisplayClass];  -- pick up Imager class record
std4BitImagerClass.SpecialOp ← SpecialOp;     -- modify procedure bindings
ImagerPrivate.RegisterDevice[std4BitImagerClass];   -- register device class
vt ← Terminal.Current[];
};
Init[];
END.