PImage resize error

edited October 2013 in Programming Questions

Hi,

It seems like once I draw an image to the display window, it's not possible to redraw it with an increased size any more.

I get an ArrayIndexOutOfBoundsException: Coordinate out of bounds! error message.

Decreasing size works, however.

Any hint?

Thanks, Gyula

Test code:

PImage img;
int imgWidth = 66;

void setup() {
  size(100, 100);

// Create image
img = createImage(imgWidth, imgWidth, RGB);
img.loadPixels();
for (int i = 0; i < img.pixels.length; i++) {
  img.pixels[i] = color(255);
}
img.updatePixels();

// Do not loop draw()
noLoop();
}  

void draw() {
background(0);

imageMode(CENTER);
image(img, 50, 50);
} 

void keyPressed() {
if (key == CODED) {

  if (keyCode == UP) 
    imgWidth += 10;
  else if (keyCode == DOWN) 
    imgWidth -= 10;

  // Constrain imgWidth
  imgWidth = max(imgWidth, 6);

  println(imgWidth);
  img.resize(imgWidth, 0);

  // Redraw image
  redraw();
  }
}

P.S.: My test code looks messed up in the Preview window. Anyway, I'll post this message.

Tagged:

Answers

  • I'm sorry to see the same ugly code format in the posted message. :( Are the curly braces the culprits or me?

  • I'm trying to edit it to help out right now. Just place four spaces or a tab before each line of code.

  • Ok, thanks a lot, REAS.

  • edited October 2013

    Well, seems like resize() only works for shrinking.
    But resizing w/ image() works in all cases:

    P.S: To get those 4 spaces for code display here, in Processing use CTRL+A,
    then CTRL+] to increase indentation as many times you need to get at least 4.
    And finally CTRL+C there and CTRL+V here! (*)

    // forum.processing.org/two/discussion/79/pimage-resize-error
    
    PImage img;
    int dim = 50;
    
    void setup() {
      size(300, 300);
      noLoop();
      imageMode(CENTER);
    
      img = createImage(dim, dim, RGB);
    
      img.loadPixels();
      for (int i = img.pixels.length; i-- != 0; img.pixels[i] = -1);
      img.updatePixels();
    }  
    
    void draw() {
      background(0);
      image(img, width>>1, height>>1, dim, dim);
    } 
    
    void keyPressed() {
      final int k = keyCode;
    
      if (k == UP)          dim += 10;
      else if (k == DOWN)   dim -= 10;
    
      dim = constrain(dim, 5, width - 50);
      print(dim + " - ");
    
      redraw();
    }
    
  • It is a bad idea to resize your image several times like this, anyway. The quality will degrade on each operation. That's why image() with the wanted size is a better option. resize() is more to do a one-time operation once you loaded the image, IMHO.

  • @GoToLoop:

    Well, seems like resize() only works for shrinking.

    I think once I've used image() to display the image, somehow its maximum size gets "freezed". But before the first call of image() for the image, I can freely use resize() even for enlargement (if I'm fool enough to ignore PhiLho's previous post :) ).

    But resizing w/ image() works in all cases

    How very true! Shouldn't that be emphasized in the Processing reference?

    BTW, the reason why I want to resize the image several times is because my image is a "heatplot" or "rasterplot", where I need to zoom in to tiny details if necessary.

    Thanks to both of you for the help.

Sign In or Register to comment.