NoiseTest:
PROC[x, y:
REAL]
RETURNS [
REAL] ~ {
x01: REAL ~ x;
y01: REAL ~ y;
x ← XF[x, x0, x1];
y ← XF[y, y0, y1];
SELECT type FROM
noise =>
RETURN [Noise01[x * y, y]];
-- noise: [x0, x1, y0, y1] ← [10, 8, -2.2, 8]
bumps => {
-- bumps: [x0, x1, y0, y1] ← [10, 8, -2.2, 8]
freq: REAL ← f0;
dz: REAL ← 0;
FOR i:
INT
IN [0..1)
DO
FNoise:
PROC[x, y:
REAL]
RETURNS [
REAL] ~ {
RETURN [Noise01[x * freq, y * freq] / freq];
};
dz ← dz + (FNoise[x + epsilon, y] - FNoise[x, y]) / epsilon;
freq ← freq * 2;
ENDLOOP;
dz ← (1 + dz)/2;
IF dz < 0 THEN RETURN [0]
ELSE IF dz > 1 THEN RETURN [1]
ELSE RETURN [dz]
};
bark, -- bark: [x0, x1, y0, y1] ← [10, 8, -2.2, 4]
rock => {
-- rock: [x0, x1, y0, y1] ← [10, 4, -2.2, 4]
freq: REAL ← f0;
dz: REAL ← 0;
FOR i:
INT
IN [0..n)
DO
FNoise:
PROC[x, y:
REAL]
RETURNS [
REAL] ~ {
RETURN [Noise01[x * freq, y * freq] / freq];
};
dz ← dz + (FNoise[x + epsilon, y] - FNoise[x, y]) / epsilon;
freq ← freq * 2;
ENDLOOP;
dz ← dz / n * 2;
dz ← (1 + dz)/2;
IF dz < 0 THEN RETURN [0]
ELSE IF dz > 1 THEN RETURN [1]
ELSE RETURN [dz]
};
elevation => {
-- elevation: [x0, x1, y0, y1] ← [10, 4, -2.2, 4]
freq: REAL ← f0;
dz: REAL ← 0;
FOR i:
INT
IN [0..n)
DO
dz ← dz + Noise3[x * freq, y * freq, 0.5] / freq;
freq ← freq * 2;
ENDLOOP;
dz ← dz / n * evScale;
IF christmas THEN {
t: REAL ~ 4*(x01-0.5)*(x01-0.5)/((1.001-y01)*(1.001-y01));
dz ← dz / (1-t);
dz ← dz + 0.5-t;
RETURN [0.5-t];
};
dz ← (1 + dz)/2;
RETURN [dz]
};
elevationGrad => {
-- elevationGrad: [x0, x1, y0, y1] ← [10, 4, -2.2, 4]
freq: REAL ← f0;
dz: REAL ← 0;
FOR i:
INT
IN [0..n)
DO
z0: REAL ← Noise3[x * freq, y * freq, 0.5];
z1: REAL ← Noise3[(x+epsilon) * freq, (y-epsilon) * freq, 0.5];
dz ← dz + (z1-z0)/(epsilon * freq);
freq ← freq * 2;
ENDLOOP;
dz ← (1 + dz*evScale)/2;
RETURN [MIN[MAX[dz, 0], 1]]
};
drops => {
-- drops: [x0, x1, y0, y1] ← [10, 4, -2.2, 4]
za, zb, z, dz: REAL;
za ← Noise3[x, y, 0];
zb ← Noise3[x+epsilon, y, 0];
z← XF[za, z0, z1];
dz← XF[(zb-za)/epsilon, 0, z1];
IF z < zlo THEN RETURN [0]
ELSE IF dz < -1 THEN RETURN [0]
ELSE IF dz > 1 THEN RETURN [1]
ELSE RETURN [(1 + dz)/2]
};
sine => {
-- sine: [x0, x1, y0, y1] ← [10, 8, -2.2, 8]
RETURN [Wave[x]]
};
dots => {
-- dots: [x0, x1, y0, y1] ← [10, 8, -2.2, 8]
RETURN [DotGrid[x, y]]
};
swirl1 => {
-- swirl1: [x0, x1, y0, y1] ← [10, 8, -2.2, 8]
freq: REAL ← f0;
z: REAL ← 0;
FOR i:
INT
IN [0..1)
DO
FNoise:
PROC[x, y:
REAL]
RETURNS [
REAL] ~ {
RETURN [Noise3[x * freq, y * freq, 0] / freq];
};
zz: REAL ← FNoise[x, y];
z ← z + zz * zz * freq;
freq ← freq * 2;
ENDLOOP;
z ← z * 6;
RETURN [Wave[x + z]]
};
marble1 => {
-- marble1: [x0, x1, y0, y1] ← [10, 8, -2.2, 8]
freq: REAL ← f0;
z: REAL ← 0;
FOR i:
INT
IN [0..1)
DO
FNoise:
PROC[x, y:
REAL]
RETURNS [
REAL] ~ {
RETURN [Noise3[x * freq, y * freq, 0] / freq];
};
z ← z + ABS[FNoise[x, y]];
freq ← freq * 2;
ENDLOOP;
z ← z * 3;
RETURN [Wave[x + z]]
};
tree => {
freq: REAL ← f0 / 2;
famp: REAL ← 1.0;
z: REAL ← 0;
IF 2*ABS[x01-0.5] > 1-y01 THEN RETURN [0];
FOR i:
INT
IN [0..n)
DO
FNoise:
PROC[x, y:
REAL]
RETURNS [
REAL] ~ {
RETURN [Noise3[x * freq, y * freq, 0] * famp];
};
z ← z + ABS[FNoise[x, y]];
z ← z + FNoise[x, y];
freq ← freq * 2;
famp ← famp * 0.7;
ENDLOOP;
z ← z / n * 24;
zTreeSum ← zTreeSum + z;
zTreeN ← zTreeN + 1;
z ← (z-zTreeMean) * fudgeCone;
RETURN [Cone[x01 + z, y01 + z]]
};
swirl => {
-- swirl: [x0, x1, y0, y1] ← [10, 8, -2.2, 8]
freq: REAL ← f0 / 2;
z: REAL ← 0;
FOR i:
INT
IN [0..n)
DO
FNoise:
PROC[x, y:
REAL]
RETURNS [
REAL] ~ {
RETURN [Noise3[x * freq, y * freq, 0] / freq];
};
zz: REAL ← FNoise[x, y];
z ← z + zz * zz * freq;
freq ← freq * 2;
ENDLOOP;
z ← z / n * 16;
RETURN [Wave[x + z] ]
};
marble => {
-- marble: [x0, x1, y0, y1] ← [10, 8, -2.2, 8]
freq: REAL ← f0 / 2;
z: REAL ← 0;
FOR i:
INT
IN [0..n)
DO
FNoise:
PROC[x, y:
REAL]
RETURNS [
REAL] ~ {
RETURN [Noise3[x * freq, y * freq, 0] / freq];
};
z ← z + ABS[FNoise[x, y]];
freq ← freq * 2;
ENDLOOP;
z ← z / n * 8;
RETURN [Wave[x + z] ]
};
chmarble => {
-- chmarble: [x0, x1, y0, y1] ← [10, 8, -2.2, 8]
freq: REAL ← f0 / 2;
z: REAL ← 0;
FOR i:
INT
IN [0..n)
DO
FNoise:
PROC[x, y:
REAL]
RETURNS [
REAL] ~ {
RETURN [Noise3[x * freq, y * freq, 0] / freq];
};
z ← z + ABS[FNoise[x, y]];
freq ← freq * 2;
ENDLOOP;
z ← z / n * 8;
RETURN [Wave[x + z] * Wave[y + z]]
};
dotRoil => {
-- dotRoil: [x0, x1, y0, y1] ← [10, 8, -2.2, 8]
freq: REAL ← f0 / 2;
z: REAL ← 0;
FOR i:
INT
IN [0..n)
DO
FNoise:
PROC[x, y:
REAL]
RETURNS [
REAL] ~ {
RETURN [Noise3[x * freq, y * freq, 0] / freq];
};
z ← z + ABS[FNoise[x, y]];
freq ← freq * 2;
ENDLOOP;
z ← z / n * 24;
RETURN [DotGrid[x + z, y]]
};
smoke => {
-- smoke: [x0, x1, y0, y1] ← [10, 8, -2.2, 8]
freq: REAL ← f0;
z: REAL ← 0;
FOR i:
INT
IN [0..n)
DO
FNoise:
PROC[x, y:
REAL]
RETURNS [
REAL] ~ {
RETURN [Noise3[x * freq, y * freq, 0] / freq];
};
z ← z + ABS[FNoise[x, y]];
freq ← freq * 2;
ENDLOOP;
z ← z / n * 8;
z ← Wave[x + z];
IF z < 0 THEN RETURN [0]
ELSE IF z > 1 THEN RETURN [1]
ELSE RETURN [z]
};
ENDCASE => RETURN [0];
};
FunctionAIS:
PUBLIC
PROC [outputName:
ROPE, width, height:
NAT, f:
PROC [x, y:
REAL]
RETURNS [
REAL], bitsPerPixel:
NAT ← 8] ~
TRUSTED {
raster:
AIS.Raster ←
NEW[
AIS.RasterPart ← [
scanCount: height,
scanLength: width,
scanMode: rd,
bitsPerPixel: bitsPerPixel,
linesPerBlock: -1,
paddingPerBlock: 177777B
]];
maxPixelValue: NAT ← Real.Round[RealFns.Exp[bitsPerPixel*RealFns.Ln[2]]]-1;
output: AIS.FRef ← AIS.CreateFile[name: outputName, raster: raster];
outputWindow: AIS.WRef ← AIS.OpenWindow[output];
BufferRep8: TYPE ~ PACKED ARRAY [0..3000) OF [0..256);
lineBuffer: REF BufferRep8 ← NEW[BufferRep8];
lineBufferDesc: AIS.Buffer ← [length: SIZE[BufferRep8], addr: BASE[lineBuffer^]];
min: REAL ← 0.0;
max: REAL ← 1.0;
inner:
SAFE
PROC ~
TRUSTED {
scale: REAL ~ maxPixelValue/(max-min);
FOR i:
NAT
IN [0..outputWindow.GetWindowParams.lastScan]
DO
y: REAL ← (height-0.5-i)/height;
Process.CheckForAbort[];
lineBuffer^ ← ALL[0];
FOR j:
NAT
IN [0..width)
DO
x: REAL ← (0.5+j)/width;
pixel: [0..256) ← MIN[MAX[Real.RoundLI[(f[x,y]-min)*scale], 0], maxPixelValue];
IF pixel>0
THEN {
bitIndex: INT = LONG[j]*bitsPerPixel;
bufIndex: NAT = bitIndex/8;
lineBuffer[bufIndex] ← lineBuffer[bufIndex]+
Basics.BITSHIFT[pixel, 8-bitsPerPixel-(bitIndex MOD 8)];
};
ENDLOOP;
AIS.UnsafeWriteLine[outputWindow, lineBufferDesc, i];
ENDLOOP;
};
IF autoRange > 0
THEN {
random: Random.RandomStream ← Random.Create[];
m: REAL ← 0;
min ← 99999999.0;
max ← -99999999.0;
FOR i:
INT
IN [0..autoRange)
DO
x: REAL ← Real.FScale[Random.ChooseInt[random, 0, LONG[256]*256*256], -24];
y: REAL ← Real.FScale[Random.ChooseInt[random, 0, LONG[256]*256*256], -24];
z: REAL ← f[x,y];
m ← m + z;
min ← MIN[min, z];
max ← MAX[max, z];
ENDLOOP;
IF max = min THEN max ← max + 3.90625e-3;
m ← (max+min)/2;
max ← m + (max-m)/confidence;
min ← m - (m-min)/confidence;
};
CedarProcess.DoWithPriority[background, inner];
AIS.CloseFile[output];
};
ValueRange:
PROC [inputName:
ROPE]
RETURNS [minvalue, maxvalue:
REAL] ~
TRUSTED {
ais: AIS.FRef ← AIS.OpenFile[name: inputName];
window: AIS.WRef ← AIS.OpenWindow[ais];
BufferRep: TYPE ~ PACKED ARRAY [0..3000) OF [0..256);
nPerLine: NAT ← window.GetWindowParams.lastPixel+1-window.GetWindowParams.firstPixel;
lineBuffer1: REF BufferRep ← NEW[BufferRep];
lineBuffer1Desc: AIS.Buffer ← [length: SIZE[BufferRep], addr: BASE[lineBuffer1^]];
minvalue ← 1.0;
maxvalue ← 0.0;
FOR i:
NAT
IN [0..window.GetWindowParams.lastScan]
DO
AIS.UnsafeReadLine[window, lineBuffer1Desc, i];
FOR j:
NAT
IN [0..nPerLine)
DO
pix: REAL ← lineBuffer1[j]/255.0;
minvalue ← MIN[pix, minvalue];
maxvalue ← MAX[pix, maxvalue];
ENDLOOP;
ENDLOOP;
AIS.CloseFile[ais];
};
ChangeContrast:
PROC [inputName, outputName:
ROPE, oldminvalue, oldmaxvalue, newminvalue, newmaxvalue:
REAL] ~
TRUSTED {
ais: AIS.FRef ← AIS.OpenFile[name: inputName];
raster: AIS.Raster ← AIS.ReadRaster[ais];
output: AIS.FRef ← AIS.CreateFile[name: outputName, raster: 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;
outBuf: REF BufferRep ← NEW[BufferRep];
lineBuffer1: REF BufferRep ← NEW[BufferRep];
lineBuffer1Desc: AIS.Buffer ← [length: SIZE[BufferRep], addr: BASE[lineBuffer1^]];
outBufferDesc: AIS.Buffer ← [length: SIZE[BufferRep], addr: BASE[outBuf^]];
m: REAL ← (newmaxvalue-newminvalue)/(oldmaxvalue-oldminvalue);
FOR i:
NAT
IN [0..outputWindow.GetWindowParams.lastScan]
DO
AIS.UnsafeReadLine[window, lineBuffer1Desc, i];
FOR j:
NAT
IN [0..nPerLine)
DO
pix: REAL ← lineBuffer1[j]/255.0;
out: REAL ← (pix-oldminvalue)*m+newminvalue;
outBuf[j] ← MIN[MAX[Real.RoundLI[out*255], 0], 255];
ENDLOOP;
AIS.UnsafeWriteLine[outputWindow, outBufferDesc, i];
ENDLOOP;
AIS.CloseFile[ais];
AIS.CloseFile[output];
};