-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplotImage.pde
More file actions
228 lines (174 loc) · 5.65 KB
/
Copy pathplotImage.pde
File metadata and controls
228 lines (174 loc) · 5.65 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
219
220
221
222
223
224
225
226
227
import controlP5.*;
import java.awt.*;
import processing.serial.*;
ControlP5 cp5;
Serial plotter;
PImage source, destination;
String serPort = "";
int printSize = 0;
int printSpeed = 0;
int dither = 0;
int passes = 1;
boolean invertState = false;
int lineY = -1;
int x = 0;
boolean printing = false;
void setup() {
size(600, 600);
background(0);
image(loadImage("splash1.png"), 0, 0);
// CONTROL P5 INIT ----------------------------------------------------------------------------------------------
cp5 = new ControlP5(this);
cp5.addSlider("printSize")
.setPosition(2, 0)
.setRange(1, 16)
.setValue(1)
.setId(0)
;
cp5.addSlider("dither")
.setPosition(2, 11)
.setRange(0, 8)
.setValue(4)
.setId(4)
;
cp5.addButton("invert")
.setValue(10)
.setPosition(2, 22)
.setSize(99, 10)
.setId(5);
cp5.addButton("PRINT!")
.setValue(10)
.setPosition(152, 12)
.setSize(100, 10)
.setId(1);
DropdownList dl = cp5.addDropdownList("Serial Port").setPosition(152, 10).setSize(200, 200).setId(2);
for (int i=0; i<Serial.list ().length; i++) {
dl.addItem(Serial.list()[i], i);
}
DropdownList dlSpeed = cp5.addDropdownList("Print Speed").setPosition(256, 22).setSize(96, 100).setId(3);
dlSpeed.addItem("Ultra", 80);
dlSpeed.addItem("Draft", 100);
dlSpeed.addItem("Normal", 200);
dlSpeed.addItem("Beautiful", 350);
// END CONTROL P5 INIT ----------------------------------------------------------------------------------------------
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
exit();
} else {
source = loadImage(selection.getAbsolutePath());
resizeImages();
dither = 8;
reDither();
dither = 4;
reDither();
}
}
void draw() {
if (printing)cp5.hide();
if (destination!=null)image(destination, 0, 0); //set preview image
fill(0, 179, 237);
noStroke();
if (!printing)rect(2, 0, 148, 22); // cp5 background rectangle
stroke(255, 0, 0);
line(0, lineY, width, lineY); // draw status line
}
public void controlEvent(ControlEvent theEvent) {
if (theEvent.isGroup()) {
if (theEvent.getGroup().getId() == 3) {
printSpeed = int(theEvent.getGroup().getValue()); // print speed dropdown
} else if (theEvent.getGroup().getId() == 2) {
serPort = Serial.list()[int(theEvent.getGroup().getValue())]; // serial port dropdown
}
} else if (theEvent.controller().id() == 1) {
printImage(); // start print
} else if (theEvent.controller().id() == 4 || theEvent.controller().id() == 5) {
if (theEvent.controller().id() == 5) invertState=!invertState;
reDither();
}
}
void printImage() {
// check if all parameters are valid
if (serPort == "") {
javax.swing.JOptionPane.showMessageDialog(null, "please select a valid serial port!");
return;
}
if (printSpeed == 0) {
javax.swing.JOptionPane.showMessageDialog(null, "please select a printing speed!");
return;
}
println("Width in steps: "+destination.width*printSize);
if (destination.width*printSize > 2500) {
javax.swing.JOptionPane.showMessageDialog(null, "paper too small, setting print size to the maximum of " + round(2500/destination.width));
printSize = round(2500/destination.width);
}
printing = true;
javax.swing.JOptionPane.showMessageDialog(null, "Make sure the Arduino at "+serPort+" is ready!"); // show
plotter = new Serial(this, serPort, 9600);
plotter.bufferUntil('\n');
plotter.write(destination.width+","+destination.height+","+int(printSize)+","+int(printSpeed)); // send initialising data to plotter
println("width: "+destination.width+" height: "+destination.height+" Print Size: "+int(printSize)+" Print Speed: "+int(printSpeed));
printing = true;
lineY = 0;
}
void serialEvent(Serial plotter) {
if (printing) {
int pixel = (brightness(destination.pixels[x]) == 0) ? 1 : 0;
plotter.write(pixel);
x++;
if (x % destination.width == 0) {
lineY++;
}
if (x == destination.width * destination.height) {
printing = false;
javax.swing.JOptionPane.showMessageDialog(null, "Done!");
exit();
}
}
}
PImage getDitheredImage(PImage img, int n, boolean invert) {
PImage out = createImage(img.width, img.height, RGB);
img.loadPixels();
out.loadPixels();
float f = 255 / (pow(2, 2*n) + 1);
for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++) {
color c = img.get(x, y);
float t = (n > 0) ? dizza(x, y, n) * f : 128;
if (invert) {
c = color( (t >= brightness(c)) ? 255 : 0 );
} else {
c = color( (t >= brightness(c)) ? 0 : 255 );
}
out.pixels[ x + y*img.width ] = c;
}
}
out.updatePixels();
return out;
}
int dizza(int i, int j, int n) {
if (n==1) {
return (i%2!=j%2 ? 2 : 0) + j%2;
} else {
return 4 * dizza( i % 2, j % 2, n-1) + dizza( int( i / 2 ), int( j / 2 ), n - 1);
}
}
void resizeImages() {
frame.setResizable(true);
if (source.width < width || source.height < width) {
frame.resize(source.width, source.height+20);
} else if (source.width>width) {
source.resize(width, (width/source.width)*source.height);
frame.resize(source.width, source.height);
} else if (source.height>height) {
source.resize(height, (height/source.height)*source.width);
source.resize(source.width, source.height);
}
frame.setResizable(false);
}
void reDither() {
destination = getDitheredImage(source, dither, invertState); //Redithers image
destination.save("dest.png");
}