Search in sources :

Example 26 with JochreSession

use of com.joliciel.jochre.JochreSession in project jochre by urieli.

the class ShapeFillerImplTest method testFillShape.

@Test
public void testFillShape() throws Exception {
    System.setProperty("config.file", "src/test/resources/test.conf");
    ConfigFactory.invalidateCaches();
    Config config = ConfigFactory.load();
    JochreSession jochreSession = new JochreSession(config);
    final JochreImage jochreImage = mock(JochreImage.class);
    when(jochreImage.normalize(anyInt())).then(AdditionalAnswers.returnsFirstArg());
    when(jochreImage.isLeftToRight()).thenReturn(true);
    for (int i = 0; i <= 1; i++) {
        String imageName = "";
        int fillFactor = 0;
        if (i == 0) {
            imageName = "AlephWithHoles.png";
            fillFactor = 5;
        } else if (i == 1) {
            imageName = "TesWithHoles.png";
            fillFactor = 5;
        } else {
            imageName = "AlephNoHoles.png";
            fillFactor = 1;
        }
        LOG.debug(imageName);
        InputStream imageFileStream = getClass().getResourceAsStream("/com/joliciel/jochre/test/resources/" + imageName);
        assertNotNull(imageFileStream);
        BufferedImage image = ImageIO.read(imageFileStream);
        Shape shape = new Shape(jochreImage, jochreSession);
        shape.setImage(image);
        shape.setTop(0);
        shape.setLeft(0);
        shape.setRight(image.getWidth() - 1);
        shape.setBottom(image.getHeight() - 1);
        ShapeFiller shapeFiller = new ShapeFiller();
        BitSet bitset = shapeFiller.fillShape(shape, 100, fillFactor);
        for (int y = 0; y < shape.getHeight(); y++) {
            StringBuilder line = new StringBuilder();
            for (int x = 0; x < shape.getWidth(); x++) {
                if (bitset.get(y * shape.getWidth() + x))
                    line.append("x");
                else
                    line.append("o");
            }
            LOG.debug(line.toString());
        }
    }
}
Also used : Config(com.typesafe.config.Config) InputStream(java.io.InputStream) BitSet(java.util.BitSet) BufferedImage(java.awt.image.BufferedImage) JochreSession(com.joliciel.jochre.JochreSession) Test(org.junit.Test)

Example 27 with JochreSession

use of com.joliciel.jochre.JochreSession in project jochre by urieli.

the class ShapeFillerImplTest method testGetFillFactor.

@Test
public void testGetFillFactor() throws Exception {
    System.setProperty("config.file", "src/test/resources/test.conf");
    ConfigFactory.invalidateCaches();
    Config config = ConfigFactory.load();
    JochreSession jochreSession = new JochreSession(config);
    final JochreImage jochreImage = mock(JochreImage.class);
    when(jochreImage.normalize(anyInt())).then(AdditionalAnswers.returnsFirstArg());
    when(jochreImage.isLeftToRight()).thenReturn(true);
    for (int i = 0; i <= 2; i++) {
        String imageName = "";
        if (i == 0) {
            imageName = "AlephWithHoles.png";
        } else if (i == 1) {
            imageName = "TesWithHoles.png";
        } else {
            imageName = "AlephNoHoles.png";
        }
        LOG.debug(imageName);
        InputStream imageFileStream = getClass().getResourceAsStream("/com/joliciel/jochre/test/resources/" + imageName);
        assertNotNull(imageFileStream);
        BufferedImage image = ImageIO.read(imageFileStream);
        Shape shape = new Shape(jochreImage, jochreSession);
        shape.setImage(image);
        shape.setTop(0);
        shape.setLeft(0);
        shape.setRight(image.getWidth() - 1);
        shape.setBottom(image.getHeight() - 1);
        ShapeFiller shapeFiller = new ShapeFiller();
        shapeFiller.getFillFactor(shape, 100);
    }
}
Also used : Config(com.typesafe.config.Config) InputStream(java.io.InputStream) BufferedImage(java.awt.image.BufferedImage) JochreSession(com.joliciel.jochre.JochreSession) Test(org.junit.Test)

