| 1 |
|
| 2 |
package org.freehep.graphicsio.swf; |
| 3 |
|
| 4 |
import java.awt.Color; |
| 5 |
import java.awt.Image; |
| 6 |
import java.awt.image.BufferedImage; |
| 7 |
import java.awt.image.ImageObserver; |
| 8 |
import java.awt.image.RenderedImage; |
| 9 |
import java.awt.image.WritableRaster; |
| 10 |
import java.io.ByteArrayInputStream; |
| 11 |
import java.io.IOException; |
| 12 |
import java.util.Properties; |
| 13 |
import java.util.zip.InflaterInputStream; |
| 14 |
|
| 15 |
import org.freehep.graphicsbase.util.images.ImageUtilities; |
| 16 |
import org.freehep.graphicsio.ImageGraphics2D; |
| 17 |
import org.freehep.graphicsio.ImageConstants; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class DefineBitsJPEG3 extends DefineBitsJPEG2 { |
| 27 |
|
| 28 |
public DefineBitsJPEG3(int id, Image image, Properties options, Color bkg, |
| 29 |
ImageObserver observer) { |
| 30 |
this(id, ImageUtilities.createRenderedImage(image, observer, bkg), bkg, |
| 31 |
options); |
| 32 |
} |
| 33 |
|
| 34 |
public DefineBitsJPEG3(int id, RenderedImage image, Color bkg, |
| 35 |
Properties options) { |
| 36 |
this(); |
| 37 |
character = id; |
| 38 |
this.image = image; |
| 39 |
this.options = options; |
| 40 |
} |
| 41 |
|
| 42 |
public DefineBitsJPEG3() { |
| 43 |
super(35, 3); |
| 44 |
} |
| 45 |
|
| 46 |
public SWFTag read(int tagID, SWFInputStream swf, int len) |
| 47 |
throws IOException { |
| 48 |
|
| 49 |
DefineBitsJPEG3 tag = new DefineBitsJPEG3(); |
| 50 |
tag.character = swf.readUnsignedShort(); |
| 51 |
swf.getDictionary().put(tag.character, tag); |
| 52 |
int jpegLen = (int) swf.readUnsignedInt(); |
| 53 |
byte[] data = swf.readByte(jpegLen); |
| 54 |
|
| 55 |
ByteArrayInputStream bais = new ByteArrayInputStream(data); |
| 56 |
BufferedImage bi = ImageGraphics2D.readImage(ImageConstants.JPG.toLowerCase(), bais); |
| 57 |
if (bais.available() > 0) |
| 58 |
System.err.println("DefineBitsJPEG3: not all bytes read: " |
| 59 |
+ bais.available()); |
| 60 |
|
| 61 |
int width = bi.getWidth(); |
| 62 |
int height = bi.getHeight(); |
| 63 |
InflaterInputStream zip = new InflaterInputStream(swf); |
| 64 |
byte[] alpha = new byte[width * height]; |
| 65 |
zip.read(alpha); |
| 66 |
|
| 67 |
WritableRaster raster = bi.getAlphaRaster(); |
| 68 |
for (int y = 0; y < height; y++) { |
| 69 |
for (int x = 0; x < width; x++) { |
| 70 |
raster.setPixel(x, y, new int[] { alpha[y * width + x] }); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
tag.image = bi; |
| 75 |
return tag; |
| 76 |
} |
| 77 |
|
| 78 |
public void write(int tagID, SWFOutputStream swf) throws IOException { |
| 79 |
|
| 80 |
swf.writeUnsignedShort(character); |
| 81 |
swf.writeUnsignedInt(imageLength); |
| 82 |
swf.write(getImageBytes()); |
| 83 |
} |
| 84 |
|
| 85 |
public int getLength() throws IOException { |
| 86 |
return getImageBytes().length + 2; |
| 87 |
} |
| 88 |
|
| 89 |
private byte[] imageBytes; |
| 90 |
|
| 91 |
private int imageLength; |
| 92 |
|
| 93 |
private byte[] getImageBytes() throws IOException { |
| 94 |
if (imageBytes == null) { |
| 95 |
|
| 96 |
|
| 97 |
byte[] jpgBytes = ImageGraphics2D.toByteArray( |
| 98 |
image, ImageConstants.JPG, null, null); |
| 99 |
imageLength = jpgBytes.length; |
| 100 |
|
| 101 |
|
| 102 |
byte[] rawBytes = ImageGraphics2D.toByteArray( |
| 103 |
image, |
| 104 |
ImageConstants.RAW, |
| 105 |
ImageConstants.ENCODING_FLATE, |
| 106 |
ImageGraphics2D.getRAWProperties(Color.black, ImageConstants.COLOR_MODEL_A)); |
| 107 |
|
| 108 |
|
| 109 |
imageBytes = new byte[jpgBytes.length + rawBytes.length]; |
| 110 |
System.arraycopy(jpgBytes, 0, imageBytes, 0, jpgBytes.length); |
| 111 |
System.arraycopy(rawBytes, 0, imageBytes, jpgBytes.length, rawBytes.length); |
| 112 |
} |
| 113 |
return imageBytes; |
| 114 |
} |
| 115 |
|
| 116 |
public String toString() { |
| 117 |
StringBuffer s = new StringBuffer(); |
| 118 |
s.append(super.toString() + "\n"); |
| 119 |
s.append(" character: " + character + "\n"); |
| 120 |
s.append(" image: " + image + "\n"); |
| 121 |
return s.toString(); |
| 122 |
} |
| 123 |
} |