propane 3.4.0-java → 3.7.0.pre-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.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +1 -2
  3. data/.mvn/wrapper/MavenWrapperDownloader.java +2 -2
  4. data/.mvn/wrapper/maven-wrapper.properties +2 -2
  5. data/.travis.yml +2 -2
  6. data/CHANGELOG.md +12 -0
  7. data/Gemfile +2 -0
  8. data/README.md +17 -8
  9. data/Rakefile +10 -11
  10. data/bin/propane +3 -1
  11. data/lib/propane.rb +6 -4
  12. data/lib/propane/app.rb +20 -10
  13. data/lib/propane/creators/sketch_class.rb +7 -1
  14. data/lib/propane/creators/sketch_factory.rb +4 -2
  15. data/lib/propane/creators/sketch_writer.rb +1 -0
  16. data/lib/propane/helper_methods.rb +23 -24
  17. data/lib/propane/helpers/numeric.rb +2 -0
  18. data/lib/propane/helpers/version_error.rb +1 -0
  19. data/lib/propane/library.rb +5 -1
  20. data/lib/propane/library_loader.rb +2 -0
  21. data/lib/propane/native_folder.rb +10 -9
  22. data/lib/propane/native_loader.rb +3 -0
  23. data/lib/propane/runner.rb +14 -6
  24. data/lib/propane/version.rb +2 -1
  25. data/library/boids/boids.rb +21 -11
  26. data/library/color_group/color_group.rb +28 -0
  27. data/library/control_panel/control_panel.rb +8 -5
  28. data/library/dxf/dxf.rb +6 -0
  29. data/library/file_chooser/chooser.rb +10 -9
  30. data/library/file_chooser/file_chooser.rb +10 -9
  31. data/library/library_proxy/library_proxy.rb +2 -0
  32. data/library/net/net.rb +7 -0
  33. data/library/simplex_noise/simplex_noise.rb +2 -0
  34. data/library/slider/slider.rb +23 -22
  35. data/library/vector_utils/vector_utils.rb +4 -0
  36. data/library/video_event/video_event.rb +4 -1
  37. data/pom.rb +37 -36
  38. data/pom.xml +7 -7
  39. data/propane.gemspec +16 -12
  40. data/src/main/java/monkstone/ColorUtil.java +13 -1
  41. data/src/main/java/monkstone/MathToolModule.java +253 -203
  42. data/src/main/java/monkstone/PropaneLibrary.java +2 -2
  43. data/src/main/java/monkstone/fastmath/Deglut.java +1 -1
  44. data/src/main/java/monkstone/filechooser/Chooser.java +2 -1
  45. data/src/main/java/monkstone/noise/SimplexNoise.java +2 -2
  46. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +1 -1
  47. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
  48. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +1 -1
  49. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +1 -1
  50. data/src/main/java/monkstone/slider/SliderBar.java +1 -1
  51. data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
  52. data/src/main/java/monkstone/slider/WheelHandler.java +7 -6
  53. data/src/main/java/monkstone/vecmath/package-info.java +1 -1
  54. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +1 -1
  55. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +3 -3
  56. data/src/main/java/monkstone/videoevent/CaptureEvent.java +27 -0
  57. data/src/main/java/monkstone/videoevent/{VideoInterface.java → MovieEvent.java} +11 -27
  58. data/src/main/java/monkstone/videoevent/package-info.java +1 -1
  59. data/src/main/java/processing/awt/PGraphicsJava2D.java +781 -285
  60. data/src/main/java/processing/awt/PImageAWT.java +377 -0
  61. data/src/main/java/processing/awt/PShapeJava2D.java +56 -52
  62. data/src/main/java/processing/awt/PSurfaceAWT.java +309 -209
  63. data/src/main/java/processing/awt/ShimAWT.java +581 -0
  64. data/src/main/java/processing/core/PApplet.java +4510 -4503
  65. data/src/main/java/processing/core/PConstants.java +477 -447
  66. data/src/main/java/processing/core/PFont.java +914 -880
  67. data/src/main/java/processing/core/PGraphics.java +193 -177
  68. data/src/main/java/processing/core/PImage.java +611 -309
  69. data/src/main/java/processing/core/PMatrix.java +172 -159
  70. data/src/main/java/processing/core/PMatrix2D.java +478 -415
  71. data/src/main/java/processing/core/PMatrix3D.java +762 -735
  72. data/src/main/java/processing/core/PShape.java +2888 -2652
  73. data/src/main/java/processing/core/PShapeOBJ.java +97 -92
  74. data/src/main/java/processing/core/PShapeSVG.java +1705 -1490
  75. data/src/main/java/processing/core/PStyle.java +40 -37
  76. data/src/main/java/processing/core/PSurface.java +139 -97
  77. data/src/main/java/processing/core/PSurfaceNone.java +296 -218
  78. data/src/main/java/processing/core/PVector.java +997 -965
  79. data/src/main/java/processing/core/ThinkDifferent.java +15 -13
  80. data/src/main/java/processing/data/DoubleDict.java +756 -710
  81. data/src/main/java/processing/data/DoubleList.java +749 -696
  82. data/src/main/java/processing/data/FloatDict.java +748 -702
  83. data/src/main/java/processing/data/FloatList.java +751 -697
  84. data/src/main/java/processing/data/IntDict.java +720 -673
  85. data/src/main/java/processing/data/IntList.java +699 -633
  86. data/src/main/java/processing/data/JSONArray.java +931 -873
  87. data/src/main/java/processing/data/JSONObject.java +1262 -1165
  88. data/src/main/java/processing/data/JSONTokener.java +351 -341
  89. data/src/main/java/processing/data/LongDict.java +710 -663
  90. data/src/main/java/processing/data/LongList.java +701 -635
  91. data/src/main/java/processing/data/Sort.java +37 -41
  92. data/src/main/java/processing/data/StringDict.java +525 -486
  93. data/src/main/java/processing/data/StringList.java +626 -580
  94. data/src/main/java/processing/data/Table.java +3690 -3510
  95. data/src/main/java/processing/data/TableRow.java +182 -183
  96. data/src/main/java/processing/data/XML.java +957 -883
  97. data/src/main/java/processing/dxf/RawDXF.java +404 -0
  98. data/src/main/java/processing/event/Event.java +87 -67
  99. data/src/main/java/processing/event/KeyEvent.java +48 -41
  100. data/src/main/java/processing/event/MouseEvent.java +88 -113
  101. data/src/main/java/processing/event/TouchEvent.java +10 -6
  102. data/src/main/java/processing/javafx/PGraphicsFX2D.java +20 -345
  103. data/src/main/java/processing/javafx/PSurfaceFX.java +149 -121
  104. data/src/main/java/processing/net/Client.java +744 -0
  105. data/src/main/java/processing/net/Server.java +388 -0
  106. data/src/main/java/processing/opengl/FontTexture.java +289 -270
  107. data/src/main/java/processing/opengl/FrameBuffer.java +386 -364
  108. data/src/main/java/processing/opengl/LinePath.java +547 -500
  109. data/src/main/java/processing/opengl/LineStroker.java +588 -581
  110. data/src/main/java/processing/opengl/PGL.java +3047 -2914
  111. data/src/main/java/processing/opengl/PGraphics2D.java +408 -315
  112. data/src/main/java/processing/opengl/PGraphics3D.java +107 -72
  113. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +12378 -12075
  114. data/src/main/java/processing/opengl/PJOGL.java +1753 -1670
  115. data/src/main/java/processing/opengl/PShader.java +1266 -1257
  116. data/src/main/java/processing/opengl/PShapeOpenGL.java +4678 -4580
  117. data/src/main/java/processing/opengl/PSurfaceJOGL.java +1114 -1027
  118. data/src/main/java/processing/opengl/Texture.java +1492 -1401
  119. data/src/main/java/processing/opengl/VertexBuffer.java +57 -55
  120. data/test/create_test.rb +21 -20
  121. data/test/deglut_spec_test.rb +4 -2
  122. data/test/helper_methods_test.rb +49 -20
  123. data/test/math_tool_test.rb +39 -32
  124. data/test/native_folder.rb +47 -0
  125. data/test/respond_to_test.rb +3 -2
  126. data/test/sketches/key_event.rb +2 -2
  127. data/test/sketches/library/my_library/my_library.rb +3 -0
  128. data/test/test_helper.rb +2 -0
  129. data/test/vecmath_spec_test.rb +35 -22
  130. data/vendors/Rakefile +33 -62
  131. metadata +56 -48
  132. data/src/main/java/processing/core/util/image/ImageLoadFacade.java +0 -161
  133. data/src/main/java/processing/core/util/image/ImageSaveFacade.java +0 -169
  134. data/src/main/java/processing/core/util/image/constants/TifConstants.java +0 -45
  135. data/src/main/java/processing/core/util/image/load/AwtImageLoadStrategy.java +0 -80
  136. data/src/main/java/processing/core/util/image/load/Base64StringImageLoadStrategy.java +0 -73
  137. data/src/main/java/processing/core/util/image/load/FallbackImageLoadStrategy.java +0 -70
  138. data/src/main/java/processing/core/util/image/load/ImageIoImageLoadStrategy.java +0 -132
  139. data/src/main/java/processing/core/util/image/load/ImageLoadStrategy.java +0 -48
  140. data/src/main/java/processing/core/util/image/load/ImageLoadUtil.java +0 -45
  141. data/src/main/java/processing/core/util/image/load/TgaImageLoadStrategy.java +0 -255
  142. data/src/main/java/processing/core/util/image/load/TiffImageLoadStrategy.java +0 -98
  143. data/src/main/java/processing/core/util/image/save/ImageSaveStrategy.java +0 -49
  144. data/src/main/java/processing/core/util/image/save/ImageSaveUtil.java +0 -48
  145. data/src/main/java/processing/core/util/image/save/ImageWriterImageSaveStrategy.java +0 -179
  146. data/src/main/java/processing/core/util/image/save/SaveImageException.java +0 -41
  147. data/src/main/java/processing/core/util/image/save/TgaImageSaveStrategy.java +0 -198
  148. data/src/main/java/processing/core/util/image/save/TiffImageSaveStrategy.java +0 -91
  149. data/src/main/java/processing/core/util/image/save/TiffNakedFilenameImageSaveStrategy.java +0 -57
  150. data/src/main/java/processing/core/util/io/InputFactory.java +0 -285
  151. data/src/main/java/processing/core/util/io/PathUtil.java +0 -109
  152. data/src/main/java/processing/opengl/shaders/LightVert-brcm.glsl +0 -154
  153. data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +0 -154
  154. data/src/main/java/processing/opengl/shaders/TexLightVert-brcm.glsl +0 -160
  155. data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +0 -160
