Skip to content

Commit 419336c

Browse files
committed
BufferedImage fixes for TRACKABLE (Jmol)
1 parent d92f54f commit 419336c

24 files changed

+224
-204
lines changed
420 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200529073141
1+
20200529133959
420 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200529073141
1+
20200529133959
421 KB
Binary file not shown.

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

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public String getStateString() {
151151
if ((秘state & (1<< i)) != 0)
152152
s += states[i] + ";";
153153
}
154-
return "rasterStolen=" + dataStolen() + " " + (s == "" ? "UN" : s)
154+
return "rasterStolen=" + isDataStolen() + " " + (s == "" ? "UN" : s)
155155
+ " unDisposedGraphicCount=" + gCount
156156
+ " data[0]=" + (data == null ? null : Integer.toHexString(data[0]));
157157
}
@@ -182,7 +182,7 @@ private boolean haveCanvas() {
182182
return ((秘state & STATE_GRAPHICS) != 0);
183183
}
184184

185-
private boolean dataStolen() {
185+
private boolean isDataStolen() {
186186
boolean b = raster.dataBuffer.theTrackable.getState() == sun.java2d.StateTrackable.State.UNTRACKABLE;
187187
return b;
188188
}
@@ -606,20 +606,18 @@ public BufferedImage(int width, int height, int imageType) {
606606
colorModel = new IndexColorModel(8, 256, cmap, 0, false, -1, DataBuffer.TYPE_BYTE);
607607
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, 1, null);
608608
break;
609-
case TYPE_USHORT_565_RGB: {
609+
case TYPE_USHORT_565_RGB:
610610
colorModel = new DirectColorModel(16, DCM_565_RED_MASK, DCM_565_GRN_MASK, DCM_565_BLU_MASK);
611611
raster = colorModel.createCompatibleWritableRaster(width, height);
612-
}
613612
break;
614-
case TYPE_USHORT_555_RGB: {
613+
case TYPE_USHORT_555_RGB:
615614
colorModel = new DirectColorModel(15, DCM_555_RED_MASK, DCM_555_GRN_MASK, DCM_555_BLU_MASK);
616615
raster = colorModel.createCompatibleWritableRaster(width, height);
617-
}
618616
break;
619-
620617
default:
621618
throw new IllegalArgumentException("Unknown image type " + imageType);
622619
}
620+
raster.秘setStable(true);
623621
this.imageType = imageType;
624622
}
625623

@@ -904,7 +902,7 @@ public WritableRaster getRaster() {
904902
}
905903

