File: NumberReader.java

package info (click to toggle)
mkgmap 0.0.0%2Bsvn4905-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,032 kB
  • sloc: java: 73,856; xml: 1,695; python: 713; sh: 240; makefile: 149; perl: 31
file content (479 lines) | stat: -rw-r--r-- 12,133 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
 * Copyright (C) 2008 Steve Ratcliffe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 or
 * version 2 as published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 */
package func.lib;

import java.util.ArrayList;
import java.util.List;

import uk.me.parabola.imgfmt.app.BitReader;
import uk.me.parabola.imgfmt.app.net.NumberStyle;
import uk.me.parabola.imgfmt.app.net.Numbers;

import static uk.me.parabola.imgfmt.app.net.NumberStyle.*;


/**
 * This is a test reader of the numbering streams. Since there are multiple ways of writing
 * the same set of house numbers, the only reasonable way of testing the write process is to
 * read the bit stream back and compare with the intended numbers.
 *
 * There is no attempt at efficiency given it is for testing, but it is believed to correctly
 * read numbers from any map.
 *
 * This code is derived directly from the NetDisplay class in the display project, so see that
 * to see the development of this file.
 * The algorithm that is required to read the bit stream was partly derived by studying the
 * the released GPL code of cGPSmapper by Stanislaw Kozicki.
 *
 * @author Steve Ratcliffe
 */
public class NumberReader {

	private final BitReader br;

	// For reading the start differences and end difference numbers.
	private VarBitReader startReader;
	private VarBitReader endReader;
	private VarBitReader savedStartReader;
	private VarBitReader savedEndReader;
	private boolean doRestoreBitWidths;

	// base numbers
	private int leftBase;
	private int rightBase;

	// numbering styles
	private NumberStyle leftStyle = ODD;
	private NumberStyle rightStyle = EVEN;

	// start numbers
	private int leftStart;
	private int rightStart;

	// end numbers
	private int leftEnd;
	private int rightEnd;

	// saved end numbers
	private int leftLastEndDiff;
	private int rightLastEndDiff;

	// Numbers are a range between nodes. Keep count of them here
	private int nodeCounter;
	private int numberOfNodes;

	public NumberReader(BitReader br) {
		this.br = br;
	}

	public void setNumberOfNodes(int numberOfNodes) {
		this.numberOfNodes = numberOfNodes;
	}

	/**
	 * Read the numbers into a list of Numbers classes.
	 * @param swap If the default starting position of left=ODD right=EVEN should be swapped.
	 * @return A list of the numbers that the input stream represents.
	 */
	public List<Numbers> readNumbers(boolean swap) {
		if (swap) {
			leftStyle = EVEN;
			rightStyle = ODD;
		}

		getBitWidths();

		getInitialBase();

		List<Numbers> numbers = new ArrayList<>();

		// To do this properly we need to know the number of nodes I think, this is the
		// best we can do: if there are more than 8 bits left, there must be another command
		// left.  We could leave a short command at the end.
		while (nodeCounter < numberOfNodes/* + 1*/) {
			try {
			runCommand(numbers);
			} catch (NumberException | ArrayIndexOutOfBoundsException e) {
				System.out.printf("collected %d, wanted %d\n", numbers.size(), numberOfNodes+1);
		return numbers;
	}
		}

		return numbers;
	}


	/**
	 * Get the bit widths for the start and end differences.
	 * Based on code for reading the RGN streams, but the signed bit is the
	 * opposite value.
	 * x is for start value differences.  y is for end value differences.
	 */
	private void getBitWidths() {
		startReader = new VarBitReader(br, 5);
		endReader = new VarBitReader(br, 2);
	}

	/**
	 * Decode the next command in the stream and run it.
	 * @param numbers When numbers are read, they are saved here.
	 */
	private void runCommand(List<Numbers> numbers) throws NumberException {
		int cmd = readCommand(); // fetch 1, 3 skip, 2 reload, 0 style

		switch (cmd) {
		case 0:
			changeStyles();
			break;
		case 1:
			fetchNumbers(numbers);
			break;
		case 2:
			useBits();
			break;
		case 6:
			skipNodes();
			break;
		default:
			fail("unimplemented command: " + cmd);
		}
	}

