CtPixImpl.mesa
Copyright Ó 1985, 1992 by Xerox Corporation. All rights reserved.
Bloomenthal, July 3, 1992 1:26 pm PDT
Andrew Glassner November 10, 1990 2:49 pm PST
DIRECTORY CtBasic, CtPix, ImagerSample, Process, Random, Real;
CtPixImpl: CEDAR PROGRAM
IMPORTS ImagerSample, Process, Random, Real
EXPORTS CtPix
~ BEGIN
Box:   TYPE ~ ImagerSample.Box;
Vec:   TYPE ~ ImagerSample.Vec;
SampleMap: TYPE ~ ImagerSample.SampleMap;
SampleMaps: TYPE ~ CtBasic.SampleMaps;
Color Display Modification Procedures:
Dither: PUBLIC PROC [map: SampleMap, intensity: CARDINAL] ~ {
box: Box ¬ ImagerSample.GetBox[map];
IF intensity = 0 AND ImagerSample.GetBitsPerSample[map] = 8 THEN {
FOR y: INTEGER IN [box.min.s..box.max.s) DO
Process.CheckForAbort[];
FOR x: INTEGER IN [box.min.f..box.max.f) DO
pval: CARDINAL ~ ImagerSample.Get[map, [y, x]];
v: CARDINAL ~ IF CARD[Random.ChooseInt[, 0, 255]] > pval THEN 0 ELSE 255;
ImagerSample.Put[map, [y, x], v];
ENDLOOP;
ENDLOOP;
RETURN;
};
FOR y: INTEGER IN [box.min.s..box.max.s) DO
Process.CheckForAbort[];
FOR x: INTEGER IN [box.min.f..box.max.f) DO
v: CARDINAL ¬ IF CARD[Random.ChooseInt[, 0, 255]] > intensity THEN 0 ELSE 255;
ImagerSample.Put[map, [y, x], v];
ENDLOOP;
ENDLOOP;
};
RampH: PUBLIC PROC [map: SampleMap] ~ {
box: Box ¬ ImagerSample.GetBox[map];
size: Vec ¬ ImagerSample.GetSize[map];
inc: REAL ¬ 255.0/(size.s-1);
Action: PROC [samples: ImagerSample.SampleBuffer] ~ TRUSTED {
FOR y: INTEGER IN [box.min.s..box.max.s) DO
ival: CARDINAL ¬ Real.Round[(y-box.min.s)*inc];
ival ← 256*ival+ival;         
FOR x: INTEGER IN [0..size.f) DO samples[x] ¬ ival; ENDLOOP;
ImagerSample.PutSamples[map, [y, box.min.f],, samples, 0, size.f];
ENDLOOP;
};
ImagerSample.DoWithScratchSamples[size.f, Action];
};
RampV: PUBLIC PROC [map: SampleMap] ~ {
box: Box ¬ ImagerSample.GetBox[map];
size: Vec ¬ ImagerSample.GetSize[map];
inc: REAL ¬ 255.0/(size.f-1);
Action: PROC [samples: ImagerSample.SampleBuffer] ~ TRUSTED {
FOR x: INTEGER IN [0..size.f) DO
v: CARDINAL ¬ Real.Round[x*inc];
samples[x] ← 256*v+v;        -- for 8 or 16 bit pixel maps
samples[x] ¬ v;        
ENDLOOP;
FOR y: INTEGER IN [box.min.s..box.max.s) DO
ImagerSample.PutSamples[map, [y, box.min.f],, samples, 0, size.f];
ENDLOOP;
};
ImagerSample.DoWithScratchSamples[size.f, Action];
};
Pie: PUBLIC PROC [map: SampleMap, center: Vec, nRings: INTEGER] ~ {
Init: PROC ~ {
val: INT ¬ 0; dval: INT ¬ 1; ddval: INT ~ 2;
factor: REAL ¬ ratio*ratio;
FOR i: NAT IN [0..512] DO sq[i] ¬ val; val ¬ val + dval; dval ¬ dval + ddval; ENDLOOP;
FOR i: NAT IN [0..256] DO threshs[i] ¬ Real.Round[factor*sq[i]]; ENDLOOP;
};
Scan: PROC [y: CARDINAL] ~ {
xStart: INTEGER ¬ 0;
pval: CARDINAL ¬ MIN[255, MAX[1, Real.Round[REAL[y]/ratio]]];
thresh: INT ¬ threshs[pval]-sq[y];
FOR x: NAT IN [0..dx] DO
WHILE sq[x] > thresh AND pval < 255 DO
pval ¬ pval+1;
thresh ¬ threshs[pval]-sq[y];
ENDLOOP;
IF pval = 255 THEN {xStart ¬ dx-x; EXIT};
line[dx-x] ¬ line[dx+x] ¬ pval;
ENDLOOP;
ImagerSample.PutSamples[map, [cy+y, box.min.f+xStart],, line, xStart, size.f-2*xStart];
ImagerSample.PutSamples[map, [cy-y, box.min.f+xStart],, line, xStart, size.f-2*xStart];
};
box: Box ¬ ImagerSample.GetBox[map];
size: Vec ¬ ImagerSample.GetSize[map];
line: ImagerSample.SampleBuffer ~ ImagerSample.ObtainScratchSamples[size.f];
dx: NAT ~ MIN[511, (size.f+1)/2-1];
dy: NAT ~ MIN[511, (size.s+1)/2-1];
cx: NAT ~ box.min.f+dx;
cy: NAT ~ box.min.s+dy;
sq: ARRAY [0..512] OF INT;
threshs: ARRAY [0..256] OF INT;
ratio: REAL ¬ Real.Round[dx+1]/256.0;
Init[];
FOR y: NAT IN [0..dy] DO Process.CheckForAbort[]; Scan[y]; ENDLOOP;
ImagerSample.ReleaseScratchSamples[line];
};
END.