View Javadoc

1   // Copyright 2002-2007, FreeHEP.
2   package org.freehep.graphicsio.emf.gdi;
3   
4   import java.awt.Color;
5   import java.awt.Rectangle;
6   import java.awt.geom.AffineTransform;
7   import java.awt.image.RenderedImage;
8   import java.awt.image.BufferedImage;
9   import java.io.IOException;
10  
11  import org.freehep.graphicsio.ImageGraphics2D;
12  import org.freehep.graphicsio.ImageConstants;
13  import org.freehep.graphicsio.emf.EMFConstants;
14  import org.freehep.graphicsio.emf.EMFInputStream;
15  import org.freehep.graphicsio.emf.EMFOutputStream;
16  import org.freehep.graphicsio.emf.EMFTag;
17  import org.freehep.graphicsio.emf.EMFImageLoader;
18  import org.freehep.graphicsio.emf.EMFRenderer;
19  import org.freehep.util.io.NoCloseOutputStream;
20  
21  /**
22   * PNG and JPG seem not to work.
23   * 
24   * @author Mark Donszelmann
25   * @version $Id: AlphaBlend.java 10367 2007-01-22 19:26:48Z duns $
26   */
27  public class AlphaBlend extends EMFTag implements EMFConstants {
28  
29      private final static int size = 108;
30  
31      private Rectangle bounds;
32  
33      private int x, y, width, height;
34  
35      private BlendFunction dwROP;
36  
37      private int xSrc, ySrc;
38  
39      private AffineTransform transform;
40  
41      private Color bkg;
42  
43      private int usage;
44  
45      private BitmapInfo bmi;
46  
47      private BufferedImage image;
48  
49      public AlphaBlend() {
50          super(114, 1);
51      }
52  
53      public AlphaBlend(
54          Rectangle bounds,
55          int x,
56          int y,
57          int width,
58          int height,
59          AffineTransform transform,
60          BufferedImage image,
61          Color bkg) {
62  
63          this();
64          this.bounds = bounds;
65          this.x = x;
66          this.y = y;
67          this.width = width;
68          this.height = height;
69          this.dwROP = new BlendFunction(AC_SRC_OVER, 0, 0xFF, AC_SRC_ALPHA);
70          this.xSrc = 0;
71          this.ySrc = 0;
72          this.transform = transform;
73          this.bkg = (bkg == null) ? new Color(0, 0, 0, 0) : bkg;
74          this.usage = DIB_RGB_COLORS;
75          this.image = image;
76          this.bmi = null;
77      }
78  
79      public EMFTag read(int tagID, EMFInputStream emf, int len)
80              throws IOException {
81  
82          AlphaBlend tag = new AlphaBlend();
83          tag.bounds = emf.readRECTL(); // 16
84          tag.x = emf.readLONG(); // 20
85          tag.y = emf.readLONG(); // 24
86          tag.width = emf.readLONG(); // 28
87          tag.height = emf.readLONG(); // 32
88          tag.dwROP = new BlendFunction(emf); // 36
89          tag.xSrc = emf.readLONG(); // 40
90          tag.ySrc = emf.readLONG(); // 44
91          tag.transform = emf.readXFORM(); // 68
92          tag.bkg = emf.readCOLORREF(); // 72
93          tag.usage = emf.readDWORD(); // 76
94  
95          // ignored
96          /* int bmiOffset = */ emf.readDWORD(); // 80
97          int bmiSize = emf.readDWORD(); // 84
98          /* int bitmapOffset = */ emf.readDWORD(); // 88
99          int bitmapSize = emf.readDWORD(); // 92
100 
101         /* int width = */ emf.readLONG(); // 96
102         /* int height = */ emf.readLONG(); // 100
103 
104         // FIXME: this size can differ and can be placed somewhere else
105         tag.bmi = (bmiSize > 0) ? new BitmapInfo(emf) : null;
106 
107         tag.image = EMFImageLoader.readImage(
108             tag.bmi.getHeader(),
109             tag.width,
110             tag.height,
111             emf,
112             bitmapSize,
113             tag.dwROP);
114 
115         return tag;
116     }
117 
118     public void write(int tagID, EMFOutputStream emf) throws IOException {
119         emf.writeRECTL(bounds);
120         emf.writeLONG(x);
121         emf.writeLONG(y);
122         emf.writeLONG(width);
123         emf.writeLONG(height);
124         dwROP.write(emf);
125         emf.writeLONG(xSrc);
126         emf.writeLONG(ySrc);
127         emf.writeXFORM(transform);
128         emf.writeCOLORREF(bkg);
129         emf.writeDWORD(usage);
130         emf.writeDWORD(size); // bmi follows this record immediately
131         emf.writeDWORD(BitmapInfoHeader.size);
132         emf.writeDWORD(size + BitmapInfoHeader.size); // bitmap follows bmi
133 
134         emf.pushBuffer();
135 
136         int encode;
137         // plain
138         encode = BI_RGB;
139         ImageGraphics2D.writeImage(
140             (RenderedImage) image,
141             ImageConstants.RAW.toLowerCase(),
142             ImageGraphics2D.getRAWProperties(bkg, "*BGRA"),
143             new NoCloseOutputStream(emf));
144 
145         // emf.writeImage(image, bkg, "*BGRA", 1);
146         // png
147         // encode = BI_PNG;
148         // ImageGraphics2D.writeImage(image, "png", new Properties(), new
149         // NoCloseOutputStream(emf));
150         // jpg
151         // encode = BI_JPEG;
152         // ImageGraphics2D.writeImage(image, "jpg", new Properties(), new
153         // NoCloseOutputStream(emf));
154         int length = emf.popBuffer();
155 
156         emf.writeDWORD(length);
157         emf.writeLONG(image.getWidth());
158         emf.writeLONG(image.getHeight());
159 
160         BitmapInfoHeader header = new BitmapInfoHeader(image.getWidth(), image
161                 .getHeight(), 32, encode, length, 0, 0, 0, 0);
162         bmi = new BitmapInfo(header);
163         bmi.write(emf);
164 
165         emf.append();
166     }
167 
168     public String toString() {
169         return super.toString() +
170             "\n  bounds: " + bounds +
171             "\n  x, y, w, h: " + x + " " + y + " " + width + " " + height +
172             "\n  dwROP: " + dwROP +
173             "\n  xSrc, ySrc: " + xSrc + " " + ySrc +
174             "\n  transform: " + transform +
175             "\n  bkg: " + bkg +
176             "\n  usage: " + usage +
177             "\n" + ((bmi != null) ? bmi.toString() : "  bitmap: null");
178     }
179 
180     /**
181      * displays the tag using the renderer
182      *
183      * @param renderer EMFRenderer storing the drawing session data
184      */
185     public void render(EMFRenderer renderer) {
186         // This function displays bitmaps that have transparent or semitransparent pixels.
187         if (image != null) {
188             renderer.drawImage(image, x, y, width, height);
189         }
190     }
191 }