michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.gfx; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: import android.util.FloatMath; michael@0: michael@0: public class IntSize { michael@0: public final int width, height; michael@0: michael@0: public IntSize(IntSize size) { width = size.width; height = size.height; } michael@0: public IntSize(int inWidth, int inHeight) { width = inWidth; height = inHeight; } michael@0: michael@0: public IntSize(FloatSize size) { michael@0: width = Math.round(size.width); michael@0: height = Math.round(size.height); michael@0: } michael@0: michael@0: public IntSize(JSONObject json) { michael@0: try { michael@0: width = json.getInt("width"); michael@0: height = json.getInt("height"); michael@0: } catch (JSONException e) { michael@0: throw new RuntimeException(e); michael@0: } michael@0: } michael@0: michael@0: public int getArea() { michael@0: return width * height; michael@0: } michael@0: michael@0: public boolean equals(IntSize size) { michael@0: return ((size.width == width) && (size.height == height)); michael@0: } michael@0: michael@0: public boolean isPositive() { michael@0: return (width > 0 && height > 0); michael@0: } michael@0: michael@0: @Override michael@0: public String toString() { return "(" + width + "," + height + ")"; } michael@0: michael@0: public IntSize scale(float factor) { michael@0: return new IntSize(Math.round(width * factor), michael@0: Math.round(height * factor)); michael@0: } michael@0: michael@0: /* Returns the power of two that is greater than or equal to value */ michael@0: public static int nextPowerOfTwo(int value) { michael@0: // code taken from http://acius2.blogspot.com/2007/11/calculating-next-power-of-2.html michael@0: if (0 == value--) { michael@0: return 1; michael@0: } michael@0: value = (value >> 1) | value; michael@0: value = (value >> 2) | value; michael@0: value = (value >> 4) | value; michael@0: value = (value >> 8) | value; michael@0: value = (value >> 16) | value; michael@0: return value + 1; michael@0: } michael@0: michael@0: public IntSize nextPowerOfTwo() { michael@0: return new IntSize(nextPowerOfTwo(width), nextPowerOfTwo(height)); michael@0: } michael@0: michael@0: public static boolean isPowerOfTwo(int value) { michael@0: if (value == 0) michael@0: return false; michael@0: return (value & (value - 1)) == 0; michael@0: } michael@0: michael@0: public static int largestPowerOfTwoLessThan(float value) { michael@0: int val = (int)FloatMath.floor(value); michael@0: if (val <= 0) { michael@0: throw new IllegalArgumentException("Error: value must be > 0"); michael@0: } michael@0: // keep dropping the least-significant set bits until only one is left michael@0: int bestVal = val; michael@0: while (val != 0) { michael@0: bestVal = val; michael@0: val &= (val - 1); michael@0: } michael@0: return bestVal; michael@0: } michael@0: } michael@0: