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;
12 public DOMNode _imgNode; // used by JSGraphics2D directly
13 private int width, height;
15 public JSImage(int[] argb, int width, int height) {
16 super(width, height, TYPE_INT_ARGB);
23 * convert [r g b a r g b a ...] into [argb argb argb ...]
25 * currently does not respect transparency
27 * @param imgData HTML5 canvas.context.imageData.data
28 * @return array of ARGB values
31 int[] toIntARGB(int[] imgData) {
33 * red=imgData.data[0];
34 * green=imgData.data[1];
35 * blue=imgData.data[2];
36 * alpha=imgData.data[3];
38 int n = imgData.length / 4;
39 int[] iData = new int[n];
40 for (int i = 0, j = 0; i < n; j++)
41 iData[i++] = (imgData[j++] << 16) | (imgData[j++] << 8) | imgData[j++] | 0xFF000000;
46 * Use HTML5 to load PNG, JPG, or GIF image in order to extract its pixels
51 @SuppressWarnings("unused")
52 public void getDOMImage(byte[] b, String type) {
53 String dataurl = "data:image/" + type + ";base64," + Base64.getBase64(b).toString();
58 * img = new Image(this.width, this.height);
59 * //img.onLoad = function() { me.setDOMImage(img); };
67 * callback from Image.src = ... ; extract the int[] data from this image;
68 * also sets img._pbuf32 for graphing
71 @SuppressWarnings("unused")
72 public void setDOMImage(DOMNode img) {
73 DOMNode canvas = DOMNode.createElement("canvas", "JSImage");
82 * var ctx = canvas.getContext("2d");
83 * ctx.drawImage(img, 0, 0, w, h);
84 * var data = ctx.getImageData(0, 0, w, h).data;
85 * img._pbuf32 = this.toIntARGB(data);
94 public int getHeight(ImageObserver o) {
99 public int getWidth(ImageObserver o) {