70 lines
1.8 KiB
Java
70 lines
1.8 KiB
Java
package io.gitlab.jfronny.ImgJava.imageProcessing;
|
|
|
|
import io.gitlab.jfronny.ImgJava.util.Picture;
|
|
|
|
import java.awt.Color;
|
|
|
|
public class UtilGeometric {
|
|
public enum MirrorMode {
|
|
Vertical,
|
|
Horizontal
|
|
}
|
|
|
|
public enum RotateMode {
|
|
Left,
|
|
Right
|
|
}
|
|
|
|
/**
|
|
* Horizontally mirrors an image (mutates original instance)
|
|
*
|
|
* @param picture Picture to mirror
|
|
* @return The mirrored image
|
|
*/
|
|
public static Picture mirror(Picture picture, MirrorMode mirrorMode) {
|
|
int w = picture.getWidth();
|
|
int h = picture.getHeight();
|
|
|
|
Color[][] pixel = picture.getPixelArray();
|
|
Color[][] pixelNeu = new Color[w][h];
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
for (int y = 0; y < h; y++) {
|
|
pixelNeu[x][y] = switch (mirrorMode) {
|
|
case Vertical -> pixel[x][h - y - 1];
|
|
case Horizontal -> pixel[w - x - 1][y];
|
|
};
|
|
}
|
|
}
|
|
|
|
picture.setPixelArray(pixelNeu);
|
|
return picture;
|
|
}
|
|
|
|
/**
|
|
* Rotates an image by 90° (mutates original instance)
|
|
*
|
|
* @param picture Picture to mirror
|
|
* @return The mirrored image
|
|
*/
|
|
public static Picture rotate(Picture picture, RotateMode rotateMode) {
|
|
int w = picture.getWidth();
|
|
int h = picture.getHeight();
|
|
|
|
Color[][] pixel = picture.getPixelArray();
|
|
Color[][] pixelNeu = new Color[h][w];
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
for (int y = 0; y < h; y++) {
|
|
pixelNeu[y][x] = switch (rotateMode) {
|
|
case Left -> pixel[w - x - 1][y];
|
|
case Right -> pixel[x][h - y - 1];
|
|
};
|
|
}
|
|
}
|
|
|
|
picture.setPixelArray(pixelNeu);
|
|
return picture;
|
|
}
|
|
}
|