-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSGraphicsCompositor.java
More file actions
334 lines (310 loc) · 8 KB
/
Copy pathJSGraphicsCompositor.java
File metadata and controls
334 lines (310 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package swingjs;
import jsjava.awt.AlphaComposite;
import jsjava.awt.Image;
import jsjava.awt.image.AffineTransformOp;
import jsjava.awt.image.BufferedImage;
import jsjava.awt.image.BufferedImageOp;
import jsjava.awt.image.ByteLookupTable;
import jsjava.awt.image.ConvolveOp;
import jsjava.awt.image.Kernel;
import jsjava.awt.image.LookupOp;
import jsjava.awt.image.LookupTable;
import jsjava.awt.image.Raster;
import jsjava.awt.image.RasterOp;
import jsjava.awt.image.RescaleOp;
import jsjava.awt.image.WritableRaster;
import jssun.awt.image.SunWritableRaster;
import swingjs.api.DOMNode;
public class JSGraphicsCompositor {
private static double[] mat6;
JSGraphicsCompositor() {
// for reflection
}
/**
* apply a source/destination rule to a canvas.context2d object
*
* see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
*
*
* @param g
* @param alphaRule
* @return
*/
public boolean setGraphicsCompositeAlpha(JSGraphics2D g, int alphaRule) {
// source-over This is the default setting and draws new shapes on top of
// the existing canvas content.
// source-in The new shape is drawn only where both the new shape and the
// destination canvas overlap. Everything else is made transparent.
// source-out The new shape is drawn where it doesn't overlap the existing
// canvas content.
// source-atop The new shape is only drawn where it overlaps the existing
// canvas content.
// lighter Where both shapes overlap the color is determined by adding color
// values.
// xor Shapes are made transparent where both overlap and drawn normal
// everywhere else.
// destination-over New shapes are drawn behind the existing canvas content.
// destination-in The existing canvas content is kept where both the new
// shape and existing canvas content overlap. Everything else is made
// transparent.
// destination-out The existing content is kept where it doesn't overlap the
// new shape.
// destination-atop The existing canvas is only kept where it overlaps the
// new shape. The new shape is drawn behind the canvas content.
// darker Where both shapes overlap the color is determined by subtracting
// color values.
@SuppressWarnings("unused")
String s = null;
switch (alphaRule) {
default:
case AlphaComposite.SRC_OVER:
s = "source-over";
break;
case AlphaComposite.SRC_IN:
s = "source-in";
break;
case AlphaComposite.SRC_OUT:
s = "source-out";
break;
case AlphaComposite.SRC_ATOP:
s = "source-atop";
break;
case AlphaComposite.XOR:
s = "xor";
break;
case AlphaComposite.DST_OVER:
s = "destination-over";
break;
case AlphaComposite.DST_IN:
s = "destination-in";
break;
case AlphaComposite.DST_OUT:
s = "destination-out";
break;
case AlphaComposite.DST_ATOP:
s = "destination-atop";
break;
}
/**
* @j2sNative
*
* g.ctx.globalCompositeOperation = s;
*/
{
}
return true;
}
/**
* apply a BufferedImageOp to an image enroute to
*
* @param g
* @param img
* @param op
* @param x
* @param y
* @return
*/
public boolean drawImageOp(JSGraphics2D g, BufferedImage img,
BufferedImageOp op, int x, int y) {
int type = 0;
/**
* @j2sNative
*
* type = op.swingJStype;
*
*/
{
}
switch (type) {
case 'R':
// HTML5 can only do simple alpha rescaling
RescaleOp rop = (RescaleOp) op;
float[] offsets = rop.offsets;
float[] scaleFactors = rop.scaleFactors;
boolean canDo = (offsets.length == 4 && offsets[3] == 0);
if (canDo)
for (int i = 0; i < 3; i++)
if (offsets[i] != 0 || scaleFactors[i] != 1) {
canDo = false;
break;
}
if (canDo) {
g.setAlpha(scaleFactors[3]);
g.drawImagePriv(img, x, y, null);
g.setAlpha(1);
return true;
}
break;
case 'L':
// SwingJS TODO
break;
case 'A':
// SwingJS TODO
break;
case 'C':
// SwingJS TODO
break;
}
return false;
}
public WritableRaster filterRaster(Raster src, WritableRaster dst, RasterOp op) {
// Create the destination tile
if (dst == null) {
dst = op.createCompatibleDestRaster(src);
}
WritableRaster retRaster = null;
int type = 0;
/**
* @j2sNative
*
* type = op.swingJStype;
*
*/
{
}
switch (type) {
case 'L':
LookupTable table = ((LookupOp) op).getTable();
if (table instanceof ByteLookupTable) {
ByteLookupTable bt = (ByteLookupTable) table;
if (lookupByteRaster(src, dst, bt.getTable()) > 0) {
retRaster = dst;
}
}
break;
case 'A':
AffineTransformOp bOp = (AffineTransformOp) op;
double[] matrix = (mat6 == null ? mat6 = new double[6] : mat6);
bOp.getTransform().getMatrix(matrix);
if (transformRaster(src, dst, matrix, bOp.getInterpolationType()) > 0) {
retRaster = dst;
}
break;
case 'C':
ConvolveOp cOp = (ConvolveOp) op;
if (convolveRaster(src, dst, cOp.getKernel(), cOp.getEdgeCondition()) > 0) {
retRaster = dst;
}
break;
default:
break;
}
if (retRaster != null) {
SunWritableRaster.markDirty(retRaster);
}
return retRaster;
}
private int convolveRaster(Raster src, WritableRaster dst, Kernel kernel,
int edgeCondition) {
// SwingJS TODO
return 0;
}
private int transformRaster(Raster src, WritableRaster dst, double[] matrix,
int interpolationType) {
// SwingJS TODO
return 0;
}
private int lookupByteRaster(Raster src, WritableRaster dst, byte[][] table) {
// SwingJS TODO
return 0;
}
public BufferedImage filterImage(BufferedImage src, BufferedImage dst,
BufferedImageOp op) {
BufferedImage retBI = null;
int type = 0;
/**
* @j2sNative
*
* type = op.swingJStype;
*
*/
{
}
switch (type) {
default:
retBI = op.filter(src, dst);
break;
case 'A':
dst.getImageGraphic().drawImage(src,
((AffineTransformOp) op).getTransform(), null);
retBI = dst;
break;
case 'L':
// not implemented
LookupTable table = ((LookupOp) op).getTable();
if (table.getOffset() != 0) {
// Right now the native code doesn't support offsets
return null;
}
if (table instanceof ByteLookupTable) {
ByteLookupTable bt = (ByteLookupTable) table;
if (lookupByteBI(src, dst, bt.getTable()) > 0) {
retBI = dst;
}
}
break;
case 'C':
// not implemented
ConvolveOp cOp = (ConvolveOp) op;
if (convolveBI(src, dst, cOp.getKernel(), cOp.getEdgeCondition()) > 0) {
retBI = dst;
}
break;
}
// if (retBI != null) {
// SunWritableRaster.markDirty(retBI);
// }
//
return retBI;
}
private int convolveBI(BufferedImage src, BufferedImage dst, Kernel kernel,
int edgeCondition) {
// SwingJS TODO
return 0;
}
private int lookupByteBI(BufferedImage src, BufferedImage dst, byte[][] table) {
// SwingJS TODO
return 0;
}
/**
* Get or create a DOM image node, as needed.
* Images could be from:
*
* a) java.awt.Toolkit.createImage()
* b) javax.imageio.ImageIO.read()
* c) new java.awt.BufferedImage()
*
*
*
* @param img
* @return
*/
public DOMNode createImageNode(Image img) {
DOMNode imgNode = null;
/**
* @j2sNative
*
* imgNode = img._imgNode;
*
*/
{}
if (imgNode == null && img instanceof BufferedImage) {
/**
* @j2sNative
*
* var canvas = img._canvas;
* if (canvas == null) {
* img.getGraphics();
* canvas = img._canvas;
* }
* imgNode = canvas;
* imgNode.style.width = img.getWidth() + "px";
* imgNode.style.height = img.getHeight() + "px";
*
*/
{}
// note: images created some other way are presumed to have int[] pix defined and possibly byte[pix]
}
return imgNode;
}
}