picrate 1.2.3-java → 2.1.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.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/Gemfile +3 -1
  4. data/README.md +8 -9
  5. data/Rakefile +10 -9
  6. data/bin/picrate +3 -1
  7. data/docs/_config.yml +1 -1
  8. data/docs/_editors/geany.md +1 -0
  9. data/docs/_gems/gems/gems.md +1 -1
  10. data/docs/_posts/2018-05-06-getting_started.md +4 -4
  11. data/docs/_posts/2018-05-06-install_jruby.md +5 -11
  12. data/docs/_posts/2018-05-11-arch-linux-arm.md +1 -11
  13. data/docs/_posts/2018-11-18-building-gem.md +2 -2
  14. data/docs/_posts/2018-11-27-getting_started_geany.md +4 -4
  15. data/docs/_posts/2019-11-11-getting_started_buster.md +4 -7
  16. data/docs/_posts/2020-03-09-auto_install_picrate.md +34 -0
  17. data/docs/_posts/2020-05-11-getting_started_manjaro.md +94 -0
  18. data/docs/about.md +1 -1
  19. data/lib/picrate.rb +1 -1
  20. data/lib/picrate/app.rb +11 -3
  21. data/lib/picrate/creators/parameters.rb +8 -8
  22. data/lib/picrate/creators/sketch_factory.rb +5 -3
  23. data/lib/picrate/helper_methods.rb +21 -21
  24. data/lib/picrate/helpers/numeric.rb +2 -0
  25. data/lib/picrate/library.rb +5 -1
  26. data/lib/picrate/library_loader.rb +2 -0
  27. data/lib/picrate/native_folder.rb +2 -1
  28. data/lib/picrate/native_loader.rb +3 -0
  29. data/lib/picrate/runner.rb +5 -2
  30. data/lib/picrate/version.rb +1 -1
  31. data/library/boids/boids.rb +17 -8
  32. data/library/chooser/chooser.rb +10 -9
  33. data/library/color_group/color_group.rb +2 -0
  34. data/library/control_panel/control_panel.rb +7 -4
  35. data/library/dxf/dxf.rb +2 -0
  36. data/library/library_proxy/library_proxy.rb +2 -0
  37. data/library/net/net.rb +2 -0
  38. data/library/slider/slider.rb +24 -23
  39. data/library/vector_utils/vector_utils.rb +4 -0
  40. data/library/video_event/video_event.rb +2 -0
  41. data/picrate.gemspec +13 -14
  42. data/pom.rb +28 -26
  43. data/pom.xml +18 -6
  44. data/src/main/java/monkstone/ColorUtil.java +1 -1
  45. data/src/main/java/monkstone/MathToolModule.java +1 -1
  46. data/src/main/java/monkstone/PicrateLibrary.java +8 -8
  47. data/src/main/java/monkstone/fastmath/Deglut.java +16 -16
  48. data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
  49. data/src/main/java/monkstone/noise/SimplexNoise.java +3 -3
  50. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +1 -1
  51. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
  52. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +12 -12
  53. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +1 -1
  54. data/src/main/java/monkstone/slider/SliderBar.java +1 -1
  55. data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
  56. data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
  57. data/src/main/java/monkstone/vecmath/package-info.java +1 -1
  58. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +1 -1
  59. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +1 -1
  60. data/src/main/java/monkstone/videoevent/CaptureEvent.java +1 -1
  61. data/src/main/java/monkstone/videoevent/MovieEvent.java +1 -1
  62. data/src/main/java/monkstone/videoevent/package-info.java +1 -1
  63. data/src/main/java/processing/awt/PGraphicsJava2D.java +33 -36
  64. data/src/main/java/processing/awt/PImageAWT.java +377 -0
  65. data/src/main/java/processing/awt/PSurfaceAWT.java +0 -20
  66. data/src/main/java/processing/awt/ShimAWT.java +545 -0
  67. data/src/main/java/processing/core/PApplet.java +699 -1523
  68. data/src/main/java/processing/core/PConstants.java +180 -180
  69. data/src/main/java/processing/core/PFont.java +2 -2
  70. data/src/main/java/processing/core/PGraphics.java +190 -176
  71. data/src/main/java/processing/core/PImage.java +1536 -1721
  72. data/src/main/java/processing/core/PMatrix.java +39 -39
  73. data/src/main/java/processing/core/PSurface.java +69 -103
  74. data/src/main/java/processing/core/PSurfaceNone.java +29 -0
  75. data/src/main/java/processing/core/PVector.java +2 -2
  76. data/src/main/java/processing/data/FloatDict.java +251 -284
  77. data/src/main/java/processing/data/TableRow.java +32 -32
  78. data/src/main/java/processing/dxf/RawDXF.java +3 -3
  79. data/src/main/java/processing/net/Client.java +1 -1
  80. data/src/main/java/processing/opengl/PGL.java +1016 -4132
  81. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +223 -184
  82. data/src/main/java/processing/opengl/PJOGL.java +374 -1526
  83. data/src/main/java/processing/opengl/PShapeOpenGL.java +5 -6
  84. data/src/main/java/processing/opengl/PSurfaceJOGL.java +262 -147
  85. data/test/color_group_test.rb +4 -4
  86. data/test/deglut_spec_test.rb +2 -0
  87. data/test/helper_methods_test.rb +41 -13
  88. data/test/math_tool_test.rb +46 -37
  89. data/test/respond_to_test.rb +5 -3
  90. data/test/sketches/key_event.rb +2 -2
  91. data/test/sketches/library/my_library/my_library.rb +3 -0
  92. data/test/test_helper.rb +2 -0
  93. data/test/vecmath_spec_test.rb +30 -19
  94. data/vendors/Rakefile +13 -7
  95. data/vendors/{picrate_sketches.geany → geany.rb} +32 -7
  96. metadata +26 -47
  97. data/docs/_posts/2018-06-26-auto_install_picrate.md +0 -30
  98. data/src/main/java/processing/opengl/shaders/LightVert-brcm.glsl +0 -154
  99. data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +0 -154
  100. data/src/main/java/processing/opengl/shaders/TexLightVert-brcm.glsl +0 -160
  101. data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +0 -160
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-18 Martin Prout
2
+ * Copyright (c) 2016-20 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU General Public
@@ -36,7 +36,7 @@ public class SimplexNoise { // Simplex noise in 2D, 3D and 4D
36
36
  new Grad(1, 1, 1, 0), new Grad(1, 1, -1, 0), new Grad(1, -1, 1, 0), new Grad(1, -1, -1, 0),
37
37
  new Grad(-1, 1, 1, 0), new Grad(-1, 1, -1, 0), new Grad(-1, -1, 1, 0), new Grad(-1, -1, -1, 0)};
