ImagerSmoothGrayDisplayImpl.mesa
Michael Plass, March 26, 1984 10:15:15 am PST
Frank Crow, August 10, 1984 3:40:38 pm PDT
DIRECTORY Atom, Basics, Imager, ImagerBasic, ImagerDisplay, ImagerMasks, ImagerPixelMaps, ImagerPrivate, Terminal, ImagerStdColorDisplay, Real, RealFns, ConstantColors;
ImagerSmoothGrayDisplayImpl: CEDAR PROGRAM
IMPORTS Atom, Basics, Imager, ImagerDisplay, ImagerMasks, ImagerPixelMaps, ImagerPrivate, Terminal, ImagerStdColorDisplay, Real, RealFns, ConstantColors ~ BEGIN
This is an example implementation of an Imager display device. It implements an 8-bit-per-pixel gray-level display with antialiasing.
DisplayClass: TYPE ~ ImagerDisplay.DisplayClass;
DisplayClassRep: TYPE ~ ImagerDisplay.DisplayClassRep;
DisplayData: TYPE ~ ImagerDisplay.DisplayData;
DisplayDataRep: TYPE ~ ImagerDisplay.DisplayDataRep;
DeviceRectangle: TYPE ~ ImagerBasic.DeviceRectangle;
Color: TYPE ~ ImagerBasic.Color;
ColorRep: TYPE ~ ImagerBasic.ColorRep;
SampledColor: TYPE = REF ColorRep.sampled;
ConstantColor: TYPE = REF ColorRep.constant;
SpecialColor: TYPE = REF ColorRep.special;
Mask: TYPE ~ ImagerMasks.Mask;
vt: Terminal.Virtual;
mode: Terminal.ColorMode ~ [full: FALSE, bitsPerPixelChannelA: 8, bitsPerPixelChannelB: 2];
bitsPerPixel: NAT ~ 10;
smthGrayDisplayClass: ImagerDisplay.DisplayClass ~ NEW[ImagerDisplay.DisplayClassRep ← [
displayType: $SmoothGray,
viewUnitsPerPixel: 5,
Create: Create,
ApplyMask: ApplyMask,
DoUnderLock: ImagerStdColorDisplay.DoUnderLock
]];
Create: PROC [displayClass: DisplayClass, creationData: REF]
RETURNS [displayData: DisplayData] ~ {
The creationData is inherited from the Create call. Here we ignore it, but a possible use for it is to point to an alternate buffer.
displayData ← ImagerStdColorDisplay.Create[
     vt, mode, displayClass, creationData, bitsPerPixel, SetUpColorMap];
};
SetUpColorMap: ImagerStdColorDisplay.SetUpMapProc ~ {
PROC[displayData: DisplayData]
IF Atom.GetPropFromList[displayData.props, $PixelMapStatus] = $Displayed THEN
FOR i: NAT IN [0..256) DO
j: NAT ← Real.FixC[RealFns.Power[i/256.0, .43] * 256.0]; -- Linearize power law display
vt.SetColor[i, 0, j, j, j];
ENDLOOP;
};
ApplyMask: PROC [displayData: DisplayData, color: Color, mask: Mask, sTranslate, fTranslate: INTEGER] ~ {
LockedApplyMask: PROC ~ {
WITH color SELECT FROM
constantColor: ConstantColor => {
pixelValue: [0..255] ← constantColor.Y/256;
lineBuff: ARRAY [0..1034] OF INTEGERALL[0];
minPixel: NAT ← 1024;
maxPixel: NAT ← 0;
vupp: NAT ← displayData.displayClass.viewUnitsPerPixel;
squpp: NAT ← vupp*vupp;
line: NATCARDINAL[MAX[bb.sMin, 0]]/vupp;
s: NAT ← line*vupp;
sModVupp: NAT ← 0;
DoLine: PROC ~ {
sum: NAT ← 0;
pixel: NAT ← minPixel;
FOR j: NAT IN [minPixel..maxPixel] DO
lineBuff[j] ← sum ← sum + lineBuff[j];
ENDLOOP;
lineBuff[maxPixel+1] ← LAST[NAT];
WHILE pixel <= maxPixel DO
bltStart: NAT;
WHILE lineBuff[pixel] = 0 DO pixel ← pixel + 1 ENDLOOP;
bltStart ← pixel;
WHILE lineBuff[pixel] = squpp DO pixel ← pixel + 1 ENDLOOP;
IF pixel > bltStart THEN {
displayData[0].Fill[[line, bltStart, 1, pixel-bltStart], pixelValue];
};
IF pixel <= maxPixel THEN {
oldValue: CARDINAL ← displayData[0].Get8Bits[line, pixel];
mix: CARDINAL ← lineBuff[pixel];
mixedValue: CARDINAL ← Basics.LongDiv[
Basics.LongMult[oldValue, squpp-mix]+Basics.LongMult[pixelValue, mix],
squpp
];
displayData[0].Fill[[line, pixel, 1, 1], mixedValue];
pixel ← pixel + 1;
};
ENDLOOP;
lineBuff[maxPixel+1] ← 0;
FOR j: NAT IN [minPixel..maxPixel] DO
lineBuff[j] ← 0;
ENDLOOP;
minPixel ← 1024;
maxPixel ← 0;
};
RunProc: PROC [sMin, fMin: INTEGER, fSize: NAT] ~ {
q0, r0, q1, r1: CARDINAL;
f0: CARDINALMIN[MAX[fMin, 0], displayData.surfaceWidth];
f1: CARDINALMIN[MAX[fMin+fSize, 0], displayData.surfaceWidth];
WHILE s < sMin DO
s ← s + 1;
sModVupp ← sModVupp + 1;
IF sModVupp = vupp THEN {
DoLine[];
sModVupp ← 0;
line ← line + 1;
};
ENDLOOP;
IF fSize > 0 THEN {
[q0, r0] ← Basics.LongDivMod[f0, vupp];
[q1, r1] ← Basics.LongDivMod[f1, vupp];
IF q0 < minPixel THEN minPixel ← q0;
IF q1 > maxPixel THEN maxPixel ← q1;
lineBuff[q0] ← lineBuff[q0] + vupp - r0;
lineBuff[q0+1] ← lineBuff[q0+1] + r0;
lineBuff[q1] ← lineBuff[q1] + r1 - vupp;
lineBuff[q1+1] ← lineBuff[q1+1] - r1;
};
};
ImagerMasks.GenerateRuns[
mask: mask,
clipper: displayData.compositeClipper,
runProc: RunProc,
sTranslate: sTranslate,
fTranslate: fTranslate
];
RunProc[s+vupp+1, 0, 0];
};
ENDCASE => Imager.Error[$UnsupportedColorType];
};
bb: DeviceRectangle ← ImagerMasks.BoundingBox[mask];
bb.sMin ← bb.sMin + sTranslate;
bb.fMin ← bb.fMin + fTranslate;
IF color # displayData.cachedColor THEN {
displayData.cachedColorData ← NIL;
This is where we would calculate any cached color data.
displayData.cachedColor ← color;
};
smthGrayDisplayClass.DoUnderLock[displayData, LockedApplyMask, bb];
};
SpecialOp: PROC[context: Imager.Context, op: ATOM, data: REF] RETURNS[REF] ~ {
SELECT op FROM
$LoadColorMap => ImagerStdColorDisplay.LoadColorMap[vt, data,
            NARROW[context.data, ImagerDisplay.DisplayData],
            LoadColor ];
ENDCASE => Imager.Error[$UnimplementedSpecialOp];
RETURN[ NIL ];
};
LoadColor: ImagerStdColorDisplay.LoadColorProc ~ {
PROC [ color: ConstantColor, colorCalibration: ColorModels.Calibration, mapEntry: [0..256) ]
r, g, b: REAL;
mr, mg, mb: [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]]; 
mr ← Real.FixC[255.0 * RealFns.Power[r, .43]]; 
mg ← Real.FixC[255.0 * RealFns.Power[g, .43]]; 
mb ← Real.FixC[255.0 * RealFns.Power[b, .43]];
vt.SetColor[ mapEntry, 0, mr, mg, mb];
};
Init: PROC[] ~ {
smthGrayImagerClass: ImagerPrivate.Class ~ ImagerDisplay.CreateImagerClass[smthGrayDisplayClass];  -- pick up Imager class record
smthGrayImagerClass.SpecialOp ← SpecialOp;     -- modify procedure bindings
ImagerPrivate.RegisterDevice[smthGrayImagerClass];   -- register device class
vt ← Terminal.Current[];
};
Init[];
END.