Example 28 with JochreSession

use of com.joliciel.jochre.JochreSession in project jochre by urieli.

the class ShapeImplTest method testGetBrightnessTotalsBySector.

@Test
public void testGetBrightnessTotalsBySector() throws Exception {
    System.setProperty("config.file", "src/test/resources/test.conf");
    ConfigFactory.invalidateCaches();
    Config config = ConfigFactory.load();
    JochreSession jochreSession = new JochreSession(config);
    Shape shape = new Shape(jochreSession);
    final int top = 0;
    final int bottom = 6;
    final int left = 0;
    final int right = 4;
    int[] pixels = new int[] { // row
    0, // row
    255, // row
    254, // row
    253, // row
    252, // row
    251, // row
    250, // row
    249, // row
    248, // row
    247, // row
    246, // row
    245, // row
    244, // row
    243, // row
    242, // row
    241, // row
    240, // row
    239, // row
    238, // row
    237, // row
    236, // row
    235, // row
    234, // row
    233, // row
    232, // row
    231, // row
    230, // row
    229, // row
    228, // row
    227, 226, 225, 224, 223, 222 };
    ImagePixelGrabber pixelGrabber = new ImagePixelGrabberMock(pixels, right - left + 1, bottom - top + 1);
    final GroupOfShapes group = mock(GroupOfShapes.class);
    final RowOfShapes row = mock(RowOfShapes.class);
    final Paragraph paragraph = mock(Paragraph.class);
    final JochreImage image = mock(JochreImage.class);
    final BufferedImage shapeImage = mock(BufferedImage.class);
    when(group.getId()).thenReturn(1);
    when(group.getRow()).thenReturn(row);
    when(row.getParagraph()).thenReturn(paragraph);
    when(paragraph.getImage()).thenReturn(image);
    when(image.normalize(anyInt())).thenAnswer(invocationOnMock -> {
        return invocationOnMock.getArgument(0);
    });
    when(image.isLeftToRight()).thenReturn(true);
    shape.setPixelGrabber(pixelGrabber);
    shape.setGroup(group);
    shape.setTop(0);
    shape.setBottom(6);
    shape.setLeft(0);
    shape.setRight(4);
    shape.setMeanLine(1);
    shape.setBaseLine(5);
    shape.setImage(shapeImage);
    double[][] totals = shape.getBrightnessBySection(5, 5, 1, SectionBrightnessMeasurementMethod.RAW);
    LOG.debug("Pixels:");
    for (int y = 0; y < shape.getHeight(); y++) {
        String pixelsStr = "";
        for (int x = 0; x < shape.getWidth(); x++) {
            pixelsStr += shape.getPixel(x, y) + ",";
        }
        LOG.debug(pixelsStr);
    }
    LOG.debug("Brightness totals by sector:");
    for (int y = 0; y < totals[0].length; y++) {
        String brightnessTotalsStr = "";
        for (int x = 0; x < totals.length; x++) {
            brightnessTotalsStr += totals[x][y] + ",";
        }
        LOG.debug(brightnessTotalsStr);
    }
    assertEquals(255.0, totals[0][0], 0.0001);
    double testBrightness = 0.0;
    for (int y = 0; y < totals[0].length; y++) {
        for (int x = 0; x < totals.length; x++) {
            if (x != 0 || y != 0) {
                assertEquals("For x=" + x + ",y=" + y + " expected " + testBrightness + " but was " + totals[x][y], testBrightness, totals[x][y], 0.0001);
                testBrightness += 1.0;
            }
        }
    }
}
Also used : Config(com.typesafe.config.Config) BufferedImage(java.awt.image.BufferedImage) ImagePixelGrabber(com.joliciel.jochre.utils.graphics.ImagePixelGrabber) JochreSession(com.joliciel.jochre.JochreSession) Test(org.junit.Test)

Example 29 with JochreSession

use of com.joliciel.jochre.JochreSession in project jochre by urieli.

