Search in sources :

Example 1 with BitWriter

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

the class BitmapImage method write.

public void write(ImgFileWriter writer) {
    final int bitSize = colourInfo.getBitsPerPixel();
    int cpp = colourInfo.getCharsPerPixel();
    int width = colourInfo.getWidth();
    int height = colourInfo.getHeight();
    int i = 0;
    for (int h = 0; h < height; h++) {
        // Each row is padded to a byte boundary, creating a new bit writer for every
        // row ensures that happens.
        BitWriter bitWriter = new BitWriter();
        for (int w = 0; w < width; w++) {
            String idx = image.substring(i, i + cpp);
            i += cpp;
            int val = colourInfo.getIndex(idx);
            bitWriter.putn(val, bitSize);
        }
        writer.put(bitWriter.getBytes(), 0, bitWriter.getLength());
    }
}
Also used : BitWriter(uk.me.parabola.imgfmt.app.BitWriter)

Example 2 with BitWriter

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

the class ColourInfo method writeColours20.

/**
 * Write out the colours in the colormode=x20 case.
 */
private void writeColours20(ImgFileWriter writer) {
    BitWriter bw = new BitWriter();
    for (Rgb rgb : colours) {
        bw.putn(rgb.getB(), 8);
        bw.putn(rgb.getG(), 8);
        bw.putn(rgb.getR(), 8);
        int alpha = 0xff - rgb.getA();
        alpha = alphaRound4(alpha);
        bw.putn(alpha, 4);
    }
    writer.put(bw.getBytes(), 0, bw.getLength());
}
Also used : BitWriter(uk.me.parabola.imgfmt.app.BitWriter)

Example 3 with BitWriter

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

the class Polyline method write.

/*
	 * write the polyline to an OutputStream - only use for outputting
	 * lines with extended (3 byte) types.
	 *
	 */
public void write(OutputStream stream) throws IOException {
    assert hasExtendedType();
    int type = getType();
    int labelOff = getLabel().getOffset();
    byte[] extraBytes = getExtTypeExtraBytes();
    LinePreparer w;
    try {
        // need to prepare line info before outputing lat/lon
        w = new LinePreparer(this);
    } catch (AssertionError ae) {
        log.error("Problem writing line (" + getClass() + ") of type 0x" + Integer.toHexString(getType()) + " containing " + points.size() + " points and starting at " + points.get(0).toOSMURL());
        log.error("  Subdivision shift is " + getSubdiv().getShift() + " and its centre is at " + getSubdiv().getCenter().toOSMURL());
        log.error("  " + ae.getMessage());
        if (roaddef != null)
            log.error("  Way is " + roaddef);
        return;
    }
    int minPointsRequired = (this instanceof Polygon) ? 3 : 2;
    BitWriter bw = w.makeShortestBitStream(minPointsRequired);
    if (bw == null) {
        log.error("Level " + getSubdiv().getZoom().getLevel() + " " + ((this instanceof Polygon) ? "polygon" : "polyline") + " has less than " + minPointsRequired + " points, discarding");
        return;
    }
    int blen = bw.getLength();
    assert blen > 1 : "zero length bitstream";
    assert blen < 0x10000 : "bitstream too long " + blen;
    if (labelOff != 0)
        // has label
        type |= 0x20;
    if (extraBytes != null)
        // has extra bytes
        type |= 0x80;
    stream.write(type >> 8);
    stream.write(type);
    int deltaLong = getDeltaLong();
    int deltaLat = getDeltaLat();
    stream.write(deltaLong);
    stream.write(deltaLong >> 8);
    stream.write(deltaLat);
    stream.write(deltaLat >> 8);
    if (blen >= 0x7f) {
        stream.write((blen << 2) | 2);
        stream.write((blen << 2) >> 8);
    } else {
        stream.write((blen << 1) | 1);
    }
    stream.write(bw.getBytes(), 0, blen);
    if (labelOff != 0) {
        stream.write(labelOff);
        stream.write(labelOff >> 8);
        stream.write(labelOff >> 16);
    }
    if (extraBytes != null)
        stream.write(extraBytes);
}
Also used : BitWriter(uk.me.parabola.imgfmt.app.BitWriter)

