summaryrefslogtreecommitdiff
path: root/ipl/gpacks/weaving/tieutils.icn
blob: 7f5bb4b55fc53f08dcafa3c5ad4f7d5c931b30cd (plain)
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
############################################################################
#
#	File:     tieutils.icn
#
#	Subject:  Procedures related to weaving tie-ups
#
#	Author:   Ralph E. Griswold
#
#	Date:     June 15, 1999
#
############################################################################
#
#  This file is in the public domain.
#
############################################################################
#
#	imr2tie(imr)	converts g2 image record to tie-ip
#
#	pat2tie(pat)	converts bi-level pattern to tie-up string
#
#	pat2tier(pat)	converts bi-level pattern to tie-up record
#
#	showtie(s, size, fg, bg)
#			produces a hidden window for the tie-up as a matrix
#			with the specified foreground and background colors
#
#	testtie(s)	succeeds if s is a valid tie-up but fails otherwise
#
#	tie2imr(s)	converts tie-up to g2 image record
#
#	tie2pat(tie)	converts tie-up to bi-level pattern
#
#	tie2coltier(s)	creates a black/white color tieup-record for
#			  tie-up s
#
#	tie2tier(s)	creates a 0/1 tie-up record for tie-up s
#
#	tier2rstring(r)	creates a tie-up string from a tie-up record
#
############################################################################
#
#  Requires:  Version 9 graphics
#
############################################################################
#
#  Links:  cells, wopen, patutils, imrutils
#
############################################################################

link cells
link wopen
link patutils
link imrutils

record tie(shafts, treadles, matrix)

procedure imr2tie(imr)			#: convert image record to tie-up

   return imr.width || ";" || *imr.pixels / imr.width || ";" || imr.pixels

end

procedure pat2tie(pat)			#: convert pattern to tie-up string
   local matrix, tieup, shafts, treadles

   pat ? {		# OLD-STYLE BIT STRING TIE-UP
      if shafts := tab(upto(',')) &
      move(1) &
      treadles := tab(upto(',')) &
      move(1) then {
         matrix := list(shafts)
         while put(matrix, move(treadles))
         }
      else matrix := pat2rows(pat)
      }

   tieup := tie(*matrix[1], *matrix, matrix)

   return tier2string(tieup)

end

procedure pat2tier(pat)			#: convert pattern to tie-up record
   local matrix

   matrix := pat2rows(pat)

   return tie(*matrix[1], *matrix, matrix)

end

#  Set up empty palette grid

procedure showtie(tieup, cellsize, fg, bg)	#: create image of tie-up
   local x, y, panel, row, n, m, color

   /cellsize := 10

   tieup ?:= {
      n := tab(upto(';')) &
      move(1) &
      m := tab(upto(';')) &
      move(1) &
      tab(0)
      } | stop("*** invalid tieup")

   panel := makepanel(n, m, cellsize, fg, bg)

   tieup ? {
      y := 1
      while row := move(n) do {
         every x := 1 to n do {
            color := if row[x] == "1" then "black" else "white"
            colorcell(panel, x, y, color)
            }
         y +:= 1
         }
      }

   return panel
 
end

procedure testtie(s)			#: test validity of tie-up s
   local n, m, bits

   s ? {
      n := (0 < integer(tab(upto(';')))) &
      move(1) &
      m := (0 < integer(tab(upto(';')))) &
      move(1) &
      bits := tab(0)
      } | fail					# bad header

   if *(cset(bits) -- '01') > 0 then fail	# illegal characters

   if *bits ~= (n * m) then fail		# wrong length

   return s

end

procedure tie2imr(tie)			#: convert tie-up to image record
   local width

   tie ? {
      width := tab(upto(';'))
      move(1)
      tab(upto(';') + 1)
      return imstoimr(width || ",g2," || tab(0))
      }

end

procedure tie2pat(shafts, treadles, tie)	#: convert tie-up record to ims
   local tieup, matrix

   tieup := tie2tier(shafts, treadles, tie)
   matrix := tieup.matrix
   return rows2pat(matrix)

end 

procedure tie2tier(shafts, treadles, tieup)	#: create 0/1 tie-up record
   local matrix

   matrix := []

   tieup ? {
      every 1 to treadles do 
         put(matrix, move(shafts))
      }

   return tie(shafts, treadles, matrix)

end

procedure tie2coltier(tieup)		#: create color tie-up record
   local result, shafts, treadles, rec

   result := []

   if not upto(';', tieup) then		# old-style tie-up
      tieup := "8;8;" || tieup

   tieup ? {
      (
         shafts := tab(upto(';')) &
         move(1) &
         treadles := tab(upto(';')) &
         move(1)
         ) | stop("*** invalid tieup")
      every 1 to shafts do 
         put(result, tcolors(move(treadles)))
      }

   return tie(shafts, treadles, result)

end

procedure tcolors(s)
   local i, result

   result := []

   every i := 1 to *s do
      put(result, if s[i] == "0" then "black" else "white")

   return result

end

procedure tier2string(rec)	#: convert tie-up record to string
   local result

   result := ""

   every result ||:= !rec.matrix

   return result

end