38
38
 
39
- private static short p[] = {151, 160, 137, 91, 90, 15,
39
+ private final static short PERMS[] = {151, 160, 137, 91, 90, 15,
40
40
  131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
41
41
  190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33,
42
42
  88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
@@ -51,11 +51,11 @@ public class SimplexNoise { // Simplex noise in 2D, 3D and 4D
51
51
  138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180};
52
52
  // To remove the need for index wrapping, double the permutation table length
53
53
  static final short[] PERM = new short[512];
54
- static short[] PERM_MOD_12 = new short[512];
54
+ static final short[] PERM_MOD_12 = new short[512];
55
55
 
56
56
  static {
57
57
  for (int i = 0; i < 512; i++) {
58
- PERM[i] = p[i & 255];
58
+ PERM[i] = PERMS[i & 255];
59
59
  PERM_MOD_12[i] = (short) (PERM[i] % 12);
60
60
  }
61
61
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-18 Martin Prout
2
+ * Copyright (c) 2016-20 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU General Public
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-18 Martin Prout
2
+ * Copyright (c) 2016-20 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU General Public
@@ -1,18 +1,18 @@
1
- /*
2
- * Copyright (c) 2016-18 Martin Prout
3
- *
1
+ /*
2
+ * Copyright (c) 2016-20 Martin Prout
3
+ *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU General Public
6
6
  * License as published by the Free Software Foundation; either
7
7
  * version 3.0 of the License, or (at your option) any later version.
8
- *
8
+ *
9
9
  * http://creativecommons.org/licenses/LGPL/2.1/
10
- *
10
+ *
11
11
  * This library is distributed in the hope that it will be useful,
12
12
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
13
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
14
  * Lesser General Public License for more details.
15
- *
15
+ *
16
16
  * You should have received a copy of the GNU General Public
17
17
  * License along with this library; if not, write to the Free Software
18
18
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
@@ -27,10 +27,10 @@ import processing.core.PConstants;
27
27
  * @author Martin Prout
28
28
  */
29
29
  public class SimpleHorizontalSlider extends SimpleSlider {
30
-
30
+
31
31
  final int SPACING = 20;
32
32
  final int LEFT_SPC = SPACING * 2;
33
- final int RIGHT_SPC = SPACING * 4;
33
+ final int RIGHT_SPC = SPACING * 4;
34
34
 
35
35
  /**
36
36
  *
@@ -62,7 +62,7 @@ public class SimpleHorizontalSlider extends SimpleSlider {
62
62
  applet.registerMethod("dispose", this);
63
63
  applet.registerMethod("draw", this);
64
64
  } else {
65
- applet.unregisterMethod("draw", this);
65
+ applet.unregisterMethod("draw", this);
66
66
  }
67
67
  }
68
68
 
@@ -84,7 +84,7 @@ public class SimpleHorizontalSlider extends SimpleSlider {
84
84
  applet.text(String.format(lFormat, (int) vMax), pX + pW, pY );
85
85
  }
86
86
  }
87
-
87
+
88
88
  @Override
89
89
  void drawGui() {
90
90
  if (backgroundVisible) {
@@ -94,7 +94,7 @@ public class SimpleHorizontalSlider extends SimpleSlider {
94
94
  applet.noStroke();
95
95
  applet.fill(255);
96
96
  applet.ellipse(pX + pScaled, pY + pH / 2, 10, 10);
97
- }
97
+ }
98
98
 
99
99
  /**
100
100
  *
@@ -136,7 +136,7 @@ public class SimpleHorizontalSlider extends SimpleSlider {
136
136
  pScaled = map(pValue, vMin, vMax, 0, pW);
137
137
  }
138
138
  }
139
-
139
+
140
140
  /**
141
141
  *
142
142
  * @return
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-18 Martin Prout
2
+ * Copyright (c) 2016-20 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU General Public
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-18 Martin Prout
2
+ * Copyright (c) 2016-20 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU General Public
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-18 Martin Prout
2
+ * Copyright (c) 2016-20 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU General Public
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-18 Martin Prout
2
+ * Copyright (c) 2016-20 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU General Public
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2018-19 Martin Prout
2
+ * Copyright (c) 2018-20 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU General Public
@@ -1,7 +1,7 @@
1
1
  package monkstone.vecmath.vec2;
2
2
 
3
3
  /*
4
- * Copyright (c) 2015-18 Martin Prout
4
+ * Copyright (c) 2015-20 Martin Prout
5
5
  *
6
6
  * This library is free software; you can redistribute it and/or
7
7
  * modify it under the terms of the GNU Lesser General Public
@@ -1,7 +1,7 @@
1
1
  package monkstone.vecmath.vec3;
2
2
 
3
3
  /*
4
- * Copyright (c) 2018-19 Martin Prout
4
+ * Copyright (c) 2018-20 Martin Prout
5
5
  *
6
6
  * This library is free software; you can redistribute it and/or
7
7
  * modify it under the terms of the GNU General Public
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2015-19 Martin Prout
2
+ * Copyright (c) 2015-20 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU Lesser General Public
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2015-19 Martin Prout
2
+ * Copyright (c) 2015-20 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU Lesser General Public
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2018-19 Martin Prout
2
+ * Copyright (c) 2018-20 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU General Public
@@ -29,7 +29,6 @@ import java.awt.font.TextAttribute;
29
29
  import java.awt.geom.*;
30
30
  import java.awt.image.*;
31
31
  import java.util.Arrays;
32
- import java.util.HashMap;
33
32
  import java.util.Map;
34
33
 
35
34
  import processing.core.*;
@@ -86,7 +85,7 @@ public class PGraphicsJava2D extends PGraphics {
86
85
  float[] curveDrawY;
87
86
 
88
87
  int transformCount;
89
- AffineTransform transformStack[] =
88
+ AffineTransform[] transformStack =
90
89
  new AffineTransform[MATRIX_STACK_DEPTH];
91
90
  double[] transform = new double[6];
92
91
 
@@ -295,10 +294,10 @@ public class PGraphicsJava2D extends PGraphics {
295
294
  * is going to return the {@link Graphics2D} object.
296
295
  * @return
297
296
  */
298
- @Override
299
- public Image getImage() {
300
- return image;
301
- }
297
+ // @Override
298
+ // public Image getImage() {
299
+ // return image;
300
+ // }
302
301
 
303
302
 
304
303
  /** Returns the java.awt.Graphics2D object used by this renderer.
@@ -346,30 +345,30 @@ public class PGraphicsJava2D extends PGraphics {
346
345
  // image = new BufferedImage(width * pixelFactor, height * pixelFactor
347
346
  // format == RGB ? BufferedImage.TYPE_INT_ARGB);
348
347
 
349
- GraphicsConfiguration gc = null;
350
- if (surface != null) {
351
- Component comp = null; //surface.getComponent();
352
- if (comp == null) {
353
- // System.out.println("component null, but parent.frame is " + parent.frame);
354
- comp = parent.frame;
355
- }
356
- if (comp != null) {
357
- gc = comp.getGraphicsConfiguration();
358
- }
359
- }
360
- // If not realized (off-screen, i.e the Color Selector Tool), gc will be null.
361
- if (gc == null) {
362
- //System.err.println("GraphicsConfiguration null in initImage()");
363
- GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
364
- gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
365
- }
348
+ // GraphicsConfiguration gc = null;
349
+ // if (surface != null) {
350
+ // Component comp = null; //surface.getComponent();
351
+ // if (comp == null) {
352
+ //// System.out.println("component null, but parent.frame is " + parent.frame);
353
+ // comp = parent.frame;
354
+ // }
355
+ // if (comp != null) {
356
+ // gc = comp.getGraphicsConfiguration();
357
+ // }
358
+ // }
359
+ // // If not realized (off-screen, i.e the Color Selector Tool), gc will be null.
360
+ // if (gc == null) {
361
+ // //System.err.println("GraphicsConfiguration null in initImage()");
362
+ // GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
363
+ // gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
364
+ // }
366
365
 
367
366
  // Formerly this was broken into separate versions based on offscreen or
368
367
  // not, but we may as well create a compatible image; it won't hurt, right?
369
368
  int wide = width * pixelDensity;
370
369
  int high = height * pixelDensity;
371
370
  // System.out.println("re-creating image");
372
- image = gc.createCompatibleImage(wide, high, Transparency.TRANSLUCENT);
371
+ image = new BufferedImage(wide, high, BufferedImage.TYPE_INT_ARGB);
373
372
  // image = gc.createCompatibleVolatileImage(wide, high);
374
373
  //image = surface.getComponent().createImage(width, height);
375
374
  }
@@ -817,7 +816,7 @@ public class PGraphicsJava2D extends PGraphics {
817
816
  //float vertex[];
818
817
 
819
818
  if (vertexCount == vertices.length) {
820
- float temp[][] = new float[vertexCount<<1][VERTEX_FIELD_COUNT];
819
+ float[][] temp = new float[vertexCount<<1][VERTEX_FIELD_COUNT];
821
820
  System.arraycopy(vertices, 0, temp, 0, vertexCount);
822
821
  vertices = temp;
823
822
  //message(CHATTER, "allocating more vertices " + vertices.length);
@@ -1969,13 +1968,11 @@ public class PGraphicsJava2D extends PGraphics {
1969
1968
  // don't derive again if the font size has not changed
1970
1969
  if (font != null) {
1971
1970
  if (font.getSize2D() != size) {
1972
- Map<TextAttribute, Object> map =
1973
- new HashMap<>();
1974
- map.put(TextAttribute.SIZE, size);
1975
- map.put(TextAttribute.KERNING,
1976
- TextAttribute.KERNING_ON);
1977
- // map.put(TextAttribute.TRACKING,
1978
- // TextAttribute.TRACKING_TIGHT);
1971
+ Map<TextAttribute, Object> map = Map.of(
1972
+ TextAttribute.SIZE, size,
1973
+ TextAttribute.KERNING, TextAttribute.KERNING_ON
1974
+ );
1975
+ // TextAttribute.TRACKING, TextAttribute.TRACKING_TIGHT);
1979
1976
  font = font.deriveFont(map);
1980
1977
  }
1981
1978
  g2.setFont(font);
@@ -2004,7 +2001,7 @@ public class PGraphicsJava2D extends PGraphics {
2004
2001
 
2005
2002
 
2006
2003
  @Override
2007
- protected float textWidthImpl(char buffer[], int start, int stop) {
2004
+ protected float textWidthImpl(char[] buffer, int start, int stop) {
2008
2005
  if (textFont == null) {
2009
2006
  defaultFontOrDeath("textWidth");
2010
2007
  }
@@ -2068,12 +2065,12 @@ public class PGraphicsJava2D extends PGraphics {
2068
2065
  // TEXT IMPL
2069
2066
 
2070
2067
 
2071
- //protected void textLineAlignImpl(char buffer[], int start, int stop,
2068
+ //protected void textLineAlignImpl(char[] buffer, int start, int stop,
2072
2069
  // float x, float y)
2073
2070
 
2074
2071
 
2075
2072
  @Override
2076
- protected void textLineImpl(char buffer[], int start, int stop,
2073
+ protected void textLineImpl(char[] buffer, int start, int stop,
2077
2074
  float x, float y) {
2078
2075
  Font font = (Font) textFont.getNative();
2079
2076
  // if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
@@ -2886,7 +2883,7 @@ public class PGraphicsJava2D extends PGraphics {
2886
2883
  // GET/SET
2887
2884
 
2888
2885
 
2889
- static int getset[] = new int[1];
2886
+ static int[] getset = new int[1];
2890
2887
 
2891
2888
 
2892
2889
  @Override
@@ -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
+ }