Skip to content

Commit

Permalink
Add resize flag to Rotate.rotate() and implement rotation unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Viverette committed Sep 14, 2012
1 parent 9f536ff commit d988beb
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,68 @@

package com.googlecode.leptonica.android.test;

import com.googlecode.leptonica.android.Pix;
import com.googlecode.leptonica.android.ReadFile;
import com.googlecode.leptonica.android.Rotate;
import com.googlecode.leptonica.android.WriteFile;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.test.suitebuilder.annotation.SmallTest;

import junit.framework.TestCase;

public class RotateTest extends TestCase {
@SmallTest
public void testRotate() {
Bitmap bmp = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();

// Paint the background white
canvas.drawColor(Color.WHITE);

// Paint a black circle in the center
paint.setColor(Color.BLACK);
paint.setStyle(Style.FILL);
canvas.drawCircle(50, 50, 10, paint);

Pix pixs = ReadFile.readBitmap(bmp);
Pix pixd = Rotate.rotate(pixs, 180);
pixs.recycle();

Bitmap rotated = WriteFile.writeBitmap(pixd);
pixd.recycle();

float match = TestUtils.compareBitmaps(bmp, rotated);
bmp.recycle();
rotated.recycle();

assertTrue("Bitmaps match", (match > 0.99f));
}

@SmallTest
public void testRotateResize() {
Bitmap bmp = Bitmap.createBitmap(100, 10, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();

// Paint the background white
canvas.drawColor(Color.BLACK);

// Paint a black circle in the center
paint.setColor(Color.BLACK);
paint.setStyle(Style.FILL);
canvas.drawCircle(50, 50, 10, paint);

Pix pixs = ReadFile.readBitmap(bmp);
Pix pixd = Rotate.rotate(pixs, 180);
pixs.recycle();

assertTrue("Rotated width is 100", (pixd.getWidth() == 100));
pixd.recycle();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2012 Google Inc.
*
* 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.googlecode.leptonica.android.test;

import android.graphics.Bitmap;

/**
* Utility methods for running Leptonica unit tests.
*
* @author [email protected] (Alan Viverette)
*/
public class TestUtils {
public static float compareBitmaps(Bitmap a, Bitmap b) {
int found = 0;

for (int y = 0; y < a.getHeight(); y++) {
for (int x = 0; x < a.getWidth(); x++) {
if (a.getPixel(x, y) == a.getPixel(x, y)) {
found++;
}
}
}

return found / (float)(a.getWidth() * a.getHeight());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ jfloat Java_com_googlecode_leptonica_android_Skew_nativeFindSkew(JNIEnv *env, jc

jint Java_com_googlecode_leptonica_android_Rotate_nativeRotate(JNIEnv *env, jclass clazz,
jint nativePix, jfloat degrees,
jboolean quality) {
jboolean quality, jboolean resize) {
PIX *pixd;
PIX *pixs = (PIX *) nativePix;

Expand All @@ -183,7 +183,9 @@ jint Java_com_googlecode_leptonica_android_Rotate_nativeRotate(JNIEnv *env, jcla
pixd = pixRotateBinaryNice(pixs, radians, L_BRING_IN_WHITE);
} else {
type = quality == JNI_TRUE ? L_ROTATE_AREA_MAP : L_ROTATE_SAMPLING;
pixd = pixRotate(pixs, radians, type, L_BRING_IN_WHITE, 0, 0);
w = (resize == JNI_TRUE) ? w : 0;
h = (resize == JNI_TRUE) ? h : 0;
pixd = pixRotate(pixs, radians, type, L_BRING_IN_WHITE, w, h);
}

return (jint) pixd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public static Pix rotate(Pix pixs, float degrees) {
return rotate(pixs, degrees, false);
}

/**
* Performs rotation with resizing using the default parameters.
*
* @param pixs The source pix.
* @param degrees The number of degrees to rotate; clockwise is positive.
* @param quality Whether to use high-quality rotation.
* @return the rotated source image
*/
public static Pix rotate(Pix pixs, float degrees, boolean quality) {
return rotate(pixs, degrees, quality, true);
}

/**
* Performs basic image rotation about the center.
* <p>
Expand All @@ -61,13 +73,16 @@ public static Pix rotate(Pix pixs, float degrees) {
* @param pixs The source pix.
* @param degrees The number of degrees to rotate; clockwise is positive.
* @param quality Whether to use high-quality rotation.
* @param resize Whether to expand the output so that no pixels are lost.
* <strong>Note:</strong> 1bpp images are always resized when
* quality is {@code true}.
* @return the rotated source image
*/
public static Pix rotate(Pix pixs, float degrees, boolean quality) {
public static Pix rotate(Pix pixs, float degrees, boolean quality, boolean resize) {
if (pixs == null)
throw new IllegalArgumentException("Source pix must be non-null");

int nativePix = nativeRotate(pixs.mNativePix, degrees, quality);
int nativePix = nativeRotate(pixs.mNativePix, degrees, quality, resize);

if (nativePix == 0)
return null;
Expand All @@ -79,5 +94,6 @@ public static Pix rotate(Pix pixs, float degrees, boolean quality) {
// * NATIVE CODE *
// ***************

private static native int nativeRotate(int nativePix, float degrees, boolean quality);
private static native int nativeRotate(int nativePix, float degrees, boolean quality,
boolean resize);
}

0 comments on commit d988beb

Please sign in to comment.