mobile/android/base/gfx/IntSize.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.gfx;
michael@0 7
michael@0 8 import org.json.JSONException;
michael@0 9 import org.json.JSONObject;
michael@0 10
michael@0 11 import android.util.FloatMath;
michael@0 12
michael@0 13 public class IntSize {
michael@0 14 public final int width, height;
michael@0 15
michael@0 16 public IntSize(IntSize size) { width = size.width; height = size.height; }
michael@0 17 public IntSize(int inWidth, int inHeight) { width = inWidth; height = inHeight; }
michael@0 18
michael@0 19 public IntSize(FloatSize size) {
michael@0 20 width = Math.round(size.width);
michael@0 21 height = Math.round(size.height);
michael@0 22 }
michael@0 23
michael@0 24 public IntSize(JSONObject json) {
michael@0 25 try {
michael@0 26 width = json.getInt("width");
michael@0 27 height = json.getInt("height");
michael@0 28 } catch (JSONException e) {
michael@0 29 throw new RuntimeException(e);
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 public int getArea() {
michael@0 34 return width * height;
michael@0 35 }
michael@0 36
michael@0 37 public boolean equals(IntSize size) {
michael@0 38 return ((size.width == width) && (size.height == height));
michael@0 39 }
michael@0 40
michael@0 41 public boolean isPositive() {
michael@0 42 return (width > 0 && height > 0);
michael@0 43 }
michael@0 44
michael@0 45 @Override
michael@0 46 public String toString() { return "(" + width + "," + height + ")"; }
michael@0 47
michael@0 48 public IntSize scale(float factor) {
michael@0 49 return new IntSize(Math.round(width * factor),
michael@0 50 Math.round(height * factor));
michael@0 51 }
michael@0 52
michael@0 53 /* Returns the power of two that is greater than or equal to value */
michael@0 54 public static int nextPowerOfTwo(int value) {
michael@0 55 // code taken from http://acius2.blogspot.com/2007/11/calculating-next-power-of-2.html
michael@0 56 if (0 == value--) {
michael@0 57 return 1;
michael@0 58 }
michael@0 59 value = (value >> 1) | value;
michael@0 60 value = (value >> 2) | value;
michael@0 61 value = (value >> 4) | value;
michael@0 62 value = (value >> 8) | value;
michael@0 63 value = (value >> 16) | value;
michael@0 64 return value + 1;
michael@0 65 }
michael@0 66
michael@0 67 public IntSize nextPowerOfTwo() {
michael@0 68 return new IntSize(nextPowerOfTwo(width), nextPowerOfTwo(height));
michael@0 69 }
michael@0 70
michael@0 71 public static boolean isPowerOfTwo(int value) {
michael@0 72 if (value == 0)
michael@0 73 return false;
michael@0 74 return (value & (value - 1)) == 0;
michael@0 75 }
michael@0 76
michael@0 77 public static int largestPowerOfTwoLessThan(float value) {
michael@0 78 int val = (int)FloatMath.floor(value);
michael@0 79 if (val <= 0) {
michael@0 80 throw new IllegalArgumentException("Error: value must be > 0");
michael@0 81 }
michael@0 82 // keep dropping the least-significant set bits until only one is left
michael@0 83 int bestVal = val;
michael@0 84 while (val != 0) {
michael@0 85 bestVal = val;
michael@0 86 val &= (val - 1);
michael@0 87 }
michael@0 88 return bestVal;
michael@0 89 }
michael@0 90 }
michael@0 91

mercurial