Permalink
Browse files

Add filter() builtin/Processing pun.

Indicate the current sketch firectory via a floag, rather than the user.dir environment variable.
Set this "finished" flag when exit()ing so that OpenGL resources get released.
Temporarily switching to jdf/processing to get a bugfix into PGraphicsOpenGL.
  • Loading branch information...
1 parent 450c669 commit 34eb45560f9e3869e8d480c046da834b650a3558 @jdf committed Dec 27, 2013
View
@@ -1,5 +1,5 @@
[submodule "externals/processing"]
path = externals/processing
- url = git@github.com:processing/processing.git
+ url = git@github.com:jdf/processing.git
ignore = dirty
View
@@ -367,7 +367,7 @@
<target name="build-tests" depends="jar">
<mkdir dir="testing/bin"/>
<javac classpath="testing/library/junit-4.11.jar:processing-py.jar" destdir="testing/bin"
- includeantruntime="false">
+ includeantruntime="false" deprecation="true">
<src path="testing/src"/>
</javac>
</target>
Submodule processing updated 120 files
@@ -44,6 +44,7 @@
import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PImage;
+import processing.opengl.PShader;
import fisica.FContact;
import fisica.FContactResult;
@@ -114,6 +115,7 @@ public PAppletJythonDriver(final InteractiveConsole interp, final String sketchP
this.builtins = (PyStringMap)interp.getSystemState().getBuiltins();
this.interp = interp;
initializeStatics(builtins);
+ setFilter();
setMap();
setSet();
builtins.__setitem__("g", Py.java2py(g));
@@ -336,6 +338,8 @@ public void init() {
public void await() throws InterruptedException {
finishedLatch.await();
+ // Cause PApplet to dispose OpenGL and other resources:
+ finished = true;
}
/**
@@ -382,10 +386,8 @@ public PyObject __call__(final PyObject[] args, final String[] kws) {
}
/**
- * Permit the punning use of set() by mucking with the builtin "set" Type.
- * If you call it with 3 arguments, it acts like the Processing set(x, y,
- * whatever) method. If you call it with 0 or 1 args, it constructs a Python
- * set.
+ * Permit both the Processing map() (which is a linear interpolation function) and
+ * the Python map() (which is a list transformation).
*/
private void setMap() {
final PyObject builtinMap = builtins.__getitem__("map");
@@ -416,6 +418,39 @@ public PyObject __call__(final PyObject[] args, final String[] kws) {
}
/**
+ * Permit both the Processing filter() (which does image processing) and the
+ * Python filter() (which does list comprehensions).
+ */
+ private void setFilter() {
+ final PyObject builtinFilter = builtins.__getitem__("filter");
+ builtins.__setitem__("filter", new PyObject() {
+ @Override
+ public PyObject __call__(final PyObject[] args, final String[] kws) {
+ switch (args.length) {
+ case 1:
+ final PyObject value = args[0];
+ if (value.isNumberType()) {
+ filter(value.asInt());
+ } else {
+ filter(Py.tojava(value, PShader.class));
+ }
+ return Py.None;
+ case 2:
+ final PyObject a = args[0];
+ final PyObject b = args[1];
+ if (a.isNumberType()) {
+ filter(a.asInt(), (float)b.asDouble());
+ return Py.None;
+ }
+ //$FALL-THROUGH$
+ default:
+ return builtinFilter.__call__(args, kws);
+ }
+ }
+ });
+ }
+
+ /**
* Populate the Python builtins namespace with PConstants.
*/
public static void initializeStatics(final PyStringMap builtins) {
@@ -15,14 +15,6 @@
*/
package jycessing;
-import org.python.core.Py;
-import org.python.core.PyString;
-import org.python.util.InteractiveConsole;
-import org.python.util.PythonInterpreter;
-
-import processing.core.PApplet;
-import processing.core.PConstants;
-
import java.awt.SplashScreen;
import java.awt.Window;
import java.io.BufferedReader;
@@ -54,6 +46,14 @@
import jycessing.annotations.PythonUsage;
import jycessing.launcher.LaunchHelper;
+import org.python.core.Py;
+import org.python.core.PyString;
+import org.python.util.InteractiveConsole;
+import org.python.util.PythonInterpreter;
+
+import processing.core.PApplet;
+import processing.core.PConstants;
+
public class Runner {
private static final String ARCH;
@@ -272,11 +272,6 @@ public static void runFromCommandLineArguments(final String[] args) throws Excep
sketchPath = new File(getRuntimeRoot(), "Runtime/sketch.py").getAbsolutePath();
}
- // Sanity check in case parameter order is wrong
- if (sketchPath.startsWith("--")) {
- throw new RuntimeException("The last parameter MUST be the script to execute, not an option!");
- }
-
// Debug when using launcher
if (Arrays.asList(args).contains("--redirect")) {
@@ -371,10 +366,6 @@ public static void runSketch(final String[] args, final String sketchPath,
// tests
interp.setOut(System.out);
- // Tell PApplet to make its home there, so that it can find the data
- // folder
- System.setProperty("user.dir", sketchDir);
-
// Add it to the Python library path for auxilliary modules
Py.getSystemState().path.insert(0, new PyString(sketchDir));
@@ -410,9 +401,14 @@ public static void runSketch(final String[] args, final String sketchPath,
splash.close();
}
+ // Tell the applet where to load and save data files, etc.
+ final String[] massagedArgs = new String[args.length + 1];
+ System.arraycopy(args, 0, massagedArgs, 0, args.length);
+ massagedArgs[args.length] =
+ PApplet.ARGS_SKETCH_FOLDER + "=" + new File(sketchPath).getCanonicalFile().getParent();
+
try {
- log("Running " + args[0]);
- PApplet.runSketch(args, applet);
+ PApplet.runSketch(massagedArgs, applet);
applet.await();
log("Applet terminated.");
log("Disposing window.");
@@ -360,11 +360,4 @@ def __mul__(a, b):
__builtin__.unhex = PApplet.unhex
__builtin__.year = PApplet.year
-# Extra helper functions go here
-def pwd(name = ""):
- """Returns the script's path, or the path for some data close to the script."""
- import java.lang.System as System
- return System.getProperty("python.main.root") + "/" + name
-__builtin__.pwd = pwd
-
del monkeypatch_method, PAppletJythonDriver
@@ -0,0 +1,34 @@
+#ifdef GL_ES
+precision mediump float;
+precision mediump int;
+#endif
+
+#define PROCESSING_TEXTURE_SHADER
+
+uniform sampler2D texture;
+uniform vec2 texOffset;
+
+varying vec4 vertColor;
+varying vec4 vertTexCoord;
+
+const vec4 lumcoeff = vec4(0.299, 0.587, 0.114, 0);
+
+void main() {
+ vec2 tc0 = vertTexCoord.st + vec2(-texOffset.s, -texOffset.t);
+ vec2 tc1 = vertTexCoord.st + vec2( 0.0, -texOffset.t);
+ vec2 tc2 = vertTexCoord.st + vec2(-texOffset.s, 0.0);
+ vec2 tc3 = vertTexCoord.st + vec2(+texOffset.s, 0.0);
+ vec2 tc4 = vertTexCoord.st + vec2( 0.0, +texOffset.t);
+ vec2 tc5 = vertTexCoord.st + vec2(+texOffset.s, +texOffset.t);
+
+ vec4 col0 = texture2D(texture, tc0);
+ vec4 col1 = texture2D(texture, tc1);
+ vec4 col2 = texture2D(texture, tc2);
+ vec4 col3 = texture2D(texture, tc3);
+ vec4 col4 = texture2D(texture, tc4);
+ vec4 col5 = texture2D(texture, tc5);
+
+ vec4 sum = vec4(0.5) + (col0 + col1 + col2) - (col3 + col4 + col5);
+ float lum = dot(sum, lumcoeff);
+ gl_FragColor = vec4(lum, lum, lum, 1.0) * vertColor;
+}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,27 @@
+def setup():
+ size(48, 48, P2D)
+ global img, emboss
+ img = loadImage("data/python.png")
+ emboss = loadShader("data/emboss.glsl")
+
+def draw():
+ global img, emboss
+ # Processing builtins
+ # filter(PShader)
+ image(img, 0, 0)
+ filter(emboss)
+
+ # filter(kind)
+ filter(BLUR)
+
+ # filter(kind, param)
+ filter(POSTERIZE, 4)
+
+ # Python builtin
+ a = filter(lambda x: x == 'banana',
+ ['apple', 'grape', 'banana', 'banana'])
+ assert a == ['banana', 'banana']
+
+ print 'OK'
+
+ exit()
@@ -1,10 +1,4 @@
-# To change this template, choose Tools | Templates
-# and open the template in the editor.
-
-__author__="max.franks"
-
-font = loadFont(pwd("data/Cabal1-48.vlw"))
-
+font = loadFont("data/Cabal1-48.vlw")
def setup():
size(10, 10, P3D)
noLoop();
@@ -1,4 +1,3 @@
-import urllib2
# Processing builtin
# map(value, low1, high1, low2, high2)
print int(map(5, 0, 10, 0, 100))
@@ -1,19 +1,21 @@
package test.jycessing;
-import static junit.framework.Assert.assertEquals;
-
-import org.junit.Test;
-import jycessing.Runner;
+import static org.junit.Assert.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
+import jycessing.Runner;
+
+import org.junit.Test;
+
public class JycessingTests {
private static String run(final String testResource) throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintStream saved = System.out;
try {
+ System.err.println("Running " + testResource + " test.");
System.setOut(new PrintStream(baos, true));
Runner.runFromCommandLineArguments(new String[] { "testing/resources/test_" + testResource
+ ".py" });
@@ -28,6 +30,7 @@ private static void testImport(final String module) throws Exception {
final PrintStream saved = System.out;
try {
System.setOut(new PrintStream(baos, true));
+ System.err.println("Running import " + module + " test.");
final String testClass = module + "_test";
final String bogusFileName = "<test " + module + ">";
final String testText = "import " + module + "\nprint 'OK'\nexit()";
@@ -50,6 +53,11 @@ public void static_size() throws Exception {
}
@Test
+ public void filter_builtins() throws Exception {
+ assertEquals("OK\n", run("filter"));
+ }
+
+ @Test
public void set_builtins() throws Exception {
assertEquals("128\nset(['banana'])\nissubclass: True\nMySet(['baz'])\n", run("set"));
}

0 comments on commit 34eb455

Please sign in to comment.