-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimage-lace.js
executable file
·153 lines (127 loc) · 3.19 KB
/
image-lace.js
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
#!/bin/sh
':' //; exec "$(command -v nodejs || command -v node)" "$0" "$@"
"use strict";
/*
load an image and build a lace pattern from it
*/
if (process.argv.length < 3) {
console.error("Usage:\n\timage-lace.js <image.png>");
process.exit(1);
}
const image = process.argv[2];
const Carriers = {
'Background':'8',
'Insert':['6','7'],
};
const fs = require('fs');
const PNG = require('pngjs').PNG;
const png = PNG.sync.read(fs.readFileSync(image));
const Width = png.width;
const Height = png.height;
console.log(";!knitout-2")
console.log(";;Carriers: 1 2 3 4 5 6 7 8 9 10")
function caston_stitch() {
console.log("x-stitch-number 102");
}
function lace_stitch() {
console.log("x-stitch-number 103");
}
const min = 1;
const max = Width;
//all-needle cast-on:
function caston(Carrier) {
caston_stitch();
console.log("inhook " + Carrier);
for (let n = max; n >= min; --n) {
if ((max-n)%2 == 0) {
console.log(`knit - f${n} ${Carrier}`);
}
}
for (let n = min; n <= max; ++n) {
if ((max-n)%2 != 0) {
console.log(`knit + f${n} ${Carrier}`);
}
}
for (let n = max; n >= min; --n) {
console.log(`knit - f${n} ${Carrier}`);
}
console.log(`releasehook ${Carrier}`);
}
caston(Carriers.Background);
lace_stitch();
function toHex(r,g,b) {
r = Math.max(0, Math.min(255, Math.round(r)));
g = Math.max(0, Math.min(255, Math.round(g)));
b = Math.max(0, Math.min(255, Math.round(b)));
r = r.toString(16).padStart(2,'0');
g = g.toString(16).padStart(2,'0');
b = b.toString(16).padStart(2,'0');
return r + g + b;
}
let dir = "+";
let unrecognized = {};
for (let row = 0; row < Height; ++row) {
//figure out target offsets + beds based on the image:
let targets = [];
for (let c = 0; c < Width; ++c) {
let col = {
r:png.data[4*(png.width*(Height-1-row)+c)+0],
g:png.data[4*(png.width*(Height-1-row)+c)+1],
b:png.data[4*(png.width*(Height-1-row)+c)+2]
};
let hcol = toHex(col.r, col.g, col.b);
if (hcol === "008800") {
targets.push(0);
} else if (hcol === "880000") {
targets.push(-1);
} else if (hcol === "000088") {
targets.push(+1);
} else {
if (!(hcol in unrecognized)) {
console.warn(`Unrecognized color ${hcol}.`);
unrecognized[hcol] = 0;
}
unrecognized[hcol] += 1;
targets.push(0);
}
}
//move loops that need moving:
for (let n = min; n <= max; ++n) {
if (targets[n-min] === -1) {
console.log(`xfer f${n} b${n}`);
}
}
console.log('rack -1');
for (let n = min; n <= max; ++n) {
if (targets[n-min] === -1) {
console.log(`xfer b${n} f${n-1}`);
} else if (targets[n-min] === 1) {
console.log(`xfer f${n} b${n+1}`);
}
}
console.log('rack 0');
for (let n = min; n <= max; ++n) {
if (targets[n-min] === 1) {
console.log(`xfer b${n+1} f${n+1}`);
}
}
//knit:
if (dir === '+') {
for (let n = min; n <= max; ++n) {
console.log(`knit ${dir} f${n} ${Carriers.Background}`);
}
dir = '-';
} else if (dir === '-') {
for (let n = max; n >= min; --n) {
console.log(`knit ${dir} f${n} ${Carriers.Background}`);
}
dir = '+';
}
}
console.log("outhook " + Carriers.Background);
for (let n = min-4; n <= max+4; ++n) {
console.log("drop f" + n);
}
for (let n = min-4; n <= max+4; ++n) {
console.log("drop b" + n);
}