	/**
	 * Temporarily use a different bit width for the following number fetch.
	 */
	private void useBits() {
		if (!doRestoreBitWidths) {
			savedStartReader = startReader;
			savedEndReader = endReader;
		}
		doRestoreBitWidths = true;

		if (br.get1()) {
			endReader = new VarBitReader(br, 2);
		} else {
			startReader = new VarBitReader(br, 5);
		}
	}

	/**
	 * Skip nodes. For parts of a road that has no numbers.
	 */
	private void skipNodes() {
		boolean f = br.get1();
		int skip;
		if (f)
			skip = 1 + br.get(10);
		else
			skip = 1 + br.get(5);
		nodeCounter += skip;
	}

	/**
	 * Read the next command from the stream. Commands are variable length in the bit
	 * stream.
	 * 0 - numbering style (none, odd, even, both)
	 * 1 - fetch numbers
	 * 2 - change bit widths
	 * 6 - skip nodes
	 * @return The command number
	 */
	private int readCommand() {
		int cmd = 0;
		if (br.get1()) {
			cmd |= 0x1;
		} else {
			if (br.get1()) {
				cmd |= 0x2;
				if (br.get1()) {
					cmd |= 0x4;
				}
			}
		}
		return cmd;
	}

	/**
	 * Read the house numbers for a stretch of road.
	 *
	 * The start and end positions of the the left hand side of the road is first, followed
	 * by the right hand side of the road.
	 *
	 * The differences to the last point are stored. It is also possible to
	 * @param numbers When numbers are read, they are saved here.
	 */
	private void fetchNumbers(List<Numbers> numbers) {

		// If one side has no numbers, then there is only one set of numbers to calculate, but
		// changes to base are applied to both sides.
		boolean doSingleSide = (leftStyle == NONE || rightStyle == NONE);

		if (leftStyle == NONE)
			leftBase = rightBase;

		// Check for command to copy the base number
		boolean doSameBase = false;
		if (!doSingleSide) {
			doSameBase = br.get1();
			if (doSameBase)
				copyBase();
		}

		//int abc = br.get(3);
		boolean doRightOverride = false;
		if (!doSingleSide)
			doRightOverride = !br.get1();
		boolean doReadStart = !br.get1();
		boolean doReadEnd = !br.get1();

		//item.addText("cmd: fetch numbers abc: %x", abc);

		int startDiff = 0, endDiff = leftLastEndDiff;

		if (doReadStart) {
			startDiff = startReader.read();
		}
		if (doReadEnd) {
			endDiff = endReader.read();
		}

		leftStart = leftBase + startDiff;
		leftEnd = leftStart + endDiff;

		leftBase = leftEnd;
		leftLastEndDiff = endDiff;

		if (doSingleSide) {
			readSingleSide(numbers);
			restoreReaders();
			return;
		}

		// *** Now for the right hand side numbers ***

		// Note that endDiff falls through to this part
		// start diff falls through at least when doSameBase is in force
		if (!doSameBase)
			startDiff = 0;

		// If we didn't read an endDiff value for the left side or right is different then
		// default to the saved value.
		if (doRightOverride || !doReadEnd)
			endDiff = rightLastEndDiff;

		doReadStart = false;
		doReadEnd = false;

		if (!doSameBase)
			doReadStart = !br.get1();

		if (doRightOverride)
			doReadEnd = !br.get1();

		if (doReadStart)
			startDiff = startReader.read();

		if (doReadEnd)
			endDiff = endReader.read();

		rightStart = rightBase + startDiff;
		rightEnd = rightStart + endDiff;

		rightBase = rightEnd;
		rightLastEndDiff = endDiff;

		adjustValues();

		Numbers n = new Numbers();
		n.setIndex(nodeCounter);
		n.setNumbers(Numbers.LEFT, leftStyle, leftStart, leftEnd);
		n.setNumbers(Numbers.RIGHT, rightStyle, rightStart, rightEnd);

		numbers.add(n);
		nodeCounter++;

		restoreReaders();
	}

	/**
	 * After a temporary bit width change.
	 */
	private void restoreReaders() {
		if (doRestoreBitWidths) {
			startReader = savedStartReader;
			endReader = savedEndReader;
			doRestoreBitWidths = false;
		}
	}

