3 import javajs.util.Base64;
4 import swingjs.api.DOMNode;
5 import java.awt.image.BufferedImage;
6 import java.awt.image.ImageObserver;
8 public class JSImage extends BufferedImage {
9 // a BufferedImage in name only, actually;
11 int[] pix; // not used in Java, but may be used in JavaScript
12 public DOMNode _imgNode; // used by JSGraphics2D directly
13 private int width, height;
16 super(1,1, TYPE_INT_ARGB);
20 public JSImage setData(int width, int height, int[] argb) {
24 getColorModel().createCompatibleWritableRaster(width, height);
29 * convert [r g b a r g b a ...] into [argb argb argb ...]
31 * currently does not respect transparency
33 * @param imgData HTML5 canvas.context.imageData.data
34 * @return array of ARGB values
37 int[] toIntARGB(int[] imgData) {
39 * red=imgData.data[0];
40 * green=imgData.data[1];
41 * blue=imgData.data[2];
42 * alpha=imgData.data[3];
44 int n = imgData.length / 4;
45 int[] iData = new int[n];
46 for (int i = 0, j = 0; i < n; j++)
47 iData[i++] = (imgData[j++] << 16) | (imgData[j++] << 8) | imgData[j++] | 0xFF000000;
52 * Use HTML5 to load PNG, JPG, or GIF image in order to extract its pixels
57 @SuppressWarnings("unused")
58 public void getDOMImage(byte[] b, String type) {
59 String dataurl = "data:image/" + type + ";base64," + Base64.getBase64(b).toString();
64 * img = new Image(this.width, this.height);
65 * //img.onLoad = function() { me.setDOMImage(img); };
73 * callback from Image.src = ... ; extract the int[] data from this image;
74 * also sets img._pbuf32 for graphing
77 @SuppressWarnings("unused")
78 public void setDOMImage(DOMNode img) {
79 DOMNode canvas = DOMNode.createElement("canvas", "JSImage");
88 * var ctx = canvas.getContext("2d");
89 * ctx.drawImage(img, 0, 0, w, h);
90 * var data = ctx.getImageData(0, 0, w, h).data;
91 * img._pbuf32 = this.toIntARGB(data);
100 public int getHeight(ImageObserver o) {
105 public int getWidth(ImageObserver o) {