Skip to content

Commit 47cf74c

Browse files
committed
better check for having pixels in an image
The issue will be if the developer is using raw raster calls to change an image. One must call image.checkHavePixels() and possibly even set image._havePix false first to force the reading of the HTML5 canvas pixels into the image raster data.
1 parent ce1cb82 commit 47cf74c

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ public BufferedImage(int width, int height, int imageType) {
322322
0x0 // Alpha
323323
);
324324
raster = colorModel.createCompatibleWritableRaster(width, height);
325+
raster.setImage(this);
325326
_pix = ((DataBufferInt) raster.getDataBuffer()).data;
326327
}
327328
break;
@@ -330,6 +331,7 @@ public BufferedImage(int width, int height, int imageType) {
330331
case TYPE_INT_ARGB: {
331332
colorModel = ColorModel.getRGBdefault();
332333
raster = colorModel.createCompatibleWritableRaster(width, height);
334+
raster.setImage(this);
333335
_pix = ((DataBufferInt) raster.getDataBuffer()).data;
334336
}
335337
break;
@@ -631,6 +633,7 @@ public BufferedImage(ColorModel cm, WritableRaster raster,
631633

632634
colorModel = cm;
633635
this.raster = raster;
636+
raster.setImage(this);
634637
_pix = ((DataBufferInt) raster.getDataBuffer()).data;
635638
this.properties = properties;
636639
// int numBands = raster.getNumBands();
@@ -837,7 +840,6 @@ public ColorModel getColorModel() {
837840
* .
838841
*/
839842
public WritableRaster getRaster() {
840-
checkHavePixels();
841843
return raster;
842844
}
843845

@@ -946,7 +948,7 @@ public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int of
946948
*
947949
* @return true if pixels had to be set
948950
*/
949-
private boolean checkHavePixels() {
951+
public boolean checkHavePixels() {
950952
if ((_imgNode != null || _g != null) && !_havePix) {
951953
setPixels();
952954
return true;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@
125125

126126
public class Raster {
127127

128+
private BufferedImage image;
129+
130+
public void setImage(BufferedImage image) {
131+
this.image = image;
132+
}
133+
128134
/**
129135
* The SampleModel that describes how pixels from this Raster
130136
* are stored in the DataBuffer.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* in the Raster class.
5151
*/
5252
public class WritableRaster extends Raster {
53-
53+
5454
public void setParams(SampleModel model,
5555
DataBuffer dataBuffer, Point origin) {
5656
// for subclasses IntegerInterleaved and ByteInterleaved

sources/net.sf.j2s.java.core/src/sun/awt/image/OffScreenImageSource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public void requestTopDownLeftRightResend(ImageConsumer ic) {
9797

9898
private void sendPixels() {
9999
ColorModel cm = image.getColorModel();
100+
image.checkHavePixels();
100101
WritableRaster raster = image.getRaster();
101102
int numDataElements = raster.getNumDataElements();
102103
int dataType = raster.getDataBuffer().getDataType();

0 commit comments

Comments
 (0)