Search in sources :

Example 6 with JochreSession

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

the class ShapeImplTest method testGetBrightnessTotalsBySectorWithSquareBigger.

@Test
public void testGetBrightnessTotalsBySectorWithSquareBigger() 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 = 4;
    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 };
    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, 0.5, 0.5, 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 = 160.0 / 16.0;
            if (x < 1)
                expected = 0.0;
            else if (y < 1)
                expected = 0.0;
            else if (y > 6)
                expected = 0.0;
            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)

Example 7 with JochreSession

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

the class ShapeImplTest method testGetWidth.

@Test
public void testGetWidth() 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);
    shape.setLeft(10);
    shape.setRight(40);
    assertEquals(31, shape.getWidth());
}
Also used : Config(com.typesafe.config.Config) JochreSession(com.joliciel.jochre.JochreSession) Test(org.junit.Test)

Example 8 with JochreSession

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

the class ShapeImplTest method getVerticalCounts.

@Test
public void getVerticalCounts() throws Exception {
    System.setProperty("config.file", "src/test/resources/test.conf");
    ConfigFactory.invalidateCaches();
    Config config = ConfigFactory.load();
    JochreSession jochreSession = new JochreSession(config);
    final int threshold = 100;
    final int width = 8;
    final int height = 8;
    int[] pixels = { // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 1
    0, // row 1
    1, // row 1
    0, // row 1
    0, // row 1
    0, // row 1
    0, // row 1
    0, // row 1
    0, // row 2
    0, // row 2
    0, // row 2
    0, // row 2
    1, // row 2
    0, // row 2
    0, // row 2
    1, // row 2
    1, // row 3
    0, // row 3
    0, // row 3
    1, // row 3
    1, // row 3
    1, // row 3
    0, // row 3
    0, // row 3
    1, // row 4
    0, // row 4
    0, // row 4
    0, // row 4
    1, // row 4
    1, // row 4
    1, // row 4
    1, // row 4
    1, // row 5
    0, // row 5
    1, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 6
    0, // row 6
    1, // row 6
    1, // row 6
    0, // row 6
    0, // row 6
    0, // row 6
    0, // row 6
    0, // row 7
    0, // row 7
    0, // row 7
    1, // row 7
    1, // row 7
    0, // row 7
    0, // row 7
    0, // row 7
    0 };
    SourceImage sourceImage = new SourceImageMock(pixels, height, width, jochreSession);
    sourceImage.setSeparationThreshold(threshold);
    Shape shape = new Shape(sourceImage, jochreSession);
    shape.setTop(0);
    shape.setLeft(0);
    shape.setBottom(7);
    shape.setRight(7);
    int[] verticalCounts = shape.getVerticalCounts();
    for (int i = 0; i < verticalCounts.length; i++) {
        switch(i) {
            case 0:
                assertEquals(0 * 255, verticalCounts[i]);
                break;
            case 1:
                assertEquals(3 * 255, verticalCounts[i]);
                break;
            case 2:
                assertEquals(3 * 255, verticalCounts[i]);
                break;
            case 3:
                assertEquals(4 * 255, verticalCounts[i]);
                break;
            case 4:
                assertEquals(2 * 255, verticalCounts[i]);
                break;
            case 5:
                assertEquals(1 * 255, verticalCounts[i]);
                break;
            case 6:
                assertEquals(2 * 255, verticalCounts[i]);
                break;
            case 7:
                assertEquals(3 * 255, verticalCounts[i]);
                break;
        }
        assertEquals(shape.getWidth(), verticalCounts.length);
    }
}
Also used : Config(com.typesafe.config.Config) JochreSession(com.joliciel.jochre.JochreSession) Test(org.junit.Test)

Example 9 with JochreSession

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

the class ShapeImplTest method testGetBrightnessTotalsBySectorWithSquareSmaller.

@Test
public void testGetBrightnessTotalsBySectorWithSquareSmaller() 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, 0.5, 0.5, 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 = 120.0 / 12.0;
            if (x < 3)
                expected = 0.0;
            else if (y < 1)
                expected = 0.0;
            else if (y > 6)
                expected = 0.0;
            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)

Example 10 with JochreSession

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

the class ShapeImplTest method testGetOutline.

@Test
public void testGetOutline() throws Exception {
    System.setProperty("config.file", "src/test/resources/test.conf");
    ConfigFactory.invalidateCaches();
    Config config = ConfigFactory.load();
    JochreSession jochreSession = new JochreSession(config);
    final int threshold = 100;
    final int width = 8;
    final int height = 8;
    int[] pixels = { // row 0
    0, // row 0
    1, // row 0
    1, // row 0
    0, // row 0
    0, // row 0
    1, // row 0
    1, // row 0
    1, // row 1
    0, // row 1
    1, // row 1
    1, // row 1
    1, // row 1
    0, // row 1
    1, // row 1
    1, // row 1
    1, // row 2
    0, // row 2
    0, // row 2
    1, // row 2
    1, // row 2
    0, // row 2
    0, // row 2
    1, // row 2
    1, // row 3
    0, // row 3
    0, // row 3
    1, // row 3
    1, // row 3
    0, // row 3
    1, // row 3
    1, // row 3
    0, // row 4
    0, // row 4
    0, // row 4
    0, // row 4
    1, // row 4
    1, // row 4
    1, // row 4
    1, // row 4
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    1, // row 5
    1, // row 5
    1, // row 5
    0, // row 5
    0, // row 6
    0, // row 6
    0, // row 6
    1, // row 6
    1, // row 6
    1, // row 6
    0, // row 6
    0, // row 6
    0, // row 7
    1, // row 7
    1, // row 7
    1, // row 7
    1, // row 7
    1, // row 7
    0, // row 7
    0, // row 7
    0 };
    SourceImage sourceImage = new SourceImageMock(pixels, height, width, jochreSession);
    sourceImage.setWhiteGapFillFactor(0);
    sourceImage.setBlackThreshold(threshold);
    Shape shape = new Shape(sourceImage, jochreSession);
    shape.setTop(0);
    shape.setBottom(7);
    shape.setLeft(0);
    shape.setRight(7);
    BitSet outline = shape.getOutline(threshold);
    int[] outlinePixels = { // row 0
    0, // row 0
    1, // row 0
    1, // row 0
    0, // row 0
    0, // row 0
    1, // row 0
    1, // row 0
    1, // row 1
    0, // row 1
    1, // row 1
    0, // row 1
    1, // row 1
    0, // row 1
    1, // row 1
    0, // row 1
    1, // row 2
    0, // row 2
    0, // row 2
    1, // row 2
    1, // row 2
    0, // row 2
    0, // row 2
    1, // row 2
    1, // row 3
    0, // row 3
    0, // row 3
    1, // row 3
    1, // row 3
    0, // row 3
    1, // row 3
    1, // row 3
    0, // row 4
    0, // row 4
    0, // row 4
    0, // row 4
    1, // row 4
    1, // row 4
    0, // row 4
    1, // row 4
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    1, // row 5
    0, // row 5
    1, // row 5
    0, // row 5
    0, // row 6
    0, // row 6
    0, // row 6
    1, // row 6
    0, // row 6
    1, // row 6
    0, // row 6
    0, // row 6
    0, // row 7
    1, // row 7
    1, // row 7
    1, // row 7
    1, // row 7
    1, // row 7
    0, // row 7
    0, // row 7
    0 };
    for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) {
        assertEquals("x = " + x + ",y = " + y, outlinePixels[y * 8 + x] == 1, outline.get(y * 8 + x));
    }
}
Also used : Config(com.typesafe.config.Config) BitSet(java.util.BitSet) 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