1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
############################################################################
#
# File: patutils.icn
#
# Subject: Procedures to manipulate patterns
#
# Author: Ralph E. Griswold
#
# Date: July 8, 2002
#
############################################################################
#
# This file is in the public domain.
#
############################################################################
#
# This file contains procedures that manipulate graphic pattern
# representations. These procedures are intended for bi-level patterns
# representable by 0s and 1s.
#
# A row pattern is a list of strings, with each string representing
# a row in the pattern.
#
# DrawTile(win, xoff, yoff, pattern, magnif, mode)
# DrawRows(win, xoff, yoff, rows, magnif, mode)
# bits2hex(s)
# decspec(pattern)
# eqpats(prws, rows2)
# getpatt(line)
# getpattnote(line)
# hex2bits(s)
# hexspec(pattern)
# legalpat(tile)
# legaltile(tile)
# pat2xbm(pattern, name)
# tilebits(rows)
# pdensity(pattern)
# pix2pat(window, x, y, cols, rows)
# readpatt(input)
# readpattline(input)
# rowbits(pattern)
# pat2rows(pattern)
# rows2pat(rlist)
# showbits(pattern)
# tiledim(pattern)
# xbm2rows(input)
#
############################################################################
#
# Requires: Version 9 graphics
#
############################################################################
#
# Links: convert
#
############################################################################
link convert
record tdim(w, h)
#
# Draw a tile at a given location. If mode is nonnull, the
# area on which the tile is drawn is erased.
procedure DrawTile(win, xoff, yoff, pattern, magnif, mode)
local x, y, row, pixel, dims, arglist
if type(win) ~== "window" then {
win :=: xoff :=: yoff :=: pattern :=: mode
win := &window
}
/magnif := 1
y := yoff
if \mode then {
dims := tiledim(pattern)
EraseArea(xoff, yoff, dims.w * magnif, dims.h * magnif)
}
every row := rowbits(pattern) do { # draw a row
x := xoff
arglist := []
if magnif = 1 then {
every pixel := !row do {
if pixel == "1" then put(arglist, x, y)
x +:= 1
}
y +:= 1
}
else {
every pixel := !row do {
if pixel == "1" then put(arglist, x, y, magnif, magnif)
x +:= magnif
}
y +:= magnif
}
if *arglist = 0 then next
if magnif = 1 then DrawPoint ! arglist else FillRectangle ! arglist
}
return
end
#
# Draw rows at a given location. If mode is nonnull, the
# area on which the tile is drawn is erased.
procedure DrawRows(win, xoff, yoff, rows, magnif, mode)
local x, y, row, pixel, arglist
if type(win) ~== "window" then {
win :=: xoff :=: yoff :=: rows :=: magnif :=: mode
win := &window
}
/magnif := 1
y := yoff
if \mode then
EraseArea(xoff, yoff, *rows[1] * magnif, *rows * magnif)
every row := !rows do { # draw a row
x := xoff
arglist := []
if magnif = 1 then {
every pixel := !row do {
if pixel == "1" then put(arglist, x, y)
x +:= 1
}
y +:= 1
}
else {
every pixel := !row do {
if pixel = "1" then put(arglist, x, y, magnif, magnif)
x +:= magnif
}
y +:= magnif
}
if *arglist = 0 then next
if magnif = 1 then DrawPoint ! arglist else FillRectangle ! arglist
}
return
end
#
# Convert bit string to hex pattern string
procedure bits2hex(s)
static bittab
local hex
initial {
bittab := table()
bittab["0000"] := "0"
bittab["1000"] := "1"
bittab["0100"] := "2"
bittab["1100"] := "3"
bittab["0010"] := "4"
bittab["1010"] := "5"
bittab["0110"] := "6"
bittab["1110"] := "7"
bittab["0001"] := "8"
bittab["1001"] := "9"
bittab["0101"] := "a"
bittab["1101"] := "b"
bittab["0011"] := "c"
bittab["1011"] := "d"
bittab["0111"] := "e"
bittab["1111"] := "f"
}
hex := ""
s ? {
while hex := bittab[move(4)] || hex
if not pos(0) then hex := bittab[left(tab(0), 4, "0")] || hex
}
return hex
end
#
# Convert pattern specification to decimal form
procedure decspec(pattern)
local cols, chunk, dec
pattern ? {
if not upto("#") then return pattern
cols := tab(upto(','))
move(2)
chunk := (cols + 3) / 4
dec := cols || ","
while dec ||:= integer("16r" || move(chunk)) || ","
}
return dec[1:-1]
end
procedure eqpats(rows1, rows2) #: test row patterns for equality
local i
if (*rows1 ~= *rows2) | (*rows1[1] ~= *rows2[1]) then fail
every i := 1 to *rows1 do
if rows1[i] ~== rows2[i] then fail
return rows2
end
#
# Get pattern from line. It trims off leading and trailing whitespace
# and removes any annotation (beginning with a # after the first whitespace
procedure getpatt(line)
line ? {
tab(many(' \t'))
return tab(upto(' \t') | 0)
}
end
#
# Get pattern annotation. It returns an empty string if there is
# no annotation.
procedure getpattnote(line)
line ? {
tab(many(' \t')) # remove leading whitespace
tab(upto(' \t')) | return "" # skip pattern
tab(upto('#')) | return "" # get to annotation
tab(many('# \t')) # get rid of leading junk
return tab(0) # annotation
}
end
# Convert hexadecimal string to bits
procedure hex2bits(s)
static hextab
local bits
initial {
hextab := table()
hextab["0"] := "0000"
hextab["1"] := "0001"
hextab["2"] := "0010"
hextab["3"] := "0011"
hextab["4"] := "0100"
hextab["5"] := "0101"
hextab["6"] := "0110"
hextab["7"] := "0111"
hextab["8"] := "1000"
hextab["9"] := "1001"
hextab["a"] := "1010"
hextab["b"] := "1011"
hextab["c"] := "1100"
hextab["d"] := "1101"
hextab["e"] := "1110"
hextab["f"] := "1111"
}
bits := ""
map(s) ? {
while bits ||:= hextab[move(1)]
}
return bits
end
#
# Convert pattern to hexadecimal form
procedure hexspec(pattern)
local cols, chunk, hex
pattern ? {
if find("#") then return pattern
cols := tab(upto(','))
move(1)
chunk := (cols + 3) / 4
hex := cols || ",#"
while hex ||:= right(exbase10(tab(upto(',') | 0), 16), chunk, "0") do
move(1) | break
}
return hex
end
#
# Succeed if tile is legal and small enough for (X) pattern. Other
# windows systems may be more restrictive.
procedure legalpat(tile)
if not legaltile(tile) then fail
tile ? {
if 0 < integer(tab(upto(','))) <= 32 then return tile
else fail
}
end
#
# Succeed if tile is legal. Accepts tiles that are too big for
# patterns.
procedure legaltile(tile)
map(tile) ? { # first check syntax
(tab(many(&digits)) & =",") | fail
if ="#" then (tab(many('0123456789abcdef')) & pos(0)) | fail
else {
while tab(many(&digits)) do {
if pos(0) then break # okay; end of string
else ="," | fail
}
if not pos(0) then fail # non-digit
}
}
return hexspec(decspec(tile)) == tile
end
#
# Convert pattern specification to an XBM image file.
procedure pat2xbm(pattern, name)
local dims, chunk, row
/name := "noname"
dims := tiledim(pattern)
write("#define ", name, "_width ", dims.w)
write("#define ", name, "_height ", dims.h)
write("static char ", name, "_bits[] = {")
chunk := (dims.w + 3) / 4
pattern ? {
tab(upto('#') + 1)
while row := move(chunk) do {
if *row % 2 ~= 0 then row := "0" || row
row ? {
tab(0)
while writes("0x", move(-2), ",")
}
write()
}
}
write("};")
end
#
# Count the number of bits set in a tile
procedure tilebits(rows)
local bits
bits := 0
every bits +:= !!rows
return bits
end
#
# Compute density (percentage of black bits) of pattern
procedure pdensity(pattern)
local dark, dims
dims := tiledim(pattern)
hexspec(pattern) ? {
dark := 0
every rowbits(pattern) ? {
every upto('1') do
dark +:= 1
}
return dark / real(dims.w * dims.h)
}
end
#
# Procedure to produce pattern specification from a square section of a window.
procedure pix2pat(window, x, y, cols, rows)
local c, j, tile, pattern, pixels, y0
pattern := ""
every y0 := 0 to rows - 1 do {
pixels := ""
every j := 0 to cols - 1 do
every c := Pixel(window, x + j, y0 + y, 1, 1) do
pixels ||:= (if c == "0,0,0" then "1" else "0")
pattern ||:= bits2hex(pixels)
}
if *pattern = 0 then fail # out of bounds specification
else return cols || ",#" || pattern
end
#
# Read pattern. It skips lines starting with a #,
# empty lines, and trims off any trailing characters after the
# first whitespace of a pattern.
procedure readpatt(input)
local line
while line := read(input) do
line ? {
if pos(0) | ="#" then next
return tab(upto(' \t') | 0)
}
fail
end
#
# Read pattern line. It skips lines starting with a # and empty lines but
# does not trim off any trailing characters after the first whitespace of
# a pattern.
procedure readpattline(input)
local line
while line := read(input) do
line ? {
if pos(0) | ="#" then next
return tab(0)
}
fail
end
#
# Generate rows of bits in a pattern. Doesn't work correctly for small
# patterns. (Why?)
procedure rowbits(pattern)
local row, dims, chunk, hex
dims := tiledim(pattern)
hexspec(pattern) ? {
tab(upto(',') + 2)
hex := tab(0)
chunk := *hex / dims.h
hex ? {
while row := right(hex2bits(move(chunk)), dims.w, "0") do
suspend reverse(row)
}
}
end
#
# Produce a list of the rows of a pattern
procedure pat2rows(pattern)
local rlist
rlist := []
every put(rlist, rowbits(pattern))
return rlist
end
#
# Convert row list to pattern specification
procedure rows2pat(rlist)
local pattern
pattern := *rlist[1] || ",#"
every pattern ||:= bits2hex(!rlist)
return pattern
end
# Show bits of a pattern
procedure showbits(pattern)
every write(rowbits(pattern))
write()
return
end
#
# Produce dimensions of the tile for a pattern
procedure tiledim(pattern)
local cols
hexspec(pattern) ? {
cols := integer(tab(upto(',')))
=",#" | fail
return tdim(cols, *tab(0) / ((cols + 3) / 4))
}
end
#
# Generate rows of bits from an XBM file
procedure xbm2rows(input)
local image, bits, row, hex, width, height, chunks
image := ""
read(input) ? {
tab(find("width") + 6)
tab(upto(&digits))
width := integer(tab(many(&digits)))
}
read(input) ? {
tab(find("height") + 6)
tab(upto(&digits))
height := integer(tab(many(&digits)))
}
chunks := (width / 8) + if (width % 8) > 0 then 1 else 0
while image ||:= reads(input, 500000) # Boo! -- can do better
image ? {
every 1 to height do {
row := ""
every 1 to chunks do {
tab(find("0x") + 2)
hex := move(2) # a bit of optimization
row ||:= case hex of {
"00": "00000000"
"ff": "11111111"
default: reverse(right(hex2bits(hex), 8, "0"))
}
}
suspend left(row, width)
}
}
end
|