API change

This commit is contained in:
Paul Schaller 2023-11-21 17:56:45 +01:00
parent aa45c6926f
commit b282d41fc9
13 changed files with 303 additions and 128 deletions

View File

@ -51,6 +51,12 @@ public class Scanner {
}
public void setData(ScannerData dataType, BufferedImage images[]) {
//TODO convert image to known format utilizing
// BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
//Graphics g = image.getGraphics();
//g.drawImage(colorImage, 0, 0, null);
//g.dispose();
if(distinctColorChannels == null || sideCount == null); //TODO error
if(distinctColorChannels.booleanValue() == false) {

View File

@ -1,102 +0,0 @@
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 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++) {
if (brightestValue < imageColors[i]) {
brightestValue = imageColors[i];
}
}
List<Integer> brightSpots = new ArrayList<Integer>();
for (int i = 0; i < imageColors.length; i++) {
if (imageColors[i] >= brightestValue - gammaDelta) {
brightSpots.add(Integer.valueOf(i));
}
}
// The following line might be incorrect; TODO double check and write a test
Point2D brightPoints[] = brightSpots.stream()
.map(s -> new Point2D.Double(s.intValue() % grayScaleImage.getWidth(), s.intValue() / grayScaleImage.getWidth())) // https://softwareengineering.stackexchange.com/questions/212808/treating-a-1d-data-structure-as-2d-grid
.toArray(Point2D.Double[]::new);
Point2D brightestSpot = new PointInterpolator(brightPoints).getCenter();
return brightestSpot;
}
// gammaDelta is the difference between the brightest pixel and the darkest
// pixel included in the search set for the brightest spot
// The brightest point Operation is done per color dimension, so you could
// provide different images for red, green and blue brightest points
static public Point2D getBrightestSpot(BufferedImage redImage, BufferedImage greenImage, BufferedImage blueImage,
byte gammaDelta) {
Point2D brightestRedSpot = getBrightestSpotGrayscale(redImage, gammaDelta);
Point2D brightestGreenSpot = getBrightestSpotGrayscale(greenImage, gammaDelta);
Point2D brightestBlueSpot = getBrightestSpotGrayscale(blueImage, gammaDelta);
double RGdistance = brightestRedSpot.distanceSq(brightestGreenSpot);
double GBdistance = brightestGreenSpot.distanceSq(brightestBlueSpot);
double BRdistance = brightestBlueSpot.distanceSq(brightestRedSpot);
PointInterpolator brightestSpot;
brightestSpot = new PointInterpolator(brightestRedSpot);
// // Get the center between the two closest points; a better implementation would
// // work on a confidentnes-value
// if (RGdistance < GBdistance && RGdistance < BRdistance) {
// brightestSpot = new PointInterpolator(brightestRedSpot, brightestGreenSpot);
// } else if (GBdistance < RGdistance && GBdistance < BRdistance) {
// brightestSpot = new PointInterpolator(brightestGreenSpot, brightestBlueSpot);
// } else {
// brightestSpot = new PointInterpolator(brightestBlueSpot, brightestRedSpot);
// }
return brightestSpot.getCenter();
}
static public Point2D getBrightestSpot(BufferedImage image, byte gammaDelta) {
BufferedImage redImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
BufferedImage greenImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
BufferedImage blueImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
int currentRGB = image.getRGB(i, j);
int currentBlue = currentRGB & 0xFF;
int currentGreen = (currentRGB >> 8) & 0xFF;
int currentRed = (currentRGB >> 16) & 0xFF;
redImage.setRGB(i, j, (currentRed << 16) | (currentRed << 8) | currentRed);
greenImage.setRGB(i, j, (currentGreen << 16) | (currentGreen << 8) | currentGreen);
blueImage.setRGB(i, j, (currentBlue << 16) | (currentBlue << 8) | currentBlue);
}
}
return getBrightestSpot(redImage, greenImage, blueImage, gammaDelta);
}
static public Point2D getBrightestSpot(BufferedImage image) {
return getBrightestSpot(image, GAMMA_DELTA_DEFAULT);
}
}

View File

