programdemos/stencil-macros/stencil-macros.xtl

Stencils by macro: image kernels written as pictures of numbers, and a macro library of our own (Stencil.xtlm, beside this file) that turns each into rotation arithmetic when the program is expanded. A 0 in a kernel writes no code at all and a 1 no multiply, so blur, edges, sharpen, emboss and heat each cost exactly their nonzero numbers. Run it with "xetal run demos/stencil-macros/stencil-macros.xtl"; it prints the totals a kernel keeps (blur and heat move the picture's total without changing it, edges sum to 0), then the picture and each result drawn with characters. See what the program becomes: "xetal expand demos/stencil-macros/stencil-macros.xtl". The live page lets you edit a kernel and see its expansion: https://softwarewrighter.github.io/X_eTaL-demos/stencil-macros/

source · imports s: demos/stencil-macros/Stencil.xtlm

rows : Int

value · line 19

rows, cols: the picture's size.

rows ← 24
Used in: row, col

cols : Int

value · line 20
cols ← 48
Used in: row, col

row : Int

value · line 25

-- the picture: masks of coordinates, and noise. row, col: by broadcasting. disk, box: two masks. img: their brightness, plus a little noise.

row ← (o̲ffsets rows) 'l̲eft t̲able o̲ffsets cols
Used in: disk, box, img, spot

col : Int

value · line 26
col ← (o̲ffsets rows) 'r̲ight t̲able o̲ffsets cols
Used in: disk, box, spot

disk : Bool

value · line 27
disk ← (((row − 11) × row − 11) + ((col − 12) × col − 12) d̲iv 2) < 49
Used in: img

box : Bool

value · line 28
box ← ((row ≥ 5) ∧ row < 19) ∧ (col ≥ 28) ∧ col < 41
Used in: img

img : Float

value · line 29
img ← 0.1 + (0.6 × f̲loat disk) + (0.3 × f̲loat box) + 0.001 × f̲loat r̲oll! (s̲hape row) r̲eshape 50

ᵘb̲lur : Num a => a -> Float

function · line 35
ᵘb̲lur ← { p → ("1 2 1  2 4 2  1 2 1" ˢs̲tencil< "p") ÷ 16 }
ˢs̲tencil< expands to
(((-1 o̲-₁ (-1 o̲-₂ p)) + (2 × (-1 o̲-₁ p)) + (-1 o̲-₁ (1 o̲-₂ p)) + (2 × (-1 o̲-₂ p)) + (4 × p) + (2 × (1 o̲-₂ p)) + (1 o̲-₁ (-1 o̲-₂ p)) + (2 × (1 o̲-₁ p)) + (1 o̲-₁ (1 o̲-₂ p))))
Used in: smooth

ᵘe̲dges : Num a => a -> a

function · line 36
ᵘe̲dges ← { p → "-1 -1 -1  -1 8 -1  -1 -1 -1" ˢs̲tencil< "p" }
ˢs̲tencil< expands to
((n̲eg (-1 o̲-₁ (-1 o̲-₂ p))) + (n̲eg (-1 o̲-₁ p)) + (n̲eg (-1 o̲-₁ (1 o̲-₂ p))) + (n̲eg (-1 o̲-₂ p)) + (8 × p) + (n̲eg (1 o̲-₂ p)) + (n̲eg (1 o̲-₁ (-1 o̲-₂ p))) + (n̲eg (1 o̲-₁ p)) + (n̲eg (1 o̲-₁ (1 o̲-₂ p))))
Used in: edges

ᵘs̲harpen : Num a => a -> a

function · line 37
ᵘs̲harpen ← { p → "0 -1 0  -1 5 -1  0 -1 0" ˢs̲tencil< "p" }
ˢs̲tencil< expands to
((n̲eg (-1 o̲-₁ p)) + (n̲eg (-1 o̲-₂ p)) + (5 × p) + (n̲eg (1 o̲-₂ p)) + (n̲eg (1 o̲-₁ p)))
Used in: sharp

ᵘe̲mboss : Num a => a -> a

function · line 38
ᵘe̲mboss ← { p → "-2 -1 0  -1 1 1  0 1 2" ˢs̲tencil< "p" }
ˢs̲tencil< expands to
((-2 × (-1 o̲-₁ (-1 o̲-₂ p))) + (n̲eg (-1 o̲-₁ p)) + (n̲eg (-1 o̲-₂ p)) + p + (1 o̲-₂ p) + (1 o̲-₁ p) + (2 × (1 o̲-₁ (1 o̲-₂ p))))
Used in: relief

ᵘh̲eat : Float -> Float

function · line 42
ᵘh̲eat ← { p → p + 0.2 × "0 1 0  1 -4 1  0 1 0" ˢs̲tencil< "p" }
ˢs̲tencil< expands to
(((-1 o̲-₁ p) + (-1 o̲-₂ p) + (-4 × p) + (1 o̲-₂ p) + (1 o̲-₁ p)))
Used in: warm

smooth : Float

value · line 47

-- the results. smooth, edges, sharp, relief: the four kernels applied to the picture. spot, warm: one hot cell, and the heat after 60 steps (function power).

smooth ← ᵘb̲lur img

edges : Float

value · line 48
edges ← ᵘe̲dges smooth

sharp : Float

value · line 49
sharp ← ᵘs̲harpen img

relief : Float

value · line 50
relief ← ᵘe̲mboss img

spot : Float

value · line 51
spot ← 1000.0 × f̲loat (row = 12) ∧ col = 24
Used in: warm

warm : Float

value · line 52
warm ← 60 'ᵘh̲eat p̲ower spot

ᵘd̲raw : Float -> Char

function · line 64

u:d_raw a: the picture, smoothed, its edges, sharpened, embossed, and the heat of one hot cell after 60 steps, drawn with characters (darkest first).

ᵘd̲raw ← { a → (1 + f̲loor 0 m̲ax 7 m̲in 8.0 × a) s̲elect " .:-=+*#" }