toni@521
|
1 |
/**
|
toni@1156
|
2 |
* Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
|
toni@1156
|
3 |
* <jaroslav.tulach@apidesign.org>
|
toni@521
|
4 |
*
|
toni@1156
|
5 |
* This program is free software: you can redistribute it and/or modify it under
|
toni@1156
|
6 |
* the terms of the GNU General Public License as published by the Free Software
|
toni@1156
|
7 |
* Foundation, version 2 of the License.
|
toni@521
|
8 |
*
|
toni@1156
|
9 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
toni@1156
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
toni@1156
|
11 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
toni@1156
|
12 |
* details.
|
toni@521
|
13 |
*
|
toni@1156
|
14 |
* You should have received a copy of the GNU General Public License along with
|
toni@1156
|
15 |
* this program. Look for COPYING file in the top folder. If not, see
|
toni@1156
|
16 |
* http://opensource.org/licenses/GPL-2.0.
|
toni@521
|
17 |
*/
|
toni@1158
|
18 |
|
toni@1135
|
19 |
package org.apidesign.bck2brwsr.htmlpage;
|
toni@521
|
20 |
|
toni@1145
|
21 |
import net.java.html.canvas.ImageData;
|
toni@521
|
22 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
toni@521
|
23 |
|
toni@521
|
24 |
/**
|
toni@521
|
25 |
*
|
toni@521
|
26 |
* @author Anton Epple <toni.epple@eppleton.de>
|
toni@521
|
27 |
*/
|
toni@1156
|
28 |
public class ImageDataWrapper implements ImageData {
|
toni@521
|
29 |
|
toni@1156
|
30 |
private double width, height = -1;
|
toni@521
|
31 |
private Object imageData;
|
toni@521
|
32 |
private Data data;
|
toni@521
|
33 |
|
toni@1134
|
34 |
public ImageDataWrapper(Object imageData) {
|
toni@521
|
35 |
this.imageData = imageData;
|
toni@521
|
36 |
}
|
toni@1156
|
37 |
|
toni@1156
|
38 |
private Data getData() {
|
toni@1156
|
39 |
if (data == null) {
|
toni@521
|
40 |
data = new Data(getDataImpl(imageData));
|
toni@521
|
41 |
}
|
toni@521
|
42 |
return data;
|
toni@521
|
43 |
}
|
toni@1156
|
44 |
|
toni@521
|
45 |
@JavaScriptBody(args = {"imageData"}, body = "return imageData.data")
|
toni@521
|
46 |
public native Object getDataImpl(Object imageData);
|
toni@521
|
47 |
|
toni@521
|
48 |
public double getWidth() {
|
toni@1156
|
49 |
if (width == -1) {
|
toni@1156
|
50 |
width = getWidthImpl(imageData);
|
toni@1156
|
51 |
}
|
toni@1156
|
52 |
return width;
|
toni@521
|
53 |
}
|
toni@521
|
54 |
|
toni@521
|
55 |
@JavaScriptBody(args = {"imageData"}, body = "return imagedata.width;")
|
toni@1441
|
56 |
private static native int getWidthImpl(Object imageData);
|
toni@521
|
57 |
|
toni@521
|
58 |
public double getHeight() {
|
toni@1156
|
59 |
if (height == -1) {
|
toni@1156
|
60 |
height = getHeightImpl(imageData);
|
toni@1156
|
61 |
}
|
toni@1156
|
62 |
return height;
|
toni@521
|
63 |
}
|
toni@521
|
64 |
|
toni@521
|
65 |
@JavaScriptBody(args = {"imageData"}, body = "return imagedata.height;")
|
toni@1441
|
66 |
private static native int getHeightImpl(Object imageData);
|
toni@521
|
67 |
|
toni@521
|
68 |
Object object() {
|
toni@521
|
69 |
return imageData;
|
toni@521
|
70 |
}
|
toni@521
|
71 |
|
toni@1145
|
72 |
@Override
|
toni@1441
|
73 |
public int getR(int x, int y) {
|
toni@1441
|
74 |
int idx = (x + y * (int)width) * 4;
|
toni@1156
|
75 |
return getData().get(idx);
|
toni@1145
|
76 |
}
|
toni@1145
|
77 |
|
toni@1145
|
78 |
@Override
|
toni@1441
|
79 |
public int getG(int x, int y) {
|
toni@1441
|
80 |
int idx = (x + y * (int)width) * 4;
|
toni@1156
|
81 |
return getData().get(idx + 1);
|
toni@1145
|
82 |
}
|
toni@1145
|
83 |
|
toni@1156
|
84 |
@Override
|
toni@1441
|
85 |
public int getB(int x, int y) {
|
toni@1441
|
86 |
int idx = (x + y * (int)width) * 4;
|
toni@1156
|
87 |
return getData().get(idx + 2);
|
toni@1156
|
88 |
}
|
toni@1156
|
89 |
|
toni@1156
|
90 |
@Override
|
toni@1441
|
91 |
public int getA(int x, int y) {
|
toni@1441
|
92 |
int idx = (x + y * (int)width) * 4;
|
toni@1156
|
93 |
return getData().get(idx + 3);
|
toni@1156
|
94 |
}
|
toni@1156
|
95 |
|
toni@1156
|
96 |
@Override
|
toni@1441
|
97 |
public void setR(int x, int y, int value) {
|
toni@1441
|
98 |
int idx = (x + y * (int)width) * 4;
|
toni@1156
|
99 |
getData().set(idx, value);
|
toni@1156
|
100 |
}
|
toni@1156
|
101 |
|
toni@1156
|
102 |
@Override
|
toni@1441
|
103 |
public void setG(int x, int y, int value) {
|
toni@1441
|
104 |
int idx = (x + y * (int)width) * 4;
|
toni@1156
|
105 |
getData().set(idx + 1, value);
|
toni@1156
|
106 |
}
|
toni@1156
|
107 |
|
toni@1156
|
108 |
@Override
|
toni@1441
|
109 |
public void setB(int x, int y, int value) {
|
toni@1441
|
110 |
int idx = (x + y * (int)width) * 4;
|
toni@1156
|
111 |
getData().set(idx + 2, value);
|
toni@1156
|
112 |
}
|
toni@1156
|
113 |
|
toni@1156
|
114 |
@Override
|
toni@1441
|
115 |
public void setA(int x, int y, int value) {
|
toni@1441
|
116 |
int idx = (x + y * (int)width) * 4;
|
toni@1156
|
117 |
getData().set(idx + 3, value);
|
toni@1156
|
118 |
}
|
toni@1134
|
119 |
|
toni@1158
|
120 |
private static class Data {
|
toni@521
|
121 |
|
toni@521
|
122 |
Object data;
|
toni@521
|
123 |
|
toni@521
|
124 |
public Data(Object data) {
|
toni@521
|
125 |
this.data = data;
|
toni@521
|
126 |
}
|
toni@521
|
127 |
|
toni@1441
|
128 |
public int get(int index) {
|
toni@521
|
129 |
return getImpl(data, index);
|
toni@521
|
130 |
}
|
toni@521
|
131 |
|
toni@1441
|
132 |
public void set(int index, int value) {
|
toni@521
|
133 |
setImpl(data, index, value);
|
toni@521
|
134 |
}
|
toni@521
|
135 |
|
toni@521
|
136 |
@JavaScriptBody(args = {"data", "index", "value"}, body = "data[index]=value;")
|
toni@1441
|
137 |
private static native void setImpl(Object data, int index, int value);
|
toni@521
|
138 |
|
toni@521
|
139 |
@JavaScriptBody(args = {"imagedata", "index"}, body = "return data[index];")
|
toni@1441
|
140 |
private static native int getImpl(Object data, int index);
|
toni@521
|
141 |
}
|
toni@521
|
142 |
}
|