Skip to content

Commit 34eb455

Browse files
author
Jonathan Feinberg
committed
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.
1 parent 450c669 commit 34eb455

File tree

12 files changed

+131
-45
lines changed

12 files changed

+131
-45
lines changed

Diff for: .gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[submodule "externals/processing"]
22
path = externals/processing
3-
url = [email protected]:processing/processing.git
3+
url = [email protected]:jdf/processing.git
44
ignore = dirty
55

Diff for: build.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@
367367
<target name="build-tests" depends="jar">
368368
<mkdir dir="testing/bin"/>
369369
<javac classpath="testing/library/junit-4.11.jar:processing-py.jar" destdir="testing/bin"
370-
includeantruntime="false">
370+
includeantruntime="false" deprecation="true">
371371
<src path="testing/src"/>
372372
</javac>
373373
</target>

Diff for: externals/processing

Submodule processing updated 120 files

Diff for: runtime/src/jycessing/PAppletJythonDriver.java

+39-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import processing.core.PApplet;
4545
import processing.core.PConstants;
4646
import processing.core.PImage;
47+
import processing.opengl.PShader;
4748
import fisica.FContact;
4849
import fisica.FContactResult;
4950

@@ -114,6 +115,7 @@ public PAppletJythonDriver(final InteractiveConsole interp, final String sketchP
114115
this.builtins = (PyStringMap)interp.getSystemState().getBuiltins();
115116
this.interp = interp;
116117
initializeStatics(builtins);
118+
setFilter();
117119
setMap();
118120
setSet();
119121
builtins.__setitem__("g", Py.java2py(g));
@@ -336,6 +338,8 @@ public void init() {
336338

337339
public void await() throws InterruptedException {
338340
finishedLatch.await();
341+
// Cause PApplet to dispose OpenGL and other resources:
342+
finished = true;
339343
}
340344

341345
/**
@@ -382,10 +386,8 @@ public PyObject __call__(final PyObject[] args, final String[] kws) {
382386
}
383387

384388
/**
385-
* Permit the punning use of set() by mucking with the builtin "set" Type.
386-
* If you call it with 3 arguments, it acts like the Processing set(x, y,
387-
* whatever) method. If you call it with 0 or 1 args, it constructs a Python
388-
* set.
389+
* Permit both the Processing map() (which is a linear interpolation function) and
390+
* the Python map() (which is a list transformation).
389391
*/
390392
private void setMap() {
391393
final PyObject builtinMap = builtins.__getitem__("map");
@@ -415,6 +417,39 @@ public PyObject __call__(final PyObject[] args, final String[] kws) {
415417
});
416418
}
417419

420+
/**
421+
* Permit both the Processing filter() (which does image processing) and the
422+
* Python filter() (which does list comprehensions).
423+
*/
424+
private void setFilter() {
425+
final PyObject builtinFilter = builtins.__getitem__("filter");
426+
builtins.__setitem__("filter", new PyObject() {
427+
@Override
428+
public PyObject __call__(final PyObject[] args, final String[] kws) {
429+
switch (args.length) {
430+
case 1:
431+
final PyObject value = args[0];
432+
if (value.isNumberType()) {
433+
filter(value.asInt());
434+
} else {
435+
filter(Py.tojava(value, PShader.class));
436+
}
437+
return Py.None;
438+
case 2:
439+
final PyObject a = args[0];
440+
final PyObject b = args[1];
441+
if (a.isNumberType()) {
442+
filter(a.asInt(), (float)b.asDouble());
443+
return Py.None;
444+
}
445+
//$FALL-THROUGH$
446+
default:
447+
return builtinFilter.__call__(args, kws);
448+
}
449+
}
450+
});
451+
}
452+
418453
/**
419454
* Populate the Python builtins namespace with PConstants.
420455
*/

Diff for: runtime/src/jycessing/Runner.java

+15-19
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515
*/
1616
package jycessing;
1717

18-
import org.python.core.Py;
19-
import org.python.core.PyString;
20-
import org.python.util.InteractiveConsole;
21-
import org.python.util.PythonInterpreter;
22-
23-
import processing.core.PApplet;
24-
import processing.core.PConstants;
25-
2618
import java.awt.SplashScreen;
2719
import java.awt.Window;
2820
import java.io.BufferedReader;
@@ -54,6 +46,14 @@
5446
import jycessing.annotations.PythonUsage;
5547
import jycessing.launcher.LaunchHelper;
5648

49+
import org.python.core.Py;
50+
import org.python.core.PyString;
51+
import org.python.util.InteractiveConsole;
52+
import org.python.util.PythonInterpreter;
53+
54+
import processing.core.PApplet;
55+
import processing.core.PConstants;
56+
5757
public class Runner {
5858

5959
private static final String ARCH;
@@ -272,11 +272,6 @@ public static void runFromCommandLineArguments(final String[] args) throws Excep
272272
sketchPath = new File(getRuntimeRoot(), "Runtime/sketch.py").getAbsolutePath();
273273
}
274274

275-
// Sanity check in case parameter order is wrong
276-
if (sketchPath.startsWith("--")) {
277-
throw new RuntimeException("The last parameter MUST be the script to execute, not an option!");
278-
}
279-
280275
// Debug when using launcher
281276
if (Arrays.asList(args).contains("--redirect")) {
282277

@@ -371,10 +366,6 @@ public static void runSketch(final String[] args, final String sketchPath,
371366
// tests
372367
interp.setOut(System.out);
373368

374-
// Tell PApplet to make its home there, so that it can find the data
375-
// folder
376-
System.setProperty("user.dir", sketchDir);
377-
378369
// Add it to the Python library path for auxilliary modules
379370
Py.getSystemState().path.insert(0, new PyString(sketchDir));
380371

@@ -410,9 +401,14 @@ public static void runSketch(final String[] args, final String sketchPath,
410401
splash.close();
411402
}
412403

404+
// Tell the applet where to load and save data files, etc.
405+
final String[] massagedArgs = new String[args.length + 1];
406+
System.arraycopy(args, 0, massagedArgs, 0, args.length);
407+
massagedArgs[args.length] =
408+
PApplet.ARGS_SKETCH_FOLDER + "=" + new File(sketchPath).getCanonicalFile().getParent();
409+
413410
try {
414-
log("Running " + args[0]);
415-
PApplet.runSketch(args, applet);
411+
PApplet.runSketch(massagedArgs, applet);
416412
applet.await();
417413
log("Applet terminated.");
418414
log("Disposing window.");

Diff for: runtime/src/jycessing/core.py

-7
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,4 @@ def __mul__(a, b):
360360
__builtin__.unhex = PApplet.unhex
361361
__builtin__.year = PApplet.year
362362

363-
# Extra helper functions go here
364-
def pwd(name = ""):
365-
"""Returns the script's path, or the path for some data close to the script."""
366-
import java.lang.System as System
367-
return System.getProperty("python.main.root") + "/" + name
368-
__builtin__.pwd = pwd
369-
370363
del monkeypatch_method, PAppletJythonDriver

Diff for: testing/resources/data/emboss.glsl

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
#define PROCESSING_TEXTURE_SHADER
7+
8+
uniform sampler2D texture;
9+
uniform vec2 texOffset;
10+
11+
varying vec4 vertColor;
12+
varying vec4 vertTexCoord;
13+
14+
const vec4 lumcoeff = vec4(0.299, 0.587, 0.114, 0);
15+
16+
void main() {
17+
vec2 tc0 = vertTexCoord.st + vec2(-texOffset.s, -texOffset.t);
18+
vec2 tc1 = vertTexCoord.st + vec2( 0.0, -texOffset.t);
19+
vec2 tc2 = vertTexCoord.st + vec2(-texOffset.s, 0.0);
20+
vec2 tc3 = vertTexCoord.st + vec2(+texOffset.s, 0.0);
21+
vec2 tc4 = vertTexCoord.st + vec2( 0.0, +texOffset.t);
22+
vec2 tc5 = vertTexCoord.st + vec2(+texOffset.s, +texOffset.t);
23+
24+
vec4 col0 = texture2D(texture, tc0);
25+
vec4 col1 = texture2D(texture, tc1);
26+
vec4 col2 = texture2D(texture, tc2);
27+
vec4 col3 = texture2D(texture, tc3);
28+
vec4 col4 = texture2D(texture, tc4);
29+
vec4 col5 = texture2D(texture, tc5);
30+
31+
vec4 sum = vec4(0.5) + (col0 + col1 + col2) - (col3 + col4 + col5);
32+
float lum = dot(sum, lumcoeff);
33+
gl_FragColor = vec4(lum, lum, lum, 1.0) * vertColor;
34+
}

Diff for: testing/resources/data/python.png

3.07 KB
Loading

Diff for: testing/resources/test_filter.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def setup():
2+
size(48, 48, P2D)
3+
global img, emboss
4+
img = loadImage("data/python.png")
5+
emboss = loadShader("data/emboss.glsl")
6+
7+
def draw():
8+
global img, emboss
9+
# Processing builtins
10+
# filter(PShader)
11+
image(img, 0, 0)
12+
filter(emboss)
13+
14+
# filter(kind)
15+
filter(BLUR)
16+
17+
# filter(kind, param)
18+
filter(POSTERIZE, 4)
19+
20+
# Python builtin
21+
a = filter(lambda x: x == 'banana',
22+
['apple', 'grape', 'banana', 'banana'])
23+
assert a == ['banana', 'banana']
24+
25+
print 'OK'
26+
27+
exit()

Diff for: testing/resources/test_load_in_initializer.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
# To change this template, choose Tools | Templates
2-
# and open the template in the editor.
3-
4-
__author__="max.franks"
5-
6-
font = loadFont(pwd("data/Cabal1-48.vlw"))
7-
1+
font = loadFont("data/Cabal1-48.vlw")
82
def setup():
93
size(10, 10, P3D)
104
noLoop();

Diff for: testing/resources/test_map.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import urllib2
21
# Processing builtin
32
# map(value, low1, high1, low2, high2)
43
print int(map(5, 0, 10, 0, 100))

Diff for: testing/src/test/jycessing/JycessingTests.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package test.jycessing;
22

3-
import static junit.framework.Assert.assertEquals;
4-
5-
import org.junit.Test;
6-
import jycessing.Runner;
3+
import static org.junit.Assert.assertEquals;
74

85
import java.io.ByteArrayOutputStream;
96
import java.io.PrintStream;
107

8+
import jycessing.Runner;
9+
10+
import org.junit.Test;
11+
1112
public class JycessingTests {
1213

1314
private static String run(final String testResource) throws Exception {
1415
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
1516
final PrintStream saved = System.out;
1617
try {
18+
System.err.println("Running " + testResource + " test.");
1719
System.setOut(new PrintStream(baos, true));
1820
Runner.runFromCommandLineArguments(new String[] { "testing/resources/test_" + testResource
1921
+ ".py" });
@@ -28,6 +30,7 @@ private static void testImport(final String module) throws Exception {
2830
final PrintStream saved = System.out;
2931
try {
3032
System.setOut(new PrintStream(baos, true));
33+
System.err.println("Running import " + module + " test.");
3134
final String testClass = module + "_test";
3235
final String bogusFileName = "<test " + module + ">";
3336
final String testText = "import " + module + "\nprint 'OK'\nexit()";
@@ -49,6 +52,11 @@ public void static_size() throws Exception {
4952
assertEquals("OK\n", run("static_size"));
5053
}
5154

55+
@Test
56+
public void filter_builtins() throws Exception {
57+
assertEquals("OK\n", run("filter"));
58+
}
59+
5260
@Test
5361
public void set_builtins() throws Exception {
5462
assertEquals("128\nset(['banana'])\nissubclass: True\nMySet(['baz'])\n", run("set"));

0 commit comments

Comments
 (0)