View Javadoc

1   // Copyright 2001-2005, FreeHEP.
2   package org.freehep.graphicsio.swf;
3   
4   import java.awt.Dimension;
5   import java.awt.geom.Rectangle2D;
6   import java.io.IOException;
7   
8   /**
9    * SWF File Header.
10   * 
11   * @author Mark Donszelmann
12   * @author Charles Loomis
13   * @version $Id: SWFHeader.java 8584 2006-08-10 23:06:37Z duns $
14   */
15  public class SWFHeader implements SWFConstants {
16  
17      private int version;
18  
19      private long length;
20  
21      private Dimension size;
22  
23      private float rate;
24  
25      private int count;
26  
27      private boolean compress;
28  
29      public SWFHeader(int version, long length, Dimension size, float rate,
30              int count, boolean compress) {
31          this.version = version;
32          this.length = length;
33          this.size = size;
34          this.rate = rate;
35          this.count = count;
36          this.compress = compress;
37      }
38  
39      public SWFHeader(SWFInputStream swf) throws IOException {
40  
41          // NOTE: page 8 and 13 are in conflict about the HEADER for compressed
42          // files.
43          // the tag that can be read by Flash is CWS (and not FWC).
44  
45          int s = swf.readUnsignedByte();
46          if (((int) 'F' != s) && ((int) 'C' != s))
47              throw new IOException();
48          if ((int) 'W' != swf.readUnsignedByte())
49              throw new IOException();
50          if ((int) 'S' != swf.readUnsignedByte())
51              throw new IOException();
52          version = swf.readUnsignedByte();
53          length = swf.readUnsignedInt();
54  
55          compress = (version >= 6) && ((int) 'C' == s);
56          if (compress)
57              swf.startDecompressing();
58  
59          Rectangle2D frame = swf.readRect();
60          size = new Dimension((int) frame.getMaxX(), (int) frame.getMaxY());
61          rate = (float) swf.readUnsignedShort() / 256f;
62          count = swf.readUnsignedShort();
63      }
64  
65      public void write(SWFOutputStream swf) throws IOException {
66          // NOTE: page 8 and 13 are in conflict about the HEADER for compressed
67          // files.
68          // the tag that can be read by Flash is CWS (and not FWC).
69  
70          if ((version >= 6) && compress) {
71              swf.writeUnsignedByte('C');
72          } else {
73              swf.writeUnsignedByte('F');
74          }
75          swf.writeUnsignedByte('W');
76          swf.writeUnsignedByte('S');
77          swf.writeUnsignedByte(version);
78          swf.writeUnsignedInt(length);
79  
80          if ((version >= 6) && compress)
81              swf.startCompressing();
82  
83          // the rectangle seems to may be 16 bits instead of 15...
84          Rectangle2D frame = new Rectangle2D.Double(0, 0, size.width,
85                  size.height);
86          swf.writeRect(frame);
87          swf.writeUnsignedShort((int) (rate * 256f));
88          swf.writeUnsignedShort(count);
89      }
90  
91      public static int size() {
92          return 21; // this is fixed
93      }
94  
95      public int getVersion() {
96          return version;
97      }
98  
99      public long getLength() {
100         return length;
101     }
102 
103     public boolean isCompressed() {
104         return compress;
105     }
106 
107     public Dimension getSize() {
108         return size;
109     }
110 
111     public float getRate() {
112         return rate;
113     }
114 
115     public int getCount() {
116         return count;
117     }
118 
119     public String toString() {
120         StringBuffer s = new StringBuffer("SWF Header\n");
121         s.append("  version: " + version + "\n");
122         s.append("  length: " + length + "\n");
123         s.append("  compress: " + compress + "\n");
124         s.append("  size: " + size + "\n");
125         s.append("  rate: " + rate + "\n");
126         s.append("  count: " + count);
127         return s.toString();
128     }
129 }