906904
void 秘ensureRasterUpToDate() {
907-
if (dataStolen() || !haveRaster() && 秘state != STATE_UNINITIALIZED) {
905+
if (isDataStolen() || !haveRaster() && 秘state != STATE_UNINITIALIZED) {
908906
秘updateStateFromHTML5Canvas(0, false);
909907
秘state &= ~(STATE_GRAPHICS | STATE_IMAGENODE_AVAIL);
910908
}
@@ -992,7 +990,7 @@ public WritableRaster getAlphaRaster() {
992990
* @see #setRGB(int, int, int, int, int[], int, int)
993991
*/
994992
public int getRGB(int x, int y) {
995-
if (!dataStolen() && (haveCanvas()
993+
if (!isDataStolen() && (haveCanvas()
996994
|| 秘state == STATE_UNINITIALIZED)
997995
&& 秘ensureCanGetRGBPixels()
998996
) {
@@ -1032,7 +1030,7 @@ public int getRGB(int x, int y) {
10321030
* @see #setRGB(int, int, int, int, int[], int, int)
10331031
*/
10341032
public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) {
1035-
if (!dataStolen()
1033+
if (!isDataStolen()
10361034
&& (haveCanvas() || 秘state == STATE_IMAGENODE_AVAIL || 秘state == STATE_UNINITIALIZED)
10371035
&& 秘ensureCanGetRGBPixels()
10381036
) {
@@ -1111,7 +1109,7 @@ public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int of
11111109
* @see #getRGB(int, int, int, int, int[], int, int)
11121110
*/
11131111
public synchronized void setRGB(int x, int y, int rgb) {
1114-
if (!dataStolen() && (haveCanvas()
1112+
if (!isDataStolen() && (haveCanvas()
11151113
|| 秘state == STATE_IMAGENODE_AVAIL || 秘state == STATE_UNINITIALIZED)
11161114
&& 秘ensureCanGetRGBPixels()
11171115
) {
@@ -1154,7 +1152,7 @@ public synchronized void setRGB(int x, int y, int rgb) {
11541152
* @see #getRGB(int, int, int, int, int[], int, int)
11551153
*/
11561154
public void setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) {
1157-
if (!dataStolen() && (haveCanvas() || 秘state == STATE_IMAGENODE_AVAIL || 秘state == STATE_UNINITIALIZED)
1155+
if (!isDataStolen() && (haveCanvas() || 秘state == STATE_IMAGENODE_AVAIL || 秘state == STATE_UNINITIALIZED)
11581156
&& 秘ensureCanGetRGBPixels()
11591157
) {
11601158
int[] pixels = (int[]) 秘pix;
@@ -1373,7 +1371,7 @@ public Graphics2D createGraphics() {
13731371

13741372
@Override
13751373
public void flush() {
1376-
if (dataStolen())
1374+
if (isDataStolen())
13771375
秘state |= STATE_RASTER;
13781376
if (haveRaster() && 秘pix == null) {
13791377
秘getPixelsFromRaster(0);
@@ -1452,6 +1450,7 @@ public void run() {
14521450
秘pix = pixels;
14531451
秘canvas.getContext("2d").drawImage(秘imgNode = node, 0, 0, width, height);
14541452
秘setImageFromHTML5Canvas(this.秘g);
1453+
raster.秘setStable(true);
14551454
秘state |= STATE_IMAGENODE_AVAIL;
14561455
}
14571456

@@ -1986,14 +1985,24 @@ public int getTransparency() {
19861985
int h = height;
19871986
byte[] data = HTML5Canvas.getDataBufferBytes(canvas, node, w, h);
19881987
DataBuffer buf = raster.getDataBuffer();
1989-
// from
19901988
秘pix = null;
1989+
boolean haveHTML5Pixels = false;
19911990
switch (imageType) {
19921991
case TYPE_INT_RGB:
19931992
case TYPE_INT_ARGB_PRE:// #3
19941993
case TYPE_INT_ARGB:
19951994
// convert canvas [r g b a r g b a ...] into [argb argb argb ...]
19961995
toIntARGB(data, (int[]) (秘pix = ((DataBufferInt) buf).data));
1996+
// Jmol, for instance, uses a 4 * w * h buffer for antialiasing
1997+
if (((int[])秘pix).length != 秘wxh)
1998+
秘pix = null;
1999+
break;
2000+
case TYPE_4BYTE_HTML5:
2001+
data = ((DataBufferByte) buf).data;
2002+
if (nBits != 32) {
2003+
秘pix = data;
2004+
haveHTML5Pixels = true;
2005+
}
19972006
break;
19982007
case TYPE_INT_BGR: // #4
19992008
// convert canvas [r g b a r g b a ...] into [0bgr 0bgr 0bgr ...]
@@ -2024,15 +2033,11 @@ public int getTransparency() {
20242033
JSUtil.notImplemented("BufferedImage setPixels for type " + imageType);
20252034
break;
20262035
}
2027-
if (nBits == 32 && ((int[])秘pix).length != 秘wxh) {
2028-
data = (byte[]) 秘pix;
2029-
秘pix = null;
2030-
}
2031-
if (秘pix == null)
2036+
if (秘pix == null) {
20322037
toIntARGB(data, (int[]) (秘pix = new int[data.length >> 2]));
2033-
2038+
}
20342039
秘state = STATE_GRAPHICS_AVAIL | STATE_RASTER_AVAIL;
2035-
秘state |= (((int[]) 秘pix).length == 秘wxh ? STATE_HAVE_PIXELS32 : STATE_HAVE_PIXELS8);
2040+
秘state |= (haveHTML5Pixels ? STATE_HAVE_PIXELS8 : STATE_HAVE_PIXELS32);
20362041
秘imgNode = (andSetImageNode ? canvas : null);
20372042
if (andSetImageNode) {
20382043
秘state |= STATE_IMAGENODE_AVAIL;
@@ -2150,19 +2155,19 @@ private void toInt3BGR(byte[] ctxData, int[] buf) {
21502155
*/
21512156
public Object get秘pixFromRaster() {
21522157
if (!haveRaster())
2153-
return null;
2154-
2158+
return null;
21552159
SunWritableRaster r = (SunWritableRaster) raster;
21562160
switch (imageType) {
21572161
case TYPE_INT_RGB:
21582162
case TYPE_INT_ARGB:
2163+
// Jmol, for instance, uses a 4*w*h raster when antialiased
2164+
if (((DataBufferInt)r.dataBuffer).data.length != 秘wxh)
2165+
break;
2166+
return ((DataBufferInt) r.dataBuffer).data;
21592167
case TYPE_4BYTE_HTML5:
2160-
// if the raster has been created using Raster.createWritableRaster, so we
2161-
// already have its pixels.
2162-
return (r.秘pix == null ? 秘pix : r.秘pix);
2163-
default:
2164-
return 秘getPixelsFromRaster(8);
2168+
return 秘pix;
21652169
}
2170+
return 秘getPixelsFromRaster(8);
21662171
}
21672172

21682173
/**
@@ -2291,7 +2296,7 @@ private void toInt3BGR(byte[] ctxData, int[] buf) {
22912296
public DOMNode 秘getImageNode(int mode) {
22922297
if (秘state == STATE_VIDEO)
22932298
return 秘imgNode;
2294-
if (dataStolen())
2299+
if (isDataStolen())
22952300
秘state |= STATE_RASTER_AVAIL;
22962301

22972302
// If we have raster data and are not forcing, return the canvas if it exists.
@@ -2301,10 +2306,10 @@ private void toInt3BGR(byte[] ctxData, int[] buf) {
23012306
switch (mode) {
23022307
default:
23032308
case GET_IMAGE_ALLOW_NULL:
2304-
return dataStolen() ? null : (DOMNode) (haveRaster() || 秘canvas != null ? 秘canvas
2309+
return isDataStolen() ? null : (DOMNode) (haveRaster() || 秘canvas != null ? 秘canvas
23052310
: 秘imgNode != null ? 秘imgNode : createImageNode());
23062311
case GET_IMAGE_FOR_ICON:
2307-
if (!dataStolen())
2312+
if (!isDataStolen())
23082313
return (DOMNode) (秘canvas != null ? 秘canvas : 秘imgNode != null ? 秘imgNode : createImageNode());
23092314
// $fall-through$
23102315
case GET_IMAGE_FROM_RASTER:
@@ -2338,6 +2343,7 @@ private void toInt3BGR(byte[] ctxData, int[] buf) {
23382343
case TYPE_INT_ARGB_PRE:
23392344
case TYPE_INT_ARGB:
23402345
((DataBufferInt) raster.dataBuffer).data = ((IntegerComponentRaster) raster).data = argb;
2346+
raster.秘setStable(true);
23412347
秘state |= STATE_RASTER_AVAIL;
23422348
break;
23432349
default:

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
package java.awt.image;
4040

41-
import static sun.java2d.StateTrackable.State.STABLE;
41+
import static sun.java2d.StateTrackable.State.UNTRACKABLE;
4242

4343
import sun.awt.image.DataStealer;
4444
import sun.awt.image.SunWritableRaster;
@@ -111,22 +111,29 @@ public abstract class DataBuffer {
111111

112112
public BufferedImage 秘image;
113113

114-
protected void 秘setUntrackable() {
115-
秘checkImage();
114+
public void 秘setUntrackable() {
116115
theTrackable.setUntrackable();
116+
秘checkImage();
117117
}
118118

119+
/**
120+
* track a nested set of calls to avoid image conversion once it has been done
121+
* once within a method; positive values skip checking during for loops, for example,
122+
* while 0 indicates we need to check for changes.
123+
*/
119124
protected int checkImageDepth = 0;
120125

121126
/**
122-
* start with false, end with true; can be nested.
127+
* Ensure that the associated BufferedImage has converted any
128+
* changes generated using Graphics to its bitmap. But then
129+
* turn off (false) this feature until we turn it back on (true)
123130
*
124-
* @param b
131+
* @param allowChecking set to false before a series of transfers, then finally set that true
125132
*/
126-
public void 秘setDoCheckImage(boolean b) {
127-
if (!b && checkImageDepth == 0 && 秘image != null)
128-
秘image.秘ensureRasterUpToDate();
129-
checkImageDepth = Math.max(0, checkImageDepth + (b ? 1 : -1));
133+
public void 秘setDoCheckImage(boolean allowChecking) {
134+
if (!allowChecking)
135+
秘checkImage();
136+
checkImageDepth = Math.max(0, checkImageDepth + (allowChecking ? -1 : 1));
130137
}
131138

132139
/**
@@ -167,7 +174,7 @@ public static int getDataTypeSize(int type) {
167174
* @param size the size of the banks
168175
*/
169176
protected DataBuffer(int dataType, int size) {
170-
this(STABLE, dataType, size);
177+
this(UNTRACKABLE, dataType, size);
171178
}
172179

173180
/**
@@ -197,7 +204,7 @@ protected DataBuffer(int dataType, int size) {
197204
* @param numBanks the number of banks in this <code>DataBuffer</code>
198205
*/
199206
protected DataBuffer(int dataType, int size, int numBanks) {
200-
this(STABLE, dataType, size, numBanks);
207+
this(UNTRACKABLE, dataType, size, numBanks);
201208
}
202209

203210
/**
@@ -230,7 +237,7 @@ protected DataBuffer(int dataType, int size, int numBanks) {
230237
* @param offset the offset for each bank
231238
*/
232239
protected DataBuffer(int dataType, int size, int numBanks, int offset) {
233-
this(STABLE, dataType, size, numBanks, offset);
240+
this(UNTRACKABLE, dataType, size, numBanks, offset);
234241
}
235242

236243
/**
@@ -272,7 +279,7 @@ protected DataBuffer(int dataType, int size, int numBanks, int offset) {
272279
* <code>offsets</code>
273280
*/
274281
protected DataBuffer(int dataType, int size, int numBanks, int offsets[]) {
275-
this(STABLE, dataType, size, numBanks, offsets);
282+
this(UNTRACKABLE, dataType, size, numBanks, offsets);
276283
}
277284

278285
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public final class DataBufferByte extends DataBuffer
7676
* @param size The size of the <CODE>DataBuffer</CODE>.
7777
*/
7878
public DataBufferByte(int size) {
79-
super(STABLE, TYPE_BYTE, size);
79+
super(/*?*/UNTRACKABLE, TYPE_BYTE, size);
8080
data = new byte[size];
8181
bankdata = new byte[1][];
8282
bankdata[0] = data;
@@ -90,7 +90,7 @@ public DataBufferByte(int size) {
9090
* @param numBanks The number of banks in the a<CODE>DataBuffer</CODE>.
9191
*/
9292
public DataBufferByte(int size, int numBanks) {
93-
super(STABLE, TYPE_BYTE, size, numBanks);
93+
super(/*?*/UNTRACKABLE, TYPE_BYTE, size, numBanks);
9494
bankdata = new byte[numBanks][];
9595
for (int i= 0; i < numBanks; i++) {
9696
bankdata[i] = new byte[size];

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public final class DataBufferDouble extends DataBuffer {
6565
* @param size The number of elements in the <code>DataBuffer</code>.
6666
*/
6767
public DataBufferDouble(int size) {
68-
super(STABLE, TYPE_DOUBLE, size);
68+
super(/*?*/UNTRACKABLE, TYPE_DOUBLE, size);
6969
data = new double[size];
7070
bankdata = new double[1][];
7171
bankdata[0] = data;
@@ -81,7 +81,7 @@ public DataBufferDouble(int size) {
8181
* @param numBanks The number of banks in the <code>DataBuffer</code>.
8282
*/
8383
public DataBufferDouble(int size, int numBanks) {
84-
super(STABLE, TYPE_DOUBLE, size, numBanks);
84+
super(/*?*/UNTRACKABLE, TYPE_DOUBLE, size, numBanks);
8585
bankdata = new double[numBanks][];
8686
for (int i= 0; i < numBanks; i++) {
8787
bankdata[i] = new double[size];

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public final class DataBufferFloat extends DataBuffer {
6565
* @param size The number of elements in the DataBuffer.
6666
*/
6767
public DataBufferFloat(int size) {
68-
super(STABLE, TYPE_FLOAT, size);
68+
super(/*?*/UNTRACKABLE, TYPE_FLOAT, size);
6969
data = new float[size];
7070
bankdata = new float[1][];
7171
bankdata[0] = data;
@@ -82,7 +82,7 @@ public DataBufferFloat(int size) {
8282
* <code>DataBuffer</code>.
8383
*/
8484
public DataBufferFloat(int size, int numBanks) {
85-
super(STABLE, TYPE_FLOAT, size, numBanks);
85+
super(/*?*/UNTRACKABLE, TYPE_FLOAT, size, numBanks);
8686
bankdata = new float[numBanks][];
8787
for (int i= 0; i < numBanks; i++) {
8888
bankdata[i] = new float[size];

0 commit comments

Comments
 (0)