| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app.trergn; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 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 |
|
| 45 |
|
| 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 |
|
| 60 |
|
| 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 |
|
| 74 |
|
| 75 |
calcThird(); |
| 76 |
calcFourth(); |
| 77 |
calcFirst(); |
| 78 |
calcSecond(); |
| 79 |
|
| 80 |
addOffset(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private void addOffset() { |
| 87 |
|
| 88 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
private void calcFirst() { |
| 104 |
byte[] out = values[0]; |
| 105 |
byte[] v3 = values[3]; |
| 106 |
|
| 107 |
|
| 108 |
|
| 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 |
|
| 115 |
out[4] = v3[4]; |
| 116 |
out[5] = v3[5]; |
| 117 |
out[6] = v3[6]; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
out[7] = (byte) (v3[7] + 1); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private void calcSecond() { |
| 130 |
byte[] out = values[1]; |
| 131 |
byte[] v3 = values[3]; |
| 132 |
|
| 133 |
|
| 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 |
|
| 143 |
|
| 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 |
|
| 152 |
|
| 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 |
|
| 164 |
|
| 165 |
private void calcFourth() { |
| 166 |
System.arraycopy(values[2], 0, values[3], 0, values[3].length); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
private int mapIdDigit(int i) { |
| 175 |
return (mapId >>> (4 * (7 - i))) & 0xf; |
| 176 |
} |
| 177 |
} |