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

thumbnail: add support for other options #135

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
71 changes: 71 additions & 0 deletions src/main/c/VipsImage.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,42 @@ Java_com_criteo_vips_VipsImage_thumbnailImageNative(JNIEnv *env, jobject obj, ji
g_object_unref(im);
}

JNIEXPORT void JNICALL
Java_com_criteo_vips_VipsImage_thumbnailImageWithOptionsNative(JNIEnv *env, jobject obj, jint width, jint height, jobject options)
{
VipsImage *im = (VipsImage *) (*env)->GetLongField(env, obj, handle_fid);
VipsImage *out = NULL;

jclass optionsCls = (*env)->GetObjectClass(env, options);
jfieldID sizeFid = (*env)->GetFieldID(env, optionsCls, "size", "I");
jfieldID noRotateFid = (*env)->GetFieldID(env, optionsCls, "noRotate", "Z");
jfieldID cropFid = (*env)->GetFieldID(env, optionsCls, "crop", "I");
jfieldID linearFid = (*env)->GetFieldID(env, optionsCls, "linear", "Z");
jfieldID importProfileFid = (*env)->GetFieldID(env, optionsCls, "importProfile", "Ljava/lang/String;");
jfieldID exportProfileFid = (*env)->GetFieldID(env, optionsCls, "exportProfile", "Ljava/lang/String;");
jfieldID intentFid = (*env)->GetFieldID(env, optionsCls, "intent", "I");

jint size = (*env)->GetIntField(env, options, sizeFid);
jboolean noRotate = (*env)->GetBooleanField(env, options, noRotateFid);
jint crop = (*env)->GetIntField(env, options, cropFid);
jboolean linear = (*env)->GetBooleanField(env, options, linearFid);
jstring importProfile = (jstring) (*env)->GetObjectField(env, options, importProfileFid); // TODO how to pass if set, as there is no default
jstring exportProfile = (jstring) (*env)->GetObjectField(env, options, exportProfileFid); // TODO how to pass if set, as there is no default
jint intent = (*env)->GetIntField(env, options, intentFid);

VipsSize vipsSize = size != -1 ? size : VIPS_SIZE_BOTH;
VipsInteresting vipsCrop = crop != -1 ? crop : VIPS_INTERESTING_NONE;
VipsIntent vipsIntent = intent != -1 ? intent : VIPS_INTENT_RELATIVE;

if (vips_thumbnail_image(im, &out, width, "height", height, "size", vipsSize, "no-rotate", noRotate, "crop", vipsCrop, "linear", linear, "intent", intent, NULL))
{
throwVipsException(env, "Unable to make thumbnail image");
return;
}
(*env)->SetLongField(env, obj, handle_fid, (jlong) out);
g_object_unref(im);
}