Example 4 with BitWriter

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

the class Polyline method write.

/**
 * Format and write the contents of the object to the given
 * file.
 *
 * @param file A reference to the file that should be written to.
 */
public void write(ImgFileWriter file) {
    // Prepare for writing by doing all the required calculations.
    LinePreparer w;
    try {
        // Prepare the information that we need.
        w = new LinePreparer(this);
    } catch (AssertionError ae) {
        log.error("Problem writing line (" + getClass() + ") of type 0x" + Integer.toHexString(getType()) + " containing " + points.size() + " points and starting at " + points.get(0).toOSMURL());
        log.error("  Subdivision shift is " + getSubdiv().getShift() + " and its centre is at " + getSubdiv().getCenter().toOSMURL());
        log.error("  " + ae.getMessage());
        if (roaddef != null)
            log.error("  Way is " + roaddef);
        return;
    }
    int minPointsRequired = (this instanceof Polygon) ? 3 : 2;
    BitWriter bw = w.makeShortestBitStream(minPointsRequired);
    if (bw == null) {
        log.error("Level " + getSubdiv().getZoom().getLevel() + " " + ((this instanceof Polygon) ? "polygon" : "polyline") + " has less than " + minPointsRequired + " points, discarding");
        return;
    }
    // The type of feature, also contains a couple of flags hidden inside.
    byte b1 = (byte) getType();
    if (direction)
        // Polylines only.
        b1 |= FLAG_DIR;
    // allow for the sizes
    int blen = bw.getLength() - 1;
    assert blen > 0 : "zero length bitstream";
    assert blen < 0x10000 : "bitstream too long " + blen;
    if (blen >= 0x100)
        b1 |= FLAG_2BYTE_LEN;
    file.put(b1);
    // The label, contains a couple of flags within it.
    int loff = getLabel().getOffset();
    if (w.isExtraBit())
        loff |= FLAG_EXTRABIT;
    // so that we can change it to the index in the net section
    if (roaddef != null) {
        roaddef.addLabel(getLabel());
        roaddef.addOffsetTarget(file.position(), FLAG_NETINFO | (loff & FLAG_EXTRABIT));
        // also add ref label(s) if present
        if (refLabels != null)
            for (Label rl : refLabels) roaddef.addLabel(rl);
    }
    file.put3(loff);
    // The delta of the longitude from the subdivision centre point
    // note that this has already been calculated.
    file.putChar((char) getDeltaLong());
    file.putChar((char) getDeltaLat());
    if (log.isDebugEnabled())
        log.debug("out center", getDeltaLat(), getDeltaLong());
    if (blen < 0x100)
        file.put((byte) (blen & 0xff));
    else
        file.putChar((char) (blen & 0xffff));
    file.put(bw.getBytes(), 0, blen + 1);
}
Also used : BitWriter(uk.me.parabola.imgfmt.app.BitWriter) Label(uk.me.parabola.imgfmt.app.Label)

Example 5 with BitWriter

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

the class NumberPreparerTest method writeAndRead.

private List<Numbers> writeAndRead(List<Numbers> numbers) {
    NumberPreparer preparer = new NumberPreparer(numbers);
    BitWriter bw = preparer.fetchBitStream();
    bytesUsed += bw.getLength();
    assertTrue("check valid flag", preparer.isValid());
    boolean swapped = preparer.getSwapped();
    // Now read it all back in again
    byte[] b1 = bw.getBytes();
    byte[] bytes = new byte[bw.getLength()];
    System.arraycopy(b1, 0, bytes, 0, bw.getLength());
    BitReader br = new BitReader(bytes);
    NumberReader nr = new NumberReader(br);
    nr.setNumberOfNodes(numbers.get(numbers.size() - 1).getIndex() + 1);
    List<Numbers> list = nr.readNumbers(swapped);
    for (Numbers n : list) n.setNodeNumber(n.getIndex());
    return list;
}
Also used : BitWriter(uk.me.parabola.imgfmt.app.BitWriter) NumberReader(func.lib.NumberReader) BitReader(uk.me.parabola.imgfmt.app.BitReader)

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