Skip to content

Commit 392433b

Browse files
committed
BufferedImage preliminary fix for raster-based images
1 parent dbac2ec commit 392433b

File tree

5 files changed

+58
-37
lines changed

5 files changed

+58
-37
lines changed

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

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ public class BufferedImage extends Image implements RenderedImage, Transparency
126126
protected int width, height;
127127

128128
/**
129-
* the JSGrpahics2D object associated with this image
129+
* the JSGrpahics2D object associated with this image, if it has been requested
130130
*/
131131
public JSGraphics2D 秘g; // a JSGraphics2D instance
132132

133133
/**
134-
* if an image is used just for graphics that the HTML5 canvas can use, we back
135-
* the BufferedImage with an HTML5 canvas and just draw to it, never using the
134+
* if an image is used just for graphics that the HTML5 canvas can draw, we back
135+
* the BufferedImage with an HTML5 canvas and draw to it, never using the
136136
* raster associated with this image.
137137
*/
138138
public Object 秘imgNode; // used by JSGraphics2D directly
@@ -150,7 +150,7 @@ public class BufferedImage extends Image implements RenderedImage, Transparency
150150
* set to true if pixels have been generated from an HTML5 canvas
151151
*
152152
*/
153-
private boolean 秘havePixels;
153+
private boolean 秘haveFilePixels;
154154

