How can pixel data be saved and loaded as an array of integers?
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import processing.core.PApplet;
import processing.core.PGraphics;
public class Rendering extends PApplet {
PGraphics buffer;
float x, y, s = 30;
public void setup() {
size(800, 600);
buffer = createGraphics(800, 600, JAVA2D);
}
public void draw() {
buffer.beginDraw();
buffer.smooth();
buffer.background(0);
buffer.ellipse(x, y, s, s);
buffer.endDraw();
x += 0.1;
y += 0.1;
// show
image(buffer, 0, 0);
}
public void mousePressed() {
buffer.loadPixels();
writeData("out-" + frameCount + ".dat", buffer.pixels);
}
public void writeData(String filename, int[] data) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
for (int i = 0; i < data.length; i++)
writer.write(data[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String _args[]) {
PApplet.main(new String[] { rendering.Rendering.class.getName() });
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import processing.core.PApplet;
import processing.core.PImage;
public class RenderWorker extends PApplet {
public void setup() {
size(800, 600);
colorMode(RGB);
}
public void draw() {
}
public void mousePressed() {
int[] p = readData("out-262.dat");
PImage img = createImage(800, 600, RGB);
System.arraycopy(p, 0, img.pixels, 0, p.length);
img.updatePixels();
image(img, 0, 0);
}
public int[] readData(String filename) {
File file = new File(filename);
int[] data = new int[(int) file.length()];
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
for (int i = 0; i < data.length; i++)
data[i] = reader.read();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
public static void main(String _args[]) {
PApplet.main(new String[] { rendering.RenderWorker.class.getName() });
}
}