-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to pass image data from JS to WASM and vice versa #1011
Comments
Ok, import de.mirkosertic.bytecoder.api.Export;
import de.mirkosertic.bytecoder.api.web.Int8Array;
import de.mirkosertic.bytecoder.api.web.OpaqueArrays;
public class ImageModifier {
public static void main(String[] args) {
}
@Export("process")
public static Int8Array process(Int8Array imageData) {
byte[] bytes = int8ToByteArray(imageData);
// do some image data processing
int sum = 0;
for(byte b: bytes) {
sum += b;
}
System.out.println(sum);
return byteArrayToInt8(bytes); // return the processed bytes (in this case the unchanged data)
}
private static byte[] int8ToByteArray(Int8Array int8Array) {
final int arrayLen = int8Array.byteArrayLength();
byte[] res = new byte[arrayLen];
for (int i = 0; i < arrayLen; ++i) {
res[i] = int8Array.getByte(i);
}
return res;
}
private static Int8Array byteArrayToInt8(byte[] byteArray) {
final int arrayLen = byteArray.length;
final Int8Array res = OpaqueArrays.createInt8Array(arrayLen);
for (int i = 0; i < arrayLen; ++i) {
res.setByte(i, byteArray[i]);
}
return res;
}
} // properly initialize everything with no errors
const array = new Int8Array([1, 2, 3, 4]);
const res = bytecoder.exports.process(null, array); // TypeError: bad type
console.log(res); But it raises a TypeError when calling the exported Thanks in advance for your help, |
Yeah, I tried to do it in a similar way and got stuck like you. edit: I also worked with the bytecoder.toBytecoderString(), but I might have made a mistake while using it |
@tzirit Quick disclaimer : If you have any questions on which I might have an answer don't hesitate to ping me, also the TeaVM maintainer is very nice and helpful. |
Hello !
I'm currently trying to process image data on the browser using java code compiled to WASM.
To do so, I'm looking for a way to pass an Uint8Array from the JS side to WASM and get one in return, like below :
The issue is that in the documentation it is said that supported types are :
From what I understand from #433 and #542 it would be possible in my case to use an OpaqueReferenceType, but I wasn't able to find how I should use them.
Could you provide me (if it's possible) an example on how I could pass my data around ?
Also on a side note, I wasn't able to find an explanation on why we have to provide an unused parameter for all functions exported from WASM. Am I missing something ?
Thanks in advance,
--Barnabé
The text was updated successfully, but these errors were encountered: