From 6b188cb91bd3f02039ec31a82f72cf35bdeb2bd4 Mon Sep 17 00:00:00 2001 From: Dimitri Bouron Date: Tue, 9 Feb 2021 12:25:00 +0100 Subject: [PATCH] Add vips_image_write_to_file bidding --- src/main/c/VipsImage.c | 13 +++++++++++++ src/main/c/VipsImage.h | 16 ++++++++++++---- src/main/java/com/criteo/vips/Image.java | 8 ++++++++ src/main/java/com/criteo/vips/VipsImage.java | 4 +++- .../java/com/criteo/vips/VipsImageTest.java | 18 ++++++++++++++++++ 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/main/c/VipsImage.c b/src/main/c/VipsImage.c index cd9f0a67..f9a79b1d 100644 --- a/src/main/c/VipsImage.c +++ b/src/main/c/VipsImage.c @@ -387,6 +387,19 @@ Java_com_criteo_vips_VipsImage_writeAVIFToArrayNative(JNIEnv *env, jobject obj, return ret; } +JNIEXPORT void JNICALL +Java_com_criteo_vips_VipsImage_writeToFile(JNIEnv *env, jobject obj, jstring name) +{ + const char *filename = (*env)->GetStringUTFChars(env, name, NULL); + VipsImage *im = (VipsImage *) (*env)->GetLongField(env, obj, handle_fid); + + if (vips_image_write_to_file(im, filename, NULL)) + { + throwVipsException(env, "Unable to write to file"); + } + (*env)->ReleaseStringUTFChars(env, name, filename); +} + JNIEXPORT jint JNICALL Java_com_criteo_vips_VipsImage_getWidth(JNIEnv *env, jobject obj) { diff --git a/src/main/c/VipsImage.h b/src/main/c/VipsImage.h index e0841e2e..47640ce3 100644 --- a/src/main/c/VipsImage.h +++ b/src/main/c/VipsImage.h @@ -167,6 +167,14 @@ JNIEXPORT void JNICALL Java_com_criteo_vips_VipsImage_compose JNIEXPORT void JNICALL Java_com_criteo_vips_VipsImage_flattenNative (JNIEnv *, jobject, jdoubleArray); +/* + * Class: com_criteo_vips_VipsImage + * Method: writeToArrayNative + * Signature: (Ljava/lang/String;IZ)[B + */ +JNIEXPORT jbyteArray JNICALL Java_com_criteo_vips_VipsImage_writeToArrayNative + (JNIEnv *, jobject, jstring, jint, jboolean); + /* * Class: com_criteo_vips_VipsImage * Method: writePNGToArrayNative @@ -185,11 +193,11 @@ JNIEXPORT jbyteArray JNICALL Java_com_criteo_vips_VipsImage_writeAVIFToArrayNati /* * Class: com_criteo_vips_VipsImage - * Method: writeToArrayNative - * Signature: (Ljava/lang/String;IZ)[B + * Method: writeToFile + * Signature: (Ljava/lang/String;)V */ -JNIEXPORT jbyteArray JNICALL Java_com_criteo_vips_VipsImage_writeToArrayNative - (JNIEnv *, jobject, jstring, jint, jboolean); +JNIEXPORT void JNICALL Java_com_criteo_vips_VipsImage_writeToFile + (JNIEnv *, jobject, jstring); /* * Class: com_criteo_vips_VipsImage diff --git a/src/main/java/com/criteo/vips/Image.java b/src/main/java/com/criteo/vips/Image.java index 24805214..c034315c 100644 --- a/src/main/java/com/criteo/vips/Image.java +++ b/src/main/java/com/criteo/vips/Image.java @@ -220,6 +220,14 @@ public interface Image extends AutoCloseable { */ byte[] writePNGToArray(int compression, boolean palette, int colors, boolean strip) throws VipsException; + /** + * Write VipsImage to file + * + * @param name Output file name + * @throws VipsException if error + */ + void writeToFile(String name) throws VipsException; + /** * @return VipsImage width */ diff --git a/src/main/java/com/criteo/vips/VipsImage.java b/src/main/java/com/criteo/vips/VipsImage.java index 55bbcb04..db57be6c 100644 --- a/src/main/java/com/criteo/vips/VipsImage.java +++ b/src/main/java/com/criteo/vips/VipsImage.java @@ -190,6 +190,8 @@ public byte[] writeToArray(VipsImageFormat imageFormat, int quality, boolean str return writeToArrayNative(imageFormat.getFileExtension(), quality, strip); } + private native byte[] writeToArrayNative(String extension, int quality, boolean strip) throws VipsException; + public byte[] writePNGToArray(int compression, boolean palette, int colors, boolean strip) throws VipsException { return writePNGToArrayNative(compression, palette, colors, strip); } @@ -202,7 +204,7 @@ public byte[] writeAVIFToArray(int Q, boolean lossless, int speed) throws VipsEx private native byte[] writeAVIFToArrayNative(int Q, boolean lossless, int speed) throws VipsException; - private native byte[] writeToArrayNative(String extension, int quality, boolean strip) throws VipsException; + public native void writeToFile(String name) throws VipsException;; public native int getWidth(); diff --git a/src/test/java/com/criteo/vips/VipsImageTest.java b/src/test/java/com/criteo/vips/VipsImageTest.java index e27b106b..89f65ad4 100644 --- a/src/test/java/com/criteo/vips/VipsImageTest.java +++ b/src/test/java/com/criteo/vips/VipsImageTest.java @@ -31,8 +31,11 @@ import org.junit.runner.RunWith; import java.awt.*; +import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import java.util.stream.IntStream; @@ -860,4 +863,19 @@ public void TestClone() throws IOException, VipsException { copy.release(); } } + + @Test + public void TestWriteToFilename() throws IOException, VipsException { + String filename = "in_vips.jpg"; + ByteBuffer buffer = VipsTestUtils.getDirectByteBuffer(filename); + Path path = Paths.get(System.getProperty("java.io.tmpdir")); + try (VipsImage img = new VipsImage(buffer, buffer.capacity())) { + path = path.resolve(filename); + File file = path.toAbsolutePath().toFile(); + img.writeToFile(file.getAbsolutePath()); + assertTrue(file.exists()); + assertTrue(file.isFile()); + assertTrue(file.delete()); + } + } }