Added Test, Added Test image, fixed nasty bug, rethought life choices

This commit is contained in:
Paul Schaller 2023-11-20 16:15:51 +01:00
parent f152d34134
commit ffd38dcd85
4 changed files with 60 additions and 25 deletions

View File

@ -3,13 +3,16 @@ package com.cringe_studios.christmastreescanning.scanning;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class ImageScanner { public class ImageScanner {
public static final byte GAMMA_DELTA_DEFAULT = 0; public static final byte GAMMA_DELTA_DEFAULT = 0;
static private Point2D getBrightestSpotGrayscale(BufferedImage grayScaleImage, byte gammaDelta) { static public Point2D getBrightestSpotGrayscale(BufferedImage grayScaleImage, byte gammaDelta) {
int[] imageColors = grayScaleImage.getRGB(0, 0, grayScaleImage.getWidth(), grayScaleImage.getHeight(), new int[grayScaleImage.getWidth() * grayScaleImage.getHeight()], 0, 0); int[] imageColors = new int[grayScaleImage.getWidth() * grayScaleImage.getHeight()];
grayScaleImage.getRGB(0, 0, grayScaleImage.getWidth(), grayScaleImage.getHeight(), imageColors, 0, grayScaleImage.getWidth());
for (int i = 0; i < imageColors.length; i++) { for (int i = 0; i < imageColors.length; i++) {
imageColors[i] &= 0x0000_00FF; // because the image is grayscale, only one component is needed imageColors[i] &= 0x0000_00FF; // because the image is grayscale, only one component is needed

View File

@ -18,6 +18,7 @@ public class PointInterpolator {
ySum += points[i].getY(); ySum += points[i].getY();
} }
//https://math.stackexchange.com/questions/1599095/how-to-find-the-equidistant-middle-point-for-3-points-on-an-arbitrary-polygon
return new Point2D.Double( xSum / points.length, ySum / points.length); return new Point2D.Double( xSum / points.length, ySum / points.length);
} }
} }

View File

@ -1,4 +1,5 @@
package com.cringe_studios.cristmastreescanning; package com.cringe_studios.cristmastreescanning;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.awt.Color; import java.awt.Color;
@ -22,18 +23,21 @@ public class ExampleTest {
assertEquals(1 + 1, 2); assertEquals(1 + 1, 2);
} }
@Test @Test
public void generateVisualTestImages() { public void generateVisualTestImages() {
Path results; Path results;
try { try {
results = Files.createTempDirectory("testResult"); results = Files.createTempDirectory("testResult");
for(int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
try { try {
BufferedImage image = ImageIO.read(ExampleTest.class.getResourceAsStream("/0_" + i + ".png")); BufferedImage image = ImageIO.read(ExampleTest.class.getResourceAsStream("/0_" + i + ".png"));
Point2D p = ImageScanner.getBrightestSpot(image); Point2D p = ImageScanner.getBrightestSpot(image);
for(int j = -2; j < 2; j++) { for (int j = -2; j < 2; j++) {
for(int k = -2; k < 2; k++) { for (int k = -2; k < 2; k++) {
if((int) p.getX() + j > 0 && (int) p.getX() + j < image.getWidth() && (int) p.getY() + k > 0 && (int) p.getY() + k < image.getHeight()) image.setRGB((int) p.getX() + j, (int) p.getY() + k, Color.red.getRGB()); if ((int) p.getX() + j > 0 && (int) p.getX() + j < image.getWidth()
&& (int) p.getY() + k > 0 && (int) p.getY() + k < image.getHeight())
image.setRGB((int) p.getX() + j, (int) p.getY() + k, Color.red.getRGB());
} }
} }
ImageIO.write(image, "png", Files.newOutputStream(results.resolve(String.valueOf(i) + ".png"))); ImageIO.write(image, "png", Files.newOutputStream(results.resolve(String.valueOf(i) + ".png")));
@ -47,16 +51,41 @@ public class ExampleTest {
} }
@Test @Test
public void gradientTest() {
Path results;
try {
results = Files.createTempDirectory("testResultGradient");
try {
BufferedImage image = ImageIO.read(ExampleTest.class.getResourceAsStream("/gradient.png"));
Point2D p = ImageScanner.getBrightestSpotGrayscale(image, (byte)0);
for (int j = -2; j < 2; j++) {
for (int k = -2; k < 2; k++) {
if ((int) p.getX() + j > 0 && (int) p.getX() + j < image.getWidth() && (int) p.getY() + k > 0
&& (int) p.getY() + k < image.getHeight())
image.setRGB((int) p.getX() + j, (int) p.getY() + k, Color.red.getRGB());
}
}
ImageIO.write(image, "png", Files.newOutputStream(results.resolve(String.valueOf("gradientout") + ".png")));
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void generateVisualTestImagesWithOldMethod() { public void generateVisualTestImagesWithOldMethod() {
Path results; Path results;
try { try {
results = Files.createTempDirectory("testResultOldWay"); results = Files.createTempDirectory("testResultOldWay");
for(int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
try { try {
BufferedImage image = ImageIO.read(ExampleTest.class.getResourceAsStream("/0_" + i + ".png")); BufferedImage image = ImageIO.read(ExampleTest.class.getResourceAsStream("/0_" + i + ".png"));
BufferedImage bi2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); BufferedImage bi2 = new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_INT_RGB);
bi2.createGraphics().drawImage(image, 0, 0, null); bi2.createGraphics().drawImage(image, 0, 0, null);
int[] colors = ((DataBufferInt) bi2.getRaster().getDataBuffer()).getData(); int[] colors = ((DataBufferInt) bi2.getRaster().getDataBuffer()).getData();
int x = -1, y = -1, brightness = 0; int x = -1, y = -1, brightness = 0;
@ -64,7 +93,7 @@ public class ExampleTest {
int boxLeangth = 3; int boxLeangth = 3;
while (multipleBright) { while (multipleBright) {
if(boxLeangth > 1000) { if (boxLeangth > 1000) {
System.out.println("alles gleich hell"); System.out.println("alles gleich hell");
return; return;
} }
@ -79,7 +108,8 @@ public class ExampleTest {
} else { } else {
continue y; continue y;
} }
if (c + (k - boxLeangth / 2) * bi2.getWidth() > 0 && c + (k - boxLeangth / 2) * bi2.getWidth() < colors.length) { if (c + (k - boxLeangth / 2) * bi2.getWidth() > 0
&& c + (k - boxLeangth / 2) * bi2.getWidth() < colors.length) {
c += (k - boxLeangth / 2) * bi2.getWidth(); c += (k - boxLeangth / 2) * bi2.getWidth();
} else { } else {
continue x; continue x;
@ -97,14 +127,15 @@ public class ExampleTest {
multipleBright = true; multipleBright = true;
} }
} }
if(multipleBright) { if (multipleBright) {
boxLeangth += 2; boxLeangth += 2;
} }
} }
for(int j = -2; j < 2; j++) { for (int j = -2; j < 2; j++) {
for(int k = -2; k < 2; k++) { for (int k = -2; k < 2; k++) {
if(x + j > 0 && x + j < image.getWidth() && y + k > 0 && y + k < image.getHeight()) image.setRGB(x + j, y + k, Color.red.getRGB()); if (x + j > 0 && x + j < image.getWidth() && y + k > 0 && y + k < image.getHeight())
image.setRGB(x + j, y + k, Color.red.getRGB());
} }
} }
ImageIO.write(image, "png", Files.newOutputStream(results.resolve(String.valueOf(i) + ".png"))); ImageIO.write(image, "png", Files.newOutputStream(results.resolve(String.valueOf(i) + ".png")));

Binary file not shown.

After

Width:  |  Height:  |  Size: 776 KiB