Skip to content
Snippets Groups Projects

Feat/rasterizer update 21

Merged Ghost User requested to merge feat/rasterizer-update-21 into master
Files
2
package de.tudresden.inf.mci.brailleplot.rendering;
import de.tudresden.inf.mci.brailleplot.printabledata.MatrixData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.image.BufferedImage;
@@ -20,6 +22,8 @@ public class ImageRasterizer implements Rasterizer<Image> {
// it is technically also possible to implement a rasterizer as a method independent from a class, as long
// as it takes a renderable and a canvas as parameters.
private final Logger mLogger = LoggerFactory.getLogger(this.getClass());
// Switches
private boolean mPreventOverStretch;
private boolean mPreserveAspectRatio;
@@ -27,16 +31,14 @@ public class ImageRasterizer implements Rasterizer<Image> {
// Output threshold: A dot will be set for gray scale values below (darker than/equal) this threshold.
private int mLowThreshold;
private final int mDefaultThreshold = 80;
private static final int DEFAULT_THRESHOLD = 80;
/**
* Constructor. Creates a new {@link Rasterizer} for instances of {@link Image} with default settings.
*/
public ImageRasterizer() {
mPreventOverStretch = true;
mPreserveAspectRatio = true;
mQuantifiedPositions = true;
mLowThreshold = mDefaultThreshold;
this(true, true, true, DEFAULT_THRESHOLD);
mLogger.trace("ImageRasterizer has been setup with default settings");
}
/**
@@ -60,6 +62,8 @@ public class ImageRasterizer implements Rasterizer<Image> {
mPreserveAspectRatio = preserveAspectRatio;
mQuantifiedPositions = useQuantifiedPositions;
mLowThreshold = threshold;
mLogger.trace("Created ImageRasterizer. Settings: preventOverStretch={}, preserveAspectRatio={}, useQuantifiedPositions={}, lowThreshold={}",
mPreventOverStretch, mPreserveAspectRatio, mQuantifiedPositions, mLowThreshold);
}
/**
@@ -83,6 +87,9 @@ public class ImageRasterizer implements Rasterizer<Image> {
// the grey scale value of each pixel.
// A more sophisticated implementation could utilize an edge finding algorithm.
mLogger.info("Rasterizing Image {}", imgData);
mLogger.trace("Retrieving buffered image");
// First, a readable representation of the is retrieved.
BufferedImage imgBuf = imgData.getBufferedImage();
@@ -98,6 +105,8 @@ public class ImageRasterizer implements Rasterizer<Image> {
private void linearMapping(final BufferedImage imgBuf, final RasterCanvas canvas) {
mLogger.trace("Applying linear mapping algorithm");
// A canvas is basically a wrapper for multiple representations of printable data, each representing a page.
// These representations can be acquired by either requesting the current page or creating a new page.
MatrixData<Boolean> data = canvas.getNewPage();
@@ -134,6 +143,7 @@ public class ImageRasterizer implements Rasterizer<Image> {
// This can lead to distortions because the original pixel raster is equidistant, but the output raster
// does not have to be equidistant.
mLogger.trace("Starting scanning through image pixel values...");
// Scan through each pixel of the original image
for (int x = 0; x < imgBuf.getWidth(); x++) {
// Convert from original pixel x-position to braille dot x-position.
@@ -149,15 +159,19 @@ public class ImageRasterizer implements Rasterizer<Image> {
}
}
}
mLogger.trace("Finished scanning the image");
}
private void quantifiedPositionMapping(final BufferedImage imgBuf, final RasterCanvas canvas) {
mLogger.trace("Applying quantified position algorithm");
MatrixData<Boolean> data = canvas.getNewPage();
// Instead of using the dot rectangle a rectangle representing the target printing space in millimeters
// is built from the canvas information.
Rectangle availableArea = new Rectangle(0, 0, canvas.getPrintableWidth(), canvas.getPrintableHeight());
mLogger.trace("Determined available area (in mm): {}", availableArea);
// Calculate the ratios between original image and target printable area. (mm / pixel)
double hRatio = (availableArea.getWidth() / imgBuf.getWidth());
@@ -179,6 +193,7 @@ public class ImageRasterizer implements Rasterizer<Image> {
// In a second step, the calculated exact position is quantified to fit a dot position on the raster.
// Distortions can still be introduced but are minimized.
mLogger.trace("Staring scanning through image pixel values...");
// Scan through all pixels of the original image
for (int x = 0; x < imgBuf.getWidth(); x++) {
// Convert from original pixel x-position to printed dot x-position in millimeters.
@@ -197,6 +212,7 @@ public class ImageRasterizer implements Rasterizer<Image> {
}
}
}
mLogger.trace("Finished scanning the image");
}
Loading