Skip to content

Commit

Permalink
Add vips_image_write_to_file bidding
Browse files Browse the repository at this point in the history
  • Loading branch information
dbouron committed Feb 11, 2021
1 parent 87cb43d commit 6b188cb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/main/c/VipsImage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
16 changes: 12 additions & 4 deletions src/main/c/VipsImage.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/main/java/com/criteo/vips/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/criteo/vips/VipsImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();

Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/criteo/vips/VipsImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit 6b188cb

Please sign in to comment.