JNIEXPORT jobject JNICALL
Java_com_criteo_vips_VipsImage_thumbnailNative(JNIEnv *env, jclass cls, jstring filename, jint width, jint height, jboolean scale)
{
Expand All @@ -244,6 +280,41 @@ Java_com_criteo_vips_VipsImage_thumbnailNative(JNIEnv *env, jclass cls, jstring
return (*env)->NewObject(env, cls, ctor_mid, (jlong) out);
}

JNIEXPORT jobject JNICALL
Java_com_criteo_vips_VipsImage_thumbnailWithImageNative(JNIEnv *env, jclass cls, jstring filename, jint width, jint height, jobject options)
{
VipsImage *out = NULL;
const char *name = (*env)->GetStringUTFChars(env, filename, NULL);

jclass optionsCls = (*env)->GetObjectClass(env, options);
jfieldID sizeFid = (*env)->GetFieldID(env, optionsCls, "size", "I");
jfieldID noRotateFid = (*env)->GetFieldID(env, optionsCls, "noRotate", "Z");
jfieldID cropFid = (*env)->GetFieldID(env, optionsCls, "crop", "I");
jfieldID linearFid = (*env)->GetFieldID(env, optionsCls, "linear", "Z");
jfieldID importProfileFid = (*env)->GetFieldID(env, optionsCls, "importProfile", "Ljava/lang/String;");
jfieldID exportProfileFid = (*env)->GetFieldID(env, optionsCls, "exportProfile", "Ljava/lang/String;");
jfieldID intentFid = (*env)->GetFieldID(env, optionsCls, "intent", "I");

jint size = (*env)->GetIntField(env, options, sizeFid);
jboolean noRotate = (*env)->GetBooleanField(env, options, noRotateFid);
jint crop = (*env)->GetIntField(env, options, cropFid);
jboolean linear = (*env)->GetBooleanField(env, options, linearFid);
jstring importProfile = (jstring) (*env)->GetObjectField(env, options, importProfileFid); // TODO how to pass if set, as there is no default
jstring exportProfile = (jstring) (*env)->GetObjectField(env, options, exportProfileFid); // TODO how to pass if set, as there is no default
jint intent = (*env)->GetIntField(env, options, intentFid);

VipsSize vipsSize = size != -1 ? size : VIPS_SIZE_BOTH;
VipsInteresting vipsCrop = crop != -1 ? crop : VIPS_INTERESTING_NONE;
VipsIntent vipsIntent = intent != -1 ? intent : VIPS_INTENT_RELATIVE;

if (vips_thumbnail(name, &out, width, "height", height, "size", vipsSize, "no-rotate", noRotate, "crop", vipsCrop, "linear", linear, "intent", intent, NULL))
{
throwVipsException(env, "Unable to make thumbnail");
}
(*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
16 changes: 16 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.

3 changes: 2 additions & 1 deletion src/main/java/com/criteo/vips/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ set(JAVA_SOURCE_FILES
"${JAVA_SOURCE_DIRECTORY}/Image.java"
"${JAVA_SOURCE_DIRECTORY}/VipsImage.java")
file(GLOB JAVA_ENUM_SOURCE_FILES "${JAVA_SOURCE_DIRECTORY}/enums/*.java")
file(GLOB JAVA_OPTIONS_SOURCE_FILES "${JAVA_SOURCE_DIRECTORY}/options/*.java")

add_jar(JVipsWrapper ${JAVA_ENUM_SOURCE_FILES} ${JAVA_SOURCE_FILES})
add_jar(JVipsWrapper ${JAVA_ENUM_SOURCE_FILES} ${JAVA_OPTIONS_SOURCE_FILES} ${JAVA_SOURCE_FILES})

file(REMOVE ${JNI_HEADER_DIRECTORY}/Vips.h
${JNI_HEADER_DIRECTORY}/VipsContext.h
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/criteo/vips/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.criteo.vips;

import com.criteo.vips.enums.*;
import com.criteo.vips.options.ThumbnailOptions;

import java.awt.*;

Expand Down Expand Up @@ -138,6 +139,8 @@ public interface Image extends AutoCloseable {
*/
void thumbnailImage(Dimension dimension, boolean scale) throws VipsException;

void thumbnailImage(Dimension dimension, ThumbnailOptions options) throws VipsException;

/**
* Make a thumbnail of this VipsImage with new target dimension
*
Expand All @@ -148,6 +151,8 @@ public interface Image extends AutoCloseable {
*/
void thumbnailImage(int width, int height, boolean scale) throws VipsException;

void thumbnailImage(int width, int height, ThumbnailOptions options) throws VipsException;

/**
* Make a thumbnail of this VipsImage with new target dimension
*
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/criteo/vips/VipsImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.criteo.vips;

import com.criteo.vips.enums.*;
import com.criteo.vips.options.ThumbnailOptions;

import java.awt.*;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -152,14 +153,27 @@ public void thumbnailImage(Dimension dimension, boolean scale) throws VipsExcept
thumbnailImageNative(dimension.width, dimension.height, scale);
}

public void thumbnailImage(Dimension dimension, ThumbnailOptions options) throws VipsException {
thumbnailImageWithOptionsNative(dimension.width, dimension.height, options);
}

public void thumbnailImage(int width, int height, boolean scale) throws VipsException {
thumbnailImageNative(width, height, scale);
}

@Override
public void thumbnailImage(int width, int height, ThumbnailOptions options) throws VipsException {
thumbnailImageWithOptionsNative(width, height, options);
}

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

public static VipsImage thumbnail(String filename, Dimension dimension, ThumbnailOptions options) throws VipsException {
return thumbnailWithOptionsNative(filename, dimension.width, dimension.height, options);
}

/**
* Make a thumbnail from a file with new target dimension
*
Expand All @@ -173,6 +187,10 @@ public static VipsImage thumbnail(String filename, int width, int height, boolea
return thumbnailNative(filename, width, height, scale);
}

public static VipsImage thumbnail(String filename, int width, int height, ThumbnailOptions options) throws VipsException {
return thumbnailWithOptionsNative(filename, width, height, options);
}

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

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

private native void thumbnailImageWithOptionsNative(int width, int height, ThumbnailOptions options) throws VipsException;

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

private static native VipsImage thumbnailWithOptionsNative(String filename, int width, int height, ThumbnailOptions options) throws VipsException;

public void resize(double hscale, double vscale, VipsKernel kernel) throws VipsException {
resizeNative(hscale, vscale, kernel.getValue());
}
Expand Down
148 changes: 148 additions & 0 deletions src/main/java/com/criteo/vips/options/ThumbnailOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
Copyright (c) 2021 Criteo

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.
*/

package com.criteo.vips.options;

import com.criteo.vips.enums.VipsIntent;
import com.criteo.vips.enums.VipsInteresting;
import com.criteo.vips.enums.VipsSize;

public class ThumbnailOptions {

private int size = 1;
private boolean noRotate;
private int crop = -1;
private boolean linear;
private String importProfile;
private String exportProfile;
private int intent = -1;

public VipsSize getSize() {
if (size != -1) {
return VipsSize.valueOf(size);
} else {
return null;
}
}

public void setSize(VipsSize size) {
if (size != null) {
this.size = size.getValue();
} else {
this.size = -1;
}
}

public ThumbnailOptions size(VipsSize size) {
setSize(size);
return this;
}

public boolean isNoRotate() {
return noRotate;
}

public void setNoRotate(boolean noRotate) {
this.noRotate = noRotate;
}

public ThumbnailOptions noRotate(boolean noRotate) {
setNoRotate(noRotate);
return this;
}

public VipsInteresting getCrop() {
if (crop != -1) {
return VipsInteresting.valueOf(crop);
} else {
return null;
}
}

public void setCrop(VipsInteresting crop) {
if (crop != null) {
this.crop = crop.getValue();
} else {
this.crop = -1;
}
}

public ThumbnailOptions crop(VipsInteresting crop) {
setCrop(crop);
return this;
}

public boolean isLinear() {
return linear;
}

public void setLinear(boolean linear) {
this.linear = linear;
}

public ThumbnailOptions linear(boolean linear) {
setLinear(linear);
return this;
}

public String getImportProfile() {
return importProfile;
}

public void setImportProfile(String importProfile) {
this.importProfile = importProfile;
}

public ThumbnailOptions importProfile(String importProfile) {
setImportProfile(importProfile);
return this;
}

public String getExportProfile() {
return exportProfile;
}

public void setExportProfile(String exportProfile) {
this.exportProfile = exportProfile;
}

public ThumbnailOptions exportProfile(String exportProfile) {
setExportProfile(exportProfile);
return this;
}

public VipsIntent getIntent() {
if (intent != -1) {
return VipsIntent.valueOf(intent);
} else {
return null;
}
}

public void setIntent(VipsIntent intent) {
if (intent != null) {
this.intent = intent.getValue();
} else {
this.intent = -1;
}
}

public ThumbnailOptions intent(VipsIntent intent) {
setIntent(intent);
return this;
}

}
22 changes: 22 additions & 0 deletions src/test/java/com/criteo/vips/VipsImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.criteo.vips;

import com.criteo.vips.enums.*;
import com.criteo.vips.options.ThumbnailOptions;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -341,6 +343,26 @@ public void TestShouldRenderThumbnailImageWithExactDimension() throws IOExceptio
}
}

@Test
public void TestShouldRenderThumbnailImageWithExactDimensionUsingOptions() throws IOException, VipsException {
ByteBuffer buffer = VipsTestUtils.getDirectByteBuffer("in_vips.jpg");
try (VipsImage img = new VipsImage(buffer, buffer.capacity())) {
img.thumbnailImage(new Dimension(800, 800), new ThumbnailOptions().size(VipsSize.Force));
assertEquals(800, img.getWidth());
assertEquals(800, img.getHeight());
}
}

@Test
public void TestShouldRenderThumbnailImageWithExactDimensionUsingCrop() throws IOException, VipsException {
ByteBuffer buffer = VipsTestUtils.getDirectByteBuffer("in_vips.jpg");
try (VipsImage img = new VipsImage(buffer, buffer.capacity())) {
img.thumbnailImage(new Dimension(800, 800), new ThumbnailOptions().crop(VipsInteresting.Attention));
assertEquals(800, img.getWidth());
assertEquals(800, img.getHeight());
}
}

@Test
public void TestShouldRenderThumbnailWithExactDimension() throws IOException, VipsException {
String filename = VipsTestUtils.getRessourcePath("in_vips.jpg");
Expand Down