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,18 +3,21 @@ package com.cringe_studios.christmastreescanning.scanning;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ImageScanner {
public static final byte GAMMA_DELTA_DEFAULT = 0;
static private Point2D getBrightestSpotGrayscale(BufferedImage grayScaleImage, byte gammaDelta) {
int[] imageColors = grayScaleImage.getRGB(0, 0, grayScaleImage.getWidth(), grayScaleImage.getHeight(), new int[grayScaleImage.getWidth() * grayScaleImage.getHeight()], 0, 0);
static public Point2D getBrightestSpotGrayscale(BufferedImage grayScaleImage, byte gammaDelta) {
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++) {
imageColors[i] &= 0x0000_00FF; // because the image is grayscale, only one component is needed
}
int brightestValue = 0;
for (int i = 0; i < imageColors.length; i++) {
@ -37,7 +40,7 @@ public class ImageScanner {
.toArray(Point2D.Double[]::new);
Point2D brightestSpot = new PointInterpolator(brightPoints).getCenter();
return brightestSpot;
}

View File

@ -18,6 +18,7 @@ public class PointInterpolator {
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);
}
}

View File

@ -1,4 +1,5 @@
package com.cringe_studios.cristmastreescanning;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.awt.Color;
@ -21,19 +22,22 @@ public class ExampleTest {
public void testSus() {
assertEquals(1 + 1, 2);
}
@Test
public void generateVisualTestImages() {
Path results;
try {
results = Files.createTempDirectory("testResult");
for(int i = 0; i < 100; i++) {
for (int i = 0; i < 100; i++) {
try {
BufferedImage image = ImageIO.read(ExampleTest.class.getResourceAsStream("/0_" + i + ".png"));
Point2D p = ImageScanner.getBrightestSpot(image);
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());
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(i) + ".png")));
@ -45,32 +49,57 @@ public class ExampleTest {
e.printStackTrace();
}
}
@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() {
Path results;
try {
results = Files.createTempDirectory("testResultOldWay");
for(int i = 0; i < 100; i++) {
for (int i = 0; i < 100; i++) {
try {
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);
int[] colors = ((DataBufferInt) bi2.getRaster().getDataBuffer()).getData();
int x = -1, y = -1, brightness = 0;
boolean multipleBright = true;
int boxLeangth = 3;
while (multipleBright) {
if(boxLeangth > 1000) {
if (boxLeangth > 1000) {
System.out.println("alles gleich hell");
return;
}
for (int ä = 0; ä < colors.length; ä++) {
int b = 0;
x: for (int k = 0; k < boxLeangth; k++) {
y: for (int j = 0; j < boxLeangth; j++) {
int c = ä;
@ -79,7 +108,8 @@ public class ExampleTest {
} else {
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();
} else {
continue x;
@ -87,7 +117,7 @@ public class ExampleTest {
b += colors[c] & 255 + ((colors[c] >> 8) & 255) + ((colors[c] >> 8) & 255);
}
}
if (b > brightness) {
brightness = b;
x = ä % bi2.getWidth();
@ -97,18 +127,19 @@ public class ExampleTest {
multipleBright = true;
}
}
if(multipleBright) {
if (multipleBright) {
boxLeangth += 2;
}
}
for(int j = -2; j < 2; j++) {
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());
for (int j = -2; j < 2; j++) {
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());
}
}
ImageIO.write(image, "png", Files.newOutputStream(results.resolve(String.valueOf(i) + ".png")));
} catch (IOException e) {
e.printStackTrace();
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 776 KiB