JAL-1807 - Bob's last(?) before leaving Dundee -- adds fast file loading
[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;
12         public DOMNode _imgNode; // used by JSGraphics2D directly
13         private int width, height;
14         
15         public JSImage(int[] argb, int width, int height) {
16                 super(width, height, TYPE_INT_ARGB);
17                 this.width = width;
18                 this.height = height;
19                 pix = argb;
20         }
21         
22         /**
23          * convert [r g b a  r g b a ...] into [argb argb argb ...]
24          * 
25          * currently does not respect transparency
26          * 
27          * @param imgData HTML5 canvas.context.imageData.data
28          * @return array of ARGB values
29          * 
30          */
31   int[] toIntARGB(int[] imgData) {
32     /*
33      * red=imgData.data[0];
34      * green=imgData.data[1];
35      * blue=imgData.data[2];
36      * alpha=imgData.data[3];
37      */
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;
42     return iData;
43   }      
44   
45   /**
46    * Use HTML5 to load PNG, JPG, or GIF image in order to extract its pixels
47    * 
48    * @param b
49    * @param type
50    */
51         @SuppressWarnings("unused")
52         public void getDOMImage(byte[] b, String type) {
53                 String dataurl = "data:image/" + type + ";base64,"  + Base64.getBase64(b).toString();
54                 Object me = this;
55                 DOMNode img = null;
56                 /**
57                  * @j2sNative
58                  *   img = new Image(this.width, this.height);
59                  *   //img.onLoad = function() { me.setDOMImage(img); };
60                  *   img.src = dataurl;
61                  */
62                 {}
63                 setDOMImage(img);
64         }
65                 
66         /**
67          * callback from Image.src = ... ; extract the int[] data from this image;
68          * also sets img._pbuf32 for graphing
69          * 
70          */
71         @SuppressWarnings("unused")
72         public void setDOMImage(DOMNode img) {
73                 DOMNode canvas = DOMNode.createElement("canvas", "JSImage");
74                 int w = width;
75                 int h = height;
76                 _imgNode = img;
77                 /**
78                  * @j2sNative
79                  * 
80                  * canvas.width = w;
81                  * canvas.height = h;
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);
86                  * 
87                  */
88                 {
89                         toIntARGB(null);
90                 }
91         }
92
93         @Override
94         public int getHeight(ImageObserver o) {
95                 return height;
96         }
97
98         @Override
99         public int getWidth(ImageObserver o) {
100                 return width;
101         }
102
103 }