| 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 |
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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 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 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 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 |
} |