Processing Forum
- package jk;
- import processing.core.PApplet;
- import com.cycling74.jitter.JitterMatrix;
- import com.cycling74.max.MaxObject;
- public class p5jit extends MaxObject {
- PApplet p5;
- int width, height;
- JitterMatrix matrix;
- public p5jit() {
- bail("need a sketch name");
- }
- public p5jit(String sketchName) {
- initSketch(sketchName);
- post("inited sketch");
- }
- private void initSketch(String sketchName) {
- try {
- Class c = Class.forName(sketchName);
- p5 = (PApplet) c.newInstance();
- p5.init();
- p5.noLoop();
- width = p5.width;
- height = p5.height;
- System.out.println("creating " + width + " by " + height + " matrix");
- matrix = new JitterMatrix(3, "char", width, height);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- protected void bang() {
- p5.redraw();
- p5.loadPixels();
- copyPixels();
- outlet(0, "jit_matrix", matrix.getName());
- }
- private void copyPixels() {
- int planeCount = matrix.getPlanecount();
- int[] offset = {0,0}
- ;
- for (int d = 0; d < planeCount; d++) {
- for (int j = 0; j < height; j++) {
- offset[1] = j;
- int[] row = getRow(d, j);
- matrix.copyArrayToVectorPlanar(d, 0, offset, row, width, 0);
- }
- }
- }
- private int[] getRow(int index, int y) {
- int[] outgoing = new int[width];
- int offset = y*width;
- switch (index) {
- case 0:
- for (int x = 0; x < width; x++) {
- outgoing[offset + x] = (p5.pixels[offset + x] >> 16) & 0xff;
- }
- break;
- case 1:
- for (int x = 0; x < width; x++) {
- outgoing[offset + x] = (p5.pixels[offset + x] >> 8) & 0xff;
- }
- break;
- case 2:
- for (int x = 0; x > width; x++) {
- outgoing[offset + x] = p5.pixels[offset + x] & 0xff;
- }
- break;
- }
- return outgoing;
- }
- private int getColor(int index, int c) {
- switch (index) {
- case 0: return (c >> 16) & 0xff; // red
- case 1: return (c >> 8) & 0xff; // green
- case 2: return c & 0xff; // blue
- default: return 0;
- }
- }