00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00026 package com.googlecode.leptonica.android;
00027
00028 import android.graphics.Rect;
00029
00030 import java.io.File;
00031 import java.util.ArrayList;
00032 import java.util.Iterator;
00033
00039 public class Pixa implements Iterable<Pix> {
00040 static {
00041 System.loadLibrary("lept");
00042 }
00043
00048 final int mNativePixa;
00049
00051 final int mWidth;
00052
00054 final int mHeight;
00055
00056 private boolean mRecycled;
00057
00066 public static Pixa createPixa(int size) {
00067 return createPixa(size, 0, 0);
00068 }
00069
00085 public static Pixa createPixa(int size, int width, int height) {
00086 int nativePixa = nativeCreate(size);
00087
00088 if (nativePixa == 0) {
00089 throw new OutOfMemoryError();
00090 }
00091
00092 return new Pixa(nativePixa, width, height);
00093 }
00094
00105 public Pixa(int nativePixa, int width, int height) {
00106 mNativePixa = nativePixa;
00107 mWidth = width;
00108 mHeight = height;
00109 mRecycled = false;
00110 }
00111
00118 public int getNativePixa() {
00119 return mNativePixa;
00120 }
00121
00128 public Pixa copy() {
00129 int nativePixa = nativeCopy(mNativePixa);
00130
00131 if (nativePixa == 0) {
00132 throw new OutOfMemoryError();
00133 }
00134
00135 return new Pixa(nativePixa, mWidth, mHeight);
00136 }
00137
00150 public Pixa sort(int field, int order) {
00151 int nativePixa = nativeSort(mNativePixa, field, order);
00152
00153 if (nativePixa == 0) {
00154 throw new OutOfMemoryError();
00155 }
00156
00157 return new Pixa(nativePixa, mWidth, mHeight);
00158 }
00159
00165 public int size() {
00166 return nativeGetCount(mNativePixa);
00167 }
00168
00177 public synchronized void recycle() {
00178 if (!mRecycled) {
00179 nativeDestroy(mNativePixa);
00180
00181 mRecycled = true;
00182 }
00183 }
00184
00185 @Override
00186 protected void finalize() throws Throwable {
00187 recycle();
00188
00189 super.finalize();
00190 }
00191
00198 public boolean join(Pixa otherPixa) {
00199 return nativeJoin(mNativePixa, otherPixa.mNativePixa);
00200 }
00201
00211 public void addPix(Pix pix, int mode) {
00212 nativeAddPix(mNativePixa, pix.mNativePix, mode);
00213 }
00214
00224 public void addBox(Box box, int mode) {
00225 nativeAddBox(mNativePixa, box.mNativeBox, mode);
00226 }
00227
00239 public void add(Pix pix, Box box, int mode) {
00240 nativeAdd(mNativePixa, pix.mNativePix, box.mNativeBox, mode);
00241 }
00242
00251 public Box getBox(int index) {
00252 int nativeBox = nativeGetBox(mNativePixa, index);
00253
00254 if (nativeBox == 0) {
00255 return null;
00256 }
00257
00258 return new Box(nativeBox);
00259 }
00260
00269 public Pix getPix(int index) {
00270 int nativePix = nativeGetPix(mNativePixa, index);
00271
00272 if (nativePix == 0) {
00273 return null;
00274 }
00275
00276 return new Pix(nativePix);
00277 }
00278
00286 public int getWidth() {
00287 return mWidth;
00288 }
00289
00297 public int getHeight() {
00298 return mHeight;
00299 }
00300
00307 public Rect getRect() {
00308 return new Rect(0, 0, mWidth, mHeight);
00309 }
00310
00318 public Rect getBoxRect(int index) {
00319 int[] dimensions = getBoxGeometry(index);
00320
00321 if (dimensions == null) {
00322 return null;
00323 }
00324
00325 int x = dimensions[Box.INDEX_X];
00326 int y = dimensions[Box.INDEX_Y];
00327 int w = dimensions[Box.INDEX_W];
00328 int h = dimensions[Box.INDEX_H];
00329
00330 Rect bound = new Rect(x, y, x + w, y + h);
00331
00332 return bound;
00333 }
00334
00343 public int[] getBoxGeometry(int index) {
00344 int[] dimensions = new int[4];
00345
00346 if (getBoxGeometry(index, dimensions)) {
00347 return dimensions;
00348 }
00349
00350 return null;
00351 }
00352
00364 public boolean getBoxGeometry(int index, int[] dimensions) {
00365 return nativeGetBoxGeometry(mNativePixa, index, dimensions);
00366 }
00367
00373 public ArrayList<Rect> getBoxRects() {
00374 final int pixaCount = nativeGetCount(mNativePixa);
00375 final int[] buffer = new int[4];
00376 final ArrayList<Rect> rects = new ArrayList<Rect>(pixaCount);
00377
00378 for (int i = 0; i < pixaCount; i++) {
00379 getBoxGeometry(i, buffer);
00380
00381 final int x = buffer[Box.INDEX_X];
00382 final int y = buffer[Box.INDEX_Y];
00383 final Rect bound = new Rect(x, y, x + buffer[Box.INDEX_W], y
00384 + buffer[Box.INDEX_H]);
00385
00386 rects.add(bound);
00387 }
00388
00389 return rects;
00390 }
00391
00404 public void replacePix(int index, Pix pix, Box box) {
00405 nativeReplacePix(mNativePixa, index, pix.mNativePix, box.mNativeBox);
00406 }
00407
00418 public void mergeAndReplacePix(int indexA, int indexB) {
00419 nativeMergeAndReplacePix(mNativePixa, indexA, indexB);
00420 }
00421
00430 public boolean writeToFileRandomCmap(File file) {
00431 return nativeWriteToFileRandomCmap(mNativePixa, file.getAbsolutePath(),
00432 mWidth, mHeight);
00433 }
00434
00435 @Override
00436 public Iterator<Pix> iterator() {
00437 return new PixIterator();
00438 }
00439
00440 private class PixIterator implements Iterator<Pix> {
00441 private int mIndex;
00442
00443 private PixIterator() {
00444 mIndex = 0;
00445 }
00446
00447 @Override
00448 public boolean hasNext() {
00449 final int size = size();
00450 return (size > 0 && mIndex < size);
00451 }
00452
00453 @Override
00454 public Pix next() {
00455 return getPix(mIndex++);
00456 }
00457
00458 @Override
00459 public void remove() {
00460 throw new UnsupportedOperationException();
00461 }
00462 }
00463
00464
00465
00466
00467
00468 private static native int nativeCreate(int size);
00469
00470 private static native int nativeCopy(int nativePixa);
00471
00472 private static native int nativeSort(int nativePixa, int field, int order);
00473
00474 private static native boolean nativeJoin(int nativePixa, int otherPixa);
00475
00476 private static native int nativeGetCount(int nativePixa);
00477
00478 private static native void nativeDestroy(int nativePixa);
00479
00480 private static native void nativeAddPix(int nativePixa, int nativePix,
00481 int mode);
00482
00483 private static native void nativeAddBox(int nativePixa, int nativeBox,
00484 int mode);
00485
00486 private static native void nativeAdd(int nativePixa, int nativePix,
00487 int nativeBox, int mode);
00488
00489 private static native boolean nativeWriteToFileRandomCmap(int nativePixa,
00490 String fileName, int width, int height);
00491
00492 private static native void nativeReplacePix(int nativePixa, int index,
00493 int nativePix, int nativeBox);
00494
00495 private static native void nativeMergeAndReplacePix(int nativePixa,
00496 int indexA, int indexB);
00497
00498 private static native int nativeGetBox(int nativePix, int index);
00499
00500 private static native int nativeGetPix(int nativePix, int index);
00501
00502 private static native boolean nativeGetBoxGeometry(int nativePixa,
00503 int index, int[] dimensions);
00504 }