use of uk.me.parabola.imgfmt.app.BitWriter in project mkgmap by openstreetmap.
the class RoadDefTest method testBitArray.
/**
* Compares the result of the loop in writeNod2 with using a BitWriter.
*
* You have to copy the code to here for this test to mean anything.
*/
@Test
public void testBitArray() {
Random r = new Random();
final int MAX_BITS = 26;
boolean[] bits = new boolean[MAX_BITS];
for (int i = 0; i < bits.length; i++) bits[i] = r.nextBoolean();
ByteArrayOutputStream writer = new ByteArrayOutputStream();
// This is the loop taken from the code
for (int i = 0; i < bits.length; i += 8) {
int b = 0;
for (int j = 0; j < 8 && j < bits.length - i; j++) if (bits[i + j])
b |= 1 << j;
writer.write((byte) b);
}
// End of loop
BitWriter bw = new BitWriter();
for (boolean b : bits) bw.put1(b);
byte[] loopResult = writer.toByteArray();
// Get the bit writer result and trim the array to the correct size
byte[] bwResult = new byte[bw.getLength()];
System.arraycopy(bw.getBytes(), 0, bwResult, 0, bwResult.length);
assertArrayEquals("Loop against bitwriter", bwResult, loopResult);
}
use of uk.me.parabola.imgfmt.app.BitWriter in project mkgmap by openstreetmap.
the class NumberRangeTest method run.
private void run(String[] strings) {
List<Numbers> numbers = new ArrayList<Numbers>();
for (String s : strings) {
Numbers n = new Numbers(s);
n.setIndex(n.getNodeNumber());
numbers.add(n);
}
NumberPreparer np = new NumberPreparer(numbers);
BitWriter bitWriter = np.fetchBitStream();
bytesUsed += bitWriter.getLength();
// Now read it back in
byte[] bytes = new byte[bitWriter.getLength()];
System.arraycopy(bitWriter.getBytes(), 0, bytes, 0, bytes.length);
NumberReader nr = new NumberReader(new BitReader(bytes));
nr.setNumberOfNodes(numbers.get(numbers.size() - 1).getIndex() + 1);
List<Numbers> list = nr.readNumbers(np.getSwapped());
// Have to fix up the node numbers
for (Numbers n : list) {
n.setNodeNumber(n.getIndex());
}
// Test that they are the same.
String orig = numbers.toString();
String calculated = list.toString();
if (!orig.equals(calculated)) {
System.out.printf("Fail: expecting: %s\n Got: %s\n", orig, calculated);
}
}
use of uk.me.parabola.imgfmt.app.BitWriter in project mkgmap by openstreetmap.
the class CityZipWriter method fetchBitStream.
/**
* Make the bit stream and return it. This is only done once, if you call this several times
* the same bit writer is returned every time.
* @return A bit writer containing the computed house number stream.
*/
public BitWriter fetchBitStream() {
if (bw != null)
return bw;
int initialValue = setup();
// Write the bitstream
bw = new BitWriter();
try {
// Look at the numbers and calculate some optimal values for the bit field widths etc.
State state = new GatheringState(initialValue);
process(new BitWriter(), state);
// Write the initial values.
writeWidths(state);
writeInitialValue(state);
state = new WritingState(state);
process(bw, state);
// If we get this far and there is something there, the stream might be valid!
if (bw.getLength() > 1)
valid = true;
} catch (Abandon e) {
log.error(e.getMessage());
valid = false;
}
return bw;
}
use of uk.me.parabola.imgfmt.app.BitWriter in project mkgmap by openstreetmap.
the class RoadDef method writeNet1.
/**
* This is for writing to NET1.
* @param writer A writer that is positioned within NET1.
*/
void writeNet1(ImgFileWriter writer, int numCities, int numZips) {
if (numlabels == 0)
return;
assert numlabels > 0;
Zip zip = getZips().isEmpty() ? null : getZips().get(0);
City city = getCities().isEmpty() ? null : getCities().get(0);
offsetNet1 = writer.position();
NumberPreparer numbers = null;
if (numbersList != null) {
numbers = new NumberPreparer(numbersList, zip, city, numCities, numZips);
if (!numbers.prepare()) {
numbers = null;
log.warn("Invalid housenumbers in", this.toString());
}
}
writeLabels(writer);
if (numbers != null && numbers.getSwapped()) {
// swapped default; left=even, right=odd
netFlags |= 0x20;
}
writer.put((byte) netFlags);
writer.put3(roadLength);
int maxlevel = writeLevelCount(writer);
writeLevelDivs(writer, maxlevel);
if ((netFlags & NET_FLAG_ADDRINFO) != 0) {
nodeCount--;
if (nodeCount + 2 != nnodes) {
log.error("internal error? The nodeCount doesn't match value calculated by RoadNetWork:", this);
}
// lo bits of node count
writer.put((byte) (nodeCount & 0xff));
// top bits of node count
int code = ((nodeCount >> 8) & 0x3);
int len, flag;
ByteArrayOutputStream zipBuf = null, cityBuf = null;
len = (numbers == null) ? 0 : numbers.zipWriter.getBuffer().size();
if (len > 0) {
zipBuf = numbers.zipWriter.getBuffer();
flag = Utils.numberToPointerSize(len) - 1;
} else
flag = (zip == null) ? 3 : 2;
code |= flag << 2;
len = (numbers == null) ? 0 : numbers.cityWriter.getBuffer().size();
if (len > 0) {
cityBuf = numbers.cityWriter.getBuffer();
flag = Utils.numberToPointerSize(len) - 1;
} else
flag = (city == null) ? 3 : 2;
code |= flag << 4;
len = (numbers == null) ? 0 : numbers.fetchBitStream().getLength();
if (len > 0) {
flag = Utils.numberToPointerSize(len) - 1;
} else
flag = 3;
code |= flag << 6;
writer.put((byte) code);
if (zipBuf != null) {
len = zipBuf.size();
writer.putN(Utils.numberToPointerSize(len), len);
writer.put(zipBuf.toByteArray());
} else {
if (zip != null) {
char zipIndex = (char) zip.getIndex();
writer.putN(Utils.numberToPointerSize(numZips), zipIndex);
}
}
if (cityBuf != null) {
len = cityBuf.size();
writer.putN(Utils.numberToPointerSize(len), len);
writer.put(cityBuf.toByteArray());
} else {
if (city != null) {
char cityIndex = (char) city.getIndex();
writer.putN(Utils.numberToPointerSize(numCities), cityIndex);
}
}
if (numbers != null) {
BitWriter bw = numbers.fetchBitStream();
writer.putN(Utils.numberToPointerSize(bw.getLength()), bw.getLength());
writer.put(bw.getBytes(), 0, bw.getLength());
}
}
if (hasNodInfo()) {
// This is the offset of an entry in NOD2
int val = offsetNod2;
if (val < 0x7fff) {
writer.put((byte) 1);
writer.putChar((char) val);
} else {
writer.put((byte) 2);
writer.put3(val);
}
}
}
use of uk.me.parabola.imgfmt.app.BitWriter in project mkgmap by openstreetmap.
the class LinePreparer method makeBitStream.
/**
* Write the bit stream to a BitWriter and return it.
*
* @return A class containing the written byte stream.
*/
public BitWriter makeBitStream(int minPointsRequired, int xb, int yb) {
assert xb >= 0 && yb >= 0;
int xbits = base2Bits(xb);
if (!xSameSign)
xbits++;
int ybits = base2Bits(yb);
if (!ySameSign)
ybits++;
// Note no sign included.
if (log.isDebugEnabled())
log.debug("xbits", xbits, ", y=", ybits);
// Write the bitstream
BitWriter bw = new BitWriter();
// Pre bit stream info
bw.putn(xb, 4);
bw.putn(yb, 4);
bw.put1(xSameSign);
if (xSameSign)
bw.put1(xSignNegative);
bw.put1(ySameSign);
if (ySameSign)
bw.put1(ySignNegative);
if (log.isDebugEnabled()) {
log.debug("x same is", xSameSign, "sign is", xSignNegative);
log.debug("y same is", ySameSign, "sign is", ySignNegative);
}
if (extTypeLine) {
// no extra bits required
bw.put1(false);
}
// refers to the start point?
if (extraBit)
bw.put1(false);
int numPointsEncoded = 1;
for (int i = 0; i < deltas.length; i += 2) {
int dx = deltas[i];
int dy = deltas[i + 1];
if (dx == 0 && dy == 0) {
if (// don't skip CoordNode
extraBit && nodes[i / 2 + 1] == false && i + 2 != deltas.length)
continue;
}
++numPointsEncoded;
if (log.isDebugEnabled())
log.debug("x delta", dx, "~", xbits);
if (xSameSign) {
bw.putn(Math.abs(dx), xbits);
} else {
bw.sputn(dx, xbits);
}
if (log.isDebugEnabled())
log.debug("y delta", dy, ybits);
if (ySameSign) {
bw.putn(Math.abs(dy), ybits);
} else {
bw.sputn(dy, ybits);
}
if (extraBit)
bw.put1(nodes[i / 2 + 1]);
}
if (log.isDebugEnabled())
log.debug(bw);
if (numPointsEncoded < minPointsRequired)
return null;
return bw;
}
Aggregations