Search in sources :

Example 11 with BitWriter

use of uk.me.parabola.imgfmt.app.BitWriter in project mkgmap by openstreetmap.

the class LinePreparer method makeShortestBitStream.

/**
 * Write the bit stream to a BitWriter and return it.
 * Try different values for xBase and yBase to find the one
 * that results in the shortest bit stream.
 *
 * @return A class containing the written byte stream.
 */
public BitWriter makeShortestBitStream(int minPointsRequired) {
    BitWriter bsSimple = makeBitStream(minPointsRequired, xBase, yBase);
    if (bsSimple == null)
        return bsSimple;
    BitWriter bsBest = bsSimple;
    int xBestBase = xBase;
    int yBestBase = yBase;
    if (xBase > 0 || yBase > 0) {
        if (log.isDebugEnabled())
            log.debug("start opt:", xBase, yBase, xSameSign, xSignNegative, ySameSign, ySignNegative);
    }
    if (xBase > 0) {
        int notBetter = 0;
        boolean xSameSignBak = xSameSign;
        xSameSign = false;
        for (int xTestBase = xBase - 1; xTestBase >= 0; xTestBase--) {
            BitWriter bstest = makeBitStream(minPointsRequired, xTestBase, yBase);
            // System.out.println(xBase + " "  + xTestBase + " -> " + bsBest.getBitPosition() + " " + bstest.getBitPosition());
            if (bstest.getBitPosition() >= bsBest.getBitPosition()) {
                if (++notBetter >= 2)
                    // give up
                    break;
            } else {
                xBestBase = xTestBase;
                bsBest = bstest;
                xSameSignBak = false;
            }
        }
        xSameSign = xSameSignBak;
    }
    if (yBase > 0) {
        int notBetter = 0;
        boolean ySameSignBak = ySameSign;
        ySameSign = false;
        for (int yTestBase = yBase - 1; yTestBase >= 0; yTestBase--) {
            BitWriter bstest = makeBitStream(minPointsRequired, xBestBase, yTestBase);
            // System.out.println(yBase + " "  + yTestBase + " -> " + bsBest.getBitPosition() + " " + bstest.getBitPosition());
            if (bstest.getBitPosition() >= bsBest.getBitPosition()) {
                if (++notBetter >= 2)
                    // give up
                    break;
            } else {
                yBestBase = yTestBase;
                bsBest = bstest;
                ySameSignBak = false;
            }
        }
        ySameSign = ySameSignBak;
    }
    if (xBase != xBestBase || yBestBase != yBase) {
        if (log.isInfoEnabled()) {
            if (bsSimple.getLength() > bsBest.getLength())
                log.info("optimizer reduced bit stream byte length from", bsSimple.getLength(), "->", bsBest.getLength(), "(" + (bsSimple.getLength() - bsBest.getLength()), " byte(s)) for", polyline.getClass().getSimpleName(), "with", polyline.getPoints().size(), "points");
            else
                log.info("optimizer only reduced bit stream bit length from", bsSimple.getBitPosition(), "->", bsBest.getBitPosition(), "bits for", polyline.getClass().getSimpleName(), "with", polyline.getPoints().size(), "points, using original bit stream");
        }
    }
    if (bsSimple.getLength() == bsBest.getLength()) {
        // few CPU cycles when reading the map
        return bsSimple;
    }
    return bsBest;
}
Also used : BitWriter(uk.me.parabola.imgfmt.app.BitWriter)

Example 12 with BitWriter

use of uk.me.parabola.imgfmt.app.BitWriter in project mkgmap by openstreetmap.

the class TrueImage method write.

/**
 * Write out the image. It is a set of pixel values that are full RGB values, rather than
 * table driven as in the other image type. If the colour mode is 32 the colours have an
 * extra 4 bit opacity value following.
 *
 * If the colour mode is 16, then the transparent pixel is written just before the image
 * itself.
 */
public void write(ImgFileWriter writer) {
    int width = colourInfo.getWidth();
    int height = colourInfo.getHeight();
    int mode = colourInfo.getColourMode();
    // For mode 16, the transparent pixel precedes the pixmap data.
    if (mode == 16) {
        writer.put((byte) (transparentPixel >> 8));
        writer.put((byte) (transparentPixel >> 16));
        writer.put((byte) (transparentPixel >> 24));
    }
    boolean hasAlpha = mode == 32;
    // Unlike the xpm based images, the true-colour image format does not appear to
    // have any padding so write as a continuous block.
    BitWriter bitWriter = new BitWriter();
    for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
            int col = image[h * width + w];
            bitWriter.putn(col >> 8 & 0xff, 8);
            bitWriter.putn(col >> 16 & 0xff, 8);
            bitWriter.putn(col >> 24 & 0xff, 8);
            if (hasAlpha) {
                int alpha = 0xff - (col & 0xff);
                alpha = ColourInfo.alphaRound4(alpha);
                bitWriter.putn(alpha, 4);
            }
        }
    }
    writer.put(bitWriter.getBytes(), 0, bitWriter.getLength());
}
Also used : BitWriter(uk.me.parabola.imgfmt.app.BitWriter)

Aggregations

BitWriter (uk.me.parabola.imgfmt.app.BitWriter)12 NumberReader (func.lib.NumberReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BitReader (uk.me.parabola.imgfmt.app.BitReader)2 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 Test (org.junit.Test)1 Label (uk.me.parabola.imgfmt.app.Label)1 City (uk.me.parabola.imgfmt.app.lbl.City)1 Zip (uk.me.parabola.imgfmt.app.lbl.Zip)1 NumberPreparer (uk.me.parabola.imgfmt.app.net.NumberPreparer)1 Numbers (uk.me.parabola.imgfmt.app.net.Numbers)1