	/**
	 * If the road has numbers on just one side, then there is a shortened reading routine.
	 * The left variables are mostly used during reading regardless of which side of the
	 * road has numbers. Make everything work here.
	 * @param numbers The output list that the number record should be added to.
	 */
	private void readSingleSide(List<Numbers> numbers) {
		rightBase = leftBase;
		rightStart = leftStart;
		rightEnd = leftEnd;
		rightLastEndDiff = leftLastEndDiff;
		adjustValues();

		Numbers n = new Numbers();
		n.setIndex(nodeCounter);
		if (leftStyle == NONE) 
			n.setNumbers(Numbers.RIGHT, rightStyle, rightStart, rightEnd);
		else 
			n.setNumbers(Numbers.LEFT, leftStyle, leftStart, leftEnd);
		numbers.add(n);
		nodeCounter++;
	}

	/**
	 * When it is known if the numbers are odd or even, then a shorter bitstream is made
	 * by taking advantage of that fact. This leaves the start and end points needing
	 * adjustment to made them odd or even as appropriate.
	 */
	private void adjustValues() {
		int ldirection = 1; // direction start is adjusted in; end in the opposite direction.
		if (leftStart < leftEnd)
			leftEnd--;
		else if (leftStart > leftEnd) {
			leftEnd++;
			ldirection = -1;
		}

		int rdirection = 1; // direction start is adjusted in; end in the opposite direction.
		if (rightStart < rightEnd)
			rightEnd--;
		else if (rightStart > rightEnd) {
			rightEnd++;
			rdirection = -1;
		}

		if (leftStyle == EVEN) {
			if ((leftStart & 1) == 1) leftStart += ldirection;
			if ((leftEnd & 1) == 1) leftEnd -= ldirection;
		} else if (leftStyle == ODD) {
			if ((leftStart & 1) == 0) leftStart+=ldirection;
			if ((leftEnd & 1) == 0) leftEnd-=ldirection;
		}
		if (rightStyle == EVEN) {
			if ((rightStart & 1) == 1) rightStart+=rdirection;
			if ((rightEnd & 1) == 1) rightEnd-=rdirection;
		} else if (rightStyle == ODD) {
			if ((rightStart & 1) == 0) rightStart+=rdirection;
			if ((rightEnd & 1) == 0) rightEnd-=rdirection;
		}
	}

	/**
	 * Copy one of the bases to the other so they have the same value.
	 * The source is determined by reading a bit from the input.
	 */
	private void copyBase() {
		boolean f2 = br.get1();
		if (f2) {
			rightBase = leftBase;
		} else {
			leftBase = rightBase;
		}
	}

	/**
	 * Change the numbering styles for this section of roads.
	 */
	private void changeStyles() {
		leftStyle = fromInt(br.get(2));
		rightStyle = fromInt(br.get(2));
	}

	/**
	 * Get the initial base value. The first number for this section of road (although a diff
	 * can be applied to it).
	 *
	 * @throws NumberException
	 */
	private void getInitialBase() {
		int extra = 0;
		boolean b1 = br.get1();
		if (!b1)
			extra = br.get(4);

		leftBase = br.get(5 + extra);
		rightBase = leftBase;
	}

	/**
	 * For cases that are not implemented yet.
	 */
	private void fail(String s) throws NumberException {
		System.out.printf("ABANDON: %s\n", s);
		remainingBits();
		throw new NumberException();
	}

	/**
	 * Just print out any remaining bits.
	 *
	 * Was mostly used during development, before the whole stream was decoded.
	 */
	private void remainingBits() {
		StringBuilder sb = new StringBuilder();
		while (br.getBitPosition() < br.getNumberOfBits()) {
			sb.insert(0, br.get1() ? "1" : "0");
		}
		System.out.print(sb.toString());
	}

}

/**
 * Reads integers with specified numbers of bits and optionally with sign bits.
 */
class VarBitReader {
	private final boolean signed;   // read as signed values
	private final boolean negative; // all values are read as positive and then negated
	private final int width;        // the number of bits
	private final int off;    // a value to be added to width to get the true number to read.
	private final BitReader br;

	public VarBitReader(BitReader br, int off) {
		this.br = br;
		this.off = off;
		negative = br.get1();
		signed = br.get1();
		width = br.get(4);
	}

	public int read() {
		int val;
		if (signed) {
			val = br.sget(width + off + 1);
		} else {
			val = br.get(width + off);
		}

		if (negative)
			val = -val;
		return val;
	}

	public String toString() {
		return String.format("sign=%b neg=%b width=%d+%d", signed, negative, width, off);
	}
}


class NumberException extends RuntimeException {
}