Skip to content
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

Add Java bindings #855

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4300586
feat: add initial Maven POM
ctrueden Jun 7, 2023
733ab4b
feat: extend pom-scijava parent POM
ctrueden Jun 7, 2023
5904f33
feat: add placeholder main class as starting point
ctrueden Jun 7, 2023
7f6ab63
feat: ignore build folder and Eclipse metadata
ctrueden Jun 7, 2023
61eae3f
feat: update main class to run WASM code
ctrueden Jun 7, 2023
059a6a8
feat: update main code to call itk-wasm code
ctrueden Jun 7, 2023
30fb527
Remove unused imports
ctrueden Jun 7, 2023
dcc84e2
Translate some Python classes to Java
ctrueden Jun 7, 2023
446830d
WIP: start migrating Pipeline class
ctrueden Jun 7, 2023
fb4d1ea
WIP: continuing Pipeline implementation
ctrueden Jun 7, 2023
73a3972
Fix whitespace
ctrueden Jun 7, 2023
485a18c
Migrate packages test files to packages root
ctrueden Jun 7, 2023
44dd043
WIP: migrate pipeline test class
ctrueden Jun 7, 2023
f9a4528
Fix itk-wasm java version
ctrueden Jun 9, 2023
20bbece
Ignore more WASI build files
ctrueden Jun 9, 2023
78f8b4f
Remove empty Python source file
ctrueden Jun 9, 2023
f80002a
Add BinaryFile and TextFile types
ctrueden Jun 9, 2023
b430589
Migrate a few more classes from Python
ctrueden Jun 9, 2023
2265d68
TestPipeline: fix resource paths
ctrueden Jun 9, 2023
60c0123
Pipeline: comment out non-working methods
ctrueden Jun 9, 2023
85b7225
Main: fix path to .wasm
ctrueden Jun 9, 2023
2eba934
WIP: IT WORKS
ctrueden Jun 9, 2023
d1cda25
Migrate more pipeline test functions
ctrueden Jun 9, 2023
15ea877
WIP: start adding image support and related code
ctrueden Jun 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ build-emscripten
build-wasi
src/io/internal/ImageIOIndex.ts
src/io/internal/MeshIOIndex.ts
wasi-build/

