1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/gfx/IntSize.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,91 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.gfx; 1.10 + 1.11 +import org.json.JSONException; 1.12 +import org.json.JSONObject; 1.13 + 1.14 +import android.util.FloatMath; 1.15 + 1.16 +public class IntSize { 1.17 + public final int width, height; 1.18 + 1.19 + public IntSize(IntSize size) { width = size.width; height = size.height; } 1.20 + public IntSize(int inWidth, int inHeight) { width = inWidth; height = inHeight; } 1.21 + 1.22 + public IntSize(FloatSize size) { 1.23 + width = Math.round(size.width); 1.24 + height = Math.round(size.height); 1.25 + } 1.26 + 1.27 + public IntSize(JSONObject json) { 1.28 + try { 1.29 + width = json.getInt("width"); 1.30 + height = json.getInt("height"); 1.31 + } catch (JSONException e) { 1.32 + throw new RuntimeException(e); 1.33 + } 1.34 + } 1.35 + 1.36 + public int getArea() { 1.37 + return width * height; 1.38 + } 1.39 + 1.40 + public boolean equals(IntSize size) { 1.41 + return ((size.width == width) && (size.height == height)); 1.42 + } 1.43 + 1.44 + public boolean isPositive() { 1.45 + return (width > 0 && height > 0); 1.46 + } 1.47 + 1.48 + @Override 1.49 + public String toString() { return "(" + width + "," + height + ")"; } 1.50 + 1.51 + public IntSize scale(float factor) { 1.52 + return new IntSize(Math.round(width * factor), 1.53 + Math.round(height * factor)); 1.54 + } 1.55 + 1.56 + /* Returns the power of two that is greater than or equal to value */ 1.57 + public static int nextPowerOfTwo(int value) { 1.58 + // code taken from http://acius2.blogspot.com/2007/11/calculating-next-power-of-2.html 1.59 + if (0 == value--) { 1.60 + return 1; 1.61 + } 1.62 + value = (value >> 1) | value; 1.63 + value = (value >> 2) | value; 1.64 + value = (value >> 4) | value; 1.65 + value = (value >> 8) | value; 1.66 + value = (value >> 16) | value; 1.67 + return value + 1; 1.68 + } 1.69 + 1.70 + public IntSize nextPowerOfTwo() { 1.71 + return new IntSize(nextPowerOfTwo(width), nextPowerOfTwo(height)); 1.72 + } 1.73 + 1.74 + public static boolean isPowerOfTwo(int value) { 1.75 + if (value == 0) 1.76 + return false; 1.77 + return (value & (value - 1)) == 0; 1.78 + } 1.79 + 1.80 + public static int largestPowerOfTwoLessThan(float value) { 1.81 + int val = (int)FloatMath.floor(value); 1.82 + if (val <= 0) { 1.83 + throw new IllegalArgumentException("Error: value must be > 0"); 1.84 + } 1.85 + // keep dropping the least-significant set bits until only one is left 1.86 + int bestVal = val; 1.87 + while (val != 0) { 1.88 + bestVal = val; 1.89 + val &= (val - 1); 1.90 + } 1.91 + return bestVal; 1.92 + } 1.93 +} 1.94 +