Tesseract-android-tools  1.0
 Todo Clases Namespaces Archivos Funciones Variables
Pix.java
Ir a la documentación de este archivo.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00026 package com.googlecode.leptonica.android;
00027 
00028 import android.graphics.Rect;
00029 
00034 public class Pix {
00035         static {
00036                 System.loadLibrary("lept");
00037         }
00038 
00040         public static final int INDEX_W = 0;
00041 
00043         public static final int INDEX_H = 1;
00044 
00046         public static final int INDEX_D = 2;
00047 
00049         final int mNativePix;
00050 
00051         private boolean mRecycled;
00052 
00061         public Pix(int nativePix) {
00062                 mNativePix = nativePix;
00063                 mRecycled = false;
00064         }
00065 
00066         public Pix(int width, int height, int depth) {
00067                 if (width <= 0 || height <= 0) {
00068                         throw new IllegalArgumentException(
00069                                         "Pix width and height must be > 0");
00070                 } else if (depth != 1 && depth != 2 && depth != 4 && depth != 8
00071                                 && depth != 16 && depth != 24 && depth != 32) {
00072                         throw new IllegalArgumentException(
00073                                         "Depth must be one of 1, 2, 4, 8, 16, or 32");
00074                 }
00075 
00076                 mNativePix = nativeCreatePix(width, height, depth);
00077                 mRecycled = false;
00078         }
00079 
00087         public int getNativePix() {
00088                 return mNativePix;
00089         }
00090 
00097         public byte[] getData() {
00098                 int size = nativeGetDataSize(mNativePix);
00099 
00100                 byte[] buffer = new byte[size];
00101 
00102                 if (!nativeGetData(mNativePix, buffer)) {
00103                         throw new RuntimeException("native getData failed");
00104                 }
00105 
00106                 return buffer;
00107         }
00108 
00116         public int[] getDimensions() {
00117                 int[] dimensions = new int[4];
00118 
00119                 if (getDimensions(dimensions)) {
00120                         return dimensions;
00121                 }
00122 
00123                 return null;
00124         }
00125 
00134         public boolean getDimensions(int[] dimensions) {
00135                 return nativeGetDimensions(mNativePix, dimensions);
00136         }
00137 
00145         @Override
00146         public Pix clone() {
00147                 int nativePix = nativeClone(mNativePix);
00148 
00149                 if (nativePix == 0) {
00150                         throw new OutOfMemoryError();
00151                 }
00152 
00153                 return new Pix(nativePix);
00154         }
00155 
00162         public Pix copy() {
00163                 int nativePix = nativeCopy(mNativePix);
00164 
00165                 if (nativePix == 0) {
00166                         throw new OutOfMemoryError();
00167                 }
00168 
00169                 return new Pix(nativePix);
00170         }
00171 
00177         public boolean invert() {
00178                 return nativeInvert(mNativePix);
00179         }
00180 
00185         public void recycle() {
00186                 if (!mRecycled) {
00187                         nativeDestroy(mNativePix);
00188 
00189                         mRecycled = true;
00190                 }
00191         }
00192 
00193         @Override
00194         protected void finalize() throws Throwable {
00195                 recycle();
00196 
00197                 super.finalize();
00198         }
00199 
00213         public static Pix createFromPix(byte[] pixData, int width, int height,
00214                         int depth) {
00215                 int nativePix = nativeCreateFromData(pixData, width, height, depth);
00216 
00217                 if (nativePix == 0) {
00218                         throw new OutOfMemoryError();
00219                 }
00220 
00221                 return new Pix(nativePix);
00222         }
00223 
00229         public Rect getRect() {
00230                 int w = getWidth();
00231                 int h = getHeight();
00232 
00233                 return new Rect(0, 0, w, h);
00234         }
00235 
00241         public int getWidth() {
00242                 return nativeGetWidth(mNativePix);
00243         }
00244 
00250         public int getHeight() {
00251                 return nativeGetHeight(mNativePix);
00252         }
00253 
00259         public int getDepth() {
00260                 return nativeGetDepth(mNativePix);
00261         }
00262 
00276         public int getPixel(int x, int y) {
00277                 if (x < 0 || x >= getWidth()) {
00278                         throw new IllegalArgumentException(
00279                                         "Supplied x coordinate exceeds image bounds");
00280                 } else if (y < 0 || y >= getHeight()) {
00281                         throw new IllegalArgumentException(
00282                                         "Supplied x coordinate exceeds image bounds");
00283                 }
00284 
00285                 return nativeGetPixel(mNativePix, x, y);
00286         }
00287 
00301         public void setPixel(int x, int y, int color) {
00302                 if (x < 0 || x >= getWidth()) {
00303                         throw new IllegalArgumentException(
00304                                         "Supplied x coordinate exceeds image bounds");
00305                 } else if (y < 0 || y >= getHeight()) {
00306                         throw new IllegalArgumentException(
00307                                         "Supplied x coordinate exceeds image bounds");
00308                 }
00309 
00310                 nativeSetPixel(mNativePix, x, y, color);
00311         }
00312 
00313         // ***************
00314         // * NATIVE CODE *
00315         // ***************
00316 
00317         private static native int nativeCreatePix(int w, int h, int d);
00318 
00319         private static native int nativeCreateFromData(byte[] data, int w, int h,
00320                         int d);
00321 
00322         private static native boolean nativeGetData(int nativePix, byte[] data);
00323 
00324         private static native int nativeGetDataSize(int nativePix);
00325 
00326         private static native int nativeClone(int nativePix);
00327 
00328         private static native int nativeCopy(int nativePix);
00329 
00330         private static native boolean nativeInvert(int nativePix);
00331 
00332         private static native void nativeDestroy(int nativePix);
00333 
00334         private static native boolean nativeGetDimensions(int nativePix,
00335                         int[] dimensions);
00336 
00337         private static native int nativeGetWidth(int nativePix);
00338 
00339         private static native int nativeGetHeight(int nativePix);
00340 
00341         private static native int nativeGetDepth(int nativePix);
00342 
00343         private static native int nativeGetPixel(int nativePix, int x, int y);
00344 
00345         private static native void nativeSetPixel(int nativePix, int x, int y,
00346                         int color);
00347 }
 Todo Clases Namespaces Archivos Funciones Variables