| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app.net; |
| 18 |
|
| 19 |
import java.util.Arrays; |
| 20 |
|
| 21 |
import uk.me.parabola.imgfmt.Utils; |
| 22 |
import uk.me.parabola.imgfmt.app.CommonHeader; |
| 23 |
import uk.me.parabola.imgfmt.app.ImgFileReader; |
| 24 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 25 |
import uk.me.parabola.imgfmt.app.Section; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class NODHeader extends CommonHeader { |
| 35 |
public static final int HEADER_LEN = 127; |
| 36 |
|
| 37 |
static final char DEF_ALIGN = 6; |
| 38 |
private static final char BOUNDARY_ITEM_SIZE = 9; |
| 39 |
|
| 40 |
private final Section nodes = new Section(); |
| 41 |
private final Section roads = new Section(nodes); |
| 42 |
private final Section boundary = new Section(roads, BOUNDARY_ITEM_SIZE); |
| 43 |
private final Section highClassBoundary = new Section(boundary); |
| 44 |
private final int[] classBoundaries = new int[5]; |
| 45 |
private final Section nod5 = new Section(); |
| 46 |
private final Section nod6 = new Section(); |
| 47 |
|
| 48 |
private int flags; |
| 49 |
private int align; |
| 50 |
private int mult1; |
| 51 |
private int tableARecordLen; |
| 52 |
private boolean driveOnLeft; |
| 53 |
|
| 54 |
private int indexIdSize; |
| 55 |
|
| 56 |
public NODHeader() { |
| 57 |
super(HEADER_LEN, "GARMIN NOD"); |
| 58 |
Arrays.fill(classBoundaries, Integer.MAX_VALUE); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected void readFileHeader(ImgFileReader reader) { |
| 69 |
nodes.readSectionInfo(reader, false); |
| 70 |
flags = reader.get2u(); |
| 71 |
reader.get2u(); |
| 72 |
align = reader.get1u(); |
| 73 |
mult1 = reader.get1u(); |
| 74 |
tableARecordLen = reader.get2u(); |
| 75 |
roads.readSectionInfo(reader, false, true); |
| 76 |
boundary.readSectionInfo(reader, true, true); |
| 77 |
if (getHeaderLength() > 0x3f) { |
| 78 |
highClassBoundary.readSectionInfo(reader, false); |
| 79 |
|
| 80 |
classBoundaries[0] = reader.get4(); |
| 81 |
classBoundaries[1] = classBoundaries[0] + reader.get4(); |
| 82 |
classBoundaries[2] = classBoundaries[1] + reader.get4(); |
| 83 |
classBoundaries[3] = classBoundaries[2] + reader.get4(); |
| 84 |
classBoundaries[4] = classBoundaries[3] + reader.get4(); |
| 85 |
} |
| 86 |
if (getHeaderLength() >= 0x7f) { |
| 87 |
reader.position(reader.getGMPOffset() + 0x67); |
| 88 |
nod5.readSectionInfo(reader, false); |
| 89 |
reader.get2u(); |
| 90 |
nod6.readSectionInfo(reader, true); |
| 91 |
indexIdSize = Utils.numberToPointerSize(nod6.getNumItems()); |
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
static final int DISTANCE_MULT_SHIFT = 1; |
| 106 |
static final int DISTANCE_MULT = 1 << DISTANCE_MULT_SHIFT; |
| 107 |
protected void writeFileHeader(ImgFileWriter writer) { |
| 108 |
nodes.setPosition(HEADER_LEN); |
| 109 |
nodes.writeSectionInfo(writer); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
int flags = 0x0207; |
| 116 |
assert Integer.bitCount(DISTANCE_MULT) == 1; |
| 117 |
assert DISTANCE_MULT_SHIFT < 8; |
| 118 |
flags |= DISTANCE_MULT_SHIFT << 5; |
| 119 |
if(driveOnLeft) |
| 120 |
flags |= 0x0100; |
| 121 |
|
| 122 |
writer.put4(flags); |
| 123 |
|
| 124 |
writer.put1u(DEF_ALIGN); |
| 125 |
writer.put1u(0); |
| 126 |
writer.put2u(5); |
| 127 |
|
| 128 |
roads.writeSectionInfo(writer); |
| 129 |
writer.put4(0); |
| 130 |
|
| 131 |
boundary.writeSectionInfo(writer); |
| 132 |
|
| 133 |
writer.put4(2); |
| 134 |
highClassBoundary.writeSectionInfo(writer); |
| 135 |
writer.put4(classBoundaries[0]); |
| 136 |
for (int i = 1; i < classBoundaries.length; i++){ |
| 137 |
writer.put4(classBoundaries[i] - classBoundaries[i-1]); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
private static final double UNIT_TO_METER = 2.4; |
| 142 |
public static int metersToRaw(double m) { |
| 143 |
double d = m / (DISTANCE_MULT * UNIT_TO_METER); |
| 144 |
return (int) Math.round(d); |
| 145 |
} |
| 146 |
|
| 147 |
public int getNodeStart() { |
| 148 |
return nodes.getPosition(); |
| 149 |
} |
| 150 |
|
| 151 |
public void setNodeStart(int start) { |
| 152 |
nodes.setPosition(start); |
| 153 |
} |
| 154 |
|
| 155 |
public int getNodeSize() { |
| 156 |
return nodes.getSize(); |
| 157 |
} |
| 158 |
|
| 159 |
public void setNodeSize(int size) { |
| 160 |
nodes.setSize(size); |
| 161 |
} |
| 162 |
|
| 163 |
public Section getNodeSection() { |
| 164 |
return nodes; |
| 165 |
} |
| 166 |
|
| 167 |
public void setRoadSize(int size) { |
| 168 |
roads.setSize(size); |
| 169 |
} |
| 170 |
|
| 171 |
public Section getRoadSection() { |
| 172 |
return roads; |
| 173 |
} |
| 174 |
|
| 175 |
public void setBoundarySize(int size) { |
| 176 |
boundary.setSize(size); |
| 177 |
} |
| 178 |
|
| 179 |
public Section getBoundarySection() { |
| 180 |
return boundary; |
| 181 |
} |
| 182 |
|
| 183 |
public void setHighClassBoundarySize(int size) { |
| 184 |
highClassBoundary.setSize(size); |
| 185 |
} |
| 186 |
public Section getHighClassBoundary() { |
| 187 |
return highClassBoundary; |
| 188 |
} |
| 189 |
|
| 190 |
public int[] getClassBoundaries() { |
| 191 |
return classBoundaries; |
| 192 |
} |
| 193 |
|
| 194 |
public void setDriveOnLeft(boolean dol) { |
| 195 |
driveOnLeft = dol; |
| 196 |
} |
| 197 |
|
| 198 |
public int getFlags() { |
| 199 |
return flags; |
| 200 |
} |
| 201 |
|
| 202 |
public int getAlign() { |
| 203 |
return align; |
| 204 |
} |
| 205 |
|
| 206 |
public int getMult1() { |
| 207 |
return mult1; |
| 208 |
} |
| 209 |
|
| 210 |
public int getTableARecordLen() { |
| 211 |
return tableARecordLen; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public int getIndexIdSize() { |
| 218 |
return indexIdSize; |
| 219 |
} |
| 220 |
} |