Skip to content

Commit

Permalink
✨ Implement VipsImage thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
dbouron committed May 2, 2022
1 parent 651192b commit 0ff230a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
20 changes: 19 additions & 1 deletion src/main/c/VipsImage.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2020 Criteo
Copyright (c) 2022 Criteo
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -229,6 +229,24 @@ Java_com_criteo_vips_VipsImage_thumbnailImageNative(JNIEnv *env, jobject obj, ji
g_object_unref(im);
}

JNIEXPORT jobject JNICALL
Java_com_criteo_vips_VipsImage_thumbnailNative(JNIEnv *env, jobject obj, jstring filename, jint width, jint height, jboolean scale)
{
jobject cls = (*env)->FindClass(env, "com/criteo/vips/VipsImage");
VipsImage *out = NULL;
const char *name = (*env)->GetStringUTFChars(env, filename, NULL);
VipsSize vipsSize = scale ? VIPS_SIZE_FORCE : VIPS_SIZE_BOTH;

if (vips_thumbnail(name, &out, width, "height", height, "size", vipsSize, NULL))
{
(*env)->ReleaseStringUTFChars(env, filename, name);
throwVipsException(env, "Unable to make thumbnail");
return;
}
(*env)->ReleaseStringUTFChars(env, filename, name);
return (*env)->NewObject(env, cls, ctor_mid, (jlong) out);
}

JNIEXPORT void JNICALL
Java_com_criteo_vips_VipsImage_resizeNative(JNIEnv *env, jobject obj, jdouble hscale, jdouble vscale, jint kernel)
{
Expand Down
8 changes: 8 additions & 0 deletions src/main/c/VipsImage.h

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

21 changes: 20 additions & 1 deletion src/main/java/com/criteo/vips/VipsImage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2019 Criteo
Copyright (c) 2022 Criteo
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -156,6 +156,23 @@ public void thumbnailImage(int width, int height, boolean scale) throws VipsExce
thumbnailImageNative(width, height, scale);
}

public static VipsImage thumbnail(String filename, Dimension dimension, boolean scale) throws VipsException {
return thumbnailNative(filename, dimension.width, dimension.height, scale);
}

/**
* Make a thumbnail from a file with new target dimension
*
* @param filename name of the file to load
* @param width Target width
* @param height Target height
* @param scale If scale is enabled, force to resize ignoring aspect ratio
* @throws VipsException if error
*/
public static VipsImage thumbnail(String filename, int width, int height, boolean scale) throws VipsException {
return thumbnailNative(filename, width, height, scale);
}

/**
* @deprecated Use {@link #thumbnailImage(Dimension, boolean)} instead.
*/
Expand All @@ -174,6 +191,8 @@ public void resize(int width, int height, boolean scale) throws VipsException {

private native void thumbnailImageNative(int width, int height, boolean scale) throws VipsException;

private static native VipsImage thumbnailNative(String filename, int width, int height, boolean scale) throws VipsException;

public void resize(double hscale, double vscale, VipsKernel kernel) throws VipsException {
resizeNative(hscale, vscale, kernel.getValue());
}
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/com/criteo/vips/VipsImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public void TestShouldHandleTransparentPixel() throws IOException, VipsException
}

@Test
public void TestShouldRenderThumbnailAndKeepAspectRatio() throws IOException, VipsException {
public void TestShouldRenderThumbnailImageAndKeepAspectRatio() throws IOException, VipsException {
ByteBuffer buffer = VipsTestUtils.getDirectByteBuffer("in_vips.jpg");
try (VipsImage img = new VipsImage(buffer, buffer.capacity())) {
double aspectRatio = (double) img.getWidth() / (double) img.getHeight();
Expand All @@ -332,7 +332,7 @@ public void TestShouldRenderThumbnailAndKeepAspectRatio() throws IOException, Vi
}

@Test
public void TestShouldRenderThumbnailWithExactDimension() throws IOException, VipsException {
public void TestShouldRenderThumbnailImageWithExactDimension() throws IOException, VipsException {
ByteBuffer buffer = VipsTestUtils.getDirectByteBuffer("in_vips.jpg");
try (VipsImage img = new VipsImage(buffer, buffer.capacity())) {
img.thumbnailImage(new Dimension(800, 800), true);
Expand All @@ -341,6 +341,17 @@ public void TestShouldRenderThumbnailWithExactDimension() throws IOException, Vi
}
}

@Test
public void TestShouldRenderThumbnailWithExactDimension() throws IOException, VipsException {
String filename = VipsTestUtils.getRessourcePath("in_vips.jpg");
int expectedWidth = 800;
int expectedHeight = 600;
try (VipsImage img = VipsImage.thumbnail(filename, new Dimension(expectedWidth, expectedHeight), true)) {
assertEquals(expectedWidth, img.getWidth());
assertEquals(expectedHeight, img.getHeight());
}
}

@Test
public void TestShouldResize() throws IOException, VipsException {
ByteBuffer buffer = VipsTestUtils.getDirectByteBuffer("in_vips.jpg");
Expand Down

0 comments on commit 0ff230a

Please sign in to comment.