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.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

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;
@ -22,6 +23,7 @@ public class ExampleTest {
assertEquals(1 + 1, 2);
}
@Test
public void generateVisualTestImages() {
Path results;
@ -33,7 +35,9 @@ public class ExampleTest {
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());
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")));
@ -47,6 +51,30 @@ public class ExampleTest {
}
@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 {
@ -56,7 +84,8 @@ public class ExampleTest {
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;
@ -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;
@ -104,7 +134,8 @@ public class ExampleTest {
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());
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")));

Binary file not shown.

After

Width:  |  Height:  |  Size: 776 KiB