Skip to content

Commit 3a27bef

Browse files
committed
Adds support for IndexColorModel for HTML5 canvas
1 parent 861d517 commit 3a27bef

File tree

2 files changed

+64
-36
lines changed

2 files changed

+64
-36
lines changed

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

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public class BufferedImage extends Image implements RenderedImage, Transparency
248248
* Green, and Red stored in 3 bytes and 1 byte of alpha. The image has a
249249
* <code>ComponentColorModel</code> with alpha. The color data in this image is
250250
* considered not to be premultiplied with alpha. The byte data is interleaved
251-
* in a single byte array in the order B, G, R, A from lower to higher byte
251+
* in a single byte array in the order R, G, B, A from lower to higher byte
252252
* addresses within each pixel.
253253
*/
254254
public static final int TYPE_4BYTE_HTML5 = JSUtilI.TYPE_4BYTE_HTML5;
@@ -1902,15 +1902,15 @@ public void flush() {
19021902
}
19031903

19041904
/**
1905-
* Creates an HTML5 Canvas-compatible int[] {b g r a...} array. We use int[]
1905+
* Creates an HTML5 Canvas-compatible int[] {r g b a...} array. We use int[]
19061906
* here just because that is what ColorModel.getComponents uses
19071907
*
19081908
* @return
19091909
*/
19101910
private int[] 秘getPixelsFromRaster() {
19111911
// Coerse byte[] to int[] for SwingJS
1912-
if (imageType == TYPE_4BYTE_HTML5)
1913-
return 秘pix = (int[])(Object) ((DataBufferByte) raster.getDataBuffer()).getData();
1912+
if (imageType == TYPE_4BYTE_HTML5)
1913+
return 秘pix = (int[]) (Object) ((DataBufferByte) raster.getDataBuffer()).getData();
19141914
int n = 秘wxh;
19151915
if (秘pix == null || 秘pix.length != n * 4)
19161916
秘pix = new int[n * 4];
@@ -1929,40 +1929,52 @@ public void flush() {
19291929
} else {
19301930
int nc = cm.getNumComponents();
19311931
raster.getPixels(0, 0, width, height, p);
1932-
switch (nc) {
1933-
case 1:
1934-
PixelConverter pc = PixelConverter.UshortGray.instance;
1935-
// gray -- first get those into the first 1/4
1936-
for (int i = n, pt = n * 4; --i >= 0;) {
1937-
int val = p[i];
1938-
p[--pt] = 0xFF;
1939-
switch (val) {
1940-
case 0xFFFF:
1941-
case 0:
1942-
p[--pt] = val;
1943-
p[--pt] = val;
1944-
p[--pt] = val;
1945-
break;
1946-
default:
1947-
int rgb = (val == 0 ? 0xFF000000 : val == 0xFFFF ? 0xFFFFFFFF : pc.pixelToRgb(val, null));
1948-
p[--pt] = rgb & 0xFF; // b
1949-
p[--pt] = (rgb >> 8) & 0xFF; // g
1950-
p[--pt] = (rgb >> 16) & 0xFF; // r
1951-
break;
1952-
}
1953-
1932+
if (cm instanceof IndexColorModel) {
1933+
IndexColorModel icm = (IndexColorModel) cm;
1934+
byte[] colors = icm.秘getColorMap();
1935+
for (int i = n, pt = n * 4; i > 0;) {
1936+
int index = (p[--i] << 2) + 4;
1937+
p[--pt] = colors[--index];
1938+
p[--pt] = colors[--index];
1939+
p[--pt] = colors[--index];
1940+
p[--pt] = colors[--index];
19541941
}
1955-
break;
1956-
case 3:
1957-
for (int i = n * 4, j = n * 3; --i >= 0;) {
1958-
if (i % 4 == 3)
1959-
p[i--] = 0xFF;
1960-
p[i] = p[--j];
1942+
} else {
1943+
switch (nc) {
1944+
case 1:
1945+
PixelConverter pc = PixelConverter.UshortGray.instance;
1946+
// gray -- first get those into the first 1/4
1947+
for (int i = n, pt = n * 4; --i >= 0;) {
1948+
int val = p[i];
1949+
p[--pt] = 0xFF;
1950+
switch (val) {
1951+
case 0xFFFF:
1952+
case 0:
1953+
p[--pt] = val;
1954+
p[--pt] = val;
1955+
p[--pt] = val;
1956+
break;
1957+
default:
1958+
int rgb = (val == 0 ? 0xFF000000 : val == 0xFFFF ? 0xFFFFFFFF : pc.pixelToRgb(val, null));
1959+
p[--pt] = rgb & 0xFF; // b
1960+
p[--pt] = (rgb >> 8) & 0xFF; // g
1961+
p[--pt] = (rgb >> 16) & 0xFF; // r
1962+
break;
1963+
}
1964+
1965+
}
1966+
break;
1967+
case 3:
1968+
for (int i = n * 4, j = n * 3; --i >= 0;) {
1969+
if (i % 4 == 3)
1970+
p[i--] = 0xFF;
1971+
p[i] = p[--j];
1972+
}
1973+
break;
1974+
case 4:
1975+
getRaster().getPixels(0, 0, width, height, p);
1976+
break;
19611977
}
1962-
break;
1963-
case 4:
1964-
getRaster().getPixels(0, 0, width, height, p);
1965-
break;
19661978
}
19671979
}
19681980
return p;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,4 +1538,20 @@ public String toString() {
15381538
+ " isAlphaPre = "+isAlphaPremultiplied
15391539
);
15401540
}
1541+
1542+
private byte[] 秘colormap;
1543+
public byte[] 秘getColorMap() {
1544+
if (秘colormap != null)
1545+
return 秘colormap;
1546+
int n = getMapSize();
1547+
byte[] rgba = new byte[n * 4];
1548+
for (int i = 0, pt = 0; i < n; i++) {
1549+
// REMIND: Needs to change if different color space
1550+
rgba[pt++] = (byte) getRed(i);
1551+
rgba[pt++] = (byte) getGreen(i);
1552+
rgba[pt++] = (byte) getBlue(i);
1553+
rgba[pt++] = (byte) getAlpha(i);
1554+
}
1555+
return rgba;
1556+
}
15411557
}

0 commit comments

Comments
 (0)