This commit is contained in:
JFronny 2021-06-30 10:24:18 +02:00
parent 4a1bfcd6f8
commit 314099c9a6
7 changed files with 119 additions and 75 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
run/
# Created by https://www.toptal.com/developers/gitignore/api/rider,java,gradle,dotnetcore
# Edit at https://www.toptal.com/developers/gitignore?templates=rider,java,gradle,dotnetcore

Binary file not shown.

Before

Width:  |  Height:  |  Size: 680 KiB

View File

@ -1,6 +1,8 @@
package io.gitlab.jfronny.ImgJava;
import io.gitlab.jfronny.ImgJava.imageProcessing.ImageUtil;
import io.gitlab.jfronny.ImgJava.imageProcessing.UtilColor;
import io.gitlab.jfronny.ImgJava.imageProcessing.UtilGeometric;
import io.gitlab.jfronny.ImgJava.imageProcessing.UtilMatrix;
import io.gitlab.jfronny.ImgJava.util.ImageStackViewer;
import io.gitlab.jfronny.ImgJava.util.Picture;
@ -19,14 +21,15 @@ public class Main {
if (!Files.isDirectory(run))
Files.createDirectory(run);
Picture pict = stackViewer.push(new Picture("iris.jpg"));
pict = stackViewer.push(ImageUtil.mirror(pict, ImageUtil.MirrorMode.Horizontal));
pict = stackViewer.push(ImageUtil.mirror(pict, ImageUtil.MirrorMode.Vertical));
pict = stackViewer.push(ImageUtil.rotate(pict, ImageUtil.RotateMode.Left));
pict = stackViewer.push(ImageUtil.rotate(pict, ImageUtil.RotateMode.Right));
pict = stackViewer.push(ImageUtil.mirror(pict, ImageUtil.MirrorMode.Vertical));
pict = stackViewer.push(UtilGeometric.mirror(pict, UtilGeometric.MirrorMode.Horizontal));
pict = stackViewer.push(UtilGeometric.mirror(pict, UtilGeometric.MirrorMode.Vertical));
pict = stackViewer.push(UtilGeometric.rotate(pict, UtilGeometric.RotateMode.Left));
pict = stackViewer.push(UtilGeometric.rotate(pict, UtilGeometric.RotateMode.Right));
pict = stackViewer.push(UtilGeometric.mirror(pict, UtilGeometric.MirrorMode.Vertical));
//pict = stackViewer.push(ImageUtil.blurBox(pict, 10));
pict = stackViewer.push(ImageUtil.blurGauss(pict, 3, 2));
pict = stackViewer.push(ImageUtil.sharpen(pict, 3));
pict = stackViewer.push(UtilMatrix.blurGauss(pict, 3, 2));
pict = stackViewer.push(UtilMatrix.sharpen(pict, 3));
pict = stackViewer.push(UtilColor.tint(pict, new Color(255, 0, 0, 128)));
pict.save(run.resolve("moscov.png"));
stackViewer.repaint();
} catch (IOException e) {

View File

@ -0,0 +1,25 @@
package io.gitlab.jfronny.ImgJava.imageProcessing;
import io.gitlab.jfronny.ImgJava.util.MColor;
import io.gitlab.jfronny.ImgJava.util.Picture;
import java.awt.*;
public class UtilColor {
public static Picture tint(Picture picture, Color tint) {
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] = new MColor(pixel[x][y]).tint(tint).get();
}
}
picture.setPixelArray(pixelNeu);
return picture;
}
}

View File

@ -0,0 +1,69 @@
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;
}
}

View File

@ -12,70 +12,7 @@ import java.awt.*;
* @author Thomas Schaller
* @version 1.1 (28.11.2019)
*/
public class ImageUtil {
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;
}
public class UtilMatrix {
public static Picture blurBox(Picture picture, int amount) {
return applyMatrix(picture, new double[][]{
new double[] {1, 1, 1},

View File

@ -32,7 +32,7 @@ public class MColor {
}
public MColor add(Color c) {
return add(new MColor(c));
return this.add(new MColor(c));
}
public MColor add(MColor m) {
@ -44,7 +44,7 @@ public class MColor {
}
public MColor addM(Color c, double factor) {
return add(new MColor(c).mult(factor));
return this.add(new MColor(c).mult(factor));
}
public MColor mult(double factor) {
@ -62,4 +62,14 @@ public class MColor {
a /= factor;
return this;
}
//Alpha of tint is strength: 255 = equal to base, 0 = no effect
public MColor tint(Color t) {
double f = t.getAlpha() / 255d;
this.mult(1 - f);
r += t.getRed() * f;
g += t.getGreen() * f;
b += t.getBlue() * f;
return this;
}
}