| 1 |
|
| 2 |
package org.freehep.graphicsio.swf; |
| 3 |
|
| 4 |
import java.awt.Color; |
| 5 |
import java.awt.geom.AffineTransform; |
| 6 |
import java.awt.geom.Rectangle2D; |
| 7 |
import java.io.IOException; |
| 8 |
import java.io.InputStream; |
| 9 |
|
| 10 |
import org.freehep.util.io.ActionHeader; |
| 11 |
import org.freehep.util.io.TagHeader; |
| 12 |
import org.freehep.util.io.TaggedInputStream; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class SWFInputStream extends TaggedInputStream implements SWFConstants { |
| 24 |
|
| 25 |
private SWFDictionary dictionary; |
| 26 |
|
| 27 |
private byte[] jpegTable; |
| 28 |
|
| 29 |
public SWFInputStream(InputStream is) throws IOException { |
| 30 |
|
| 31 |
this(is, DEFAULT_VERSION); |
| 32 |
} |
| 33 |
|
| 34 |
public SWFInputStream(InputStream is, int version) throws IOException { |
| 35 |
|
| 36 |
this(is, new SWFTagSet(version), new SWFActionSet(version)); |
| 37 |
} |
| 38 |
|
| 39 |
public SWFInputStream(InputStream is, SWFSpriteTagSet tagSet, |
| 40 |
SWFActionSet actionSet) throws IOException { |
| 41 |
|
| 42 |
|
| 43 |
super(is, tagSet, actionSet, true); |
| 44 |
|
| 45 |
dictionary = new SWFDictionary(); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public float readFixed() throws IOException { |
| 52 |
|
| 53 |
byteAlign(); |
| 54 |
int frac = readUnsignedByte(); |
| 55 |
frac |= readUnsignedByte() << 8; |
| 56 |
int whole = readUnsignedByte(); |
| 57 |
whole |= readUnsignedByte() << 8; |
| 58 |
return ((float) whole) + ((float) frac) / ((float) 0x10000); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public float readFixed8() throws IOException { |
| 65 |
|
| 66 |
byteAlign(); |
| 67 |
int frac = readUnsignedByte(); |
| 68 |
int whole = readUnsignedByte(); |
| 69 |
return ((float) whole) + ((float) frac) / ((float) 0x100); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public Rectangle2D readRect() throws IOException { |
| 76 |
|
| 77 |
byteAlign(); |
| 78 |
int nbits = (int) readUBits(5); |
| 79 |
int xmin = (int) readSBits(nbits); |
| 80 |
int xmax = (int) readSBits(nbits); |
| 81 |
int ymin = (int) readSBits(nbits); |
| 82 |
int ymax = (int) readSBits(nbits); |
| 83 |
return new Rectangle2D.Double(xmin / TWIPS, ymin / TWIPS, (xmax - xmin) |
| 84 |
/ TWIPS, (ymax - ymin) / TWIPS); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public Color readColor(boolean alpha) throws IOException { |
| 91 |
int r = readUnsignedByte(); |
| 92 |
int g = readUnsignedByte(); |
| 93 |
int b = readUnsignedByte(); |
| 94 |
int a = (alpha) ? readUnsignedByte() : 255; |
| 95 |
return new Color(r, g, b, a); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public AffineTransform readMatrix() throws IOException { |
| 102 |
|
| 103 |
byteAlign(); |
| 104 |
|
| 105 |
|
| 106 |
float sx = 1.f; |
| 107 |
float sy = 1.f; |
| 108 |
float kx = 0.f; |
| 109 |
float ky = 0.f; |
| 110 |
float tx = 0.f; |
| 111 |
float ty = 0.f; |
| 112 |
|
| 113 |
|
| 114 |
if (readBitFlag()) { |
| 115 |
int nbits = (int) readUBits(5); |
| 116 |
sx = readFBits(nbits); |
| 117 |
sy = readFBits(nbits); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
if (readBitFlag()) { |
| 122 |
int nbits = (int) readUBits(5); |
| 123 |
kx = readFBits(nbits); |
| 124 |
ky = readFBits(nbits); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
int nbits = (int) readUBits(5); |
| 129 |
tx = (float) readSBits(nbits) / TWIPS; |
| 130 |
ty = (float) readSBits(nbits) / TWIPS; |
| 131 |
|
| 132 |
return new AffineTransform(sx, ky, kx, sy, tx, ty); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public String readString() throws IOException { |
| 139 |
if (getVersion() >= 6) { |
| 140 |
String s = readUTF(); |
| 141 |
readByte(); |
| 142 |
return s; |
| 143 |
} else { |
| 144 |
return readAsciiZString(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public int readLanguageCode() throws IOException { |
| 149 |
return readUnsignedByte(); |
| 150 |
} |
| 151 |
|
| 152 |
protected TagHeader readTagHeader() throws IOException { |
| 153 |
|
| 154 |
byteAlign(); |
| 155 |
int temp = read(); |
| 156 |
|
| 157 |
if (temp == -1) |
| 158 |
return null; |
| 159 |
|
| 160 |
temp |= readUnsignedByte() << 8; |
| 161 |
int tagID = temp >> 6; |
| 162 |
long length = temp & FIELD_MASK[5]; |
| 163 |
if (length == 0x3f) |
| 164 |
length = readUnsignedInt(); |
| 165 |
return new TagHeader(tagID, length); |
| 166 |
} |
| 167 |
|
| 168 |
protected ActionHeader readActionHeader() throws IOException { |
| 169 |
|
| 170 |
int actionCode = readUnsignedByte(); |
| 171 |
if (actionCode == 0) |
| 172 |
return null; |
| 173 |
|
| 174 |
int length = 0; |
| 175 |
if ((actionCode & 0x80) > 0) { |
| 176 |
length = readUnsignedShort(); |
| 177 |
} |
| 178 |
|
| 179 |
return new ActionHeader(actionCode, length); |
| 180 |
} |
| 181 |
|
| 182 |
private SWFHeader header; |
| 183 |
|
| 184 |
public SWFHeader readHeader() throws IOException { |
| 185 |
if (header == null) { |
| 186 |
header = new SWFHeader(this); |
| 187 |
int version = header.getVersion(); |
| 188 |
tagSet = new SWFTagSet(version); |
| 189 |
actionSet = new SWFActionSet(version); |
| 190 |
} |
| 191 |
return header; |
| 192 |
} |
| 193 |
|
| 194 |
public SWFDictionary getDictionary() { |
| 195 |
return dictionary; |
| 196 |
} |
| 197 |
|
| 198 |
public void setJPEGTable(byte[] table) { |
| 199 |
jpegTable = table; |
| 200 |
} |
| 201 |
|
| 202 |
public byte[] getJPEGTable() { |
| 203 |
return jpegTable; |
| 204 |
} |
| 205 |
|
| 206 |
public int getVersion() { |
| 207 |
return ((SWFSpriteTagSet) tagSet).getVersion(); |
| 208 |
} |
| 209 |
} |