Debian Codegraph @

src/uk/me/parabola/imgfmt/app/typ/TYPHeader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 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: Dec 14, 2007
16 */
17 package uk.me.parabola.imgfmt.app.typ;
18
19 import uk.me.parabola.imgfmt.app.CommonHeader;
20 import uk.me.parabola.imgfmt.app.ImgFileReader;
21 import uk.me.parabola.imgfmt.app.ImgFileWriter;
22 import uk.me.parabola.imgfmt.app.Section;
23
24 /**
25 * The header for the TYP file.
26 *
27 * @author Thomas Lußnig
28 */
29 public class TYPHeader extends CommonHeader {
30 public static final int HEADER_LEN = 0x9c; // 0x6e;
31
32 private char familyId;
33 private char productId;
34 private char codePage;
35
36 private final Section pointData = new Section();
37 private final Section lineData = new Section(pointData);
38 private final Section polygonData = new Section(lineData);
39
40 private final Section pointIndex = new Section(polygonData, 2);
41 private final Section lineIndex = new Section(pointIndex, 2);
42 private final Section polygonIndex = new Section(lineIndex, 2);
43
44 private final Section shapeStacking = new Section(polygonIndex, 5);
45
46 private final Section iconData = new Section(polygonIndex);
47 private final Section iconIndex = new Section(iconData, 3);
48
49 private final Section labels = new Section(iconIndex);
50 private final Section stringIndex = new Section(labels);
51 private final Section typeIndex = new Section(stringIndex);
52
53 public TYPHeader() {
54 super(HEADER_LEN, "GARMIN TYP");
55 }
56
57 /**
58 * Read the rest of the header. Specific to the given file. It is guaranteed
59 * that the file position will be set to the correct place before this is
60 * called.
61 *
62 * @param reader The header is read from here.
63 */
64 protected void readFileHeader(ImgFileReader reader) {
65 // Reset position for the real header reading code.
66 reader.position(COMMON_HEADER_LEN);
67 codePage = (char) reader.get2u(); // 1252
68 pointData.setPosition(reader.get4());
69 pointData.setSize(reader.get4());
70
71 lineData.setPosition(reader.get4());
72 lineData.setSize(reader.get4());
73
74 polygonData.setPosition(reader.get4());
75 polygonData.setSize(reader.get4());
76
77 familyId = (char) reader.get2u();
78 productId = (char) reader.get2u();
79
80 pointIndex.setPosition(reader.get4());
81 pointIndex.setItemSize(reader.get2u());
82 pointIndex.setSize(reader.get4());
83
84 lineIndex.setPosition(reader.get4());
85 lineIndex.setItemSize(reader.get2u());
86 lineIndex.setSize(reader.get4());
87
88 polygonIndex.setPosition(reader.get4());
89 polygonIndex.setItemSize(reader.get2u());
90 polygonIndex.setSize(reader.get4());
91
92 shapeStacking.setPosition(reader.get4());
93 shapeStacking.setItemSize(reader.get2u());
94 shapeStacking.setSize(reader.get4());
95 }
96
97 /**
98 * Write the rest of the header. It is guaranteed that the writer will be set
99 * to the correct position before calling.
100 *
101 * This header appears to have a different layout to most other headers.
102 *
103 * @param writer The header is written here.
104 */
105 protected void writeFileHeader(ImgFileWriter writer) {
106 writer.put2u(codePage);
107
108 pointData.writeSectionInfo(writer);
109 lineData.writeSectionInfo(writer);
110 polygonData.writeSectionInfo(writer);
111
112 writer.put2u(familyId);
113 writer.put2u(productId);
114
115 // Can't use Section.writeSectionInfo here as there is an unusual layout.
116 writeSectionInfo(writer, pointIndex);
117 writeSectionInfo(writer, lineIndex);
118 writeSectionInfo(writer, polygonIndex);
119 writeSectionInfo(writer, shapeStacking);
120
121 if (getHeaderLength() > 0x5b) {
122 writeSectionInfo(writer, iconIndex);
123 writer.put1u(0x13);
124 iconData.writeSectionInfo(writer);
125 writer.put4(0);
126 }
127
128 if (getHeaderLength() > 0x6e) {
129 labels.writeSectionInfo(writer);
130
131 // not known, guessing. Different layout to other files.
132 writer.put4(stringIndex.getItemSize());
133 writer.put4(0x1b);
134 writer.put4(stringIndex.getPosition());
135 writer.put4(stringIndex.getSize());
136
137 writer.put4(typeIndex.getItemSize());
138 writer.put4(0x1b);
139 writer.put4(typeIndex.getPosition());
140 writer.put4(typeIndex.getSize());
141
142 writer.put2u(0);
143 }
144 }
145
146 /**
147 * There is an unusual layout of the section pointers in the TYP file for the sections
148 * that have an item size.
149 */
150 private void writeSectionInfo(ImgFileWriter writer, Section section) {
151 writer.put4(section.getPosition());
152 writer.put2u(section.getItemSize());
153 writer.put4(section.getSize());
154 }
155
156 void setCodePage(char codePage) {
157 this.codePage = codePage;
158 }
159
160 Section getPointData() {
161 return pointData;
162 }
163
164 void setFamilyId(char familyId) {
165 this.familyId = familyId;
166 }
167
168 void setProductId(char productId) {
169 this.productId = productId;
170 }
171
172 Section getPointIndex() {
173 return pointIndex;
174 }
175
176 Section getShapeStacking() {
177 return shapeStacking;
178 }
179
180 public Section getPolygonData() {
181 return polygonData;
182 }
183
184 public Section getPolygonIndex() {
185 return polygonIndex;
186 }
187
188 public Section getLineData() {
189 return lineData;
190 }
191
192 public Section getLineIndex() {
193 return lineIndex;
194 }
195
196 public Section getIconData() {
197 return iconData;
198 }
199
200 public Section getIconIndex() {
201 return iconIndex;
202 }
203
204 public Section getLabels() {
205 return labels;
206 }
207
208 public Section getStringIndex() {
209 return stringIndex;
210 }
211
212 public Section getTypeIndex() {
213 return typeIndex;
214 }
215 }