Loading...
Logo
Processing Forum
Hello!

I'm struggling with the subject. While I use standard renderer, it's all good.

Copy code
  1. PImage image;
  2. void setup() {
  3.       noLoop();
  4.       selectInput("Choose an image", "inputFile");
          interrupt();
          size(image.width, image.height);
          image(image, 0, 0);
  5.       loadPixels();
  6.       //then manipulate the pixels
  7. }

  8. void inputFile(File selected) {
          if (selected == null) exit();
          image = loadImage(selected.getAbsolutePath());
          loop();
    }

  9. 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:

Copy code
  1. PImage image;
  2. boolean alreadyLoaded;

    void setup() {
          noLoop();
          if (!alreadyLoaded)
          {
                selectInput("Choose an image", "inputFile");
                interrupt();
                alreadyLoaded = true;
          }
          size(image.width, image.height, P2D);
  3.       image(image, 0, 0);
  4.       loadPixels();
          //then manipulate the pixels
    }

  5. void inputFile(File selected) {....}
  6. void interrupt() {....}
but isn't there really a cleaner soultion? I'd be very grateful if someone points out,
thanks.

Replies(2)

Note that this concerns the Processing 2.0 beta's where the selectInput() methods have been threaded.

How about this?
Copy code
  1. PImage image;
  2.  
  3. void setup() {
  4.   noLoop();
  5.   if (image==null) selectInput("Choose an image", "inputFile");     
  6.   while (image==null) delay(200);
  7.   size(image.width, image.height, P2D);    
  8.   image(image, 0, 0);
  9. }
  10.  
  11. void inputFile(File selected) {     
  12.   if (selected == null) exit();      
  13.   image = loadImage(selected.getAbsolutePath());      
  14.   loop();
  15. }


amazing, my friend, thank you! :)

says volumes about my (yet) poor java skills,
gotta learn about threading right now.