dist/*
!dist/dicom
Expand Down
4 changes: 4 additions & 0 deletions packages/core/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.classpath
/.project
/.settings/
/target/
111 changes: 111 additions & 0 deletions packages/core/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>35.1.1</version>
</parent>

<groupId>org.itk.wasm</groupId>
<artifactId>itk-wasm</artifactId>
<version>0.1.0-SNAPSHOT</version>

<name>itk-wasm for Java</name>
<description>Java interface to itk-wasm WebAssembly modules.</description>
<url>https://github.com/InsightSoftwareConsortium/itk-wasm</url>
<inceptionYear>2023</inceptionYear>
<organization>
<name>ITK</name>
<url>https://itk.org/</url>
</organization>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>

<developers>
<developer>
<name>Matt McCormick</name>
<url>https://github.com/thewtex</url>
<id>thewtex</id>
</developer>
</developers>
<contributors>
<contributor>
<name>Curtis Rueden</name>
<url>https://github.com/ctrueden</url>
<properties><id>ctrueden</id></properties>
</contributor>
</contributors>

<mailingLists>
<mailingList>
<name>ITK Forum</name>
<archive>https://discourse.itk.org/</archive>
</mailingList>
</mailingLists>

<scm>
<connection>scm:https://github.com/InsightSoftwareConsortium/itk-wasm</connection>
<developerConnection>scm:[email protected]:InsightSoftwareConsortium/itk-wasm</developerConnection>
<url>https://github.com/InsightSoftwareConsortium/itk-wasm</url>
</scm>
<issueManagement>
<system>GitHub Issues</system>
<url>https://github.com/InsightSoftwareConsortium/itk-wasm/issues</url>
</issueManagement>
<ciManagement>
<system>GitHub Actions</system>
<url>https://github.com/InsightSoftwareConsortium/itk-wasm/actions</url>
</ciManagement>

<properties>
<main-class>org.itk.wasm.Main</main-class>

<license.licenseName>apache_v2</license.licenseName>
<license.copyrightOwners>ITK developers.</license.copyrightOwners>
<license.projectName>Java bindings for itk-wasm.</license.projectName>

<wasmtime-java.version>0.14.0</wasmtime-java.version>
</properties>

<dependencies>
<dependency>
<groupId>io.github.kawamuray.wasmtime</groupId>
<artifactId>wasmtime-java</artifactId>
<version>${wasmtime-java.version}</version>
</dependency>
<dependency>
<groupId>net.imglib2</groupId>
<artifactId>imglib2</artifactId>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<!-- Test scope dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
15 changes: 15 additions & 0 deletions packages/core/java/src/main/java/org/itk/wasm/BinaryFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.itk.wasm;

import java.nio.file.Path;

public class BinaryFile {
public PurePosixPath path;

public BinaryFile(PurePosixPath path) {
this.path = path;
}

public BinaryFile(Path path) {
this(new PurePosixPath(path));
}
}
28 changes: 28 additions & 0 deletions packages/core/java/src/main/java/org/itk/wasm/BinaryStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*-
* #%L
* Java bindings for itk-wasm.
* %%
* Copyright (C) 2023 ITK developers.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.itk.wasm;

public class BinaryStream {
public byte[] data;

public BinaryStream(byte[] data) {
this.data = data;
}
}
18 changes: 18 additions & 0 deletions packages/core/java/src/main/java/org/itk/wasm/FloatTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.itk.wasm;

public enum FloatTypes {
Float32("float32"),
Float64("float64"),
SpacePrecisionType("float64");

private final String value;

FloatTypes(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}
}
29 changes: 29 additions & 0 deletions packages/core/java/src/main/java/org/itk/wasm/IO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.itk.wasm;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public final class IO {

public IO() { }

public static byte[] readBytes(String path) throws IOException {
//try (InputStream is = Main.class.getResourceAsStream(filename)) {
try (InputStream is = new FileInputStream(path)) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] buf = new byte[16384];
while ((nRead = is.read(buf, 0, buf.length)) != -1) {
buffer.write(buf, 0, nRead);
}
return buffer.toByteArray();
}
}

public static String readString(String path) throws IOException {
return new String(readBytes(path), StandardCharsets.UTF_8);
}
}
69 changes: 69 additions & 0 deletions packages/core/java/src/main/java/org/itk/wasm/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.itk.wasm;

import java.util.*;

public class Image {
public ImageType imageType;
public String name;
public List<Double> origin;
public List<Double> spacing;
public double[][] direction;
public List<Integer> size;
public Map<String, Object> metadata;
public double[] data;

public Image() {
this.imageType = new ImageType();
this.name = "Image";
this.origin = new ArrayList<>();
this.spacing = new ArrayList<>();
this.direction = new double[0][0];
this.size = new ArrayList<>();
this.metadata = new HashMap<>();
this.data = null;
}

public void postInit() {
if (imageType instanceof Map) {
imageType = new ImageType();
Map<String, Object> imageTypeMap = asdict(imageType);
// Set values from the map to the corresponding fields in ImageType
// Example: imageType.setDimension((int) imageTypeMap.get("dimension"));
// Add similar code for other fields
}

int dimension = imageType.dimension;
if (origin.isEmpty()) {
for (int i = 0; i < dimension; i++) {
origin.add(0.0);
}
}

if (spacing.isEmpty()) {
for (int i = 0; i < dimension; i++) {
spacing.add(1.0);
}
}

if (direction.length == 0) {
direction = new double[dimension][dimension];
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
if (i == j) {
direction[i][j] = 1.0;
} else {
direction[i][j] = 0.0;
}
}
}
}

if (size.isEmpty()) {
for (int i = 0; i < dimension; i++) {
size.add(1);
}
}
}

// Add getters and setters for the fields
}
17 changes: 17 additions & 0 deletions packages/core/java/src/main/java/org/itk/wasm/ImageType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.itk.wasm;

public class ImageType {
public int dimension;
public IntTypes componentType;
public PixelType pixelType;
public int components;

public ImageType() {
this.dimension = 2;
this.componentType = IntTypes.UInt8;
this.pixelType = PixelType.Scalar;
this.components = 1;
}

// Add getters and setters for the fields
}
27 changes: 27 additions & 0 deletions packages/core/java/src/main/java/org/itk/wasm/IntTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.itk.wasm;

public enum IntTypes {
Int8("int8"),
UInt8("uint8"),
Int16("int16"),
UInt16("uint16"),
Int32("int32"),
UInt32("uint32"),
Int64("int64"),
UInt64("uint64"),
SizeValueType("uint64"),
IdentifierType("uint64"),
IndexValueType("int64"),
OffsetValueType("int64");

private final String value;

IntTypes(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}
}
31 changes: 31 additions & 0 deletions packages/core/java/src/main/java/org/itk/wasm/InterfaceType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*-
* #%L
* Java bindings for itk-wasm.
* %%
* Copyright (C) 2023 ITK developers.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.itk.wasm;

public enum InterfaceType {
TextFile,
BinaryFile,
TextStream,
BinaryStream,
Image,
Mesh,
PolyData,
JsonObject
}
19 changes: 19 additions & 0 deletions packages/core/java/src/main/java/org/itk/wasm/JsonObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.itk.wasm;

import java.util.Map;

public class JsonObject {
private Map<String, Object> data;

public JsonObject(Map<String, Object> data) {
this.data = data;
}

public Map<String, Object> getData() {
return data;
}

public void setData(Map<String, Object> data) {
this.data = data;
}
}
Loading