HistImpl.mesa
Mik Lamming - October 23, 1984 11:45:56 am PDT
DIRECTORY AIS, Hist, Real, Rope;
HistImpl: CEDAR PROGRAM
IMPORTS AIS, Real
EXPORTS Hist
= BEGIN OPEN Hist;
Calc: PUBLIC PROC [filename:Rope.ROPE, low,high: NAT] RETURNS [histogram: Histogram] ~ {
Buffer: TYPE = REF BufferRec;
BufferRec: TYPE = PACKED ARRAY [0..2048) OF [0..255];
fdIn: AIS.FRef;
wIn: AIS.WRef;
rIn:AIS.Raster;
buffer:Buffer ← NEW[BufferRec];
counts: ARRAY[0..255] OF LONG CARDINALALL[0];
nPixels:REAL ← 0;
histogram ← NEW[HistogramRec];
fdIn ← AIS.OpenFile[name:filename];
wIn ← AIS.OpenWindow[fdIn];
rIn ← AIS.ReadRaster[fdIn];
FOR line: NAT IN [0.. rIn.scanCount) DO
TRUSTED {
AIS.UnsafeReadLine[w:wIn, buffer:[length:rIn.scanLength, addr:LOOPHOLE[buffer]], line:line]
};
FOR pixel: NAT IN [0..rIn.scanLength) DO
IF buffer[pixel] IN [low..high] THEN {
nPixels ← nPixels + 1;
counts[buffer[pixel]] ← counts[buffer[pixel]] + 1;
}
ENDLOOP;
ENDLOOP;
FOR i: NAT IN [0..255] DO
histogram[i] ← counts[i]/nPixels;
ENDLOOP;
AIS.CloseWindow[wIn];
AIS.CloseFile[fdIn];
};
Equalize: PUBLIC PROC [histogram: Histogram] RETURNS [trcVec: TRCVec] ~ {
currentFraction: REAL ← 0;
trcVec ← NEW[TRCVecRec];
FOR i: NAT IN [0..255] DO
currentFraction ← currentFraction + histogram[i];
trcVec[i].out ← MIN[Real.RoundC[currentFraction*256], 255];
trcVec[i].pinned ← FALSE;
ENDLOOP;
};
END.