0
votes

I am using Processing and am copying the pixels[] array pixel by pixel in a for loop to a PGraphics object.

I am interested in using pushMatrix() and popMatrix() along with some transformations, but I cannot find any info on how the translate(), rotate(), and scale() functions affect how the pixels[] array is organized.

Also, in the info I could find, it says to push the matrix, then draw, and then pop the matrix back to its original state. I am curious if copying pixels pixel by pixel would count as drawing. I know that image() is affected, but what else? Where can I find a list? What are all the types of drawing and editing of pixels that the matrix transformations affect?

Thanks

1

1 Answers

0
votes

If you want to render an image into a PGraphics instance, there's no need to manually access the pixels[] array, pixel by pixel.

Notice that PGraphics provides image() which can take prior transformations (translation/rotation/scale) into account.

Here's a basic example:

PImage testImage;
PGraphics buffer;

void setup(){
  size(400,400);

  testImage = createImage(100,100,RGB);
  //make some noise
  for(int i = 0; i < testImage.pixels.length; i++){
    testImage.pixels[i] = color(random(255),random(255),random(255));
  }
  testImage.updatePixels();

  //setup PGraphics
  buffer = createGraphics(width,height);
  buffer.beginDraw();
  //apply localised transformations
  buffer.pushMatrix();
    buffer.translate(width / 2, height / 2);
    buffer.rotate(radians(45));
    buffer.scale(1.5);
    //render transformed image 
    buffer.image(testImage,0,0);
  buffer.popMatrix();

  //draw the image with no transformations
  buffer.image(testImage,0,0);
  buffer.endDraw();
}
void draw(){
  image(buffer,0,0);
}