Skip to content

Commit

Permalink
Implement ImageIO with Foreign
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinrobot committed Jul 14, 2024
1 parent e7325cf commit 0d937d0
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 9 deletions.
2 changes: 2 additions & 0 deletions libwebp-foreign-jdk21/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module net.vinrobot.webp.foreign.jdk21 {
requires net.vinrobot.webp;
requires java.desktop;

exports net.vinrobot.webp.foreign.jdk21;

provides net.vinrobot.webp.WebPReaderSpi with net.vinrobot.webp.foreign.jdk21.JDK21WebPReaderSpi;
provides javax.imageio.spi.ImageReaderSpi with net.vinrobot.webp.foreign.jdk21.JDK21WebPImageReaderSpi;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.vinrobot.webp.foreign.jdk21;

import net.vinrobot.webp.WebPReader;
import net.vinrobot.webp.imageio.BaseImageReader;

import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.ImageInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class JDK21WebPImageReader extends BaseImageReader {
private static final int DEFAULT_BUFFER_SIZE = 16384;

public JDK21WebPImageReader(final ImageReaderSpi originatingProvider) {
super(originatingProvider);
}

private static byte[] readAll(final ImageInputStream stream) throws IOException {
try (final ByteArrayOutputStream result = new ByteArrayOutputStream()) {
final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = stream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toByteArray();
}
}

@Override
protected WebPReader getReader(final ImageInputStream stream) throws IOException {
return this.getReader(readAll(stream));
}

@Override
protected WebPReader getReader(final byte[] data) throws IOException {
return new JDK21WebPReader(data, 0, data.length);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.vinrobot.webp.foreign.jdk21;

import net.vinrobot.webp.foreign.jdk21.internal.Loader;
import net.vinrobot.webp.imageio.BaseImageReaderSpi;

import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.io.IOException;

public final class JDK21WebPImageReaderSpi extends BaseImageReaderSpi {
public JDK21WebPImageReaderSpi() {
super(JDK21WebPImageReader.class, new Class[]{ImageInputStream.class, byte[].class});
}

@Override
public boolean canDecodeInput(final Object source) throws IOException {
if (!super.canDecodeInput(source)) {
return false;
}

try {
// The reader needs the native library to work
Loader.ensureLibrary();
return true;
} catch (Exception ex) {
// Unable to load native library
return false;
}
}

@Override
public ImageReader createReaderInstance(final Object extension) {
return new JDK21WebPImageReader(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.vinrobot.webp.foreign.jdk21.internal.WebPAnimInfo;
import net.vinrobot.webp.foreign.jdk21.internal.WebPData;

import java.io.IOException;
import java.lang.foreign.Arena;

public class JDK21WebPReader implements WebPReader {
Expand All @@ -17,7 +18,7 @@ public class JDK21WebPReader implements WebPReader {
private final WebPAnimDecoder decoder;
private WebPMetadata metadata;

public JDK21WebPReader(final byte[] buffer, final int offset, final int length) {
public JDK21WebPReader(final byte[] buffer, final int offset, final int length) throws IOException {
Loader.ensureLibrary();

this.arena = Arena.ofConfined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ public final class Loader {

private static boolean initialized = false;

public static void ensureLibrary() {
public static void ensureLibrary() throws IOException {
if (!initialized) {
synchronized (Loader.class) {
if (!initialized) {
try {
final Path libPath = Platform.extractFromResourcePath(LIB_NAME);
System.load(libPath.toString());
initialized = true;
} catch (IOException e) {
throw new RuntimeException(e);
}
final Path libPath = Platform.extractFromResourcePath(LIB_NAME);
System.load(libPath.toString());
initialized = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
net.vinrobot.webp.foreign.jdk21.JDK21WebPImageReaderSpi
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.vinrobot.webp.foreign.jdk21;

import net.vinrobot.webp.ImageReaderSpiTest;

import javax.imageio.spi.ImageReaderSpi;

class JDK21WebPImageReaderSpiTest extends ImageReaderSpiTest {
@Override
public Class<? extends ImageReaderSpi> getServiceClass() {
return JDK21WebPImageReaderSpi.class;
}

@Override
public ImageReaderSpi getService() {
return new JDK21WebPImageReaderSpi();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.vinrobot.webp.foreign.jdk21;

import net.vinrobot.webp.ImageReaderTest;

import javax.imageio.ImageReader;
import java.io.IOException;

class JDK21WebPImageReaderTest extends ImageReaderTest {
@Override
public Class<? extends ImageReader> getImageReaderClass() {
return JDK21WebPImageReader.class;
}

@Override
public ImageReader newImageReader() {
try {
return new JDK21WebPImageReaderSpi().createReaderInstance();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 0d937d0

Please sign in to comment.