[Likely a Bug] Processing can't save() different PGraphics of different sizes

edited June 2016 in Questions about Code

Hi, i was trying to save various PGraphics as an "export" function on my sketch and noticed a strange behavior. When exporting some PGraphics would be saved perfectly, while others would be saved as entire black images. I've reduced the code and made an minimal sketch to test the PGraphic's save() method and i noticed when you're saving various PGraphics and they have the same size, everything works perfectly. When they have different sizes, the output is buggy and some images will be black (and of varying size)

Below is just the code, but i've uploaded the sketch+images to my dropbox: download (5 kb)

PGraphics[] pg;
PImage[] imgs;
File dir;
File[] files;

void setup() {
  size(300, 300);
  initialize();
}

void draw() { 
  background(0, 255, 0);
  //draw the image on every PGraphics. Then draw the PGraphics on the screen.
  for (int i = 0; i< imgs.length; ++i)
  {
    pg[i].beginDraw();
    pg[i].image(imgs[i], 0, 0);
    pg[i].endDraw();
    image(pg[0], 0, 0);
  }
}

void initialize() {
  dir = new File(sketchPath("")+"/img"); 
  files = dir.listFiles();
  loadImages();
  //initialize the PGraphics
  pg = new PGraphics[imgs.length];
  for (int i = 0; i< imgs.length; ++i)
  {
    pg[i] = createGraphics(imgs[i].width, imgs[i].height);
  }
}

void loadImages() {
  imgs = new PImage[files.length];
  //loads every .png file into the imgs array
  for (int i = 0; i <= files.length - 1; ++i)
  {
    String path = files[i].getAbsolutePath();
    if (   path.toLowerCase().endsWith(".png")   )
    {
      imgs[i] = loadImage(path);
    }
    println("image "+i+":  "+path);
  }
}

void keyPressed() {
  if (key=='s') {
    println("EXPORTING");
    for (int i = 0; i< imgs.length; ++i)
    {
      //pg[i].beginDraw();//tried without beginDraw() and endDraw. Results are the same
      pg[i].save("test-"+i+".png");
      //pg[i].endDraw();//tirar isso se não funfar
    }
  }
}

Answers

  • edited June 2016

    I did some experiments on it. Your sketch works correctly under P2.2.1.
    But indeed it fails under P3.1.1. And also under P3.0.2! :-SS
    Unless I call method get() before save(): pg[i].get().save("test-"+i+".png");
    This is a bad regression. You should tell the devs about it: :)]
    https://GitHub.com/processing/processing/issues

  • I was having a similar problem, using a similar export function, but not using PGraphics. I checked the source code, and, as I am a novice, looked for the first save function related to graphics. So, I found this:

    hint(DISABLE_ASYNC_SAVEFRAME) - P2D/P3D only - save() and saveFrame() will not use separate threads for saving and will block until the image is written to the drive. This was the default behavior in 3.0b7 and before.

    So I inserted hint(DISABLE_ASYNC_SAVEFRAME) into the setup function, and it now seems to work in my program(no longer saving as a black box). Maybe this can help you.

  • Excellent research job there, @pandanautinspace! =D>

Sign In or Register to comment.