Copying screen

Hi, I am trying to copy my main Processing output to an off screen buffer (to later send out to some LEDs).

I feel like I'm close but I'm not getting any results.

I have simplified my code and commented the lines that I'm having trouble with.

int y= 1;
PImage tempBuffer;
PGraphics offScreenBuffer;


void setup() {
  size(1280,720);
  offScreenBuffer = createGraphics(16, 9); 
  stroke(255);

}

void draw() {

background (0);
offScreenBuffer.beginDraw();

fill (255,0,0,255);
rect (0,y,1280,80);
y=(y+2) % 720;
loadPixels();


tempBuffer = get(0,0,1280,720); // copy screen to temp buffer
offScreenBuffer.image(tempBuffer, 0, 0,16,9); // copy temp to offscreen and scale to 16x9
offScreenBuffer.copy(0,0,16,9,0,0,160,90); // copy back to the top corner for debug

offScreenBuffer.endDraw();

noFill();
rect(0,0,160,90); // stroke for debug
}

Answers

  • I don't think it's a good idea read/write pixels until after endDraw(). L-)

  • edited February 2016

    Thanks, I just added an endDraw() to my offscreen buffer and it still didn't work.

    I'm wondering if my methodology is right as currently my code runs really slow, possibly because I am copying from draw() to temp then temp to offScreen.

    Is there a way to go directly from my 1280x720 draw() to my 16x9 offscreen?

    Thanks.

    Phil

    `

    int y= 1;
    PImage tempBuffer;
    PGraphics offScreenBuffer;
    
    
    void setup() {
      size(1280,720);
      offScreenBuffer = createGraphics(16, 9); 
      stroke(255);
    
    }
    
    void draw() {
    
    background (0);
    offScreenBuffer.beginDraw();
    
    fill (255,0,0,255);
    rect (0,y,1280,80);
    y=(y+2) % 720;
    loadPixels();
    
    
    tempBuffer = get(0,0,1280,720); // copy screen to temp buffer
    offScreenBuffer.image(tempBuffer, 0, 0,16,9); // copy temp to offscreen and scale to 16x9
    offScreenBuffer.endDraw();
    offScreenBuffer.copy(0,0,16,9,0,0,160,90); // copy back to the top corner for debug
    
    offScreenBuffer.endDraw();
    
    noFill();
    rect(0,0,160,90); // stroke for debug
    }`
    
  • Is there a way to go directly from my 1280x720 draw() to my 16x9 offscreen?

    We can always use image() w/ 5 arguments for resized display:
    https://processing.org/reference/image_.html

    Your copy() function is a better option. However, pass getGraphics() as the 1st source argument, so you save the extra get():
    https://Processing.org/reference/PImage_copy_.html

  • hmmm.

    I feel like I'm close, I simplified my code and it does run much faster (thanks)

    But I am still not seeing the copy in the top left.

    Below is my simplified code.

    Any ideas?

    Thanks

    Phil

    int y= 1;
    PGraphics offScreenBuffer;
    
    
    void setup() {
      size(1280,720);
      offScreenBuffer = createGraphics(16, 9); 
      stroke(255);
    
    }
    
    void draw() {
    
    background (0);
    offScreenBuffer.beginDraw();
    
    fill (255,0,0,255);
    rect (0,y,1280,80);
    y=(y+2) % 720;
    
    offScreenBuffer.image(getGraphics(), 0, 0,16,9); // copy temp to offscreen and scale to 16x9
    offScreenBuffer.endDraw();
    
    offScreenBuffer.copy(0,0,16,9,0,0,160,90); // copy to the top corner for debug
    
    noFill();
    rect(0,0,160,90); // stroke for debug
    
    }
    
  • edited February 2016 Answer ✓
    // forum.Processing.org/two/discussion/15017/copying-screen
    // GoToLoop (2016-Feb-20)
    
    PImage led;
    
    void setup() {
      size(1280, 720);
      smooth(4);
    
      fill(#FF0000);
      stroke(#FFFF00);
      strokeWeight(1.5);
      rectMode(CORNER);
    
      led = createImage(16, 9, ARGB);
    }
    
    void draw() {
      background(0);
      rect(0, frameCount*4 % height, width, 80);
    
      led.copy(getGraphics()
        , 0, 0, width, height
        , 0, 0, led.width, led.height);
    
      set(0, 0, led);
    }
    
  • This works perfectly thanks so much.

    I presume I can use

    led.loadPixels();

    and

    led.get(x, y);

    in the rest of my code and if I don't want to display the buffer I remove the.

    set(0, 0, led);

    Thanks again, this is perfect.

    Phil

  • edited February 2016

    I presume I can use led.loadPixels(); and led.get(x, y);...

    Neither get() needs loadPixels() before, nor set() needs updatePixels() after in Processing (except p5.js).
    loadPixels() is used before reading from pixels[]. updatePixels() is used after finished writing to pixels[].

  • Hi, I am SURE that this code used to work but I just reopened my sketch and it's giving me an error in Processing 2.21 and Processing3.

    The line that failed is the line copying one buffer to another.

    led.copy(getGraphics()
        , 0, 0, width, height
        , 0, 0, led.width, led.height);
    

    The error I get is

    the method copy (Pimage, int, int, int, int, int, int, int) in the type Pimage is not applicable for the arguments (Graphics,int, int, int, int, int, int, int)

    It seems like copy function doesn't like using getGraphics().

    I just tried the example sketch from @GoToLoop above and get the same error.

    Any ideas?

    Thanks Phil

  • The example I've posted here for you almost 1 year ago is still working under Processing 3.1.2 for me. :-@

  • Weird, I just tried again on the most recent version (3.23) and still get an error.

    Here is my screenshot, maybe you can see something obvious.

    Thanks,

    Phil

    pImage

  • edited January 2017

    led.copy(g
      , 0, 0, width, height
      , 0, 0, led.width, led.height);
    
  • Thanks @GoToLoop weird about the version. I have multiple versions installed and thought I had launched v3.

    The g parameter works PERFECTLY.

    Thanks

    Phil

Sign In or Register to comment.