• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.gallery3d.common;
18 
19 import android.database.Cursor;
20 import android.graphics.RectF;
21 import android.os.ParcelFileDescriptor;
22 import android.util.Log;
23 
24 import java.io.Closeable;
25 import java.io.IOException;
26 
27 public class Utils {
28     private static final String TAG = "Utils";
29 
30     // Throws AssertionError if the input is false.
assertTrue(boolean cond)31     public static void assertTrue(boolean cond) {
32         if (!cond) {
33             throw new AssertionError();
34         }
35     }
36 
37     // Returns the next power of two.
38     // Returns the input if it is already power of 2.
39     // Throws IllegalArgumentException if the input is <= 0 or
40     // the answer overflows.
nextPowerOf2(int n)41     public static int nextPowerOf2(int n) {
42         if (n <= 0 || n > (1 << 30)) throw new IllegalArgumentException("n is invalid: " + n);
43         n -= 1;
44         n |= n >> 16;
45         n |= n >> 8;
46         n |= n >> 4;
47         n |= n >> 2;
48         n |= n >> 1;
49         return n + 1;
50     }
51 
52     // Returns the previous power of two.
53     // Returns the input if it is already power of 2.
54     // Throws IllegalArgumentException if the input is <= 0
prevPowerOf2(int n)55     public static int prevPowerOf2(int n) {
56         if (n <= 0) throw new IllegalArgumentException();
57         return Integer.highestOneBit(n);
58     }
59 
60     // Returns the input value x clamped to the range [min, max].
clamp(int x, int min, int max)61     public static int clamp(int x, int min, int max) {
62         if (x > max) return max;
63         if (x < min) return min;
64         return x;
65     }
66 
ceilLog2(float value)67     public static int ceilLog2(float value) {
68         int i;
69         for (i = 0; i < 31; i++) {
70             if ((1 << i) >= value) break;
71         }
72         return i;
73     }
74 
floorLog2(float value)75     public static int floorLog2(float value) {
76         int i;
77         for (i = 0; i < 31; i++) {
78             if ((1 << i) > value) break;
79         }
80         return i - 1;
81     }
82 
closeSilently(Closeable c)83     public static void closeSilently(Closeable c) {
84         if (c == null) return;
85         try {
86             c.close();
87         } catch (IOException t) {
88             Log.w(TAG, "close fail ", t);
89         }
90     }
91 
closeSilently(ParcelFileDescriptor fd)92     public static void closeSilently(ParcelFileDescriptor fd) {
93         try {
94             if (fd != null) fd.close();
95         } catch (Throwable t) {
96             Log.w(TAG, "fail to close", t);
97         }
98     }
99 
closeSilently(Cursor cursor)100     public static void closeSilently(Cursor cursor) {
101         try {
102             if (cursor != null) cursor.close();
103         } catch (Throwable t) {
104             Log.w(TAG, "fail to close", t);
105         }
106     }
107 
getMaxCropRect( int inWidth, int inHeight, int outWidth, int outHeight, boolean leftAligned)108     public static RectF getMaxCropRect(
109             int inWidth, int inHeight, int outWidth, int outHeight, boolean leftAligned) {
110         RectF cropRect = new RectF();
111         // Get a crop rect that will fit this
112         if (inWidth / (float) inHeight > outWidth / (float) outHeight) {
113              cropRect.top = 0;
114              cropRect.bottom = inHeight;
115              cropRect.left = (inWidth - (outWidth / (float) outHeight) * inHeight) / 2;
116              cropRect.right = inWidth - cropRect.left;
117              if (leftAligned) {
118                  cropRect.right -= cropRect.left;
119                  cropRect.left = 0;
120              }
121         } else {
122             cropRect.left = 0;
123             cropRect.right = inWidth;
124             cropRect.top = (inHeight - (outHeight / (float) outWidth) * inWidth) / 2;
125             cropRect.bottom = inHeight - cropRect.top;
126         }
127         return cropRect;
128     }
129 
130     /**
131      * Find the min x that 1 / x >= scale
132      */
computeSampleSizeLarger(float scale)133     public static int computeSampleSizeLarger(float scale) {
134         int initialSize = (int) Math.floor(1f / scale);
135         if (initialSize <= 1) return 1;
136         return initialSize <= 8 ? prevPowerOf2(initialSize) : (initialSize / 8 * 8);
137     }
138 }
139