How to copy a display area and apply transformation to the copy?

edited October 2014 in How To...

Hi,

I am trying to create a sort of mirror effect so that when I draw in a certain area it is then captured, transformed and finally copied to a specific area of my display.

Of course my first idea was to use the copy() or blend() functions but unfortunately it is a straight copy to a different area and does not allow us to actually apply transformation to the copy. Also I thought of actually directly working on the pixels[] array but I was wondering if that would impact performance.

Does any one ever did that or has an idea how I could achieve that?

Thanks.

Tagged:

Answers

  • Answer ✓
    void setup(){
      size(220,220);
      strokeWeight(10);
      smooth();
      colorMode(HSB,10000);
      background(0);
    }
    
    void draw(){
      // Pretty colors!
      stroke(millis()%10000,10000,10000);
      // Draw on left side only.
      //line(mouseX,mouseY,pmouseX,pmouseY);  
      // Draw on either side.
      line(mouseX>width/2?width-mouseX:mouseX,mouseY,pmouseX>width/2?width-pmouseX:pmouseX,pmouseY);
      // Nab half the current image.
      PImage img = get(0,0,width/2,height);
      // Move origin to top left corner.
      translate(width,0);
      // Flip coordinate system horizontally.
      scale(-1,1);
      // Plonk down image.
      image(img,0,0);
    }
    
  • edited October 2014

    Your solution is the most performant I have tried so far. I tried a version that is reading/writing from pixels[] array but it was clearly slower. This is lightning fast! Thanks very much.

  • Coming back a few months later on this question because with the discovery of OpenCV for Processing I also found some quite performant functions to flip pixels around. So here is a simplified code that mirror the current screen four ways:

    import gab.opencv.*;
    
    OpenCV opencv;
    
    void setup() {
      size(800, 800);
    
      opencv = new OpenCV(this, width / 2, height / 2);
    }
    
    void draw() {
      background(255);
    
      line(mouseX, 0, mouseX, height);
      line(0, mouseY, width, mouseY);
    
      mirrorCV();
    }
    
    void mirrorCV() {
      opencv.loadImage(get(0, 0, width / 2, height / 2));
      opencv.flip(1);
      set(width / 2, 0, opencv.getSnapshot());
      opencv.flip(0);
      set(width / 2, height / 2, opencv.getSnapshot());
      opencv.flip(1);
      set(0, height / 2, opencv.getSnapshot());
    }
    
Sign In or Register to comment.