Debian Codegraph @

test/func/lib/NumberReader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13 package func.lib;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import uk.me.parabola.imgfmt.app.BitReader;
19 import uk.me.parabola.imgfmt.app.net.NumberStyle;
20 import uk.me.parabola.imgfmt.app.net.Numbers;
21
22 import static uk.me.parabola.imgfmt.app.net.NumberStyle.*;
23
24
25 /**
26 * This is a test reader of the numbering streams. Since there are multiple ways of writing
27 * the same set of house numbers, the only reasonable way of testing the write process is to
28 * read the bit stream back and compare with the intended numbers.
29 *
30 * There is no attempt at efficiency given it is for testing, but it is believed to correctly
31 * read numbers from any map.
32 *
33 * This code is derived directly from the NetDisplay class in the display project, so see that
34 * to see the development of this file.
35 * The algorithm that is required to read the bit stream was partly derived by studying the
36 * the released GPL code of cGPSmapper by Stanislaw Kozicki.
37 *
38 * @author Steve Ratcliffe
39 */
40 public class NumberReader {
41
42 private final BitReader br;
43
44 // For reading the start differences and end difference numbers.
45 private VarBitReader startReader;
46 private VarBitReader endReader;
47 private VarBitReader savedStartReader;
48 private VarBitReader savedEndReader;
49 private boolean doRestoreBitWidths;
50
51 // base numbers
52 private int leftBase;
53 private int rightBase;
54
55 // numbering styles
56 private NumberStyle leftStyle = ODD;
57 private NumberStyle rightStyle = EVEN;
58
59 // start numbers
60 private int leftStart;
61 private int rightStart;
62
63 // end numbers
64 private int leftEnd;
65 private int rightEnd;
66
67 // saved end numbers
68 private int leftLastEndDiff;
69 private int rightLastEndDiff;
70
71 // Numbers are a range between nodes. Keep count of them here
72 private int nodeCounter;
73 private int numberOfNodes;
74
75 public NumberReader(BitReader br) {
76 this.br = br;
77 }
78
79 public void setNumberOfNodes(int numberOfNodes) {
80 this.numberOfNodes = numberOfNodes;
81 }
82
83 /**
84 * Read the numbers into a list of Numbers classes.
85 * @param swap If the default starting position of left=ODD right=EVEN should be swapped.
86 * @return A list of the numbers that the input stream represents.
87 */
88 public List<Numbers> readNumbers(boolean swap) {
89 if (swap) {
90 leftStyle = EVEN;
91 rightStyle = ODD;
92 }
93
94 getBitWidths();
95
96 getInitialBase();
97
98 List<Numbers> numbers = new ArrayList<>();
99
100 // To do this properly we need to know the number of nodes I think, this is the
101 // best we can do: if there are more than 8 bits left, there must be another command
102 // left. We could leave a short command at the end.
103 while (nodeCounter < numberOfNodes/* + 1*/) {
104 try {
105 runCommand(numbers);
106 } catch (NumberException | ArrayIndexOutOfBoundsException e) {
107 System.out.printf("collected %d, wanted %d\n", numbers.size(), numberOfNodes+1);
108 return numbers;
109 }
110 }
111
112 return numbers;
113 }
114
115
116 /**
117 * Get the bit widths for the start and end differences.
118 * Based on code for reading the RGN streams, but the signed bit is the
119 * opposite value.
120 * x is for start value differences. y is for end value differences.
121 */
122 private void getBitWidths() {
123 startReader = new VarBitReader(br, 5);
124 endReader = new VarBitReader(br, 2);
125 }
126
127 /**
128 * Decode the next command in the stream and run it.
129 * @param numbers When numbers are read, they are saved here.
130 */
131 private void runCommand(List<Numbers> numbers) throws NumberException {
132 int cmd = readCommand(); // fetch 1, 3 skip, 2 reload, 0 style
133
134 switch (cmd) {
135 case 0:
136 changeStyles();
137 break;
138 case 1:
139 fetchNumbers(numbers);
140 break;
141 case 2:
142 useBits();
143 break;
144 case 6:
145 skipNodes();
146 break;
147 default:
148 fail("unimplemented command: " + cmd);
149 }
150 }
151
152 /**
153 * Temporarily use a different bit width for the following number fetch.
154 */
155 private void useBits() {
156 if (!doRestoreBitWidths) {
157 savedStartReader = startReader;
158 savedEndReader = endReader;
159 }
160 doRestoreBitWidths = true;
161
162 if (br.get1()) {
163 endReader = new VarBitReader(br, 2);
164 } else {
165 startReader = new VarBitReader(br, 5);
166 }
167 }
168
169 /**
170 * Skip nodes. For parts of a road that has no numbers.
171 */
172 private void skipNodes() {
173 boolean f = br.get1();
174 int skip;
175 if (f)
176 skip = 1 + br.get(10);
177 else
178 skip = 1 + br.get(5);
179 nodeCounter += skip;
180 }
181
182 /**
183 * Read the next command from the stream. Commands are variable length in the bit
184 * stream.
185 * 0 - numbering style (none, odd, even, both)
186 * 1 - fetch numbers
187 * 2 - change bit widths
188 * 6 - skip nodes
189 * @return The command number
190 */
191 private int readCommand() {
192 int cmd = 0;
193 if (br.get1()) {
194 cmd |= 0x1;
195 } else {
196 if (br.get1()) {
197 cmd |= 0x2;
198 if (br.get1()) {
199 cmd |= 0x4;
200 }
201 }
202 }
203 return cmd;
204 }
205
206 /**
207 * Read the house numbers for a stretch of road.
208 *
209 * The start and end positions of the the left hand side of the road is first, followed
210 * by the right hand side of the road.
211 *
212 * The differences to the last point are stored. It is also possible to
213 * @param numbers When numbers are read, they are saved here.
214 */
215 private void fetchNumbers(List<Numbers> numbers) {
216
217 // If one side has no numbers, then there is only one set of numbers to calculate, but
218 // changes to base are applied to both sides.
219 boolean doSingleSide = (leftStyle == NONE || rightStyle == NONE);
220
221 if (leftStyle == NONE)
222 leftBase = rightBase;
223
224 // Check for command to copy the base number
225 boolean doSameBase = false;
226 if (!doSingleSide) {
227 doSameBase = br.get1();
228 if (doSameBase)
229 copyBase();
230 }
231
232 //int abc = br.get(3);
233 boolean doRightOverride = false;
234 if (!doSingleSide)
235 doRightOverride = !br.get1();
236 boolean doReadStart = !br.get1();
237 boolean doReadEnd = !br.get1();
238
239 //item.addText("cmd: fetch numbers abc: %x", abc);
240
241 int startDiff = 0, endDiff = leftLastEndDiff;
242
243 if (doReadStart) {
244 startDiff = startReader.read();
245 }
246 if (doReadEnd) {
247 endDiff = endReader.read();
248 }
249
250 leftStart = leftBase + startDiff;
251 leftEnd = leftStart + endDiff;
252
253 leftBase = leftEnd;
254 leftLastEndDiff = endDiff;
255
256 if (doSingleSide) {
257 readSingleSide(numbers);
258 restoreReaders();
259 return;
260 }
261
262 // *** Now for the right hand side numbers ***
263
264 // Note that endDiff falls through to this part
265 // start diff falls through at least when doSameBase is in force
266 if (!doSameBase)
267 startDiff = 0;
268
269 // If we didn't read an endDiff value for the left side or right is different then
270 // default to the saved value.
271 if (doRightOverride || !doReadEnd)
272 endDiff = rightLastEndDiff;
273
274 doReadStart = false;
275 doReadEnd = false;
276
277 if (!doSameBase)
278 doReadStart = !br.get1();
279
280 if (doRightOverride)
281 doReadEnd = !br.get1();
282
283 if (doReadStart)
284 startDiff = startReader.read();
285
286 if (doReadEnd)
287 endDiff = endReader.read();
288
289 rightStart = rightBase + startDiff;
290 rightEnd = rightStart + endDiff;
291
292 rightBase = rightEnd;
293 rightLastEndDiff = endDiff;
294
295 adjustValues();
296
297 Numbers n = new Numbers();
298 n.setIndex(nodeCounter);
299 n.setNumbers(Numbers.LEFT, leftStyle, leftStart, leftEnd);
300 n.setNumbers(Numbers.RIGHT, rightStyle, rightStart, rightEnd);
301
302 numbers.add(n);
303 nodeCounter++;
304
305 restoreReaders();
306 }
307
308 /**
309 * After a temporary bit width change.
310 */
311 private void restoreReaders() {
312 if (doRestoreBitWidths) {
313 startReader = savedStartReader;
314 endReader = savedEndReader;
315 doRestoreBitWidths = false;
316 }
317 }
318
319 /**
320 * If the road has numbers on just one side, then there is a shortened reading routine.
321 * The left variables are mostly used during reading regardless of which side of the
322 * road has numbers. Make everything work here.
323 * @param numbers The output list that the number record should be added to.
324 */
325 private void readSingleSide(List<Numbers> numbers) {
326 rightBase = leftBase;
327 rightStart = leftStart;
328 rightEnd = leftEnd;
329 rightLastEndDiff = leftLastEndDiff;
330 adjustValues();
331
332 Numbers n = new Numbers();
333 n.setIndex(nodeCounter);
334 if (leftStyle == NONE)
335 n.setNumbers(Numbers.RIGHT, rightStyle, rightStart, rightEnd);
336 else
337 n.setNumbers(Numbers.LEFT, leftStyle, leftStart, leftEnd);
338 numbers.add(n);
339 nodeCounter++;
340 }
341
342 /**
343 * When it is known if the numbers are odd or even, then a shorter bitstream is made
344 * by taking advantage of that fact. This leaves the start and end points needing
345 * adjustment to made them odd or even as appropriate.
346 */
347 private void adjustValues() {
348 int ldirection = 1; // direction start is adjusted in; end in the opposite direction.
349 if (leftStart < leftEnd)
350 leftEnd--;
351 else if (leftStart > leftEnd) {
352 leftEnd++;
353 ldirection = -1;
354 }
355
356 int rdirection = 1; // direction start is adjusted in; end in the opposite direction.
357 if (rightStart < rightEnd)
358 rightEnd--;
359 else if (rightStart > rightEnd) {
360 rightEnd++;
361 rdirection = -1;
362 }
363
364 if (leftStyle == EVEN) {
365 if ((leftStart & 1) == 1) leftStart += ldirection;
366 if ((leftEnd & 1) == 1) leftEnd -= ldirection;
367 } else if (leftStyle == ODD) {
368 if ((leftStart & 1) == 0) leftStart+=ldirection;
369 if ((leftEnd & 1) == 0) leftEnd-=ldirection;
370 }
371 if (rightStyle == EVEN) {
372 if ((rightStart & 1) == 1) rightStart+=rdirection;
373 if ((rightEnd & 1) == 1) rightEnd-=rdirection;
374 } else if (rightStyle == ODD) {
375 if ((rightStart & 1) == 0) rightStart+=rdirection;
376 if ((rightEnd & 1) == 0) rightEnd-=rdirection;
377 }
378 }
379
380 /**
381 * Copy one of the bases to the other so they have the same value.
382 * The source is determined by reading a bit from the input.
383 */
384 private void copyBase() {
385 boolean f2 = br.get1();
386 if (f2) {
387 rightBase = leftBase;
388 } else {
389 leftBase = rightBase;
390 }
391 }
392
393 /**
394 * Change the numbering styles for this section of roads.
395 */
396 private void changeStyles() {
397 leftStyle = fromInt(br.get(2));
398 rightStyle = fromInt(br.get(2));
399 }
400
401 /**
402 * Get the initial base value. The first number for this section of road (although a diff
403 * can be applied to it).
404 *
405 * @throws NumberException
406 */
407 private void getInitialBase() {
408 int extra = 0;
409 boolean b1 = br.get1();
410 if (!b1)
411 extra = br.get(4);
412
413 leftBase = br.get(5 + extra);
414 rightBase = leftBase;
415 }
416
417 /**
418 * For cases that are not implemented yet.
419 */
420 private void fail(String s) throws NumberException {
421 System.out.printf("ABANDON: %s\n", s);
422 remainingBits();
423 throw new NumberException();
424 }
425
426 /**
427 * Just print out any remaining bits.
428 *
429 * Was mostly used during development, before the whole stream was decoded.
430 */
431 private void remainingBits() {
432 StringBuilder sb = new StringBuilder();
433 while (br.getBitPosition() < br.getNumberOfBits()) {
434 sb.insert(0, br.get1() ? "1" : "0");
435 }
436 System.out.print(sb.toString());
437 }
438
439 }
440
441 /**
442 * Reads integers with specified numbers of bits and optionally with sign bits.
443 */
444 class VarBitReader {
445 private final boolean signed; // read as signed values
446 private final boolean negative; // all values are read as positive and then negated
447 private final int width; // the number of bits
448 private final int off; // a value to be added to width to get the true number to read.
449 private final BitReader br;
450
451 public VarBitReader(BitReader br, int off) {
452 this.br = br;
453 this.off = off;
454 negative = br.get1();
455 signed = br.get1();
456 width = br.get(4);
457 }
458
459 public int read() {
460 int val;
461 if (signed) {
462 val = br.sget(width + off + 1);
463 } else {
464 val = br.get(width + off);
465 }
466
467 if (negative)
468 val = -val;
469 return val;
470 }
471
472 public String toString() {
473 return String.format("sign=%b neg=%b width=%d+%d", signed, negative, width, off);
474 }
475 }
476
477
478 class NumberException extends RuntimeException {
479 }