Skip to content

Commit 252cb9a

Browse files
committed
BufferedImage fixes for file-based image resources
1 parent 353730b commit 252cb9a

File tree

4 files changed

+105
-89
lines changed

4 files changed

+105
-89
lines changed

sources/net.sf.j2s.java.core/src/java/awt/image/BufferedImage.java

Lines changed: 83 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
import java.util.Hashtable;
4141
import java.util.Vector;
4242

43-
import javax.swing.SwingUtilities;
44-
4543
import sun.awt.image.ByteComponentRaster;
4644
import sun.awt.image.BytePackedRaster;
4745
import sun.awt.image.IntegerComponentRaster;
@@ -50,7 +48,6 @@
5048
import sun.awt.image.ShortComponentRaster;
5149
import sun.awt.image.SunWritableRaster;
5250
import swingjs.JSGraphics2D;
53-
import swingjs.JSToolkit;
5451
import swingjs.JSUtil;
5552
import swingjs.api.JSUtilI;
5653
import swingjs.api.js.DOMNode;
@@ -152,38 +149,38 @@ public String getStateString() {
152149
if ((秘state & (1<< i)) != 0)
153150
s += states[i] + ";";
154151
}
155-
return "rasterStolen=" + isDataStolen() + " " + (s == "" ? "UN" : s)
152+
return "rasterStolen=" + 秘isDataStolen() + " " + (s == "" ? "UN" : s)
156153
+ " unDisposedGraphicCount=" + gCount
157154
+ " data[0]=" + (data == null ? null : Integer.toHexString(data[0]));
158155
}
159156

