View Javadoc

1   // Copyright 2001, FreeHEP.
2   package org.freehep.graphicsio.emf;
3   
4   import java.awt.Color;
5   import java.awt.Dimension;
6   import java.awt.Point;
7   import java.awt.Rectangle;
8   import java.awt.geom.AffineTransform;
9   import java.io.IOException;
10  import java.io.InputStream;
11  
12  import org.freehep.util.io.ActionHeader;
13  import org.freehep.util.io.TagHeader;
14  import org.freehep.util.io.TaggedInputStream;
15  
16  /**
17   * This class extends the TaggedInputStream with several methods to read EMF
18   * primitives from the stream and to read TagHeaders.
19   * 
20   * @author Mark Donszelmann
21   * @version $Id: EMFInputStream.java 10367 2007-01-22 19:26:48Z duns $
22   */
23  public class EMFInputStream extends TaggedInputStream implements EMFConstants {
24  
25      public static int DEFAULT_VERSION = 1;
26  
27      public EMFInputStream(InputStream is) {
28          this(is, DEFAULT_VERSION);
29      }
30  
31      public EMFInputStream(InputStream is, int version) {
32          this(is, new EMFTagSet(version));
33      }
34  
35      public EMFInputStream(InputStream is, EMFTagSet tagSet)  {
36          // EMF is little-endian
37          super(is, tagSet, null, true);
38      }
39  
40      public int readDWORD() throws IOException {
41          long i = readUnsignedInt();
42          return (int) i;
43      }
44  
45      public int[] readDWORD(int size) throws IOException {
46          int[] x = new int[size];
47          for (int i = 0; i < x.length; i++) {
48              x[i] = readDWORD();
49          }
50          return x;
51      }
52  
53      public int readWORD() throws IOException {
54          return readUnsignedShort();
55      }
56  
57      public int readLONG() throws IOException {
58          return readInt();
59      }
60  
61      public int[] readLONG(int size) throws IOException {
62          int[] x = new int[size];
63          for (int i = 0; i < x.length; i++) {
64              x[i] = readLONG();
65          }
66          return x;
67      }
68  
69      public float readFLOAT() throws IOException {
70          return readFloat();
71      }
72  
73      public int readUINT() throws IOException {
74          return (int) readUnsignedInt();
75      }
76  
77      public int readULONG() throws IOException {
78          return (int) readUnsignedInt();
79      }
80  
81      public Color readCOLORREF() throws IOException {
82          Color c = new Color(readUnsignedByte(), readUnsignedByte(),
83                  readUnsignedByte());
84          readByte();
85          return c;
86      }
87  
88      public Color readCOLOR16() throws IOException {
89          return new Color(readShort() >> 8, readShort() >> 8, readShort() >> 8,
90                  readShort() >> 8);
91      }
92  
93      public AffineTransform readXFORM() throws IOException {
94          return new AffineTransform(readFLOAT(), readFLOAT(), readFLOAT(),
95                  readFLOAT(), readFLOAT(), readFLOAT());
96      }
97  
98      public Rectangle readRECTL() throws IOException {
99          int x = readLONG();
100         int y = readLONG();
101         int w = readLONG() - x;
102         int h = readLONG() - y;
103         return new Rectangle(x, y, w, h);
104     }
105 
106     public Point readPOINTL() throws IOException {
107         int x = readLONG();
108         int y = readLONG();
109         return new Point(x, y);
110     }
111 
112     public Point[] readPOINTL(int size) throws IOException {
113         Point[] p = new Point[size];
114         for (int i = 0; i < p.length; i++) {
115             p[i] = readPOINTL();
116         }
117         return p;
118     }
119 
120     public Point readPOINTS() throws IOException {
121         int x = readShort();
122         int y = readShort();
123         return new Point(x, y);
124     }
125 
126     public Point[] readPOINTS(int size) throws IOException {
127         Point[] p = new Point[size];
128         for (int i = 0; i < p.length; i++) {
129             p[i] = readPOINTS();
130         }
131         return p;
132     }
133 
134     public Dimension readSIZEL() throws IOException {
135         return new Dimension(readLONG(), readLONG());
136     }
137 
138     public int readBYTE() throws IOException {
139         return readByte();
140     }
141 
142     public byte[] readBYTE(int size) throws IOException {
143         byte[] x = new byte[size];
144         for (int i = 0; i < x.length; i++) {
145             x[i] = (byte) readBYTE();
146         }
147         return x;
148     }
149 
150     public boolean readBOOLEAN() throws IOException {
151         return (readBYTE() != 0);
152     }
153 
154     public String readWCHAR(int size) throws IOException {
155         byte[] bytes = readByte(2 * size);
156         int length = 2 * size;
157         for (int i = 0; i < 2 * size; i += 2) {
158             if (bytes[i] == 0 && bytes[i + 1] == 0) {
159                 length = i;
160                 break;
161             }
162         }
163         return new String(bytes, 0, length, "UTF-16LE");
164     }
165 
166     protected TagHeader readTagHeader() throws IOException {
167         // Read the tag.
168         // byteAlign();
169         int tagID = read();
170         // End of stream
171         if (tagID == -1)
172             return null;
173 
174         tagID |= readUnsignedByte() << 8;
175         tagID |= readUnsignedByte() << 16;
176         tagID |= readUnsignedByte() << 24;
177 
178         long length = readDWORD();
179         return new TagHeader(tagID, length - 8);
180     }
181 
182     protected ActionHeader readActionHeader() throws IOException {
183         return null;
184     }
185 
186     private EMFHeader header;
187 
188     public EMFHeader readHeader() throws IOException {
189         if (header == null) {
190             header = new EMFHeader(this);
191         }
192         return header;
193     }
194 
195     public int getVersion() {
196         return DEFAULT_VERSION;
197     }
198 }