Skip to content

Commit

Permalink
Deprecate resize method in favor of thumbnailImage
Browse files Browse the repository at this point in the history
Future goal is to add the vips_resize binding in the JVips API.

But we need to make some changes to avoid name conflicts because:

* resize(width, height, scale) use the vips_thumbnail_image binding
* resize(hscale, vscale, kernel) will use the vips_resize binding

To avoid this conflict and follow the JVips naming convention (#51), I propose to
deprecate the resize method in favor of a new thumbnailImage method.

See #72
  • Loading branch information
anthonyroussel authored and dbouron committed Mar 8, 2021
1 parent aac2acb commit beceafd
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/main/c/VipsImage.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ Java_com_criteo_vips_VipsImage_colourspaceNative__II(JNIEnv *env, jobject obj, i
}

JNIEXPORT void JNICALL
Java_com_criteo_vips_VipsImage_resizeNative(JNIEnv *env, jobject obj, jint width, jint height, jboolean scale)
Java_com_criteo_vips_VipsImage_thumbnailImageNative(JNIEnv *env, jobject obj, jint width, jint height, jboolean scale)
{
VipsImage *im = (VipsImage *) (*env)->GetLongField(env, obj, handle_fid);
VipsImage *out = NULL;
VipsSize vipsSize = scale ? VIPS_SIZE_FORCE : VIPS_SIZE_BOTH;

if (vips_thumbnail_image(im, &out, width, "height", height, "size", vipsSize, NULL))
{
throwVipsException(env, "Unable to resize image");
throwVipsException(env, "Unable to make thumbnail image");
return;
}
(*env)->SetLongField(env, obj, handle_fid, (jlong) out);
Expand Down
4 changes: 2 additions & 2 deletions src/main/c/VipsImage.h

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

24 changes: 23 additions & 1 deletion src/main/java/com/criteo/vips/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,34 @@ public interface Image extends AutoCloseable {
void colourspace(VipsInterpretation space, VipsInterpretation source_space) throws VipsException;

/**
* Resize this VipsImage with new target dimension
* Make a thumbnail of this VipsImage with new target dimension
*
* @param dimension Target dimension
* @param scale If scale is enabled, force to resize ignoring aspect ratio
* @throws VipsException if error
*/
void thumbnailImage(Dimension dimension, boolean scale) throws VipsException;

/**
* Make a thumbnail of this VipsImage with new target dimension
*
* @param width Target width
* @param height Target height
* @param scale If scale is enabled, force to resize ignoring aspect ratio
* @throws VipsException if error
*/
void thumbnailImage(int width, int height, boolean scale) throws VipsException;

/**
* Make a thumbnail of this VipsImage with new target dimension
*
* @param dimension Target dimension
* @param scale If scale is enabled, force to resize ignoring aspect ratio
* @throws VipsException if error
*
* @deprecated Use {@link #thumbnailImage(Dimension, boolean)} instead.
*/
@Deprecated
void resize(Dimension dimension, boolean scale) throws VipsException;

/**
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/com/criteo/vips/VipsImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,31 @@ public void histFindNdim(int bins) throws VipsException {

private native void histFindNdimNative(int bins) throws VipsException;

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

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

/**
* @deprecated Use {@link #thumbnailImage(Dimension, boolean)} instead.
*/
@Deprecated
public void resize(Dimension dimension, boolean scale) throws VipsException {
resizeNative(dimension.width, dimension.height, scale);
thumbnailImageNative(dimension.width, dimension.height, scale);
}

/**
* @deprecated Use {@link #thumbnailImage(int, int, boolean)} instead.
*/
@Deprecated
public void resize(int width, int height, boolean scale) throws VipsException {
resizeNative(width, height, scale);
thumbnailImageNative(width, height, scale);
}

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

public Max1Result max1() throws VipsException {
Max1Result r = new Max1Result();
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/criteo/vips/VipsImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,22 +310,22 @@ public void TestShouldHandleTransparentPixel() throws IOException, VipsException
}

@Test
public void TestShouldResizeAndKeepAspectRatio() throws IOException, VipsException {
public void TestShouldRenderThumbnailAndKeepAspectRatio() 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();
img.resize(new Dimension(800, 800), false);
img.thumbnailImage(new Dimension(800, 800), false);
assertEquals(800, img.getWidth());
assertEquals(450, img.getHeight());
assertEquals(aspectRatio, (double) img.getWidth() / (double) img.getHeight(), Delta);
}
}

@Test
public void TestShouldResizeWithExactDimension() throws IOException, VipsException {
public void TestShouldRenderThumbnailWithExactDimension() throws IOException, VipsException {
ByteBuffer buffer = VipsTestUtils.getDirectByteBuffer("in_vips.jpg");
try (VipsImage img = new VipsImage(buffer, buffer.capacity())) {
img.resize(new Dimension(800, 800), true);
img.thumbnailImage(new Dimension(800, 800), true);
assertEquals(800, img.getWidth());
assertEquals(800, img.getHeight());
}
Expand Down Expand Up @@ -542,7 +542,7 @@ public void TestSimplePipelineShouldNotThrow(String filename, VipsImageFormat vi
ByteBuffer buffer = VipsTestUtils.getDirectByteBuffer(filename);
PixelPacket pixel = new PixelPacket(5.0, 255.0, 25.0);
try (VipsImage img = new VipsImage(buffer, buffer.capacity())) {
img.resize(new Dimension(400, 400), false);
img.thumbnailImage(new Dimension(400, 400), false);
int width = img.getWidth();
int height = img.getHeight();
img.crop(new Rectangle(10, 10, width - 10, height - 10));
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/com/criteo/vips/benchmark/SimpleBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.concurrent.TimeUnit;

public class SimpleBenchmark {
private static Dimension resizeTarget = new Dimension(512, 512);
private static Dimension thumbnailTarget = new Dimension(512, 512);
private static Rectangle cropTarget = new Rectangle(128, 128, 128, 128);
private static Dimension padTarget = new Dimension(256, 256);
private static PixelPacket pixelPacket = new PixelPacket(255.0, 255.0, 255.0);
Expand Down Expand Up @@ -80,19 +80,19 @@ public void initialize() throws IOException {
}

@Benchmark
public void ResizeCropPadJpeg(BenchmarkState state, Blackhole bh) {
ResizeCropPad(state.jpegContent, VipsImageFormat.JPG);
public void ThumbnailCropPadJpeg(BenchmarkState state, Blackhole bh) {
ThumbnailCropPad(state.jpegContent, VipsImageFormat.JPG);
}

@Benchmark
public void ResizeCropPadPng(BenchmarkState state, Blackhole bh) {
ResizeCropPad(state.pngContent, VipsImageFormat.PNG);
public void ThumbnailCropPadPng(BenchmarkState state, Blackhole bh) {
ThumbnailCropPad(state.pngContent, VipsImageFormat.PNG);
}

private void ResizeCropPad(byte[] content, VipsImageFormat format) {
private void ThumbnailCropPad(byte[] content, VipsImageFormat format) {
VipsImage img = new VipsImage(content, content.length);

img.resize(resizeTarget, false);
img.thumbnailImage(thumbnailTarget, false);
img.crop(cropTarget);
img.pad(padTarget, pixelPacket, VipsCompassDirection.Centre);
byte[] out = img.writeToArray(format, 80, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void main(String[] args) {
@Override
public Void call() throws Exception {
try (VipsImage image = new VipsImage(contents.clone(), contents.length);) {
image.resize(new Dimension(256, 256), true);
image.thumbnailImage(new Dimension(256, 256), true);
image.pad(new Dimension(512, 512),
new PixelPacket(255, 255, 255),
VipsCompassDirection.Centre);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/criteo/vips/example/SimpleExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void main(String[] args) {
int width = image.getWidth();
int height = image.getHeight();

image.resize(new Dimension(width / 2, height / 2), true);
image.thumbnailImage(new Dimension(width / 2, height / 2), true);
System.out.println(String.format("Image has been correctly resized: (%d,%d) -> (%d,%d)",
width, height, image.getWidth(), image.getHeight()));
contents = image.writeToArray(VipsImageFormat.JPG, false);
Expand Down

0 comments on commit beceafd

Please sign in to comment.