How to stamp a Pgraphics canvas into a Pimage?

edited February 2018 in Programming Questions

I know usually you draw in pimage and then save to pgraphics, but is it possible to go the other way around? for example you draw the canvas image(canvas,0,0) with some effects; and then -> save that to a PImage to further effects. If that's not possible, how do you do it the other way around?

Answers

  • The PGraphics class extends the PImage class, so an instance of PGraphics is an instance of PImage. Why do you think you need to do this?

    In any case, the PImage class contains a set() function, which allows you to draw an image (which can be a PGraphics instance) on top of a PImage.

  • edited February 2018

    Instead of directly editing the canvas pixels, I need to let the canvas pixels do their thing and then show the modified image. If I modify the actual canvas pixels those changes will be visible between frames.

  • Cool, and what have you tried so far? What exactly are you stuck on?

  • I'm not sure, I've tried just about everything for the last few hours. Basically I have two PGraphics objects which I used to control the amount of blur between two layers. However I want to get the resulting image from everything and apply a pixel transformation on it. After trying to get this working for the so many hours I don't know what's going on. I'm so confused.

  • edited February 2018 Answer ✓

    You an always use get() at any point to grab a current PImage of any PGraphics object - including the main canvas.

    PGraphics pg = ...;
    
    PImage img0 = pg.get(); // Get the image of some PGraphics.
    PImage img1 = get(); // Get main canvas's image
    
    image( img0, ... ); // Draw an image on the main canvas.
    pg.beginDraw();
    pg.image( img1, ... ); // Draw an image on the PGraphics.
    pg.endDraw();
    
  • Oh my gosh... that works amazingly. Thank you SO much. That's a really powerful tool. One more question, is there a faster way to do exactly that? When I insert PImage output = get(); in my draw function it cuts my framerate in half.

  • Without seeing you full code? No idea.

  • edited February 2018 Answer ✓

    Regardless if it's a PImage, or 1 of its subclasses like PGraphics, Movie, Capture, etc., if we wanna copy the pixels[] of 1 into another, as long as both of their width & height are the same, we can use arrayCopy() to do it fastest:

    https://Processing.org/reference/arrayCopy_.html

    /**
     * Pixels[] ArrayCopy() (v1.0)
     * GoToLoop (2018/Feb/27)
     *
     * Forum.Processing.org/two/discussion/26564/
     * how-to-stamp-a-pgraphics-canvas-into-a-pimage#Item_8
     */
    
    PGraphics pg;
    PImage img;
    
    void setup() {
      size(500, 400);
      noLoop();
      colorMode(RGB);
    
      pg = createGraphics(width, height);
      img = createImage(pg.width, pg.height, ARGB);
    
      mousePressed();
    }
    
    void draw() {
      background(img);
    }
    
    void mousePressed() {
      pg.beginDraw();
      pg.background((color) random(#000000));
      pg.endDraw();
    
      arrayCopy(pg.pixels, img.pixels);
      img.updatePixels();
    
      redraw();
    }
    
    void keyPressed() {
      mousePressed();
    }
    
  • Awesome, thanks. You guys are great.

Sign In or Register to comment.