Debian Codegraph @

src/uk/me/parabola/imgfmt/app/trergn/RGNFile.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2006 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: 03-Dec-2006
16 */
17 package uk.me.parabola.imgfmt.app.trergn;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21
22 import uk.me.parabola.imgfmt.app.BufferedImgFileWriter;
23 import uk.me.parabola.imgfmt.app.ImgFile;
24 import uk.me.parabola.imgfmt.app.ImgFileWriter;
25 import uk.me.parabola.imgfmt.fs.ImgChannel;
26 import uk.me.parabola.log.Logger;
27
28 /**
29 * The region file. Holds actual details of points and lines etc.
30 *
31 *
32 *
33 * The data is rather complicated and is packed to save space. This class does
34 * not really handle that format however as it is written by the
35 * {@link MapObject}s themselves.
36 *
37 * Each subdivision takes space in this file. The I am expecting this to be the
38 * biggest file, although it seems that TRE may be in some circumstances.
39 *
40 * @author Steve Ratcliffe
41 */
42 public class RGNFile extends ImgFile {
43 private static final Logger log = Logger.getLogger(RGNFile.class);
44
45 private static final int HEADER_LEN = RGNHeader.HEADER_LEN;
46
47 private final RGNHeader header = new RGNHeader();
48
49 private Subdivision currentDivision;
50 private int indPointPtrOff;
51 private int polylinePtrOff;
52 private int polygonPtrOff;
53 private final ByteArrayOutputStream extTypePointsData = new ByteArrayOutputStream();
54 private final ByteArrayOutputStream extTypeLinesData = new ByteArrayOutputStream();
55 private final ByteArrayOutputStream extTypeAreasData = new ByteArrayOutputStream();
56
57 public RGNFile(ImgChannel chan) {
58 setHeader(header);
59
60 setWriter(new BufferedImgFileWriter(chan, "RGN"));
61
62 // Position at the start of the writable area.
63 position(HEADER_LEN);
64 }
65
66 public void write() {
67 if (!isWritable())
68 throw new IllegalStateException("File not writable");
69
70 header.setDataSize(position() - HEADER_LEN);
71
72 if(extTypeAreasData.size() > 0) {
73 header.setExtTypeAreasInfo(position(), extTypeAreasData.size());
74 getWriter().put(extTypeAreasData.toByteArray());
75 }
76 if(extTypeLinesData.size() > 0) {
77 header.setExtTypeLinesInfo(position(), extTypeLinesData.size());
78 getWriter().put(extTypeLinesData.toByteArray());
79 }
80 if(extTypePointsData.size() > 0) {
81 header.setExtTypePointsInfo(position(), extTypePointsData.size());
82 getWriter().put(extTypePointsData.toByteArray());
83 }
84
85 getHeader().writeHeader(getWriter());
86 }
87
88 public void startDivision(Subdivision sd) {
89
90 sd.setStartRgnPointer(position() - HEADER_LEN);
91
92 // We need to reserve space for a pointer for each type of map
93 // element that is supported by this division. Note that these
94 // pointers are only 2bytes long. A pointer to the points is never
95 // needed as it will always be first if present.
96 if (sd.needsIndPointPtr()) {
97 indPointPtrOff = position();
98 position(position() + 2L);
99 }
100
101 if (sd.needsPolylinePtr()) {
102 polylinePtrOff = position();
103 position(position() + 2L);
104 }
105
106 if (sd.needsPolygonPtr()) {
107 polygonPtrOff = position();
108 position(position() + 2L);
109 }
110
111 currentDivision = sd;
112 }
113
114 public void addMapObject(MapObject item) {
115 if (item.hasExtendedType()) {
116 try {
117 if (item instanceof Point) {
118 item.write(extTypePointsData);
119 } else if (item instanceof Polygon) {
120 item.write(extTypeAreasData);
121 } else if (item instanceof Polyline) {
122 item.write(extTypeLinesData);
123 } else
124 log.error("Can't add object of type " + item.getClass());
125 } catch (IOException ioe) {
126 log.error("Error writing extended type object: " + ioe.getMessage());
127 }
128 } else {
129 item.write(getWriter());
130 }
131 }
132
133 public void setIndPointPtr() {
134 if (currentDivision.needsIndPointPtr()) {
135 long currPos = position();
136 position(indPointPtrOff);
137 long off = currPos - currentDivision.getStartRgnPointer() - HEADER_LEN;
138 if (off > 0xffff)
139 throw new IllegalStateException("IndPoint offset too large: " + off);
140
141 getWriter().put2u((int) off);
142 position(currPos);
143 }
144 }
145
146 public void setPolylinePtr() {
147 if (currentDivision.needsPolylinePtr()) {
148 long currPos = position();
149 position(polylinePtrOff);
150 long off = currPos - currentDivision.getStartRgnPointer() - HEADER_LEN;
151 if (off > 0xffff)
152 throw new IllegalStateException("Polyline offset too large: " + off);
153
154 if (log.isDebugEnabled())
155 log.debug("setting polyline offset to", off);
156 getWriter().put2u((int) off);
157
158 position(currPos);
159 }
160 }
161
162 public void setPolygonPtr() {
163 if (currentDivision.needsPolygonPtr()) {
164 long currPos = position();
165 long off = currPos - currentDivision.getStartRgnPointer() - HEADER_LEN;
166 log.debug("currpos=", currPos, ", off=", off);
167 if (off > 0xffff)
168 throw new IllegalStateException("Polygon offset too large: " + off);
169
170 if (log.isDebugEnabled())
171 log.debug("setting polygon offset to", off, "@", polygonPtrOff);
172 position(polygonPtrOff);
173 getWriter().put2u((int) off);
174 position(currPos);
175 }
176 }
177
178 @Override
179 public ImgFileWriter getWriter() {
180 return super.getWriter();
181 }
182
183 public int getExtTypePointsSize() {
184 return extTypePointsData.size();
185 }
186
187 public int getExtTypeLinesSize() {
188 return extTypeLinesData.size();
189 }
190
191 public int getExtTypeAreasSize() {
192 return extTypeAreasData.size();
193 }
194
195 public boolean haveExtendedTypes() {
196 return extTypeAreasData.size() != 0 ||
197 extTypeLinesData.size() != 0 ||
198 extTypePointsData.size() != 0;
199 }
200 }