load an image via selectInput and THEN set sketch size to its dimensions, using P2D ?
in
Programming Questions
•
6 months ago
Hello!
I'm struggling with the subject. While I use standard renderer, it's all good.
-
PImage image;
-
void setup() {
-
noLoop();
-
selectInput("Choose an image", "inputFile");
interrupt();
size(image.width, image.height);
image(image, 0, 0); -
loadPixels();
-
//then manipulate the pixels
-
}
-
-
void inputFile(File selected) {
if (selected == null) exit();
image = loadImage(selected.getAbsolutePath());
loop();
} -
-
void interrupt() {
while (image==null) delay(200);
}
but the same code with size(image.width, image.height, P2D)
runs the lines before it multiple times and is unusable.
yes, I've read that it's the way P2D gets initialized.
I came up with the following workaround:
-
PImage image;
-
boolean alreadyLoaded;
void setup() {
noLoop();
if (!alreadyLoaded)
{
selectInput("Choose an image", "inputFile");
interrupt();
alreadyLoaded = true;
}
size(image.width, image.height, P2D); -
image(image, 0, 0);
-
loadPixels();
//then manipulate the pixels
} -
-
void inputFile(File selected) {....}
-
void interrupt() {....}
but isn't there really a cleaner soultion? I'd be very grateful if someone points out,
thanks.
1