-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPatternWork.pde
More file actions
219 lines (188 loc) · 7.24 KB
/
PatternWork.pde
File metadata and controls
219 lines (188 loc) · 7.24 KB
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
boolean loadPatternImage(String filename) {
println(filename);
patternImageName = filename;
patternImage = loadImage(filename); // Load the image into the program
patternImageWorkingSet = loadImage(filename); // Load the image into the program
patternImageName = filename;
//check colour of the image
//
// if (createColourPalette()) {
// for (int k=0; k<patternImageColorList.length; k++ ) {
// println("Color"+ k +": " + patternImageColorList[k][0] + " Count: " + patternImageColorList[k][1]); //prints the colors and the number of times it is prsent in the image (you'll notice that the values are not in rgb format)
// }
// }
// show colour palette of the pattern in serial window
if (createColourPalette()==true) {
for (int k=0; k<patternImageColorList.length; k++ ) {
println("Color"+ k +": " + patternImageColorList[k]); //prints the colors (you'll notice that the values are not in rgb format)
coloursInUse = patternImageColorList.length;
}
} else {
//warning if more than 4 colours
return false;
}
//change background to transparent and mirror image if it is a superba
if (knittingMachine ==1) {
// makePatternImageTransparent();
mirrorPatternImageX();
}
return true;
}
boolean loadPatternImageBack(String filename) {
println(filename);
patternImageNameBack = filename;
patternImageBack = loadImage(filename); // Load the image into the program
patternImageWorkingSetBack = loadImage(filename); // Load the image into the program
patternImageNameBack = filename;
// show colour palette of the pattern in serial window
if (createColourPaletteBack()) {
for (int k=0; k<patternImageColorListBack.length; k++ ) {
println("Color"+ k +": " + patternImageColorListBack[k]); //prints the colors (you'll notice that the values are not in rgb format)
}
//to do warning if more than 4 colours
coloursInUseBack = patternImageColorListBack.length;
} else {
//warning if more than 4 colours
return false;
}
return true;
}
void makePatternImageTransparent() {
for (int i = 0; i < patternImage.width * patternImage.height; i++) {
if ((patternImage.pixels[i] & 0x00FFFFFF) == 0x0000000) {
//patternImage.pixels[i] = 0;
} else {
patternImage.pixels[i] = 0;
}
}
patternImage.format = ARGB;
patternImage.updatePixels();
}
void mirrorPatternImageX() {
for ( int i=0; i < patternImage.width; i++ ) {
for (int j=0; j < patternImage.height; j++) {
patternImageWorkingSet.set( patternImage.width - 1 - i, j, patternImage.get(i, j) );
}
}
//zurückschreiben
for (int i = 0; i < patternImage.width * patternImage.height; i++) {
patternImage.pixels[i] = patternImageWorkingSet.pixels[i];
}
patternImage.updatePixels();
}
void mirrorPatternImageBackX() {
for ( int i=0; i < patternImageBack.width; i++ ) {
for (int j=0; j < patternImageBack.height; j++) {
patternImageWorkingSetBack.set( patternImageBack.width - 1 - i, j, patternImageBack.get(i, j) );
}
}
//zurückschreiben
for (int i = 0; i < patternImageBack.width * patternImageBack.height; i++) {
patternImageBack.pixels[i] = patternImageWorkingSetBack.pixels[i];
}
patternImageBack.updatePixels();
}
/*
void mirrorPatternImageX() {
patternImage.loadPixels();
int k = 0;
int patternHeight = patternImage.height;
int patternWidth = patternImage.width;
for ( int i=0; i < patternWidth; i++ ) {
for (int j=0; j < patternHeight; j++) {
// patternImage.set( patternImage.width - 1 - i, j, patternImage.get(i, j) );
patternImage.pixels[j*patternWidth+i] = patternImage.get(patternWidth-1-i, j);
k++;
}
}
patternImage.updatePixels();
}
*/
void inversePatternImage() {
//only invert 2 colour pattern
patternImage.loadPixels();
if (patternImageColorList.length < 3) {
for (int i = 0; i < patternImage.width * patternImage.height; i++) {
//println(patternImage.pixels[i] +" -- "+ patternImageColorList[0]+" -- "+ patternImageColorList[1]);
if (patternImage.pixels[i] == patternImageColorList[0]) {
patternImage.pixels[i] = patternImageColorList[1];
} else {
patternImage.pixels[i] = patternImageColorList[0];
}
}
}
patternImage.updatePixels();
}
void inversePatternImageBack() {
//only invert 2 colour pattern
patternImageBack.loadPixels();
if (patternImageColorListBack.length < 3) {
for (int i = 0; i < patternImageBack.width * patternImageBack.height; i++) {
if (patternImageBack.pixels[i] == patternImageColorListBack[0]) {
patternImageBack.pixels[i] = patternImageColorListBack[1];
} else {
patternImageBack.pixels[i] = patternImageColorListBack[0];
}
}
}
patternImageBack.updatePixels();
}
//count colours in patternImage
boolean createColourPalette() {
//clear array
if (patternImageColorList.length > 0) {
for (int i = patternImageColorList.length; i >0; i--) {
patternImageColorList = shorten(patternImageColorList);
}
}
//loop through all the pixels of the image
for (int i = 0; i < patternImage.pixels.length; i++) {
boolean colorExists = false; //bollean variable that checks if the color already exists in the array
//loop through the values in the array
for (int j = 0; j < patternImageColorList.length; j++) {
if (patternImageColorList[j] == patternImage.pixels[i]) {
colorExists = true; //color already exists in the array
}
}
//if the color hasn't been added to the array
if (colorExists == false) {
//stop if more than 4 colours for passap or more than 2 for superba
if (patternImageColorList.length >= 4 || (patternImageColorList.length >= 2 && knittingMachine ==1)) {
InfoText = ("WARNING: The picture has more then 4 Colours!!\nIt will not be knitted properly.");
println("WARNING: The picture has more then 4 Colours!! It will not be knitted properly.");
return false;
}
patternImageColorList = (int[])append(patternImageColorList, patternImage.pixels[i]); //add it
}
}
return true;
}
boolean createColourPaletteBack() {
//clear array
if (patternImageColorListBack.length > 0) {
for (int i = patternImageColorListBack.length; i >0; i--) {
patternImageColorListBack = shorten(patternImageColorListBack);
}
}
//loop through all the pixels of the image
for (int i = 0; i < patternImageBack.pixels.length; i++) {
boolean colorExists = false; //bollean variable that checks if the color already exists in the array
//loop through the values in the array
for (int j = 0; j < patternImageColorListBack.length; j++) {
if (patternImageColorListBack[j] == patternImageBack.pixels[i]) {
colorExists = true; //color already exists in the array
}
}
//if the color hasn't been added to the array
if (colorExists == false) {
//stop if more than 4 colours
if (patternImageColorListBack.length >= 4) {
InfoText = ("WARNING: The picture has more then 4 Colours!!\nIt will not be knitted properly.");
println("WARNING: The picture has more then 4 Colours!! It will not be knitted properly.");
return false;
}
patternImageColorListBack = (int[])append(patternImageColorListBack, patternImageBack.pixels[i]); //add it
}
}
return true;
}