| 1 |
|
| 2 |
package org.freehep.graphicsio.cgm; |
| 3 |
|
| 4 |
import java.awt.Color; |
| 5 |
import java.awt.geom.Point2D; |
| 6 |
import java.io.IOException; |
| 7 |
import java.io.OutputStream; |
| 8 |
|
| 9 |
import org.freehep.util.io.ActionHeader; |
| 10 |
import org.freehep.util.io.Tag; |
| 11 |
import org.freehep.util.io.TagHeader; |
| 12 |
import org.freehep.util.io.TaggedOutputStream; |
| 13 |
import org.freehep.util.io.UndefinedTagException; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class CGMOutputStream extends TaggedOutputStream { |
| 26 |
|
| 27 |
private static short COMMAND_PARTITION_SIZE = Short.MAX_VALUE; |
| 28 |
|
| 29 |
private int version; |
| 30 |
|
| 31 |
private boolean direct = true; |
| 32 |
|
| 33 |
private int colorIndexPrecision = 8; |
| 34 |
|
| 35 |
private int directColorPrecision = 8; |
| 36 |
|
| 37 |
private int indexPrecision = 16; |
| 38 |
|
| 39 |
private int integerPrecision = 16; |
| 40 |
|
| 41 |
private boolean fixedPrecision = true; |
| 42 |
|
| 43 |
private boolean doublePrecision = false; |
| 44 |
|
| 45 |
private boolean vdcReal = false; |
| 46 |
|
| 47 |
private boolean vdcFixedPrecision = true; |
| 48 |
|
| 49 |
private boolean vdcDoublePrecision = false; |
| 50 |
|
| 51 |
private int vdcIntegerPrecision = 16; |
| 52 |
|
| 53 |
private int namePrecision = 16; |
| 54 |
|
| 55 |
private int lineWidthSpecificationMode = LineWidthSpecificationMode.ABSOLUTE; |
| 56 |
|
| 57 |
private int markerSizeSpecificationMode = MarkerSizeSpecificationMode.ABSOLUTE; |
| 58 |
|
| 59 |
private int edgeWidthSpecificationMode = EdgeWidthSpecificationMode.ABSOLUTE; |
| 60 |
|
| 61 |
private int interiorStyleSpecificationMode = InteriorStyleSpecificationMode.ABSOLUTE; |
| 62 |
|
| 63 |
public CGMOutputStream(OutputStream os, int version) throws IOException { |
| 64 |
|
| 65 |
|
| 66 |
super(os, new CGMTagSet(version), null, false); |
| 67 |
this.version = version; |
| 68 |
} |
| 69 |
|
| 70 |
public CGMOutputStream(OutputStream os) throws IOException { |
| 71 |
|
| 72 |
this(os, 1); |
| 73 |
} |
| 74 |
|
| 75 |
void setColorMode(boolean direct) { |
| 76 |
this.direct = direct; |
| 77 |
} |
| 78 |
|
| 79 |
void setColorIndexPrecision(int precision) { |
| 80 |
colorIndexPrecision = precision; |
| 81 |
} |
| 82 |
|
| 83 |
void setDirectColorPrecision(int precision) { |
| 84 |
directColorPrecision = precision; |
| 85 |
} |
| 86 |
|
| 87 |
void setIndexPrecision(int precision) { |
| 88 |
indexPrecision = precision; |
| 89 |
} |
| 90 |
|
| 91 |
void setIntegerPrecision(int precision) { |
| 92 |
integerPrecision = precision; |
| 93 |
} |
| 94 |
|
| 95 |
void setRealPrecision(boolean fixedPrecision, boolean doublePrecision) { |
| 96 |
this.fixedPrecision = fixedPrecision; |
| 97 |
this.doublePrecision = doublePrecision; |
| 98 |
} |
| 99 |
|
| 100 |
void setVDCReal(boolean real) { |
| 101 |
vdcReal = real; |
| 102 |
} |
| 103 |
|
| 104 |
void setVDCIntegerPrecision(int precision) { |
| 105 |
vdcIntegerPrecision = precision; |
| 106 |
} |
| 107 |
|
| 108 |
void setVDCRealPrecision(boolean fixedPrecision, boolean doublePrecision) { |
| 109 |
this.vdcFixedPrecision = fixedPrecision; |
| 110 |
this.vdcDoublePrecision = doublePrecision; |
| 111 |
} |
| 112 |
|
| 113 |
void setNamePrecision(int precision) { |
| 114 |
namePrecision = precision; |
| 115 |
} |
| 116 |
|
| 117 |
void setLineWidthSpecificationMode(int mode) { |
| 118 |
lineWidthSpecificationMode = mode; |
| 119 |
} |
| 120 |
|
| 121 |
int getLineWidthSpecificationMode() { |
| 122 |
return lineWidthSpecificationMode; |
| 123 |
} |
| 124 |
|
| 125 |
void setMarkerSizeSpecificationMode(int mode) { |
| 126 |
markerSizeSpecificationMode = mode; |
| 127 |
} |
| 128 |
|
| 129 |
int getMarkerSizeSpecificationMode() { |
| 130 |
return markerSizeSpecificationMode; |
| 131 |
} |
| 132 |
|
| 133 |
void setEdgeWidthSpecificationMode(int mode) { |
| 134 |
edgeWidthSpecificationMode = mode; |
| 135 |
} |
| 136 |
|
| 137 |
int getEdgeWidthSpecificationMode() { |
| 138 |
return edgeWidthSpecificationMode; |
| 139 |
} |
| 140 |
|
| 141 |
void setInteriorStyleSpecificationMode(int mode) { |
| 142 |
interiorStyleSpecificationMode = mode; |
| 143 |
} |
| 144 |
|
| 145 |
int getInteriorStyleSpecificationMode() { |
| 146 |
return interiorStyleSpecificationMode; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
public void writeFixedPoint(double d) throws IOException { |
| 151 |
int whole = (int) Math.floor(d); |
| 152 |
int frac = (int) Math.floor((d - whole) * 0x10000); |
| 153 |
writeShort(whole); |
| 154 |
writeShort(frac); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
public void writeFloatingPoint(double d) throws IOException { |
| 159 |
if (doublePrecision) { |
| 160 |
writeDouble(d); |
| 161 |
} else { |
| 162 |
writeFloat((float) d); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
public void writeColorIndex(int i) throws IOException { |
| 168 |
switch (colorIndexPrecision) { |
| 169 |
case 8: |
| 170 |
writeByte(i); |
| 171 |
break; |
| 172 |
case 16: |
| 173 |
writeShort(i); |
| 174 |
break; |
| 175 |
default: |
| 176 |
case 24: |
| 177 |
case 32: |
| 178 |
writeInt(i); |
| 179 |
break; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
public void writeColorComponent(int c) throws IOException { |
| 185 |
switch (directColorPrecision) { |
| 186 |
default: |
| 187 |
case 8: |
| 188 |
case 16: |
| 189 |
case 24: |
| 190 |
case 32: |
| 191 |
writeUnsignedByte(c); |
| 192 |
break; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
public void writeColorDirect(Color c) throws IOException { |
| 198 |
|
| 199 |
writeColorComponent(c.getRed()); |
| 200 |
writeColorComponent(c.getGreen()); |
| 201 |
writeColorComponent(c.getBlue()); |
| 202 |
|
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
public void writeIntegerIndex(int i) throws IOException { |
| 208 |
switch (indexPrecision) { |
| 209 |
case 8: |
| 210 |
writeByte(i); |
| 211 |
break; |
| 212 |
default: |
| 213 |
case 16: |
| 214 |
writeShort(i); |
| 215 |
break; |
| 216 |
case 24: |
| 217 |
case 32: |
| 218 |
writeInt(i); |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
public void writeEnumerate(int i) throws IOException { |
| 225 |
writeShort(i); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
public void writeInteger(int i) throws IOException { |
| 230 |
switch (integerPrecision) { |
| 231 |
case 8: |
| 232 |
writeByte(i); |
| 233 |
break; |
| 234 |
default: |
| 235 |
case 16: |
| 236 |
writeShort(i); |
| 237 |
break; |
| 238 |
case 24: |
| 239 |
case 32: |
| 240 |
writeInt(i); |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
public void writeReal(double r) throws IOException { |
| 247 |
if (fixedPrecision) { |
| 248 |
writeFixedPoint(r); |
| 249 |
} else { |
| 250 |
writeFloatingPoint(r); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
public void writeString(String s) throws IOException { |
| 256 |
int len = s.length(); |
| 257 |
if (len < 0xFF) { |
| 258 |
|
| 259 |
writeUnsignedByte(len); |
| 260 |
} else { |
| 261 |
|
| 262 |
writeUnsignedByte(0xFF); |
| 263 |
writeUnsignedShort(len & 0x7FFF); |
| 264 |
} |
| 265 |
writeBytes(s); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
public void writeData(byte[] data) throws IOException { |
| 270 |
|
| 271 |
writeString(new String(data)); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
public void writeVDC(double d) throws IOException { |
| 276 |
if (vdcReal) { |
| 277 |
if (vdcFixedPrecision) { |
| 278 |
writeFixedPoint(d); |
| 279 |
} else { |
| 280 |
if (vdcDoublePrecision) { |
| 281 |
writeDouble(d); |
| 282 |
} else { |
| 283 |
writeFloat((float) d); |
| 284 |
} |
| 285 |
} |
| 286 |
} else { |
| 287 |
switch (vdcIntegerPrecision) { |
| 288 |
default: |
| 289 |
case 16: |
| 290 |
writeShort((int) d); |
| 291 |
break; |
| 292 |
case 24: |
| 293 |
case 32: |
| 294 |
writeInt((int) d); |
| 295 |
break; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
public void writePoint(Point2D p) throws IOException { |
| 302 |
writeVDC(p.getX()); |
| 303 |
writeVDC(p.getY()); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
public void writeColor(Color c) throws IOException { |
| 308 |
if (direct) { |
| 309 |
writeColorDirect(c); |
| 310 |
} else { |
| 311 |
|
| 312 |
writeColorIndex((c.getRed() << 16) + (c.getGreen() << 8) |
| 313 |
+ c.getBlue()); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
public void writeName(int name) throws IOException { |
| 319 |
switch (namePrecision) { |
| 320 |
case 8: |
| 321 |
writeByte(name); |
| 322 |
break; |
| 323 |
default: |
| 324 |
case 16: |
| 325 |
writeShort((int) name); |
| 326 |
break; |
| 327 |
case 24: |
| 328 |
case 32: |
| 329 |
writeInt((int) name); |
| 330 |
break; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
protected void writeTagHeader(TagHeader header) throws IOException { |
| 335 |
|
| 336 |
byteAlign(); |
| 337 |
|
| 338 |
int tagID = header.getTag(); |
| 339 |
long length = header.getLength(); |
| 340 |
if (length < 0x1F) { |
| 341 |
|
| 342 |
writeUnsignedShort((tagID << 5) + (int) length); |
| 343 |
} else { |
| 344 |
|
| 345 |
writeUnsignedShort((tagID << 5) + 0x001F); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
public void writeTag(Tag tag) throws IOException { |
| 350 |
|
| 351 |
int tagID = tag.getTag(); |
| 352 |
|
| 353 |
if (!tagSet.exists(tagID)) |
| 354 |
throw new UndefinedTagException(tagID); |
| 355 |
|
| 356 |
pushBuffer(); |
| 357 |
tag.write(tagID, this); |
| 358 |
int align = getTagAlignment(); |
| 359 |
int pad = (align - (getBufferLength() % align)) % align; |
| 360 |
for (int i = 0; i < pad; i++) { |
| 361 |
write(0); |
| 362 |
} |
| 363 |
int len = getBufferLength(); |
| 364 |
byte[] buffer = popBufferBytes(); |
| 365 |
TagHeader header = new TagHeader(tagID, len); |
| 366 |
writeTagHeader(header); |
| 367 |
if (len <= 30) { |
| 368 |
write(buffer, 0, len); |
| 369 |
} else { |
| 370 |
writePartitioned(buffer, len); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
if ((size() % 2) != 0) { |
| 375 |
writeUnsignedByte(0); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
private void writePartitioned(byte[] buffer, int len) throws IOException { |
| 380 |
int bufferOffset = 0; |
| 381 |
final short partitionSize = COMMAND_PARTITION_SIZE; |
| 382 |
while (len > partitionSize) { |
| 383 |
writeUnsignedShort(0x8000 | partitionSize); |
| 384 |
write(buffer, bufferOffset, partitionSize); |
| 385 |
bufferOffset += partitionSize; |
| 386 |
len -= partitionSize; |
| 387 |
} |
| 388 |
writeUnsignedShort(len); |
| 389 |
write(buffer, bufferOffset, len); |
| 390 |
} |
| 391 |
|
| 392 |
protected void writeActionHeader(ActionHeader header) throws IOException { |
| 393 |
|
| 394 |
|
| 395 |
} |
| 396 |
|
| 397 |
public int getVersion() { |
| 398 |
return version; |
| 399 |
} |
| 400 |
} |