@ -3,6 +3,8 @@ package com.cringe_studios.christmastreescanning.scanning;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import com.cringe_studios.christmastreescanning.scanning.image_scanning.ImageScanner;
public class MultiColoredSideScanner extends SideScanner{
private BufferedImage sideImagesRed[];
private BufferedImage sideImagesGreen[];
@ -41,22 +43,20 @@ public class MultiColoredSideScanner extends SideScanner{
this.useDefaultValues = true;
}
@Override
public void scan() {
int imagesLength = this.sideImagesRed.length;
foundPoints = new Point2D[imagesLength];
for(int i = 0; i < imagesLength; i++) {
foundPoints[i] = ImageScanner.getBrightestSpot(this.sideImagesRed[i], this.sideImagesGreen[i], this.sideImagesBlue[i], this.gammaDelta);
//TODO
//foundPoints[i] = ImageScanner.getBrightestSpot(this.sideImagesRed[i], this.sideImagesGreen[i], this.sideImagesBlue[i], this.gammaDelta);
}
}
@Override
public Point2D[] getPoints() {
return foundPoints;
}
public void normalizePoints() {
if(foundPoints == null); //TODO error
//TODO
}
}

View File

@ -9,7 +9,7 @@ public class PointInterpolator {
points = na;
}
Point2D getCenter() {
public Point2D getCenter() {
double xSum = 0;
double ySum = 0;

View File

@ -3,6 +3,8 @@ package com.cringe_studios.christmastreescanning.scanning;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import com.cringe_studios.christmastreescanning.scanning.image_scanning.ImageScanner;
public abstract class SideScanner {
// private BufferedImage sideImages[];
// private BufferedImage sideImagesRed[];
@ -87,9 +89,9 @@ public abstract class SideScanner {
// return foundPoints;
// }
public abstract void normalizePoints(); //{
// if(foundPoints == null); //TODO error
// //TODO
// }
public void normalizePoints() {
if(foundPoints == null); //TODO error
//TODO
}
}

View File

@ -3,6 +3,8 @@ package com.cringe_studios.christmastreescanning.scanning;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import com.cringe_studios.christmastreescanning.scanning.image_scanning.ImageScanner;
public class SingleColoredSideScanner extends SideScanner {
private BufferedImage sideImages[];
@ -15,8 +17,7 @@ public class SingleColoredSideScanner extends SideScanner {
Point2D foundPoints[] = null;
public SingleColoredSideScanner(BufferedImage[] images, byte gammaDelta, double baseline, double minX,
double maxX) {
public SingleColoredSideScanner(BufferedImage[] images, byte gammaDelta, double baseline, double minX, double maxX) {
this.sideImages = images;
this.gammaDelta = gammaDelta;
@ -33,7 +34,8 @@ public class SingleColoredSideScanner extends SideScanner {
this.useDefaultValues = true;
}
@Override
public void scan() {
int imagesLength = this.sideImages.length;
@ -41,20 +43,17 @@ public class SingleColoredSideScanner extends SideScanner {
for (int i = 0; i < imagesLength; i++) {
if (this.useDefaultValues) {
foundPoints[i] = ImageScanner.getBrightestSpot(sideImages[i]);
//TODO
//foundPoints[i] = ImageScanner.getBrightestSpot(sideImages[i]);
} else {
foundPoints[i] = ImageScanner.getBrightestSpot(sideImages[i], this.gammaDelta);
//TODO
//foundPoints[i] = ImageScanner.getBrightestSpot(sideImages[i], this.gammaDelta);
}
}
}
@Override
public Point2D[] getPoints() {
return foundPoints;
}
public void normalizePoints() {
if (foundPoints == null)
; // TODO error
// TODO
}
}

View File

@ -0,0 +1,56 @@
package com.cringe_studios.christmastreescanning.scanning.image_scanning;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import com.cringe_studios.christmastreescanning.scanning.PointInterpolator;
public class GrayscaleImageScanner extends ImageScanner {
BufferedImage grayScaleImage;
public GrayscaleImageScanner(BufferedImage grayScaleImage) {
super();
this.grayScaleImage = grayScaleImage;
}
@Override
public Point2D getBrightestSpot(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++) {
if (brightestValue < imageColors[i]) {
brightestValue = imageColors[i];
}
}
List<Integer> brightSpots = new ArrayList<Integer>();
for (int i = 0; i < imageColors.length; i++) {
if (imageColors[i] >= brightestValue - gammaDelta) {
brightSpots.add(Integer.valueOf(i));
}
}
// The following line might be incorrect; TODO double check and write a test
Point2D brightPoints[] = brightSpots.stream()
.map(s -> new Point2D.Double(s.intValue() % grayScaleImage.getWidth(),
s.intValue() / grayScaleImage.getWidth())) // https://softwareengineering.stackexchange.com/questions/212808/treating-a-1d-data-structure-as-2d-grid
.toArray(Point2D.Double[]::new);
Point2D brightestSpot = new PointInterpolator(brightPoints).getCenter();
return brightestSpot;
}
}

View File

@ -0,0 +1,75 @@
package com.cringe_studios.christmastreescanning.scanning.image_scanning;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.cringe_studios.christmastreescanning.scanning.PointInterpolator;
public abstract class ImageScanner {
public static final byte GAMMA_DELTA_DEFAULT = 0;
public abstract Point2D getBrightestSpot(byte gammaDelta);
public Point2D getBrightestSpot() {
return getBrightestSpot(GAMMA_DELTA_DEFAULT);
}
// // gammaDelta is the difference between the brightest pixel and the darkest
// // pixel included in the search set for the brightest spot
// // The brightest point Operation is done per color dimension, so you could
// // provide different images for red, green and blue brightest points
// static public Point2D getBrightestSpot(BufferedImage redImage, BufferedImage greenImage, BufferedImage blueImage,
// byte gammaDelta) {
// Point2D brightestRedSpot = getBrightestSpotGrayscale(redImage, gammaDelta);
// Point2D brightestGreenSpot = getBrightestSpotGrayscale(greenImage, gammaDelta);
// Point2D brightestBlueSpot = getBrightestSpotGrayscale(blueImage, gammaDelta);
//
// double RGdistance = brightestRedSpot.distanceSq(brightestGreenSpot);
// double GBdistance = brightestGreenSpot.distanceSq(brightestBlueSpot);
// double BRdistance = brightestBlueSpot.distanceSq(brightestRedSpot);
//
// PointInterpolator brightestSpot;
//
// brightestSpot = new PointInterpolator(brightestRedSpot);
//
//// // Get the center between the two closest points; a better implementation would
//// // work on a confidentnes-value
//// if (RGdistance < GBdistance && RGdistance < BRdistance) {
//// brightestSpot = new PointInterpolator(brightestRedSpot, brightestGreenSpot);
//// } else if (GBdistance < RGdistance && GBdistance < BRdistance) {
//// brightestSpot = new PointInterpolator(brightestGreenSpot, brightestBlueSpot);
//// } else {
//// brightestSpot = new PointInterpolator(brightestBlueSpot, brightestRedSpot);
//// }
//
// return brightestSpot.getCenter();
// }
//
// static public Point2D getBrightestSpot(BufferedImage image, byte gammaDelta) {
// BufferedImage redImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
// BufferedImage greenImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
// BufferedImage blueImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
//
// for (int i = 0; i < image.getWidth(); i++) {
// for (int j = 0; j < image.getHeight(); j++) {
// int currentRGB = image.getRGB(i, j);
// int currentBlue = currentRGB & 0xFF;
// int currentGreen = (currentRGB >> 8) & 0xFF;
// int currentRed = (currentRGB >> 16) & 0xFF;
//
// redImage.setRGB(i, j, (currentRed << 16) | (currentRed << 8) | currentRed);
// greenImage.setRGB(i, j, (currentGreen << 16) | (currentGreen << 8) | currentGreen);
// blueImage.setRGB(i, j, (currentBlue << 16) | (currentBlue << 8) | currentBlue);
// }
// }
//
// return getBrightestSpot(redImage, greenImage, blueImage, gammaDelta);
// }
//
// static public Point2D getBrightestSpot(BufferedImage image) {
// return getBrightestSpot(image, GAMMA_DELTA_DEFAULT);
// }
}

View File

@ -0,0 +1,17 @@
package com.cringe_studios.christmastreescanning.scanning.image_scanning;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
public class MultiImageScanner extends ImageScanner{
public MultiImageScanner() {
throw new Error("This class is not implemented yet");
}
@Override
public Point2D getBrightestSpot(byte gammaDelta) {
return null;
}
}

View File

@ -0,0 +1,51 @@
package com.cringe_studios.christmastreescanning.scanning.image_scanning;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
public class MultiRGBImageScanner extends RGBImageScanner {
GrayscaleImageScanner redScanner;
GrayscaleImageScanner greenScanner;
GrayscaleImageScanner blueScanner;
public MultiRGBImageScanner(BufferedImage rgbImage) {
super();
BufferedImage[] grayscaleImages = toGrayscale(rgbImage);
redScanner = new GrayscaleImageScanner(grayscaleImages[0]);
greenScanner = new GrayscaleImageScanner(grayscaleImages[1]);
blueScanner = new GrayscaleImageScanner(grayscaleImages[2]);
}
@Override
protected BufferedImage[] toGrayscale(BufferedImage rgbImage) {
BufferedImage redImage = new BufferedImage(rgbImage.getWidth(), rgbImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
BufferedImage greenImage = new BufferedImage(rgbImage.getWidth(), rgbImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
BufferedImage blueImage = new BufferedImage(rgbImage.getWidth(), rgbImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
for (int i = 0; i < rgbImage.getWidth(); i++) {
for (int j = 0; j < rgbImage.getHeight(); j++) {
int currentRGB = rgbImage.getRGB(i, j);
int currentBlue = currentRGB & 0xFF;
int currentGreen = (currentRGB >> 8) & 0xFF;
int currentRed = (currentRGB >> 16) & 0xFF;
redImage.setRGB(i, j, (currentRed << 16) | (currentRed << 8) | currentRed);
greenImage.setRGB(i, j, (currentGreen << 16) | (currentGreen << 8) | currentGreen);
blueImage.setRGB(i, j, (currentBlue << 16) | (currentBlue << 8) | currentBlue);
}
}
return new BufferedImage[] {redImage, greenImage, blueImage};
}
@Override
public Point2D getBrightestSpot(byte gammaDelta) {
redScanner.getBrightestSpot();
greenScanner.getBrightestSpot();
blueScanner.getBrightestSpot();
// TODO
return null;
}
}

View File

@ -0,0 +1,15 @@
package com.cringe_studios.christmastreescanning.scanning.image_scanning;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
public abstract class RGBImageScanner extends ImageScanner{
protected abstract BufferedImage[] toGrayscale(BufferedImage rgbImage);
public static RGBImageScanner instance(BufferedImage rgbImage, boolean toMultipleGrayscaleImages) {
//TODO
return null;
}
}

View File

@ -0,0 +1,53 @@
package com.cringe_studios.christmastreescanning.scanning.image_scanning;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.Arrays;
public class SingleRGBImageScanner extends RGBImageScanner {
private GrayscaleImageScanner theRealScanner; // 🤫 this class is not doing actual work... that class is
//private static BufferedImage deepCopy(BufferedImage bi) {
// ColorModel cm = bi.getColorModel();
// boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
// WritableRaster raster = bi.copyData(null);
// return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
//}
@Override
protected BufferedImage[] toGrayscale(BufferedImage rgbImage) {
BufferedImage retImage = new BufferedImage(rgbImage.getWidth(), rgbImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
int[] imageColors = new int[rgbImage.getWidth() * rgbImage.getHeight()];
rgbImage.getRGB(0, 0, rgbImage.getWidth(), rgbImage.getHeight(), imageColors, 0, rgbImage.getWidth());
imageColors = Arrays.stream(imageColors).map(i -> {
// Luminosity: Grayscale = 0.3 * R + 0.59 * G + 0.11 * B
//int alpha = (i >> 24) & 0xFF;
int red = (i >> 16) & 0xFF;
int green = (i >> 8) & 0xFF;
int blue = (i ) & 0xFF;
int grayscale = Math.min( (int)(0.3 * red + 0.59 * green + 0.11 * blue), 255);
return (grayscale << 16) | (grayscale << 8) | (grayscale);
}).toArray();
retImage.setRGB(0, 0, rgbImage.getWidth(), rgbImage.getHeight(), imageColors, 0, rgbImage.getWidth());
return new BufferedImage[]{retImage};
}
public SingleRGBImageScanner(BufferedImage rgbImage) {
super();
BufferedImage grayScaleImage = toGrayscale(rgbImage)[0];
theRealScanner = new GrayscaleImageScanner(grayScaleImage);
}
@Override
public Point2D getBrightestSpot(byte gammaDelta) {
return theRealScanner.getBrightestSpot();
}
}

View File

@ -14,7 +14,8 @@ import javax.imageio.ImageIO;
import org.junit.jupiter.api.Test;
import com.cringe_studios.christmastreescanning.scanning.ImageScanner;
import com.cringe_studios.christmastreescanning.scanning.image_scanning.GrayscaleImageScanner;
import com.cringe_studios.christmastreescanning.scanning.image_scanning.ImageScanner;
public class ExampleTest {
@ -32,7 +33,8 @@ public class ExampleTest {
for (int i = 0; i < 100; i++) {
try {
BufferedImage image = ImageIO.read(ExampleTest.class.getResourceAsStream("/0_" + i + ".png"));
Point2D p = ImageScanner.getBrightestSpot(image, (byte)(0.1 * 255));
ImageScanner currentScanner = new GrayscaleImageScanner(image);
Point2D p = currentScanner.getBrightestSpot((byte)(0.1 * 255));
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()
@ -57,7 +59,8 @@ public class ExampleTest {
results = Files.createTempDirectory("testResultGradient");
try {
BufferedImage image = ImageIO.read(ExampleTest.class.getResourceAsStream("/gradient.png"));
Point2D p = ImageScanner.getBrightestSpotGrayscale(image, (byte)0);
ImageScanner currentScanner = new GrayscaleImageScanner(image);
Point2D p = currentScanner.getBrightestSpot((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