diff --git a/.gitignore b/.gitignore index 15b7b45..41eccee 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/run/moscov.png b/run/moscov.png deleted file mode 100644 index 29f3714..0000000 Binary files a/run/moscov.png and /dev/null differ diff --git a/src/main/java/io/gitlab/jfronny/ImgJava/Main.java b/src/main/java/io/gitlab/jfronny/ImgJava/Main.java index a03b5dd..b62bf15 100644 --- a/src/main/java/io/gitlab/jfronny/ImgJava/Main.java +++ b/src/main/java/io/gitlab/jfronny/ImgJava/Main.java @@ -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) { diff --git a/src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/UtilColor.java b/src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/UtilColor.java new file mode 100644 index 0000000..521e88f --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/UtilColor.java @@ -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; + } +} diff --git a/src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/UtilGeometric.java b/src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/UtilGeometric.java new file mode 100644 index 0000000..e278d1a --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/UtilGeometric.java @@ -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; + } +} diff --git a/src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/ImageUtil.java b/src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/UtilMatrix.java similarity index 63% rename from src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/ImageUtil.java rename to src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/UtilMatrix.java index d669c7a..6e8a779 100644 --- a/src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/ImageUtil.java +++ b/src/main/java/io/gitlab/jfronny/ImgJava/imageProcessing/UtilMatrix.java @@ -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}, diff --git a/src/main/java/io/gitlab/jfronny/ImgJava/util/MColor.java b/src/main/java/io/gitlab/jfronny/ImgJava/util/MColor.java index 6fd09dd..900d145 100644 --- a/src/main/java/io/gitlab/jfronny/ImgJava/util/MColor.java +++ b/src/main/java/io/gitlab/jfronny/ImgJava/util/MColor.java @@ -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; + } }