the class ShapeImplTest method testGetBrightnessTotalsBySectorTwoSectorMargins.

@Test
public void testGetBrightnessTotalsBySectorTwoSectorMargins() throws Exception {
    System.setProperty("config.file", "src/test/resources/test.conf");
    ConfigFactory.invalidateCaches();
    Config config = ConfigFactory.load();
    JochreSession jochreSession = new JochreSession(config);
    Shape shape = new Shape(jochreSession);
    final int top = 0;
    final int bottom = 7;
    final int left = 0;
    final int right = 5;
    int[] pixels = new int[] { // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, 245, 245, 245, 245, 245, 245 };
    ImagePixelGrabber pixelGrabber = new ImagePixelGrabberMock(pixels, right - left + 1, bottom - top + 1);
    final GroupOfShapes group = mock(GroupOfShapes.class);
    final RowOfShapes row = mock(RowOfShapes.class);
    final Paragraph paragraph = mock(Paragraph.class);
    final JochreImage image = mock(JochreImage.class);
    final BufferedImage shapeImage = mock(BufferedImage.class);
    when(group.getId()).thenReturn(1);
    when(group.getRow()).thenReturn(row);
    when(row.getParagraph()).thenReturn(paragraph);
    when(paragraph.getImage()).thenReturn(image);
    when(image.normalize(anyInt())).then(returnsFirstArg());
    when(image.isLeftToRight()).thenReturn(true);
    shape.setPixelGrabber(pixelGrabber);
    shape.setGroup(group);
    shape.setTop(0);
    shape.setBottom(7);
    shape.setLeft(0);
    shape.setRight(5);
    shape.setMeanLine(2);
    shape.setBaseLine(4);
    shape.setImage(shapeImage);
    double[][] totals = shape.getBrightnessBySection(4, 4, 2, SectionBrightnessMeasurementMethod.RAW);
    LOG.debug("Pixels:");
    for (int y = 0; y < shape.getHeight(); y++) {
        String pixelsStr = "";
        for (int x = 0; x < shape.getWidth(); x++) {
            pixelsStr += shape.getPixel(x, y) + ",";
        }
        LOG.debug(pixelsStr);
    }
    LOG.debug("Brightness totals by sector:");
    for (int y = 0; y < totals[0].length; y++) {
        String brightnessTotalsStr = "";
        for (int x = 0; x < totals.length; x++) {
            brightnessTotalsStr += totals[x][y] + ",";
        }
        LOG.debug(brightnessTotalsStr);
    }
    for (int y = 0; y < totals[0].length; y++) {
        for (int x = 0; x < totals.length; x++) {
            double expected = 180.0 / 16.0;
            if (y < 2)
                expected = 120.0 / 8.0;
            else if (y > 5)
                expected = 180.0 / 8.0;
            assertEquals("For x=" + x + ",y=" + y + " expected " + expected + " but was " + totals[x][y], expected, totals[x][y], 0.1);
        }
    }
    double[][] ratios = shape.getBrightnessBySection(4, 4, 2, SectionBrightnessMeasurementMethod.SIZE_NORMALISED);
    for (int y = 0; y < ratios[0].length; y++) {
        String brightnessRatioStr = "";
        for (int x = 0; x < ratios.length; x++) {
            assertEquals(10.0, ratios[x][y], 0.01);
            brightnessRatioStr += ratios[x][y] + ",";
        }
        LOG.debug(brightnessRatioStr);
    }
}
Also used : Config(com.typesafe.config.Config) BufferedImage(java.awt.image.BufferedImage) ImagePixelGrabber(com.joliciel.jochre.utils.graphics.ImagePixelGrabber) JochreSession(com.joliciel.jochre.JochreSession) Test(org.junit.Test)

Example 30 with JochreSession

use of com.joliciel.jochre.JochreSession in project jochre by urieli.

the class ShapeImplTest method testGetBrightnessBySectorNoMargins.

@Test
public void testGetBrightnessBySectorNoMargins() throws Exception {
    System.setProperty("config.file", "src/test/resources/test.conf");
    ConfigFactory.invalidateCaches();
    Config config = ConfigFactory.load();
    JochreSession jochreSession = new JochreSession(config);
    Shape shape = new Shape(jochreSession);
    final int top = 0;
    final int bottom = 5;
    final int left = 0;
    final int right = 2;
    int[] pixels = new int[] { // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245, // row
    245 };
    ImagePixelGrabber pixelGrabber = new ImagePixelGrabberMock(pixels, right - left + 1, bottom - top + 1);
    final GroupOfShapes group = mock(GroupOfShapes.class);
    final RowOfShapes row = mock(RowOfShapes.class);
    final Paragraph paragraph = mock(Paragraph.class);
    final JochreImage image = mock(JochreImage.class);
    final BufferedImage shapeImage = mock(BufferedImage.class);
    when(group.getId()).thenReturn(1);
    when(group.getRow()).thenReturn(row);
    when(row.getParagraph()).thenReturn(paragraph);
    when(paragraph.getImage()).thenReturn(image);
    when(image.normalize(anyInt())).then(returnsFirstArg());
    when(image.isLeftToRight()).thenReturn(true);
    shape.setPixelGrabber(pixelGrabber);
    shape.setGroup(group);
    shape.setTop(0);
    shape.setBottom(bottom);
    shape.setLeft(0);
    shape.setRight(right);
    shape.setMeanLine(1);
    shape.setBaseLine(4);
    shape.setImage(shapeImage);
    double[][] totals = shape.getBrightnessBySection(6, 8, SectionBrightnessMeasurementMethod.RAW);
    LOG.debug("Pixels:");
    for (int y = 0; y < shape.getHeight(); y++) {
        String pixelsStr = "";
        for (int x = -1; x < shape.getWidth(); x++) {
            pixelsStr += shape.getPixelInShape(x, y) + ",";
        }
        LOG.debug(pixelsStr);
    }
    LOG.debug("Brightness totals by sector:");
    for (int y = 0; y < totals[0].length; y++) {
        String brightnessTotalsStr = "";
        for (int x = 0; x < totals.length; x++) {
            brightnessTotalsStr += totals[x][y] + ",";
        }
        LOG.debug(brightnessTotalsStr);
    }
    for (int y = 0; y < totals[0].length; y++) {
        for (int x = 0; x < totals.length; x++) {
            double expected = 180.0 / (6 * 8);
            assertEquals("For x=" + x + ",y=" + y + " expected " + expected + " but was " + totals[x][y], expected, totals[x][y], 0.1);
        }
    }
}
Also used : Config(com.typesafe.config.Config) BufferedImage(java.awt.image.BufferedImage) ImagePixelGrabber(com.joliciel.jochre.utils.graphics.ImagePixelGrabber) JochreSession(com.joliciel.jochre.JochreSession) Test(org.junit.Test)

Aggregations

JochreSession (com.joliciel.jochre.JochreSession)40 Config (com.typesafe.config.Config)34 Test (org.junit.Test)34 BufferedImage (java.awt.image.BufferedImage)20 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)10 Paragraph (com.joliciel.jochre.graphics.Paragraph)8 RowOfShapes (com.joliciel.jochre.graphics.RowOfShapes)8 SourceImage (com.joliciel.jochre.graphics.SourceImage)8 Segmenter (com.joliciel.jochre.graphics.Segmenter)7 Shape (com.joliciel.jochre.graphics.Shape)7 ImagePixelGrabber (com.joliciel.jochre.utils.graphics.ImagePixelGrabber)7 JochreImage (com.joliciel.jochre.graphics.JochreImage)6 Rectangle (java.awt.Rectangle)6 HashMap (java.util.HashMap)6 Session (org.zkoss.zk.ui.Session)6 BitSet (java.util.BitSet)5 JochrePage (com.joliciel.jochre.doc.JochrePage)4 SplitFeature (com.joliciel.jochre.boundaries.features.SplitFeature)3 JochreDocument (com.joliciel.jochre.doc.JochreDocument)3