Skip to content
Snippets Groups Projects
Commit 5e2d0738 authored by Leonard Kupper's avatar Leonard Kupper
Browse files

Make AbstractRasterCanvas concrete. (now RasterCanvas)

parent a8e7f489
No related branches found
No related tags found
1 merge request!8Feat/rasterizer 10
Showing
with 78 additions and 59 deletions
......@@ -13,7 +13,7 @@ import de.tudresden.inf.mci.brailleplot.commandline.SettingType;
import de.tudresden.inf.mci.brailleplot.commandline.SettingsReader;
import de.tudresden.inf.mci.brailleplot.commandline.SettingsWriter;
import de.tudresden.inf.mci.brailleplot.rendering.AbstractRasterCanvas;
import de.tudresden.inf.mci.brailleplot.rendering.RasterCanvas;
import de.tudresden.inf.mci.brailleplot.rendering.Image;
import de.tudresden.inf.mci.brailleplot.rendering.MasterRenderer;
import diagrams.BarChart;
......@@ -169,10 +169,10 @@ public final class App {
// Rasterizing
MasterRenderer renderer = new MasterRenderer(printerConfig, formatConfig);
//AbstractRasterCanvas canvas = renderer.rasterize(exampleBarChart);
//RasterCanvas canvas = renderer.rasterize(exampleBarChart);
File imageFile = new File(getClass().getClassLoader().getResource("2_image_chart.png").getFile());
Image image = new Image(imageFile);
AbstractRasterCanvas canvas = renderer.rasterize(image);
RasterCanvas canvas = renderer.rasterize(image);
System.out.println(canvas.getCurrentPage());
......
......@@ -5,7 +5,9 @@ import de.tudresden.inf.mci.brailleplot.configparser.Printer;
import de.tudresden.inf.mci.brailleplot.printabledata.PrintableData;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import static java.lang.Math.max;
import static java.lang.Math.min;
......@@ -13,7 +15,7 @@ import static java.lang.Math.min;
/**
* Representation of a target onto which can be drawn. It wraps a {@link PrintableData} instance and specifies the size of the drawing area (in mm).
* @author Leonard Kupper
* @version 2019.07.12
* @version 2019.07.20
*/
public abstract class AbstractCanvas {
......@@ -86,8 +88,21 @@ public abstract class AbstractCanvas {
return mMillimeterHeight - (mMarginTop + mMarginBottom);
}
public final int getPageCount() {
/**
* Get the number of pages in the canvas.
* @return The number of pages.
*/
public int getPageCount() {
return mPageContainer.size();
}
/**
* Get an Iterator for the PrintableData instances representing the canvas pages. The single instances should be
* casted to the regarding concrete type depending on the canvas implementation.
* @return A {@link ListIterator}<{@link PrintableData}>.
*/
public ListIterator<PrintableData> getPageIterator() {
return mPageContainer.listIterator();
}
}
......@@ -2,12 +2,12 @@ package de.tudresden.inf.mci.brailleplot.rendering;
/**
* A rasterizer for text on braille grids.
* @version 2019.07.09
* @version 2019.07.20
* @author Leonard Kupper
*/
public class BrailleTextRasterizer implements Rasterizer<Text> {
@Override
public void rasterize(final Text data, final AbstractRasterCanvas canvas) throws InsufficientRenderingAreaException {
public void rasterize(final Text data, final RasterCanvas canvas) throws InsufficientRenderingAreaException {
// TODO: rasterize the text (Take different grids into consideration! 6-dot / 8-dot)
// Until then, we just display dummy characters
int x = data.getArea().intWrapper().getX();
......@@ -23,13 +23,13 @@ public class BrailleTextRasterizer implements Rasterizer<Text> {
// TODO: Completely replace with help methods to calculate suited area for left or right alignment of given text.
public int calculateRequiredHeight(final String text, final int xPos, final int yPos, final int maxWidth,
final AbstractRasterCanvas canvas) {
final RasterCanvas canvas) {
// TODO: Add calculations for required height to fit the given text into the given canvas. (Linebreaks!)
// Until then we use a dummy value assuming one line of text:
return canvas.getCellHeight();
}
public int calculateRequiredWidth(final String text, final int xPos, final int yPos, final AbstractRasterCanvas canvas) {
public int calculateRequiredWidth(final String text, final int xPos, final int yPos, final RasterCanvas canvas) {
// TODO: Add calculations for required width to fit the given text into the given canvas. (Extra spacing for equidistant grid!)
// Until then we use a dummy value assuming single character on braille grid:
return canvas.getCellWidth();
......
......@@ -5,16 +5,16 @@ package de.tudresden.inf.mci.brailleplot.rendering;
* The rasterizing algorithm to be used is passed to the constructor as lambda function, method reference or rasterizer implementation.
* @param <T> The concrete diagram class which can be rasterized with the rasterizer.
* @author Leonard Kupper
* @version 2019.07.01
* @version 2019.07.20
*/
public class FunctionalRasterizer<T extends Renderable> implements Rasterizer {
private Class<? extends T> mSupportedDiagramClass;
private ThrowingBiConsumer<T, AbstractRasterCanvas, InsufficientRenderingAreaException> mRasterizingAlgorithm;
private ThrowingBiConsumer<T, RasterCanvas, InsufficientRenderingAreaException> mRasterizingAlgorithm;
/**
* Constructor. Creates a new rasterizer from either a given rasterizer implementation or (keep in mind that
* Rasterizer is a functional interface) from a ThrowingBiConsumer&lt;T, AbstractRasterCanvas, InsufficientRenderingAreaException&gt; method reference.
* Rasterizer is a functional interface) from a ThrowingBiConsumer&lt;T, RasterCanvas, InsufficientRenderingAreaException&gt; method reference.
* @param supportedDiagramClass A reference to the accepted diagram class (e.g. 'BarChart.class')
* @param rasterizer A reference to a Rasterizer instance.
*/
......@@ -28,7 +28,7 @@ public class FunctionalRasterizer<T extends Renderable> implements Rasterizer {
/*
public FunctionalRasterizer(
final Class<? extends T> supportedDiagramClass,
final ThrowingBiConsumer<T, AbstractRasterCanvas, InsufficientRenderingAreaException> rasterizingAlgorithm)
final ThrowingBiConsumer<T, RasterCanvas, InsufficientRenderingAreaException> rasterizingAlgorithm)
{
mSupportedDiagramClass = supportedDiagramClass;
mRasterizingAlgorithm = rasterizingAlgorithm;
......@@ -36,7 +36,7 @@ public class FunctionalRasterizer<T extends Renderable> implements Rasterizer {
*/
@Override
public void rasterize(final Renderable data, final AbstractRasterCanvas canvas) throws InsufficientRenderingAreaException {
public void rasterize(final Renderable data, final RasterCanvas canvas) throws InsufficientRenderingAreaException {
// invoke the given rasterizing algorithm
T diagram = safeCast(data);
mRasterizingAlgorithm.accept(diagram, canvas);
......
......@@ -7,12 +7,12 @@ import java.util.Objects;
* FunctionalRenderingBase. This class acts as a wrapper for multiple {@link FunctionalRasterizer} instances.
* The rasterizer instances can be registered at runtime. The main purpose of the class is to take diagram representations of any type and select the correct concrete rasterizer.
* @author Leonard Kupper
* @version 2019.07.01
* @version 2019.07.20
*/
public class FunctionalRenderingBase {
private HashMap<Class<? extends Renderable>, FunctionalRasterizer> mRasterizingAlgorithms;
private AbstractRasterCanvas mRaster;
private RasterCanvas mRaster;
public FunctionalRenderingBase() {
mRasterizingAlgorithms = new HashMap<>();
......@@ -43,10 +43,10 @@ public class FunctionalRenderingBase {
mRasterizingAlgorithms.put(rasterizer.getSupportedDiagramClass(), rasterizer);
}
public final void setRasterCanvas(final AbstractRasterCanvas raster) {
public final void setRasterCanvas(final RasterCanvas raster) {
mRaster = Objects.requireNonNull(raster);
}
public final AbstractRasterCanvas getRaster() {
public final RasterCanvas getRaster() {
return mRaster;
}
}
......@@ -8,6 +8,8 @@ import static java.lang.Math.*;
/**
* A rasterizer that is able to re-raster a raster graphics onto a canvas.
* @author Leonard Kupper
* @version 2019.07.20
*/
public class ImageRasterizer implements Rasterizer<Image> {
......@@ -44,7 +46,7 @@ public class ImageRasterizer implements Rasterizer<Image> {
}
@Override
public void rasterize(final Image imgData, final AbstractRasterCanvas canvas) throws InsufficientRenderingAreaException {
public void rasterize(final Image imgData, final RasterCanvas canvas) throws InsufficientRenderingAreaException {
// Each rasterizer essentially works by taking an instance of a Renderable (in this case Image) and then
// creating a graphical representation of the object on the raster canvas.
......@@ -73,7 +75,7 @@ public class ImageRasterizer implements Rasterizer<Image> {
}
}
private void linearMapping(final BufferedImage imgBuf, final AbstractRasterCanvas canvas) {
private void linearMapping(final BufferedImage imgBuf, final RasterCanvas canvas) {
// 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.
......@@ -128,7 +130,7 @@ public class ImageRasterizer implements Rasterizer<Image> {
}
}
private void quantifiedPositionMapping(final BufferedImage imgBuf, final AbstractRasterCanvas canvas) {
private void quantifiedPositionMapping(final BufferedImage imgBuf, final RasterCanvas canvas) {
MatrixData<Boolean> data = canvas.getNewPage();
......
package de.tudresden.inf.mci.brailleplot.rendering;
/**
* Exception that indicates too few space available to display the amount of data contained in the given diagram representation.
* Exception that indicates too few space available to display the amount of data contained in the given renderable representation.
* Typical circumstances that lead the rasterizer/plotter to throw this exception are that there are simply too much elements to
* display them physically in the given raster/area or that the value range cannot be mapped to the given output resolution.
* @author Leonard Kupper
* @version 2019.07.01
* @version 2019.07.20
*/
public class InsufficientRenderingAreaException extends Exception {
......
......@@ -6,13 +6,19 @@ import static de.tudresden.inf.mci.brailleplot.rendering.Axis.Type.X_AXIS;
import static de.tudresden.inf.mci.brailleplot.rendering.Axis.Type.Y_AXIS;
import static java.lang.Math.abs;
/**
* A rasterizer for instances of {@link Axis} which is using a simple approach by linear mapping.
* @author Leonard Kupper
* @version 2019.07.20
*/
public class LinearMappingAxisRasterizer implements Rasterizer<Axis> {
private BrailleTextRasterizer mTextRasterizer = new BrailleTextRasterizer();
private AbstractRasterCanvas mCanvas;
private RasterCanvas mCanvas;
@Override
public void rasterize(final Axis axis, final AbstractRasterCanvas canvas) throws InsufficientRenderingAreaException {
public void rasterize(final Axis axis, final RasterCanvas canvas) throws InsufficientRenderingAreaException {
mCanvas = canvas;
MatrixData<Boolean> data = mCanvas.getCurrentPage();
......
......@@ -11,7 +11,7 @@ import java.util.Objects;
* The MasterRenderer takes representations of any diagram type, calculates the available raster/area from the given printer and format configuration and dispatches
* any calls to the 'rasterize' and 'plot' methods to the given {@link FunctionalRenderingBase}.
* @author Leonard Kupper
* @version 2019.07.12
* @version 2019.07.20
*/
public final class MasterRenderer {
......@@ -40,14 +40,14 @@ public final class MasterRenderer {
setRenderingContext(printer, format, renderingBase);
}
public AbstractRasterCanvas rasterize(final Renderable data) throws InsufficientRenderingAreaException {
AbstractRasterCanvas canvas = createCompatibleRasterCanvas();
public RasterCanvas rasterize(final Renderable data) throws InsufficientRenderingAreaException {
RasterCanvas canvas = createCompatibleRasterCanvas();
mRenderingBase.setRasterCanvas(canvas);
mRenderingBase.rasterize(data);
return canvas;
}
private AbstractRasterCanvas createCompatibleRasterCanvas() throws InsufficientRenderingAreaException {
private RasterCanvas createCompatibleRasterCanvas() throws InsufficientRenderingAreaException {
return new SixDotBrailleRasterCanvas(mPrinter, mFormat);
......
......@@ -14,9 +14,9 @@ import static java.lang.Math.*;
* Representation of a target onto which an image can be rasterized.
* It wraps a {@link de.tudresden.inf.mci.brailleplot.printabledata.MatrixData} instance and describes the raster size and its (not necessarily equidistant) layout.
* @author Leonard Kupper
* @version 2019.07.12
* @version 2019.07.20
*/
public class AbstractRasterCanvas extends AbstractCanvas {
public class RasterCanvas extends AbstractCanvas {
private ArrayList<Double> mXPositions;
private ArrayList<Double> mYPositions;
......@@ -44,7 +44,7 @@ public class AbstractRasterCanvas extends AbstractCanvas {
private Rectangle mPrintingAreaCells;
/**
* Constructor. Creates a new AbstractRasterCanvas, which is a canvas that represents it pages as instances of
* Constructor. Creates a new RasterCanvas, which is a canvas that represents it pages as instances of
* {@link MatrixData} and holds information about the layout and spacing of the underlying raster grid.
* The described grid is build from uniform 'cells' consisting of a variable amount of dots.
* It is used as a target on which can be drawn by a {@link Rasterizer}.
......@@ -55,7 +55,7 @@ public class AbstractRasterCanvas extends AbstractCanvas {
* @throws InsufficientRenderingAreaException If the given configuration leads to an printable area of negative
* size or zero size, e.g. if the sum of defined margins and constraints adds up to be greater than the original page size.
*/
AbstractRasterCanvas(final Printer printer, final Format format, final int cellWidth, final int cellHeight)
RasterCanvas(final Printer printer, final Format format, final int cellWidth, final int cellHeight)
throws InsufficientRenderingAreaException {
super(printer, format);
......
......@@ -10,12 +10,12 @@ import static java.lang.Math.min;
* Rasterizer. A functional interface for anything that is able to rasterize a diagram to a raster.
* @param <T> The concrete diagram class which can be rasterized with the rasterizer.
* @author Leonard Kupper
* @version 2019.07.01
* @version 2019.07.20
*/
@FunctionalInterface
public interface Rasterizer<T extends Renderable> {
void rasterize(T data, AbstractRasterCanvas canvas) throws InsufficientRenderingAreaException;
void rasterize(T data, RasterCanvas canvas) throws InsufficientRenderingAreaException;
// Basic geometric rasterizing toolset:
......
......@@ -6,9 +6,9 @@ import de.tudresden.inf.mci.brailleplot.configparser.Printer;
/**
* Represents a raster consisting of 6-dot braille cells.
* @author Leonard Kupper
* @version 2019.07.12
* @version 2019.07.20
*/
class SixDotBrailleRasterCanvas extends AbstractRasterCanvas {
class SixDotBrailleRasterCanvas extends RasterCanvas {
SixDotBrailleRasterCanvas(final Printer printer, final Format format) throws InsufficientRenderingAreaException {
super(printer, format, 2, 3);
......
......@@ -10,14 +10,15 @@ import java.util.Objects;
import static java.lang.Math.*;
/**
* UniformTextureBarChartRasterizer. I will write an explanation when finished, this class has changed like over 9000 times...
* A rasterizer for instances of {@link BarChart} which is based on an algorithm that constructs horizontal category
* bars that are filled with a uniform texture. The rasterizer is 'cell' based, working on 6-dot or 8-dot layouts.
* @author Leonard Kupper
* @version 2019.07.12
* @version 2019.07.20
*/
final class UniformTextureBarChartRasterizer implements Rasterizer<BarChart> {
BarChart mDiagram;
AbstractRasterCanvas mCanvas;
RasterCanvas mCanvas;
MatrixData<Boolean> mData;
// TODO: move some of these into the format config.
......@@ -45,7 +46,7 @@ final class UniformTextureBarChartRasterizer implements Rasterizer<BarChart> {
@Override
public void rasterize(final BarChart diagram, final AbstractRasterCanvas canvas)
public void rasterize(final BarChart diagram, final RasterCanvas canvas)
throws InsufficientRenderingAreaException {
// The comments here can only give a very short overview, please see the wiki for a full explanation.
......
......@@ -4,7 +4,6 @@ import de.tudresden.inf.mci.brailleplot.configparser.ConfigurationParser;
import de.tudresden.inf.mci.brailleplot.configparser.Format;
import de.tudresden.inf.mci.brailleplot.configparser.JavaPropertiesConfigurationParser;
import de.tudresden.inf.mci.brailleplot.configparser.Printer;
import diagrams.BarChart;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
......@@ -49,7 +48,7 @@ public class FunctionalRasterizerTest {
// which decides which rasterizer to use based on the Renderable type.
// Directly passing the wrong Renderable type must cause exception:
Assertions.assertThrows(IllegalArgumentException.class, () -> {
AbstractRasterCanvas testCanvas = new SixDotBrailleRasterCanvas(mPrinter, mFormat);
RasterCanvas testCanvas = new SixDotBrailleRasterCanvas(mPrinter, mFormat);
// Pass Image to Text rasterizer.
textRasterizer.rasterize(new Image(getResource("dummy.bmp")), testCanvas);
});
......
......@@ -4,7 +4,6 @@ import de.tudresden.inf.mci.brailleplot.configparser.ConfigurationParser;
import de.tudresden.inf.mci.brailleplot.configparser.Format;
import de.tudresden.inf.mci.brailleplot.configparser.JavaPropertiesConfigurationParser;
import de.tudresden.inf.mci.brailleplot.configparser.Printer;
import diagrams.BarChart;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
......@@ -64,7 +63,7 @@ public class MasterRendererTest {
MasterRenderer renderer = new MasterRenderer(mPrinter, mFormat, renderingBase);
// Test rasterizer selection
AbstractRasterCanvas result;
RasterCanvas result;
result= renderer.rasterize(new Text("dummy text", new Rectangle(0,0,1,1)));
Assertions.assertEquals(1, result.getPageCount());
......
package de.tudresden.inf.mci.brailleplot.rendering;
import de.tudresden.inf.mci.brailleplot.configparser.ConfigurationParser;
import de.tudresden.inf.mci.brailleplot.configparser.Format;
import de.tudresden.inf.mci.brailleplot.configparser.JavaPropertiesConfigurationParser;
import de.tudresden.inf.mci.brailleplot.configparser.Printer;
import de.tudresden.inf.mci.brailleplot.printabledata.MatrixData;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.ListIterator;
public class AbstractRasterCanvasTest {
public class RasterCanvasTest {
public static final String mDefaultConfig = getResource("default.properties").getAbsolutePath();
public static final String mBaseConfig = getResource("base_format.properties").getAbsolutePath();
......@@ -32,7 +29,7 @@ public class AbstractRasterCanvasTest {
Assertions.assertDoesNotThrow(
() -> {
ConfigurationParser parser = new JavaPropertiesConfigurationParser(mBaseConfig, mDefaultConfig);
AbstractRasterCanvas canvas = new SixDotBrailleRasterCanvas(parser.getPrinter(), parser.getFormat("test"));
RasterCanvas canvas = new SixDotBrailleRasterCanvas(parser.getPrinter(), parser.getFormat("test"));
// pre-calculated and measured correct values:
int x = 0;
......@@ -78,7 +75,7 @@ public class AbstractRasterCanvasTest {
Assertions.assertDoesNotThrow(
() -> {
ConfigurationParser parser = new JavaPropertiesConfigurationParser(mMarginsOnlyConfig, mDefaultConfig);
AbstractRasterCanvas canvas = new SixDotBrailleRasterCanvas(parser.getPrinter(), parser.getFormat("test"));
RasterCanvas canvas = new SixDotBrailleRasterCanvas(parser.getPrinter(), parser.getFormat("test"));
// pre-calculated and measured correct values:
// 6 mm left margin -> 1 cell border
......@@ -112,7 +109,7 @@ public class AbstractRasterCanvasTest {
Assertions.assertDoesNotThrow(
() -> {
ConfigurationParser parser = new JavaPropertiesConfigurationParser(mConstraintOnlyConfig, mDefaultConfig);
AbstractRasterCanvas canvas = new SixDotBrailleRasterCanvas(parser.getPrinter(), parser.getFormat("test"));
RasterCanvas canvas = new SixDotBrailleRasterCanvas(parser.getPrinter(), parser.getFormat("test"));
// pre-calculated and measured correct values:
// width-constraint: 190.0 mm -> fits 32 cells h.
......@@ -145,7 +142,7 @@ public class AbstractRasterCanvasTest {
Assertions.assertDoesNotThrow(
() -> {
ConfigurationParser parser = new JavaPropertiesConfigurationParser(mBothConfig, mDefaultConfig);
AbstractRasterCanvas canvas = new SixDotBrailleRasterCanvas(parser.getPrinter(), parser.getFormat("test"));
RasterCanvas canvas = new SixDotBrailleRasterCanvas(parser.getPrinter(), parser.getFormat("test"));
// pre-calculated and measured correct values:
// width-constraint: 190.0 mm -> fits 32 cells h.
......@@ -174,13 +171,13 @@ public class AbstractRasterCanvasTest {
}
@Test @SuppressWarnings("unchecked")
// AbstractRasterCanvas is guaranteed to create MatrixData instances for its pages
// RasterCanvas is guaranteed to create MatrixData instances for its pages
public void testPageIterator() {
Assertions.assertDoesNotThrow(
() -> {
ConfigurationParser parser = new JavaPropertiesConfigurationParser(mBaseConfig, mDefaultConfig);
MasterRenderer renderer = new MasterRenderer(parser.getPrinter(), parser.getFormat("test"));
AbstractRasterCanvas result = renderer.rasterize(new Image(getResource("dummy.bmp")));
RasterCanvas result = renderer.rasterize(new Image(getResource("dummy.bmp")));
ListIterator iter = result.getPageIterator();
while (iter.hasNext()) {
MatrixData<Boolean> page = (MatrixData<Boolean>) iter.next();
......
......@@ -5,7 +5,7 @@
#
# Description:
# This is a test configuration file for the AbstractCanvas and
# AbstractRasterCanvas printing area calculation tests.
# RasterCanvas printing area calculation tests.
# =============================================================================
### General Printer Properties
......
......@@ -5,7 +5,7 @@
#
# Description:
# This is a test configuration file for the AbstractCanvas and
# AbstractRasterCanvas printing area calculation tests.
# RasterCanvas printing area calculation tests.
# =============================================================================
### General Printer Properties
......
......@@ -5,7 +5,7 @@
#
# Description:
# This is a test configuration file for the AbstractCanvas and
# AbstractRasterCanvas printing area calculation tests.
# RasterCanvas printing area calculation tests.
# =============================================================================
# Define these here to have neat small concrete test configs.
......
......@@ -5,7 +5,7 @@
#
# Description:
# This is a test configuration file for the AbstractCanvas and
# AbstractRasterCanvas printing area calculation tests.
# RasterCanvas printing area calculation tests.
# =============================================================================
### General Printer Properties
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment