1
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
23
24
25
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();
84 tag.x = emf.readLONG();
85 tag.y = emf.readLONG();
86 tag.width = emf.readLONG();
87 tag.height = emf.readLONG();
88 tag.dwROP = new BlendFunction(emf);
89 tag.xSrc = emf.readLONG();
90 tag.ySrc = emf.readLONG();
91 tag.transform = emf.readXFORM();
92 tag.bkg = emf.readCOLORREF();
93 tag.usage = emf.readDWORD();
94
95
96
97 int bmiSize = emf.readDWORD();
98
99 int bitmapSize = emf.readDWORD();
100
101
102
103
104
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);
131 emf.writeDWORD(BitmapInfoHeader.size);
132 emf.writeDWORD(size + BitmapInfoHeader.size);
133
134 emf.pushBuffer();
135
136 int encode;
137
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
146
147
148
149
150
151
152
153
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
182
183
184
185 public void render(EMFRenderer renderer) {
186
187 if (image != null) {
188 renderer.drawImage(image, x, y, width, height);
189 }
190 }
191 }