-
Notifications
You must be signed in to change notification settings - Fork 2
/
cross-stitch.ey
executable file
·214 lines (188 loc) · 6.3 KB
/
cross-stitch.ey
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
#!./loaded
bin .print "<=" via
{ _ ==s len <=un16 s cat } /string deffd
{ ==divisor _ 0 lt { neg divisor div neg } { divisor div } ? * } '00.0 /idiv deffd
sys .argv len 4 neq { "Usage: ./cross-stitch.ey <μm cross size (try 2000)> <input.ppm (grayscale, gimp .ppm, raw data formatting)> <output.vp3>" die } rep
sys .file ":" via
sys .file "<-" via
"Scale μm: " 1 sys .argv * txt .consume .u 100 div _ ==scale 100 mul txt .produce .u cat dump
2 sys .argv * :open
<-truncating <-creating <-writeonly 3 sys .argv * <-open
{ =*stitch
16000000 :read ==data
data "\n" str .split =*lines
0 lines "P6" neq { "Not in P6 format" die } rep
2 lines " " str .split _ 0 -01 * txt .consume .u ==width
1 -01 * txt .consume .u ==height
3 lines txt .consume .u ==maxValue
"Width: " width txt .produce .u cat " Height: " cat height txt .produce .u cat dump
"MaxValue: " maxValue txt .produce .u cat dump
"" 4 |lines len range { lines cat "\n" cat } each =*imgData
0 height range { ==y
"( " y txt .produce .u cat " / " cat height txt .produce .u cat " )\n" cat sys .out .writeall
scale ==dx
scale ==dy
{ =*s7 =*s6 =*s5 =*s4 =*s3 =*s2 =*s1 ==p
[
# base-white offset substracted
# { p 20 lt } { " " sys .out .writeall s1 }
# { p 82 lt } { "." sys .out .writeall s2 }
# { p 192 lt } { "+" sys .out .writeall s3 }
# { p 291 lt } { "*" sys .out .writeall s4 }
# { p 365 lt } { "x" sys .out .writeall s3 }
# { p 458 lt } { "%" sys .out .writeall s6 }
# { 1 } { "#" sys .out .writeall s7 }
{ p 120 lt } { " " sys .out .writeall s1 }
{ p 182 lt } { "." sys .out .writeall s2 }
{ p 292 lt } { "+" sys .out .writeall s3 }
{ p 391 lt } { "*" sys .out .writeall s4 }
{ p 465 lt } { "x" sys .out .writeall s3 }
{ p 558 lt } { "%" sys .out .writeall s6 }
{ 1 } { "#" sys .out .writeall s7 }
] conds
} /selectStitch deffd
{
{ dx 0 stitch }
{
dx dy stitch dy
neg =dy
}
{
dx 1 mul 2 idiv dx 0 mul 2 idiv sub dy stitch
dx 2 mul 2 idiv dx 1 mul 2 idiv sub dy neg stitch
}
{
dx 1 mul 3 idiv dx 0 mul 3 idiv sub dy stitch
dx 2 mul 3 idiv dx 1 mul 3 idiv sub dy neg stitch
dx 3 mul 3 idiv dx 2 mul 3 idiv sub dy stitch
dy neg =dy
}
{
dx 1 mul 4 idiv dx 0 mul 4 idiv sub dy stitch
dx 2 mul 4 idiv dx 1 mul 4 idiv sub dy neg stitch
dx 3 mul 4 idiv dx 2 mul 4 idiv sub dy stitch
dx 4 mul 4 idiv dx 3 mul 4 idiv sub dy neg stitch
}
{
dx 1 mul 5 idiv dx 0 mul 5 idiv sub dy stitch
dx 2 mul 5 idiv dx 1 mul 5 idiv sub dy neg stitch
dx 3 mul 5 idiv dx 2 mul 5 idiv sub dy stitch
dx 4 mul 5 idiv dx 3 mul 5 idiv sub dy neg stitch
dx 5 mul 5 idiv dx 4 mul 5 idiv sub dy stitch
dy neg =dy
}
{
dx 1 mul 6 idiv dx 0 mul 6 idiv sub dy stitch
dx 2 mul 6 idiv dx 1 mul 6 idiv sub dy neg stitch
dx 3 mul 6 idiv dx 2 mul 6 idiv sub dy stitch
dx 4 mul 6 idiv dx 3 mul 6 idiv sub dy neg stitch
dx 5 mul 6 idiv dx 4 mul 6 idiv sub dy stitch
dx 6 mul 6 idiv dx 5 mul 6 idiv sub dy neg stitch
}
} /stitches deffst
0 width range { # ==x
y width mul add 3 mul imgData 608 mul maxValue div neg 608 add
stitches selectStitch
} each
0 dy stitch
dx neg =dx
dy neg =dy
0 width range reverse { # ==x
y width mul add 3 mul imgData 608 mul maxValue div neg 608 add
stitches selectStitch
} each
"\n" sys .out .writeall
} each
} /produceImage deffd
0 ==xStart 0 ==yStart
0 ==minX 0 ==maxX 0 ==minY 0 ==maxY
{ ""
[ 10 246 0 ] str .fromArray cat # unknown
==data
list ==collectedData
{
stitch data -01 cat =data
data len 4096 gt {
data collectedData .append1
"" =data
} rep
} produceImage
"" collectedData { cat } each data cat
} /rawStitchContent deffst
< xStart ==x yStart ==y
{ ==dy ==dx
dx 128 neg gt dx 128 lt and
dy 128 neg gt dy 128 lt and and {
[ dx dy ] str .fromArray
} {
[ 128 1 ] str .fromArray dx <=sn16 dy <=sn16 [ 128 2 ] str .fromArray cat
} ? *
x dx add =x
y dy add =y
x minX lt { x =minX } rep
y minY lt { y =minY } rep
x maxX gt { x =maxX } rep
y maxY gt { y =maxY } rep
}
> -- /stitch deffd
rawStitchContent { }_ =*rawStitchContent
maxX minX sub ==xSize
maxY minY sub ==ySize
{ ""
minX neg xSize 2 div sub 100 mul <=sn32 # start X-offset
minY neg ySize 2 div sub 100 mul neg <=sn32 # start Y-offset
[ 1 0 ] str .fromArray cat # unknown
0 <=sn8 # red
0 <=sn8 # green
0 <=sn8 # blue
[ 0 0 0 5 40 ] str .fromArray cat # unknown
"1234" string # unknown numeric string
"Color name" string # unknown what values are acceptable
"Thread type" string # "Robison-Anton Rayon 40" in all generated files
0 <=sn32 # start x offset for next color
0 <=sn32 # start y offset for next color
"\0" string
rawStitchContent _ len "" -01 <=un32 -01 cat cat
} /stitchContent deffd
{ ""
0 <=sn32 # origin X
0 <=sn32 # origin Y
[ 0 0 0 ] str .fromArray cat # unknown
xSize 100 mul 2 div <=sn32
ySize 100 mul 2 div <=sn32
xSize 100 mul 2 div neg <=sn32
ySize 100 mul 2 div neg <=sn32
xSize 100 mul <=sn32
ySize 100 mul <=sn32
"Yet another comment" string
25700 <=un16 # unknown
4096 <=un32 # unknown
0 <=un32 # unknown
0 <=un32 # unknown
4096 <=un32 # unknown
"xxPP\0" cat # stitch section header
"Yet another vendor string" string
1 <=un16 # number of colors
[ 0 5 0 ] str .fromArray cat # unknown
stitchContent _ len 1 add "" -01 <=un32 -01 cat cat
} /innerContent deffd
{ ""
"File comments go here (or don't)" string
xSize 100 mul 2 div <=sn32
ySize 100 mul 2 div <=sn32
xSize 100 mul 2 div neg <=sn32
ySize 100 mul 2 div neg <=sn32
[ 0 0 122 134 ] str .fromArray cat # TODO thread length (ahem)
[ 0 ] str .fromArray cat # unknown
1 <=un8 # number of colors
[ 12 0 1 0 3 0 ] str .fromArray cat # unknown
innerContent _ len "" -01 <=un32 -01 cat cat
} /mainContent deffd
"%vsm%\0"
"Stratum 0 Embroidery Hack\0" string
[ 0 2 0 ] str .fromArray cat # unknown
mainContent _ len "" -01 <=un32 -01 cat cat
<-writeall
:close
<-close
# vim: syn=elymas