Skip to content

Commit b986159

Browse files
committed
Adds BufferedImage.TYPE_USHORT_GRAY
1 parent 4e3d61b commit b986159

File tree

15 files changed

+308
-144
lines changed

15 files changed

+308
-144
lines changed
164 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200228164908
1+
20200229171043
164 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200228164908
1+
20200229171043
164 KB
Binary file not shown.

sources/net.sf.j2s.java.core/src/com/sun/imageio/plugins/common/ImageUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ public static final void canEncodeImage(ImageWriter writer,
11271127
public static final boolean imageIsContiguous(RenderedImage image) {
11281128
SampleModel sm;
11291129
if(image instanceof BufferedImage) {
1130-
WritableRaster ras = ((BufferedImage)image).getRaster();
1130+
WritableRaster ras = ((BufferedImage)image).秘getRaster();
11311131
sm = ras.getSampleModel();
11321132
} else {
11331133
sm = image.getSampleModel();

sources/net.sf.j2s.java.core/src/java/awt/color/ColorSpace.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public class ColorSpace {
106106
private static ColorSpace sRGBspace;
107107
// private static ColorSpace XYZspace;
108108
// private static ColorSpace PYCCspace;
109-
// private static ColorSpace GRAYspace;
109+
public static ColorSpace GRAYspace; // SwingJS was private
110110
// private static ColorSpace LINEAR_RGBspace;
111111

112112
/**
@@ -330,19 +330,29 @@ public static ColorSpace getInstance (int colorspace)
330330
// break;
331331
//
332332
//
333-
// case CS_GRAY:
333+
case CS_GRAY:
334+
synchronized(ColorSpace.class) {
335+
if (GRAYspace == null) {
336+
GRAYspace = new ColorSpace(TYPE_GRAY, 1); // SwingJS
337+
// ICC_Profile theProfile = ICC_Profile.getInstance (CS_sRGB);
338+
// sRGBspace = new ICC_ColorSpace (theProfile);
339+
}
340+
341+
theColorSpace = GRAYspace;
342+
}
343+
break;
334344
// synchronized(ColorSpace.class) {
335345
// if (GRAYspace == null) {
336346
// ICC_Profile theProfile = ICC_Profile.getInstance (CS_GRAY);
337347
// GRAYspace = new ICC_ColorSpace (theProfile);
338-
// /* to allow access from java.awt.ColorModel */
348+
// /* to allow access from java.awt.ColorModel */
339349
// CMSManager.GRAYspace = GRAYspace;
340350
// }
341351
//
342352
// theColorSpace = GRAYspace;
343353
// }
344354
// break;
345-
//
355+
346356
//
347357
// case CS_LINEAR_RGB:
348358
// synchronized(ColorSpace.class) {

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

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import sun.awt.image.BytePackedRaster;
4545
import sun.awt.image.IntegerComponentRaster;
4646
import sun.awt.image.OffScreenImageSource;
47+
import sun.awt.image.PixelConverter;
4748
import sun.awt.image.ShortComponentRaster;
4849
import sun.awt.image.SunWritableRaster;
4950
import 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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,8 @@ static int getDefaultTransferType(int pixel_bits) {
16751675
static Map g16Tos8Map = null; // 16-bit gray values to 8-bit sRGB values
16761676
static Map lg16Toog16Map = null; // 16-bit linear to 16-bit "other" gray
16771677

1678+
public static ColorSpace GRAYspace; // SwingJS should be in CSMananger
1679+
16781680
static boolean isLinearRGBspace(ColorSpace cs) {
16791681
return false;
16801682
// // Note: CMM.LINEAR_RGBspace will be null if the linear
@@ -1685,7 +1687,7 @@ static boolean isLinearRGBspace(ColorSpace cs) {
16851687
static boolean isLinearGRAYspace(ColorSpace cs) {
16861688
// Note: CMM.GRAYspace will be null if the linear
16871689
// gray space has not been created yet.
1688-
return (cs == CMSManager.GRAYspace);
1690+
return false;//(cs == CMSManager.GRAYspace);
16891691
}
16901692

16911693

0 commit comments

Comments
 (0)