155155
/**
156156
* the HTML5 canvas that originated 秘pix or that was created from them.
@@ -453,7 +453,7 @@ public BufferedImage(int width, int height, int imageType) {
453453
DataBuffer.TYPE_BYTE);
454454
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 4, 4, bOffs, null);
455455
秘pix = ((DataBufferInt) raster.getDataBuffer()).data;
456-
秘havePixels = 秘hasRasterData = true;
456+
秘haveFilePixels = 秘hasRasterData = true;
457457
}
458458
break;
459459

@@ -909,7 +909,7 @@ public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int of
909909
* @return true if pixels had to be set
910910
*/
911911
public boolean 秘ensureHavePixels(boolean andSetImageNode) {
912-
if (!秘havePixels && (秘imgNode != null || 秘g != null)) {
912+
if (!秘haveFilePixels && (秘imgNode != null || 秘g != null)) {
913913
秘setPixelsFromHTML5Canavas(andSetImageNode);
914914
return true;
915915
}
@@ -1019,7 +1019,7 @@ public void setRGB(int startX, int startY, int w, int h, int[] rgbArray, int off
10191019
// 秘pix is used by getGraphics()
10201020
// 秘pixSaved is kept in case we need to do this again
10211021
秘g = null; // forces new this.秘canvas to be created in getGraphics()
1022-
getImageGraphic(); // sets 秘pix = null and creates 秘canvas
1022+
秘getImageGraphic(); // sets 秘pix = null and creates 秘canvas
10231023

10241024
}
10251025

@@ -1152,7 +1152,7 @@ public Graphics getGraphics() {
11521152
* @return a <code>Graphics2D</code>, used for drawing into this image.
11531153
*/
11541154
public Graphics2D createGraphics() {
1155-
return (Graphics2D) getImageGraphic().create();
1155+
return (Graphics2D) 秘getImageGraphic().create();
11561156
// GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
11571157
// return (Graphics2D) env.createGraphics(this);
11581158
}
@@ -1715,7 +1715,7 @@ public void setImageFromHTML5Canvas(JSGraphics2D g) {
17151715
if (秘pix == null)
17161716
toIntARGB(data, 秘pix = new int[data.length >> 2]);
17171717
秘imgNode = (andSetImgNode ? canvas : null);
1718-
秘havePixels = true;
1718+
秘haveFilePixels = true;
17191719
}
17201720

17211721
/**
@@ -1794,7 +1794,7 @@ public void flush() {
17941794
// call this method after drawing to ensure that
17951795
// pixels are recreated from the HTML5 canvas
17961796
秘pix = null;
1797-
秘havePixels = false;
1797+
秘haveFilePixels = false;
17981798
}
17991799

18001800
/**
@@ -1807,7 +1807,7 @@ public void flush() {
18071807
* @return a JSGraphics2D object
18081808
*/
18091809
@SuppressWarnings("unused")
1810-
public Graphics2D getImageGraphic() {
1810+
public Graphics2D 秘getImageGraphic() {
18111811
if (秘g == null) {
18121812
HTML5Canvas canvas = (HTML5Canvas) DOMNode.createElement("canvas", "img" + System.currentTimeMillis());
18131813
int w = getWidth();
@@ -1914,10 +1914,13 @@ public Graphics2D getImageGraphic() {
19141914
boolean isPacked = cm instanceof PackedColorModel;
19151915
int[] p = 秘pix;
19161916
if (isPacked) {
1917+
int alpha = (cm.getNumColorComponents() == 3 ? 0xFF : -1);
19171918
int[] a = new int[n];
19181919
raster.getDataElements(0, 0, width, height, a);
19191920
for (int i = 0, pt = 0; i < n; i++, pt += 4) {
19201921
cm.getComponents(a[i], p, pt);
1922+
if (alpha != 0)
1923+
p[pt + 3] = alpha;
19211924
}
19221925
} else {
19231926
int nc = cm.getNumComponents();
@@ -1966,20 +1969,26 @@ public Graphics2D getImageGraphic() {
19661969
* canvas associated with this image, particularly if this is from a
19671970
* user-provided raster.
19681971
*
1969-
* @param imgNode
1972+
* @param force initially false just to get the pre-constructed canvas if it exists
19701973
* @return
19711974
*/
19721975

19731976
public DOMNode 秘getImageNode(boolean force) {
1974-
if (!秘hasRasterData)
1975-
return (DOMNode) (秘canvas != null ? 秘canvas
1977+
// If we have raster data and are not forcing, return the canvas if it exists.
1978+
// If we we have no raster data and are not forcing, force the issue anyway
1979+
// and return the canvas or image node.
1980+
// If we are forcing, or force the creation of a canvas
1981+
if (!force)
1982+
return (DOMNode) (秘hasRasterData || 秘canvas != null ? 秘canvas
19761983
: 秘imgNode != null ? 秘imgNode : JSGraphicsCompositor.createImageNode(this));
1977-
if (!force)// || imageType == TYPE_4BYTE_HTML5)
1978-
return null;
1984+
// we are forcing
19791985
秘getPixelsFromRaster();
19801986
秘g = null;
1981-
getImageGraphic();
1982-
return 秘g.getCanvas();
1987+
秘getImageGraphic();
1988+
DOMNode canvas = 秘g.getCanvas();
1989+
秘g = null;
1990+
秘canvas = null;
1991+
return canvas;
19831992
}
19841993

19851994
/**
@@ -1989,11 +1998,11 @@ public Graphics2D getImageGraphic() {
19891998
*/
19901999
protected void 秘setPixels(int[] argb) {
19912000
秘pix = argb;
1992-
秘havePixels = true;
2001+
秘haveFilePixels = true;
19932002
}
19942003

19952004
/**
1996-
* This private method does not trigger actual conversion to a rastered image.
2005+
* This method, called from ImageUtil does not trigger actual conversion to a rastered image.
19972006
*
19982007
* @return
19992008
*/

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,21 @@ public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1,
607607
return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
608608
}
609609

610+
/**
611+
* Draw an image, possibly only part of it, and possibly scaled.
612+
*
613+
* @param img
614+
* @param dx1 destination x1
615+
* @param dy1 destination y1
616+
* @param dx2 destination x2
617+
* @param dy2 destination y2
618+
* @param sx1 source x1
619+
* @param sy1 source y1
620+
* @param sx2 source x2
621+
* @param sy2 source y2
622+
* @param observer
623+
* @return true
624+
*/
610625
@SuppressWarnings("unused")
611626
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
612627
ImageObserver observer) {
@@ -658,23 +673,24 @@ private boolean drawImageXT(Image img, AffineTransform xform, ImageObserver obs)
658673

659674
private boolean thinLine;
660675

676+
public boolean drawImagePriv(Image img, int x, int y, ImageObserver observer) {
677+
if (img == null)
678+
return true;
679+
return drawImagePriv(img, x, y, img.getWidth(observer), img.getHeight(observer), observer);
680+
}
681+
661682
/**
662-
* This method first checks to see if we have pixels. For example, from a
663-
* user-derived raster.
683+
* This method first checks to see if we have pixels, for example from a
684+
* user-derived raster. If we do, and the current transform is a translation only, we can use
685+
* them directly. If we don't or this a scaled or skewed transform, we must draw the canvas
686+
* object corresponding to this image instead.
664687
*
665688
* @param img
666689
* @param x
667690
* @param y
668691
* @param observer
669692
* @return
670693
*/
671-
@SuppressWarnings("unused")
672-
public boolean drawImagePriv(Image img, int x, int y, ImageObserver observer) {
673-
if (img == null)
674-
return true;
675-
return drawImagePriv(img, x, y, img.getWidth(observer), img.getHeight(observer), observer);
676-
}
677-
678694
private boolean drawImagePriv(Image img, int x, int y, int width, int height, ImageObserver observer) {
679695
double[] m = HTML5CanvasContext2D.setMatrix(ctx, transform);
680696
boolean isToSelf = (this == ((BufferedImage) img).秘g);
@@ -713,10 +729,6 @@ private boolean drawImagePriv(Image img, int x, int y, int width, int height, Im
713729
buf8[i] = pixels[i++] & 0xFF;
714730
buf8[i] = 0xFF;
715731
}
716-
// } else if (isOpaque) {
717-
// for (int i = 0, n = Math.min(buf8.length, pixels.length); i < n; i++) {
718-
// buf8[i] = pixels[i] & 0xFF;
719-
// }
720732
} else if (isToSelf) {
721733
for (int i = 0, n = Math.min(buf8.length, pixels.length); i < n; i++) {
722734
buf8[i] = pixels[i] & 0xFF;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public static BufferedImage filterImage(BufferedImage src, BufferedImage dst,
248248
retBI = op.filter(src, dst);
249249
break;
250250
case 'A':
251-
Graphics2D g = dst.getImageGraphic();
251+
Graphics2D g = dst.秘getImageGraphic();
252252
g.drawImage(src,
253253
((AffineTransformOp) op).getTransform(), null);
254254
retBI = dst;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public JSGraphicsEnvironment(){
2727

2828
@Override
2929
public Graphics2D createGraphics(BufferedImage img) {
30-
return (Graphics2D) img.getImageGraphic().create();
30+
return (Graphics2D) img.秘getImageGraphic().create();
3131
}
3232

3333
private static Font[] availableFonts;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* initialized to the raster data, but 秘imgNode will be null.
1818
*
1919
* When an image is used, then 秘pix will be there, but it will not be populated
20-
* unless a call to setRGB is made. Until then, JSGraphics2D.drawImage will
21-
* simply use the image itself.
20+
* unless a call to getRaster() or one of the setRGB() methods is made.
21+
* Until then, JSGraphics2D.drawImage will simply use the image itself.
2222
*
2323
* useful states: (needs checking)
2424
*

0 commit comments

Comments
 (0)