/**
* Serializable PImage (v1.0.1)
* GoToLoop (2019/Dec/26)
*
* https://Discourse.Processing.org/t/
* deserializing-objects-sent-from-a-client-program-to-a-server-program/16124/8
*/
import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectOutput;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectInput;
static final String FILENAME = "imagen.serial", URL =
"https://" + "Forum.Processing.org/" + "processing-org.jpg";
PImage img;
void settings() {
img = loadImage(URL);
size(img.width, img.height);
noLoop();
}
void setup() {
saveSerial(FILENAME, new SerializableImage(img));
img = ((SerializableImage) loadSerial(FILENAME)).createImage(this);
}
void draw() {
background(img);
}
void saveSerial(final String filename, final Object o) {
final File f = dataFile(filename);
createPath(f);
try {
final ObjectOutput out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(o);
out.close();
}
catch (final IOException e) {
e.printStackTrace();
}
}
Object loadSerial(final String filename) {
final File f = dataFile(filename);
try {
final ObjectInput in = new ObjectInputStream(new FileInputStream(f));
final Object o = in.readObject();
in.close();
return o;
}
catch (final IOException e) {
e.printStackTrace();
}
catch (final ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
static public class SerializableImage implements Serializable {
static protected final long serialVersionUID = 123;
public int w, h, type, scale, pix[];
public SerializableImage(final PImage img) {
w = img.width;
h = img.height;
type = img.format;
scale = img.pixelDensity;
img.loadPixels();
pix = img.pixels.clone();
}
PImage createImage(final PApplet p) {
final PImage img = new PImage(w, h, type, scale);
img.parent = p;
img.loadPixels();
arrayCopy(pix, img.pixels);
img.updatePixels();
return img;
}
}