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/
row : Int
-- 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
img : Float
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
ᵘ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))))
ᵘe̲dges : Num a => a -> a
ᵘ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))))
ᵘs̲harpen : Num a => a -> a
ᵘ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)))
ᵘe̲mboss : Num a => a -> a
ᵘ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))))
ᵘh̲eat : Float -> Float
ᵘh̲eat ← { p → p + 0.2 × "0 1 0 1 -4 1 0 1 0" ˢs̲tencil< "p" }
smooth : Float
-- 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