pixels[] and image.get

Hello list, i have this problem: i'd like to copy a portion of my screen into an image. both are different sizes. usually I use

    myImage = get(x, y, w, h).

now I'd like to use pixels[] array to speed up a little the program.

     loadPixels();   // load data from the screen
      for (int i = 0; i < grab.width; i++) {
        for (int j = 0; j < grab.height; j++) {
              grab.pixels[i + j*grab.width] = pixels[i + j*grab.width];
       }
      }
    grab.updatePixels(); // push data into grab image
    image(grab, 500, 500);

What's wrong with this code?

thank you, Bruno

Tagged:

Answers

  • "What's wrong with this code?"
    Well, you should tell us... :-)

    One missing thing is the coordinates of the grabbing, the x and y of the get() call. Something like pixels[x + i + (y + j)*grab.width]

  • philho thanks for the hint.

    i figure out with this:

    PImage bkg, grab; 
    
    int startXClone; // were the cloning starts
    int startYClone;
    
    
    void setup() {
      size(1080, 720);
      bkg = loadImage("bkg.jpg");
      grab = createImage(320, 240, RGB);
      startXClone=100;
      startYClone=100;
      // noLoop();
    }
    
    
    
    void draw() {
      image(bkg, 0, 0);
      ellipse(mouseX, mouseY, 40, 40);
      grab.loadPixels();
      loadPixels(); 
      for (int y = startYClone; y < grab.height+startYClone; y++)
      {
        println(y);
        for (int x = startXClone; x < grab.width+startXClone; x++)
        {
          // Faster array access, but still involves math to find index
          grab.pixels[x-startXClone+((y-startYClone) * grab.width)]=pixels[x + (y * width)];
        }   
      } 
      grab.updatePixels();
      image(grab, 300, 500);
    }
    

    still doubt to clean the code to make it more readable and put it in a class

Sign In or Register to comment.