com.joliciel.jochre.graphics.VectorizerImplTest.java Source code

Java tutorial

Introduction

Here is the source code for com.joliciel.jochre.graphics.VectorizerImplTest.java

Source

///////////////////////////////////////////////////////////////////////////////
//Copyright (C) 2012 Assaf Urieli
//
//This file is part of Jochre.
//
//Jochre is free software: you can redistribute it and/or modify
//it under the terms of the GNU Affero General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//Jochre is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Affero General Public License for more details.
//
//You should have received a copy of the GNU Affero General Public License
//along with Jochre.  If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////////////
package com.joliciel.jochre.graphics;

import java.util.BitSet;
import java.util.List;

import mockit.NonStrict;
import mockit.NonStrictExpectations;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;

import static org.junit.Assert.*;

public class VectorizerImplTest {
    private static final Log LOG = LogFactory.getLog(VectorizerImplTest.class);

    @Test
    public void testGetLongestLines(@NonStrict final Shape shape) {
        final int threshold = 100;
        final int maxLines = 60;
        final int whiteGapFillFactor = 5;

        new NonStrictExpectations() {
            {
                shape.getHeight();
                returns(8);
                shape.getWidth();
                returns(8);

                int[] pixels = { 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
                        0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1,
                        1, 1, 0, 0, 0 };

                for (int x = -1; x <= 8; x++)
                    for (int y = -1; y <= 8; y++) {
                        shape.isPixelBlack(x, y, threshold, whiteGapFillFactor);
                        if (x >= 0 && x < 8 && y >= 0 && y < 8)
                            returns(pixels[y * 8 + x] == 1);
                        else
                            returns(false);
                    }

            }
        };
        BitSet outline = new BitSet(64);

        int[] outlinePixels = { 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
                0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1,
                0, 0, 0 };
        for (int x = 0; x < 8; x++)
            for (int y = 0; y < 8; y++) {
                outline.set(y * 8 + x, outlinePixels[y * 8 + x] == 1);
            }

        VectorizerImpl vectorizer = new VectorizerImpl();
        GraphicsServiceInternal graphicsService = new GraphicsServiceImpl();
        vectorizer.setGraphicsService(graphicsService);
        vectorizer.setWhiteGapFillFactor(whiteGapFillFactor);
        List<LineSegment> lines = vectorizer.getLongestLines(shape, outline, maxLines, threshold);
        assertEquals(maxLines, lines.size());
    }

    @Test
    public void testGetLinesToEdge(@NonStrict final Shape shape) {
        final int threshold = 100;
        final int whiteGapFillFactor = 5;

        new NonStrictExpectations() {
            {
                shape.getHeight();
                returns(8);
                shape.getWidth();
                returns(8);

                int[] pixels = { 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
                        0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1,
                        1, 1, 0, 0, 0 };

                for (int x = -1; x <= 8; x++)
                    for (int y = -1; y <= 8; y++) {
                        shape.isPixelBlack(x, y, threshold, whiteGapFillFactor);
                        if (x >= 0 && x < 8 && y >= 0 && y < 8)
                            returns(pixels[y * 8 + x] == 1);
                        else
                            returns(false);
                    }

            }
        };

        VectorizerImpl vectorizer = new VectorizerImpl();
        GraphicsServiceInternal graphicsService = new GraphicsServiceImpl();
        vectorizer.setGraphicsService(graphicsService);
        vectorizer.setWhiteGapFillFactor(whiteGapFillFactor);
        List<LineSegment> lines = vectorizer.getLinesToEdge(shape, 2, 2, threshold);

        assertEquals(6, lines.size());
    }

    @Test
    public void testArrayListize(@NonStrict final Shape shape, @NonStrict final JochreImage image) {
        final int threshold = 100;
        final int whiteGapFillFactor = 5;

        new NonStrictExpectations() {
            {
                shape.getHeight();
                returns(8);
                shape.getWidth();
                returns(8);
                shape.getJochreImage();
                returns(image);
                image.getBlackThreshold();
                returns(threshold);

                int[] pixels = { 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
                        0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1,
                        1, 1, 0, 0, 0 };

                for (int x = -1; x <= 8; x++)
                    for (int y = -1; y <= 8; y++) {
                        shape.isPixelBlack(x, y, threshold, whiteGapFillFactor);
                        if (x >= 0 && x < 8 && y >= 0 && y < 8)
                            returns(pixels[y * 8 + x] == 1);
                        else
                            returns(false);
                    }

                int[] outlinePixels = { 0, 1, 1, 0, 0, 1, 1, 1, // row 0
                        0, 1, 0, 1, 0, 1, 0, 1, // row 1
                        0, 0, 1, 1, 0, 0, 1, 1, // row 2
                        0, 0, 1, 1, 0, 1, 1, 0, // row 3
                        0, 0, 0, 1, 1, 0, 1, 0, // row 4
                        0, 0, 0, 1, 0, 1, 0, 0, // row 5
                        0, 0, 1, 0, 1, 0, 0, 0, // row 6
                        1, 1, 1, 1, 1, 0, 0, 0, // row 7
                };

                BitSet outline = new BitSet();
                for (int i = 0; i < 8 * 8; i++)
                    outline.set(i, outlinePixels[i] == 1);

                shape.getOutline(threshold);
                returns(outline);

            }
        };

        VectorizerImpl vectorizer = new VectorizerImpl();
        GraphicsServiceInternal graphicsService = new GraphicsServiceImpl();
        vectorizer.setGraphicsService(graphicsService);
        vectorizer.setWhiteGapFillFactor(whiteGapFillFactor);
        List<LineSegment> lines = vectorizer.vectorize(shape);
        int i = 0;
        for (LineSegment lineSegment : lines) {
            double slope = (double) (lineSegment.getEndY() - lineSegment.getStartY())
                    / (double) (lineSegment.getEndX() - lineSegment.getStartX());
            LOG.debug("Line " + i++ + "(" + lineSegment.getStartX() + "," + lineSegment.getStartY() + ") " + "("
                    + lineSegment.getEndX() + "," + lineSegment.getEndY() + "). Length = " + lineSegment.getLength()
                    + ", Slope = " + slope);
        }

    }
}