Debian Codegraph @

src/uk/me/parabola/imgfmt/app/trergn/MapValues.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 2 as
6 * 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 *
14 * Author: Steve Ratcliffe
15 * Create date: 18-Jun-2008
16 */
17 package uk.me.parabola.imgfmt.app.trergn;
18
19 /**
20 * Class to calculate the values that occur at offset 9a in the TRE header.
21 * As I don't know the purpose of these the naming is a bit arbitrary here...
22 *
23 * This was worked out in the display project, so see the TreCalc file
24 * there for more history. This is a cleaned up version of what was
25 * written there.
26 *
27 * @author Steve Ratcliffe
28 * @see <a href="http://svn.parabola.me.uk/display/trunk/src/test/display/TreCalc.java">TreCalc.java</a>
29 */
30 public class MapValues {
31 private final int mapId;
32 private final int length;
33
34 private final byte[][] values = new byte[4][8];
35
36 // Converts the digits in the map id to the values seen in this section.
37 private static final byte[] mapIdCodeTable = {
38 0, 1, 0xf, 5,
39 0xd, 4, 7, 6,
40 0xb, 9, 0xe, 8,
41 2, 0xa, 0xc, 3
42 };
43
44 // Used to work out the required offset that is applied to all the
45 // digits of the values.
46 private final int[] offsetMap = {
47 6, 7, 5, 11,
48 3, 10, 13, 12,
49 1, 15, 4, 14,
50 8, 0, 2, 9
51 };
52
53 public MapValues(int mapId, int headerLength) {
54 this.mapId = mapId;
55 this.length = headerLength;
56 }
57
58 /**
59 * There are four values. Get value n.
60 * @param n Get value n, starting at 0 up to four.
61 */
62 public int value(int n) {
63 byte[] out = values[n];
64
65 int res = 0;
66 for (int i = 0; i < 8; i++) {
67 res |= ((out[i] & 0xf) << (4 * (7 - i)));
68 }
69 return res;
70 }
71
72 public void calculate() {
73 // Done in this order because the first and second depend on things
74 // we have already calculated in three.
75 calcThird();
76 calcFourth();
77 calcFirst();
78 calcSecond();
79
80 addOffset();
81 }
82
83 /**
84 * Add an offset to all previously calculated values.
85 */
86 private void addOffset() {
87 // To get the offset value we add up all the even nibbles of the map
88 // number and transform via a table.
89 int n = mapIdDigit(1) + mapIdDigit(3) + mapIdDigit(5) + mapIdDigit(7);
90
91 int offset = offsetMap[n & 0xf];
92 for (int i = 0; i < 4; i++) {
93 for (int j = 0; j < 8; j++) {
94 values[i][j] += offset;
95 }
96 }
97 }
98
99 /**
100 * This value is made from the third value, combined with the raw
101 * map id values.
102 */
103 private void calcFirst() {
104 byte[] out = values[0];
105 byte[] v3 = values[3];
106
107 // First bytes are the low bytes of the mapId, with the corresponding
108 // value from value[3] added.
109 out[0] = (byte) (mapIdDigit(4) + v3[0]);
110 out[1] = (byte) (mapIdDigit(5) + v3[1]);
111 out[2] = (byte) (mapIdDigit(6) + v3[2]);
112 out[3] = (byte) (mapIdDigit(7) + v3[3]);
113
114 // Copies of v3
115 out[4] = v3[4];
116 out[5] = v3[5];
117 out[6] = v3[6];
118
119 // Always (?) one more. The one likely comes from some other
120 // part of the header, but we don't know if or where.
121 out[7] = (byte) (v3[7] + 1);
122 }
123
124 /**
125 * This is made from various parts of the third value and the raw digits
126 * from the map id. There are two digits where the header length digits
127 * are used (or that could be a coincidence, but it holds up well so far).
128 */
129 private void calcSecond() {
130 byte[] out = values[1];
131 byte[] v3 = values[3];
132
133 // Just same as in v3
134 out[0] = v3[0];
135 out[1] = v3[1];
136
137 int h1 = length >> 4;
138 int h2 = length;
139 out[2] = (byte) ((v3[2] + h1) & 0xf);
140 out[3] = (byte) ((v3[3] + h2) & 0xf);
141
142 // The following are the sum of individual nibbles in U3 and the
143 // corresponding nibble in the top half of mapId.
144 out[4] = (byte) (v3[4] + mapIdDigit(0));
145 out[5] = (byte) (v3[5] + mapIdDigit(1));
146 out[6] = (byte) (v3[6] + mapIdDigit(2));
147 out[7] = (byte) (v3[7] + mapIdDigit(3));
148 }
149
150 /**
151 * This is made of the hex digits of the map id in a given order
152 * translated according to a given table of values.
153 */
154 private void calcThird() {
155 byte[] out = values[2];
156 for (int i = 0; i < 8; i++) {
157 int n = mapIdDigit(i);
158 out[(i ^ 1)] = mapIdCodeTable[n];
159 }
160 }
161
162 /**
163 * This is just a copy of the third value.
164 */
165 private void calcFourth() {
166 System.arraycopy(values[2], 0, values[3], 0, values[3].length);
167 }
168
169 /**
170 * Extract the given nibble of the map id. 0 is the highest four bits.
171 * @param i The nibble number, 0 most significant, 7 the least.
172 * @return The given nibble of the map id.
173 */
174 private int mapIdDigit(int i) {
175 return (mapId >>> (4 * (7 - i))) & 0xf;
176 }
177 }