@@ -0,0 +1,377 @@
1
+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
+
3
+ /*
4
+ Part of the Processing project - http://processing.org
5
+
6
+ Copyright (c) 2015 The Processing Foundation
7
+
8
+ This library is free software; you can redistribute it and/or
9
+ modify it under the terms of the GNU Lesser General Public
10
+ License version 2.1 as published by the Free Software Foundation.
11
+
12
+ This library is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ Lesser General Public License for more details.
16
+
17
+ You should have received a copy of the GNU Lesser General
18
+ Public License along with this library; if not, write to the
19
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
+ Boston, MA 02111-1307 USA
21
+ */
22
+
23
+ package processing.awt;
24
+
25
+ import java.awt.Graphics2D;
26
+ import java.awt.Image;
27
+ import java.awt.RenderingHints;
28
+ import java.awt.Transparency;
29
+ import java.awt.image.BufferedImage;
30
+ import java.awt.image.DataBuffer;
31
+ import java.awt.image.DataBufferInt;
32
+ import java.awt.image.PixelGrabber;
33
+ import java.awt.image.WritableRaster;
34
+ import java.io.BufferedOutputStream;
35
+ import java.io.File;
36
+ import java.io.IOException;
37
+ import java.util.Iterator;
38
+
39
+ import javax.imageio.IIOImage;
40
+ import javax.imageio.ImageIO;
41
+ import javax.imageio.ImageTypeSpecifier;
42
+ import javax.imageio.ImageWriteParam;
43
+ import javax.imageio.ImageWriter;
44
+ import javax.imageio.metadata.IIOInvalidTreeException;
45
+ import javax.imageio.metadata.IIOMetadata;
46
+ import javax.imageio.metadata.IIOMetadataNode;
47
+
48
+ import processing.core.PApplet;
49
+ import processing.core.PImage;
50
+
51
+
52
+ public class PImageAWT extends PImage {
53
+
54
+ /**
55
+ * Construct a new PImage from a java.awt.Image. This constructor assumes
56
+ * that you've done the work of making sure a MediaTracker has been used
57
+ * to fully download the data and that the img is valid.
58
+ *
59
+ * @nowebref
60
+ * @param img assumes a MediaTracker has been used to fully download
61
+ * the data and the img is valid
62
+ */
63
+ public PImageAWT(Image img) {
64
+ format = RGB;
65
+ if (img instanceof BufferedImage) {
66
+ BufferedImage bi = (BufferedImage) img;
67
+ width = bi.getWidth();
68
+ height = bi.getHeight();
69
+ int type = bi.getType();
70
+ if (type == BufferedImage.TYPE_3BYTE_BGR ||
71
+ type == BufferedImage.TYPE_4BYTE_ABGR) {
72
+ pixels = new int[width * height];
73
+ bi.getRGB(0, 0, width, height, pixels, 0, width);
74
+ if (type == BufferedImage.TYPE_4BYTE_ABGR) {
75
+ format = ARGB;
76
+ } else {
77
+ opaque();
78
+ }
79
+ } else {
80
+ DataBuffer db = bi.getRaster().getDataBuffer();
81
+ if (db instanceof DataBufferInt) {
82
+ pixels = ((DataBufferInt) db).getData();
83
+ if (type == BufferedImage.TYPE_INT_ARGB) {
84
+ format = ARGB;
85
+ } else if (type == BufferedImage.TYPE_INT_RGB) {
86
+ opaque();
87
+ }
88
+ }
89
+ }
90
+ }
91
+ // Implements fall-through if not DataBufferInt above, or not a
92
+ // known type, or not DataBufferInt for the data itself.
93
+ if (pixels == null) { // go the old school Java 1.0 route
94
+ width = img.getWidth(null);
95
+ height = img.getHeight(null);
96
+ pixels = new int[width * height];
97
+ PixelGrabber pg =
98
+ new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
99
+ try {
100
+ pg.grabPixels();
101
+ } catch (InterruptedException e) { }
102
+ }
103
+ pixelDensity = 1;
104
+ pixelWidth = width;
105
+ pixelHeight = height;
106
+ }
107
+
108
+
109
+ /**
110
+ * Use the getNative() method instead, which allows library interfaces to be
111
+ * written in a cross-platform fashion for desktop, Android, and others.
112
+ * This is still included for PGraphics objects, which may need the image.
113
+ */
114
+ public Image getImage() { // ignore
115
+ return (Image) getNative();
116
+ }
117
+
118
+
119
+ /**
120
+ * Returns a native BufferedImage from this PImage.
121
+ */
122
+ @Override
123
+ public Object getNative() { // ignore
124
+ loadPixels();
125
+ int type = (format == RGB) ?
126
+ BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
127
+ BufferedImage image = new BufferedImage(pixelWidth, pixelHeight, type);
128
+ WritableRaster wr = image.getRaster();
129
+ wr.setDataElements(0, 0, pixelWidth, pixelHeight, pixels);
130
+ return image;
131
+ }
132
+
133
+
134
+ @Override
135
+ public void resize(int w, int h) { // ignore
136
+ if (w <= 0 && h <= 0) {
137
+ throw new IllegalArgumentException("width or height must be > 0 for resize");
138
+ }
139
+
140
+ if (w == 0) { // Use height to determine relative size
141
+ float diff = (float) h / (float) height;
142
+ w = (int) (width * diff);
143
+ } else if (h == 0) { // Use the width to determine relative size
144
+ float diff = (float) w / (float) width;
145
+ h = (int) (height * diff);
146
+ }
147
+
148
+ BufferedImage img =
149
+ shrinkImage((BufferedImage) getNative(), w*pixelDensity, h*pixelDensity);
150
+
151
+ PImage temp = new PImageAWT(img);
152
+ this.pixelWidth = temp.width;
153
+ this.pixelHeight = temp.height;
154
+
155
+ // Get the resized pixel array
156
+ this.pixels = temp.pixels;
157
+
158
+ this.width = pixelWidth / pixelDensity;
159
+ this.height = pixelHeight / pixelDensity;
160
+
161
+ // Mark the pixels array as altered
162
+ updatePixels();
163
+ }
164
+
165
+
166
+ // Adapted from getFasterScaledInstance() method from page 111 of
167
+ // "Filthy Rich Clients" by Chet Haase and Romain Guy
168
+ // Additional modifications and simplifications have been added,
169
+ // plus a fix to deal with an infinite loop if images are expanded.
170
+ // http://code.google.com/p/processing/issues/detail?id=1463
171
+ static private BufferedImage shrinkImage(BufferedImage img,
172
+ int targetWidth, int targetHeight) {
173
+ int type = (img.getTransparency() == Transparency.OPAQUE) ?
174
+ BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
175
+ BufferedImage outgoing = img;
176
+ BufferedImage scratchImage = null;
177
+ Graphics2D g2 = null;
178
+ int prevW = outgoing.getWidth();
179
+ int prevH = outgoing.getHeight();
180
+ boolean isTranslucent = img.getTransparency() != Transparency.OPAQUE;
181
+
182
+ // Use multi-step technique: start with original size, then scale down in
183
+ // multiple passes with drawImage() until the target size is reached
184
+ int w = img.getWidth();
185
+ int h = img.getHeight();
186
+
187
+ do {
188
+ if (w > targetWidth) {
189
+ w /= 2;
190
+ // if this is the last step, do the exact size
191
+ if (w < targetWidth) {
192
+ w = targetWidth;
193
+ }
194
+ } else if (targetWidth >= w) {
195
+ w = targetWidth;
196
+ }
197
+ if (h > targetHeight) {
198
+ h /= 2;
199
+ if (h < targetHeight) {
200
+ h = targetHeight;
201
+ }
202
+ } else if (targetHeight >= h) {
203
+ h = targetHeight;
204
+ }
205
+ if (scratchImage == null || isTranslucent) {
206
+ // Use a single scratch buffer for all iterations and then copy
207
+ // to the final, correctly-sized image before returning
208
+ scratchImage = new BufferedImage(w, h, type);
209
+ g2 = scratchImage.createGraphics();
210
+ }
211
+ g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
212
+ RenderingHints.VALUE_INTERPOLATION_BILINEAR);
213
+ g2.drawImage(outgoing, 0, 0, w, h, 0, 0, prevW, prevH, null);
214
+ prevW = w;
215
+ prevH = h;
216
+ outgoing = scratchImage;
217
+ } while (w != targetWidth || h != targetHeight);
218
+ g2.dispose();
219
+
220
+
221
+ // If we used a scratch buffer that is larger than our target size,
222
+ // create an image of the right size and copy the results into it
223
+ if (targetWidth != outgoing.getWidth() ||
224
+ targetHeight != outgoing.getHeight()) {
225
+ scratchImage = new BufferedImage(targetWidth, targetHeight, type);
226
+ g2 = scratchImage.createGraphics();
227
+ g2.drawImage(outgoing, 0, 0, null);
228
+ g2.dispose();
229
+ outgoing = scratchImage;
230
+ }
231
+ return outgoing;
232
+ }
233
+
234
+
235
+ @Override
236
+ protected boolean saveImpl(String filename) {
237
+ if (saveImageFormats == null) {
238
+ saveImageFormats = javax.imageio.ImageIO.getWriterFormatNames();
239
+ }
240
+ try {
241
+ if (saveImageFormats != null) {
242
+ for (String saveImageFormat : saveImageFormats) {
243
+ if (filename.endsWith("." + saveImageFormat)) {
244
+ if (!saveImageIO(filename)) {
245
+ System.err.println("Error while saving image.");
246
+ return false;
247
+ }
248
+ return true;
249
+ }
250
+ }
251
+ }
252
+ } catch (IOException e) {
253
+ }
254
+ return false;
255
+ }
256
+
257
+
258
+ protected String[] saveImageFormats;
259
+
260
+
261
+ /**
262
+ * Use ImageIO functions from Java 1.4 and later to handle image save.
263
+ * Various formats are supported, typically jpeg, png, bmp, and wbmp.
264
+ * To get a list of the supported formats for writing, use: <BR>
265
+ * <TT>println(javax.imageio.ImageIO.getReaderFormatNames())</TT>
266
+ *
267
+ * @path The path to which the file should be written.
268
+ */
269
+ protected boolean saveImageIO(String path) throws IOException {
270
+ try {
271
+ int outputFormat = (format == ARGB) ?
272
+ BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
273
+
274
+ String extension =
275
+ path.substring(path.lastIndexOf('.') + 1).toLowerCase();
276
+
277
+ // JPEG and BMP images that have an alpha channel set get pretty unhappy.
278
+ // BMP just doesn't write, and JPEG writes it as a CMYK image.
279
+ // http://code.google.com/p/processing/issues/detail?id=415
280
+ if (extension.equals("bmp") || extension.equals("jpg") || extension.equals("jpeg")) {
281
+ outputFormat = BufferedImage.TYPE_INT_RGB;
282
+ }
283
+
284
+ BufferedImage bimage = new BufferedImage(pixelWidth, pixelHeight, outputFormat);
285
+ bimage.setRGB(0, 0, pixelWidth, pixelHeight, pixels, 0, pixelWidth);
286
+
287
+ File file = new File(path);
288
+
289
+ ImageWriter writer = null;
290
+ ImageWriteParam param = null;
291
+ IIOMetadata metadata = null;
292
+
293
+ if (extension.equals("jpg") || extension.equals("jpeg")) {
294
+ if ((writer = imageioWriter("jpeg")) != null) {
295
+ // Set JPEG quality to 90% with baseline optimization. Setting this
296
+ // to 1 was a huge jump (about triple the size), so this seems good.
297
+ // Oddly, a smaller file size than Photoshop at 90%, but I suppose
298
+ // it's a completely different algorithm.
299
+ param = writer.getDefaultWriteParam();
300
+ param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
301
+ param.setCompressionQuality(0.9f);
302
+ }
303
+ }
304
+
305
+ if (extension.equals("png")) {
306
+ if ((writer = imageioWriter("png")) != null) {
307
+ param = writer.getDefaultWriteParam();
308
+ if (false) {
309
+ metadata = imageioDPI(writer, param, 100);
310
+ }
311
+ }
312
+ }
313
+
314
+ if (writer != null) {
315
+ try (BufferedOutputStream output = new BufferedOutputStream(PApplet.createOutput(file))) {
316
+ writer.setOutput(ImageIO.createImageOutputStream(output));
317
+ // writer.write(null, new IIOImage(bimage, null, null), param);
318
+ writer.write(metadata, new IIOImage(bimage, null, metadata), param);
319
+ writer.dispose();
320
+
321
+ output.flush();
322
+ }
323
+ return true;
324
+ }
325
+ // If iter.hasNext() somehow fails up top, it falls through to here
326
+ return javax.imageio.ImageIO.write(bimage, extension, file);
327
+
328
+ } catch (IOException e) {
329
+ throw new IOException("image save failed.");
330
+ }
331
+ }
332
+
333
+
334
+ private ImageWriter imageioWriter(String extension) {
335
+ Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(extension);
336
+ if (iter.hasNext()) {
337
+ return iter.next();
338
+ }
339
+ return null;
340
+ }
341
+
342
+
343
+ private IIOMetadata imageioDPI(ImageWriter writer, ImageWriteParam param, double dpi) {
344
+ // http://stackoverflow.com/questions/321736/how-to-set-dpi-information-in-an-image
345
+ ImageTypeSpecifier typeSpecifier =
346
+ ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
347
+ IIOMetadata metadata =
348
+ writer.getDefaultImageMetadata(typeSpecifier, param);
349
+
350
+ if (!metadata.isReadOnly() && metadata.isStandardMetadataFormatSupported()) {
351
+ // for PNG, it's dots per millimeter
352
+ double dotsPerMilli = dpi / 25.4;
353
+
354
+ IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize");
355
+ horiz.setAttribute("value", Double.toString(dotsPerMilli));
356
+
357
+ IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize");
358
+ vert.setAttribute("value", Double.toString(dotsPerMilli));
359
+
360
+ IIOMetadataNode dim = new IIOMetadataNode("Dimension");
361
+ dim.appendChild(horiz);
362
+ dim.appendChild(vert);
363
+
364
+ IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
365
+ root.appendChild(dim);
366
+
367
+ try {
368
+ metadata.mergeTree("javax_imageio_1.0", root);
369
+ return metadata;
370
+
371
+ } catch (IIOInvalidTreeException e) {
372
+ System.err.println("Could not set the DPI of the output image");
373
+ }
374
+ }
375
+ return null;
376
+ }
377
+ }
@@ -1,3 +1,5 @@
1
+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
+
1
3
  /*
2
4
  Part of the Processing project - http://processing.org
3
5
 
@@ -16,7 +18,8 @@
16
18
  Public License along with this library; if not, write to the
17
19
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18
20
  Boston, MA 02111-1307 USA
19
- */
21
+ */
22
+
20
23
  package processing.awt;
