Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
Update tests. Addresses #25 and alanv/tesseract-android-tools#39
Browse files Browse the repository at this point in the history
  • Loading branch information
rmtheis committed Jun 1, 2015
1 parent 4693614 commit 8fba15b
Show file tree
Hide file tree
Showing 6 changed files with 533 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,84 @@

import junit.framework.TestCase;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Color;
import android.test.suitebuilder.annotation.SmallTest;

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

public class PixTest extends TestCase {
@SmallTest
public void testGetData() throws IOException {
File file = File.createTempFile("testGetData", ".jpg");
FileOutputStream fileStream = new FileOutputStream(file);
Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.RGB_565);
bmp.compress(CompressFormat.JPEG, 85, fileStream);
Pix pix = ReadFile.readFile(file);

byte[] pixData = pix.getData();
assertNotNull(pixData);

bmp.recycle();
pix.recycle();
}

@SmallTest
public void testGetDimensions() {
int width = 640;
int height = 480;
int depth = 32;
Pix pix = new Pix(width, height, depth);

int[] dimens = pix.getDimensions();

assertEquals("Incorrect width value found.", width, dimens[Pix.INDEX_W]);
assertEquals("Incorrect height value found.", height,
dimens[Pix.INDEX_H]);
assertEquals("Incorrect bit-depth value found.", depth,
dimens[Pix.INDEX_D]);
}

@SmallTest
public void testPixClone() {
Pix pix = new Pix(640, 480, 32);
Pix pixCopy = pix.clone();

// The clone should not have the same native pointer.
assertNotSame(pix.getNativePix(), pixCopy.getNativePix());

// The clone should share the same backing data.
pix.setPixel(0, 0, Color.RED);
assertEquals(Color.RED, pixCopy.getPixel(0, 0));

// Finally, we should be able to recycle both Pix.
pix.recycle();
pixCopy.recycle();
}

@SmallTest
public void testPixCreate() {
testPixCreate(1, 1, 1);
testPixCreate(640, 480, 32);
}

private void testPixCreate(int w, int h, int d) {
Pix pix = new Pix(w, h, d);

// Make sure the dimensions were set correctly.
assertEquals(w, pix.getWidth());
assertEquals(h, pix.getHeight());
assertEquals(d, pix.getDepth());

// Make sure we can recycle the Pix.
pix.recycle();
}