160157
@SuppressWarnings("unused")
161-
private boolean havePixels() {
158+
private boolean 秘havePixels() {
162159
return ((秘state & STATE_HAVE_PIXELS) != 0);
163160
}
164161

165162
@SuppressWarnings("unused")
166-
private boolean havePixels8() {
163+
private boolean 秘havePixels8() {
167164
return ((秘state & STATE_HAVE_PIXELS8) != 0);
168165
}
169166

170-
private boolean havePixels32() {
167+
private boolean 秘havePixels32() {
171168
return ((秘state & STATE_HAVE_PIXELS32) != 0);
172169
}
173170

174-
private boolean haveImage() {
171+
private boolean 秘haveImage() {
175172
return ((秘state & STATE_IMAGENODE_AVAIL) != 0);
176173
}
177174

178-
private boolean haveRaster() {
175+
private boolean 秘haveRaster() {
179176
return ((秘state & STATE_RASTER) != 0);
180177
}
181178

182-
private boolean haveCanvas() {
179+
private boolean 秘haveCanvas() {
183180
return ((秘state & STATE_GRAPHICS) != 0);
184181
}
185182

186-
private boolean isDataStolen() {
183+
private boolean 秘isDataStolen() {
187184
boolean b = raster.dataBuffer.theTrackable.getState() == sun.java2d.StateTrackable.State.UNTRACKABLE;
188185
return b;
189186
}
@@ -903,13 +900,18 @@ public WritableRaster getRaster() {
903900
}
904901

905902
void 秘ensureRasterUpToDate() {
906-
if (isDataStolen() || !haveRaster() && 秘state != STATE_UNINITIALIZED) {
903+
if (秘isDataStolen() || !秘haveRaster() && 秘state != STATE_UNINITIALIZED) {
907904
秘updateStateFromHTML5Canvas(0, false);
908-
秘state &= ~(STATE_GRAPHICS | STATE_IMAGENODE_AVAIL);
905+
秘unsetGraphics();
909906
}
910907
秘state |= STATE_RASTER_AVAIL;
911908
}
912909

910+
private void 秘unsetGraphics() {
911+
秘state &= ~(STATE_GRAPHICS | STATE_IMAGENODE_AVAIL);
912+
秘imgNode = null;
913+
}
914+
913915
/**
914916
* Ensure that we do have int[] pixels, either because they were there already,
915917
* or by generating them from the associated HTML5 canvas.
@@ -920,10 +922,10 @@ public WritableRaster getRaster() {
920922
case TYPE_INT_RGB:
921923
case TYPE_INT_ARGB_PRE:// #3
922924
case TYPE_INT_ARGB:
923-
if (havePixels32()) {
924-
} else if (haveCanvas() || haveImage()) {// !秘haveFilePixels && (秘imgNode != null || 秘g != null)) {
925+
if (秘havePixels32()) {
926+
} else if (秘haveCanvas() || 秘haveImage()) {// !秘haveFilePixels && (秘imgNode != null || 秘g != null)) {
925927
秘updateStateFromHTML5Canvas(32, false);
926-
} else if (haveRaster() || 秘state == STATE_UNINITIALIZED) {
928+
} else if (秘haveRaster() || 秘state == STATE_UNINITIALIZED) {
927929
秘getPixelsFromRaster(32);
928930
}
929931
return true;
@@ -991,7 +993,7 @@ public WritableRaster getAlphaRaster() {
991993
* @see #setRGB(int, int, int, int, int[], int, int)
992994
*/
993995
public int getRGB(int x, int y) {
994-
if (!isDataStolen() && (haveCanvas()
996+
if (!秘isDataStolen() && (秘haveCanvas()
995997
|| 秘state == STATE_UNINITIALIZED)
996998
&& 秘ensureCanGetRGBPixels()
997999
) {
@@ -1031,8 +1033,8 @@ public int getRGB(int x, int y) {
10311033
* @see #setRGB(int, int, int, int, int[], int, int)
10321034
*/
10331035
public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) {
1034-
if (!isDataStolen()
1035-
&& (haveCanvas() || 秘state == STATE_IMAGENODE_AVAIL || 秘state == STATE_UNINITIALIZED)
1036+
if (!秘isDataStolen()
1037+
&& (秘haveCanvas() || 秘state == STATE_IMAGENODE_AVAIL || 秘state == STATE_UNINITIALIZED)
10361038
&& 秘ensureCanGetRGBPixels()
10371039
) {
10381040
int[] pixels = (int[]) 秘pix;
@@ -1110,7 +1112,7 @@ public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int of
11101112
* @see #getRGB(int, int, int, int, int[], int, int)
11111113
*/
11121114
public synchronized void setRGB(int x, int y, int rgb) {
1113-
if (!isDataStolen() && (haveCanvas()
1115+
if (!秘isDataStolen() && (秘haveCanvas()
11141116
|| 秘state == STATE_IMAGENODE_AVAIL || 秘state == STATE_UNINITIALIZED)
11151117
&& 秘ensureCanGetRGBPixels()
11161118
) {
@@ -1153,7 +1155,7 @@ public synchronized void setRGB(int x, int y, int rgb) {
11531155
* @see #getRGB(int, int, int, int, int[], int, int)
11541156
*/
11551157
public void setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) {
1156-
if (!isDataStolen() && (haveCanvas() || 秘state == STATE_IMAGENODE_AVAIL || 秘state == STATE_UNINITIALIZED)
1158+
if (!秘isDataStolen() && (秘haveCanvas() || 秘state == STATE_IMAGENODE_AVAIL || 秘state == STATE_UNINITIALIZED)
11571159
&& 秘ensureCanGetRGBPixels()
11581160
) {
11591161
int[] pixels = (int[]) 秘pix;
@@ -1173,7 +1175,7 @@ public void setRGB(int startX, int startY, int w, int h, int[] rgbArray, int off
11731175
}
11741176
}
11751177
}
1176-
秘state &= ~(STATE_GRAPHICS | STATE_IMAGENODE_AVAIL);
1178+
秘unsetGraphics();
11771179
秘state |= STATE_RASTER_AVAIL;
11781180
}
11791181

@@ -1306,7 +1308,7 @@ public Graphics getGraphics() {
13061308
* @return a <code>Graphics2D</code>, used for drawing into this image.
13071309
*/
13081310
public Graphics2D createGraphics() {
1309-
if (haveRaster())
1311+
if (秘haveRaster())
13101312
flush();
13111313
return (Graphics2D) 秘getImageGraphic().create();
13121314
// was:
@@ -1337,7 +1339,7 @@ public Graphics2D createGraphics() {
13371339
秘g = new JSGraphics2D(秘canvas);
13381340
}
13391341

1340-
if (haveRaster()) {
1342+
if (秘haveRaster()) {
13411343
// we need to draw the image now, because it might
13421344
// have pixels. Note that Java actually does not
13431345
// allow creating a Graphics from MemoryImageSource
@@ -1372,17 +1374,19 @@ public Graphics2D createGraphics() {
13721374

13731375
@Override
13741376
public void flush() {
1375-
if (isDataStolen())
1377+
if (秘isDataStolen())
13761378
秘state |= STATE_RASTER;
1377-
if (haveRaster() && 秘pix == null) {
1379+
if (秘haveRaster() && 秘pix == null) {
13781380
秘getPixelsFromRaster(0);
13791381
}
13801382
秘getImageGraphic();
13811383
while (gCount > 0 && --gCount >= 0)
13821384
秘g.dispose();
13831385
}
13841386

1385-
/** because we are using this in HTML5Canvas, which is a public interface
1387+
/** Called only in JavaScript,
1388+
* because we are using this in HTML5Canvas, which is a
1389+
* distributed public interface,
13861390
* and the Mandarin char was causing problems.
13871391
*
13881392
* @param node
@@ -1402,34 +1406,39 @@ public void _setImageNode(Object node, boolean async) {
14021406
* @param async
14031407
*/
14041408
public void 秘setImageNode(Object node, boolean async) {
1405-
Object pixels = 秘pix;
1409+
// Object pixels = 秘pix;
14061410
秘imgNode = (DOMNode) node;
1407-
if (秘state == STATE_VIDEO && DOMNode.getAttr(node, "tagName") == "VIDEO") {
1408-
1409-
/**
1410-
* @j2sNative
1411-
*
1412-
* document.body.appendChild(node);
1413-
*
1414-
*/
1415-
return;
1416-
}
1417-
if (async) {
1418-
//秘canvas.getContext("2d").drawImage(秘imgNode = node, 0, 0, width, height);
1419-
// we need one clock tick to access the pixels.
1420-
// and we need this to happen BEFORE the rest of the EventQUeue gets loaded.
1421-
JSToolkit.dispatch(new Runnable() {
1422-
1423-
@Override
1424-
public void run() {
1425-
秘installImage((DOMNode)node, pixels);
1426-
}
1427-
1428-
}, 1, 0);
1429-
} else {
1430-
秘installImage((DOMNode)node, pixels);
1431-
}
1432-
1411+
秘state = STATE_GRAPHICS | STATE_IMAGENODE_AVAIL;
1412+
1413+
// debugging only
1414+
// if (秘state == STATE_VIDEO && DOMNode.getAttr(node, "tagName") == "VIDEO") {
1415+
//
1416+
// /**
1417+
// * @j2sNative
1418+
// *
1419+
// * document.body.appendChild(node);
1420+
// *
1421+
// */
1422+
// return;
1423+
// }
1424+
1425+
// not nec since we have state
1426+
// if (async) {
1427+
// //秘canvas.getContext("2d").drawImage(秘imgNode = node, 0, 0, width, height);
1428+
// // we need one clock tick to access the pixels.
1429+
// // and we need this to happen BEFORE the rest of the EventQUeue gets loaded.
1430+
// JSToolkit.dispatch(new Runnable() {
1431+
//
1432+
// @Override
1433+
// public void run() {
1434+
// 秘installImage((DOMNode)node, pixels);
1435+
// }
1436+
//
1437+
// }, 1, 0);
1438+
// } else {
1439+
// 秘installImage((DOMNode)node, pixels);
1440+
// }
1441+
//
14331442
// this did not work:
14341443
//
14351444
// /**
@@ -1440,20 +1449,20 @@ public void run() {
14401449
//
14411450
}
14421451

1443-
protected void 秘installImage(DOMNode node, Object pixels) {
1444-
// just in case this is the first time we are doing this
1445-
// this does not work with a blob, however
1446-
秘pix = null;
1447-
if (秘canvas == null) {
1448-
Graphics g = 秘getImageGraphic(); // just get a canvas
1449-
g.dispose();
1450-
}
1451-
秘pix = pixels;
1452-
秘canvas.getContext("2d").drawImage(秘imgNode = node, 0, 0, width, height);
1453-
秘setImageFromHTML5Canvas(this.秘g);
1454-
raster.秘setStable(true);
1455-
秘state |= STATE_IMAGENODE_AVAIL;
1456-
}
1452+
// protected void 秘installImage(DOMNode node, Object pixels) {
1453+
// // just in case this is the first time we are doing this
1454+
// // this does not work with a blob, however
1455+
// 秘pix = null;
1456+
// if (秘canvas == null) {
1457+
// Graphics g = 秘getImageGraphic(); // just get a canvas
1458+
// g.dispose();
1459+
// }
1460+
// 秘pix = pixels;
1461+
// 秘canvas.getContext("2d").drawImage(秘imgNode = node, 0, 0, width, height);
1462+
// //秘setPixels((int[])pixels);
1463+
// raster.秘setStable(true);
1464+
// 秘state = STATE_GRAPHICS | STATE_IMAGENODE_AVAIL;
1465+
// }
14571466

14581467
/**
14591468
* Get or create a DOM image node, as needed. Images could be from:
@@ -2155,7 +2164,7 @@ private void toInt3BGR(byte[] ctxData, int[] buf) {
21552164
*
21562165
*/
21572166
public Object get秘pixFromRaster() {
2158-
if (!haveRaster())
2167+
if (!秘haveRaster())
21592168
return null;
21602169
SunWritableRaster r = (SunWritableRaster) raster;
21612170
switch (imageType) {
@@ -2297,7 +2306,7 @@ private void toInt3BGR(byte[] ctxData, int[] buf) {
22972306
public DOMNode 秘getImageNode(int mode) {
22982307
if (秘state == STATE_VIDEO)
22992308
return 秘imgNode;
2300-
if (isDataStolen())
2309+
if (秘isDataStolen())
23012310
秘state |= STATE_RASTER_AVAIL;
23022311

23032312
// If we have raster data and are not forcing, return the canvas if it exists.
@@ -2307,10 +2316,10 @@ private void toInt3BGR(byte[] ctxData, int[] buf) {
23072316
switch (mode) {
23082317
default:
23092318
case GET_IMAGE_ALLOW_NULL:
2310-
return isDataStolen() ? null : (DOMNode) (haveRaster() || 秘canvas != null ? 秘canvas
2311-
: 秘imgNode != null ? 秘imgNode : createImageNode());
2319+
return 秘haveImage() ? 秘imgNode : 秘isDataStolen() ? null : (DOMNode) (秘haveRaster() || 秘canvas != null ? 秘canvas
2320+
: createImageNode());
23122321
case GET_IMAGE_FOR_ICON:
2313-
if (!isDataStolen())
2322+
if (!秘isDataStolen())
23142323
return (DOMNode) (秘canvas != null ? 秘canvas : 秘imgNode != null ? 秘imgNode : createImageNode());
23152324
// $fall-through$
23162325
case GET_IMAGE_FROM_RASTER:
@@ -2319,7 +2328,7 @@ private void toInt3BGR(byte[] ctxData, int[] buf) {
23192328
秘g = null;
23202329
秘getImageGraphic();
23212330
DOMNode canvas = 秘g.getCanvas();
2322-
秘state &= ~(STATE_GRAPHICS | STATE_IMAGENODE_AVAIL);
2331+
秘unsetGraphics();
23232332
// only a temporary creation, since we don't know if the raster will change
23242333
秘g = null;
23252334
return canvas;

sources/net.sf.j2s.java.core/src/javax/swing/ImageIcon.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ public class ImageIcon implements Icon {
139139

140140
int width = -1;
141141
int height = -1;
142+
private boolean 秘video;
143+
144+
public boolean 秘isVideo() {
145+
return 秘video;
146+
}
142147

143148
/**
144149
* Creates an ImageIcon from the specified file. The image will
@@ -150,6 +155,7 @@ public class ImageIcon implements Icon {
150155
*/
151156
public ImageIcon(String filename, String description) {
152157
if (description != null && description.indexOf("jsvideo") >= 0) {
158+
秘video = true;
153159
image = ((JSToolkit)Toolkit.getDefaultToolkit()).getVideo(filename);
154160
} else {
155161
image = Toolkit.getDefaultToolkit().getImage(filename);

sources/net.sf.j2s.java.core/src/swingjs/JSImagekit.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,17 @@ public static ImageIcon createImageIcon(Component c, Icon icon, String id) {
284284
img = (BufferedImage) imgIcon.getImage();
285285
g = img.秘g;
286286
} else {
287-
g = createCanvasGraphics(width, height, id);
288-
// A JSGraphics2D is not a real Graphics object - must coerce
289-
ColorModel cm = ColorModel.getRGBdefault();
290-
img = new BufferedImage(cm, cm.createCompatibleWritableRaster(width, height), false, null);
287+
img = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_HTML5);
288+
g = (JSGraphics2D) (Object) img.createGraphics();
289+
// createCanvasGraphics(width, height, id);
290+
// // A JSGraphics2D is not a real Graphics object - must coerce
291+
// ColorModel cm = ColorModel.getRGBdefault();
292+
// img = new BufferedImage(cm, cm.createCompatibleWritableRaster(width, height), false, null);
291293
imgIcon = new ImageIcon(img, "paintedIcon");
294+
// img.getRaster().秘setStable(true);
292295
}
293296
icon.paintIcon(c, (Graphics)(Object) g, 0, 0);
294-
img.秘setImageFromHTML5Canvas(g);
297+
// img.秘setImageFromHTML5Canvas(g);
295298
g.dispose();
296299
((ImageIcon) icon).秘tempIcon = imgIcon;
297300
return imgIcon;

0 commit comments

Comments
 (0)