picrate 2.0.1-java → 2.3.0-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.mvn/extensions.xml +1 -1
- data/.mvn/wrapper/MavenWrapperDownloader.java +1 -1
- data/.mvn/wrapper/maven-wrapper.properties +2 -2
- data/CHANGELOG.md +10 -0
- data/README.md +6 -3
- data/Rakefile +2 -1
- data/docs/_includes/footer.html +1 -1
- data/docs/_layouts/post.html +1 -1
- data/docs/_methods/alternative_methods.md +2 -1
- data/docs/_methods/noise_mode.md +88 -0
- data/docs/_posts/2018-05-06-install_jruby.md +3 -3
- data/docs/_posts/2018-11-18-building-gem.md +3 -1
- data/docs/_posts/2020-03-09-auto_install_picrate.md +2 -3
- data/docs/_posts/2020-05-11-getting_started_manjaro.md +20 -8
- data/docs/classes.md +2 -2
- data/docs/editors.md +2 -2
- data/docs/gems.md +3 -3
- data/docs/index.html +1 -1
- data/docs/libraries.md +2 -2
- data/docs/live.md +2 -2
- data/docs/magic.md +2 -2
- data/docs/methods.md +2 -2
- data/docs/modules.md +3 -3
- data/docs/objects.md +2 -2
- data/lib/picrate.rb +2 -1
- data/lib/picrate/app.rb +7 -2
- data/lib/picrate/helper_methods.rb +1 -1
- data/lib/picrate/native_folder.rb +1 -3
- data/lib/picrate/runner.rb +4 -4
- data/lib/picrate/version.rb +1 -1
- data/library/jcomplex/jcomplex.rb +1 -0
- data/library/pdf/pdf.rb +6 -0
- data/mvnw +2 -2
- data/mvnw.cmd +2 -2
- data/picrate.gemspec +3 -2
- data/pom.rb +22 -8
- data/pom.xml +27 -5
- data/src/main/java/monkstone/PicrateLibrary.java +1 -1
- data/src/main/java/monkstone/complex/JComplex.java +252 -0
- data/src/main/java/monkstone/fastmath/DegLutTables.java +111 -0
- data/src/main/java/monkstone/fastmath/Deglut.java +41 -93
- data/src/main/java/monkstone/noise/FastTerrain.java +874 -0
- data/src/main/java/monkstone/noise/Noise.java +90 -0
- data/src/main/java/monkstone/noise/NoiseGenerator.java +75 -0
- data/src/main/java/monkstone/noise/NoiseMode.java +28 -0
- data/src/main/java/monkstone/noise/OpenSimplex2F.java +881 -0
- data/src/main/java/monkstone/noise/OpenSimplex2S.java +1106 -0
- data/src/main/java/monkstone/noise/SmoothTerrain.java +1099 -0
- data/src/main/java/monkstone/vecmath/package-info.java +1 -1
- data/src/main/java/monkstone/vecmath/vec3/Vec3.java +1 -1
- data/src/main/java/monkstone/videoevent/package-info.java +1 -1
- data/src/main/java/processing/awt/PGraphicsJava2D.java +22 -23
- data/src/main/java/processing/awt/PImageAWT.java +377 -0
- data/src/main/java/processing/awt/ShimAWT.java +711 -0
- data/src/main/java/processing/core/PApplet.java +14880 -14101
- data/src/main/java/processing/core/PConstants.java +5 -5
- data/src/main/java/processing/core/PFont.java +1 -1
- data/src/main/java/processing/core/PGraphics.java +284 -271
- data/src/main/java/processing/core/PImage.java +1620 -1815
- data/src/main/java/processing/core/PShape.java +18 -18
- data/src/main/java/processing/core/PSurface.java +105 -139
- data/src/main/java/processing/core/PSurfaceNone.java +29 -0
- data/src/main/java/processing/core/PVector.java +23 -23
- data/src/main/java/processing/data/Table.java +4 -4
- data/src/main/java/processing/net/Client.java +13 -13
- data/src/main/java/processing/net/Server.java +5 -5
- data/src/main/java/processing/opengl/PGL.java +649 -3699
- data/src/main/java/processing/opengl/PGraphicsOpenGL.java +2503 -2278
- data/src/main/java/processing/opengl/PJOGL.java +374 -1526
- data/src/main/java/processing/opengl/PSurfaceJOGL.java +220 -86
- data/src/main/java/processing/pdf/PGraphicsPDF.java +607 -0
- data/test/deglut_spec_test.rb +2 -2
- data/test/respond_to_test.rb +0 -1
- data/vendors/Rakefile +33 -21
- data/vendors/{picrate_sketches.geany → geany.rb} +32 -7
- metadata +24 -9
- data/src/main/java/monkstone/noise/SimplexNoise.java +0 -465
@@ -0,0 +1,711 @@
|
|
1
|
+
package processing.awt;
|
2
|
+
|
3
|
+
import java.awt.Desktop;
|
4
|
+
import java.awt.EventQueue;
|
5
|
+
import java.awt.FileDialog;
|
6
|
+
import java.awt.Frame;
|
7
|
+
import java.awt.HeadlessException;
|
8
|
+
import java.awt.Image;
|
9
|
+
import java.awt.Toolkit;
|
10
|
+
import java.awt.color.ColorSpace;
|
11
|
+
import java.awt.image.BufferedImage;
|
12
|
+
import java.io.BufferedOutputStream;
|
13
|
+
import java.io.File;
|
14
|
+
import java.io.IOException;
|
15
|
+
import java.io.InputStream;
|
16
|
+
import java.net.URI;
|
17
|
+
import java.net.URISyntaxException;
|
18
|
+
import java.util.Iterator;
|
19
|
+
import java.awt.DisplayMode;
|
20
|
+
import java.awt.GraphicsConfiguration;
|
21
|
+
import java.awt.GraphicsDevice;
|
22
|
+
import java.awt.GraphicsEnvironment;
|
23
|
+
import java.awt.geom.AffineTransform;
|
24
|
+
|
25
|
+
import javax.imageio.IIOImage;
|
26
|
+
import javax.imageio.ImageIO;
|
27
|
+
import javax.imageio.ImageTypeSpecifier;
|
28
|
+
import javax.imageio.ImageWriteParam;
|
29
|
+
import javax.imageio.ImageWriter;
|
30
|
+
import javax.imageio.metadata.IIOInvalidTreeException;
|
31
|
+
import javax.imageio.metadata.IIOMetadata;
|
32
|
+
import javax.imageio.metadata.IIOMetadataNode;
|
33
|
+
import javax.swing.ImageIcon;
|
34
|
+
import javax.swing.JFileChooser;
|
35
|
+
import javax.swing.UIManager;
|
36
|
+
import javax.swing.UnsupportedLookAndFeelException;
|
37
|
+
|
38
|
+
// used by desktopFile() method
|
39
|
+
import javax.swing.filechooser.FileSystemView;
|
40
|
+
|
41
|
+
import processing.core.PApplet;
|
42
|
+
import processing.core.PConstants;
|
43
|
+
import processing.core.PImage;
|
44
|
+
|
45
|
+
|
46
|
+
/**
|
47
|
+
* This class exists as an abstraction layer to remove AWT from PApplet.
|
48
|
+
* It is a staging area for AWT-specific code that's shared by the Java2D,
|
49
|
+
* JavaFX, and JOGL renderers. Once PSurfaceFX and PSurfaceJOGL have
|
50
|
+
* their own implementations, these methods will move to PSurfaceAWT.
|
51
|
+
*/
|
52
|
+
public class ShimAWT implements PConstants {
|
53
|
+
/*
|
54
|
+
PGraphics graphics;
|
55
|
+
PApplet sketch;
|
56
|
+
|
57
|
+
|
58
|
+
public ShimAWT(PApplet sketch) {
|
59
|
+
this.graphics = graphics;
|
60
|
+
this.sketch = sketch;
|
61
|
+
}
|
62
|
+
*/
|
63
|
+
static private ShimAWT instance;
|
64
|
+
|
65
|
+
private GraphicsDevice[] displayDevices;
|
66
|
+
|
67
|
+
private int displayWidth;
|
68
|
+
private int displayHeight;
|
69
|
+
|
70
|
+
|
71
|
+
/** Only needed for display functions */
|
72
|
+
static private ShimAWT getInstance() {
|
73
|
+
if (instance == null) {
|
74
|
+
instance = new ShimAWT();
|
75
|
+
}
|
76
|
+
return instance;
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
private ShimAWT() {
|
81
|
+
// Need the list of display devices to be queried already for usage below.
|
82
|
+
// https://github.com/processing/processing/issues/3295
|
83
|
+
// https://github.com/processing/processing/issues/3296
|
84
|
+
// Not doing this from a static initializer because it may cause
|
85
|
+
// PApplet to cache and the values to stick through subsequent runs.
|
86
|
+
// Instead make it a runtime thing and a local variable.
|
87
|
+
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
88
|
+
GraphicsDevice device = ge.getDefaultScreenDevice();
|
89
|
+
displayDevices = ge.getScreenDevices();
|
90
|
+
|
91
|
+
// // Default or unparsed will be -1, spanning will be 0, actual displays will
|
92
|
+
// // be numbered from 1 because it's too weird to say "display 0" in prefs.
|
93
|
+
// if (display > 0 && display <= displayDevices.length) {
|
94
|
+
// device = displayDevices[display-1];
|
95
|
+
// }
|
96
|
+
// When this was called, display will always be unset (even in 3.x),
|
97
|
+
// since this happens before settings() is called.
|
98
|
+
|
99
|
+
// Set displayWidth and displayHeight for people still using those.
|
100
|
+
DisplayMode displayMode = device.getDisplayMode();
|
101
|
+
displayWidth = displayMode.getWidth();
|
102
|
+
displayHeight = displayMode.getHeight();
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
static public int getDisplayWidth() {
|
107
|
+
return getInstance().displayWidth;
|
108
|
+
}
|
109
|
+
|
110
|
+
|
111
|
+
static public int getDisplayHeight() {
|
112
|
+
return getInstance().displayHeight;
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
static public int getDisplayCount() {
|
117
|
+
return getInstance().displayDevices.length;
|
118
|
+
}
|
119
|
+
|
120
|
+
|
121
|
+
static public int getDisplayDensity(int num) {
|
122
|
+
return getInstance().displayDensityImpl(num);
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
/*
|
127
|
+
private int displayDensityImpl() {
|
128
|
+
if (display != SPAN && (fullScreen || present)) {
|
129
|
+
return displayDensity(display);
|
130
|
+
}
|
131
|
+
// walk through all displays, use 2 if any display is 2
|
132
|
+
for (int i = 0; i < displayDevices.length; i++) {
|
133
|
+
if (displayDensity(i+1) == 2) {
|
134
|
+
return 2;
|
135
|
+
}
|
136
|
+
}
|
137
|
+
// If nobody's density is 2 then everyone is 1
|
138
|
+
return 1;
|
139
|
+
}
|
140
|
+
*/
|
141
|
+
|
142
|
+
|
143
|
+
private int displayDensityImpl(int display) {
|
144
|
+
if (display > 0 && display <= displayDevices.length) {
|
145
|
+
GraphicsConfiguration graphicsConfig =
|
146
|
+
displayDevices[display - 1].getDefaultConfiguration();
|
147
|
+
AffineTransform tx = graphicsConfig.getDefaultTransform();
|
148
|
+
return (int) Math.round(tx.getScaleX());
|
149
|
+
}
|
150
|
+
|
151
|
+
System.err.println("Display " + display + " does not exist, " +
|
152
|
+
"returning 1 for displayDensity(" + display + ")");
|
153
|
+
return 1; // not the end of the world, so don't throw a RuntimeException
|
154
|
+
}
|
155
|
+
|
156
|
+
|
157
|
+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
158
|
+
|
159
|
+
|
160
|
+
static protected String[] loadImageFormats; // list of ImageIO formats
|
161
|
+
|
162
|
+
|
163
|
+
static public PImage loadImage(PApplet sketch, String filename, Object... args) {
|
164
|
+
String extension = null;
|
165
|
+
if (args != null && args.length > 0) {
|
166
|
+
// the only one that's supported for now
|
167
|
+
extension = (String) args[0];
|
168
|
+
}
|
169
|
+
|
170
|
+
if (extension == null) {
|
171
|
+
String lower = filename.toLowerCase();
|
172
|
+
int dot = filename.lastIndexOf('.');
|
173
|
+
if (dot == -1) {
|
174
|
+
extension = "unknown"; // no extension found
|
175
|
+
|
176
|
+
} else {
|
177
|
+
extension = lower.substring(dot + 1);
|
178
|
+
|
179
|
+
// check for, and strip any parameters on the url, i.e.
|
180
|
+
// filename.jpg?blah=blah&something=that
|
181
|
+
int question = extension.indexOf('?');
|
182
|
+
if (question != -1) {
|
183
|
+
extension = extension.substring(0, question);
|
184
|
+
}
|
185
|
+
}
|
186
|
+
}
|
187
|
+
|
188
|
+
// just in case. them users will try anything!
|
189
|
+
extension = extension.toLowerCase();
|
190
|
+
|
191
|
+
if (extension.equals("tga")) {
|
192
|
+
try {
|
193
|
+
InputStream input = sketch.createInput(filename);
|
194
|
+
if (input == null) return null;
|
195
|
+
|
196
|
+
PImage image = PImage.loadTGA(input);
|
197
|
+
image.parent = sketch;
|
198
|
+
return image;
|
199
|
+
|
200
|
+
} catch (IOException e) {
|
201
|
+
e.printStackTrace();
|
202
|
+
return null;
|
203
|
+
}
|
204
|
+
}
|
205
|
+
|
206
|
+
if (extension.equals("tif") || extension.equals("tiff")) {
|
207
|
+
InputStream input = sketch.createInput(filename);
|
208
|
+
PImage image = (input == null) ? null : PImage.loadTIFF(input);
|
209
|
+
return image;
|
210
|
+
}
|
211
|
+
|
212
|
+
// For jpeg, gif, and png, load them using createImage(),
|
213
|
+
// because the javax.imageio code was found to be much slower.
|
214
|
+
// http://dev.processing.org/bugs/show_bug.cgi?id=392
|
215
|
+
try {
|
216
|
+
if (extension.equals("jpg") || extension.equals("jpeg") ||
|
217
|
+
extension.equals("gif") || extension.equals("png") ||
|
218
|
+
extension.equals("unknown")) {
|
219
|
+
byte[] bytes = sketch.loadBytes(filename);
|
220
|
+
if (bytes == null) {
|
221
|
+
return null;
|
222
|
+
} else {
|
223
|
+
//Image awtImage = Toolkit.getDefaultToolkit().createImage(bytes);
|
224
|
+
Image awtImage = new ImageIcon(bytes).getImage();
|
225
|
+
|
226
|
+
if (awtImage instanceof BufferedImage) {
|
227
|
+
BufferedImage buffImage = (BufferedImage) awtImage;
|
228
|
+
int space = buffImage.getColorModel().getColorSpace().getType();
|
229
|
+
if (space == ColorSpace.TYPE_CMYK) {
|
230
|
+
System.err.println(filename + " is a CMYK image, " +
|
231
|
+
"only RGB images are supported.");
|
232
|
+
return null;
|
233
|
+
/*
|
234
|
+
// wishful thinking, appears to not be supported
|
235
|
+
// https://community.oracle.com/thread/1272045?start=0&tstart=0
|
236
|
+
BufferedImage destImage =
|
237
|
+
new BufferedImage(buffImage.getWidth(),
|
238
|
+
buffImage.getHeight(),
|
239
|
+
BufferedImage.TYPE_3BYTE_BGR);
|
240
|
+
ColorConvertOp op = new ColorConvertOp(null);
|
241
|
+
op.filter(buffImage, destImage);
|
242
|
+
image = new PImage(destImage);
|
243
|
+
*/
|
244
|
+
}
|
245
|
+
}
|
246
|
+
|
247
|
+
PImage image = new PImageAWT(awtImage);
|
248
|
+
if (image.width == -1) {
|
249
|
+
System.err.println("The file " + filename +
|
250
|
+
" contains bad image data, or may not be an image.");
|
251
|
+
}
|
252
|
+
|
253
|
+
// if it's a .gif image, test to see if it has transparency
|
254
|
+
if (extension.equals("gif") || extension.equals("png") ||
|
255
|
+
extension.equals("unknown")) {
|
256
|
+
image.checkAlpha();
|
257
|
+
}
|
258
|
+
|
259
|
+
image.parent = sketch;
|
260
|
+
return image;
|
261
|
+
}
|
262
|
+
}
|
263
|
+
} catch (Exception e) {
|
264
|
+
// show error, but move on to the stuff below, see if it'll work
|
265
|
+
e.printStackTrace();
|
266
|
+
}
|
267
|
+
|
268
|
+
if (loadImageFormats == null) {
|
269
|
+
loadImageFormats = ImageIO.getReaderFormatNames();
|
270
|
+
}
|
271
|
+
if (loadImageFormats != null) {
|
272
|
+
for (int i = 0; i < loadImageFormats.length; i++) {
|
273
|
+
if (extension.equals(loadImageFormats[i])) {
|
274
|
+
return loadImageIO(sketch, filename);
|
275
|
+
}
|
276
|
+
}
|
277
|
+
}
|
278
|
+
|
279
|
+
// failed, could not load image after all those attempts
|
280
|
+
System.err.println("Could not find a method to load " + filename);
|
281
|
+
return null;
|
282
|
+
}
|
283
|
+
|
284
|
+
|
285
|
+
/**
|
286
|
+
* Use Java 1.4 ImageIO methods to load an image.
|
287
|
+
*/
|
288
|
+
static protected PImage loadImageIO(PApplet sketch, String filename) {
|
289
|
+
InputStream stream = sketch.createInput(filename);
|
290
|
+
if (stream == null) {
|
291
|
+
System.err.println("The image " + filename + " could not be found.");
|
292
|
+
return null;
|
293
|
+
}
|
294
|
+
|
295
|
+
try {
|
296
|
+
BufferedImage bi = ImageIO.read(stream);
|
297
|
+
//PImage outgoing = new PImage(bi.getWidth(), bi.getHeight());
|
298
|
+
PImage outgoing = new PImageAWT(bi);
|
299
|
+
outgoing.parent = sketch;
|
300
|
+
|
301
|
+
//bi.getRGB(0, 0, outgoing.width, outgoing.height,
|
302
|
+
// outgoing.pixels, 0, outgoing.width);
|
303
|
+
|
304
|
+
// check the alpha for this image
|
305
|
+
// was gonna call getType() on the image to see if RGB or ARGB,
|
306
|
+
// but it's not actually useful, since gif images will come through
|
307
|
+
// as TYPE_BYTE_INDEXED, which means it'll still have to check for
|
308
|
+
// the transparency. also, would have to iterate through all the other
|
309
|
+
// types and guess whether alpha was in there, so.. just gonna stick
|
310
|
+
// with the old method.
|
311
|
+
outgoing.checkAlpha();
|
312
|
+
|
313
|
+
stream.close();
|
314
|
+
// return the image
|
315
|
+
return outgoing;
|
316
|
+
|
317
|
+
} catch (Exception e) {
|
318
|
+
e.printStackTrace();
|
319
|
+
return null;
|
320
|
+
}
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
325
|
+
|
326
|
+
|
327
|
+
static public boolean saveImage(PImage image, String path) {
|
328
|
+
if (saveImageFormats == null) {
|
329
|
+
saveImageFormats = javax.imageio.ImageIO.getWriterFormatNames();
|
330
|
+
}
|
331
|
+
try {
|
332
|
+
if (saveImageFormats != null) {
|
333
|
+
for (int i = 0; i < saveImageFormats.length; i++) {
|
334
|
+
if (path.endsWith("." + saveImageFormats[i])) {
|
335
|
+
if (!saveImageIO(image, path)) {
|
336
|
+
System.err.println("Error while saving image.");
|
337
|
+
return false;
|
338
|
+
}
|
339
|
+
return true;
|
340
|
+
}
|
341
|
+
}
|
342
|
+
}
|
343
|
+
} catch (IOException e) {
|
344
|
+
e.printStackTrace();
|
345
|
+
}
|
346
|
+
return false;
|
347
|
+
}
|
348
|
+
|
349
|
+
|
350
|
+
static protected String[] saveImageFormats;
|
351
|
+
|
352
|
+
|
353
|
+
/**
|
354
|
+
* Use ImageIO functions from Java 1.4 and later to handle image save.
|
355
|
+
* Various formats are supported, typically jpeg, png, bmp, and wbmp.
|
356
|
+
* To get a list of the supported formats for writing, use: <BR>
|
357
|
+
* <TT>println(javax.imageio.ImageIO.getReaderFormatNames())</TT>
|
358
|
+
*/
|
359
|
+
static protected boolean saveImageIO(PImage image, String path) throws IOException {
|
360
|
+
try {
|
361
|
+
int outputFormat = (image.format == ARGB) ?
|
362
|
+
BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
|
363
|
+
|
364
|
+
String extension =
|
365
|
+
path.substring(path.lastIndexOf('.') + 1).toLowerCase();
|
366
|
+
|
367
|
+
// JPEG and BMP images that have an alpha channel set get pretty unhappy.
|
368
|
+
// BMP just doesn't write, and JPEG writes it as a CMYK image.
|
369
|
+
// http://code.google.com/p/processing/issues/detail?id=415
|
370
|
+
if (extension.equals("bmp") || extension.equals("jpg") || extension.equals("jpeg")) {
|
371
|
+
outputFormat = BufferedImage.TYPE_INT_RGB;
|
372
|
+
}
|
373
|
+
|
374
|
+
BufferedImage bimage = new BufferedImage(image.pixelWidth, image.pixelHeight, outputFormat);
|
375
|
+
bimage.setRGB(0, 0, image.pixelWidth, image.pixelHeight, image.pixels, 0, image.pixelWidth);
|
376
|
+
|
377
|
+
File file = new File(path);
|
378
|
+
|
379
|
+
ImageWriter writer = null;
|
380
|
+
ImageWriteParam param = null;
|
381
|
+
IIOMetadata metadata = null;
|
382
|
+
|
383
|
+
if (extension.equals("jpg") || extension.equals("jpeg")) {
|
384
|
+
if ((writer = imageioWriter("jpeg")) != null) {
|
385
|
+
// Set JPEG quality to 90% with baseline optimization. Setting this
|
386
|
+
// to 1 was a huge jump (about triple the size), so this seems good.
|
387
|
+
// Oddly, a smaller file size than Photoshop at 90%, but I suppose
|
388
|
+
// it's a completely different algorithm.
|
389
|
+
param = writer.getDefaultWriteParam();
|
390
|
+
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
391
|
+
param.setCompressionQuality(0.9f);
|
392
|
+
}
|
393
|
+
}
|
394
|
+
|
395
|
+
if (extension.equals("png")) {
|
396
|
+
if ((writer = imageioWriter("png")) != null) {
|
397
|
+
param = writer.getDefaultWriteParam();
|
398
|
+
if (false) {
|
399
|
+
metadata = imageioDPI(writer, param, 100);
|
400
|
+
}
|
401
|
+
}
|
402
|
+
}
|
403
|
+
|
404
|
+
if (writer != null) {
|
405
|
+
BufferedOutputStream output =
|
406
|
+
new BufferedOutputStream(PApplet.createOutput(file));
|
407
|
+
writer.setOutput(ImageIO.createImageOutputStream(output));
|
408
|
+
// writer.write(null, new IIOImage(bimage, null, null), param);
|
409
|
+
writer.write(metadata, new IIOImage(bimage, null, metadata), param);
|
410
|
+
writer.dispose();
|
411
|
+
|
412
|
+
output.flush();
|
413
|
+
output.close();
|
414
|
+
return true;
|
415
|
+
}
|
416
|
+
// If iter.hasNext() somehow fails up top, it falls through to here
|
417
|
+
return javax.imageio.ImageIO.write(bimage, extension, file);
|
418
|
+
|
419
|
+
} catch (Exception e) {
|
420
|
+
e.printStackTrace();
|
421
|
+
throw new IOException("image save failed.");
|
422
|
+
}
|
423
|
+
}
|
424
|
+
|
425
|
+
|
426
|
+
static private ImageWriter imageioWriter(String extension) {
|
427
|
+
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(extension);
|
428
|
+
if (iter.hasNext()) {
|
429
|
+
return iter.next();
|
430
|
+
}
|
431
|
+
return null;
|
432
|
+
}
|
433
|
+
|
434
|
+
|
435
|
+
static private IIOMetadata imageioDPI(ImageWriter writer, ImageWriteParam param, double dpi) {
|
436
|
+
// http://stackoverflow.com/questions/321736/how-to-set-dpi-information-in-an-image
|
437
|
+
ImageTypeSpecifier typeSpecifier =
|
438
|
+
ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
|
439
|
+
IIOMetadata metadata =
|
440
|
+
writer.getDefaultImageMetadata(typeSpecifier, param);
|
441
|
+
|
442
|
+
if (!metadata.isReadOnly() && metadata.isStandardMetadataFormatSupported()) {
|
443
|
+
// for PNG, it's dots per millimeter
|
444
|
+
double dotsPerMilli = dpi / 25.4;
|
445
|
+
|
446
|
+
IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize");
|
447
|
+
horiz.setAttribute("value", Double.toString(dotsPerMilli));
|
448
|
+
|
449
|
+
IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize");
|
450
|
+
vert.setAttribute("value", Double.toString(dotsPerMilli));
|
451
|
+
|
452
|
+
IIOMetadataNode dim = new IIOMetadataNode("Dimension");
|
453
|
+
dim.appendChild(horiz);
|
454
|
+
dim.appendChild(vert);
|
455
|
+
|
456
|
+
IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
|
457
|
+
root.appendChild(dim);
|
458
|
+
|
459
|
+
try {
|
460
|
+
metadata.mergeTree("javax_imageio_1.0", root);
|
461
|
+
return metadata;
|
462
|
+
|
463
|
+
} catch (IIOInvalidTreeException e) {
|
464
|
+
System.err.println("Could not set the DPI of the output image");
|
465
|
+
e.printStackTrace();
|
466
|
+
}
|
467
|
+
}
|
468
|
+
return null;
|
469
|
+
}
|
470
|
+
|
471
|
+
|
472
|
+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
473
|
+
|
474
|
+
|
475
|
+
static public void initRun() {
|
476
|
+
// Supposed to help with flicker, but no effect on OS X.
|
477
|
+
// TODO IIRC this helped on Windows, but need to double check.
|
478
|
+
System.setProperty("sun.awt.noerasebackground", "true");
|
479
|
+
|
480
|
+
// Remove 60fps limit on the JavaFX "pulse" timer
|
481
|
+
System.setProperty("javafx.animation.fullspeed", "true");
|
482
|
+
|
483
|
+
// Catch any HeadlessException to provide more useful feedback
|
484
|
+
try {
|
485
|
+
// Call validate() while resize events are in progress
|
486
|
+
Toolkit.getDefaultToolkit().setDynamicLayout(true);
|
487
|
+
} catch (HeadlessException e) {
|
488
|
+
System.err.println("Cannot run sketch without a display. Read this for possible solutions:");
|
489
|
+
System.err.println("https://github.com/processing/processing/wiki/Running-without-a-Display");
|
490
|
+
System.exit(1);
|
491
|
+
}
|
492
|
+
}
|
493
|
+
|
494
|
+
|
495
|
+
/*
|
496
|
+
public int displayDensity() {
|
497
|
+
if (sketch.display != PConstants.SPAN && (sketch.fullScreen || sketch.present)) {
|
498
|
+
return displayDensity(sketch.display);
|
499
|
+
}
|
500
|
+
// walk through all displays, use 2 if any display is 2
|
501
|
+
for (int i = 0; i < displayDevices.length; i++) {
|
502
|
+
if (displayDensity(i+1) == 2) {
|
503
|
+
return 2;
|
504
|
+
}
|
505
|
+
}
|
506
|
+
// If nobody's density is 2 then everyone is 1
|
507
|
+
return 1;
|
508
|
+
}
|
509
|
+
*/
|
510
|
+
|
511
|
+
|
512
|
+
/**
|
513
|
+
* @param display the display number to check
|
514
|
+
* (1-indexed to match the Preferences dialog box)
|
515
|
+
*/
|
516
|
+
/*
|
517
|
+
public int displayDensity(int display) {
|
518
|
+
if (display > 0 && display <= displayDevices.length) {
|
519
|
+
GraphicsConfiguration graphicsConfig =
|
520
|
+
displayDevices[display - 1].getDefaultConfiguration();
|
521
|
+
AffineTransform tx = graphicsConfig.getDefaultTransform();
|
522
|
+
return (int) Math.round(tx.getScaleX());
|
523
|
+
}
|
524
|
+
|
525
|
+
System.err.println("Display " + display + " does not exist, returning ");
|
526
|
+
return 1; // not the end of the world, so don't throw a RuntimeException
|
527
|
+
}
|
528
|
+
*/
|
529
|
+
|
530
|
+
|
531
|
+
static public void selectInput(String prompt, String callbackMethod,
|
532
|
+
File file, Object callbackObject) {
|
533
|
+
EventQueue.invokeLater(() -> {
|
534
|
+
selectImpl(prompt, callbackMethod, file,
|
535
|
+
callbackObject, null, FileDialog.LOAD);
|
536
|
+
});
|
537
|
+
}
|
538
|
+
|
539
|
+
|
540
|
+
/*
|
541
|
+
static public void selectOutput(String prompt, String callbackMethod,
|
542
|
+
File file, Object callbackObject, Frame parent) {
|
543
|
+
selectImpl(prompt, callbackMethod, file, callbackObject, parent, FileDialog.SAVE, null);
|
544
|
+
}
|
545
|
+
|
546
|
+
|
547
|
+
static public void selectOutput(String prompt, String callbackMethod,
|
548
|
+
File file, Object callbackObject, Frame parent,
|
549
|
+
PApplet sketch) {
|
550
|
+
selectImpl(prompt, callbackMethod, file, callbackObject, parent, FileDialog.SAVE, sketch);
|
551
|
+
}
|
552
|
+
*/
|
553
|
+
|
554
|
+
|
555
|
+
static public void selectOutput(String prompt, String callbackMethod,
|
556
|
+
File file, Object callbackObject) {
|
557
|
+
EventQueue.invokeLater(() -> {
|
558
|
+
selectImpl(prompt, callbackMethod, file,
|
559
|
+
callbackObject, null, FileDialog.SAVE);
|
560
|
+
});
|
561
|
+
}
|
562
|
+
|
563
|
+
|
564
|
+
/*
|
565
|
+
// Will remove the 'sketch' parameter once we get an upstream JOGL fix
|
566
|
+
// https://github.com/processing/processing/issues/3831
|
567
|
+
static protected void selectEvent(final String prompt,
|
568
|
+
final String callbackMethod,
|
569
|
+
final File defaultSelection,
|
570
|
+
final Object callbackObject,
|
571
|
+
final Frame parentFrame,
|
572
|
+
final int mode,
|
573
|
+
final PApplet sketch) {
|
574
|
+
EventQueue.invokeLater(new Runnable() {
|
575
|
+
public void run() {
|
576
|
+
boolean hide = (sketch != null) &&
|
577
|
+
(sketch.g instanceof PGraphicsOpenGL) &&
|
578
|
+
(PApplet.platform == PConstants.WINDOWS);
|
579
|
+
if (hide) sketch.getSurface().setVisible(false);
|
580
|
+
|
581
|
+
selectImpl(prompt, callbackMethod, defaultSelection, callbackObject,
|
582
|
+
parentFrame, mode, sketch);
|
583
|
+
|
584
|
+
if (hide) sketch.getSurface().setVisible(true);
|
585
|
+
}
|
586
|
+
});
|
587
|
+
}
|
588
|
+
*/
|
589
|
+
|
590
|
+
|
591
|
+
static public void selectImpl(final String prompt,
|
592
|
+
final String callbackMethod,
|
593
|
+
final File defaultSelection,
|
594
|
+
final Object callbackObject,
|
595
|
+
final Frame parentFrame,
|
596
|
+
final int mode) {
|
597
|
+
File selectedFile = null;
|
598
|
+
|
599
|
+
if (PApplet.useNativeSelect) {
|
600
|
+
FileDialog dialog = new FileDialog(parentFrame, prompt, mode);
|
601
|
+
if (defaultSelection != null) {
|
602
|
+
dialog.setDirectory(defaultSelection.getParent());
|
603
|
+
dialog.setFile(defaultSelection.getName());
|
604
|
+
}
|
605
|
+
|
606
|
+
dialog.setVisible(true);
|
607
|
+
String directory = dialog.getDirectory();
|
608
|
+
String filename = dialog.getFile();
|
609
|
+
if (filename != null) {
|
610
|
+
selectedFile = new File(directory, filename);
|
611
|
+
}
|
612
|
+
|
613
|
+
} else {
|
614
|
+
JFileChooser chooser = new JFileChooser();
|
615
|
+
chooser.setDialogTitle(prompt);
|
616
|
+
if (defaultSelection != null) {
|
617
|
+
chooser.setSelectedFile(defaultSelection);
|
618
|
+
}
|
619
|
+
|
620
|
+
int result = -1;
|
621
|
+
if (mode == FileDialog.SAVE) {
|
622
|
+
result = chooser.showSaveDialog(parentFrame);
|
623
|
+
} else if (mode == FileDialog.LOAD) {
|
624
|
+
result = chooser.showOpenDialog(parentFrame);
|
625
|
+
}
|
626
|
+
if (result == JFileChooser.APPROVE_OPTION) {
|
627
|
+
selectedFile = chooser.getSelectedFile();
|
628
|
+
}
|
629
|
+
}
|
630
|
+
PApplet.selectCallback(selectedFile, callbackMethod, callbackObject);
|
631
|
+
}
|
632
|
+
|
633
|
+
|
634
|
+
static public void selectFolder(final String prompt,
|
635
|
+
final String callbackMethod,
|
636
|
+
final File defaultSelection,
|
637
|
+
final Object callbackObject) {
|
638
|
+
EventQueue.invokeLater(() -> {
|
639
|
+
selectFolderImpl(prompt, callbackMethod, defaultSelection,
|
640
|
+
callbackObject, null);
|
641
|
+
});
|
642
|
+
}
|
643
|
+
|
644
|
+
|
645
|
+
/*
|
646
|
+
static public void selectFolder(final String prompt,
|
647
|
+
final String callbackMethod,
|
648
|
+
final File defaultSelection,
|
649
|
+
final Object callbackObject,
|
650
|
+
final Frame parentFrame) {
|
651
|
+
selectFolderEvent(prompt, callbackMethod, defaultSelection, callbackObject, parentFrame, null);
|
652
|
+
}
|
653
|
+
|
654
|
+
|
655
|
+
// Will remove the 'sketch' parameter once we get an upstream JOGL fix
|
656
|
+
// https://github.com/processing/processing/issues/3831
|
657
|
+
static public void selectFolderEvent(final String prompt,
|
658
|
+
final String callbackMethod,
|
659
|
+
final File defaultSelection,
|
660
|
+
final Object callbackObject,
|
661
|
+
final Frame parentFrame,
|
662
|
+
final PApplet sketch) {
|
663
|
+
EventQueue.invokeLater(() -> {
|
664
|
+
selectFolderImpl(prompt, callbackMethod, defaultSelection,
|
665
|
+
callbackObject, parentFrame, sketch);
|
666
|
+
});
|
667
|
+
}
|
668
|
+
*/
|
669
|
+
|
670
|
+
|
671
|
+
static public void selectFolderImpl(final String prompt,
|
672
|
+
final String callbackMethod,
|
673
|
+
final File defaultSelection,
|
674
|
+
final Object callbackObject,
|
675
|
+
final Frame parentFrame) {
|
676
|
+
File selectedFile = null;
|
677
|
+
|
678
|
+
JFileChooser fileChooser = new JFileChooser();
|
679
|
+
fileChooser.setDialogTitle(prompt);
|
680
|
+
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
681
|
+
if (defaultSelection != null) {
|
682
|
+
fileChooser.setCurrentDirectory(defaultSelection);
|
683
|
+
}
|
684
|
+
|
685
|
+
int result = fileChooser.showOpenDialog(parentFrame);
|
686
|
+
if (result == JFileChooser.APPROVE_OPTION) {
|
687
|
+
selectedFile = fileChooser.getSelectedFile();
|
688
|
+
}
|
689
|
+
|
690
|
+
PApplet.selectCallback(selectedFile, callbackMethod, callbackObject);
|
691
|
+
}
|
692
|
+
|
693
|
+
|
694
|
+
// TODO maybe call this with reflection from inside PApplet?
|
695
|
+
// longer term, develop a more general method for other platforms
|
696
|
+
static public File getWindowsDesktop() {
|
697
|
+
return FileSystemView.getFileSystemView().getHomeDirectory();
|
698
|
+
}
|
699
|
+
|
700
|
+
|
701
|
+
static public boolean openLink(String url) {
|
702
|
+
try {
|
703
|
+
if (Desktop.isDesktopSupported()) {
|
704
|
+
Desktop.getDesktop().browse(new URI(url));
|
705
|
+
return true;
|
706
|
+
}
|
707
|
+
} catch (IOException | URISyntaxException e) {
|
708
|
+
}
|
709
|
+
return false;
|
710
|
+
}
|
711
|
+
}
|