15016880049feb4dce88a8c02e00564bf3333d6b
[jalviewjs.git] / src / swingjs / JSImage.java
1 package swingjs;
2
3 import javajs.util.Base64;
4 import swingjs.api.DOMNode;
5 import java.awt.image.BufferedImage;
6 import java.awt.image.ImageObserver;
7
8 public class JSImage extends BufferedImage {
9         // a BufferedImage in name only, actually;
10         int typeRequested;
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;
14         
15         public JSImage() {
16                 super(1,1, TYPE_INT_ARGB);
17                 
18         }
19
20         public JSImage setData(int width, int height, int[] argb) {
21                 pix = argb;
22                 this.width = width;
23                 this.height = height;
24                 getColorModel().createCompatibleWritableRaster(width, height);
25                 return this;
26         }
27         
28         /**
29          * convert [r g b a  r g b a ...] into [argb argb argb ...]
30          * 
31          * currently does not respect transparency
32          * 
33          * @param imgData HTML5 canvas.context.imageData.data
34          * @return array of ARGB values
35          * 
36          */
37   int[] toIntARGB(int[] imgData) {
38     /*
39      * red=imgData.data[0];
40      * green=imgData.data[1];
41      * blue=imgData.data[2];
42      * alpha=imgData.data[3];
43      */
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;
48     return iData;
49   }      
50   
51   /**
52    * Use HTML5 to load PNG, JPG, or GIF image in order to extract its pixels
53    * 
54    * @param b
55    * @param type
56    */
57         @SuppressWarnings("unused")
58         public void getDOMImage(byte[] b, String type) {
59                 String dataurl = "data:image/" + type + ";base64,"  + Base64.getBase64(b).toString();
60                 Object me = this;
61                 DOMNode img = null;
62                 /**
63                  * @j2sNative
64                  *   img = new Image(this.width, this.height);
65                  *   //img.onLoad = function() { me.setDOMImage(img); };
66                  *   img.src = dataurl;
67                  */
68                 {}
69                 setDOMImage(img);
70         }
71                 
72         /**
73          * callback from Image.src = ... ; extract the int[] data from this image;
74          * also sets img._pbuf32 for graphing
75          * 
76          */
77         @SuppressWarnings("unused")
78         public void setDOMImage(DOMNode img) {
79                 DOMNode canvas = DOMNode.createElement("canvas", "JSImage");
80                 int w = width;
81                 int h = height;
82                 _imgNode = img;
83                 /**
84                  * @j2sNative
85                  * 
86                  * canvas.width = w;
87                  * canvas.height = h;
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);
92                  * 
93                  */
94                 {
95                         toIntARGB(null);
96                 }
97         }
98
99         @Override
100         public int getHeight(ImageObserver o) {
101                 return height;
102         }
103
104         @Override
105         public int getWidth(ImageObserver o) {
106                 return width;
107         }
108
109 }