Skip to content

Commit

Permalink
🔃 implement some auto rotate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dbouron committed Jul 22, 2020
1 parent 29111b7 commit 885974d
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 13 deletions.
32 changes: 31 additions & 1 deletion src/main/c/VipsImage.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2019 Criteo
Copyright (c) 2020 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 @@ -553,3 +553,33 @@ JNICALL Java_com_criteo_vips_VipsImage_linearNative(JNIEnv *env, jobject image_o
}
}
}

JNIEXPORT jint
JNICALL Java_com_criteo_vips_VipsImage_getAutorotAngleNative(JNIEnv *env, jobject image_obj)
{
VipsImage *im = (VipsImage *) (*env)->GetLongField(env, image_obj, handle_fid);

return (int) vips_autorot_get_angle(im);
}

JNIEXPORT void
JNICALL Java_com_criteo_vips_VipsImage_autorot(JNIEnv *env, jobject image_obj)
{
VipsImage *im = (VipsImage *) (*env)->GetLongField(env, image_obj, handle_fid);
VipsImage *out = NULL;

if (vips_autorot(im, &out, NULL))
{
throwVipsException(env, "Unable to auto rotate image");
return;
}
(*env)->SetLongField(env, image_obj, handle_fid, (jlong) out);
g_object_unref(im);
}

JNIEXPORT void
JNICALL Java_com_criteo_vips_VipsImage_removeAutorotAngle(JNIEnv *env, jobject image_obj)
{
VipsImage *im = (VipsImage *) (*env)->GetLongField(env, image_obj, handle_fid);
vips_autorot_remove_angle(im);
}
24 changes: 24 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.

22 changes: 18 additions & 4 deletions src/main/java/com/criteo/vips/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.criteo.vips;

import com.criteo.vips.enums.VipsBandFormat;
import com.criteo.vips.enums.VipsCompassDirection;
import com.criteo.vips.enums.VipsImageFormat;
import com.criteo.vips.enums.VipsInterpretation;
import com.criteo.vips.enums.*;

import java.awt.*;
import java.io.Closeable;
Expand Down Expand Up @@ -264,6 +261,23 @@ public interface Image extends Closeable {
*/
int getNbFrame();

/**
* @return Auto rotation angle metadata
*/
VipsAngle getAutorotAngle();

/**
* Apply an auto rotation according to the image EXIF metadata
*
* @throws VipsException
*/
void autorot() throws VipsException;

/**
* Remove the orientation tag on the image
*/
void removeAutorotAngle();

/**
* Release VipsImage reference
*/
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/criteo/vips/VipsImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.criteo.vips;

import com.criteo.vips.enums.VipsBandFormat;
import com.criteo.vips.enums.VipsCompassDirection;
import com.criteo.vips.enums.VipsImageFormat;
import com.criteo.vips.enums.VipsInterpretation;
import com.criteo.vips.enums.*;

import java.awt.*;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -238,5 +235,15 @@ public VipsInterpretation getInterpretation() {

public native int getNbFrame();

public VipsAngle getAutorotAngle() {
return VipsAngle.valueOf(getAutorotAngleNative());
}

private native int getAutorotAngleNative();

public native void autorot() throws VipsException;

public native void removeAutorotAngle();

public native void release();
}
26 changes: 22 additions & 4 deletions src/test/java/com/criteo/vips/VipsImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.criteo.vips;

import com.criteo.vips.enums.VipsBandFormat;
import com.criteo.vips.enums.VipsCompassDirection;
import com.criteo.vips.enums.VipsImageFormat;
import com.criteo.vips.enums.VipsInterpretation;
import com.criteo.vips.enums.*;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -783,4 +780,25 @@ public void TestBlack() throws IOException, VipsException {
}
}
}

@Test
public void TestRemoveAutorotAngle() throws IOException, VipsException {
ByteBuffer buffer = VipsTestUtils.getDirectByteBuffer("exif_rotate_90_cw.jpg");

try (VipsImage img = new VipsImage(buffer, buffer.capacity())) {
assertEquals(VipsAngle.D90, img.getAutorotAngle());
img.removeAutorotAngle();
assertEquals(VipsAngle.D0, img.getAutorotAngle());
}
}

@Test
public void TestExif90CWAutorot() throws IOException, VipsException {
ByteBuffer buffer = VipsTestUtils.getDirectByteBuffer("exif_rotate_90_cw.jpg");

try (VipsImage img = new VipsImage(buffer, buffer.capacity())) {
img.autorot();
assertEquals(WhitePixel, img.getPointPixelPacket(0, 0));
}
}
}
Binary file added src/test/resources/exif_rotate_90_cw.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 885974d

Please sign in to comment.