1 /* 2 * Copyright (C) 2009 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.cooliris.media; 18 19 import android.graphics.Color; 20 import android.media.ExifInterface; 21 22 public final class Shared { 23 // Constants. 24 public static final int INVALID = -1; 25 public static final int INFINITY = Integer.MAX_VALUE; 26 27 public static int argb(float a, float r, float g, float b) { 28 return Color.argb((int) (a * 255f), (int) (r * 255f), (int) (g * 255f), (int) (b * 255f)); 29 } 30 31 public static boolean isPowerOf2(int n) { 32 return (n & -n) == n; 33 } 34 35 /** 36 * @param i 37 * : running variable 38 * @return 0, +1, -1, +2, -2, +3, -3 .. 39 */ 40 public static int midPointIterator(int i) { 41 if (i != 0) { 42 int tick = ((i - 1) / 2) + 1; 43 int pass = ((i - 1) % 2 == 0) ? 1 : -1; 44 return tick * pass; 45 } 46 return 0; 47 } 48 49 public static int nextPowerOf2(int n) { 50 n -= 1; 51 n |= n >>> 16; 52 n |= n >>> 8; 53 n |= n >>> 4; 54 n |= n >>> 2; 55 n |= n >>> 1; 56 return n + 1; 57 } 58 59 public static int prevPowerOf2(int n) { 60 if (isPowerOf2(n)) { 61 return nextPowerOf2(n); 62 } else { 63 return nextPowerOf2(n) - 1; 64 } 65 } 66 67 public static int clamp(int value, int min, int max) { 68 if (value < min) { 69 value = min; 70 } else if (value > max) { 71 value = max; 72 } 73 return value; 74 } 75 76 public static long clamp(long value, long min, long max) { 77 if (value < min) { 78 value = min; 79 } else if (value > max) { 80 value = max; 81 } 82 return value; 83 } 84 85 public static float scaleToFit(float srcWidth, float srcHeight, float outerWidth, float outerHeight, boolean clipToFit) { 86 float scaleX = outerWidth / srcWidth; 87 float scaleY = outerHeight / srcHeight; 88 return (clipToFit ? scaleX > scaleY : scaleX < scaleY) ? scaleX : scaleY; 89 } 90 91 // Returns an angle between 0 and 360 degrees independent of the input 92 // angle. 93 public static float normalizePositive(float angleToRotate) { 94 if (angleToRotate == 0.0f) { 95 return 0.0f; 96 } 97 float nf = (angleToRotate / 360.0f); 98 int n = 0; 99 if (angleToRotate < 0) { 100 n = (int) (nf - 1.0f); 101 } else if (angleToRotate > 360) { 102 n = (int) (nf); 103 } 104 angleToRotate -= (n * 360.0f); 105 if (angleToRotate == 360.0f) { 106 angleToRotate = 0; 107 } 108 return angleToRotate; 109 } 110 111 public static int degreesToExifOrientation(float normalizedAngle) { 112 if (normalizedAngle == 0.0f) { 113 return ExifInterface.ORIENTATION_NORMAL; 114 } else if (normalizedAngle == 90.0f) { 115 return ExifInterface.ORIENTATION_ROTATE_90; 116 } else if (normalizedAngle == 180.0f) { 117 return ExifInterface.ORIENTATION_ROTATE_180; 118 } else if (normalizedAngle == 270.0f) { 119 return ExifInterface.ORIENTATION_ROTATE_270; 120 } 121 return ExifInterface.ORIENTATION_NORMAL; 122 } 123 124 public static float exifOrientationToDegrees(int exifOrientation) { 125 if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { 126 return 90; 127 } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { 128 return 180; 129 } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { 130 return 270; 131 } 132 return 0; 133 } 134 } 135