21
24
 
22
25
  import java.awt.Paint;
@@ -35,23 +38,26 @@ import processing.core.PGraphics;
35
38
  import processing.core.PShapeSVG;
36
39
  import processing.data.*;
37
40
 
41
+
38
42
  /**
39
- * Implements features for PShape that are specific to AWT and Java2D. At the
40
- * moment, this is gradients and java.awt.Paint handling.
43
+ * Implements features for PShape that are specific to AWT and Java2D.
44
+ * At the moment, this is gradients and java.awt.Paint handling.
41
45
  */
42
46
  public class PShapeJava2D extends PShapeSVG {
43
-
44
47
  Paint strokeGradientPaint;
45
48
  Paint fillGradientPaint;
46
49
 
50
+
47
51
  public PShapeJava2D(XML svg) {
48
52
  super(svg);
49
53
  }
50
54
 
55
+
51
56
  public PShapeJava2D(PShapeSVG parent, XML properties, boolean parseKids) {
52
57
  super(parent, properties, parseKids);
53
58
  }
54
59
 
60
+
55
61
  @Override
56
62
  protected void setParent(PShapeSVG parent) {
57
63
  super.setParent(parent);
@@ -67,9 +73,8 @@ public class PShapeJava2D extends PShapeSVG {
67
73
  }
68
74
  }
69
75
 
70
- /**
71
- * Factory method for subclasses.
72
- */
76
+
77
+ /** Factory method for subclasses. */
73
78
  @Override
74
79
  protected PShapeSVG createShape(PShapeSVG parent, XML properties, boolean parseKids) {
75
80
  return new PShapeJava2D(parent, properties, parseKids);
@@ -88,9 +93,10 @@ public class PShapeJava2D extends PShapeSVG {
88
93
  strokeGradientPaint = calcGradientPaint(strokeGradient);
89
94
  }
90
95
  }
91
- */
92
- static class LinearGradientPaint implements Paint {
96
+ */
93
97
 
98
+
99
+ static class LinearGradientPaint implements Paint {
94
100
  float x1, y1, x2, y2;
95
101
  float[] offset;
96
102
  int[] color;
@@ -98,8 +104,8 @@ public class PShapeJava2D extends PShapeSVG {
98
104
  float opacity;
99
105
 
100
106
  public LinearGradientPaint(float x1, float y1, float x2, float y2,
101
- float[] offset, int[] color, int count,
102
- float opacity) {
107
+ float[] offset, int[] color, int count,
108
+ float opacity) {
103
109
  this.x1 = x1;
104
110
  this.y1 = y1;
105
111
  this.x2 = x2;
@@ -110,21 +116,22 @@ public class PShapeJava2D extends PShapeSVG {
110
116
  this.opacity = opacity;
111
117
  }
112
118
 
119
+ @Override
113
120
  public PaintContext createContext(ColorModel cm,
114
- Rectangle deviceBounds, Rectangle2D userBounds,
115
- AffineTransform xform, RenderingHints hints) {
121
+ Rectangle deviceBounds, Rectangle2D userBounds,
122
+ AffineTransform xform, RenderingHints hints) {
116
123
  Point2D t1 = xform.transform(new Point2D.Float(x1, y1), null);
117
124
  Point2D t2 = xform.transform(new Point2D.Float(x2, y2), null);
118
125
  return new LinearGradientContext((float) t1.getX(), (float) t1.getY(),
119
- (float) t2.getX(), (float) t2.getY());
126
+ (float) t2.getX(), (float) t2.getY());
120
127
  }
121
128
 
129
+ @Override
122
130
  public int getTransparency() {
123
131
  return TRANSLUCENT; // why not.. rather than checking each color
124
132
  }
125
133
 
126
134
  public class LinearGradientContext implements PaintContext {
127
-
128
135
  int ACCURACY = 2;
129
136
  float tx1, ty1, tx2, ty2;
130
137
 
@@ -136,25 +143,22 @@ public class PShapeJava2D extends PShapeSVG {
136
143
  }
137
144
 
138
145
  @Override
139
- public void dispose() {
140
- }
146
+ public void dispose() { }
141
147
 
142
148
  @Override
143
- public ColorModel getColorModel() {
144
- return ColorModel.getRGBdefault();
145
- }
149
+ public ColorModel getColorModel() { return ColorModel.getRGBdefault(); }
146
150
 
147
151
  @Override
148
152
  public Raster getRaster(int x, int y, int w, int h) {
149
- WritableRaster raster
150
- = getColorModel().createCompatibleWritableRaster(w, h);
153
+ WritableRaster raster =
154
+ getColorModel().createCompatibleWritableRaster(w, h);
151
155
 
152
156
  int[] data = new int[w * h * 4];
153
157
 
154
158
  // make normalized version of base vector
155
159
  float nx = tx2 - tx1;
156
160
  float ny = ty2 - ty1;
157
- float len = (float) Math.sqrt(nx * nx + ny * ny);
161
+ float len = (float) Math.sqrt(nx*nx + ny*ny);
158
162
  if (len != 0) {
159
163
  nx /= len;
160
164
  ny /= len;
@@ -178,9 +182,9 @@ public class PShapeJava2D extends PShapeSVG {
178
182
  int[][] interp = new int[span][4];
179
183
  int prev = 0;
180
184
  for (int i = 1; i < count; i++) {
181
- int c0 = color[i - 1];
185
+ int c0 = color[i-1];
182
186
  int c1 = color[i];
183
- int last = (int) (offset[i] * (span - 1));
187
+ int last = (int) (offset[i] * (span-1));
184
188
  //System.out.println("last is " + last);
185
189
  for (int j = prev; j <= last; j++) {
186
190
  float btwn = PApplet.norm(j, prev, last);
@@ -202,13 +206,9 @@ public class PShapeJava2D extends PShapeSVG {
202
206
  float py = (y + j) - ty1;
203
207
  // distance up the line is the dot product of the normalized
204
208
  // vector of the gradient start/stop by the point being tested
205
- int which = (int) ((px * nx + py * ny) * ACCURACY);
206
- if (which < 0) {
207
- which = 0;
208
- }
209
- if (which > interp.length - 1) {
210
- which = interp.length - 1;
211
- }
209
+ int which = (int) ((px*nx + py*ny) * ACCURACY);
210
+ if (which < 0) which = 0;
211
+ if (which > interp.length-1) which = interp.length-1;
212
212
  //if (which > 138) System.out.println("grabbing " + which);
213
213
 
214
214
  data[index++] = interp[which][0];
@@ -225,8 +225,8 @@ public class PShapeJava2D extends PShapeSVG {
225
225
  }
226
226
  }
227
227
 
228
- static class RadialGradientPaint implements Paint {
229
228
 
229
+ static class RadialGradientPaint implements Paint {
230
230
  float cx, cy, radius;
231
231
  float[] offset;
232
232
  int[] color;
@@ -234,8 +234,8 @@ public class PShapeJava2D extends PShapeSVG {
234
234
  float opacity;
235
235
 
236
236
  public RadialGradientPaint(float cx, float cy, float radius,
237
- float[] offset, int[] color, int count,
238
- float opacity) {
237
+ float[] offset, int[] color, int count,
238
+ float opacity) {
239
239
  this.cx = cx;
240
240
  this.cy = cy;
241
241
  this.radius = radius;
@@ -247,8 +247,8 @@ public class PShapeJava2D extends PShapeSVG {
247
247
 
248
248
  @Override
249
249
  public PaintContext createContext(ColorModel cm,
250
- Rectangle deviceBounds, Rectangle2D userBounds,
251
- AffineTransform xform, RenderingHints hints) {
250
+ Rectangle deviceBounds, Rectangle2D userBounds,
251
+ AffineTransform xform, RenderingHints hints) {
252
252
  return new RadialGradientContext();
253
253
  }
254
254
 
@@ -258,28 +258,24 @@ public class PShapeJava2D extends PShapeSVG {
258
258
  }
259
259
 
260
260
  public class RadialGradientContext implements PaintContext {
261
-
262
261
  int ACCURACY = 5;
263
262
 
264
263
  @Override
265
- public void dispose() {
266
- }
264
+ public void dispose() {}
267
265
 
268
266
  @Override
269
- public ColorModel getColorModel() {
270
- return ColorModel.getRGBdefault();
271
- }
267
+ public ColorModel getColorModel() { return ColorModel.getRGBdefault(); }
272
268
 
273
269
  @Override
274
270
  public Raster getRaster(int x, int y, int w, int h) {
275
- WritableRaster raster
276
- = getColorModel().createCompatibleWritableRaster(w, h);
271
+ WritableRaster raster =
272
+ getColorModel().createCompatibleWritableRaster(w, h);
277
273
 
278
274
  int span = (int) radius * ACCURACY;
279
275
  int[][] interp = new int[span][4];
280
276
  int prev = 0;
281
277
  for (int i = 1; i < count; i++) {
282
- int c0 = color[i - 1];
278
+ int c0 = color[i-1];
283
279
  int c1 = color[i];
284
280
  int last = (int) (offset[i] * (span - 1));
285
281
  for (int j = prev; j <= last; j++) {
@@ -297,7 +293,7 @@ public class PShapeJava2D extends PShapeSVG {
297
293
  for (int j = 0; j < h; j++) {
298
294
  for (int i = 0; i < w; i++) {
299
295
  float distance = PApplet.dist(cx, cy, x + i, y + j);
300
- int which = PApplet.min((int) (distance * ACCURACY), interp.length - 1);
296
+ int which = PApplet.min((int) (distance * ACCURACY), interp.length-1);
301
297
 
302
298
  data[index++] = interp[which][0];
303
299
  data[index++] = interp[which][1];
@@ -312,24 +308,26 @@ public class PShapeJava2D extends PShapeSVG {
312
308
  }
313
309
  }
314
310
 
311
+
315
312
  protected Paint calcGradientPaint(Gradient gradient) {
316
313
  if (gradient instanceof LinearGradient) {
317
314
  // System.out.println("creating linear gradient");
318
315
  LinearGradient grad = (LinearGradient) gradient;
319
316
  return new LinearGradientPaint(grad.x1, grad.y1, grad.x2, grad.y2,
320
- grad.offset, grad.color, grad.count,
321
- opacity);
317
+ grad.offset, grad.color, grad.count,
318
+ opacity);
322
319
 
323
320
  } else if (gradient instanceof RadialGradient) {
324
321
  // System.out.println("creating radial gradient");
325
322
  RadialGradient grad = (RadialGradient) gradient;
326
323
  return new RadialGradientPaint(grad.cx, grad.cy, grad.r,
327
- grad.offset, grad.color, grad.count,
328
- opacity);
324
+ grad.offset, grad.color, grad.count,
325
+ opacity);
329
326
  }
330
327
  return null;
331
328
  }
332
329
 
330
+
333
331
  // protected Paint calcGradientPaint(Gradient gradient,
334
332
  // float x1, float y1, float x2, float y2) {
335
333
  // if (gradient instanceof LinearGradient) {
@@ -340,6 +338,8 @@ public class PShapeJava2D extends PShapeSVG {
340
338
  // }
341
339
  // throw new RuntimeException("Not a linear gradient.");
342
340
  // }
341
+
342
+
343
343
  // protected Paint calcGradientPaint(Gradient gradient,
344
344
  // float cx, float cy, float r) {
345
345
  // if (gradient instanceof RadialGradient) {
@@ -350,7 +350,11 @@ public class PShapeJava2D extends PShapeSVG {
350
350
  // }
351
351
  // throw new RuntimeException("Not a radial gradient.");
352
352
  // }
353
+
354
+
353
355
  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
356
+
357
+
354
358
  @Override
355
359
  protected void styles(PGraphics g) {
356
360
  super.styles(g);
@@ -380,4 +384,4 @@ public class PShapeJava2D extends PShapeSVG {
380
384
  }
381
385
  //}
382
386
  }
383
- }
387
+ }