@SmallTest
public void testPixPixelOps() {
Pix pix = new Pix(640, 480, 32);

// Set various pixel colors.
pix.setPixel(0, 0, Color.RED);
pix.setPixel(1, 0, Color.BLUE);
Expand All @@ -65,42 +113,7 @@ public void testPixPixelOps() {
assertEquals(Color.GREEN, pix.getPixel(2, 0));
assertEquals(Color.BLACK, pix.getPixel(3, 0));
assertEquals(Color.WHITE, pix.getPixel(4, 0));

pix.recycle();
}

@SmallTest
public void testPixClone() {
Pix pix = new Pix(640, 480, 32);
Pix pixCopy = pix.clone();

// The clone should not have the same native pointer.
assertNotSame(pix.getNativePix(), pixCopy.getNativePix());

// The clone should share the same backing data.
pix.setPixel(0, 0, Color.RED);
assertEquals(Color.RED, pixCopy.getPixel(0, 0));

// Finally, we should be able to recycle both Pix.
pix.recycle();
pixCopy.recycle();
}

@SmallTest
public void testGetData() throws IOException {
File file = File.createTempFile("testGetData", "jpg");
FileOutputStream fileStream = new FileOutputStream(file);
Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.RGB_565);
bmp.compress(CompressFormat.JPEG, 85, fileStream);
Pix pix = ReadFile.readFile(file);

byte[] pixData = pix.getData();
Pix pixFromBytes = Pix.createFromPix(pixData, 640, 480, 16);

assertNotNull(pixFromBytes);

fileStream.close();
bmp.recycle();
pix.recycle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,95 +16,102 @@

package com.googlecode.leptonica.android.test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import junit.framework.TestCase;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;

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

import junit.framework.TestCase;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* @author [email protected] (Your Name Here)
* @author [email protected] (Alan Viverette)
*/
public class ReadFileTest extends TestCase {

private static final String TAG = ReadFileTest.class.getSimpleName();

@SmallTest
public void testReadBitmap() {
testReadBitmap(1, 1, Bitmap.Config.ARGB_8888);
testReadBitmap(640, 480, Bitmap.Config.ARGB_8888);
}

private void testReadBitmap(int width, int height, Bitmap.Config format) {
Bitmap bmp = Bitmap.createBitmap(width, height, format);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();

if (width > 1 && height > 1) {
// Paint the left half white
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawRect(new Rect(0, 0, width / 2 , height - 1), paint);

// Paint the right half black
paint.setColor(Color.BLACK);
paint.setStyle(Style.FILL);
canvas.drawRect(new Rect(width / 2, 0, width - 1, height - 1), paint);
}

Bitmap bmp = TestUtils.createTestBitmap(640, 480, format);
Pix pix = ReadFile.readBitmap(bmp);

assertEquals(bmp.getWidth(), pix.getWidth());
assertEquals(bmp.getHeight(), pix.getHeight());

if (width > 1 && height > 1) {
// Make sure the colors were preserved.
assertEquals(Color.WHITE, pix.getPixel(0, 0));
assertEquals(Color.BLACK, pix.getPixel(width - 1, height - 1));
}
float match = TestUtils.compareImages(pix, bmp);
Log.d(TAG, "match=" + match);
assertTrue("Images do not match.", (match >= 0.99f));

bmp.recycle();
pix.recycle();
}

@SmallTest
public void testReadFile() throws IOException {
File file = File.createTempFile("testReadFile", "jpg");
public void testReadFile_bmp() throws IOException {
File file = File.createTempFile("testReadFile", ".bmp");
FileOutputStream fileStream = new FileOutputStream(file);
Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.RGB_565);
Bitmap bmp = TestUtils.createTestBitmap(640, 480, Bitmap.Config.ARGB_8888);
bmp.compress(CompressFormat.PNG, 100, fileStream);
Pix pix = ReadFile.readFile(file);

assertEquals(bmp.getWidth(), pix.getWidth());
assertEquals(bmp.getHeight(), pix.getHeight());

float match = TestUtils.compareImages(pix, bmp);
Log.d(TAG, "match=" + match);
assertTrue("Images do not match.", (match >= 0.99f));

fileStream.close();
bmp.recycle();
pix.recycle();
}

@SmallTest
public void testReadFile_jpg() throws IOException {
File file = File.createTempFile("testReadFile", ".jpg");
FileOutputStream fileStream = new FileOutputStream(file);
Bitmap bmp = TestUtils.createTestBitmap(640, 480, Bitmap.Config.ARGB_8888);
bmp.compress(CompressFormat.JPEG, 85, fileStream);
Pix pix = ReadFile.readFile(file);

assertEquals(bmp.getWidth(), pix.getWidth());
assertEquals(bmp.getHeight(), pix.getHeight());

float match = TestUtils.compareImages(pix, bmp);
Log.d(TAG, "match=" + match);
assertTrue("Images do not match. match=" + match, (match >= 0.99f));

fileStream.close();
bmp.recycle();
pix.recycle();
}

@SmallTest
public void testReadMem() throws IOException {
public void testReadMem_jpg() throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.RGB_565);
Bitmap bmp = TestUtils.createTestBitmap(640, 480, Bitmap.Config.ARGB_8888);
bmp.compress(CompressFormat.JPEG, 85, byteStream);
byte[] encodedData = byteStream.toByteArray();
Pix pix = ReadFile.readMem(encodedData);

assertEquals(bmp.getWidth(), pix.getWidth());
assertEquals(bmp.getHeight(), pix.getHeight());

// TODO(alanv): Need some way to test content, ex. Pix.getPixel(int, int)
float match = TestUtils.compareImages(pix, bmp);
Log.d(TAG, "match=" + match);
assertTrue("Images do not match. match=" + match, (match >= 0.99f));

byteStream.close();
bmp.recycle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void testFindSkew() {
testFindSkew(SENTENCE, 640, 480, 15.0f);
}

private void testFindSkew(String text, int width, int height, float requestedSkew) {
private void testFindSkew(String text, int width, int height, float skew) {
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
Canvas canvas = new Canvas(bmp);
Expand All @@ -53,14 +53,16 @@ private void testFindSkew(String text, int width, int height, float requestedSke
paint.setTextSize(32.0f);

canvas.drawColor(Color.WHITE);
canvas.rotate(requestedSkew, width / 2, height / 2);
canvas.rotate(skew, width / 2, height / 2);
canvas.drawText(SENTENCE, width / 2, height / 2 , paint);

Pix pixs = ReadFile.readBitmap(bmp);
Pix pixd = GrayQuant.pixThresholdToBinary(pixs, 1);
pixs.recycle();
float measuredSkew = -Skew.findSkew(pixd);
pixd.recycle();

boolean isInRange = requestedSkew - 1 < measuredSkew && measuredSkew < requestedSkew + 1;
boolean isInRange = skew - 1 < measuredSkew && measuredSkew < skew + 1;
assertTrue("Skew has incorrect value.", isInRange);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
package com.googlecode.leptonica.android.test;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;

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

/**
* Utility methods for running Leptonica unit tests.
Expand All @@ -29,12 +37,64 @@ public static float compareBitmaps(Bitmap a, Bitmap b) {

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)) {
if (a.getPixel(x, y) == b.getPixel(x, y)) {
found++;
}
}
}

return found / (float)(a.getWidth() * a.getHeight());
}

public static float compareImages(Pix 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) == b.getPixel(x, y)) {
found++;
}
}
}

return found / (float)(a.getWidth() * a.getHeight());
}

public static float comparePix(Pix a, Pix 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) == b.getPixel(x, y)) {
found++;
}
}
}

return found / (float)(a.getWidth() * a.getHeight());
}

public static Bitmap createTestBitmap(int width, int height, Bitmap.Config format) {
Bitmap bmp = Bitmap.createBitmap(width, height, format);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();

if (width > 1 && height > 1) {
// Paint the left half white
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawRect(new Rect(0, 0, width / 2 , height - 1), paint);

// Paint the right half black
paint.setColor(Color.BLACK);
paint.setStyle(Style.FILL);
canvas.drawRect(new Rect(width / 2, 0, width - 1, height - 1), paint);
}
return bmp;
}

public static Pix createTestPix(int width, int height) {
Bitmap bmp = TestUtils.createTestBitmap(width, height, Bitmap.Config.ARGB_8888);
return ReadFile.readBitmap(bmp);
}
}
Loading

0 comments on commit 8fba15b

Please sign in to comment.