View Javadoc

1   // Copyright 2001-2007, FreeHEP.
2   package org.freehep.graphicsio.swf;
3   
4   import java.awt.Color;
5   import java.awt.Image;
6   import java.awt.image.ImageObserver;
7   import java.awt.image.RenderedImage;
8   import java.io.ByteArrayInputStream;
9   import java.io.IOException;
10  import java.util.Properties;
11  
12  import org.freehep.graphicsio.ImageGraphics2D;
13  import org.freehep.graphicsio.ImageConstants;
14  import org.freehep.util.images.ImageUtilities;
15  
16  /**
17   * DefineBitsJPEG2 TAG.
18   * 
19   * @author Mark Donszelmann
20   * @author Charles Loomis
21   * @version $Id: DefineBitsJPEG2.java 10269 2007-01-09 00:32:55Z duns $
22   */
23  public class DefineBitsJPEG2 extends DefinitionTag {
24  
25      protected int character;
26  
27      protected RenderedImage image;
28  
29      protected Properties options;
30  
31      public DefineBitsJPEG2(int id, Image image, Properties options, Color bkg,
32              ImageObserver observer) {
33          this(id, ImageUtilities.createRenderedImage(image, observer, bkg), bkg,
34                  options);
35      }
36  
37      public DefineBitsJPEG2(int id, RenderedImage image, Color bkg,
38              Properties options) {
39          this();
40          character = id;
41          this.image = image;
42          this.options = options;
43      }
44  
45      public DefineBitsJPEG2() {
46          super(21, 2);
47      }
48  
49      protected DefineBitsJPEG2(int tag, int level) {
50          super(tag, level);
51      }
52  
53      public SWFTag read(int tagID, SWFInputStream swf, int len)
54              throws IOException {
55  
56          DefineBitsJPEG2 tag = new DefineBitsJPEG2();
57          tag.character = swf.readUnsignedShort();
58          swf.getDictionary().put(tag.character, tag);
59  
60          byte[] data = swf.readByte(len - 2);
61  
62          ByteArrayInputStream bais = new ByteArrayInputStream(data);
63          tag.image = ImageGraphics2D.readImage(ImageConstants.JPG.toLowerCase(), bais);
64  
65          if (bais.available() > 0)
66              System.err.println("DefineBitsJPEG2: not all bytes read: "
67                      + bais.available());
68  
69          return tag;
70      }
71  
72      public void write(int tagID, SWFOutputStream swf) throws IOException {
73  
74          swf.writeUnsignedShort(character);
75          swf.write(getImageBytes());
76      }
77  
78      public int getLength() throws IOException {
79          return getImageBytes().length + 2;
80      }
81  
82      private byte[] imageBytes;
83  
84      private byte[] getImageBytes() throws IOException {
85          if (imageBytes == null) {
86               imageBytes = ImageGraphics2D.toByteArray(
87                  image, ImageConstants.JPG, null, options);
88          }
89          return imageBytes;
90      }
91  
92      public String toString() {
93          StringBuffer s = new StringBuffer();
94          s.append(super.toString() + "\n");
95          s.append("  character:  " + character + "\n");
96          s.append("  image:      " + image + "\n");
97          return s.toString();
98      }
99  }