Rotate and flipY axis of multiple RGBpixels images

<
I have created 4 images from pixels in a grid format. I want to flip two images in x and y axis. Rotate one image on its own places and keep one as it is. Please help.

PImage source, imgFlipxaxis, imgFlipyaxis, imgFlipxyaxis;
PFont font;
float r, angle = 0;

void setup() {
frameRate(24);
size(900, 600);
source = loadImage(“f.png”);
imgFlipxaxis= createImage(source.width, source.height, RGB);
imgFlipyaxis= createImage(source.width, source.height, RGB);
imgFlipxyaxis= createImage(source.width, source.height, RGB);

}

void draw(){
color p01;
float r01=0, g01=0, b01=0;
background(150,150,150);
//lights();

image(source, 0, 0);
image(imgFlipxaxis, 0, 300);
image(imgFlipyaxis, 300, 300);

   source.loadPixels();
   imgFlipxaxis.loadPixels();
   imgFlipyaxis.loadPixels();
   imgFlipxyaxis.loadPixels(); 
    
   for (int x = 0; x < source.width; x++) {
   for (int y = 0; y < source.height; y++ ) {
   int loc = x + y*source.width;
   p01 = source.pixels[loc];  // Read color from source01
   r01 = red(p01);       
   g01 = green(p01);     
   b01 = blue(p01);    
   imgFlipxaxis.pixels[loc] = color(r01,g01,b01);  
   imgFlipyaxis.pixels[loc] = color(r01,g01,b01);  
   imgFlipxyaxis.pixels[loc] = color(r01,g01,b01);  
   }}

int flipXindex = 0;
for (int y = source.height-1; y>= 0; y–) {
for(int x = 0; x < source.width; x++) {
imgFlipxaxis.pixels[flipXindex++] = source.pixels[ y*source.width + x];
}
}

int flipYindex = 0;
for (int x = source.width+1; x<= 0; x++) {
for(int y = 0; y > source.height; y–) {
imgFlipyaxis.pixels[flipYindex++] = source.pixels[ x*source.height + y];
}
}

{
angle += 0.01;
pushMatrix();
translate(width/2, height/2);
scale(0.5);
rotate(angle);
image(imgFlipxyaxis, 600, 300);
popMatrix();
}

 source.updatePixels();
 imgFlipxaxis.updatePixels();
 imgFlipyaxis.updatePixels();
 imgFlipxyaxis.updatePixels(); 

}

Umm I’m not quite sure what exactly you mean, but I’ll try my best to help you.
Here’s some things you can do with a PImage.
Flip on x-axis:

color [] oldpixels = i.pixels;
for (int y=0; y<i.height; y++) {
  for (int x=0; x<i.width; x++) {
    i.pixels[y*i.width+x] = oldpixels[y*i.width+(i.width-1-x)];
  }
}

Flip on y-axis:

color [] oldpixels = i.pixels;
for (int y=0; y<i.height; y++) {
  for (int x=0; x<i.width; x++) {
    i.pixels[y*i.width+x] = oldpixels[(i.height-1-y)*i.width+x];
  }
}

Rotate clockwise:

color [] oldpixels = i.pixels;
i = createImage(i.height, i.width, ARGB);
for (int y=0; y<i.height; y++) {
  for (int x=0; x<i.width; x++) {
    i.pixels[y*i.width+x] = oldpixels[(i.height-1-x)*i.width+y];
  }
}

Rotate counterclockwise:

color [] oldpixels = i.pixels;
i = createImage(i.height, i.width, ARGB);
for (int y=0; y<i.height; y++) {
  for (int x=0; x<i.width; x++) {
    i.pixels[y*i.width+x] = oldpixels[x*i.width+(i.height-1-y)];
  }
}

I hope this could help you.
Simon

1 Like

Thanks for that it worked.

Hello @ashk ,

Welcome!

Please format your code:
https://discourse.processing.org/faq#format-your-code

It helps us and it helps you.

:)

1 Like