sourcedemos/stencil-macros/stencil-macros.xtl
1⍝!/usr/bin/env xetal
2⍝# Stencils by macro: image kernels written as pictures of numbers,
3⍝# and a macro library of our own (Stencil.xtlm, beside this file)
4⍝# that turns each into rotation arithmetic when the program is
5⍝# expanded. A 0 in a kernel writes no code at all and a 1 no
6⍝# multiply, so blur, edges, sharpen, emboss and heat each cost
7⍝# exactly their nonzero numbers. Run it with "xetal run
8⍝# demos/stencil-macros/stencil-macros.xtl"; it prints the totals a
9⍝# kernel keeps (blur and heat move the picture's total without
10⍝# changing it, edges sum to 0), then the picture and each result
11⍝# drawn with characters. See what the program becomes: "xetal expand
12⍝# demos/stencil-macros/stencil-macros.xtl". The live page lets you
13⍝# edit a kernel and see its expansion:
14⍝# https://softwarewrighter.github.io/X_eTaL-demos/stencil-macros/
15
16ˢ⁼u̲se< "Stencil"
17
18⍝# rows, cols: the picture's size.
19rows ← 24
20cols ← 48
21
22⍝# -- the picture: masks of coordinates, and noise. row, col: by
23⍝# broadcasting. disk, box: two masks. img: their brightness, plus a
24⍝# little noise.
25row ← (o̲ffsets rows) 'l̲eft t̲able o̲ffsets cols
26col ← (o̲ffsets rows) 'r̲ight t̲able o̲ffsets cols
27disk ← (((row − 11) × row − 11) + ((col − 12) × col − 12) d̲iv 2) < 49
28box ← ((row ≥ 5) ∧ row < 19) ∧ (col ≥ 28) ∧ col < 41
29img ← 0.1 + (0.6 × f̲loat disk) + (0.3 × f̲loat box) + 0.001 × f̲loat r̲oll! (s̲hape row) r̲eshape 50
30⍝ -- the stencils: kernels written as pictures (plain #: the page's
31⍝ web app finds this exact text to cut the program it runs, right up
32⍝ to it, so a ## here would leave a stray # in what it cuts). Each
33⍝ kernel is three rows of three numbers; the macro writes the sum of
34⍝ the shifted copies they weigh, and skips every 0.
35ᵘb̲lur ← { p → ("1 2 1 2 4 2 1 2 1" ˢs̲tencil< "p") ÷ 16 }
36ᵘe̲dges ← { p → "-1 -1 -1 -1 8 -1 -1 -1 -1" ˢs̲tencil< "p" }
37ᵘs̲harpen ← { p → "0 -1 0 -1 5 -1 0 -1 0" ˢs̲tencil< "p" }
38ᵘe̲mboss ← { p → "-2 -1 0 -1 1 1 0 1 2" ˢs̲tencil< "p" }
39⍝ Heat flows to the cooler neighbors: one step adds a
40⍝ fifth of the Laplacian (its four neighbors less four
41⍝ times itself).
42ᵘh̲eat ← { p → p + 0.2 × "0 1 0 1 -4 1 0 1 0" ˢs̲tencil< "p" }
43
44⍝# -- the results. smooth, edges, sharp, relief: the four kernels
45⍝# applied to the picture. spot, warm: one hot cell, and the heat
46⍝# after 60 steps (function power).
47smooth ← ᵘb̲lur img
48edges ← ᵘe̲dges smooth
49sharp ← ᵘs̲harpen img
50relief ← ᵘe̲mboss img
51spot ← 1000.0 × f̲loat (row = 12) ∧ col = 24
52warm ← 60 'ᵘh̲eat p̲ower spot
53⍝ -- end of the core --------------------------------
54
55⍝ What a kernel keeps: blur and heat move the picture's total without
56⍝ changing it (their numbers sum to 1, the edges wrap), edges sum to 0.
57f̲loor 0.5 + '+ r̲/ r̲avel img
58f̲loor 0.5 + '+ r̲/ r̲avel smooth
59f̲loor 0.5 + '+ r̲/ r̲avel warm
60f̲loor 0.5 + 1000000.0 × '+ r̲/ r̲avel edges
61⍝# u:d_raw a: the picture, smoothed, its edges, sharpened, embossed,
62⍝# and the heat of one hot cell after 60 steps, drawn with characters
63⍝# (darkest first).
64ᵘd̲raw ← { a → (1 + f̲loor 0 m̲ax 7 m̲in 8.0 × a) s̲elect " .:-=+*#" }
65ᵘd̲raw img
66ᵘd̲raw smooth
67ᵘd̲raw 4.0 × a̲bs edges
68ᵘd̲raw sharp
69ᵘd̲raw relief
70ᵘd̲raw warm ÷ 8.0
ˢ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))))
ˢ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̲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)))
ˢ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))))