Tesseract-android-tools  1.0
 Todo Clases Namespaces Archivos Funciones Variables
Scale.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 
00025 package com.googlecode.leptonica.android;
00026 
00032 public class Scale {
00033         static {
00034                 System.loadLibrary("lept");
00035         }
00036 
00037         public enum ScaleType {
00042                 FILL,
00043 
00049                 FIT,
00050 
00056                 FIT_SHRINK,
00057         }
00058 
00070         public static Pix scaleToSize(Pix pixs, int width, int height,
00071                         ScaleType type) {
00072                 if (pixs == null)
00073                         throw new IllegalArgumentException("Source pix must be non-null");
00074 
00075                 int pixWidth = pixs.getWidth();
00076                 int pixHeight = pixs.getHeight();
00077 
00078                 float scaleX = width / (float) pixWidth;
00079                 float scaleY = height / (float) pixHeight;
00080 
00081                 switch (type) {
00082                 case FILL:
00083                         // Retains default scaleX and scaleY values
00084                         break;
00085                 case FIT:
00086                         scaleX = Math.min(scaleX, scaleY);
00087                         scaleY = scaleX;
00088                         break;
00089                 case FIT_SHRINK:
00090                         scaleX = Math.min(1.0f, Math.min(scaleX, scaleY));
00091                         scaleY = scaleX;
00092                         break;
00093                 }
00094 
00095                 return scale(pixs, scaleX, scaleY);
00096         }
00097 
00108         public static Pix scale(Pix pixs, float scale) {
00109                 return scale(pixs, scale, scale);
00110         }
00111 
00124         public static Pix scale(Pix pixs, float scaleX, float scaleY) {
00125                 if (pixs == null)
00126                         throw new IllegalArgumentException("Source pix must be non-null");
00127                 if (scaleX <= 0.0f)
00128                         throw new IllegalArgumentException(
00129                                         "X scaling factor must be positive");
00130                 if (scaleY <= 0.0f)
00131                         throw new IllegalArgumentException(
00132                                         "Y scaling factor must be positive");
00133 
00134                 int nativePix = nativeScale(pixs.mNativePix, scaleX, scaleY);
00135 
00136                 if (nativePix == 0)
00137                         throw new RuntimeException("Failed to natively scale pix");
00138 
00139                 return new Pix(nativePix);
00140         }
00141 
00142         // ***************
00143         // * NATIVE CODE *
00144         // ***************
00145 
00146         private static native int nativeScale(int nativePix, float scaleX,
00147                         float scaleY);
00148 }
 Todo Clases Namespaces Archivos Funciones Variables