AISTest:
CEDAR
PROGRAM
IMPORTS ImagerPixelMap, AIS, Rope
~ BEGIN
ROPE: TYPE ~ Rope.ROPE;
Show:
PROC [name:
ROPE, colorDisplay: ImagerPixelMap.PixelMap] ~
TRUSTED {
ais: AIS.FRef ← AIS.OpenFile[name];
window: AIS.WRef ← AIS.OpenWindow[ais];
lineMap: ImagerPixelMap.PixelMap ← ImagerPixelMap.Create[3, [window.GetWindowParams.firstScan, window.GetWindowParams.firstPixel, 1, window.GetWindowParams.lastPixel+1-window.GetWindowParams.firstPixel]];
lineBufferDesc: AIS.Buffer ← [length: lineMap.refRep.words, addr: lineMap.refRep.pointer];
FOR i:
NAT
IN [window.GetWindowParams.firstScan..window.GetWindowParams.lastScan]
DO
AIS.UnsafeReadLine[window, lineBufferDesc, i];
colorDisplay.Transfer[lineMap];
lineMap.sOrigin ← lineMap.sOrigin + 1;
ENDLOOP;
};
Invert:
PROC [name:
ROPE] ~
TRUSTED {
ais: AIS.FRef ← AIS.OpenFile[name: name, write: TRUE];
window: AIS.WRef ← AIS.OpenWindow[ais];
BufferRep: TYPE ~ RECORD [SEQUENCE length: NAT OF WORD];
lineBuffer: REF BufferRep ← NEW[BufferRep[(window.GetWindowParams.lastPixel+1-window.GetWindowParams.firstPixel+1)/2]];
lineBufferDesc: AIS.Buffer ← [length: lineBuffer.length, addr: @(lineBuffer[0])];
FOR i:
NAT
IN [window.GetWindowParams.firstScan..window.GetWindowParams.lastScan]
DO
AIS.UnsafeReadLine[window, lineBufferDesc, i];
FOR j:
NAT
IN [0..lineBuffer.length)
DO
lineBuffer[j] ← LAST[CARDINAL]-lineBuffer[j];
ENDLOOP;
AIS.UnsafeWriteLine[window, lineBufferDesc, i];
ENDLOOP;
AIS.CloseFile[ais];
};
Reduce:
PROC [name:
ROPE] ~
TRUSTED {
ais: AIS.FRef ← AIS.OpenFile[name: name];
output:
AIS.FRef ←
AIS.CreateFile[name: name, raster: HalfRaster[
AIS.ReadRaster[ais]]];
HalfRaster:
PROC [raster:
AIS.Raster]
RETURNS [
AIS.Raster] ~
CHECKED {
raster.scanCount ← raster.scanCount/2;
raster.scanLength ← raster.scanLength/2;
IF raster.bitsPerPixel # 8 THEN ERROR;
RETURN [raster]
};
window: AIS.WRef ← AIS.OpenWindow[ais];
outputWindow: AIS.WRef ← AIS.OpenWindow[output];
BufferRep: TYPE ~ PACKED ARRAY [0..3000) OF [0..256);
nPerLine: NAT ← window.GetWindowParams.lastPixel+1-window.GetWindowParams.firstPixel;
waste: NAT ← 3000-nPerLine;
lineBuffer1: REF BufferRep ← NEW[BufferRep];
lineBuffer2: REF BufferRep ← NEW[BufferRep];
lineBuffer1Desc: AIS.Buffer ← [length: SIZE[BufferRep], addr: BASE[lineBuffer1^]];
lineBuffer2Desc: AIS.Buffer ← [length: SIZE[BufferRep], addr: BASE[lineBuffer2^]];
AIS.WriteComment[output, AIS.ReadComment[ais].Concat[" Reduced 50%"]];
AIS.WritePhotometry[output, AIS.ReadPhotometry[ais], AIS.ReadHistogram[ais]];
FOR i:
NAT
IN [0..outputWindow.GetWindowParams.lastScan]
DO
AIS.UnsafeReadLine[window, lineBuffer1Desc, 2*i];
AIS.UnsafeReadLine[window, lineBuffer2Desc, MIN[2*i+1, window.GetWindowParams.lastScan]];
FOR j:
NAT
IN [0..nPerLine/2)
DO
j2Next: NAT ~ MIN[2*j+1, nPerLine-1];
lineBuffer1[j] ← (CARDINAL[lineBuffer1[2*j]] + lineBuffer1[j2Next] + lineBuffer2[2*j] + lineBuffer2[j2Next])/4 ;
ENDLOOP;
AIS.UnsafeWriteLine[outputWindow, lineBuffer1Desc, i];
ENDLOOP;
AIS.CloseFile[ais];
AIS.CloseFile[output];
};
Rotate:
PROC [name:
ROPE] ~
TRUSTED {
ais: AIS.FRef ← AIS.OpenFile[name: name];
output:
AIS.FRef ←
AIS.CreateFile[name: name, raster: RotatedRaster[
AIS.ReadRaster[ais]]];
RotatedRaster:
PROC [raster:
AIS.Raster]
RETURNS [
AIS.Raster] ~
CHECKED {
t: CARDINAL;
IF raster.bitsPerPixel # 8 OR raster.scanMode # rd THEN ERROR;
raster.scanMode ← dr;
t ← raster.scanCount;
raster.scanCount ← raster.scanLength;
raster.scanLength ← (t+1)/2*2; -- make even to satisfy a SirPress.UnsafeShowLine restriction
RETURN [raster]
};
window: AIS.WRef ← AIS.OpenWindow[ais];
outputWindow: AIS.WRef ← AIS.OpenWindow[output];
BufferRep: TYPE ~ PACKED ARRAY [0..500) OF [0..256);
nPerLine: NAT ← window.GetWindowParams.lastPixel+1-window.GetWindowParams.firstPixel;
waste: NAT ← 500-nPerLine;
image: ARRAY [0..500) OF REF BufferRep;
lineBuffer: REF BufferRep ← NEW[BufferRep];
lineBufferDesc: AIS.Buffer ← [length: SIZE[BufferRep], addr: BASE[lineBuffer^]];
AIS.WriteComment[output, AIS.ReadComment[ais].Concat[" Rotated scan direction"]];
AIS.WritePhotometry[output, AIS.ReadPhotometry[ais], AIS.ReadHistogram[ais]];
FOR s:
NAT
IN [0..outputWindow.GetWindowParams.lastScan]
DO
image[s] ← NEW[BufferRep];
image[s][outputWindow.GetWindowParams.lastPixel] ← 0;
ENDLOOP;
FOR s:
NAT
IN [0..window.GetWindowParams.lastScan]
DO
AIS.UnsafeReadLine[window, lineBufferDesc, s];
FOR p:
NAT
IN [0..window.GetWindowParams.lastPixel]
DO
image[p][s] ← lineBuffer[p];
ENDLOOP;
ENDLOOP;
FOR s:
NAT
IN [0..outputWindow.GetWindowParams.lastScan]
DO
FOR p:
NAT
IN [0..outputWindow.GetWindowParams.lastPixel]
DO
lineBuffer[p] ← image[s][p];
ENDLOOP;
AIS.UnsafeWriteLine[outputWindow, lineBufferDesc, s];
ENDLOOP;
AIS.CloseFile[ais];
AIS.CloseFile[output];
};
Expand:
PROC [name:
ROPE] ~
TRUSTED {
ais: AIS.FRef ← AIS.OpenFile[name: name];
output:
AIS.FRef ←
AIS.CreateFile[name: name, raster: DoubleRaster[
AIS.ReadRaster[ais]]];
DoubleRaster:
PROC [raster:
AIS.Raster]
RETURNS [
AIS.Raster] ~
CHECKED {
raster.scanCount ← raster.scanCount*2;
raster.scanLength ← raster.scanLength*2;
IF raster.bitsPerPixel # 8 THEN ERROR;
RETURN [raster]
};
window: AIS.WRef ← AIS.OpenWindow[ais];
outputWindow: AIS.WRef ← AIS.OpenWindow[output];
BufferRep: TYPE ~ PACKED ARRAY [0..3000) OF [0..256);
nPerLine: NAT ← window.GetWindowParams.lastPixel+1-window.GetWindowParams.firstPixel;
waste: NAT ← 3000-nPerLine;
lineBuffer1: REF BufferRep ← NEW[BufferRep];
lineBuffer2: REF BufferRep ← NEW[BufferRep];
lineBuffer1Desc: AIS.Buffer ← [length: SIZE[BufferRep], addr: BASE[lineBuffer1^]];
lineBuffer2Desc: AIS.Buffer ← [length: SIZE[BufferRep], addr: BASE[lineBuffer2^]];
AIS.WriteComment[output, AIS.ReadComment[ais].Concat[" Doubled"]];
AIS.WritePhotometry[output, AIS.ReadPhotometry[ais], AIS.ReadHistogram[ais]];
FOR i:
NAT
IN [0..window.GetWindowParams.lastScan]
DO
AIS.UnsafeReadLine[window, lineBuffer1Desc, i];
FOR j:
NAT
IN [0..nPerLine)
DO
lineBuffer2[2*j] ← lineBuffer2[2*j+1] ← lineBuffer1[j];
ENDLOOP;
AIS.UnsafeWriteLine[outputWindow, lineBuffer2Desc, 2*i];
AIS.UnsafeWriteLine[outputWindow, lineBuffer2Desc, 2*i+1];
ENDLOOP;
AIS.CloseFile[ais];
AIS.CloseFile[output];
};
END.