4444import sun .awt .image .BytePackedRaster ;
4545import sun .awt .image .IntegerComponentRaster ;
4646import sun .awt .image .OffScreenImageSource ;
47+ import sun .awt .image .PixelConverter ;
4748import sun .awt .image .ShortComponentRaster ;
4849import sun .awt .image .SunWritableRaster ;
4950import swingjs .JSGraphics2D ;
@@ -181,7 +182,8 @@ public class BufferedImage extends Image implements RenderedImage, Transparency
181182 * so we have no idea if it has been changed or not.
182183 *
183184 */
184- private boolean 秘userRaster ;
185+ private boolean 秘hasRasterData ;
186+ private int 秘wxh ;
185187
186188 /**
187189 * Image Type Constants
@@ -389,6 +391,7 @@ public class BufferedImage extends Image implements RenderedImage, Transparency
389391 public BufferedImage (int width , int height , int imageType ) {
390392 this .width = width ;
391393 this .height = height ;
394+ 秘wxh = width * height ;
392395 switch (imageType ) {
393396 case TYPE_INT_RGB :
394397 colorModel = new DirectColorModel (24 , 0x00ff0000 , // Red
@@ -450,7 +453,7 @@ public BufferedImage(int width, int height, int imageType) {
450453 DataBuffer .TYPE_BYTE );
451454 raster = Raster .createInterleavedRaster (DataBuffer .TYPE_BYTE , width , height , width * 4 , 4 , bOffs , null );
452455 秘pix = ((DataBufferInt ) raster .getDataBuffer ()).data ;
453- this . 秘havePixels = true ;
456+ 秘havePixels = 秘hasRasterData = true ;
454457 }
455458 break ;
456459
@@ -553,6 +556,7 @@ public BufferedImage(int width, int height, int imageType, IndexColorModel cm) {
553556 }
554557 this .width = width ;
555558 this .height = height ;
559+ 秘wxh = width * height ;
556560 switch (imageType ) {
557561 case TYPE_BYTE_BINARY :
558562 int bits ; // Will be set below
@@ -637,11 +641,12 @@ public BufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremu
637641 this .raster = raster ;
638642 this .width = raster .getWidth ();
639643 this .height = raster .getHeight ();
644+ 秘wxh = width * height ;
640645 raster .setImage (this );
641646 if (getColorModel () == ColorModel .秘RGBdefault )
642647 秘pix = ((DataBufferInt ) raster .getDataBuffer ()).data ;
643648 else
644- 秘userRaster = true ;
649+ 秘hasRasterData = true ;
645650 this .properties = properties ;
646651 int numBands = raster .getNumBands ();
647652 boolean isAlphaPre = cm .isAlphaPremultiplied ();
@@ -802,6 +807,7 @@ public ColorModel getColorModel() {
802807 * @return the <code>WriteableRaster</code> of this <code>BufferedImage</code> .
803808 */
804809 public WritableRaster getRaster () {
810+ 秘hasRasterData = true ;
805811 // NO!!! checkHavePixels();
806812 return raster ;
807813 }
@@ -907,6 +913,10 @@ public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int of
907913 秘setPixelsFromHTML5Canavas (andSetImageNode );
908914 return true ;
909915 }
916+ if (秘hasRasterData ) {
917+ 秘getPixelsFromRaster ();
918+ return true ;
919+ }
910920 return false ;
911921 }
912922
@@ -926,9 +936,21 @@ public int[] getRangeRGB(int startX, int startY, int w, int h, int[] rgbArray, i
926936 if (秘pix == null && 秘pixSaved == null )
927937 秘ensureHavePixels (false );
928938 int [] pixels = (秘pix == null ? 秘pixSaved : 秘pix );
939+ if (pixels == null )
940+ return rgbArray ;
941+ if (pixels .length == 秘wxh ) {
929942 for (int y = startY , yoff = offset ; y < startY + h ; y ++, yoff += scansize )
930943 for (int off = yoff , x = startX ; x < startX + w ; x ++)
931- rgbArray [off ++] = pixels [y * this .width + x ];
944+ rgbArray [off ++] = pixels [y * width + x ];
945+ } else {
946+ // already in HTML5 format
947+ for (int y = startY , yoff = offset ; y < startY + h ; y ++, yoff += scansize ) {
948+ for (int off = yoff , x = startX , pt = (y * width )<<2 ; x < startX + w ; x ++) {
949+ // r g b a to argb
950+ rgbArray [off ++] = (pixels [pt ++] << 16 ) | (pixels [pt ++] << 8 ) | pixels [pt ++] | (pixels [pt ++] << 24 );
951+ }
952+ }
953+ }
932954 return rgbArray ;
933955 }
934956
@@ -1879,9 +1901,9 @@ public Graphics2D getImageGraphic() {
18791901 * @return
18801902 */
18811903 private int [] 秘getPixelsFromRaster () {
1882- int n = width * height ;
18831904 if (imageType == TYPE_4BYTE_HTML5 )
18841905 return 秘pix ;
1906+ int n = 秘wxh ;
18851907 if (秘pix == null || 秘pix .length != n * 4 )
18861908 秘pix = new int [n * 4 ];
18871909 ColorModel cm = getColorModel ();
@@ -1894,8 +1916,31 @@ public Graphics2D getImageGraphic() {
18941916 }
18951917 } else {
18961918 int nc = cm .getNumComponents ();
1897- getRaster () .getPixels (0 , 0 , width , height , 秘pix );
1919+ raster .getPixels (0 , 0 , width , height , p );
18981920 switch (nc ) {
1921+ case 1 :
1922+ PixelConverter pc = PixelConverter .UshortGray .instance ;
1923+ // gray -- first get those into the first 1/4
1924+ for (int i = n , pt = n * 4 ; --i >= 0 ;) {
1925+ int val = p [i ];
1926+ p [--pt ] = 0xFF ;
1927+ switch (val ) {
1928+ case 0xFFFF :
1929+ case 0 :
1930+ p [--pt ] = val ;
1931+ p [--pt ] = val ;
1932+ p [--pt ] = val ;
1933+ break ;
1934+ default :
1935+ int rgb = (val == 0 ? 0xFF000000 : val == 0xFFFF ? 0xFFFFFFFF : pc .pixelToRgb (val , null ));
1936+ p [--pt ] = rgb & 0xFF ; // b
1937+ p [--pt ] = (rgb >> 8 ) & 0xFF ; // g
1938+ p [--pt ] = (rgb >> 16 ) & 0xFF ; // r
1939+ break ;
1940+ }
1941+
1942+ }
1943+ break ;
18991944 case 3 :
19001945 for (int i = n * 4 , j = n * 3 ; --i >= 0 ;) {
19011946 if (i % 4 == 3 )
@@ -1904,7 +1949,7 @@ public Graphics2D getImageGraphic() {
19041949 }
19051950 break ;
19061951 case 4 :
1907- getRaster ().getPixels (0 , 0 , width , height , 秘pix );
1952+ getRaster ().getPixels (0 , 0 , width , height , p );
19081953 break ;
19091954 }
19101955 }
@@ -1920,13 +1965,13 @@ public Graphics2D getImageGraphic() {
19201965 * @return
19211966 */
19221967
1923- public DOMNode 秘getImageNode () {
1924- if (imageType == TYPE_4BYTE_HTML5 )
1968+ public DOMNode 秘getImageNode (boolean force ) {
1969+ if (! force && 秘hasRasterData || imageType == TYPE_4BYTE_HTML5 )
19251970 return null ;
19261971 Object node = (秘canvas != null ? 秘canvas : 秘imgNode );
1927- if (node == null )
1928- node = JSGraphicsCompositor .createImageNode (this );
1929- else if (秘userRaster ) {
1972+ if (node == null && ( force || ! 秘hasRasterData ) )
1973+ return JSGraphicsCompositor .createImageNode (this );
1974+ if (秘hasRasterData ) {
19301975 秘getPixelsFromRaster ();
19311976 秘g = null ;
19321977 createGraphics ();
@@ -1946,6 +1991,15 @@ else if (秘userRaster) {
19461991 秘havePixels = true ;
19471992 }
19481993
1994+ /**
1995+ * This private method does not trigger actual conversion to a rastered image.
1996+ *
1997+ * @return
1998+ */
1999+ public WritableRaster 秘getRaster () {
2000+ return raster ;
2001+ }
2002+
19492003
19502004
19512005
0 commit comments